Skip to Content
New: codec-png-native and codec-png-wasm bring the blazediff-png codec to Node and WebAssembly, faster than spng and jSquash. Read more β†’
APIs@blazediff/ssim

@blazediff/ssim

Fast SSIM (Structural Similarity Index) implementations for perceptual image quality assessment. Includes standard SSIM, MS-SSIM (Multi-Scale SSIM), and Hitchhiker’s SSIM for various use cases and performance requirements.

Installation

npm install @blazediff/ssim

Features

  • Three variants - Standard SSIM, MS-SSIM, and Hitchhiker’s SSIM (~4x faster)
  • MATLAB-compatible - Standard SSIM matches reference implementation with <0.01% error
  • SSIM map output - Optional grayscale visualization of similarity

API Reference

All three functions take two images of the same size and one options object, and return a result object:

interface Image { data: Uint8Array | Uint8ClampedArray; // RGBA, width * height * 4 bytes. Buffer works too. width: number; height: number; } type SsimResult = | { kind: "compared"; score: number } // 0-1, 1 when identical | { kind: "dimension-mismatch"; a: Size; b: Size };
FunctionDescription
ssim(a, b, options?)Standard SSIM with a Gaussian window and automatic downsampling to about 256px on the short edge, like MATLAB’s ssim.m
msssim(a, b, options?)SSIM pooled across a dyadic pyramid, like msssim.m. The default five scales need at least 176px on the short edge
hitchhikersSSIM(a, b, options?)Box windows over integral images, pooled by coefficient of variation. About 4x faster than standard SSIM, using integral images for O(1) window computation

A buffer that does not hold width * height * 4 bytes, or an output of the wrong size, throws.

Options

SsimOptions is one type for all three functions, with the field names of @blazediff/ssim-native. Each function reads the shared fields and its own block.

OptionTypeDefaultDescription
windowSizenumber11Side of the local window
k1number0.01Luminance stability constant
k2number0.03Contrast stability constant
bitDepthnumber8Sample bit depth, setting L = 2^bitDepth - 1
outputImage-Draws the local score map as grayscale
msSsim.weightsnumber[][0.0448, 0.2856, 0.3001, 0.2363, 0.1333]msssim only. Per-scale weights. The length sets the number of scales
msSsim.method"product" | "weighted-sum""product"msssim only. How the per-scale scores combine
hitchhikers.windowStridenumberwindowSizehitchhikersSSIM only. Distance between window origins
hitchhikers.covPoolingbooleantruehitchhikersSSIM only. Coefficient of Variation pooling (recommended)

Score Interpretation

Score RangeSimilarity LevelDescription
1.0IdenticalImages are identical or perceptually identical
0.99+ExcellentExtremely high similarity (minor compression artifacts)
0.95-0.99Very GoodHigh similarity (small compression or noise)
0.90-0.95GoodNoticeable but acceptable differences
0.80-0.90FairSignificant but tolerable differences
<0.80PoorMajor structural differences

Threshold Guidelines: - Use threshold >0.99 for strict visual regression testing - Use threshold >0.95 for standard visual regression testing - Scores below 0.90 indicate substantial visual differences

Usage Examples

import ssim from "@blazediff/ssim/ssim"; // Basic usage const result = ssim(img1, img2); if (result.kind === "compared") console.log(result.score); // With SSIM map output const output = { data: new Uint8ClampedArray(width * height * 4), width, height }; ssim(img1, img2, { output }); // With custom options ssim(img1, img2, { windowSize: 8, k1: 0.01, k2: 0.03 });

CLI Usage

All three variants are available via the @blazediff/cli CLI:

# Standard SSIM blazediff-cli ssim image1.png image2.png # MS-SSIM blazediff-cli msssim image1.png image2.png # Hitchhiker's SSIM blazediff-cli hitchhikers-ssim image1.png image2.png # With options blazediff-cli hitchhikers-ssim image1.png image2.png --window-size 16 --no-cov-pooling
Last updated on