Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Playwright SsimStats

From Leeroopedia

Template:Implementation Page

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:

  • ssim function -- computes the SSIM value for a rectangular region of two image channels using the standard SSIM formula with luminance, contrast, and structure components
  • FastStats class -- an optimized implementation of the Stats interface 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 ImageChannel instances 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

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment