jpegxl_sys\common/
types.rs

1/*
2This file is part of jpegxl-sys.
3
4jpegxl-sys is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 3 of the License, or
7(at your option) any later version.
8
9jpegxl-sys is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with jpegxl-sys.  If not, see <https://www.gnu.org/licenses/>.
16*/
17
18//! Data types for the JPEG XL API, for both encoding and decoding.
19
20use std::ffi::c_char;
21
22#[cfg(doc)]
23use crate::metadata::codestream_header::JxlBasicInfo;
24
25#[repr(i32)]
26#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
27pub enum JxlBool {
28    True = 1,
29    False = 0,
30}
31
32impl From<bool> for JxlBool {
33    fn from(b: bool) -> Self {
34        if b {
35            JxlBool::True
36        } else {
37            JxlBool::False
38        }
39    }
40}
41
42/// Data type for the sample values per channel per pixel.
43#[repr(C)]
44#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
45pub enum JxlDataType {
46    /// Use 32-bit single-precision floating point values, with range 0.0-1.0
47    /// (within gamut, may go outside this range for wide color gamut). Floating
48    /// point output, either [`JxlDataType::Float`] or [`JxlDataType::Float16`], is recommended
49    /// for HDR and wide gamut images when color profile conversion is required.
50    Float = 0,
51
52    /// Use type `u8`. May clip wide color gamut data.
53    Uint8 = 2,
54
55    /// Use type `u16`. May clip wide color gamut data.
56    Uint16 = 3,
57
58    /// Use 16-bit IEEE 754 half-precision floating point values.
59    Float16 = 5,
60}
61
62/// Ordering of multi-byte data.
63#[repr(C)]
64#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
65pub enum JxlEndianness {
66    /// Use the endianness of the system, either little endian or big endian,
67    /// without forcing either specific endianness. Do not use if pixel data
68    /// should be exported to a well defined format.
69    Native = 0,
70    /// Force little endian
71    Little = 1,
72    /// Force big endian
73    Big = 2,
74}
75
76/// Data type for the sample values per channel per pixel for the output buffer
77/// for pixels. This is not necessarily the same as the data type encoded in the
78/// codestream. The channels are interleaved per pixel. The pixels are
79/// organized row by row, left to right, top to bottom.
80/// TODO: support different channel orders if needed (RGB, BGR, ...)
81#[repr(C)]
82#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
83pub struct JxlPixelFormat {
84    /// Amount of channels available in a pixel buffer.
85    /// 1: single-channel data, e.g. grayscale or a single extra channel
86    /// 2: single-channel + alpha
87    /// 3: trichromatic, e.g. RGB
88    /// 4: trichromatic + alpha
89    /// TODO: this needs finetuning. It is not yet defined how the user
90    /// chooses output color space. CMYK+alpha needs 5 channels.
91    pub num_channels: u32,
92
93    /// Data type of each channel.
94    pub data_type: JxlDataType,
95
96    /// Whether multi-byte data types are represented in big endian or little
97    /// endian format. This applies to [`JxlDataType::Uint16`] and [`JxlDataType::Float`].
98    pub endianness: JxlEndianness,
99
100    /// Align scanlines to a multiple of align bytes, or 0 to require no
101    /// alignment at all (which has the same effect as value 1)
102    pub align: usize,
103}
104
105/// Settings for the interpretation of UINT input and output buffers.
106/// (buffers using a FLOAT data type are not affected by this)
107#[repr(i32)]
108#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
109pub enum JxlBitDepthType {
110    /// This is the default setting, where the encoder expects the input pixels
111    /// to use the full range of the pixel format data type (e.g. for UINT16, the
112    /// input range is 0 .. 65535 and the value 65535 is mapped to 1.0 when
113    /// converting to float), and the decoder uses the full range to output
114    /// pixels. If the bit depth in the basic info is different from this, the
115    /// encoder expects the values to be rescaled accordingly (e.g. multiplied by
116    /// 65535/4095 for a 12-bit image using UINT16 input data type).
117    FromPixelFormat = 0,
118
119    /// If this setting is selected, the encoder expects the input pixels to be
120    /// in the range defined by the [`JxlBasicInfo::bits_per_sample`] value (e.g.
121    /// for 12-bit images using UINT16 input data types, the allowed range is
122    /// 0 .. 4095 and the value 4095 is mapped to 1.0 when converting to float),
123    /// and the decoder outputs pixels in this range.
124    FromCodestream = 1,
125
126    /// This setting can only be used in the decoder to select a custom range for
127    /// pixel output.
128    Custom = 2,
129}
130
131/// Data type for describing the interpretation of the input and output buffers
132/// in terms of the range of allowed input and output pixel values.
133#[repr(C)]
134#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
135pub struct JxlBitDepth {
136    /// Bit depth setting, see comment on [`JxlBitDepthType`]
137    pub r#type: JxlBitDepthType,
138
139    /// Custom bits per sample
140    pub bits_per_sample: u32,
141
142    /// Custom exponent bits per sample
143    pub exponent_bits_per_sample: u32,
144}
145
146/// Data type holding the 4-character type name of an ISOBMFF box.
147#[repr(transparent)]
148pub struct JxlBoxType(pub [c_char; 4]);