function diffBgra(a, b, options = {}) { if (!a || !b || a.length !== b.length) { throw new Error(`bitmap size mismatch: ${a?.length || 0} != ${b?.length || 0}`) } const perChannelTolerance = Number.isFinite(options.perChannelTolerance) ? options.perChannelTolerance : 2 const maxMismatchedRatio = Number.isFinite(options.maxMismatchedRatio) ? options.maxMismatchedRatio : 0.001 let mismatchedPixels = 0 let maxDelta = 0 let totalDelta = 0 const pixels = a.length / 4 for (let i = 0; i < a.length; i += 4) { let pixelMismatch = false for (let c = 0; c < 4; c += 1) { const delta = Math.abs(a[i + c] - b[i + c]) maxDelta = Math.max(maxDelta, delta) totalDelta += delta if (delta > perChannelTolerance) pixelMismatch = true } if (pixelMismatch) mismatchedPixels += 1 } const mismatchedRatio = pixels ? mismatchedPixels / pixels : 0 return { ok: mismatchedRatio <= maxMismatchedRatio, pixels, mismatchedPixels, mismatchedRatio, maxDelta, meanDelta: pixels ? totalDelta / (pixels * 4) : 0, perChannelTolerance, maxMismatchedRatio, } } module.exports = { diffBgra }