Struct JpegDecoder
pub struct JpegDecoder<T> { /* private fields */ }
A JPEG Decoder Instance.
Implementations
impl<T> JpegDecoder<T>
where
T: ZByteReaderTrait,
fn decode(&mut self) -> Result<Vec<u8>, DecodeErrors>Decode a buffer already in memory
The buffer should be a valid jpeg file, perhaps created by the command
std:::fs::read()or a JPEG file downloaded from the internet.Errors
See DecodeErrors for an explanation
fn new(stream: T) -> JpegDecoder<T>Create a new Decoder instance
Arguments
stream: The raw bytes of a jpeg file.
fn info(&self) -> Option<ImageInfo>Returns the image information
This must be called after a subsequent call to
decodeordecode_headersit will returnNoneReturns
Some(info): Image information,width, height, number of components- None: Indicates image headers haven't been decoded
fn output_buffer_size(&self) -> Option<usize>Return the number of bytes required to hold a decoded image frame decoded using the given input transformations
Returns
Some(usize): Minimum size for a buffer needed to decode the imageNone: Indicates the image was not decoded, or image dimensions would overflow a usize
const fn options(&self) -> &DecoderOptionsGet an immutable reference to the decoder options for the decoder instance
This can be used to modify options before actual decoding but after initial creation
Example
use zune_core::bytestream::ZCursor; use zune_jpeg::JpegDecoder; let mut decoder = JpegDecoder::new(ZCursor::new(&[])); // get current options let mut options = decoder.options(); // modify it let new_options = options.set_max_width(10); // set it back decoder.set_options(new_options);fn input_colorspace(&self) -> Option<ColorSpace>Return the input colorspace of the image
This indicates the colorspace that is present in the image, but this may be different to the colorspace that the output will be transformed to
Returns
-
Some(Colorspace): Input colorspace- None : Indicates the headers weren't decoded
fn set_options(&mut self, options: DecoderOptions)Set decoder options
This can be used to set new options even after initialization but before decoding.
This does not bear any significance after decoding an image
Arguments
options: New decoder options
Example
Set maximum jpeg progressive passes to be 4
use zune_core::bytestream::ZCursor; use zune_jpeg::JpegDecoder; let mut decoder =JpegDecoder::new(ZCursor::new(&[])); // this works also because DecoderOptions implements `Copy` let options = decoder.options().jpeg_set_max_scans(4); // set the new options decoder.set_options(options); // now decode decoder.decode().unwrap();fn icc_profile(&self) -> Option<Vec<u8>>Get the embedded ICC profile if it exists and is correct
One needs not to decode the whole image to extract this, calling
decode_headersfor an image with an ICC profile allows you to decode thisReturns
Some(Vec<u8>): The raw ICC profile of the imageNone: May indicate an error in the ICC profile , non-existence of an ICC profile, or that the headers weren't decoded.
fn exif(&self) -> Option<&Vec<u8>>Return the exif data for the file
This returns the raw exif data starting at the TIFF header
Returns
-
Some(data): The raw exif data, if present in the image-
None: May indicate the following
- The image doesn't have exif data
- The image headers haven't been decoded
-
fn xmp(&self) -> Option<&Vec<u8>>Return the XMP data for the file
This returns raw XMP data starting at the XML header One needs an XML/XMP decoder to extract valuable metadata
Returns
Some(data): Raw xmp dataNone: May indicate the following- The image does not have xmp data
- The image headers have not been decoded
Example
use zune_core::bytestream::ZCursor; use zune_jpeg::JpegDecoder; let mut decoder = JpegDecoder::new(ZCursor::new(&[])); // decode headers to extract xmp metadata if present decoder.decode_headers().unwrap(); if let Some(data) = decoder.xmp(){ let stringified = String::from_utf8_lossy(data); println!("XMP") } else{ println!("No XMP Found") }fn iptc(&self) -> Option<&Vec<u8>>Return the IPTC data for the file
This returns the raw IPTC data.
Returns
-
Some(data): The raw IPTC data, if present in the image-
None: May indicate the following
- The image doesn't have IPTC data
- The image headers haven't been decoded
-
fn output_colorspace(&self) -> Option<ColorSpace>Get the output colorspace the image pixels will be decoded into
Note.
This field can only be regarded after decoding headers, as markers such as Adobe APP14 may dictate different colorspaces than requested.
Calling
decode_headersis sufficient to know what colorspace the output is, if this is called afterdecodeit indicates the colorspace the output is currently inAdditionally not all input->output colorspace mappings are supported but all input colorspaces can map to RGB colorspace, so that's a safe bet if one is handling image formats
Returns
Some(Colorspace): If headers have been decoded, the colorspace the output array will be in- `None
fn decode_into(&mut self, out: &mut [u8]) -> Result<(), DecodeErrors>Decode into a pre-allocated buffer
It is an error if the buffer size is smaller than
output_buffer_size()If the buffer is bigger than expected, we ignore the end padding bytes
Example
- Read headers and then alloc a buffer big enough to hold the image
use zune_core::bytestream::ZCursor; use zune_jpeg::JpegDecoder; let mut decoder = JpegDecoder::new(ZCursor::new(&[])); // before we get output, we must decode the headers to get width // height, and input colorspace decoder.decode_headers().unwrap(); let mut out = vec![0;decoder.output_buffer_size().unwrap()]; // write into out decoder.decode_into(&mut out).unwrap();fn decode_headers(&mut self) -> Result<(), DecodeErrors>Read only headers from a jpeg image buffer
This allows you to extract important information like image width and height without decoding the full image
Examples
use zune_core::bytestream::ZCursor; use zune_jpeg::{JpegDecoder}; let img_data = std::fs::read("a_valid.jpeg").unwrap(); let mut decoder = JpegDecoder::new(ZCursor::new(&img_data)); decoder.decode_headers().unwrap(); println!("Total decoder dimensions are : {:?} pixels",decoder.dimensions()); println!("Number of components in the image are {}", decoder.info().unwrap().components);Errors
See DecodeErrors enum for list of possible errors during decoding
fn new_with_options(buf: T, options: DecoderOptions) -> JpegDecoder<T>Create a new decoder with the specified options to be used for decoding an image
Arguments
buf: The input buffer from where we will pull in compressed jpeg bytes fromoptions: Options specific to this decoder instance
const fn dimensions(&self) -> Option<(usize, usize)>Get image dimensions as a tuple of width and height or
Noneif the image hasn't been decoded.Returns
Some(width,height): Image dimensions- None : The image headers haven't been decoded
Auto Trait Implementations
impl<T> Freeze for JpegDecoder<T>
where
ZReader<T>: Freeze,
impl<T> RefUnwindSafe for JpegDecoder<T>
where
ZReader<T>: RefUnwindSafe,
impl<T> Send for JpegDecoder<T>
where
ZReader<T>: Send,
impl<T> Sync for JpegDecoder<T>
where
ZReader<T>: Sync,
impl<T> Unpin for JpegDecoder<T>
where
ZReader<T>: Unpin,
impl<T> UnsafeUnpin for JpegDecoder<T>
where
ZReader<T>: UnsafeUnpin,
impl<T> UnwindSafe for JpegDecoder<T>
where
ZReader<T>: UnwindSafe,
Blanket Implementations
impl<T> Any for JpegDecoder<T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for JpegDecoder<T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for JpegDecoder<T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for JpegDecoder<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for JpegDecoder<T>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for JpegDecoder<T>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for JpegDecoder<T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>