Skip to main content

Crate zenpnm

Crate zenpnm 

Source
Expand description

§zenpnm

PNM/PAM/PFM image format decoder and encoder, with optional basic BMP support.

§Zero-Copy Decoding

For PNM files with maxval=255 (the common case), decoding returns a borrowed slice into the input buffer — no allocation or copy needed. Formats that require transformation (16-bit downscaling, PFM byte reordering) allocate.

§Supported Formats

§PNM family (always available)

  • P5 (PGM binary) — grayscale, 8-bit and 16-bit
  • P6 (PPM binary) — RGB, 8-bit and 16-bit
  • P7 (PAM) — arbitrary channels (grayscale, RGB, RGBA), 8-bit and 16-bit
  • PFM — floating-point grayscale and RGB (32-bit float per channel)

§Basic BMP (basic-bmp feature, opt-in)

  • Uncompressed 24-bit (RGB) and 32-bit (RGBA) only
  • Not auto-detected — use decode_bmp and encode_bmp explicitly
  • No RLE, no indexed color, no advanced headers

§Usage

use zenpnm::*;
use enough::Unstoppable;

// Encode pixels to PPM
let pixels = vec![255u8, 0, 0, 0, 255, 0]; // 2 RGB pixels
let encoded = encode_ppm(&pixels, 2, 1, PixelLayout::Rgb8, Unstoppable)?;

// Decode (auto-detects PNM format, zero-copy when possible)
let decoded = decode(&encoded, Unstoppable)?;
assert!(decoded.is_borrowed()); // zero-copy for PPM with maxval=255
assert_eq!(decoded.pixels(), &pixels[..]);

§Credits

PNM implementation draws from zune-ppm by Caleb Etemesi (MIT/Apache-2.0/Zlib licensed).

Structs§

DecodeOutput
Decoded image output. Pixels may be borrowed (zero-copy) or owned.
Limits
Resource limits for decode/encode operations.
Unstoppable
A Stop implementation that never stops (no cooperative cancellation).

Enums§

PixelLayout
Pixel memory layout.
PnmError
Errors from PNM/BMP decoding and encoding.

Traits§

Stop
Cooperative cancellation check.

Functions§

decode
Decode any PNM format (auto-detected from magic bytes).
decode_with_limits
Decode any PNM format with resource limits.
encode_pam
Encode pixels as PAM (P7, arbitrary channels).
encode_pfm
Encode pixels as PFM (floating-point).
encode_pgm
Encode pixels as PGM (P5, binary grayscale).
encode_ppm
Encode pixels as PPM (P6, binary RGB).