Skip to main content

blazediff-ssim

Structural-similarity metrics in Rust: SSIM, MS-SSIM and Hitchhiker's SSIM, vectorised through one lane-generic SIMD layer and held to the reference MATLAB scripts through Octave. No dependencies, no threads, no runtime dispatch.

Why it exists

BlazeDiff compares screenshots for visual regression testing. A per-pixel diff answers "which pixels changed", which is the right question until anti-aliasing, font hinting or a codec's rounding moves a few thousand pixels by one unit and the run goes red for no reason a human would call a change.

SSIM answers the other question: how alike do these look. The catch is that "SSIM" names a family whose members disagree by more than their published formulas suggest, because the reference implementations differ in window placement, boundary handling and downsampling. So the contract here is not "we implemented the paper", it is land where MATLAB lands, to a stated tolerance, and keep landing there.

The metrics turned out to have no dependency on the diff engine at all, so they live in their own crate. blazediff consumes it; so can you.

What it does

  • ssim: Gaussian-windowed single-scale SSIM in 'valid' mode, with the automatic downsampling to ~256px on the short edge that MATLAB's ssim.m does. That downsample is why it is also the fastest of the three on large inputs.
  • ms_ssim: SSIM pooled across a 5-octave dyadic pyramid, per msssim.m. Product or weighted-sum pooling. Needs at least 176px on the short edge for the default five scales.
  • hitchhikers_ssim: box windows over five integral images, pooled by coefficient of variation (Venkataramanan et al. 2021). Every window sum is an O(1) summed-area-table lookup instead of ten 11-tap convolutions.
  • perceptual_ssim: the tunable variant. CIE L*a*b*, chroma weighting, chroma subsampling and mean-absolute-deviation pooling, each an independent knob. With PerceptualOptions::default() it reduces bit-identically to ms_ssim, which is what makes it usable as an ablation study rather than a second opinion.

Every metric returns a pooled score and the local map it was pooled from; render_map paints that map into an RGBA8 buffer as grayscale.

Usage

use blazediff_ssim::{ms_ssim, MsSsimOptions, Plane, Rgba8, SsimOptions};

let plane1 = Plane::from_rgba8(Rgba8::new(&rgba1, width, height))?;
let plane2 = Plane::from_rgba8(Rgba8::new(&rgba2, width, height))?;

let outcome = ms_ssim(
    &plane1,
    &plane2,
    &SsimOptions::default(),
    &MsSsimOptions::default(),
)?;
println!("{:.6}", outcome.score); // 1.0 means identical

Rgba8 is a borrowed view, so nothing is copied to call in. Decoding is the caller's problem: the crate takes RGBA8 bytes and has no I/O.

Python - blazediff-ssim

pip install blazediff-ssim

PyO3 bindings shipped as abi3-py38 wheels for CPython ≥ 3.8 (macOS, Linux manylinux, Windows; arm64 + x86_64). Built from this crate's python Cargo feature, which pulls in PNG/JPEG/QOI decoding so paths and encoded bytes work directly.

import blazediff_ssim as ssim

result = ssim.compare("expected.png", "actual.png", metric="ms-ssim")
print(result.score)  # 1.0 means identical

# Also: compare_buffers(bytes, bytes), compare_rgba(bytes, bytes, w, h),
# render_map(map, map_w, map_h, w, h) and metrics().

Every knob the Rust API exposes is a keyword argument: min_score, window_size, k1, k2, bit_depth, weights, method, window_stride, cov_pooling, color, chroma_weight, chroma_subsample, pooling and deviation_weight. Pass return_map=True to get the local scores back as little-endian float32 bytes — numpy.frombuffer(result.map, dtype="<f4").

Performance

Wall-clock on a 4K pair, decode included (decode is ~200 ms of each):

Metric 4K pair Why
ssim 320 ms MATLAB's automatic downsample shrinks the plane to ~256px before any convolution runs
hitchhikers-ssim 380 ms full resolution, but O(1) window sums
ms-ssim 480 ms full resolution at the finest of five scales

