#jpegxl #decoder #image-format #jpeg-xl-decoder

no-std zune-jpegxl

A simple, fast and fully safe modular jxl encoder

2 unstable releases

0.5.0-rc0 Apr 7, 2024
0.4.0 Nov 16, 2023

#753 in Images

Download history 18/week @ 2024-01-06 59/week @ 2024-01-13 35/week @ 2024-01-20 1/week @ 2024-01-27 19/week @ 2024-02-10 84/week @ 2024-02-17 35/week @ 2024-02-24 13/week @ 2024-03-02 60/week @ 2024-03-09 185/week @ 2024-03-16 45/week @ 2024-03-23 55/week @ 2024-03-30 109/week @ 2024-04-06 32/week @ 2024-04-13 60/week @ 2024-04-20

257 downloads per month
Used in 2 crates

MIT OR Apache-2.0 OR Zlib

170KB
3K SLoC

zune-jpegxl

A simple jpeg-xl encoder

This features a simple jpeg-xl lossless encoder with the following features

  • Lossless encoding
  • 8 bit and 16 bit support
  • Grayscale and RGB{A} encoding
  • Threading capabilities

Usage

First add the latest into your cargo toml

By cargo add

cargo add zune-jpegxl

Or adding directly to your Cargo.toml

zune-jpegxl = "0.4"

Then use the JxlSimpleEncoder struct to encode an image

use zune_core::bit_depth::BitDepth;
use zune_core::options::EncoderOptions;
use zune_jpegxl::JxlSimpleEncoder;
// this example won't work
fn main()->Result<(),JxlEncodeErrors> {
    let mut encoder = JxlSimpleEncoder::new(&[255,0,255,0], EncoderOptions::new(2,2,co));
    encoder.encode().unwrap();
}

lib.rs:

Zune-jpegxl

Support for encoding jpeg xl images in pure rust

This currently features a small POC encoder for JPEG-XL format based on the POC over at the libjxl crate

It supports the following features

  • lossless compression

  • up to 16 bits of depth

  • Up to 4 channels for images

  • Non supported features -> Palette support

Currently, it's fast with slightly worse compression when compared png for non-photo content and much better for other situations.

The library is also fully safe

Features

  • std: Enables linking against the standard library
  • threads: Enables using the standard library threading capabilities, this feature requires the std feature to work (threading doesn't exist in no-std), if the above feature isn't enabled this is a no-op
  • log: Enables use of log to report on encoding configs and status

Both features are enabled by default.

16 bit data

  • 16 bit data should be reinterpreted as 2 u8's in native endian,

Example

  • Encode a 2x2 8 bit image
use zune_core::bit_depth::BitDepth;
use zune_core::colorspace::ColorSpace;
use zune_core::options::EncoderOptions;
use zune_jpegxl::JxlSimpleEncoder;
use zune_jpegxl::JxlEncodeErrors;
// encode a 2x2 image

fn main()->Result<(),JxlEncodeErrors>{
    let mut encoder = JxlSimpleEncoder::new(&[0,0,0,0],EncoderOptions::new(2,2,ColorSpace::Luma,BitDepth::Eight));
let mut write_to = vec![];
    encoder.encode(&mut write_to)?;
    Ok(())
}
  • Encode a 2x2 16 bit image
use zune_core::bit_depth::BitDepth;
use zune_core::colorspace::ColorSpace;
use zune_core::options::EncoderOptions;
use zune_jpegxl::JxlSimpleEncoder;
use zune_jpegxl::JxlEncodeErrors;
// encode a 2x2 image

fn main()->Result<(),JxlEncodeErrors>{
    // convert a 16 bit input to 8 bit native endian output, each two bytes represent one sample
    let sixteen_bit = [0,u16::MAX,0,u16::MAX].iter().flat_map(|x| x.to_ne_bytes()).collect::<Vec<u8>>();
    let mut encoder = JxlSimpleEncoder::new(&sixteen_bit,EncoderOptions::new(2,2,ColorSpace::Luma,BitDepth::Sixteen));
    let mut write_to = vec![];
    encoder.encode(&mut write_to)?;
    Ok(())
}

Dependencies