-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_test.go
More file actions
214 lines (186 loc) · 5.09 KB
/
encode_test.go
File metadata and controls
214 lines (186 loc) · 5.09 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package xtx
import (
"bytes"
"image"
"image/color"
"testing"
)
func TestEncodeDecodeXTG_RoundTrip(t *testing.T) {
// Create a known monochrome image.
img := NewMonochrome(image.Rect(0, 0, 16, 8))
// Set a stripe pattern: even rows white, odd rows black.
for y := 0; y < 8; y++ {
for x := 0; x < 16; x++ {
if y%2 == 0 {
img.SetMono(x, y, MonoWhite)
}
}
}
// Encode.
var buf bytes.Buffer
if err := Encode(&buf, img, nil); err != nil {
t.Fatalf("Encode XTG: %v", err)
}
// Verify size: 22-byte header + 2 bytes/row × 8 rows = 38 bytes.
if buf.Len() != 22+16 {
t.Fatalf("encoded size = %d, want %d", buf.Len(), 22+16)
}
// Decode.
decoded, err := Decode(bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatalf("Decode: %v", err)
}
mono, ok := decoded.(*Monochrome)
if !ok {
t.Fatalf("decoded type = %T, want *Monochrome", decoded)
}
// Verify pixel-exact match.
for y := 0; y < 8; y++ {
for x := 0; x < 16; x++ {
want := img.MonoAt(x, y)
got := mono.MonoAt(x, y)
if got != want {
t.Errorf("pixel (%d,%d): got %v, want %v", x, y, got, want)
}
}
}
}
func TestEncodeDecodeXTH_RoundTrip(t *testing.T) {
// Create a 4×8 grayscale with all four levels.
img := NewGrayscale(image.Rect(0, 0, 4, 8))
for y := 0; y < 8; y++ {
for x := 0; x < 4; x++ {
img.SetGray4(x, y, Gray4{V: uint8(x % 4)})
}
}
var buf bytes.Buffer
if err := Encode(&buf, img, &Options{Format: FormatGrayscale}); err != nil {
t.Fatalf("Encode XTH: %v", err)
}
decoded, err := Decode(bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatalf("Decode: %v", err)
}
gray, ok := decoded.(*Grayscale)
if !ok {
t.Fatalf("decoded type = %T, want *Grayscale", decoded)
}
for y := 0; y < 8; y++ {
for x := 0; x < 4; x++ {
want := img.Gray4At(x, y)
got := gray.Gray4At(x, y)
if got != want {
t.Errorf("pixel (%d,%d): got %d, want %d", x, y, got.V, want.V)
}
}
}
}
func TestEncode_FromStdlibImage(t *testing.T) {
// Encode a standard *image.Gray as XTG (monochrome).
src := image.NewGray(image.Rect(0, 0, 8, 8))
// Set left half to white, right half to black.
for y := 0; y < 8; y++ {
for x := 0; x < 8; x++ {
if x < 4 {
src.SetGray(x, y, color.Gray{Y: 255})
}
}
}
var buf bytes.Buffer
if err := Encode(&buf, src, nil); err != nil {
t.Fatalf("Encode: %v", err)
}
decoded, err := Decode(bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatalf("Decode: %v", err)
}
mono := decoded.(*Monochrome)
for y := 0; y < 8; y++ {
for x := 0; x < 8; x++ {
wantWhite := x < 4
got := mono.MonoAt(x, y)
if got.V != wantWhite {
t.Errorf("pixel (%d,%d) = %v, want white=%v", x, y, got.V, wantWhite)
}
}
}
}
func TestEncode_FormatGrayscale(t *testing.T) {
// Encode a standard image.Gray as XTH via Options.
src := image.NewGray(image.Rect(0, 0, 4, 4))
src.SetGray(0, 0, color.Gray{Y: 255}) // white → level 0
src.SetGray(1, 0, color.Gray{Y: 85}) // dark grey → level 1
src.SetGray(2, 0, color.Gray{Y: 170}) // light grey → level 2
src.SetGray(3, 0, color.Gray{Y: 0}) // black → level 3
var buf bytes.Buffer
err := Encode(&buf, src, &Options{Format: FormatGrayscale})
if err != nil {
t.Fatalf("Encode XTH: %v", err)
}
decoded, err := Decode(bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatalf("Decode: %v", err)
}
gray := decoded.(*Grayscale)
wantLevels := []uint8{0, 1, 2, 3}
for x, want := range wantLevels {
got := gray.Gray4At(x, 0)
if got.V != want {
t.Errorf("pixel (%d,0) = %d, want %d", x, got.V, want)
}
}
}
func TestEncode_WithDither(t *testing.T) {
// Verify that dithering produces a valid output (doesn't crash).
src := image.NewGray(image.Rect(0, 0, 32, 32))
// Fill with a gradient.
for y := 0; y < 32; y++ {
for x := 0; x < 32; x++ {
src.SetGray(x, y, color.Gray{Y: uint8(x * 8)})
}
}
var buf bytes.Buffer
err := Encode(&buf, src, &Options{Ditherer: Dither{Algo: DitherFloydSteinberg}})
if err != nil {
t.Fatalf("Encode with dither: %v", err)
}
decoded, err := Decode(bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatalf("Decode: %v", err)
}
if decoded.Bounds().Dx() != 32 || decoded.Bounds().Dy() != 32 {
t.Errorf("decoded size = %v, want 32×32", decoded.Bounds().Size())
}
}
func TestDecodeConfig(t *testing.T) {
img := NewMonochrome(image.Rect(0, 0, 480, 800))
var buf bytes.Buffer
Encode(&buf, img, nil)
cfg, err := DecodeConfig(bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatalf("DecodeConfig: %v", err)
}
if cfg.Width != 480 || cfg.Height != 800 {
t.Errorf("config = %dx%d, want 480x800", cfg.Width, cfg.Height)
}
}
func TestEncode_1x1(t *testing.T) {
img := NewMonochrome(image.Rect(0, 0, 1, 1))
img.SetMono(0, 0, MonoWhite)
var buf bytes.Buffer
if err := Encode(&buf, img, nil); err != nil {
t.Fatalf("Encode 1x1: %v", err)
}
if buf.Len() != 22+1 {
t.Errorf("1x1 encoded size = %d, want 23", buf.Len())
}
}
func TestEncode_DimensionErrors(t *testing.T) {
// Zero dimensions.
src := image.NewGray(image.Rect(0, 0, 0, 0))
var buf bytes.Buffer
err := Encode(&buf, src, nil)
if err != ErrDimensionZero {
t.Errorf("zero dim: got %v, want ErrDimensionZero", err)
}
}