Skip to main content

Encoder

Trait Encoder 

Source
pub trait Encoder: Send {
    // Required methods
    fn write_header(
        &mut self,
        desc: &ImageDescriptor,
        sink: &mut dyn Sink,
    ) -> Result<()>;
    fn write_row(&mut self, row: &[u8], sink: &mut dyn Sink) -> Result<()>;
    fn finish(&mut self, sink: &mut dyn Sink) -> Result<()>;

    // Provided method
    fn set_icc_profile(&mut self, profile: Option<&[u8]>) -> Result<()> { ... }
}
Expand description

Encodes rows of pixels into a byte stream.

The sink is passed to each call rather than held by the encoder, which keeps Encoder object-safe and lets the caller retain ownership of the destination. Encoders write incrementally as rows arrive; they must not buffer the whole image unless the format leaves no choice (ADR-0005).

Required Methods§

Source

fn write_header( &mut self, desc: &ImageDescriptor, sink: &mut dyn Sink, ) -> Result<()>

Begin an image, writing any container header.

Must be called exactly once, before any Encoder::write_row.

§Errors

Returns PixelsError::Io on sink failure, or PixelsError::Unsupported if the encoder cannot represent desc (for example an alpha channel in a format without one).

Source

fn write_row(&mut self, row: &[u8], sink: &mut dyn Sink) -> Result<()>

Write the next row, top to bottom.

row is exactly ImageDescriptor::row_bytes long.

§Errors

Returns PixelsError::Io on sink failure, or PixelsError::InvalidArgument if row is the wrong length or more rows are written than the header declared.

Source

fn finish(&mut self, sink: &mut dyn Sink) -> Result<()>

Finish the image, writing any trailer and flushing the sink.

§Errors

Returns PixelsError::Io on sink failure, or PixelsError::Malformed if fewer rows were written than the header declared — a partial image is never silently emitted (ARCHITECTURE §Failure model).

Provided Methods§

Source

fn set_icc_profile(&mut self, profile: Option<&[u8]>) -> Result<()>

Embed profile as the image’s ICC colour profile, or write none.

Called before Encoder::write_header, whose header usually holds it. The default ignores it, which is right for a format with nowhere to put one (raw pixels, GIF): the pixels are written as given either way.

§Errors

Returns PixelsError::InvalidArgument if the header has already been written.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§