Struct JpegDecoder

Source
pub struct JpegDecoder<T: ZReaderTrait> {
Show 33 fields pub(crate) info: ImageInfo, pub(crate) qt_tables: [Option<[i32; 64]>; 4], pub(crate) dc_huffman_tables: [Option<HuffmanTable>; 4], pub(crate) ac_huffman_tables: [Option<HuffmanTable>; 4], pub(crate) components: Vec<Components>, pub(crate) h_max: usize, pub(crate) v_max: usize, pub(crate) mcu_width: usize, pub(crate) mcu_height: usize, pub(crate) mcu_x: usize, pub(crate) mcu_y: usize, pub(crate) is_interleaved: bool, pub(crate) sub_sample_ratio: SampleRatios, pub(crate) input_colorspace: ColorSpace, pub(crate) is_progressive: bool, pub(crate) spec_start: u8, pub(crate) spec_end: u8, pub(crate) succ_high: u8, pub(crate) succ_low: u8, pub(crate) num_scans: u8, pub(crate) idct_func: fn(&mut [i32; 64], &mut [i16], usize), pub(crate) color_convert_16: fn(&[i16; 16], &[i16; 16], &[i16; 16], &mut [u8], &mut usize), pub(crate) z_order: [usize; 4], pub(crate) restart_interval: usize, pub(crate) todo: usize, pub(crate) options: DecoderOptions, pub(crate) stream: ZByteReader<T>, pub(crate) headers_decoded: bool, pub(crate) seen_sof: bool, pub(crate) exif_data: Option<Vec<u8>>, pub(crate) icc_data: Vec<ICCChunk>, pub(crate) is_mjpeg: bool, pub(crate) coeff: usize,
}
Expand description

A JPEG Decoder Instance.

Fields§

§info: ImageInfo

Struct to hold image information from SOI

§qt_tables: [Option<[i32; 64]>; 4]

Quantization tables, will be set to none and the tables will be moved to components field

§dc_huffman_tables: [Option<HuffmanTable>; 4]

DC Huffman Tables with a maximum of 4 tables for each component

§ac_huffman_tables: [Option<HuffmanTable>; 4]

AC Huffman Tables with a maximum of 4 tables for each component

§components: Vec<Components>

Image components, holds information like DC prediction and quantization tables of a component

§h_max: usize

maximum horizontal component of all channels in the image

§v_max: usize§mcu_width: usize

mcu’s width (interleaved scans)

§mcu_height: usize

MCU height(interleaved scans

§mcu_x: usize

Number of MCU’s in the x plane

§mcu_y: usize

Number of MCU’s in the y plane

§is_interleaved: bool

Is the image interleaved?

§sub_sample_ratio: SampleRatios§input_colorspace: ColorSpace

Image input colorspace, should be YCbCr for a sane image, might be grayscale too

§is_progressive: bool

Is the image progressive?

§spec_start: u8

Start of spectral scan

§spec_end: u8

End of spectral scan

§succ_high: u8

Successive approximation bit position high

§succ_low: u8

Successive approximation bit position low

§num_scans: u8

Number of components.

§idct_func: fn(&mut [i32; 64], &mut [i16], usize)

Dequantize and idct function

§color_convert_16: fn(&[i16; 16], &[i16; 16], &[i16; 16], &mut [u8], &mut usize)§z_order: [usize; 4]§restart_interval: usize

restart markers

§todo: usize§options: DecoderOptions§stream: ZByteReader<T>§headers_decoded: bool§seen_sof: bool§exif_data: Option<Vec<u8>>§icc_data: Vec<ICCChunk>§is_mjpeg: bool§coeff: usize

Implementations§

Source§

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

Source

fn default(options: DecoderOptions, buffer: T) -> Self

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

fn decode_headers_internal(&mut self) -> Result<(), DecodeErrors>

Decode Decoder headers

This routine takes care of parsing supported headers from a Decoder image

§Supported Headers
  • APP(0)
  • SOF(O)
  • DQT -> Quantization tables
  • DHT -> Huffman tables
  • SOS -> Start of Scan
§Unsupported Headers
  • SOF(n) -> Decoder images which are not baseline/progressive
  • DAC -> Images using Arithmetic tables
  • JPG(n)
Source

pub(crate) fn parse_marker_inner( &mut self, m: Marker, ) -> Result<(), DecodeErrors>

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(crate) fn set_upsampling(&mut self) -> Result<(), DecodeErrors>

Set up-sampling routines in case an image is down sampled

Source

pub(crate) fn width(&self) -> u16

Get the width of the image as a u16

The width lies between 1 and 65535

Source

pub(crate) fn height(&self) -> u16

Get the height of the image as a u16

The height lies between 1 and 65535

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
Source§

impl<T: ZReaderTrait> JpegDecoder<T>

Source

pub(crate) fn check_tables(&self) -> Result<(), DecodeErrors>

Check for existence of DC and AC Huffman Tables

Source

pub(crate) fn decode_mcu_ycbcr_baseline( &mut self, pixels: &mut [u8], ) -> Result<(), DecodeErrors>

Decode MCUs and carry out post processing.

This is the main decoder loop for the library, the hot path.

Because of this, we pull in some very crazy optimization tricks hence readability is a pinch here.

Source

pub(crate) fn finish_baseline_decoding( &mut self, block: &[Vec<i16>; 4], _mcu_width: usize, pixels: &mut [u8], ) -> Result<(), DecodeErrors>

Process all MCUs when baseline decoding has been processing them component-after-component. For simplicity this assembles the dequantized blocks in the order that the post processing of an interleaved baseline decoding would use.

Source

fn decode_mcu_width<const PROGRESSIVE: bool>( &mut self, mcu_width: usize, mcu_height: usize, tmp: &mut [i32; 64], stream: &mut BitStream, progressive: &mut [Vec<i16>; 4], ) -> Result<McuContinuation, DecodeErrors>

Source

pub(crate) fn handle_rst( &mut self, stream: &mut BitStream, ) -> Result<(), DecodeErrors>

Source

pub(crate) fn post_process( &mut self, pixels: &mut [u8], i: usize, mcu_height: usize, width: usize, padded_width: usize, pixels_written: &mut usize, upsampler_scratch_space: &mut [i16], ) -> Result<(), DecodeErrors>

Source§

impl<T: ZReaderTrait> JpegDecoder<T>

Source

pub(crate) fn decode_mcu_ycbcr_progressive( &mut self, pixels: &mut [u8], ) -> Result<(), DecodeErrors>

Decode a progressive image

This routine decodes a progressive image, stopping if it finds any error.

Source

fn reset_prog_params(&mut self, stream: &mut BitStream)

Reset progressive parameters

Source

fn parse_entropy_coded_data( &mut self, stream: &mut BitStream, buffer: &mut [Vec<i16>; 4], ) -> Result<(), DecodeErrors>

Source

pub(crate) fn handle_rst_main( &mut self, stream: &mut BitStream, ) -> Result<(), DecodeErrors>

Source

fn finish_progressive_decoding( &mut self, block: &[Vec<i16>; 4], _mcu_width: usize, pixels: &mut [u8], ) -> Result<(), DecodeErrors>

Source

pub(crate) fn reset_params(&mut self)

Auto Trait Implementations§

§

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

§

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>,

Source§

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>,

Source§

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.