-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
slicer.go
90 lines (74 loc) · 1.9 KB
/
slicer.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
package main
import (
"archive/zip"
"bytes"
"fmt"
"image"
"image/color"
"image/png"
"syscall/js"
)
func sliceShader(this js.Value, args []js.Value) interface{} {
clearLog()
logf("Starting slicing...")
// Create ZIP encoder:
var buf bytes.Buffer
w := zip.NewWriter(&buf)
img := renderSlice(0.0)
i := 0
filename := fmt.Sprintf("slices/out%04d.png", i)
f, err := w.Create(filename)
if err != nil {
logf("Unable to create file %q: %v", filename, err)
return nil
}
if err := png.Encode(f, img); err != nil {
logf("PNG encode: %v", err)
return nil
}
if err := w.Close(); err != nil {
logf("Unable to close ZIP: %v", err)
return nil
}
logf("Wrote %v bytes to ZIP file.", buf.Len())
img.Release()
js.Global().Call("saveAs", buf.Bytes(), "slices.zip")
return nil
}
func renderSlice(z float64) *imageBuf {
js.Global().Call("renderSliceToTexture", z)
pixelBuffer := js.Global().Call("getPixelBuffer")
logf("pixelBuffer=%v", pixelBuffer.Length())
b := &imageBuf{pb: pixelBuffer}
// b := &imageBuf{pb: js.TypedArrayOf([]uint8{})}
// ta := js.TypedArrayOf([]uint8{})
// b := &imageBuf{pb: pixelBuffer.ValueOf(ta), ta: ta}
// logf("pb: %v, ta: %v", b.pb.Type(), b.ta.Type())
// b := &imageBuf{}
// if n := js.CopyBytesToGo(b.pb, pixelBuffer); n != 4*512*512 {
// logf("Got %v bytes from pixelBuffer; want %v", 4*512*512)
// }
return b
}
type imageBuf struct {
// pb []byte
pb js.Value
// ta js.TypedArray
}
func (i *imageBuf) Release() {
// i.ta.Release()
}
func (i *imageBuf) At(x, y int) color.Color {
ind := 4 * ((y * 512) + x)
r := uint8(i.pb.Index(ind).Int())
g := uint8(i.pb.Index(ind + 1).Int())
b := uint8(i.pb.Index(ind + 2).Int())
a := uint8(i.pb.Index(ind + 3).Int())
return color.NRGBA{R: r, G: g, B: b, A: a}
}
func (i *imageBuf) Bounds() image.Rectangle {
return image.Rect(0, 0, 512, 512)
}
func (i *imageBuf) ColorModel() color.Model {
return color.NRGBAModel
}