Against single-threaded dssim, ms_ssim runs about 1.9× faster. Two things bought that, neither of them threads:

One fused statistics pass. A scale needs five moments (µ1, µ2, σ1², σ2², σ12), which the textbook pipeline computes as eleven full-size intermediates. stats.rs streams them through a row ring buffer in a single pass instead. It is bit-identical to the unfused path by construction, and streaming_matches_the_unfused_pipeline_bit_for_bit is the test that keeps it that way.

Compile-time lane selection. Five kernel shapes carry nearly all the time, so each is written once against a SimdF32 trait and instantiated per ISA: NEON on aarch64, SSE2 on x86_64, simd128 on wasm32, a scalar fallback elsewhere. All are baseline for their target, so nothing dispatches inside a hot loop and there is no runtime feature detection.

Bit-exactness is the constraint, not an outcome

Tap-by-tap accumulation order is frozen to the @blazediff/ssim TypeScript port. That is a deliberate handcuff: the JS port is the one whose MATLAB agreement was measured, so matching its order means this crate inherits that agreement instead of drifting away from it by an unmeasured amount. Anything that would reassociate the sums, including some obvious-looking vectorisations, is out of bounds even when it is faster.

Two consequences worth knowing about:

  • cube_root replaces cbrtf in the Lab conversion, and is checked against libm across the whole L*a*b* domain rather than assumed equivalent.
  • FMA is used inside the vector body and deliberately not in the scalar tail, because the reference does not fuse either. Un-fusing the tail is a correctness fix, not a pessimisation.

MsSsimMethod::Product returns NaN when a scale's mean contrast-structure term goes negative. That takes globally anticorrelated content (an inverted image) rather than ordinary degradation, and both references degenerate the same way: the JS gives NaN, MATLAB gives a complex number. The behaviour is kept rather than papered over. WeightedSum stays finite throughout.

Verified

Layer Result
MATLAB ssim.m within 0.01% on three fixture pairs, 0.05% on the one where downsampling by 5 costs the most precision
MATLAB msssim.m within 0.05 absolute. The references pool 'valid' statistics where both ports pool symmetric 'same', so the gap is algorithmic, not numerical
TypeScript port all three metrics agree to within 5e-6, the only cross-port pin for hitchhikers-ssim, which has no MATLAB reference
Fused statistics bit-identical to the unfused eleven-buffer pipeline
cube_root exhaustive over ~67M f32 values across the Lab domain (--release --ignored)
Unit + integration tests 55 + 4

The MATLAB half shells out to Octave. Install it (brew install octave) and:

BLAZEDIFF_REQUIRE_OCTAVE=1 cargo test -p blazediff-ssim --test matlab_parity

Without Octave those tests report a skip and pass, so the default cargo test needs no toolchain beyond Rust. BLAZEDIFF_REQUIRE_OCTAVE=1 turns a missing Octave into a failure, which is what CI should set so parity cannot pass vacuously.

Caveats

All three shipped metrics reduce to luma, so a change carried entirely by chroma or by alpha is invisible to them. perceptual_ssim with ColorSpace::Lab and a non-zero chroma_weight sees colour; blazediff-ssim-benchmark measures what each knob is worth against dssim on KADID-10k.

Scores are pooled over a local map, so these metrics say how much two images differ, not where beyond the resolution of that map. For exact locations, use a pixel diff.

License

MIT. The algorithms are published research; attributions are in licenses/.

Metadata

