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,
impl<T> JpegDecoder<T>where
T: ZReaderTrait,
sourcepub fn decode(&mut self) -> Result<Vec<u8>, DecodeErrors>
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
sourcepub fn new(stream: T) -> JpegDecoder<T>
pub fn new(stream: T) -> JpegDecoder<T>
sourcepub fn info(&self) -> Option<ImageInfo>
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
sourcepub fn output_buffer_size(&self) -> Option<usize>
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 imageNone
: Indicates the image was not decoded, or image dimensions would overflow a usize
sourcepub const fn get_options(&self) -> &DecoderOptions
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);
sourcepub fn get_input_colorspace(&self) -> Option<ColorSpace>
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
sourcepub fn set_options(&mut self, options: DecoderOptions)
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();
sourcepub fn icc_profile(&self) -> Option<Vec<u8>>
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 imageNone
: May indicate an error in the ICC profile , non-existence of an ICC profile, or that the headers weren’t decoded.
sourcepub fn exif(&self) -> Option<&Vec<u8>>
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
- The image doesn’t have exif data
- The image headers haven’t been decoded
sourcepub fn get_output_colorspace(&self) -> Option<ColorSpace>
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
sourcepub fn decode_into(&mut self, out: &mut [u8]) -> Result<(), DecodeErrors>
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();
sourcepub fn decode_headers(&mut self) -> Result<(), DecodeErrors>
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
sourcepub fn new_with_options(buf: T, options: DecoderOptions) -> JpegDecoder<T>
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 fromoptions
: Options specific to this decoder instance