-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.go
More file actions
55 lines (44 loc) · 1.19 KB
/
canvas.go
File metadata and controls
55 lines (44 loc) · 1.19 KB
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
package rasterizer
import (
"fmt"
"image"
"image/png"
"io"
"math"
)
type Canvas struct {
width int
height int
canvas *image.NRGBA
zbuffer []float64
label_face *fontFace
fixture_labels []fixtureLabel
fixture_zbuf []bool // used for label placement to search for "free" pixels
}
func (cv *Canvas) Init(canvasConfig CanvasConfig) error {
cv.width = canvasConfig.Width
cv.height = canvasConfig.Height
cv.canvas = image.NewNRGBA(image.Rect(0, 0, canvasConfig.Width, canvasConfig.Height))
cv.fixture_zbuf = make([]bool, canvasConfig.Height*canvasConfig.Width)
cv.zbuffer = make([]float64, canvasConfig.Height*canvasConfig.Width)
for i := range cv.zbuffer {
cv.zbuffer[i] = math.Inf(-1)
}
var err error
cv.label_face, err = initFontFace(canvasConfig.LabelFontSize, canvasConfig.LabelDPI, canvasConfig.LabelFont)
if err != nil {
return err
}
initFontDrawer(cv.label_face, cv.canvas, 0, 0)
return nil
}
func (cv *Canvas) SaveAsPNG(w io.Writer) error {
encoder := png.Encoder{
CompressionLevel: png.DefaultCompression,
}
err := encoder.Encode(w, cv.canvas)
if err != nil {
return fmt.Errorf("Error Saving Canvas as PNG: %s", err)
}
return nil
}