Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/diff/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("Compare image difference", () => {
Jimp.fromBitmap(makeTestImage("2468", "2468", "2468", "2468")),
Jimp.fromBitmap(makeTestImage("2212", "4434", "6656", "8878")),
Jimp.fromBitmap(makeTestImage("2232", "4454", "6676", "8898")),
Jimp.fromBitmap(makeTestImage("22322", "44544", "66766", "88988")),
] as const;

test("images 0 and 1", () => {
Expand Down Expand Up @@ -46,4 +47,10 @@ describe("Compare image difference", () => {
"threshold must be a number between 0 and 1"
);
});

test("should resize image before diffing", () => {
expect(diff(imgs[3], imgs[4]).percent).toStrictEqual(
diff(imgs[4], imgs[3]).percent
);
});
});
12 changes: 6 additions & 6 deletions packages/diff/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ import pixelMatch from "pixelmatch";
* ```
*/
export function diff<I extends JimpClass>(img1: I, img2: I, threshold = 0.1) {
const bmp1 = img1.bitmap;
const bmp2 = img2.bitmap;
let bmp1 = img1.bitmap;
let bmp2 = img2.bitmap;

if (bmp1.width !== bmp2.width || bmp1.height !== bmp2.height) {
if (bmp1.width * bmp1.height > bmp2.width * bmp2.height) {
// img1 is bigger
img1 = methods.resize(clone(img1), {
bmp1 = methods.resize(clone(img1), {
w: bmp2.width,
h: bmp2.height,
});
}).bitmap;
} else {
// img2 is bigger (or they are the same in area)
img2 = methods.resize(clone(img2), {
bmp2 = methods.resize(clone(img2), {
w: bmp1.width,
h: bmp1.height,
});
}).bitmap;
}
}

Expand Down