Implementation:Microsoft Playwright SsimStats
Overview
SsimStats implements the SSIM (Structural Similarity Index Measure) statistical calculations with optimized partial sum tables for efficient windowed computations over image channels.
Description
This module provides:
ssimfunction -- computes the SSIM value for a rectangular region of two image channels using the standard SSIM formula with luminance, contrast, and structure componentsFastStatsclass -- an optimized implementation of theStatsinterface using partial (prefix) sums for O(1) windowed mean, variance, and covariance computations
The FastStats class precomputes five partial sum tables (sum of each channel, sum of squares of each, sum of products) enabling constant-time computation of statistics for any rectangular window, regardless of window size.
Usage
Used by the SSIM image comparison algorithm to efficiently compute structural similarity across sliding windows.
Code Reference
Source Location
packages/playwright-core/src/server/utils/image_tools/stats.ts (126 lines)
Signatures
export interface Stats {
c1: ImageChannel;
c2: ImageChannel;
meanC1(x1: number, y1: number, x2: number, y2: number): number;
meanC2(x1: number, y1: number, x2: number, y2: number): number;
varianceC1(x1: number, y1: number, x2: number, y2: number): number;
varianceC2(x1: number, y1: number, x2: number, y2: number): number;
covariance(x1: number, y1: number, x2: number, y2: number): number;
}
export function ssim(stats: Stats, x1: number, y1: number, x2: number, y2: number): number;
export class FastStats implements Stats {
constructor(c1: ImageChannel, c2: ImageChannel)
}
Import
import { FastStats, ssim } from './server/utils/image_tools/stats';
import type { Stats } from './server/utils/image_tools/stats';
I/O Contract
Inputs
- Two
ImageChannelinstances to compare - Rectangle coordinates (x1, y1, x2, y2) for windowed computation
Outputs
- SSIM value in range [-1, 1] where 1 means identical
- Mean, variance, and covariance values for specified regions
Constants
DYNAMIC_RANGE = 255(8-bit depth)- SSIM constants: c1 = (0.01 * 255)^2, c2 = (0.03 * 255)^2
Related Pages
- Microsoft_Playwright_ImageCompare -- SSIM comparison using these stats
- Microsoft_Playwright_ImageChannel -- Image channel data structure
- Microsoft_Playwright_ColorUtils -- Color utility functions