Expand description
Core traits, image buffers, dimensions, and error types shared across the gamut codecs.
This crate is dependency-free with respect to the format crates: every codec in the
workspace builds on the EncodeImage / DecodeImage traits, the branded ImageRef /
ImageBuf buffers, and the Error type defined here, so that callers get a single,
consistent error surface regardless of format.
§Scope and design notes
The surface is deliberately small and frozen; the following are intentional decisions rather than omissions:
- Interleaved
u8/u16layouts only.Sampleis sealed overu8andu16; planar layouts and codec-side concerns such as coded bit depth live ingamut-color(gamut_color::Planar8,gamut_color::BitDepth), not here. - Open where growth is additive, sealed where it must not be.
ErrorandColorModelare#[non_exhaustive]so variants can be added without a breaking change, whilePixelandSampleare sealed — the set of pixel layouts is closed and only this crate defines it. - Static classifications, optional diagnostics.
Error::InvalidInput/Error::Unsupportedretain their allocation-free&'static strpayloads and public shape. Producers can wrap any error inError::Contextto attach an origin, byte offset, or owned detail without changing itsErrorKind.Error::Iopreserves the underlyingstd::io::Errorso transport failures stay distinguishable from malformed input. - The length invariant lives on the buffers, not on
Dimensions.Dimensionsis a plain value type with public fields; non-emptiness andlen == width * height * channelsare enforced once, atImageRef::new/ImageBuf::new, so codecs receive a known-good buffer. - Byte accounting shares its shape and law, not its walks.
segment::Segmentis generic over each format’s ownkindenum andsegment::coversis the one “tiles0..len” predicate; the walks that produce the segments stay format-specific.
§Example
Drive any codec through the shared EncodeImage trait, handing it a branded, already-validated
ImageRef:
use gamut_core::{Dimensions, EncodeImage, ImageRef, Pixel, Result, Rgb8};
// A toy encoder; a real codec would compress `image.as_samples()` instead of copying it.
struct RawEncoder;
impl EncodeImage<Rgb8> for RawEncoder {
fn encode_image(&self, image: ImageRef<'_, Rgb8>, out: &mut Vec<u8>) -> Result<usize> {
let start = out.len();
out.extend_from_slice(image.as_samples()); // input is pre-validated by `ImageRef::new`
Ok(out.len() - start)
}
}
let dims = Dimensions::new(2, 2).unwrap();
let pixels = vec![0u8; dims.sample_count(Rgb8::CHANNELS).unwrap()];
let image = ImageRef::<Rgb8>::new(&pixels, dims).unwrap();
let mut out = Vec::new();
let written = RawEncoder.encode_image(image, &mut out).unwrap();
assert_eq!(written, 2 * 2 * 3);Modules§
- convert
- Format-agnostic conversion between interleaved pixel layouts, with every lossy decision made explicit by the caller.
- luminance
- Reference luminance levels and luma weights, shared across the colour, tone-mapping, and conversion paths so a single definition is authoritative.
- segment
- Byte-tiling: the format-agnostic shape of a “every input byte maps to exactly one typed segment” report, and the predicate that checks it.
Structs§
- Bilevel
- 8-bit bilevel: one byte per pixel,
0= black and non-zero = white. Distinct fromGray8so a grayscale buffer cannot be mistaken for a 1-bit image. - Cmyk8
- 8-bit CMYK: four interleaved ink bytes per pixel. Distinct from
Rgba8despite the shared channel count. - Diagnostic
- Structured diagnostic information attached to an
Error. - Dimensions
- Width and height of an image, in pixels.
- Gray8
- 8-bit grayscale: one luminance byte per pixel.
- Gray16
- 16-bit grayscale: one
u16luminance sample per pixel (high-bit-depth). - Gray
Alpha8 - 8-bit grayscale + alpha: two interleaved bytes per pixel (luminance, then unassociated alpha). The PNG “greyscale with alpha” colour type.
- Gray
Alpha16 - 16-bit grayscale + alpha: two interleaved
u16samples per pixel (luminance, then unassociated alpha; high-bit-depth). - Image
Buf - An owned, length-validated interleaved image of pixel type
P. - Image
Ref - A borrowed, length-validated view of an interleaved image of pixel type
P. - Indexed8
- 8-bit palette indices: one index byte per pixel into a separate colour table. Distinct from
Gray8so indices cannot be mistaken for luminance. - Rgb8
- 8-bit RGB: three interleaved bytes per pixel, row-major.
- Rgb16
- 16-bit RGB: three interleaved
u16samples per pixel (high-bit-depth). - Rgba8
- 8-bit RGBA: four interleaved bytes per pixel (unassociated alpha).
- Rgba16
- 16-bit RGBA: four interleaved
u16samples per pixel (high-bit-depth).
Enums§
- Color
Model - The colour interpretation of a pixel’s channels.
- Error
- Errors produced by gamut encoders and decoders.
- Error
Kind - Stable classification of a gamut error, independent of any diagnostic context around it.
- Pixel
Format - Runtime tag for the closed set of
Pixelmarker types — one variant per marker.
Traits§
- Decode
Image - Decodes a compressed byte stream into an owned
ImageBufof pixel layoutP. - Encode
Image - Encodes an
ImageRefof a specific pixel layoutPinto a compressed byte stream. - Pixel
- Compile-time description of one interleaved pixel layout.
- Sample
- A pixel-sample storage primitive:
u8(8-bit) oru16(10/12/16-bit, high-bit-depth).
Type Aliases§
- Result
- Convenience result type for gamut operations.