Encoder

Struct Encoder 

Source
pub struct Encoder { /* private fields */ }
Expand description

JPEG encoder with configurable quality and features.

Implementations§

Source§

impl Encoder

Source

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)
Source

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.

Source

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).

Source

pub fn quality(self, quality: u8) -> Encoder

Set quality level (1-100).

Higher values produce larger, higher-quality images.

Source

pub fn progressive(self, enable: bool) -> Encoder

Enable or disable progressive mode.

Source

pub fn subsampling(self, mode: Subsampling) -> Encoder

Set chroma subsampling mode.

Source

pub fn quant_tables(self, idx: QuantTableIdx) -> Encoder

Set quantization table variant.

Source

pub fn trellis(self, config: TrellisConfig) -> Encoder

Configure trellis quantization.

Source

pub fn force_baseline(self, enable: bool) -> Encoder

Force baseline-compatible output.

Source

pub fn optimize_huffman(self, enable: bool) -> Encoder

Enable Huffman table optimization.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 DPI
Source

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
Source

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.

Source

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)
Source

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)
Source

pub fn encode_rgb( &self, rgb_data: &[u8], width: u32, height: u32, ) -> Result<Vec<u8>, Error>

Encode RGB image data to JPEG.

§Arguments
  • rgb_data - RGB pixel data (3 bytes per pixel, row-major)
  • width - Image width in pixels
  • height - Image height in pixels
§Returns

JPEG-encoded data as a Vec<u8>.

Source

pub fn encode_gray( &self, gray_data: &[u8], width: u32, height: u32, ) -> Result<Vec<u8>, Error>

Encode grayscale image data to JPEG.

§Arguments
  • gray_data - Grayscale pixel data (1 byte per pixel, row-major)
  • width - Image width in pixels
  • height - Image height in pixels
§Returns

JPEG-encoded data as a Vec<u8>.

Source

pub fn encode_gray_to_writer<W>( &self, gray_data: &[u8], width: u32, height: u32, output: W, ) -> Result<(), Error>
where W: Write,

Encode grayscale image data to a writer.

Source

pub fn encode_rgb_to_writer<W>( &self, rgb_data: &[u8], width: u32, height: u32, output: W, ) -> Result<(), Error>
where W: Write,

Encode RGB image data to a writer.

Source§

impl Encoder

Source

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()?;

Trait Implementations§

Source§

impl Clone for Encoder

Source§

fn clone(&self) -> Encoder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Encoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Encoder

Source§

fn default() -> Encoder

Returns the “default value” for a type. Read more
Source§

impl Encode for Encoder

Source§

fn encode_rgb( &self, rgb_data: &[u8], width: u32, height: u32, ) -> Result<Vec<u8>, Error>

Encode RGB image data to JPEG. Read more
Source§

fn encode_gray( &self, gray_data: &[u8], width: u32, height: u32, ) -> Result<Vec<u8>, Error>

Encode grayscale image data to JPEG. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.