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.0

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.0
File Actions
blazediff_ssim-6.2.0-cp38-abi3-win_arm64.whl CPython 3.8 abi3 Windows ARM64 Details for blazediff_ssim-6.2.0-cp38-abi3-win_arm64.whl
blazediff_ssim-6.2.0-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details for blazediff_ssim-6.2.0-cp38-abi3-win_amd64.whl
blazediff_ssim-6.2.0-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.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
blazediff_ssim-6.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.8 abi3 Linux glibc 2.17+ ARM64 Details for blazediff_ssim-6.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
blazediff_ssim-6.2.0-cp38-abi3-macosx_11_0_arm64.whl CPython 3.8 abi3 macOS 11.0+ ARM64 Details for blazediff_ssim-6.2.0-cp38-abi3-macosx_11_0_arm64.whl
blazediff_ssim-6.2.0-cp38-abi3-macosx_10_12_x86_64.whl CPython 3.8 abi3 macOS 10.12+ x86-64 Details for blazediff_ssim-6.2.0-cp38-abi3-macosx_10_12_x86_64.whl

Total release size: 3.8 MB

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

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

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

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

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

Download URL blazediff_ssim-6.2.0-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
410dfe68b2470710f5783b927904337fd554c8ca042c6bb3935ab9173a3a5fd2
BLAKE2b-256 checksum
How to use checksums
f0eaecced1897af6187cec08a513336617e7da349a89f8461b4c041a5de73233
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.16.0

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

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

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

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

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

Download URL blazediff_ssim-6.2.0-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
82640e7140fdedd67b69982bc25df88fe76b2f22c804e56357787bbcbf2dab9a
BLAKE2b-256 checksum
How to use checksums
82758a42203835bad65a874e449369dc22a8377b2834d02f220feccdf6ed4e1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.16.0

Security for blazediff-ssim 6.2.0

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