Function turbojpeg::compress

source ·
pub fn compress(
    image: Image<&[u8]>,
    quality: i32,
    subsamp: Subsamp
) -> Result<OwnedBuf>
Expand description

Compress an image to JPEG.

Uses the given quality and chrominance subsampling option and returns the JPEG data in a buffer owned by TurboJPEG. If this function does not fit your needs, please see Compressor.

§Example

// create an image (a Mandelbrot set visualization)
let image = turbojpeg::Image::mandelbrot(500, 500, turbojpeg::PixelFormat::RGB);

// compress the image into JPEG with quality 75 and in grayscale
// (we use as_deref() to convert from &Image<Vec<u8>> to Image<&[u8]>)
let jpeg_data = turbojpeg::compress(image.as_deref(), 75, turbojpeg::Subsamp::Sub2x2)?;

// write the JPEG to disk
std::fs::write(std::env::temp_dir().join("mandelbrot.jpg"), &jpeg_data)?;