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,
impl<T> JpegDecoder<T>where
T: ZReaderTrait,
fn default(options: DecoderOptions, buffer: T) -> Self
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();
Sourcefn decode_headers_internal(&mut self) -> Result<(), DecodeErrors>
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)
pub(crate) fn parse_marker_inner( &mut self, m: Marker, ) -> Result<(), DecodeErrors>
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
Sourcepub(crate) fn set_upsampling(&mut self) -> Result<(), DecodeErrors>
pub(crate) fn set_upsampling(&mut self) -> Result<(), DecodeErrors>
Set up-sampling routines in case an image is down sampled
Sourcepub(crate) fn width(&self) -> u16
pub(crate) fn width(&self) -> u16
Get the width of the image as a u16
The width lies between 1 and 65535
Source§impl<T: ZReaderTrait> JpegDecoder<T>
impl<T: ZReaderTrait> JpegDecoder<T>
Sourcepub(crate) fn check_tables(&self) -> Result<(), DecodeErrors>
pub(crate) fn check_tables(&self) -> Result<(), DecodeErrors>
Check for existence of DC and AC Huffman Tables
Sourcepub(crate) fn decode_mcu_ycbcr_baseline(
&mut self,
pixels: &mut [u8],
) -> Result<(), DecodeErrors>
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.
Sourcepub(crate) fn finish_baseline_decoding(
&mut self,
block: &[Vec<i16>; 4],
_mcu_width: usize,
pixels: &mut [u8],
) -> Result<(), DecodeErrors>
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.
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>
pub(crate) fn handle_rst( &mut self, stream: &mut BitStream, ) -> Result<(), DecodeErrors>
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>
impl<T: ZReaderTrait> JpegDecoder<T>
Sourcepub(crate) fn decode_mcu_ycbcr_progressive(
&mut self,
pixels: &mut [u8],
) -> Result<(), DecodeErrors>
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.
Sourcefn reset_prog_params(&mut self, stream: &mut BitStream)
fn reset_prog_params(&mut self, stream: &mut BitStream)
Reset progressive parameters