Skip to main content

Crate gamut_core

Crate gamut_core 

Source
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 / u16 layouts only. Sample is sealed over u8 and u16; planar layouts and codec-side concerns such as coded bit depth live in gamut-color (gamut_color::Planar8, gamut_color::BitDepth), not here.
  • Open where growth is additive, sealed where it must not be. Error and ColorModel are #[non_exhaustive] so variants can be added without a breaking change, while Pixel and Sample are sealed — the set of pixel layouts is closed and only this crate defines it.
  • Static classifications, optional diagnostics. Error::InvalidInput / Error::Unsupported retain their allocation-free &'static str payloads and public shape. Producers can wrap any error in Error::Context to attach an origin, byte offset, or owned detail without changing its ErrorKind. Error::Io preserves the underlying std::io::Error so transport failures stay distinguishable from malformed input.
  • The length invariant lives on the buffers, not on Dimensions. Dimensions is a plain value type with public fields; non-emptiness and len == width * height * channels are enforced once, at ImageRef::new / ImageBuf::new, so codecs receive a known-good buffer.
  • Byte accounting shares its shape and law, not its walks. segment::Segment is generic over each format’s own kind enum and segment::covers is the one “tiles 0..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 from Gray8 so a grayscale buffer cannot be mistaken for a 1-bit image.
Cmyk8
8-bit CMYK: four interleaved ink bytes per pixel. Distinct from Rgba8 despite 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 u16 luminance sample per pixel (high-bit-depth).
GrayAlpha8
8-bit grayscale + alpha: two interleaved bytes per pixel (luminance, then unassociated alpha). The PNG “greyscale with alpha” colour type.
GrayAlpha16
16-bit grayscale + alpha: two interleaved u16 samples per pixel (luminance, then unassociated alpha; high-bit-depth).
ImageBuf
An owned, length-validated interleaved image of pixel type P.
ImageRef
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 Gray8 so indices cannot be mistaken for luminance.
Rgb8
8-bit RGB: three interleaved bytes per pixel, row-major.
Rgb16
16-bit RGB: three interleaved u16 samples per pixel (high-bit-depth).
Rgba8
8-bit RGBA: four interleaved bytes per pixel (unassociated alpha).
Rgba16
16-bit RGBA: four interleaved u16 samples per pixel (high-bit-depth).

Enums§

ColorModel
The colour interpretation of a pixel’s channels.
Error
Errors produced by gamut encoders and decoders.
ErrorKind
Stable classification of a gamut error, independent of any diagnostic context around it.
PixelFormat
Runtime tag for the closed set of Pixel marker types — one variant per marker.

Traits§

DecodeImage
Decodes a compressed byte stream into an owned ImageBuf of pixel layout P.
EncodeImage
Encodes an ImageRef of a specific pixel layout P into a compressed byte stream.
Pixel
Compile-time description of one interleaved pixel layout.
Sample
A pixel-sample storage primitive: u8 (8-bit) or u16 (10/12/16-bit, high-bit-depth).

Type Aliases§

Result
Convenience result type for gamut operations.