pub struct Encoder { /* private fields */ }Expand description
JPEG encoder with configurable quality and features.
Implementations§
Source§impl Encoder
impl Encoder
Sourcepub fn new() -> Encoder
pub fn new() -> Encoder
Create a new encoder with default settings (mozjpeg defaults).
Default configuration:
- Quality: 75
- Progressive: false
- Subsampling: 4:2:0
- Quant tables: ImageMagick (mozjpeg default)
- Trellis: enabled (core mozjpeg optimization)
- Huffman optimization: enabled (2-pass for optimal tables)
- Overshoot deringing: enabled (reduces ringing on edges)
Sourcepub fn max_compression() -> Encoder
pub fn max_compression() -> Encoder
Create encoder with max compression settings (mozjpeg defaults).
Enables progressive mode, trellis quantization, Huffman optimization, and overshoot deringing.
Note: optimize_scans tries multiple scan configurations to find the smallest. Results may differ from C mozjpeg’s default 9-scan successive approximation script.
Sourcepub fn fastest() -> Encoder
pub fn fastest() -> Encoder
Create encoder with fastest settings (libjpeg-turbo compatible).
Disables all mozjpeg optimizations (trellis, Huffman optimization, deringing). Uses ImageMagick quant tables (same as C mozjpeg defaults from jpeg_set_defaults).
Sourcepub fn quality(self, quality: u8) -> Encoder
pub fn quality(self, quality: u8) -> Encoder
Set quality level (1-100).
Higher values produce larger, higher-quality images.
Sourcepub fn progressive(self, enable: bool) -> Encoder
pub fn progressive(self, enable: bool) -> Encoder
Enable or disable progressive mode.
Sourcepub fn subsampling(self, mode: Subsampling) -> Encoder
pub fn subsampling(self, mode: Subsampling) -> Encoder
Set chroma subsampling mode.
Sourcepub fn quant_tables(self, idx: QuantTableIdx) -> Encoder
pub fn quant_tables(self, idx: QuantTableIdx) -> Encoder
Set quantization table variant.
Sourcepub fn trellis(self, config: TrellisConfig) -> Encoder
pub fn trellis(self, config: TrellisConfig) -> Encoder
Configure trellis quantization.
Sourcepub fn force_baseline(self, enable: bool) -> Encoder
pub fn force_baseline(self, enable: bool) -> Encoder
Force baseline-compatible output.
Sourcepub fn optimize_huffman(self, enable: bool) -> Encoder
pub fn optimize_huffman(self, enable: bool) -> Encoder
Enable Huffman table optimization.
Sourcepub fn overshoot_deringing(self, enable: bool) -> Encoder
pub fn overshoot_deringing(self, enable: bool) -> Encoder
Enable overshoot deringing.
Reduces visible ringing artifacts near hard edges, especially on white backgrounds. Works by allowing encoded values to “overshoot” above 255 (which will clamp back to 255 when decoded) to create smoother waveforms.
This is a mozjpeg-specific feature that can improve visual quality at minimal file size cost. Enabled by default.
Sourcepub fn optimize_scans(self, enable: bool) -> Encoder
pub fn optimize_scans(self, enable: bool) -> Encoder
Enable or disable scan optimization for progressive mode.
When enabled, the encoder tries multiple scan configurations and picks the one that produces the smallest output. This can improve compression by 1-3% but increases encoding time.
Only has effect when progressive mode is enabled.
Sourcepub fn restart_interval(self, interval: u16) -> Encoder
pub fn restart_interval(self, interval: u16) -> Encoder
Set restart interval in MCUs.
Restart markers are inserted every N MCUs, which can help with error recovery and parallel decoding. Set to 0 to disable (default).
Common values: 0 (disabled), or image width in MCUs for row-by-row restarts.
Sourcepub fn exif_data(self, data: Vec<u8>) -> Encoder
pub fn exif_data(self, data: Vec<u8>) -> Encoder
Set EXIF data to embed in the JPEG.
§Arguments
data- Raw EXIF data (TIFF structure). The “Exif\0\0” header will be added automatically.
Pass empty or call without this method to omit EXIF data.
Sourcepub fn pixel_density(self, density: PixelDensity) -> Encoder
pub fn pixel_density(self, density: PixelDensity) -> Encoder
Set pixel density for the JFIF APP0 marker.
This specifies the physical pixel density (DPI/DPC) or aspect ratio. Note that most software ignores JFIF density in favor of EXIF metadata.
§Example
use mozjpeg_rs::{Encoder, PixelDensity};
let encoder = Encoder::new()
.pixel_density(PixelDensity::dpi(300, 300)); // 300 DPISourcepub fn icc_profile(self, profile: Vec<u8>) -> Encoder
pub fn icc_profile(self, profile: Vec<u8>) -> Encoder
Set ICC color profile to embed.
The profile will be embedded in APP2 markers with the standard “ICC_PROFILE” identifier. Large profiles are automatically chunked.
§Arguments
profile- Raw ICC profile data
Sourcepub fn add_marker(self, app_num: u8, data: Vec<u8>) -> Encoder
pub fn add_marker(self, app_num: u8, data: Vec<u8>) -> Encoder
Add a custom APP marker.
§Arguments
app_num- APP marker number (0-15, e.g., 1 for EXIF, 2 for ICC)data- Raw marker data (including any identifier prefix)
Multiple markers with the same number are allowed. Markers are written in the order they are added.
Sourcepub fn custom_luma_qtable(self, table: [u16; 64]) -> Encoder
pub fn custom_luma_qtable(self, table: [u16; 64]) -> Encoder
Set custom luminance quantization table.
This overrides the table selected by quant_tables().
Values should be in natural (row-major) order, not zigzag.
§Arguments
table- 64 quantization values (quality scaling still applies)
Sourcepub fn custom_chroma_qtable(self, table: [u16; 64]) -> Encoder
pub fn custom_chroma_qtable(self, table: [u16; 64]) -> Encoder
Set custom chrominance quantization table.
This overrides the table selected by quant_tables().
Values should be in natural (row-major) order, not zigzag.
§Arguments
table- 64 quantization values (quality scaling still applies)
Sourcepub fn encode_rgb(
&self,
rgb_data: &[u8],
width: u32,
height: u32,
) -> Result<Vec<u8>, Error>
pub fn encode_rgb( &self, rgb_data: &[u8], width: u32, height: u32, ) -> Result<Vec<u8>, Error>
Sourcepub fn encode_gray(
&self,
gray_data: &[u8],
width: u32,
height: u32,
) -> Result<Vec<u8>, Error>
pub fn encode_gray( &self, gray_data: &[u8], width: u32, height: u32, ) -> Result<Vec<u8>, Error>
Source§impl Encoder
impl Encoder
Sourcepub fn streaming() -> StreamingEncoder
pub fn streaming() -> StreamingEncoder
Create a streaming encoder.
Returns a StreamingEncoder which supports scanline-by-scanline encoding.
Note that streaming mode does NOT support trellis quantization, progressive
mode, or Huffman optimization (these require buffering the entire image).
For full-featured encoding with all mozjpeg optimizations, use Encoder::new()
with encode_rgb() or encode_gray().
§Example
use mozjpeg_rs::Encoder;
use std::fs::File;
let file = File::create("output.jpg")?;
let mut stream = Encoder::streaming()
.quality(85)
.start_rgb(1920, 1080, file)?;
// Write scanlines...
stream.finish()?;