New to the zenpixels 0.5 stack? This page explains how garb,
linear-srgb, zenpixels, and zenpixels-convert fit together,
what every type actually means, and the real-world gotchas to avoid on day one.
| If you want to… | Use | Why |
|---|---|---|
Swap, expand, or strip raw u8 channels (RGBA↔BGRA, RGB→RGBA, strided rows) with zero color math | garb (garb::bytes) | Tiny, no_std, zero-panic Result<(), SizeError>, pure byte layout |
Apply a transfer curve (sRGB, gamma, PQ, HLG, BT.709) to f32/u8/u16 slices | linear-srgb (linear_srgb::default) | Pure transfer math, SIMD slices (*_slice / *_slice_into), no buffer types |
Carry images, row stride, and color meaning (PixelBuf, PixelView, PlanarBuf, PixelFormat, ColorOrigin) | zenpixels | Lightweight interchange types (no_std + alloc, #![forbid(unsafe_code)]) for codecs and image pipelines |
| Convert images, rows, planar YCbCr, gain maps, orientation, HDR, or ICC profiles | zenpixels-convert | Full planner + executor (re-exports zenpixels::* flat at the crate root) |
zenpixels 0.5Think of a shipping box:
SampleLayout = the box shape (sample: SampleType, order: ChannelOrder, packing: BitPacking — e.g. SampleLayout::RGB8, RGBAF32, or 10-bit U10_LSB / P010). It describes bytes in memory only; it cannot authorize a color conversion by itself.ColorSpace = what the numbers mean right now (Rgb, Gray, Oklab, Xyz, Perceptual, Cmyk, Icc, or Untagged). Primaries (Primaries), transfer curve (Transfer), signal range (SignalRange), alpha mode (AlphaMode), and optional diffuse-white anchor (Option<DiffuseWhite>) live inside the variant that actually uses them — impossible states don’t exist.PixelFormat = the complete shipping label (layout: SampleLayout + color: ColorSpace).
PixelFormat::RGB8_SRGB when constructing buffers, &formats::RGB8_SRGB (&'static PixelFormat) for zero-allocation views and plans, and const SUPPORTED: &[PixelFormat] for static codec capability tables.buf.relabel_color(...) or buf.relabel_format(...) only replaces the sticker on the box — it never converts a single pixel byte. To change sample values, use zenpixels-convert.ColorOrigin = what the original file claimed on disk (for example a PNG or AVIF carrying both an ICC chunk and a CICP chunk). It is history for encoder provenance (OutputSignaling::PreserveOrigin / Auto) and never overrides the active ColorSpace.PixelBuf, PixelView, BandedBuf, PlanarBuf, DualLayerHdrBuf):
PixelBuf owns one alignment-exact, stride-preserving 2D image allocation (or adopts zero-copy external Arc<[u8]> / FFI memory with fallible copy-on-write via try_cow_view_mut()), carrying Orientation, HdrMetadata, ColorOrigin, and SampleClaims.PixelView<'a> / PixelViewMut<'a> is a zero-copy borrowed strided view or rectangular crop (PixelRect). Width and height count pixels; stride_bytes() counts bytes. Crops preserve the parent stride, so read active pixels with row(y).BandedBuf owns a gigapixel-safe image split into capped-size horizontal PixelBuf bands (BandSizing).PlanarBuf / PlanarView<'a> carries multi-plane YCbCr / GBR / planar RGB (PlaneFormat, MatrixCoefficients, ChromaSiting) plus zero-copy CodePlane access.DualLayerHdrBuf / DualLayerHdrView<'a> pairs a base rendition with an ISO 21496-1 / UltraHDR gain map and GainMapMetadata.Converting pixels separates policy (what lossy changes are allowed) from plan compilation and zero-scratch execution:
ConvertPolicy:
ConvertPolicy::default() equals ConvertPolicy::lossless() — it refuses silent depth reduction, alpha loss, gamut clipping, and Narrow↔Full range rescaling.ConvertPolicy::permissive(), ConvertPolicy::working_linear(), or ConvertPolicy::display_sdr(white, peak) (or chain .with_depth(...), .with_alpha(...), .with_gamut(...), .with_range(...), .with_color_services(&services)).view.to_rgb8_if_opaque(), view.to_gray8_if_opaque(), view.to_rgba8(), view.to_bgra8().view.convert_to(&formats::RGBF32_LINEAR, &policy) or src.convert_into(&mut dst, &policy).ImagePlan::build(src_fmt, dst_fmt, &policy, PeakSource::Measured) works on PixelView, PixelBuf, and BandedBuf via PixelSource / PixelSink.RowPlan::build(src_fmt, dst_fmt, &policy, resolved_peak) executes via convert_row / convert_row_at over a 1 KiB stack-allocated SoA StripBuf.OutputPlan::adapt_for_encode / OutputPlan::build returns EncodeReady<'a> (PixelCow::Borrowed on identity with zero copy!) whose cicp and icc match the converted pixels atomically.reconstruct_planar_to_buf / reconstruct_planar_into (planar → packed) and PlanarCapturePlan + CaptureScratch (packed → planar YCbCr / GBR / RGB with phase-exact chroma downsampling).accelerators & const Accelerators)In Rust, Cargo features unify globally across the dependency tree. If every codec crate turned on every SIMD conversion table (sRGB, BT.709, Gamma 2.2, PQ, HLG, Oklab, GainMap, gamut matrices), tiny decoders and Wasm apps would pay hundreds of kilobytes of dead code and slow builds.
Instead, zenpixels-convert 0.5 separates correctness from SIMD codegen:
Accelerators::SCALAR) that produces 0-bit identical output to the x86-64/AArch64/WASM128 SIMD kernels. Libraries never fail for missing SIMD knobs, and unused SIMD kernels dead-strip to 0 B.Accelerators::ALL.install_global() once in main() installs every compiled SIMD accelerator (including gain-map SIMD) across the process, including inside codec crates.Accelerators::ROW_PIPELINE.install_global() (everything except gain-map SIMD), Accelerators::WEB_SDR.install_global(), or MY_CONST_ACCEL.install_global().const Accelerators presets and builders (SCALAR, LAYOUT, WEB_SDR, HDR, GAINMAP, OKLAB, ROW_PIPELINE, ALL, .with_*(), .merge_prefer_self(...)) passed to ConvertPolicy::with_accelerators(...) with zero global state.fast (never enable in library crates — it unifies globally across Cargo’s dependency graph, though per-policy .with_accelerators(Accelerators::SCALAR) still overrides it hermetically).use zenpixels_convert::{Accelerators, ConvertPolicy, accelerators};
// Full SIMD for apps/CLIs/servers (returns true on first install, false if already installed):
let _ = Accelerators::ALL.install_global();
// Compile-time scoped subset (dead-strips unused SIMD families):
const LAYOUT_AND_SRGB: Accelerators = Accelerators::SCALAR
.with_byte_layout()
.with_strip_simd()
.with_srgb();
let scoped = ConvertPolicy::lossless().with_accelerators(LAYOUT_AND_SRGB);
// Hermetic scalar-only override for 0-bit parity tests (ignores global registry):
let scalar = ConvertPolicy::lossless().with_accelerators(Accelerators::SCALAR);
assert!(!accelerators::resolve(Some(Accelerators::SCALAR)).has_byte_layout());See Balancing speed and light compilation for feature budgets and recipes by workload.
SampleClaims & FloatClaims: proven bounds without guessworkUntrusted f32 buffers can contain NaN, ±inf, subnormals, negatives, or values above 1.0, and RGBA8 buffers may secretly be 100% opaque or grayscale.
SampleClaims bundles content invariants on buffers, views, and policies:
float: FloatClaims — bitflags (NO_NAN, NO_INFINITE, NO_SUBNORMALS, NON_NEGATIVE, POSITIVE, AT_MOST_ONE, FINITE, UNIT_INTERVAL, NORMAL_UNIT_INTERVAL).depth: Option<ExactDepth> — proven bit-grid precision (e.g. U16 samples that actually came from 8-bit data).all_opaque: bool, all_grayscale: bool, binary_alpha: bool — unlocks lossless AlphaPolicy::DiscardIfOpaque and compact_to_buf.use zenpixels_convert::{
ConvertPolicy, DepthPolicy, FloatClaims, PixelBuf, PixelFormat, SampleClaims, PixelConvertExt,
PixelScanExt, formats,
};
let mut buf = PixelBuf::new(4, 1, PixelFormat::RGBF32_LINEAR)?;
let claims = buf.measure_claims();
assert!(claims.float.contains(FloatClaims::UNIT_INTERVAL));
// Attach proven claims to the buffer or policy:
buf.assume_claims(claims);
let policy = ConvertPolicy::lossless()
.with_depth(DepthPolicy::Round)
.with_source_claims(claims);
let srgb = buf.convert_to(&formats::RGB8_SRGB, &policy)?;
assert_eq!(srgb.row(0), &[0; 12]);
// Any mutable borrow automatically clears cached claims so stale proofs never lie:
let _mut_view = buf.as_view_mut();
assert_eq!(buf.attached_claims(), SampleClaims::NONE);Wide-gamut Oklab is CIE XYZ D65-anchored (zero BT.709 clipping):
PixelFormat::OKLAB_F32 represents perceptual color in CIE XYZ D65. Converting Display P3 or BT.2020 → formats::OKLAB_F32 → Display P3 uses that gamut’s own RGB ↔ LMS matrix directly — bit-exact roundtrip with zero BT.709 clipping:
use zenpixels_convert::{
ConvertPolicy, DepthPolicy, PixelBuf, PixelFormat, Primaries, PixelConvertExt, formats,
};
let p3_srgb = PixelFormat::RGB8_SRGB.with_primaries(Primaries::DisplayP3)?;
let src = PixelBuf::from_typed(vec![0u8, 255, 128], 1, 1, p3_srgb.clone())?;
let policy = ConvertPolicy::lossless().with_depth(DepthPolicy::Round);
let oklab = src.convert_to(&formats::OKLAB_F32, &policy)?;
let back = oklab.convert_to(&p3_srgb, &policy)?;
assert_eq!(back.row(0), &[0, 255, 128]);Flat single-crate re-export: zenpixels-convert re-exports all of zenpixels flat (pub use zenpixels::*;) with zero shadowed module names — you can import PixelBuf, PixelFormat, Nits, DiffuseWhite, HlgRendering, ConvertPolicy, ImagePlan, RowPlan, and ColorServices straight from zenpixels_convert.
HDR→SDR needs features = ["bt2446a"] (or a custom ToneMapper) and explicit peaks:
tone_map_hdr_to_sdr on PQ/HLG → SDR returns ConvertError::HdrFeatureRequired until you enable bt2446a or attach a custom ToneMapper (via policy.with_tone_mapper(...) or ColorServices).ImagePlan::build accepts PeakSource::{Measured, Declared, Fixed(Nits)} because it can inspect whole images; RowPlan::build sees rows one at a time and therefore takes a pre-resolved Option<Nits>.Transfer::Hlg(HlgMode::Scene) vs Transfer::Hlg(HlgMode::Display(HlgRendering)): scene-referred HLG requires explicit HlgRendering (e.g. HlgRendering::REFERENCE_1000_NIT) before converting to display-referred SDR/PQ, or planning fails with ConvertError::HlgRenderingRequired.CMS backends are always explicit (ColorServices / with_cms):
cms-moxcms or cms-lcms never silently routes pixels through a CMS. Configure ColorServices::with_cms_binding(MoxCms::binding()) (or Lcms::binding() when you need AbsoluteColorimetric or ICC v4 float MPE) and apply .with_color_services(&services) on ConvertPolicy.ConvertError::CmsRequired.Features & ConvertError::FeatureRequired:
zenpixels-convert: std, fast-transpose, oklab, hdr-transfers, gainmap.perceptual (Oklch, CieLab, CieLch, Jzazbz, Jzczhz, Hsl, Hsv), bt2446a, icc-db, cms-moxcms, cms-lcms, rgb, imgref, avx512, precise-portable, fast.PlanarBuf, PlanarView, reconstruct_planar_*, PlanarCapturePlan) are always compiled in (no planar feature needed in 0.5).ConvertError::FeatureRequired(name) fires when a conversion route needs an optional feature ("oklab", "hdr-transfers", "perceptual").Located errors: Carrier and conversion errors are wrapped in whereat::At<E>, so {err:?} prints the exact raising source location alongside the variant.
Data Structures Atlas · Type drilldown · Pixel types guide · Transfer functions · Primaries, HDR & CICP/ICC · Kernel browser · Speed & light builds · Explore one pixel · Quick starts