@blazediff/core
v2.0.3
Published
Blazing-fast pixel-by-pixel image comparison. About 7x faster than pixelmatch, with the same output
Maintainers
Readme
@blazediff/core
High-performance pixel-by-pixel image comparison with a chunked, branch-free scan and YIQ perceptual color difference. About 7x faster than pixelmatch in total time across the benchmark fixtures, with the same counts and output bytes.
Features:
- YIQ color space for perceptual color difference
- Anti-aliasing detection and filtering
- Chunked scan that skips identical and provably sub-threshold pixels with 32-bit integer tests
- Support for alpha channel and transparency
For detailed algorithm explanation and mathematical formulas, see FORMULA.md.
Installation
npm install @blazediff/coreAPI
diff(a, b, options?)
Compares two RGBA images and returns a result object. a and b are { data, width, height }, where data is a Uint8Array, Uint8ClampedArray or Buffer of width * height * 4 bytes. Browser ImageData has this shape.
The result is one of two kinds:
type DiffResult =
| { kind: "compared"; count: number; percentage: number; identical: boolean;
window?: { x: number; y: number; count: number } }
| { kind: "dimension-mismatch"; a: Size; b: Size };count is the number of pixels that differ beyond threshold, after the mask and the anti-aliasing exclusion. With window, count stays the total and window gives the top-left corner and count of the busiest window. Images of different sizes return dimension-mismatch. Invalid buffers or options throw a BlazeDiffError with a code: invalid-image, invalid-output, invalid-mask or invalid-option.
Usage
import { diff } from "@blazediff/core";
const output = { data: new Uint8Array(width * height * 4), width, height };
const result = diff(a, b, { threshold: 0.1, output });
if (result.kind === "compared" && !result.identical) {
console.log(`${result.count} pixels differ (${result.percentage.toFixed(2)}%)`);
}
// OKLab metric, a skipped region and the busiest 16×16 window.
const mask = new Uint8Array(width * height); // 1 = skip this pixel
const worst = diff(a, b, { metric: "oklab", mask, window: 16 });Coming from pixelmatch
@blazediff/core/pixelmatch has the same signature, options, errors, counts and output bytes as pixelmatch 8.0.0. Change the import and nothing else:
import pixelmatch from "@blazediff/core/pixelmatch";
const count = pixelmatch(img1.data, img2.data, output, width, height, { threshold: 0.1 });Algorithm
The default YIQ diff walks the image in chunks of 4096 pixels. Both inputs of a chunk fit the L1 cache. Each chunk narrows the work in steps:
- Identical chunks: In Node, a native
Buffercompare skips chunks whose bytes are equal. - Dirty groups: Groups of 4 pixels are tested as 32-bit words. A first pass drops the groups whose words agree on every bit that can matter at the threshold. A second pass keeps the groups with a pixel over the eigenvalue bound or a pixel that is not opaque.
- Candidates: The full YIQ metric runs on the dirty groups only. It is branch-free, because most differing pixels are noise and noise defeats branch prediction.
- Anti-aliasing: Only pixels over the threshold take the Vysniauskas (2009) check. Interior opaque pixels use exact integer luma keys.
- Output: The gray background comes from an exact integer-luma lookup table, with flat runs reused and long runs passed to the native
fill. The few diff pixels are then drawn over it.
Steps 2 and 3 have no data-dependent branch, and the scratch lists are allocated once. OKLab, mask and window take separate loops that match pixelmatch 8.0.0. Counts and output bytes are the same as the previous release.
See FORMULA.md for detailed mathematical formulas and algorithm explanation.
Performance
Total: ~7.4x faster | Average: ~79% | Median: ~86% | With an output image: ~4.2x faster in total, ~66% average
Benchmarked against pixelmatch across 34 fixtures (50 iterations, 5 warmup) on Apple M1 Max with Node.js 22, count only. Full numbers in the root BENCHMARKS.md.
| Benchmark | Pixelmatch | BlazeDiff | Improvement | |-----------|------------|-----------|-------------| | 4k/1 (5600×3200) | 333.85ms | 41.73ms | 87.5% | | 4k/1 (identical) | 23.22ms | 3.31ms | 85.7% | | page/1 (3598×16384) | 148.48ms | 14.90ms | 90.0% | | page/1 (identical) | 69.44ms | 9.63ms | 86.1% | | small images | 0.07-12ms | 0.01-4.1ms | 38-88% |
The largest gains are on large images, where most pixels are identical or below the threshold. Small images with dense diffs gain the least.
References
- Algorithm Documentation: FORMULA.md: mathematical foundation and formulas
- YIQ Color Space: Kotsarenko & Ramos (2009) - "Measuring perceived color difference using YIQ NTSC transmission color space"
- Anti-Aliasing Detection: Vysniauskas (2009) - "Anti-aliased Pixel and Intensity Slope Detector"
- Inspiration: pixelmatch - Original pixel-by-pixel diff algorithm
