1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
export default function pixelmatch(img1, img2, output, width, height, options = {}) { const { threshold = 0.1, alpha = 0.1, aaColor = [255, 255, 0], diffColor = [255, 0, 0], includeAA, diffColorAlt, diffMask } = options;
if (!isPixelData(img1) || !isPixelData(img2) || (output && !isPixelData(output))) throw new Error('图像数据:需要Uint8Array、Uint8ClampedArray或Buffer。');
if (img1.length !== img2.length || (output && output.length !== img1.length)) throw new Error('图像大小不匹配。');
if (img1.length !== width * height * 4) throw new Error('图像数据大小与宽度/高度不匹配。');
const len = width * height; const a32 = new Uint32Array(img1.buffer, img1.byteOffset, len); const b32 = new Uint32Array(img2.buffer, img2.byteOffset, len); let identical = true;
for (let i = 0; i < len; i++) { if (a32[i] !== b32[i]) { identical = false; break; } } if (identical) { if (output && !diffMask) { for (let i = 0; i < len; i++) drawGrayPixel(img1, 4 * i, alpha, output); } return 0; }
const maxDelta = 35215 * threshold * threshold; const [aaR, aaG, aaB] = aaColor; const [diffR, diffG, diffB] = diffColor; const [altR, altG, altB] = diffColorAlt || diffColor; let diff = 0;
for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) {
const i = y * width + x; const pos = i * 4;
const delta = a32[i] === b32[i] ? 0 : colorDelta(img1, img2, pos, pos, false);
if (Math.abs(delta) > maxDelta) { const isAA = antialiased(img1, x, y, width, height, a32, b32) || antialiased(img2, x, y, width, height, b32, a32); if (!includeAA && isAA) { if (output && !diffMask) drawPixel(output, pos, aaR, aaG, aaB);
} else { if (output) { if (delta < 0) { drawPixel(output, pos, altR, altG, altB); } else { drawPixel(output, pos, diffR, diffG, diffB); } } diff++; }
} else if (output && !diffMask) { drawGrayPixel(img1, pos, alpha, output); } } }
return diff; }
function isPixelData(arr) { return ArrayBuffer.isView(arr) && arr.BYTES_PER_ELEMENT === 1; }
function colorDelta(img1, img2, k, m, yOnly) { const r1 = img1[k]; const g1 = img1[k + 1]; const b1 = img1[k + 2]; const a1 = img1[k + 3];
const r2 = img2[m]; const g2 = img2[m + 1]; const b2 = img2[m + 2]; const a2 = img2[m + 3];
let dr = r1 - r2; let dg = g1 - g2; let db = b1 - b2; const da = a1 - a2;
if (!dr && !dg && !db && !da) return 0;
if (a1 < 255 || a2 < 255) { const rb = 48 + 159 * (k % 2); const gb = 48 + 159 * ((k / 1.618033988749895 | 0) % 2); const bb = 48 + 159 * ((k / 2.618033988749895 | 0) % 2); dr = (r1 * a1 - r2 * a2 - rb * da) / 255; dg = (g1 * a1 - g2 * a2 - gb * da) / 255; db = (b1 * a1 - b2 * a2 - bb * da) / 255; }
const y = dr * 0.29889531 + dg * 0.58662247 + db * 0.11448223;
if (yOnly) return y;
const i = dr * 0.59597799 - dg * 0.27417610 - db * 0.32180189; const q = dr * 0.21147017 - dg * 0.52261711 + db * 0.31114694;
const delta = 0.5053 * y * y + 0.299 * i * i + 0.1957 * q * q;
return y > 0 ? -delta : delta; }
function antialiased(img, x1, y1, width, height, a32, b32) { const x0 = Math.max(x1 - 1, 0); const y0 = Math.max(y1 - 1, 0); const x2 = Math.min(x1 + 1, width - 1); const y2 = Math.min(y1 + 1, height - 1); const pos = y1 * width + x1; let zeroes = x1 === x0 || x1 === x2 || y1 === y0 || y1 === y2 ? 1 : 0; let min = 0; let max = 0; let minX = 0; let minY = 0; let maxX = 0; let maxY = 0;
for (let x = x0; x <= x2; x++) { for (let y = y0; y <= y2; y++) { if (x === x1 && y === y1) continue;
const delta = colorDelta(img, img, pos * 4, (y * width + x) * 4, true);
if (delta === 0) { zeroes++; if (zeroes > 2) return false;
} else if (delta < min) { min = delta; minX = x; minY = y;
} else if (delta > max) { max = delta; maxX = x; maxY = y; } } }
if (min === 0 || max === 0) return false;
return (hasManySiblings(a32, minX, minY, width, height) && hasManySiblings(b32, minX, minY, width, height)) || (hasManySiblings(a32, maxX, maxY, width, height) && hasManySiblings(b32, maxX, maxY, width, height)); }
function hasManySiblings(img, x1, y1, width, height) { const x0 = Math.max(x1 - 1, 0); const y0 = Math.max(y1 - 1, 0); const x2 = Math.min(x1 + 1, width - 1); const y2 = Math.min(y1 + 1, height - 1); const val = img[y1 * width + x1]; let zeroes = x1 === x0 || x1 === x2 || y1 === y0 || y1 === y2 ? 1 : 0;
for (let x = x0; x <= x2; x++) { for (let y = y0; y <= y2; y++) { if (x === x1 && y === y1) continue; zeroes += +(val === img[y * width + x]); if (zeroes > 2) return true; } } return false; }
function drawPixel(output, pos, r, g, b) { output[pos + 0] = r; output[pos + 1] = g; output[pos + 2] = b; output[pos + 3] = 255; }
function drawGrayPixel(img, i, alpha, output) { const val = 255 + (img[i] * 0.29889531 + img[i + 1] * 0.58662247 + img[i + 2] * 0.11448223 - 255) * alpha * img[i + 3] / 255; drawPixel(output, i, val, val, val); }
|