Skip to main content

EncodeImage

Trait EncodeImage 

Source
pub trait EncodeImage<P: Pixel> {
    // Required method
    fn encode_image(
        &self,
        image: ImageRef<'_, P>,
        out: &mut Vec<u8>,
    ) -> Result<usize>;

    // Provided method
    fn encode_to_vec(&self, image: ImageRef<'_, P>) -> Result<Vec<u8>> { ... }
}
Expand description

Encodes an ImageRef of a specific pixel layout P into a compressed byte stream.

A codec implements this once per pixel layout it supports (impl EncodeImage<Rgb8> for …, impl EncodeImage<Cmyk8> for …, …), so asking it to encode an unsupported layout is a compile error rather than a runtime Unsupported. The input is pre-validated by ImageRef::new, so an implementation never re-checks the buffer length. Bytes are appended to out to keep callers that reuse a scratch buffer allocation-conscious.

Required Methods§

Source

fn encode_image( &self, image: ImageRef<'_, P>, out: &mut Vec<u8>, ) -> Result<usize>

Encode image into out (appended), returning the number of bytes written.

§Errors

Returns Error::Unsupported if the requested encoder configuration is not implemented, or Error::InvalidInput if the image violates a format constraint (e.g. a dimension limit).

Provided Methods§

Source

fn encode_to_vec(&self, image: ImageRef<'_, P>) -> Result<Vec<u8>>

Encode image into a fresh Vec, returning the encoded bytes.

A convenience over EncodeImage::encode_image for callers that just want the bytes; reach for encode_image with a reused &mut Vec<u8> when encoding many images and you want to amortise the allocation.

§Errors

As EncodeImage::encode_image.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§