-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_test.go
More file actions
161 lines (136 loc) · 3.62 KB
/
render_test.go
File metadata and controls
161 lines (136 loc) · 3.62 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package dotbeam
import (
"image"
"strings"
"testing"
)
// matchColorFromImage finds the closest palette color for an RGB sample.
func matchColorFromImage(r, g, b uint8) uint8 {
bestIdx := uint8(0)
bestDist := 1<<31 - 1
for i, c := range DefaultColors {
dr := int(r) - int(c.R)
dg := int(g) - int(c.G)
db := int(b) - int(c.B)
d := dr*dr + dg*dg + db*db
if d < bestDist {
bestDist = d
bestIdx = uint8(i)
}
}
return bestIdx
}
// sampleDotFromImage reads the pixel at the dot's canvas position and
// returns a Dot with the matched palette Value.
func sampleDotFromImage(img *image.RGBA, dot Dot, width, height int) Dot {
px, py := ScaleToCanvas(dot.X, dot.Y, float64(width), float64(height))
x := int(px)
y := int(py)
// Clamp to image bounds
if x < 0 {
x = 0
}
if x >= width {
x = width - 1
}
if y < 0 {
y = 0
}
if y >= height {
y = height - 1
}
c := img.RGBAAt(x, y)
return Dot{
Ring: dot.Ring,
Index: dot.Index,
Value: matchColorFromImage(c.R, c.G, c.B),
X: dot.X,
Y: dot.Y,
}
}
func TestRenderRoundTrip(t *testing.T) {
msg := "Hello"
cfg := DefaultConfig()
enc := NewEncoder(cfg)
frames := enc.Encode([]byte(msg))
if len(frames) == 0 {
t.Fatal("encoder produced no frames")
}
layout := NewLayout(cfg, 1, 1) // normalized
const imgSize = 800
dec := NewDecoder(cfg)
for _, frame := range frames {
img := RenderFrame(frame, layout, imgSize, imgSize)
// Read dot colors back from the rendered image
var sampledDots []Dot
for _, dot := range frame.Dots {
sampledDots = append(sampledDots, sampleDotFromImage(img, dot, imgSize, imgSize))
}
done, err := dec.AddFrame(sampledDots)
if err != nil {
t.Fatalf("frame %d: AddFrame error: %v", frame.Index, err)
}
if frame.Index == len(frames)-1 && !done {
t.Fatalf("expected done after last frame")
}
}
data, err := dec.Data()
if err != nil {
t.Fatalf("Data() error: %v", err)
}
// Trim trailing null padding
got := strings.TrimRight(string(data), "\x00")
if got != msg {
t.Errorf("round-trip mismatch: got %q, want %q", got, msg)
}
}
func TestRenderRoundTripLonger(t *testing.T) {
msg := "The quick brown fox jumps over the lazy dog"
cfg := DefaultConfig()
enc := NewEncoder(cfg)
frames := enc.Encode([]byte(msg))
if len(frames) < 2 {
t.Fatalf("expected multiple frames for long message, got %d", len(frames))
}
layout := NewLayout(cfg, 1, 1)
const imgSize = 800
dec := NewDecoder(cfg)
for _, frame := range frames {
img := RenderFrame(frame, layout, imgSize, imgSize)
var sampledDots []Dot
for _, dot := range frame.Dots {
sampledDots = append(sampledDots, sampleDotFromImage(img, dot, imgSize, imgSize))
}
done, err := dec.AddFrame(sampledDots)
if err != nil {
t.Fatalf("frame %d: AddFrame error: %v", frame.Index, err)
}
if frame.Index == len(frames)-1 && !done {
t.Fatalf("expected done after last frame")
}
}
data, err := dec.Data()
if err != nil {
t.Fatalf("Data() error: %v", err)
}
got := strings.TrimRight(string(data), "\x00")
if got != msg {
t.Errorf("round-trip mismatch:\n got: %q\nwant: %q", got, msg)
}
}
func TestRenderFrameSize(t *testing.T) {
cfg := DefaultConfig()
enc := NewEncoder(cfg)
frames := enc.Encode([]byte("test"))
layout := NewLayout(cfg, 1, 1)
img := RenderFrame(frames[0], layout, 400, 400)
bounds := img.Bounds()
if bounds.Dx() != 400 || bounds.Dy() != 400 {
t.Errorf("image size = %dx%d, want 400x400", bounds.Dx(), bounds.Dy())
}
// Center pixel should be dark background
c := img.RGBAAt(200, 200)
if c.R > 20 || c.G > 20 || c.B > 40 {
t.Errorf("center pixel = (%d,%d,%d), expected dark background", c.R, c.G, c.B)
}
}