Trait image::ImageDecoder
source · pub trait ImageDecoder<'a>: Sized {
type Reader: Read + 'a;
// Required methods
fn dimensions(&self) -> (u32, u32);
fn color_type(&self) -> ColorType;
fn into_reader(self) -> ImageResult<Self::Reader>;
// Provided methods
fn original_color_type(&self) -> ExtendedColorType { ... }
fn icc_profile(&mut self) -> Option<Vec<u8>> { ... }
fn total_bytes(&self) -> u64 { ... }
fn scanline_bytes(&self) -> u64 { ... }
fn read_image(self, buf: &mut [u8]) -> ImageResult<()> { ... }
fn read_image_with_progress<F: Fn(Progress)>(
self,
buf: &mut [u8],
progress_callback: F
) -> ImageResult<()> { ... }
fn set_limits(&mut self, limits: Limits) -> ImageResult<()> { ... }
}Expand description
The trait that all decoders implement
Required Associated Types§
Required Methods§
sourcefn dimensions(&self) -> (u32, u32)
fn dimensions(&self) -> (u32, u32)
Returns a tuple containing the width and height of the image
sourcefn color_type(&self) -> ColorType
fn color_type(&self) -> ColorType
Returns the color type of the image data produced by this decoder
sourcefn into_reader(self) -> ImageResult<Self::Reader>
fn into_reader(self) -> ImageResult<Self::Reader>
Returns a reader that can be used to obtain the bytes of the image. For the best
performance, always try to read at least scanline_bytes from the reader at a time. Reading
fewer bytes will cause the reader to perform internal buffering.
Provided Methods§
sourcefn original_color_type(&self) -> ExtendedColorType
fn original_color_type(&self) -> ExtendedColorType
Returns the color type of the image file before decoding
sourcefn icc_profile(&mut self) -> Option<Vec<u8>>
fn icc_profile(&mut self) -> Option<Vec<u8>>
Returns the ICC color profile embedded in the image
For formats that don’t support embedded profiles this function will always return None.
This feature is currently only supported for the JPEG, PNG, and AVIF formats.
sourcefn total_bytes(&self) -> u64
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.
sourcefn scanline_bytes(&self) -> u64
fn scanline_bytes(&self) -> u64
Returns the minimum number of bytes that can be efficiently read from this decoder. This may
be as few as 1 or as many as total_bytes().
sourcefn read_image(self, buf: &mut [u8]) -> ImageResult<()>
fn read_image(self, buf: &mut [u8]) -> ImageResult<()>
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
}
sourcefn read_image_with_progress<F: Fn(Progress)>(
self,
buf: &mut [u8],
progress_callback: F
) -> ImageResult<()>
fn read_image_with_progress<F: Fn(Progress)>( self, buf: &mut [u8], progress_callback: F ) -> ImageResult<()>
Same as read_image but periodically calls the provided callback to give updates on loading
progress.
sourcefn set_limits(&mut self, limits: Limits) -> ImageResult<()>
fn set_limits(&mut self, limits: Limits) -> ImageResult<()>
Set decoding limits for this decoder. 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.
Object Safety§
Implementors§
source§impl<'a, R: 'a + BufRead> ImageDecoder<'a> for HdrAdapter<R>
Available on crate feature hdr only.
impl<'a, R: 'a + BufRead> ImageDecoder<'a> for HdrAdapter<R>
hdr only.source§impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for BmpDecoder<R>
Available on crate feature bmp only.
impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for BmpDecoder<R>
bmp only.source§impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for IcoDecoder<R>
Available on crate feature ico only.
impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for IcoDecoder<R>
ico only.source§impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for OpenExrDecoder<R>
Available on crate feature exr only.
impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for OpenExrDecoder<R>
exr only.source§impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for TgaDecoder<R>
Available on crate feature tga only.
impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for TgaDecoder<R>
tga only.source§impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for TiffDecoder<R>
Available on crate feature tiff only.
impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for TiffDecoder<R>
tiff only.type Reader = TiffReader<R>
source§impl<'a, R: 'a + Read> ImageDecoder<'a> for AvifDecoder<R>
Available on crate feature avif-decoder and (crate features avif-encoder or avif-decoder) only.
impl<'a, R: 'a + Read> ImageDecoder<'a> for AvifDecoder<R>
avif-decoder and (crate features avif-encoder or avif-decoder) only.source§impl<'a, R: 'a + Read> ImageDecoder<'a> for DdsDecoder<R>
Available on crate feature dds only.
impl<'a, R: 'a + Read> ImageDecoder<'a> for DdsDecoder<R>
dds only.source§impl<'a, R: 'a + Read> ImageDecoder<'a> for DxtDecoder<R>
Available on crate feature dxt only.
impl<'a, R: 'a + Read> ImageDecoder<'a> for DxtDecoder<R>
dxt only.source§impl<'a, R: 'a + Read> ImageDecoder<'a> for FarbfeldDecoder<R>
Available on crate feature farbfeld only.
impl<'a, R: 'a + Read> ImageDecoder<'a> for FarbfeldDecoder<R>
farbfeld only.type Reader = FarbfeldReader<R>
source§impl<'a, R: 'a + Read> ImageDecoder<'a> for GifDecoder<R>
Available on crate feature gif only.
impl<'a, R: 'a + Read> ImageDecoder<'a> for GifDecoder<R>
gif only.source§impl<'a, R: 'a + Read> ImageDecoder<'a> for JpegDecoder<R>
Available on crate feature jpeg only.
impl<'a, R: 'a + Read> ImageDecoder<'a> for JpegDecoder<R>
jpeg only.source§impl<'a, R: 'a + Read> ImageDecoder<'a> for PngDecoder<R>
Available on crate feature png only.
impl<'a, R: 'a + Read> ImageDecoder<'a> for PngDecoder<R>
png only.source§impl<'a, R: 'a + Read> ImageDecoder<'a> for PnmDecoder<R>
Available on crate feature pnm only.
impl<'a, R: 'a + Read> ImageDecoder<'a> for PnmDecoder<R>
pnm only.source§impl<'a, R: 'a + Read> ImageDecoder<'a> for WebPDecoder<R>
Available on crate feature webp only.
impl<'a, R: 'a + Read> ImageDecoder<'a> for WebPDecoder<R>
webp only.source§impl<'a, R: Read + 'a> ImageDecoder<'a> for QoiDecoder<R>
Available on crate feature qoi only.
impl<'a, R: Read + 'a> ImageDecoder<'a> for QoiDecoder<R>
qoi only.