MCPcopy
hub / github.com/jimp-dev/jimp / diff

Function diff

packages/diff/src/index.ts:27–71  ·  view source on GitHub ↗
(img1: I, img2: I, threshold = 0.1)

Source from the content-addressed store, hash-verified

25 * ```
26 */
27export function diff<I extends JimpClass>(img1: I, img2: I, threshold = 0.1) {
28 let bmp1 = img1.bitmap;
29 let bmp2 = img2.bitmap;
30
31 if (bmp1.width !== bmp2.width || bmp1.height !== bmp2.height) {
32 if (bmp1.width * bmp1.height > bmp2.width * bmp2.height) {
33 // img1 is bigger
34 bmp1 = methods.resize(clone(img1), {
35 w: bmp2.width,
36 h: bmp2.height,
37 }).bitmap;
38 } else {
39 // img2 is bigger (or they are the same in area)
40 bmp2 = methods.resize(clone(img2), {
41 w: bmp1.width,
42 h: bmp1.height,
43 }).bitmap;
44 }
45 }
46
47 if (typeof threshold !== "number" || threshold < 0 || threshold > 1) {
48 throw new Error("threshold must be a number between 0 and 1");
49 }
50
51 // eslint-disable-next-line @typescript-eslint/no-explicit-any
52 const diff = new (img1 as any).constructor({
53 width: bmp1.width,
54 height: bmp1.height,
55 color: 0xffffffff,
56 });
57
58 const numDiffPixels = pixelMatch(
59 bmp1.data,
60 bmp2.data,
61 diff.bitmap.data,
62 diff.bitmap.width,
63 diff.bitmap.height,
64 { threshold }
65 );
66
67 return {
68 percent: numDiffPixels / (diff.bitmap.width * diff.bitmap.height),
69 image: diff,
70 };
71}

Callers 1

index.test.tsFile · 0.85

Calls 2

cloneFunction · 0.90
resizeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…