Butteraugli (Butteraugli v0.1.0)

Copy Markdown View Source

Butteraugli perceptual image-difference metric for Elixir, backed by the butteraugli Rust crate.

Butteraugli is a distance: lower is better. A score below 1.0 means the images are perceptually identical, 1.02.0 is a subtle/borderline difference, and above 2.0 is a clearly visible difference. (This is the opposite orientation from quality metrics like SSIMULACRA2.)

Inputs are packed binaries whose layout is chosen with the :format option (default :rgb888):

formatelementchannelsbytes/pixelcolor space
:rgb888 (default)u833sRGB (gamma)
:linear_rgbf32312linear RGB

Multi-byte elements (f32) are native-endian (<<v::native-float-32>>). A binary's size must equal width * height * channels * bytes_per_element.

Comparisons return a Butteraugli.Result.

Cancellation

compare/5 and Butteraugli.Reference.compare/3 accept cancel: (a Butteraugli.CancelRef) and timeout: (milliseconds); aborted calls return {:error, :cancelled} or {:error, :timeout}. Create a ref with Butteraugli.CancelRef.new/0 and trip it with cancel/1.

The granularity differs by path, because the binding polls the ref at different points:

  • compare/5 on images ≥ 8×8 (either format) checks the ref between strips, so it aborts mid-computation — a ref cancelled, or a timeout firing, partway through a long compare stops it promptly.
  • Sub-8×8 images, and Butteraugli.Reference.compare/3 with the default prefer: :speed check the ref once, at the start of the computation. (Sub-8×8 inputs are padded onto the non-strip path; prefer: :speed uses the precomputed reference for the ~2× speedup, which has no strip-wise stop.) These honor a ref that is already cancelled when the call begins — including batch cancellation, where cancelling one ref aborts every subsequent compare that uses it — but a cancel/timeout arriving after the computation is underway will not interrupt it; that call runs to completion.
  • Butteraugli.Reference.compare/3 with prefer: :memory opts into the strip-bounded walker: bounded peak memory and per-strip mid-computation cancellation, trading away the precompute speedup.

If you must bound the wall-clock of an individual long compare, use compare/5 on a ≥ 8×8 image, or Butteraugli.Reference.compare/3 with prefer: :memory.

Summary

Functions

Trip a Butteraugli.CancelRef, aborting any comparison that uses it.

Compare a reference and distorted image of the same dimensions.

Like compare/5 but returns the bare Butteraugli.Result and raises Butteraugli.Error on failure. Accepts the same options.

Types

image_data()

@type image_data() :: binary()

reason()

@type reason() ::
  :invalid_dimensions
  | :size_mismatch
  | :dimension_mismatch
  | :unknown_format
  | :invalid_cancel
  | :invalid_timeout
  | :invalid_prefer
  | :cancelled
  | :timeout
  | {:butteraugli, String.t()}

Functions

cancel(cancel_ref)

@spec cancel(Butteraugli.CancelRef.t()) :: :ok

Trip a Butteraugli.CancelRef, aborting any comparison that uses it.

Call from any process to cancel an in-flight compare/5 or Butteraugli.Reference.compare/3 that was passed this ref as cancel:. Returns :ok and is safe to call more than once.

compare(reference, distorted, width, height, opts \\ [])

@spec compare(image_data(), image_data(), pos_integer(), pos_integer(), keyword()) ::
  {:ok, Butteraugli.Result.t()} | {:error, reason()}

Compare a reference and distorted image of the same dimensions.

Options:

  • :format:rgb888 (default) or :linear_rgb.
  • :compute_diffmap — when true, the Result includes a per-pixel diffmap binary (default false).
  • :intensity_target — display brightness in nits (crate default if omitted).
  • :hf_asymmetry — high-frequency penalty asymmetry (crate default if omitted).
  • :cancel — a Butteraugli.CancelRef; cancelling it from another process aborts the call with {:error, :cancelled}.
  • :timeout — positive integer milliseconds; the call returns {:error, :timeout} if it exceeds that.

Returns {:ok, %Butteraugli.Result{}} or {:error, reason}.

compare!(reference, distorted, width, height, opts \\ [])

Like compare/5 but returns the bare Butteraugli.Result and raises Butteraugli.Error on failure. Accepts the same options.