pixon 0.8.0

SIMD-dispatched color conversion for the FFmpeg AVPixelFormat space: one-call decode of validated source frames to RGB / RGBA / Luma / HSV, fused resampling while converting, colorimetry-honest matrices and chroma siting, no_std-friendly.
Documentation
//! Scalar reference for the fused-downscale H-pass: per output span,
//! the weighted sum of its source samples. The exact-integer contract
//! (`u32` sums bounded by `src * 255`) makes every backend's results
//! bit-identical to this one.

#![cfg_attr(
  any(
    not(feature = "std"),
    not(any(feature = "yuv-planar", feature = "rgb"))
  ),
  allow(dead_code)
)]

/// `h_tmp[j * channels + ch] = Σ weights[k] * row[(start_j + i) * channels + ch]`
/// for each output span `j` described by `(starts, offsets, weights)`.
pub(crate) fn area_h_reduce_row(
  row: &[u8],
  channels: usize,
  starts: &[usize],
  offsets: &[usize],
  weights: &[usize],
  h_tmp: &mut [u32],
) {
  for (j, &start) in starts.iter().enumerate() {
    let span = &weights[offsets[j]..offsets[j + 1]];
    let base = j * channels;
    for ch in 0..channels {
      let mut sum = 0u32;
      for (i, &w) in span.iter().enumerate() {
        sum += w as u32 * u32::from(row[(start + i) * channels + ch]);
      }
      h_tmp[base + ch] = sum;
    }
  }
}

/// V-pass AXPY reference: `acc[i] += w * h_tmp[i]` over the H-reduced
/// row. Products and sums are exact in u64 by the engine's
/// denominator bound (`denom * 255 <= u64::MAX`).
pub(crate) fn area_v_accumulate(acc: &mut [u64], h_tmp: &[u32], w: u64) {
  for (a, t) in acc.iter_mut().zip(h_tmp.iter()) {
    *a += w * u64::from(*t);
  }
}

/// 16-bit-element H-pass reference: like [`area_h_reduce_row`] but the
/// samples are `u16` and the per-span sums live in `u64` — a `u16`
/// sample times a `u16`-bounded weight reaches `~2^32`, so even a
/// single span overflows `u32`. Bit-identical contract for every u16
/// backend.
pub(crate) fn area_h_reduce_row_u16(
  row: &[u16],
  channels: usize,
  starts: &[usize],
  offsets: &[usize],
  weights: &[usize],
  h_tmp: &mut [u64],
) {
  for (j, &start) in starts.iter().enumerate() {
    let span = &weights[offsets[j]..offsets[j + 1]];
    let base = j * channels;
    for ch in 0..channels {
      let mut sum = 0u64;
      for (i, &w) in span.iter().enumerate() {
        sum += w as u64 * u64::from(row[(start + i) * channels + ch]);
      }
      h_tmp[base + ch] = sum;
    }
  }
}

/// 16-bit-element V-pass AXPY reference: `acc[i] += w * h_tmp[i]` with
/// `h_tmp` already `u64`. Exact in `u64` by the engine's denominator
/// bound (`denom * 65535 <= u64::MAX`).
pub(crate) fn area_v_accumulate_u16(acc: &mut [u64], h_tmp: &[u64], w: u64) {
  for (a, t) in acc.iter_mut().zip(h_tmp.iter()) {
    *a += w * *t;
  }
}

/// 32-bit-element H-pass reference: like [`area_h_reduce_row_u16`] but the
/// samples are `u32` and the per-span sums live in `u128`. A single term is
/// `weight * sample` with the weight `u16`-bounded (the plan's coverage
/// length) and the sample up to `u32::MAX`, so one term reaches `~2^48` and a
/// span accumulates many of them — far past `u64`. The `u32` source formats
/// are rare and the engine ships **no `u128` SIMD kernel**, so this scalar
/// reference is the production H-pass; its exact integer sums are what keep
/// the `u32` resample 0-ULP.
pub(crate) fn area_h_reduce_row_u32(
  row: &[u32],
  channels: usize,
  starts: &[usize],
  offsets: &[usize],
  weights: &[usize],
  h_tmp: &mut [u128],
) {
  for (j, &start) in starts.iter().enumerate() {
    let span = &weights[offsets[j]..offsets[j + 1]];
    let base = j * channels;
    for ch in 0..channels {
      let mut sum = 0u128;
      for (i, &w) in span.iter().enumerate() {
        sum += w as u128 * u128::from(row[(start + i) * channels + ch]);
      }
      h_tmp[base + ch] = sum;
    }
  }
}

/// 32-bit-element V-pass AXPY reference: `acc[i] += w * h_tmp[i]` with
/// `h_tmp` already `u128`. Exact in `u128` by the engine's denominator
/// bound — `denom * u32::MAX` is at most `~2^96`, leaving the `u128`
/// accumulator wide headroom (see [`area_h_reduce_row_u32`]).
pub(crate) fn area_v_accumulate_u32(acc: &mut [u128], h_tmp: &[u128], w: u128) {
  for (a, t) in acc.iter_mut().zip(h_tmp.iter()) {
    *a += w * *t;
  }
}

/// Float-element H-pass reference: the samples are `f32` but the
/// per-span sums are `f64` — the unnormalized area numerator reaches
/// `denom * sample`, which an `f32` accumulator would round away past
/// `2^24` (a wide constant image would not round-trip) or overflow to
/// infinity. The float stream has no SIMD tier yet, so this is its
/// production H-pass; float adds reorder under any future SIMD, so that
/// tier's parity is a small tolerance, not the integer streams' 0-ULP.
pub(crate) fn area_h_reduce_row_f32(
  row: &[f32],
  channels: usize,
  starts: &[usize],
  offsets: &[usize],
  weights: &[usize],
  h_tmp: &mut [f64],
) {
  for (j, &start) in starts.iter().enumerate() {
    let span = &weights[offsets[j]..offsets[j + 1]];
    let base = j * channels;
    for ch in 0..channels {
      let mut sum = 0.0f64;
      for (i, &w) in span.iter().enumerate() {
        sum += w as f64 * f64::from(row[(start + i) * channels + ch]);
      }
      h_tmp[base + ch] = sum;
    }
  }
}

/// Float-element V-pass AXPY reference: `acc[i] += w * h_tmp[i]` in
/// `f64` (see [`area_h_reduce_row_f32`] for why the float accumulators
/// are `f64`).
pub(crate) fn area_v_accumulate_f32(acc: &mut [f64], h_tmp: &[f64], w: f64) {
  for (a, t) in acc.iter_mut().zip(h_tmp.iter()) {
    *a += w * *t;
  }
}