Release files for blazediff-ssim 6.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for blazediff-ssim 6.2.1
File Actions
blazediff_ssim-6.2.1-cp38-abi3-win_arm64.whl CPython 3.8 abi3 Windows ARM64 Details for blazediff_ssim-6.2.1-cp38-abi3-win_arm64.whl
blazediff_ssim-6.2.1-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details for blazediff_ssim-6.2.1-cp38-abi3-win_amd64.whl
blazediff_ssim-6.2.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.8 abi3 Linux glibc 2.17+ x86-64 Details for blazediff_ssim-6.2.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
blazediff_ssim-6.2.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.8 abi3 Linux glibc 2.17+ ARM64 Details for blazediff_ssim-6.2.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
blazediff_ssim-6.2.1-cp38-abi3-macosx_11_0_arm64.whl CPython 3.8 abi3 macOS 11.0+ ARM64 Details for blazediff_ssim-6.2.1-cp38-abi3-macosx_11_0_arm64.whl
blazediff_ssim-6.2.1-cp38-abi3-macosx_10_12_x86_64.whl CPython 3.8 abi3 macOS 10.12+ x86-64 Details for blazediff_ssim-6.2.1-cp38-abi3-macosx_10_12_x86_64.whl

Total release size: 3.8 MB

Release files / blazediff_ssim-6.2.1-cp38-abi3-win_arm64.whl

Download URL blazediff_ssim-6.2.1-cp38-abi3-win_arm64.whl
Size 576.6 kB
Tags CPython 3.8 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
87f4d4537c05504c9e1a87b80aa011f78a90b399eb3f716a17eaa8b9c34cc7e4
BLAKE2b-256 checksum
How to use checksums
7b509733e2735207275b9244b463925400e407cad22f3da0a8bd59bde20ca2dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.16.0

Release files / blazediff_ssim-6.2.1-cp38-abi3-win_amd64.whl

Download URL blazediff_ssim-6.2.1-cp38-abi3-win_amd64.whl
Size 602.2 kB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
8edad9dafd0e04d656a026dbb27bf2f1173616642dcea14ee2b39420489d2f8e
BLAKE2b-256 checksum
How to use checksums
7d332b0db485bad74f4d0bb1fea01df3912bddff6bfefadeb18fd893bd94e77c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.16.0

Release files / blazediff_ssim-6.2.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL blazediff_ssim-6.2.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 696.0 kB
Tags CPython 3.8 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
0bb478c8f53c9192e7787878cf94c7f33d0c5af24a7b0b40c6d5617a482ae165
BLAKE2b-256 checksum
How to use checksums
ba97e974ef869b4c402f776dd691019b33ebdd55fa98be253f34224474cb0080
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.16.0

Release files / blazediff_ssim-6.2.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL blazediff_ssim-6.2.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 657.1 kB
Tags CPython 3.8 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
833350050eb6b0ba574520ba357f5b4cf045b3eddd765ee19694335d8ed89a88
BLAKE2b-256 checksum
How to use checksums
089d7bccc0f799ad33eedaf284f8ff1116af275627e747d27c3c1315b84bb4db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.16.0

Release files / blazediff_ssim-6.2.1-cp38-abi3-macosx_11_0_arm64.whl

Download URL blazediff_ssim-6.2.1-cp38-abi3-macosx_11_0_arm64.whl
Size 600.7 kB
Tags CPython 3.8 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ec4b735dda785a0e878429cc79a1562ff5105022c86db8e8d3f589e8e75d07bd
BLAKE2b-256 checksum
How to use checksums
cd9b905bb836956f4b0aa3cd7d46072df76f9f1cdef5226a3367404e74af8bb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.16.0

Release files / blazediff_ssim-6.2.1-cp38-abi3-macosx_10_12_x86_64.whl

Download URL blazediff_ssim-6.2.1-cp38-abi3-macosx_10_12_x86_64.whl
Size 627.4 kB
Tags CPython 3.8 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
0c0b9a29fd7648f6928f74695594ac6f1adf7f5790bb136c378d34951fd87066
BLAKE2b-256 checksum
How to use checksums
311438c2854a37fa259b50d954c5529b787c96e1366891c1b86ae3a3da662890
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.16.0

Security for blazediff-ssim 6.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page