Module Pixeldrift

Find perceptible pixel differences.

This module finds the number of perceptually differing pixels between two equally sized sRGB images with an alpha component. It is mostly suitable for comparing synthetic images: diagrams, user interface screenshots, rendering algorithms outputs, etc.

To take into account the alpha component images are first blended against an opaque white background before testing their pixels for difference using a perceptual color metric.

Anti-aliased pixels can be detected and optionally ignored by the comparison.

References. The algorithm is based on the pixelmatch JavaScript library. In contrast to the latter it performs alpha blending computations in linear space and uses a simplified anti-aliasing pixel detection algorithm which, for now, acts more like an edge detection filter. The color metric is described in this paper:


type uint32 = int32

The type for unsigned 32-bit integers.

module Bigbytes : sig ... end

Bigarrays of bytes.

type srgb_pixels = Bigbytes.t

The type for pixel data in sRGB space with an alpha component.

Pixels are stored in a linear buffer, line-by-line, from left to right and top to bottom in RGBA order with one byte per component.

Difference map

type diff_map

The type for specifying an image containing a color coded difference map. See diff_map for details.

val diff_map : ?luma_alpha:float -> ?diff:uint32 -> ?diff_darken:uint32 -> ?aa:uint32 -> srgb_pixels -> diff_map

diff_map ~luma_alpha ~diff ~diff_darken ~aa_color pixels specifies a pixel difference image in pixels as follows:

  1. First the luma component of the reference image is blended against an opaque white background using the image's alpha multiplied by luma_alpha (defaults to 0.1). If luma_alpha is 0. then no blending occurs and the difference map background is a black transparent color.
  2. Each pixel where a difference is detected is set to diff (defaults to 0xD7_19_1C_FFl)
  3. Each pixel where a darkening difference is detected is set to diff_darken (defaults equal to diff).
  4. Each pixel where an anti-aliasing pixel is detected (if applicable) is set to aa (defaults to 0xFF_FF_99_FFl).
val mask_diff_map : ?diff:uint32 -> ?diff_darken:uint32 -> srgb_pixels -> diff_map

mask_diff_map is diff_map ~luma_alpha:0 ~aa:0l. With these parameters only differing pixels are rendered over a black transparent background.

val diff_map_pixels : diff_map -> srgb_pixels

diff_map_pixels m are the pixels of m.


val count : ?tol:float -> ignore_aa:bool -> w:int -> h:int -> ref:srgb_pixels -> test:srgb_pixels -> diff_map:diff_map option -> unit -> int

count ~tol ~ignore_aa ~color_space ~w ~h ~ref ~test ~diff_map () is the number of pixels differing when comparing the wh pixels of ref to test with:

  • ignore_aa determines whether anti-aliasing pixels should be detected and ignored for the sake of comparison.
  • color_space is the color space in which all pixels are assumed to live in (including the diff_map if specified).
  • diff_map if specified, writes an image of the differences as specified by the diff_map value.
  • tol is a matching tolerance the smaller the more sensitive the comparison is. Ranges from 0. to 1., defaults to 0.1.

Raises Invalid_argument if ref, test and diff_map do not have exactly wh pixels.