Trait image::ImageDecoder

source ·
pub trait ImageDecoder {
    // Required methods
    fn dimensions(&self) -> (u32, u32);
    fn color_type(&self) -> ColorType;
    fn read_image(self, buf: &mut [u8]) -> ImageResult<()>
       where Self: Sized;
    fn read_image_boxed(self: Box<Self>, buf: &mut [u8]) -> ImageResult<()>;

    // Provided methods
    fn original_color_type(&self) -> ExtendedColorType { ... }
    fn icc_profile(&mut self) -> ImageResult<Option<Vec<u8>>> { ... }
    fn total_bytes(&self) -> u64 { ... }
    fn set_limits(&mut self, limits: Limits) -> ImageResult<()> { ... }
}
Expand description

The trait that all decoders implement

Required Methods§

source

fn dimensions(&self) -> (u32, u32)

Returns a tuple containing the width and height of the image

source

fn color_type(&self) -> ColorType

Returns the color type of the image data produced by this decoder

source

fn read_image(self, buf: &mut [u8]) -> ImageResult<()>
where Self: Sized,

Returns all the bytes in the image.

This function takes a slice of bytes and writes the pixel data of the image into it. Although not required, for certain color types callers may want to pass buffers which are aligned to 2 or 4 byte boundaries to the slice can be cast to a u16 or u32. To accommodate such casts, the returned contents will always be in native endian.

§Panics

This function panics if buf.len() != self.total_bytes().

§Examples
use zerocopy::{AsBytes, FromBytes};
fn read_16bit_image(decoder: impl ImageDecoder) -> Vec<16> {
    let mut buf: Vec<u16> = vec![0; decoder.total_bytes()/2];
    decoder.read_image(buf.as_bytes());
    buf
}
source

fn read_image_boxed(self: Box<Self>, buf: &mut [u8]) -> ImageResult<()>

Use read_image instead; this method is an implementation detail needed so the trait can be object safe.

Note to implementors: This method should be implemented by calling read_image on the boxed decoder…

    fn read_image_boxed(self: Box<Self>, buf: &mut [u8]) -> ImageResult<()> {
       (*self).read_image(buf)
   }

Provided Methods§

source

fn original_color_type(&self) -> ExtendedColorType

Returns the color type of the image file before decoding

source

fn icc_profile(&mut self) -> ImageResult<Option<Vec<u8>>>

Returns the ICC color profile embedded in the image, or Ok(None) if the image does not have one.

For formats that don’t support embedded profiles this function should always return Ok(None).

source

fn total_bytes(&self) -> u64

Returns the total number of bytes in the decoded image.

This is the size of the buffer that must be passed to read_image or read_image_with_progress. The returned value may exceed usize::MAX, in which case it isn’t actually possible to construct a buffer to decode all the image data into. If, however, the size does not fit in a u64 then u64::MAX is returned.

source

fn set_limits(&mut self, limits: Limits) -> ImageResult<()>

Set the decoder to have the specified limits. See Limits for the different kinds of limits that is possible to set.

Note to implementors: make sure you call Limits::check_support so that decoding fails if any unsupported strict limits are set. Also make sure you call Limits::check_dimensions to check the max_image_width and max_image_height limits.

Implementations on Foreign Types§

source§

impl<T: ?Sized + ImageDecoder> ImageDecoder for Box<T>

source§

fn dimensions(&self) -> (u32, u32)

source§

fn color_type(&self) -> ColorType

source§

fn original_color_type(&self) -> ExtendedColorType

source§

fn icc_profile(&mut self) -> ImageResult<Option<Vec<u8>>>

source§

fn total_bytes(&self) -> u64

source§

fn read_image(self, buf: &mut [u8]) -> ImageResult<()>
where Self: Sized,

source§

fn read_image_boxed(self: Box<Self>, buf: &mut [u8]) -> ImageResult<()>

source§

fn set_limits(&mut self, limits: Limits) -> ImageResult<()>

Implementors§

source§

impl<R: BufRead + Seek> ImageDecoder for BmpDecoder<R>

Available on crate feature bmp only.
source§

impl<R: BufRead + Seek> ImageDecoder for GifDecoder<R>

Available on crate feature gif only.
source§

impl<R: BufRead + Seek> ImageDecoder for IcoDecoder<R>

Available on crate feature ico only.
source§

impl<R: BufRead + Seek> ImageDecoder for JpegDecoder<R>

Available on crate feature jpeg only.
source§

impl<R: BufRead + Seek> ImageDecoder for OpenExrDecoder<R>

Available on crate feature exr only.
source§

impl<R: BufRead + Seek> ImageDecoder for PngDecoder<R>

Available on crate feature png only.
source§

impl<R: BufRead + Seek> ImageDecoder for TiffDecoder<R>

Available on crate feature tiff only.
source§

impl<R: BufRead + Seek> ImageDecoder for WebPDecoder<R>

Available on crate feature webp only.
source§

impl<R: Read> ImageDecoder for AvifDecoder<R>

Available on crate feature avif-native and (crate features avif or avif-native) only.
source§

impl<R: Read> ImageDecoder for DdsDecoder<R>

Available on crate feature dds only.
source§

impl<R: Read> ImageDecoder for FarbfeldDecoder<R>

Available on crate feature ff only.
source§

impl<R: Read> ImageDecoder for HdrDecoder<R>

Available on crate feature hdr only.
source§

impl<R: Read> ImageDecoder for PnmDecoder<R>

Available on crate feature pnm only.
source§

impl<R: Read> ImageDecoder for QoiDecoder<R>

Available on crate feature qoi only.
source§

impl<R: Read> ImageDecoder for TgaDecoder<R>

Available on crate feature tga only.