Struct turbojpeg::Decompressor
source · pub struct Decompressor { /* private fields */ }
Expand description
Decompresses JPEG data into raw pixels.
Implementations§
source§impl Decompressor
impl Decompressor
sourcepub fn new() -> Result<Decompressor>
pub fn new() -> Result<Decompressor>
Create a new decompressor instance.
sourcepub fn read_header(&mut self, jpeg_data: &[u8]) -> Result<DecompressHeader>
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));
sourcepub fn decompress(
&mut self,
jpeg_data: &[u8],
output: Image<&mut [u8]>
) -> Result<()>
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]);
sourcepub fn decompress_to_yuv(
&mut self,
jpeg_data: &[u8],
output: YuvImage<&mut [u8]>
) -> Result<()>
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
impl Debug for Decompressor
impl Send for Decompressor
Auto Trait Implementations§
impl Freeze for Decompressor
impl RefUnwindSafe for Decompressor
impl !Sync for Decompressor
impl Unpin for Decompressor
impl UnwindSafe for Decompressor
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more