ODiff
repository·main·Indexed 25 days ago
https://github.com/dmtrkovalenko/odiffA high-performance, native image comparison tool for pixel-by-pixel visual regression testing, optimized with SIMD (SSE2, AVX2, AVX512, NEON). It provides a CLI, Node.js bindings via odiff-bin, and a specialized playwright-odiff package as a faster, more reliable replacement for Playwright's .toHaveScreenshot() matcher. Features include a persistent server mode (ODiffServer) to reduce process overhead and support for various image formats including .png, .jpeg, .jpg, .webp, and .tiff.
What's inside odiff
- You can download prebuilt binaries for your specific platform directly from the official releases page.
Install playwright-odiff
mainInstall the
playwright-odiffpackage via npm to use a faster and more reliable replacement for Playwright's.toHaveScreenshot()matcher.npm install playwright-odiffIntegrate ODiff with Playwright
mainTo use ODiff for visual regression testing in Playwright, install the
playwright-odiffpackage. You must import the setup in your test entrypoint or setup file to enable the custom matchers.// in your setup file or test entrypoint import "playwright-odiff/setup"; expect(page).toHaveScreenshotOdiff("screenshot-name", { /* any odiff and playwright options */ });Configure playwright-odiff in Playwright tests
mainTo use
playwright-odiff, you must register it in your top-level Playwright setup script (the file where you configuretest,extend, or your common test entrypoint).For macOS users, ensure
gnu-sedis installed viabrew install gnu-sedif you intend to use the automated migration command.import "playwright-odiff/setup"Install Odiff via npm
mainThe recommended cross-platform way to install Odiff is via the
odiff-binnpm package. The package includes platform-specific binaries and apost-installscript that automatically links the correct binary for your current platform.Note: The package name is
odiff-bin, but the command-line tool isodiff.npm install odiff-binMigrate from toHaveScreenshot to toHaveScreenshotOdiff
mainYou can manually replace
expect(...).toHaveScreenshot()withexpect(...).toHaveScreenshotOdiff().If you are on macOS and have
gnu-sedinstalled, you can use the following command to automatically update all.spec.tsfiles in your project:Set up the Benchmark Studio environment
mainTo run performance benchmarks for image difference tools, ensure you have the following tools installed:
odiffpixelmatchImageMagickhyperfine(used to execute and compare performance tests)
On macOS, you can install
hyperfineusing Homebrew:brew install hyperfineRun Odiff in CLI mode to compare images
mainOdiff can be used as a command-line tool to perform visual regression testing by comparing a base image against a comparison image. It supports various modes including terminal graphics display, saving diff outputs to files, and parsable stdout for automation.
Exit Codes
0: Images are identical.21: Images have different dimensions (layout mismatch).22: Images have pixel differences.1: An error occurred (e.g., file not found, unsupported format, or invalid arguments).
Parsable Output Format
When using the
--parsable-stdoutflag, the tool outputs data in a machine-readable format:count;percentage[;lines][;cols]count: Number of different pixels.percentage: Percentage of different pixels.lines: Comma-separated list of different line indices (if requested).cols: Comma-separated list of different column indices (if requested).
Configure playwright-odiff via Playwright config
mainTo use
playwright-odiff, you must register the custom matchers globally by importing the setup file (typically located at./dist/setup.jsin the package distribution) within yourplaywright.config.ts. This enables the Odiff-specific assertion capabilities within your Playwright tests.// Register custom matchers once globally import './dist/setup.js'; import { defineConfig } from '@playwright/test'; export default defineConfig({ // ... rest of config });Use ODiff in Server Mode
mainODiff can be run in a server mode where it reads JSON requests from
stdinand writes JSON responses tostdout. This is useful for integrating ODiff into a pipeline or a long-running process.When the server starts, it immediately emits a
{"ready":true}response to signal it is ready for commands.Send image buffers via ODiff Server
mainIf using
"type": "buffer", you must provide the length and format for both the base and comparison images. The server reads the exact number of bytes specified fromstdinimmediately following the JSON command line.Note: The server uses
readSliceAllto ensure it reads the exact byte count, as binary data may contain newline characters.Required fields for buffer mode:
baseLength(integer): Size of the base image buffer in bytes.baseFormat(string): Format of the base image (e.g.,png,jpeg,jpg,bmp,tiff,webp).compareLength(integer): Size of the comparison image buffer in bytes.compareFormat(string): Format of the comparison image.
Use the Odiff CLI for image comparison
mainOdiff is a SIMD-accelerated pixel-by-pixel image comparison tool. You can run it from the command line by providing a base image and a comparison image. Optionally, you can specify a path for the diff output.
Basic Usage:
odiff <base_image> <comp_image> [diff_output] [options]Exit Codes:
0: Images match21: Layout difference (if--fail-on-layoutis used)22: Pixel differences found