@graphty/visual-review 0.0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,336 @@
1
+ /*
2
+ * pixelmatch 7.2.0 (https://github.com/mapbox/pixelmatch), vendored unchanged from index.js.
3
+ * Vendored so the trusted comparison has no install-time dependency. Upgrade by replacing the
4
+ * code below with a newer index.js and bumping the version above.
5
+ *
6
+ * ISC License
7
+ *
8
+ * Copyright (c) 2025, Mapbox
9
+ *
10
+ * Permission to use, copy, modify, and/or distribute this software for any purpose
11
+ * with or without fee is hereby granted, provided that the above copyright notice
12
+ * and this permission notice appear in all copies.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
15
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16
+ * FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
17
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
18
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
20
+ * THIS SOFTWARE.
21
+ */
22
+
23
+ /**
24
+ * Compare two equally sized images, pixel by pixel.
25
+ *
26
+ * @param {Uint8Array | Uint8ClampedArray} img1 First image data.
27
+ * @param {Uint8Array | Uint8ClampedArray} img2 Second image data.
28
+ * @param {Uint8Array | Uint8ClampedArray | void} output Image data to write the diff to, if provided.
29
+ * @param {number} width Input images width.
30
+ * @param {number} height Input images height.
31
+ *
32
+ * @param {Object} [options]
33
+ * @param {number} [options.threshold=0.1] Matching threshold (0 to 1); smaller is more sensitive.
34
+ * @param {boolean} [options.includeAA=false] Whether to skip anti-aliasing detection.
35
+ * @param {number} [options.alpha=0.1] Opacity of original image in diff output.
36
+ * @param {[number, number, number]} [options.aaColor=[255, 255, 0]] Color of anti-aliased pixels in diff output.
37
+ * @param {[number, number, number]} [options.diffColor=[255, 0, 0]] Color of different pixels in diff output.
38
+ * @param {[number, number, number]} [options.diffColorAlt=options.diffColor] Whether to detect dark on light differences between img1 and img2 and set an alternative color to differentiate between the two.
39
+ * @param {boolean} [options.diffMask=false] Draw the diff over a transparent background (a mask).
40
+ * @param {boolean} [options.checkerboard=true] Whether to blend semi-transparent pixels against a checkerboard pattern (true) or plain white (false) when comparing.
41
+ *
42
+ * @return {number} The number of mismatched pixels.
43
+ */
44
+ export default function pixelmatch(img1, img2, output, width, height, options = {}) {
45
+ const {
46
+ threshold = 0.1,
47
+ alpha = 0.1,
48
+ aaColor = [255, 255, 0],
49
+ diffColor = [255, 0, 0],
50
+ checkerboard = true,
51
+ includeAA, diffColorAlt, diffMask
52
+ } = options;
53
+
54
+ if (!isPixelData(img1) || !isPixelData(img2) || (output && !isPixelData(output)))
55
+ throw new Error('Image data: Uint8Array, Uint8ClampedArray or Buffer expected.');
56
+
57
+ if (img1.length !== img2.length || (output && output.length !== img1.length))
58
+ throw new Error(`Image sizes do not match. Image 1 size: ${img1.length}, image 2 size: ${img2.length}`);
59
+
60
+ if (img1.length !== width * height * 4) throw new Error(`Image data size does not match width/height. Expecting ${width * height * 4}. Got ${img1.length}`);
61
+
62
+ // check if images are identical
63
+ const len = width * height;
64
+ const a32 = new Uint32Array(img1.buffer, img1.byteOffset, len);
65
+ const b32 = new Uint32Array(img2.buffer, img2.byteOffset, len);
66
+ let identical = true;
67
+
68
+ for (let i = 0; i < len; i++) {
69
+ if (a32[i] !== b32[i]) { identical = false; break; }
70
+ }
71
+ if (identical) { // fast path if identical
72
+ if (output && !diffMask) {
73
+ for (let i = 0, pos = 0; i < len; i++, pos += 4) drawGrayPixel(img1, pos, alpha, output);
74
+ }
75
+ return 0;
76
+ }
77
+
78
+ // maximum acceptable square distance between two colors;
79
+ // 35215 is the maximum possible value for the YIQ difference metric
80
+ const maxDelta = 35215 * threshold * threshold;
81
+ const [aaR, aaG, aaB] = aaColor;
82
+ const [diffR, diffG, diffB] = diffColor;
83
+ const [altR, altG, altB] = diffColorAlt || diffColor;
84
+ let diff = 0;
85
+
86
+ // compare each pixel of one image against the other one
87
+ for (let i = 0, pos = 0; i < len; i++, pos += 4) {
88
+ // squared YUV distance between colors at this pixel position, negative if the img2 pixel is darker
89
+ const delta = a32[i] === b32[i] ? 0 : colorDelta(img1, img2, pos, pos, checkerboard);
90
+
91
+ // the color difference is above the threshold
92
+ if (Math.abs(delta) > maxDelta) {
93
+ const x = i % width;
94
+ const y = (i / width) | 0;
95
+ // check it's a real rendering difference or just anti-aliasing
96
+ const isExcludedAA = !includeAA && (antialiased(img1, x, y, width, height, a32, b32, checkerboard) || antialiased(img2, x, y, width, height, b32, a32, checkerboard));
97
+ if (isExcludedAA) {
98
+ // one of the pixels is anti-aliasing; draw as yellow and do not count as difference
99
+ // note that we do not include such pixels in a mask
100
+ if (output && !diffMask) drawPixel(output, pos, aaR, aaG, aaB);
101
+
102
+ } else {
103
+ // found substantial difference not caused by anti-aliasing; draw it as such
104
+ if (output) {
105
+ if (delta < 0) {
106
+ drawPixel(output, pos, altR, altG, altB);
107
+ } else {
108
+ drawPixel(output, pos, diffR, diffG, diffB);
109
+ }
110
+ }
111
+ diff++;
112
+ }
113
+
114
+ } else if (output && !diffMask) {
115
+ // pixels are similar; draw background as grayscale image blended with white
116
+ drawGrayPixel(img1, pos, alpha, output);
117
+ }
118
+ }
119
+
120
+ // return the number of different pixels
121
+ return diff;
122
+ }
123
+
124
+ /** @param {Uint8Array | Uint8ClampedArray} arr */
125
+ function isPixelData(arr) {
126
+ // work around instanceof Uint8Array not working properly in some Jest environments
127
+ return ArrayBuffer.isView(arr) && arr.BYTES_PER_ELEMENT === 1;
128
+ }
129
+
130
+ /**
131
+ * Check if a pixel is likely a part of anti-aliasing;
132
+ * based on "Anti-aliased Pixel and Intensity Slope Detector" paper by V. Vysniauskas, 2009
133
+ * @param {Uint8Array | Uint8ClampedArray} img
134
+ * @param {number} x1
135
+ * @param {number} y1
136
+ * @param {number} width
137
+ * @param {number} height
138
+ * @param {Uint32Array} a32
139
+ * @param {Uint32Array} b32
140
+ * @param {boolean} checkerboard
141
+ */
142
+ function antialiased(img, x1, y1, width, height, a32, b32, checkerboard) {
143
+ const x0 = Math.max(x1 - 1, 0);
144
+ const y0 = Math.max(y1 - 1, 0);
145
+ const x2 = Math.min(x1 + 1, width - 1);
146
+ const y2 = Math.min(y1 + 1, height - 1);
147
+ const pos4 = (y1 * width + x1) * 4;
148
+ // cache the center pixel's RGBA once instead of re-reading it on every neighbor comparison
149
+ const cr = img[pos4];
150
+ const cg = img[pos4 + 1];
151
+ const cb = img[pos4 + 2];
152
+ const ca = img[pos4 + 3];
153
+ let zeroes = x1 === x0 || x1 === x2 || y1 === y0 || y1 === y2 ? 1 : 0;
154
+ let min = 0;
155
+ let max = 0;
156
+ let minX = 0;
157
+ let minY = 0;
158
+ let maxX = 0;
159
+ let maxY = 0;
160
+
161
+ // go through 8 adjacent pixels
162
+ for (let x = x0; x <= x2; x++) {
163
+ for (let y = y0; y <= y2; y++) {
164
+ if (x === x1 && y === y1) continue;
165
+
166
+ // brightness delta between the center pixel and adjacent one
167
+ const delta = brightnessDelta(img, pos4, (y * width + x) * 4, cr, cg, cb, ca, checkerboard);
168
+
169
+ // count the number of equal, darker and brighter adjacent pixels
170
+ if (delta === 0) {
171
+ zeroes++;
172
+ // if found more than 2 equal siblings, it's definitely not anti-aliasing
173
+ if (zeroes > 2) return false;
174
+
175
+ // remember the darkest pixel
176
+ } else if (delta < min) {
177
+ min = delta;
178
+ minX = x;
179
+ minY = y;
180
+
181
+ // remember the brightest pixel
182
+ } else if (delta > max) {
183
+ max = delta;
184
+ maxX = x;
185
+ maxY = y;
186
+ }
187
+ }
188
+ }
189
+
190
+ // if there are no both darker and brighter pixels among siblings, it's not anti-aliasing
191
+ if (min === 0 || max === 0) return false;
192
+
193
+ // if either the darkest or the brightest pixel has 3+ equal siblings in both images
194
+ // (definitely not anti-aliased), this pixel is anti-aliased
195
+ return (hasManySiblings(a32, minX, minY, width, height) && hasManySiblings(b32, minX, minY, width, height)) ||
196
+ (hasManySiblings(a32, maxX, maxY, width, height) && hasManySiblings(b32, maxX, maxY, width, height));
197
+ }
198
+
199
+ /**
200
+ * Check if a pixel has 3+ adjacent pixels of the same color.
201
+ * @param {Uint32Array} img
202
+ * @param {number} x1
203
+ * @param {number} y1
204
+ * @param {number} width
205
+ * @param {number} height
206
+ */
207
+ function hasManySiblings(img, x1, y1, width, height) {
208
+ const x0 = Math.max(x1 - 1, 0);
209
+ const y0 = Math.max(y1 - 1, 0);
210
+ const x2 = Math.min(x1 + 1, width - 1);
211
+ const y2 = Math.min(y1 + 1, height - 1);
212
+ const val = img[y1 * width + x1];
213
+ let zeroes = x1 === x0 || x1 === x2 || y1 === y0 || y1 === y2 ? 1 : 0;
214
+
215
+ // go through 8 adjacent pixels
216
+ for (let x = x0; x <= x2; x++) {
217
+ for (let y = y0; y <= y2; y++) {
218
+ if (x === x1 && y === y1) continue;
219
+ zeroes += +(val === img[y * width + x]);
220
+ if (zeroes > 2) return true;
221
+ }
222
+ }
223
+ return false;
224
+ }
225
+
226
+ /**
227
+ * Calculate color difference according to the paper "Measuring perceived color difference
228
+ * using YIQ NTSC transmission color space in mobile applications" by Y. Kotsarenko and F. Ramos.
229
+ * Caller guarantees the two pixels differ, so the early-zero check is omitted.
230
+ * @param {Uint8Array | Uint8ClampedArray} img1
231
+ * @param {Uint8Array | Uint8ClampedArray} img2
232
+ * @param {number} k
233
+ * @param {number} m
234
+ * @param {boolean} checkerboard
235
+ */
236
+ function colorDelta(img1, img2, k, m, checkerboard) {
237
+ const r1 = img1[k];
238
+ const g1 = img1[k + 1];
239
+ const b1 = img1[k + 2];
240
+ const a1 = img1[k + 3];
241
+ const r2 = img2[m];
242
+ const g2 = img2[m + 1];
243
+ const b2 = img2[m + 2];
244
+ const a2 = img2[m + 3];
245
+
246
+ let dr = r1 - r2;
247
+ let dg = g1 - g2;
248
+ let db = b1 - b2;
249
+ const da = a1 - a2;
250
+
251
+ if (a1 < 255 || a2 < 255) { // blend pixels with background
252
+ let rb = 255, gb = 255, bb = 255;
253
+ if (checkerboard) {
254
+ rb = 48 + 159 * (k % 2);
255
+ gb = 48 + 159 * ((k / 1.618033988749895 | 0) % 2);
256
+ bb = 48 + 159 * ((k / 2.618033988749895 | 0) % 2);
257
+ }
258
+ dr = (r1 * a1 - r2 * a2 - rb * da) / 255;
259
+ dg = (g1 * a1 - g2 * a2 - gb * da) / 255;
260
+ db = (b1 * a1 - b2 * a2 - bb * da) / 255;
261
+ }
262
+
263
+ const y = dr * 0.29889531 + dg * 0.58662247 + db * 0.11448223;
264
+ const i = dr * 0.59597799 - dg * 0.27417610 - db * 0.32180189;
265
+ const q = dr * 0.21147017 - dg * 0.52261711 + db * 0.31114694;
266
+
267
+ const delta = 0.5053 * y * y + 0.299 * i * i + 0.1957 * q * q;
268
+
269
+ // encode whether the pixel lightens or darkens in the sign
270
+ return y > 0 ? -delta : delta;
271
+ }
272
+
273
+ /**
274
+ * Specialized brightness-only color delta for the anti-aliasing detector,
275
+ * with the center pixel's RGBA hoisted out of the neighbor loop.
276
+ * @param {Uint8Array | Uint8ClampedArray} img
277
+ * @param {number} k center pixel offset
278
+ * @param {number} m neighbor pixel offset
279
+ * @param {number} r1
280
+ * @param {number} g1
281
+ * @param {number} b1
282
+ * @param {number} a1
283
+ * @param {boolean} checkerboard
284
+ */
285
+ function brightnessDelta(img, k, m, r1, g1, b1, a1, checkerboard) {
286
+ const r2 = img[m];
287
+ const g2 = img[m + 1];
288
+ const b2 = img[m + 2];
289
+ const a2 = img[m + 3];
290
+
291
+ let dr = r1 - r2;
292
+ let dg = g1 - g2;
293
+ let db = b1 - b2;
294
+ const da = a1 - a2;
295
+
296
+ if (!dr && !dg && !db && !da) return 0;
297
+
298
+ if (a1 < 255 || a2 < 255) {
299
+ let rb = 255, gb = 255, bb = 255;
300
+ if (checkerboard) {
301
+ rb = 48 + 159 * (k % 2);
302
+ gb = 48 + 159 * ((k / 1.618033988749895 | 0) % 2);
303
+ bb = 48 + 159 * ((k / 2.618033988749895 | 0) % 2);
304
+ }
305
+ dr = (r1 * a1 - r2 * a2 - rb * da) / 255;
306
+ dg = (g1 * a1 - g2 * a2 - gb * da) / 255;
307
+ db = (b1 * a1 - b2 * a2 - bb * da) / 255;
308
+ }
309
+
310
+ return dr * 0.29889531 + dg * 0.58662247 + db * 0.11448223;
311
+ }
312
+
313
+ /**
314
+ * @param {Uint8Array | Uint8ClampedArray} output
315
+ * @param {number} pos
316
+ * @param {number} r
317
+ * @param {number} g
318
+ * @param {number} b
319
+ */
320
+ function drawPixel(output, pos, r, g, b) {
321
+ output[pos] = r;
322
+ output[pos + 1] = g;
323
+ output[pos + 2] = b;
324
+ output[pos + 3] = 255;
325
+ }
326
+
327
+ /**
328
+ * @param {Uint8Array | Uint8ClampedArray} img
329
+ * @param {number} i
330
+ * @param {number} alpha
331
+ * @param {Uint8Array | Uint8ClampedArray} output
332
+ */
333
+ function drawGrayPixel(img, i, alpha, output) {
334
+ const val = 255 + (img[i] * 0.29889531 + img[i + 1] * 0.58662247 + img[i + 2] * 0.11448223 - 255) * alpha * img[i + 3] / 255;
335
+ drawPixel(output, i, val, val, val);
336
+ }