(img1: I, img2: I, threshold = 0.1)
| 25 | * ``` |
| 26 | */ |
| 27 | export 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 | } |
no test coverage detected
searching dependent graphs…