pub struct JxlSimpleEncoder<'a> { /* private fields */ }
Expand description

A simple jxl encoder

Encoding 16 bit data

  • To encode a 16-bit image, each element needs to be re-interpreted as 2 u8’s in native endian the library will do the appropriate clamping

Multithreading support

Via the thread feature, the library can use multiple threads to speed up compression, one can configure how many threads to open or whether the library should use threads at compile time by enabling or disabling the thread feature and at runtime via EncodeOptions

Example option two threads

use zune_core::bit_depth::BitDepth;
use zune_core::colorspace::ColorSpace;
use zune_core::options::EncoderOptions;
// set threads to be 2 for encoding
let options = EncoderOptions::new(100,100,ColorSpace::RGB,BitDepth::Eight).set_num_threads(2);

Setting set_num_threads to 1 forces single threaded execution

Example

  • Encode grayscale image
use zune_core::colorspace::ColorSpace;
use zune_core::options::EncoderOptions;
use zune_jpegxl::JxlSimpleEncoder;
use zune_jpegxl::JxlEncodeErrors;

fn main()->Result<(),JxlEncodeErrors>{
    // set up options for encoder
    let options = EncoderOptions::default()
        .set_height(10)
        .set_width(10)
        .set_colorspace(ColorSpace::Luma);
    let image:[u8;100] = std::array::from_fn(|x| x as u8);

    let encoder = JxlSimpleEncoder::new(&image,options);
    // encode the image
    encoder.encode()?;
     
    Ok(())
}

Implementations§

source§

impl<'a> JxlSimpleEncoder<'a>

source

pub fn new(data: &'a [u8], options: EncoderOptions) -> JxlSimpleEncoder<'a>

Create a new jpeg xl encoder

Arguments
  • data: Raw pixel data
  • options: Encoder options for the raw pixels, this include the width, height colorspace, depth etc
source

pub fn encode_into(&mut self, buf: &mut [u8]) -> Result<usize, JxlEncodeErrors>

Encode a jxl image into a user provided buffer

Arguments
  • buf: The buffer to write bytes into It is difficult to estimate the size of output buffer as many factors influence the size of the final image. A good metric is that the buffer size can be equal to the raw pixel size.

    If the buffer is not big enough the library will bail and return an error

Returns
  • Ok(size) Number of bytes written into buf for the encoded image
  • Err() An error in case it occurred during encoding
source

pub fn encode(&self) -> Result<Vec<u8>, JxlEncodeErrors>

Encode a jxl image producing the raw encoded bytes or an error if there was any that occurred during encoding

Returns

Ok(vec): The result of encoding the image

Err(e): The error incase one is encountered

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for JxlSimpleEncoder<'a>

§

impl<'a> Send for JxlSimpleEncoder<'a>

§

impl<'a> Sync for JxlSimpleEncoder<'a>

§

impl<'a> Unpin for JxlSimpleEncoder<'a>

§

impl<'a> UnwindSafe for JxlSimpleEncoder<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.