Struct turbojpeg::Decompressor

source ·
pub struct Decompressor { /* private fields */ }
Expand description

Decompresses JPEG data into raw pixels.

Implementations§

source§

impl Decompressor

source

pub fn new() -> Result<Decompressor>

Create a new decompressor instance.

source

pub fn read_header(&mut self, jpeg_data: &[u8]) -> Result<DecompressHeader>

Read the JPEG header without decompressing the image.

§Example
// read JPEG data from file
let jpeg_data = std::fs::read("examples/parrots.jpg")?;

// initialize a decompressor
let mut decompressor = turbojpeg::Decompressor::new()?;

// read the JPEG header
let header = decompressor.read_header(&jpeg_data)?;
assert_eq!((header.width, header.height), (384, 256));
source

pub fn decompress( &mut self, jpeg_data: &[u8], output: Image<&mut [u8]> ) -> Result<()>

Decompress a JPEG image in jpeg_data into output.

The decompressed image is stored in the pixel data of the given output image, which must be fully initialized by the caller. Use read_header() to determine the image size before calling this method.

§Example
// read JPEG data from file
let jpeg_data = std::fs::read("examples/parrots.jpg")?;

// initialize a decompressor
let mut decompressor = turbojpeg::Decompressor::new()?;

// read the JPEG header
let header = decompressor.read_header(&jpeg_data)?;

// initialize the image (Image<Vec<u8>>)
let mut image = turbojpeg::Image {
    pixels: vec![0; 4 * header.width * header.height],
    width: header.width,
    pitch: 4 * header.width, // size of one image row in memory
    height: header.height,
    format: turbojpeg::PixelFormat::RGBA,
};

// decompress the JPEG into the image
// (we use as_deref_mut() to convert from &mut Image<Vec<u8>> into Image<&mut [u8]>)
decompressor.decompress(&jpeg_data, image.as_deref_mut())?;
assert_eq!(&image.pixels[0..4], &[122, 118, 89, 255]);
source

pub fn decompress_to_yuv( &mut self, jpeg_data: &[u8], output: YuvImage<&mut [u8]> ) -> Result<()>

Decompress a JPEG image in jpeg_data into output as YUV without changing color space.

The decompressed image is stored in the pixel data of the given output image, which must be fully initialized by the caller. Use read_header() to determine the image size before calling this method.

§Example
// read JPEG data from file
let jpeg_data = std::fs::read("examples/parrots.jpg")?;

// initialize a decompressor
let mut decompressor = turbojpeg::Decompressor::new()?;

// read the JPEG header
let header = decompressor.read_header(&jpeg_data)?;
// calculate YUV pixels length
let align = 4;
let yuv_pixels_len = turbojpeg::yuv_pixels_len(header.width, align, header.height, header.subsamp);

// initialize the image (YuvImage<Vec<u8>>)
let mut image = turbojpeg::YuvImage {
    pixels: vec![0; yuv_pixels_len.unwrap()],
    width: header.width,
    align,
    height: header.height,
    subsamp: header.subsamp,
};

// decompress the JPEG into the image
// (we use as_deref_mut() to convert from &mut YuvImage<Vec<u8>> into YuvImage<&mut [u8]>)
decompressor.decompress_to_yuv(&jpeg_data, image.as_deref_mut())?;
assert_eq!(&image.pixels[0..4], &[116, 117, 118, 119]);

Trait Implementations§

source§

impl Debug for Decompressor

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Send for Decompressor

Auto Trait Implementations§

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.