zengif
A GIF codec built for servers: streaming, memory-safe, and production-ready.
Getting Started
Decode a GIF
use ;
use File;
use BufReader;
Encode a GIF
use ;
Why zengif?
If you're building a server that handles untrusted GIF uploads, you need:
- Memory limits - Reject oversized images before allocating
- Cancellation - Stop processing if the request is cancelled
- Error context - Know exactly where parsing failed
- Correct compositing - Handle all disposal methods and transparency
zengif builds on the excellent gif crate, adding these production features:
| Feature | gif crate | zengif |
|---|---|---|
| Streaming decode | ✅ | ✅ |
| Memory limits | ✅ | ✅ |
| Frame compositing | ❌ (use gif-dispose) | ✅ built-in |
| Cooperative cancellation | ❌ | ✅ |
| Error tracing (file:line) | ❌ | ✅ |
| High-quality encoding | ❌ | ✅ (optional) |
Memory Protection
Protect your server from malicious inputs:
use Limits;
let limits = default
.max_dimensions // Reject huge canvases
.max_frame_count // Limit animation length
.max_memory; // 256 MB peak memory
The decoder will return an error before allocating if limits would be exceeded.
Cancellation
For web servers, you often need to stop processing if the client disconnects:
use Stopper;
use ;
let stop = new;
let stop_for_handler = stop.clone;
// In your request handler, if client disconnects:
stop_for_handler.cancel;
// The decoder will return GifError::Cancelled at the next check point
let mut decoder = new?;
Error Diagnostics
When something goes wrong, you get the full story:
Error: InvalidFrameBounds { frame_left: 0, frame_top: 0, frame_width: 5000,
frame_height: 5000, canvas_width: 100, canvas_height: 100 }
at src/decode/frame.rs:142:9
╰─ validating frame 3
at src/decode/mod.rs:89:5
╰─ in decode_frame
High-Quality Encoding
For the smallest file sizes, enable the imagequant feature:
use ;
let config = new
.quantizer; // Best quality, smallest files
Quantizer Options
| Feature | License | Quality | Speed |
|---|---|---|---|
imagequant |
AGPL-3.0* | Best | Medium |
quantizr |
MIT | Good | Fast |
color_quant |
MIT | Good | Fastest |
*Commercial license available for closed-source projects.
Without any quantizer feature, zengif is MIT/Apache-2.0 licensed.
no_std / WASM
For WASM or embedded, disable the default std feature:
= { = "0.4", = false }
You get core types (Rgba, Limits, GifError, etc.) but not the codec. Useful when you need to share types between WASM and native code.
Performance
On AMD Ryzen 9 5900X:
| Operation | Throughput |
|---|---|
| Decode (composited) | ~150 MB/s |
| Encode (quantized) | ~40 MB/s |
| Encode (pre-indexed) | ~200 MB/s |
License
MIT or Apache-2.0, at your option.
The optional imagequant feature uses libimagequant (AGPL-3.0). Use quantizr or color_quant for fully permissive licensing.
100% safe Rust - #![forbid(unsafe_code)]