Struct zune_jpeg::JpegDecoder

source ·
pub struct JpegDecoder<T: ZReaderTrait> { /* private fields */ }
Expand description

A JPEG Decoder Instance.

Implementations§

source§

impl<T> JpegDecoder<T>
where T: ZReaderTrait,

source

pub 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

source

pub fn new(stream: T) -> JpegDecoder<T>

Create a new Decoder instance

Arguments
  • stream: The raw bytes of a jpeg file.
source

pub fn info(&self) -> Option<ImageInfo>

Returns the image information

This must be called after a subsequent call to decode or decode_headers it will return None

Returns
  • Some(info): Image information,width, height, number of components
  • None: Indicates image headers haven’t been decoded
source

pub 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 image
  • None: Indicates the image was not decoded, or image dimensions would overflow a usize
source

pub const fn get_options(&self) -> &DecoderOptions

Get a mutable 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_jpeg::JpegDecoder;

let mut decoder = JpegDecoder::new(&[]);
// get current options
let mut options = decoder.get_options();
// modify it
 let new_options = options.set_max_width(10);
// set it back
decoder.set_options(new_options);
source

pub fn get_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
source

pub 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_jpeg::JpegDecoder;
let mut decoder =JpegDecoder::new(&[]);
// this works also because DecoderOptions implements `Copy`
let options = decoder.get_options().jpeg_set_max_scans(4);
// set the new options
decoder.set_options(options);
// now decode
decoder.decode().unwrap();
source

pub 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_headers for an image with an ICC profile allows you to decode this

Returns
  • Some(Vec<u8>): The raw ICC profile of the image
  • None: May indicate an error in the ICC profile , non-existence of an ICC profile, or that the headers weren’t decoded.
source

pub 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

    1. The image doesn’t have exif data
    2. The image headers haven’t been decoded
source

pub fn get_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_headers is sufficient to know what colorspace the output is, if this is called after decode it indicates the colorspace the output is currently in

Additionally 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
source

pub 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_jpeg::JpegDecoder;
let mut decoder = JpegDecoder::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();
source

pub 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_jpeg::{JpegDecoder};

let img_data = std::fs::read("a_valid.jpeg").unwrap();
let mut decoder = JpegDecoder::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

source

pub 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 from
  • options: Options specific to this decoder instance
source

pub const fn dimensions(&self) -> Option<(usize, usize)>

Get image dimensions as a tuple of width and height or None if 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> RefUnwindSafe for JpegDecoder<T>
where T: RefUnwindSafe,

§

impl<T> Send for JpegDecoder<T>
where T: Send,

§

impl<T> Sync for JpegDecoder<T>
where T: Sync,

§

impl<T> Unpin for JpegDecoder<T>
where T: Unpin,

§

impl<T> UnwindSafe for JpegDecoder<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.