1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
This file is part of jpegxl-rs.

jpegxl-rs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

jpegxl-rs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with jpegxl-rs.  If not, see <https://www.gnu.org/licenses/>.
*/

//! [![Documentation](https://docs.rs/jpegxl-rs/badge.svg)](https://docs.rs/jpegxl-rs/)
//! [![Crates.io](https://img.shields.io/crates/v/jpegxl-rs.svg)](https://crates.io/crates/jpegxl-rs)
//! [![dependency status](https://deps.rs/repo/github/inflation/jpegxl-rs/status.svg)](https://deps.rs/repo/github/inflation/jpegxl-rs)
//! [![CI](https://github.com/inflation/jpegxl-rs/workflows/CI/badge.svg)](https://github.com/inflation/jpegxl-rs/actions?query=workflow%3ACI)
//! [![codecov](https://codecov.io/gh/inflation/jpegxl-rs/branch/master/graph/badge.svg?token=3WMRUQ816H)](https://codecov.io/gh/inflation/jpegxl-rs)
//! [![License: GPL-3.0-or-later](https://img.shields.io/crates/l/jpegxl-rs)](https://github.com/inflation/jpegxl-rs/blob/master/LICENSE)
//!
//! A safe JPEGXL wrapper over `libjxl` library. Check out the original [library](https://github.com/libjxl/libjxl)
//! and the [bindings](https://github.com/inflation/jpegxl-rs/tree/master/jpegxl-sys).
//!
//! # 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
//!
//! ```
//! # use jpegxl_rs::*;
//! # use jpegxl_rs::decode::{PixelFormat, Metadata, Pixels, Data};
//! # || -> Result<(), Box<dyn std::error::Error>> {
//! # let sample = [];
//! 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 */}
//! }
//!
//! # Ok(()) };
//! ```
//!
//! ## Encoding
//!
//! ```
//! # use jpegxl_rs::{encoder_builder, encode::EncoderResult};
//! # || -> Result<(), Box<dyn std::error::Error>> {
//! 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())?;
//! # Ok(()) };
//! ```
//!
//! Set encoder options
//!
//! ```
//! # || -> Result<(), Box<dyn std::error::Error>> {
//! # use jpegxl_rs::{*, encode::EncoderSpeed};
//! 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;
//! # Ok(()) };
//! ```
//!
//! ## [`image`](https://crates.io/crates/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)?;       
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

#[macro_use]
extern crate derive_builder;

mod common;
pub mod decode;
pub mod encode;
mod errors;
pub mod memory;
pub mod parallel;
pub mod utils;

#[cfg(feature = "image")]
pub mod image;

#[cfg(test)]
mod tests;

pub use common::Endianness;
pub use decode::decoder_builder;
pub use encode::encoder_builder;
pub use errors::{DecodeError, EncodeError};

#[cfg(feature = "threads")]
pub use parallel::resizable_runner::ResizableRunner;
#[cfg(feature = "threads")]
pub use parallel::threads_runner::ThreadsRunner;