Skip to content

Commit 9828a2f

Browse files
fix diff order bug (#1311)
1 parent db18f76 commit 9828a2f

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

packages/diff/src/index.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe("Compare image difference", () => {
1313
Jimp.fromBitmap(makeTestImage("2468", "2468", "2468", "2468")),
1414
Jimp.fromBitmap(makeTestImage("2212", "4434", "6656", "8878")),
1515
Jimp.fromBitmap(makeTestImage("2232", "4454", "6676", "8898")),
16+
Jimp.fromBitmap(makeTestImage("22322", "44544", "66766", "88988")),
1617
] as const;
1718

1819
test("images 0 and 1", () => {
@@ -46,4 +47,10 @@ describe("Compare image difference", () => {
4647
"threshold must be a number between 0 and 1"
4748
);
4849
});
50+
51+
test("should resize image before diffing", () => {
52+
expect(diff(imgs[3], imgs[4]).percent).toStrictEqual(
53+
diff(imgs[4], imgs[3]).percent
54+
);
55+
});
4956
});

packages/diff/src/index.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ import pixelMatch from "pixelmatch";
2525
* ```
2626
*/
2727
export function diff<I extends JimpClass>(img1: I, img2: I, threshold = 0.1) {
28-
const bmp1 = img1.bitmap;
29-
const bmp2 = img2.bitmap;
28+
let bmp1 = img1.bitmap;
29+
let bmp2 = img2.bitmap;
3030

3131
if (bmp1.width !== bmp2.width || bmp1.height !== bmp2.height) {
3232
if (bmp1.width * bmp1.height > bmp2.width * bmp2.height) {
3333
// img1 is bigger
34-
img1 = methods.resize(clone(img1), {
34+
bmp1 = methods.resize(clone(img1), {
3535
w: bmp2.width,
3636
h: bmp2.height,
37-
});
37+
}).bitmap;
3838
} else {
3939
// img2 is bigger (or they are the same in area)
40-
img2 = methods.resize(clone(img2), {
40+
bmp2 = methods.resize(clone(img2), {
4141
w: bmp1.width,
4242
h: bmp1.height,
43-
});
43+
}).bitmap;
4444
}
4545
}
4646

0 commit comments

Comments
 (0)