Expand description
§mozjpeg-oxide
Pure Rust JPEG encoder based on Mozilla’s mozjpeg.
This library provides a high-quality JPEG encoder with mozjpeg’s advanced compression features:
- Trellis quantization - Rate-distortion optimized coefficient selection
- Progressive JPEG - Multi-scan encoding with DC-first, AC-band progression
- Huffman optimization - 2-pass encoding for optimal entropy coding
- Overshoot deringing - Reduces ringing artifacts near hard edges
§Quick Start
The Encoder struct is the main entry point for encoding images:
use mozjpeg_rs::{Encoder, Preset};
// RGB pixel data (3 bytes per pixel, row-major order)
let rgb_pixels: Vec<u8> = vec![0; 640 * 480 * 3];
// Default: progressive with all optimizations (recommended)
let jpeg_data = Encoder::new(Preset::default())
.quality(85)
.encode_rgb(&rgb_pixels, 640, 480)?;
// Fastest encoding (no optimizations)
let jpeg_data = Encoder::new(Preset::BaselineFastest)
.quality(85)
.encode_rgb(&rgb_pixels, 640, 480)?;
// Maximum compression (matches C mozjpeg)
let jpeg_data = Encoder::new(Preset::ProgressiveSmallest)
.quality(85)
.encode_rgb(&rgb_pixels, 640, 480)?;§Encoder Presets
| Preset | Time | Size | Use Case |
|---|---|---|---|
Preset::BaselineFastest | ~2ms | baseline | Real-time, thumbnails |
Preset::BaselineBalanced | ~7ms | -13% | Sequential playback |
Preset::ProgressiveBalanced | ~9ms | -13% | Web images (default) |
Preset::ProgressiveSmallest | ~21ms | -14% | Storage, archival |
Benchmarks: 512×512 Q75 image
§Advanced Configuration
use mozjpeg_rs::{Encoder, Preset, Subsampling, TrellisConfig, QuantTableIdx};
let jpeg_data = Encoder::new(Preset::BaselineBalanced)
.quality(75)
.progressive(true) // Override to progressive
.subsampling(Subsampling::S420) // 4:2:0 chroma subsampling
.quant_tables(QuantTableIdx::Flat) // Flat quantization tables
.trellis(TrellisConfig::default()) // Enable trellis quantization
.optimize_huffman(true) // 2-pass Huffman optimization
.overshoot_deringing(true) // Reduce ringing at edges
.encode_rgb(&rgb_pixels, 100, 100)?;§Grayscale Encoding
use mozjpeg_rs::{Encoder, Preset};
let gray_pixels: Vec<u8> = vec![128; 100 * 100]; // 1 byte per pixel
let jpeg_data = Encoder::new(Preset::default())
.quality(85)
.encode_gray(&gray_pixels, 100, 100)?;§Writing to a File or Stream
use mozjpeg_rs::{Encoder, Preset};
use std::fs::File;
let mut file = File::create("output.jpg")?;
Encoder::new(Preset::default())
.quality(85)
.encode_rgb_to_writer(&rgb_pixels, 100, 100, &mut file)?;§Metadata
use mozjpeg_rs::{Encoder, Preset, PixelDensity};
let jpeg_data = Encoder::new(Preset::default())
.quality(85)
.pixel_density(PixelDensity::dpi(300, 300)) // 300 DPI
.exif_data(exif_bytes) // EXIF metadata
.icc_profile(icc_profile) // Color profile
.encode_rgb(&rgb_pixels, 100, 100)?;Structs§
- Encoder
- The main JPEG encoder.
- Encoding
Stream - The main JPEG encoder.
- Limits
- Resource limits for the encoder.
- Pixel
Density - Pixel density for JFIF metadata.
- Resource
Estimate - Estimated resource usage for an encoding operation.
- Streaming
Encoder - The main JPEG encoder.
- Trellis
Config - Configuration for trellis quantization.
Enums§
- Density
Unit - Pixel density unit for JFIF metadata.
- Error
- Error type for encoding operations.
- Preset
- Encoder preset controlling compression mode and optimization level.
- Quant
Table Idx - Quantization table preset.
- Subsampling
- Chroma subsampling mode.
Constants§
- DCTSIZ
E2 - Number of coefficients in an 8x8 DCT block (64).
Traits§
- Encode
- The main JPEG encoder.
Type Aliases§
- Result
- Result type alias for encoding operations.