-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixelmatch_test.go
More file actions
387 lines (351 loc) · 10.2 KB
/
Copy pathpixelmatch_test.go
File metadata and controls
387 lines (351 loc) · 10.2 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package pixelmatch
import (
"bytes"
"encoding/base64"
"errors"
"image"
"image/color"
"image/png"
"os"
"path"
"testing"
"github.com/raf555/pixelmatch/internal/testutil"
)
func TestCompareIdentical(t *testing.T) {
w, h := 16, 16
pix := testutil.MakeRGBA(w, h, func(x, y int) (uint8, uint8, uint8, uint8) {
return uint8(x * 16), uint8(y * 16), 100, 255
})
img1 := testutil.BytesToNRGBA(pix, w, h)
img2 := testutil.BytesToNRGBA(pix, w, h)
out := image.NewNRGBA(image.Rect(0, 0, w, h))
n, err := Compare(img1, img2, WithOutput(out))
if err != nil {
t.Fatal(err)
}
if n != 0 {
t.Errorf("got %d, want 0", n)
}
}
func TestCompareNoOutput(t *testing.T) {
w, h := 8, 8
img1 := image.NewNRGBA(image.Rect(0, 0, w, h))
img2 := image.NewNRGBA(image.Rect(0, 0, w, h))
// Make img2 fully white, img1 fully zero — every pixel should differ.
for i := range img2.Pix {
img2.Pix[i] = 255
}
n, err := Compare(img1, img2)
if err != nil {
t.Fatal(err)
}
if n != w*h {
t.Errorf("got %d, want %d", n, w*h)
}
}
func TestCompareDimensionMismatch(t *testing.T) {
img1 := image.NewNRGBA(image.Rect(0, 0, 10, 10))
img2 := image.NewNRGBA(image.Rect(0, 0, 11, 10))
if _, err := Compare(img1, img2); err == nil {
t.Error("expected dimension mismatch error")
}
out := image.NewNRGBA(image.Rect(0, 0, 9, 9))
img3 := image.NewNRGBA(image.Rect(0, 0, 10, 10))
if _, err := Compare(img1, img3, WithOutput(out)); err == nil {
t.Error("expected output dimension mismatch error")
}
}
func TestCompareNilInputs(t *testing.T) {
if _, err := Compare(nil, nil); err == nil {
t.Error("expected nil-input error")
}
}
func TestCompareToImageReturnsDiff(t *testing.T) {
w, h := 4, 4
img1 := image.NewNRGBA(image.Rect(0, 0, w, h))
img2 := image.NewNRGBA(image.Rect(0, 0, w, h))
for i := range img1.Pix {
img1.Pix[i] = 0
}
for i := range img2.Pix {
img2.Pix[i] = 200
}
diff, n, err := CompareToImage(img1, img2)
if err != nil {
t.Fatal(err)
}
if diff == nil {
t.Fatal("diff is nil")
}
if n != w*h {
t.Errorf("got %d diffs, want %d", n, w*h)
}
if diff.Bounds().Dx() != w || diff.Bounds().Dy() != h {
t.Errorf("wrong diff bounds: %v", diff.Bounds())
}
}
func TestFunctionalOptions(t *testing.T) {
w, h := 4, 4
img1 := image.NewNRGBA(image.Rect(0, 0, w, h))
img2 := image.NewNRGBA(image.Rect(0, 0, w, h))
for i := range img1.Pix {
img1.Pix[i] = 100
}
for i := range img2.Pix {
img2.Pix[i] = 105 // very small difference
}
// Loose threshold → no diffs.
n, err := Compare(img1, img2, WithThreshold(0.5))
if err != nil {
t.Fatal(err)
}
if n != 0 {
t.Errorf("loose: got %d, want 0", n)
}
// Tight threshold → all pixels diff.
n, err = Compare(img1, img2, WithThreshold(0.0))
if err != nil {
t.Fatal(err)
}
if n != w*h {
t.Errorf("tight: got %d, want %d", n, w*h)
}
}
func TestWithDiffColorAlt(t *testing.T) {
w, h := 2, 1
img1 := image.NewNRGBA(image.Rect(0, 0, w, h))
img2 := image.NewNRGBA(image.Rect(0, 0, w, h))
// Pixel 0: img1 light, img2 dark → img2 darker → alt color.
img1.Set(0, 0, color.NRGBA{240, 240, 240, 255})
img2.Set(0, 0, color.NRGBA{20, 20, 20, 255})
// Pixel 1: img1 dark, img2 light → img2 lighter → diff color.
img1.Set(1, 0, color.NRGBA{20, 20, 20, 255})
img2.Set(1, 0, color.NRGBA{240, 240, 240, 255})
diff, _, err := CompareToImage(img1, img2,
WithDiffColor(255, 0, 0),
WithDiffColorAlt(0, 0, 255),
)
if err != nil {
t.Fatal(err)
}
p0 := diff.NRGBAAt(0, 0)
if p0.R != 0 || p0.G != 0 || p0.B != 255 {
t.Errorf("pixel 0: expected blue (alt), got %v", p0)
}
p1 := diff.NRGBAAt(1, 0)
if p1.R != 255 || p1.G != 0 || p1.B != 0 {
t.Errorf("pixel 1: expected red, got %v", p1)
}
}
// TestAsStraightRGBAFastPath verifies that NRGBA inputs with the standard
// layout don't trigger a copy.
func TestAsStraightRGBAFastPath(t *testing.T) {
img := image.NewNRGBA(image.Rect(0, 0, 10, 10))
for i := range img.Pix {
img.Pix[i] = byte(i)
}
pix := asStraightRGBA(img)
// Aliasing test: modify the source, verify the returned slice sees it.
img.Pix[0] = 99
if pix[0] != 99 {
t.Error("expected fast path to alias source NRGBA")
}
}
// TestAsStraightRGBAFromRGBA verifies premultiplied → straight conversion.
func TestAsStraightRGBAFromRGBA(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 1, 1))
// In premultiplied space: 50% red on 50% alpha → R=128, A=128 stored.
img.Set(0, 0, color.RGBA{128, 0, 0, 128})
pix := asStraightRGBA(img)
// After un-premultiply, R should be ~255 (full red at 50% alpha).
// (128 * 255 / 128 = 255, exact).
if pix[0] != 255 {
t.Errorf("expected R=255 after un-premultiply, got %d", pix[0])
}
if pix[3] != 128 {
t.Errorf("expected A=128 unchanged, got %d", pix[3])
}
}
// TestAsStraightRGBAFromOther verifies the generic fallback (Gray here).
func TestAsStraightRGBAFromGray(t *testing.T) {
img := image.NewGray(image.Rect(0, 0, 2, 1))
img.SetGray(0, 0, color.Gray{Y: 0}) // black
img.SetGray(1, 0, color.Gray{Y: 255}) // white
pix := asStraightRGBA(img)
if len(pix) != 8 {
t.Fatalf("wrong length: %d", len(pix))
}
// First pixel: black opaque.
if pix[0] != 0 || pix[1] != 0 || pix[2] != 0 || pix[3] != 255 {
t.Errorf("pixel 0: got [%d %d %d %d]", pix[0], pix[1], pix[2], pix[3])
}
// Second pixel: white opaque.
if pix[4] != 255 || pix[5] != 255 || pix[6] != 255 || pix[7] != 255 {
t.Errorf("pixel 1: got [%d %d %d %d]", pix[4], pix[5], pix[6], pix[7])
}
}
// TestCompareWithNonTightOutput verifies that an NRGBA with a non-zero
// origin or non-tight stride still gets the right diff written to it.
func TestCompareWithNonTightOutput(t *testing.T) {
w, h := 8, 8
img1 := image.NewNRGBA(image.Rect(0, 0, w, h))
img2 := image.NewNRGBA(image.Rect(0, 0, w, h))
for i := range img2.Pix {
img2.Pix[i] = 255 // all-white img2 vs all-zero img1
}
// Output with origin at (5, 5).
bigOut := image.NewNRGBA(image.Rect(5, 5, 5+w, 5+h))
n, err := Compare(img1, img2, WithOutput(bigOut))
if err != nil {
t.Fatal(err)
}
if n != w*h {
t.Errorf("got %d, want %d", n, w*h)
}
// Check the first diff pixel at the output's origin (5,5).
p := bigOut.NRGBAAt(5, 5)
if p.R != 255 || p.G != 0 || p.B != 0 || p.A != 255 {
t.Errorf("expected red diff at origin, got %v", p)
}
}
// TestCompareCrossValidatesAgainstByteAPI compares the high-level
// image.Image API against the low-level byte API for the JS reference
// cases, ensuring no precision loss in the wrapping.
func TestCompareCrossValidatesAgainstByteAPI(t *testing.T) {
cases, err := testutil.GetTestCases()
if err != nil {
t.Fatalf("read cases: %s", err.Error())
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
img1Pix, _ := base64.StdEncoding.DecodeString(c.Img1)
img2Pix, _ := base64.StdEncoding.DecodeString(c.Img2)
// Build opts the same way the byte test does.
var opts []Option
if c.Opts.Threshold != nil {
opts = append(opts, WithThreshold(*c.Opts.Threshold))
}
if c.Opts.IncludeAA != nil {
opts = append(opts, WithIncludeAA(*c.Opts.IncludeAA))
}
if c.Opts.Alpha != nil {
opts = append(opts, WithAlpha(*c.Opts.Alpha))
}
if c.Opts.DiffMask != nil {
opts = append(opts, WithDiffMask(*c.Opts.DiffMask))
}
if c.Opts.Checkerboard != nil {
opts = append(opts, WithCheckerboard(*c.Opts.Checkerboard))
}
if c.Opts.DiffColor != nil {
opts = append(opts, WithDiffColor(
uint8((*c.Opts.DiffColor)[0]),
uint8((*c.Opts.DiffColor)[1]),
uint8((*c.Opts.DiffColor)[2]),
))
}
if c.Opts.DiffColorAlt != nil {
opts = append(opts, WithDiffColorAlt(
uint8((*c.Opts.DiffColorAlt)[0]),
uint8((*c.Opts.DiffColorAlt)[1]),
uint8((*c.Opts.DiffColorAlt)[2]),
))
}
if c.Opts.AAColor != nil {
opts = append(opts, WithAAColor(
uint8((*c.Opts.AAColor)[0]),
uint8((*c.Opts.AAColor)[1]),
uint8((*c.Opts.AAColor)[2]),
))
}
img1 := testutil.BytesToNRGBA(img1Pix, c.W, c.H)
img2 := testutil.BytesToNRGBA(img2Pix, c.W, c.H)
diff, n, err := CompareToImage(img1, img2, opts...)
if err != nil {
t.Fatal(err)
}
if n != c.N {
t.Errorf("diff count: got %d, want %d", n, c.N)
}
wantDiff, _ := base64.StdEncoding.DecodeString(c.Diff)
if !bytes.Equal(diff.Pix, wantDiff) {
t.Errorf("diff buffer differs from JS reference")
}
})
}
}
// TestRoundTripPNG sanity-checks that decoded PNGs go through Compare
// correctly.
func TestRoundTripPNG(t *testing.T) {
w, h := 8, 8
src := image.NewNRGBA(image.Rect(0, 0, w, h))
for y := range h {
for x := range w {
src.Set(x, y, color.NRGBA{uint8(x * 32), uint8(y * 32), 128, 255})
}
}
// Encode and re-decode.
var buf bytes.Buffer
if err := png.Encode(&buf, src); err != nil {
t.Fatal(err)
}
decoded, err := png.Decode(&buf)
if err != nil {
t.Fatal(err)
}
// Decoded is *image.RGBA (since src is opaque). Compare against the
// original NRGBA — there should be zero differences.
n, err := Compare(src, decoded)
if err != nil {
t.Fatal(err)
}
if n != 0 {
t.Errorf("round-trip lost pixels: %d diffs", n)
}
}
func TestCompare(t *testing.T) {
type testcase struct {
fNamePrefix string
shouldMatch bool
}
for _, tc := range []testcase{
// orisano's fork testdata
// https://github.com/orisano/pixelmatch/blob/master/pixelmatch_test.go
{"01_matching", true},
{"02_nonmatching", false},
{"03_nonmatching_strideskip", false},
} {
t.Run(tc.fNamePrefix, func(t *testing.T) {
pathA := path.Join("internal/testutil/data", tc.fNamePrefix+"_a.png")
pathB := path.Join("internal/testutil/data", tc.fNamePrefix+"_b.png")
readpng := func(path string) image.Image {
f, err := os.Open(path)
if errors.Is(err, os.ErrNotExist) {
t.Fatalf("%s does not exist, please create it for this test to run: %v", path, err)
} else if err != nil {
t.Fatalf("open file err: %s", err.Error())
}
img, err := png.Decode(f)
if err != nil {
t.Fatalf("decode file err: %s", err.Error())
}
return img
}
imgA := readpng(pathA)
imgB := readpng(pathB)
res, err := Compare(imgA, imgB)
if err != nil {
t.Fatalf("compare err: %s", err.Error())
}
if tc.shouldMatch {
if res != 0 {
t.Errorf("should have been 0 diff, but was %d", res)
}
} else {
if res == 0 {
t.Errorf("should have been >0 diff, but was %d", res)
}
}
})
}
}