Skip to main content

jpegxl_rs/encode/
options.rs

1use std::mem::MaybeUninit;
2
3use jpegxl_sys::{color::color_encoding::JxlColorEncoding, encoder::encode as api};
4
5/// Encoding speed
6#[derive(Debug, Clone, Copy, Default)]
7pub enum EncoderSpeed {
8    /// Fastest, 1
9    Lightning = 1,
10    /// 2
11    Thunder = 2,
12    /// 3
13    Falcon = 3,
14    /// 4
15    Cheetah,
16    /// 5
17    Hare,
18    /// 6
19    Wombat,
20    /// 7, default
21    #[default]
22    Squirrel,
23    /// 8
24    Kitten,
25    /// 9
26    Tortoise,
27    /// Slowest, 10
28    Glacier,
29}
30
31/// Encoding color profile
32#[derive(Debug, Clone)]
33pub enum ColorEncoding {
34    /// sRGB, default for int pixel types
35    Srgb,
36    /// Linear sRGB, default for float pixel types
37    LinearSrgb,
38    /// sRGB, images with only luma channel
39    SrgbLuma,
40    /// Linear sRGB with only luma channel
41    LinearSrgbLuma,
42    /// Custom
43    Custom(JxlColorEncoding),
44}
45
46impl From<&ColorEncoding> for JxlColorEncoding {
47    fn from(val: &ColorEncoding) -> Self {
48        use ColorEncoding::{Custom, LinearSrgb, LinearSrgbLuma, Srgb, SrgbLuma};
49
50        let mut color_encoding = MaybeUninit::uninit();
51
52        unsafe {
53            match val {
54                Srgb => api::JxlColorEncodingSetToSRGB(color_encoding.as_mut_ptr(), false.into()),
55                LinearSrgb => {
56                    api::JxlColorEncodingSetToLinearSRGB(color_encoding.as_mut_ptr(), false.into());
57                }
58                SrgbLuma => {
59                    api::JxlColorEncodingSetToSRGB(color_encoding.as_mut_ptr(), true.into());
60                }
61                LinearSrgbLuma => {
62                    api::JxlColorEncodingSetToLinearSRGB(color_encoding.as_mut_ptr(), true.into());
63                }
64                Custom(e) => {
65                    return e.clone();
66                }
67            }
68            color_encoding.assume_init()
69        }
70    }
71}