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 viaenough - Zero-trust: Validate all inputs, handle malformed data gracefully
- no_std compatible: Works with
alloconly (disablestdfeature)
§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): Enablesstd::error::Errorimpl and std I/Osimd: SIMD acceleration via wide/multiversedrgb-interop: Interop with thergbcrateimgref-interop: Interop with theimgrefcrate
§Color Quantization Backends
Choose one or more quantization backends for encoding:
| Feature | Quality | Speed | File Size | License | Use Case |
|---|---|---|---|---|---|
imagequant | Best | Medium | Smallest | AGPL-3.0 | Recommended - LZW-aware dithering |
quantizr | Good | Fast | Medium | MIT | Best MIT-licensed option |
color_quant | Good | Fastest | Large | MIT | High-throughput servers |
exoquant-deprecated | Good | Slow | Medium | MIT | Legacy 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§
- Composed
Frame - Composited frame with RGBA pixels.
- Decoder
- Streaming GIF decoder.
- Encoder
- Streaming GIF encoder.
- Encoder
Config - Encoder configuration.
- Frame
Input - Frame input for encoding.
- Frame
Iterator - 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).
- Quantize
Config - Configuration for quantization.
- Quantized
Frame - 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.
- Screen
Builder - Builder for creating a Screen.
- Stats
- Thread-safe memory usage statistics.
- Stats
Snapshot - Immutable snapshot of statistics at a point in time.
- Tracked
Alloc - A tracked allocation that automatically updates stats on drop.
- Unstoppable
- A
Stopimplementation that never stops (no cooperative cancellation).
Enums§
- Disposal
Method - GIF disposal method.
- GifError
- All possible errors in zengif operations.
- Palette
Strategy - Strategy for palette selection during encoding.
- Quantizer
Backend - Available quantizer backends.
- Repeat
- Loop/repeat behavior for animations.
Traits§
- Quantizer
Trait - 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.