Skip to main content

EncodeOptions

Struct EncodeOptions 

Source
#[non_exhaustive]
pub struct EncodeOptions {
Show 13 fields pub quality: u8, pub lossless: bool, pub effort: Option<u8>, pub compression_level: Option<u8>, pub palette: bool, pub colours: Option<u16>, pub dither: Option<f32>, pub alpha_quality: u8, pub near_lossless: bool, pub chroma_subsampling: Option<ChromaSubsampling>, pub keep_exif: bool, pub keep_xmp: bool, pub progressive: bool,
}
Expand description

Encoder tuning shared across formats.

Fields that a format has no notion of are ignored by that format rather than rejected, so the same options value can be handed to any encoder. Every field’s default leaves a format’s output as it was before the field existed.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§quality: u8

Lossy quality, 1–100. Ignored by lossless formats, and by a format with both modes when lossless is set.

§lossless: bool

For a format that has both a lossy and a lossless mode (WebP), choose lossless. Off by default: such a format encodes lossy at quality, as cwebp and sharp do. Formats with one mode ignore it.

§effort: Option<u8>

CPU spent for a smaller file, 0 (fastest) to 10, in each format’s own range as sharp defines it: WebP 0–6 and AVIF 0–9, both with a default of 4. None is the format’s default.

§compression_level: Option<u8>

PNG’s DEFLATE level, 0 (store) to 9. None derives it from quality, as before this option existed.

§palette: bool

Write PNG as a palette image of at most colours entries (sharp’s palette). Setting colours or dither implies it, as in sharp.

§colours: Option<u16>

The palette size for palette PNG and GIF, 2–256. None is 256 for PNG, and for GIF the size quality implies.

§dither: Option<f32>

How much of the quantization error a palette image diffuses, 0 (none) to 1 (full Floyd-Steinberg, the default). None is 1.

§alpha_quality: u8

Quality of lossy WebP’s alpha channel, 0–100; 100, the default, keeps it lossless.

§near_lossless: bool

Lossless WebP that may change pixels a little where it saves bytes, by as much as quality allows (sharp’s nearLossless; 100 changes nothing). Implies lossless.

§chroma_subsampling: Option<ChromaSubsampling>

Chroma sampling for JPEG and AVIF. None is each format’s default: for JPEG 4:2:0 below quality 90 and 4:4:4 from it, for AVIF 4:2:0.

§keep_exif: bool

Copy the source’s EXIF block into the output (sharp’s keepExif), where the format has a place for one: JPEG, PNG, WebP and AVIF. Off by default, which strips camera data and location from uploads.

§keep_xmp: bool

Copy the source’s XMP packet into the output (sharp’s keepXmp), where the format has a place for one: JPEG, PNG, WebP, TIFF, AVIF and JPEG XL. Off by default, as for EXIF.

§progressive: bool

Write JPEG as progressive (sharp’s progressive): a coarse picture first, sharpened by later scans, and usually a little smaller. The encoder then holds the image’s coefficients rather than streaming.

Implementations§

Source§

impl EncodeOptions

Source

pub const DEFAULT_QUALITY: u8 = 80

The default quality used when none is given.

Source

pub fn with_quality(quality: u8) -> Result<EncodeOptions, PixelsError>

Options with the given quality.

§Errors

Returns PixelsError::InvalidArgument unless quality is in 1..=100.

Source

pub const fn with_lossless(self, lossless: bool) -> EncodeOptions

These options with lossless replaced.

Source

pub fn with_effort(self, effort: u8) -> Result<EncodeOptions, PixelsError>

These options with an encoding effort, 0..=10 (see EncodeOptions::effort). A value past a format’s own range is clamped to it.

§Errors

Returns PixelsError::InvalidArgument above 10.

Source

pub fn with_compression_level( self, level: u8, ) -> Result<EncodeOptions, PixelsError>

These options with a PNG DEFLATE level, 0..=9.

§Errors

Returns PixelsError::InvalidArgument above 9.

Source

pub const fn with_palette(self, palette: bool) -> EncodeOptions

These options with palette PNG on or off.

Source

pub fn with_colours(self, colours: u16) -> Result<EncodeOptions, PixelsError>

These options with a palette size, 2..=256, which for PNG implies a palette.

§Errors

Returns PixelsError::InvalidArgument outside 2..=256.

Source

pub fn with_dither(self, dither: f32) -> Result<EncodeOptions, PixelsError>

These options with a dithering level, 0..=1, which for PNG implies a palette.

§Errors

Returns PixelsError::InvalidArgument outside 0..=1.

Source

pub fn with_alpha_quality( self, quality: u8, ) -> Result<EncodeOptions, PixelsError>

These options with a lossy WebP alpha quality, 0..=100.

§Errors

Returns PixelsError::InvalidArgument above 100.

Source

pub const fn with_near_lossless(self, near_lossless: bool) -> EncodeOptions

These options with near-lossless WebP on or off.

Source

pub const fn with_chroma_subsampling( self, subsampling: ChromaSubsampling, ) -> EncodeOptions

These options with an explicit chroma subsampling.

Source

pub const fn with_keep_exif(self, keep: bool) -> EncodeOptions

These options keeping the source’s EXIF, or not.

Source

pub const fn with_keep_xmp(self, keep: bool) -> EncodeOptions

These options with keep_xmp replaced.

Source

pub const fn with_keep_metadata(self, keep: bool) -> EncodeOptions

These options keeping EXIF and XMP both (sharp’s keepMetadata; the ICC profile is always kept).

Source

pub const fn with_progressive(self, progressive: bool) -> EncodeOptions

These options with progressive JPEG on or off.

Source

pub const fn wants_palette(&self) -> bool

Whether a PNG should be written as a palette image.

Trait Implementations§

Source§

impl Clone for EncodeOptions

Source§

fn clone(&self) -> EncodeOptions

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for EncodeOptions

Source§

impl Debug for EncodeOptions

Source§

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

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

impl Default for EncodeOptions

Source§

fn default() -> EncodeOptions

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

impl PartialEq for EncodeOptions

Source§

fn eq(&self, other: &EncodeOptions) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for EncodeOptions

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.