#jxl #jpeg-xl

jpegxl-rs

Safe Rust wrapper for JPEG XL reference implementation

19 releases

0.8.1+libjxl-0.8.1 Feb 4, 2023
0.7.0+libjxl-0.7.0 Sep 24, 2022
0.6.1 Nov 3, 2021
0.3.7 Apr 13, 2021
0.1.4 Aug 25, 2020

#51 in Images

Download history 9/week @ 2022-12-07 27/week @ 2022-12-14 6/week @ 2022-12-21 26/week @ 2022-12-28 4/week @ 2023-01-04 25/week @ 2023-01-11 104/week @ 2023-01-18 37/week @ 2023-01-25 62/week @ 2023-02-01 47/week @ 2023-02-08 86/week @ 2023-02-15 51/week @ 2023-02-22 17/week @ 2023-03-01 29/week @ 2023-03-08 42/week @ 2023-03-15 67/week @ 2023-03-22

155 downloads per month
Used in oculante

GPL-3.0-or-later

140KB
3K SLoC

jpegxl-rs

Documentation Crates.io CI codecov License: GPL-3.0-or-later

A safe JPEGXL wrapper over libjxl library. Check out the original library and the bindings.

Building

If you wish to specify a custom library path, set the DEP_JXL_LIB environment variable.

Building libjxl and statically linking can be enabled by using the vendored feature.

If you don't want to depend on C++ standard library, disable the feature threads.

Usage

Currently, u8, u16, f16 and f32 are supported as pixel types.

Decoding

let mut decoder = decoder_builder().build()?;
let (Metadata { width, height, ..}, pixels) = decoder.decode(&sample)?;
match pixels {
    Pixels::Float(data) => { /* do something with Vec<f32> data */ },
    Pixels::Uint8(data) => { /* do something with Vec<u8> data */ },
    Pixels::Uint16(data) => { /* do something with Vec<u16> data */ },
    Pixels::Float16(data) => { /* do something with Vec<f16> data */ },
}

// Multi-threading
use jpegxl_rs::ThreadsRunner;
let runner = ThreadsRunner::default();
let mut decoder = decoder_builder()
                      .parallel_runner(&runner)
                      .build()?;

// Customize pixel format
let mut decoder = decoder_builder()
                      .pixel_format(PixelFormat {
                          num_channels: 3,
                          endianness: Endianness::Big,
                          align: 8
                      })
                      .build()?;

decoder.decode_with::<u8>(&sample);

// You can change the settings after initialization
decoder.skip_reorientation = Some(true);

// Reconstruct JPEG, fallback to pixels if JPEG reconstruction is not possible
// This operation is finished in on pass
let (metadata, data) = decoder.reconstruct(&sample)?;
match data {
    Data::Jpeg(jpeg) => {/* do something with the JPEG data */}
    Data::Pixels(pixels) => {/* do something with the pixels data */}
}

Encoding

use image::io::Reader as ImageReader;
let sample = ImageReader::open("../samples/sample.png")?.decode()?.to_rgba16();
let mut encoder = encoder_builder().build()?;
let buffer: EncoderResult<f32> = encoder.encode(&sample, sample.width(), sample.height())?;

Set encoder options

let mut encoder = encoder_builder()
                    .lossless(true)
                    .speed(EncoderSpeed::Falcon)
                    .build()?;
// You can change the settings after initialization
encoder.lossless = false;
encoder.quality = 3.0;

image crate integration

The integration is enabled by default. If you don't need it, disable image feature.

use jpegxl_rs::image::ToDynamic;
use jpegxl_rs::decoder_builder;
use image::DynamicImage;

let sample = std::fs::read("../samples/sample.jxl")?;
let mut decoder = decoder_builder().build()?;
let img = decoder.decode_to_image(&sample)?;
let img = decoder.decode_to_image_with::<f32>(&sample)?;

License: GPL-3.0-or-later

Dependencies

~3.5MB
~78K SLoC