Struct Encoder

pub struct Encoder<'a, W: Write> { /* private fields */ }

PNG Encoder.

This configures the PNG format options such as animation chunks, palette use, color types, auxiliary chunks etc.

FIXME: Configuring APNG might be easier (less individual errors) if we had an adapter which borrows this mutably but guarantees that info.frame_control is not None.

Implementations

impl<'a, W: Write> Encoder<'a, W>

fn new(w: W, width: u32, height: u32) -> Encoder<'static, W>
fn with_info(w: W, info: Info<'a>) -> Result<Encoder<'a, W>, EncodingError>
fn set_animated(&mut self, num_frames: u32, num_plays: u32) -> Result<(), EncodingError>

Specify that the image is animated.

num_frames controls how many frames the animation has, while num_plays controls how many times the animation should be repeated until it stops, if it's zero then it will repeat infinitely.

When this method is returns successfully then the images written will be encoded as fdAT chunks, except for the first image that is still encoded as IDAT. You can control if the first frame should be treated as an animation frame with [Encoder::set_sep_def_img()].

This method returns an error if num_frames is 0.

fn set_sep_def_img(&mut self, sep_def_img: bool) -> Result<(), EncodingError>

Mark the first animated frame as a 'separate default image'.

In APNG each animated frame is preceded by a special control chunk, fcTL. It's up to the encoder to decide if the first image, the standard IDAT data, should be part of the animation by emitting this chunk or by not doing so. A default image that is not part of the animation is often interpreted as a thumbnail.

This method will return an error when animation control was not configured (which is done by calling Encoder::set_animated).

fn set_palette<T: Into<Cow<'a, [u8]>>>(&mut self, palette: T)

Sets the raw byte contents of the PLTE chunk. This method accepts both borrowed and owned byte data.

fn set_trns<T: Into<Cow<'a, [u8]>>>(&mut self, trns: T)

Sets the raw byte contents of the tRNS chunk. This method accepts both borrowed and owned byte data.

fn set_source_gamma(&mut self, source_gamma: ScaledFloat)

Set the display gamma of the source system on which the image was generated or last edited.

fn set_source_chromaticities(&mut self, source_chromaticities: SourceChromaticities)

Set the chromaticities for the source system's display channels (red, green, blue) and the whitepoint of the source system on which the image was generated or last edited.

fn set_source_srgb(&mut self, rendering_intent: SrgbRenderingIntent)

Mark the image data as conforming to the SRGB color space with the specified rendering intent.

Any ICC profiles will be ignored.

Source gamma and chromaticities will be written only if they're set to fallback values specified in 11.3.2.5.

fn write_header(self) -> Result<Writer<W>, EncodingError>

Start encoding by writing the header data.

The remaining data can be supplied by methods on the returned Writer.

fn set_color(&mut self, color: ColorType)

Set the color of the encoded image.

These correspond to the color types in the png IHDR data that will be written. The length of the image data that is later supplied must match the color type, otherwise an error will be emitted.

fn set_depth(&mut self, depth: BitDepth)

Set the indicated depth of the image data.

fn set_compression(&mut self, compression: Compression)

Set compression parameters, see Compression for the available options.

fn set_deflate_compression(&mut self, compression: DeflateCompression)

Provides in-depth customization of DEFLATE compression options.

For a simpler selection of compression options see set_compression.

fn set_filter(&mut self, filter: Filter)

Set the used filter type.

The default filter is Filter::Adaptive which automatically selects the best filter for each row of the image.

You should only change this if you are after very fast compression, and either don't care about compression ratio or know exactly what works best for your images.

fn set_frame_delay(&mut self, numerator: u16, denominator: u16) -> Result<(), EncodingError>

Set the fraction of time every frame is going to be displayed, in seconds.

Note that this parameter can be set for each individual frame after Encoder::write_header is called. (see Writer::set_frame_delay)

If the denominator is 0, it is to be treated as if it were 100 (that is, the numerator then specifies 1/100ths of a second). If the value of the numerator is 0 the decoder should render the next frame as quickly as possible, though viewers may impose a reasonable lower bound.

The default value is 0 for both the numerator and denominator.

This method will return an error if the image is not animated. (see set_animated)

fn set_blend_op(&mut self, op: BlendOp) -> Result<(), EncodingError>

Set the blend operation for every frame.

The blend operation specifies whether the frame is to be alpha blended into the current output buffer content, or whether it should completely replace its region in the output buffer.

Note that this parameter can be set for each individual frame after write_header is called. (see Writer::set_blend_op)

See the BlendOp documentation for the possible values and their effects.

Note that for the first frame the two blend modes are functionally equivalent due to the clearing of the output buffer at the beginning of each play.

The default value is BlendOp::Source.

This method will return an error if the image is not animated. (see set_animated)

fn set_dispose_op(&mut self, op: DisposeOp) -> Result<(), EncodingError>

Set the dispose operation for every frame.

The dispose operation specifies how the output buffer should be changed at the end of the delay (before rendering the next frame)

Note that this parameter can be set for each individual frame after write_header is called (see Writer::set_dispose_op)

See the DisposeOp documentation for the possible values and their effects.

Note that if the first frame uses DisposeOp::Previous it will be treated as DisposeOp::Background.

The default value is DisposeOp::None.

This method will return an error if the image is not animated. (see set_animated)

fn set_pixel_dims(&mut self, pixel_dims: Option<PixelDimensions>)
fn add_text_chunk(&mut self, keyword: String, text: String) -> Result<(), EncodingError>

Convenience function to add tEXt chunks to Info struct

fn add_ztxt_chunk(&mut self, keyword: String, text: String) -> Result<(), EncodingError>

Convenience function to add zTXt chunks to Info struct

fn add_itxt_chunk(&mut self, keyword: String, text: String) -> Result<(), EncodingError>

Convenience function to add iTXt chunks to Info struct

This function only sets the keyword and text field of the iTXt chunk. To set the other fields, create a ITXtChunk directly, and then encode it to the output stream.

fn validate_sequence(&mut self, validate: bool)

Validate the written image sequence.

When validation is turned on (it's turned off by default) then attempts to write more than one IDAT image or images beyond the number of frames indicated in the animation control chunk will fail and return an error result instead. Attempts to finish the image with missing frames will also return an error.

(It's possible to circumvent these checks by writing raw chunks instead.)

Auto Trait Implementations

impl<'a, W> Freeze for Encoder<'a, W> where W: Freeze,

impl<'a, W> RefUnwindSafe for Encoder<'a, W> where W: RefUnwindSafe,

impl<'a, W> Send for Encoder<'a, W> where W: Send,

impl<'a, W> Sync for Encoder<'a, W> where W: Sync,

impl<'a, W> Unpin for Encoder<'a, W> where W: Unpin,

impl<'a, W> UnsafeUnpin for Encoder<'a, W> where W: UnsafeUnpin,

impl<'a, W> UnwindSafe for Encoder<'a, W> where W: UnwindSafe,

Blanket Implementations

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

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Encoder<'a, W> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Encoder<'a, W> where T: ?Sized,

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

impl<T> From<T> for Encoder<'a, W>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for Encoder<'a, W> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom<U> for Encoder<'a, W> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Encoder<'a, W> where U: TryFrom<T>,

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