Debian Codegraph @

ansi/sixel/sixel_test.go

raw
1 package sixel
2
3 import (
4 "bytes"
5 "image"
6 "image/color"
7 "testing"
8 )
9
10 func TestScanSize(t *testing.T) {
11 testCases := map[string]struct {
12 data string
13 expectedWidth int
14 expectedHeight int
15 }{
16 "two lines": {
17 "~~~~~~-~~~~~~-", 6, 12,
18 },
19 "two lines no newline at end": {
20 "~~~~~~-~~~~~~", 6, 12,
21 },
22 "no pixels": {
23 "", 0, 0,
24 },
25 "smaller carriage returns": {
26 "~$~~$~~~$~~~~$~~~~~$~~~~~~", 6, 6,
27 },
28 "transparent": {
29 "??????", 6, 6,
30 },
31 "RLE": {
32 "??!20?", 22, 6,
33 },
34 "Colors": {
35 "#0;2;0;0;0~~~~~$#1;2;100;100;100;~~~~~~-#0~~~~~~-#1~~~~~~", 6, 18,
36 },
37 }
38
39 for testName, testCase := range testCases {
40 t.Run(testName, func(t *testing.T) {
41 decoder := &Decoder{}
42 width, height := decoder.scanSize([]byte(testCase.data))
43 if width != testCase.expectedWidth {
44 t.Errorf("expected width of %d, but received width of %d", testCase.expectedWidth, width)
45 return
46 }
47
48 if height != testCase.expectedHeight {
49 t.Errorf("expected height of %d, but received height of %d", testCase.expectedHeight, height)
50 return
51 }
52 })
53 }
54 }
55
56 func TestFullImage(t *testing.T) {
57 testCases := map[string]struct {
58 imageWidth int
59 imageHeight int
60 bandCount int
61 // When filling the image, we'll use a map of indices to colors and change colors every
62 // time the current index is in the map- this will prevent dozens of lines with the same color
63 // in a row and make this slightly more legible
64 colors map[int]color.RGBA
65 }{
66 "3x12 single color filled": {
67 3, 12, 2,
68 map[int]color.RGBA{
69 0: {255, 0, 0, 255},
70 },
71 },
72 "3x12 two color filled": {
73 3, 12, 2,
74 map[int]color.RGBA{
75 // 3-pixel high alternating bands
76 0: {0, 0, 255, 255},
77 9: {0, 255, 0, 255},
78 18: {0, 0, 255, 255},
79 27: {0, 255, 0, 255},
80 },
81 },
82 "3x12 8 color with right gutter": {
83 3, 12, 2,
84 map[int]color.RGBA{
85 0: {255, 0, 0, 255},
86 2: {0, 255, 0, 255},
87 3: {255, 0, 0, 255},
88 5: {0, 255, 0, 255},
89 6: {255, 0, 0, 255},
90 8: {0, 255, 0, 255},
91 9: {0, 0, 255, 255},
92 11: {128, 128, 0, 255},
93 12: {0, 0, 255, 255},
94 14: {128, 128, 0, 255},
95 15: {0, 0, 255, 255},
96 17: {128, 128, 0, 255},
97 18: {0, 128, 128, 255},
98 20: {128, 0, 128, 255},
99 21: {0, 128, 128, 255},
100 23: {128, 0, 128, 255},
101 24: {0, 128, 128, 255},
102 26: {128, 0, 128, 255},
103 27: {64, 0, 0, 255},
104 29: {0, 64, 0, 255},
105 30: {64, 0, 0, 255},
106 32: {0, 64, 0, 255},
107 33: {64, 0, 0, 255},
108 35: {0, 64, 0, 255},
109 },
110 },
111 "3x12 single color with transparent band in the middle": {
112 3, 12, 2,
113 map[int]color.RGBA{
114 0: {255, 0, 0, 255},
115 15: {0, 0, 0, 0},
116 21: {255, 0, 0, 255},
117 },
118 },
119 "3x5 single color": {
120 3, 5, 1,
121 map[int]color.RGBA{
122 0: {255, 0, 0, 255},
123 },
124 },
125 "12x4 single color use RLE": {
126 12, 4, 1,
127 map[int]color.RGBA{
128 0: {255, 0, 0, 255},
129 },
130 },
131 "12x1 two color use RLE": {
132 12, 1, 1,
133 map[int]color.RGBA{
134 0: {255, 0, 0, 255},
135 6: {0, 255, 0, 255},
136 },
137 },
138 "12x12 single color use RLE": {
139 12, 12, 2,
140 map[int]color.RGBA{
141 0: {255, 0, 0, 255},
142 },
143 },
144 }
145
146 for testName, testCase := range testCases {
147 t.Run(testName, func(t *testing.T) {
148 img := image.NewRGBA(image.Rect(0, 0, testCase.imageWidth, testCase.imageHeight))
149
150 currentColor := color.RGBA{0, 0, 0, 0}
151 for y := range testCase.imageHeight {
152 for x := range testCase.imageWidth {
153 index := y*testCase.imageWidth + x
154 newColor, changingColor := testCase.colors[index]
155 if changingColor {
156 currentColor = newColor
157 }
158
159 img.Set(x, y, currentColor)
160 }
161 }
162
163 buffer := bytes.NewBuffer(nil)
164 encoder := Encoder{}
165 decoder := Decoder{}
166
167 err := encoder.Encode(buffer, img)
168 if err != nil {
169 t.Errorf("Unexpected error: %+v", err)
170 return
171 }
172
173 compareImg, err := decoder.Decode(buffer)
174 if err != nil {
175 t.Errorf("Unexpected error: %+v", err)
176 return
177 }
178
179 expectedWidth := img.Bounds().Dx()
180 expectedHeight := img.Bounds().Dy()
181 actualWidth := compareImg.Bounds().Dx()
182 actualHeight := compareImg.Bounds().Dy()
183
184 if actualHeight != expectedHeight {
185 t.Errorf("SixelImage had a height of %d, but a height of %d was expected", actualHeight, expectedHeight)
186 return
187 }
188 if actualWidth != expectedWidth {
189 t.Errorf("SixelImage had a width of %d, but a width of %d was expected", actualWidth, expectedWidth)
190 return
191 }
192
193 for y := range expectedHeight {
194 for x := range expectedWidth {
195 r, g, b, a := compareImg.At(x, y).RGBA()
196 expectedR, expectedG, expectedB, expectedA := img.At(x, y).RGBA()
197
198 if r != expectedR || g != expectedG || b != expectedB || a != expectedA {
199 t.Errorf("SixelImage had color (%d,%d,%d,%d) at coordinates (%d,%d), but color (%d,%d,%d,%d) was expected",
200 r, g, b, a, x, y, expectedR, expectedG, expectedB, expectedA)
201 return
202 }
203 }
204 }
205 })
206 }
207 }