Skip to content

Commit 0cbe435

Browse files
committed
validate ignoreMask as pixel data
1 parent ebd9a84 commit 0cbe435

3 files changed

Lines changed: 10 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Implements ideas from the following papers:
5252
- `diffMask` — Draw the diff over a transparent background (a mask), rather than over the original image. Will not draw anti-aliased pixels (if detected).
5353
- `checkerboard` — Blend semi-transparent pixels against a checkerboard pattern when comparing (`true`) rather than plain white (`false`), avoiding false matches between colors that only look alike over one background. `true` by default.
5454
- `windowSize` — If set to a finite number `N`, return the maximum number of differing pixels in any `N`×`N` sliding window instead of the total count (see below). `Infinity` by default.
55-
- `ignoreMask` — A `Uint8Array` with one value per pixel (`width * height` long); pixels with a non-zero value are excluded from the comparison and drawn as unchanged in the diff output. Not set by default.
55+
- `ignoreMask` — A `Uint8Array`, `Uint8ClampedArray` or `Buffer` with one value per pixel (`width * height` long); pixels with a non-zero value are excluded from the comparison and drawn as unchanged in the diff output. Not set by default.
5656

5757
Compares two images, writes the output diff and returns the number of mismatched pixels.
5858

‎index.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export default function pixelmatch(img1, img2, output, width, height, options =
3333
ignoreMask
3434
} = options;
3535

36-
if (!isPixelData(img1) || !isPixelData(img2) || (output && !isPixelData(output)))
36+
if (!isPixelData(img1) || !isPixelData(img2) || (output && !isPixelData(output)) ||
37+
(ignoreMask && !isPixelData(ignoreMask)))
3738
throw new Error('Image data: Uint8Array, Uint8ClampedArray or Buffer expected.');
3839

3940
if (img1.length !== img2.length || (output && output.length !== img1.length))

‎test/test.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ test('throws error if provided wrong image data format', () => {
163163
assert.throws(() => match(bad, arr, null, 20, 20), err);
164164
assert.throws(() => match(arr, bad, null, 20, 20), err);
165165
assert.throws(() => match(arr, arr, bad, 20, 20), err);
166+
assert.throws(() => match(arr, arr, null, 20, 20, {ignoreMask: new Array(20 * 20).fill(0)}), err);
167+
assert.throws(() => match(arr, arr, null, 20, 20, {ignoreMask: new Float32Array(20 * 20)}), err);
168+
});
169+
170+
test('throws error if ignore mask size does not match width and height', () => {
171+
const arr = new Uint8Array(4 * 20 * 20);
172+
assert.throws(() => match(arr, arr, null, 20, 20, {ignoreMask: new Uint8Array(20 * 20 * 4)}), 'Ignore mask size does not match');
166173
});
167174

168175
function diffTest(imgPath1, imgPath2, diffPath, options, expectedMismatch) {

0 commit comments

Comments
 (0)