This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.go
executable file
·99 lines (79 loc) · 1.89 KB
/
write.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"log"
"unsafe"
"github.com/fogleman/gg"
)
func writeVox(v *vox, out string, extrude bool) {
sz := v.sizes[0]
w := int(sz.z * sz.x)
h := int(sz.y)
var palette []uint32
if v.palette == nil {
palette = defPalette
log.Printf("\t\tusing def palette")
} else {
palette = v.palette.colors[:]
log.Printf("\t\tusing custom palette")
}
for i := range v.voxels {
// if we want to extrude, we need to pad EACH FRAME.
// that means on the boundaries, for example:
//
// frame = 0; position is from x[0,32] and y[0,32].
// we want this to be a 33x33.
//
// so each context is now:
// w = w + n-frames
// h = h + 1
// as we write frames X->.
vv := v.voxels[i]
w, h := w, h
if extrude {
w, h = w+int(sz.z), h+1
}
dc := gg.NewContext(w, h)
log.Printf("%d points to draw", len(vv.voxels))
for j := range vv.voxels { // this is _not_ a frame
vvv := vv.voxels[j]
x := int(vvv.z)*int(sz.x) +
int(vvv.x)
y := int(vvv.y)
// because we're cloning X at intervals,
// we need to offset X by something.
if extrude {
x += (x / int(sz.z)) % int(sz.z)
}
idx := int(vvv.colorIndex)
pAt := (*[4]byte)(unsafe.Pointer(&palette[idx]))[:]
dc.SetRGBA255(
int(pAt[0]),
int(pAt[1]),
int(pAt[2]),
int(pAt[3]),
)
dc.SetPixel(x, y)
}
if extrude {
img := dc.Image()
// clone across interval'd X down to Y
for x := 0; x < int(sz.x)*int(sz.z); x += int(sz.x) {
// x is offsetted earlier
truex := x + ((x / int(sz.x)) % int(sz.x))
for y := 0; y < int(sz.y); y++ {
cat := img.At(truex+7, y)
dc.SetColor(cat)
dc.SetPixel(truex+8, y)
}
}
// clone across X,MAX(Y)
for x := 0; x < int(sz.x)*int(sz.z)+int(sz.z); x++ {
cat := img.At(x, int(sz.y)-1)
dc.SetColor(cat)
dc.SetPixel(x, int(sz.y))
}
}
dc.SavePNG(fmt.Sprintf("%s_%d.png", out, i))
}
}