Crate zengif

Crate zengif 

Source
Expand description

§zengif

Server-side GIF codec with zero-trust design, memory bounds, streaming, and full animation transparency support.

§Features

  • Streaming decode/encode: Process GIFs without loading entire file
  • Complete animation support: Disposal methods, transparency, timing
  • Memory bounded: Configurable limits, reject oversized inputs
  • Production ready: Error tracing via whereat, cancellation via enough
  • Zero-trust: Validate all inputs, handle malformed data gracefully
  • no_std compatible: Works with alloc only (disable std feature)

§Quick Start

§Decoding

use zengif::{Decoder, Limits, Stats};
use enough::Unstoppable;

let limits = Limits::default();
let stats = Stats::new();

let mut decoder = Decoder::new(reader, limits, &stats, Unstoppable)?;

while let Some(frame) = decoder.next_frame()? {
    // frame.pixels is composited RGBA
    // frame.delay is in centiseconds
}

§Encoding

use zengif::{Encoder, EncoderConfig, FrameInput, Limits, Repeat};
use enough::Unstoppable;

let config = EncoderConfig::new(width, height).repeat(Repeat::Infinite);
let limits = Limits::default();

let mut encoder = Encoder::new(writer, config, limits, Unstoppable)?;

for frame in frames {
    encoder.add_frame(frame)?;
}

encoder.finish()?;

§Memory Tracking

zengif tracks its own buffer allocations (canvas, pixel buffers) through a Stats object. Note: This does not include allocations made internally by the gif crate or quantizers.

let stats = Stats::new();
// ... use decoder/encoder ...
println!("Peak zengif buffer usage: {} bytes", stats.peak());

§Cancellation

Operations support cooperative cancellation via the enough crate:

use almost_enough::Stopper;

let stop = Stopper::new();
let stop_clone = stop.clone();

// In another thread:
stop_clone.cancel();

// Decoder will return GifError::Cancelled

§Feature Flags

  • std (default): Enables std::error::Error impl and std I/O
  • simd: SIMD acceleration via wide/multiversed
  • rgb-interop: Interop with the rgb crate
  • imgref-interop: Interop with the imgref crate

§Color Quantization Backends

Choose one or more quantization backends for encoding:

FeatureQualitySpeedFile SizeLicenseUse Case
imagequantBestMediumSmallestAGPL-3.0Recommended - LZW-aware dithering
quantizrGoodFastMediumMITBest MIT-licensed option
color_quantGoodFastestLargeMITHigh-throughput servers
exoquant-deprecatedGoodSlowMediumMITLegacy compatibility only

Configure the quantizer using the [Quantizer] enum:

use zengif::{EncoderConfig, Quantizer};

// Use imagequant (recommended) for best quality
let config = EncoderConfig::new(100, 100)
    .quantizer(Quantizer::imagequant());

// Use quantizr (MIT) for permissive licensing
let config = EncoderConfig::new(100, 100)
    .quantizer(Quantizer::quantizr_with_dithering(0.3));

// Auto-select best available
let config = EncoderConfig::new(100, 100)
    .quantizer(Quantizer::auto());

Without any quantization feature, zengif is purely MIT/Apache-2.0 licensed.

Modules§

heuristics
Resource estimation heuristics for GIF encoding and decoding operations.

Structs§

ComposedFrame
Composited frame with RGBA pixels.
Decoder
Streaming GIF decoder.
Encoder
Streaming GIF encoder.
EncoderConfig
Encoder configuration.
FrameInput
Frame input for encoding.
FrameIterator
Iterator adapter for decoder frames.
Limits
Configuration for decode/encode limits.
Metadata
Metadata about a GIF file.
Palette
A color palette (up to 256 colors).
QuantizeConfig
Configuration for quantization.
QuantizedFrame
Result of quantizing a single frame.
RawFrame
Raw frame data as read from GIF (before compositing).
Rgba
RGBA pixel (4 bytes).
Screen
GIF compositing screen.
ScreenBuilder
Builder for creating a Screen.
Stats
Thread-safe memory usage statistics.
StatsSnapshot
Immutable snapshot of statistics at a point in time.
TrackedAlloc
A tracked allocation that automatically updates stats on drop.
Unstoppable
A Stop implementation that never stops (no cooperative cancellation).

Enums§

DisposalMethod
GIF disposal method.
GifError
All possible errors in zengif operations.
PaletteStrategy
Strategy for palette selection during encoding.
QuantizerBackend
Available quantizer backends.
Repeat
Loop/repeat behavior for animations.

Traits§

QuantizerTrait
Trait for color quantization implementations.
Stop
Cooperative cancellation check.

Functions§

decode_gif
Convenience function to decode a GIF from bytes.
encode_gif
Convenience function to encode frames to a byte vector.
tracked_vec_filled
Helper to allocate a Vec filled with a value.
tracked_vec_with_capacity
Helper to allocate a Vec with tracking.

Type Aliases§

Result
Result type alias using At<GifError> for automatic location tracking.