jpegxl_rs/decode/
result.rs

1/*
2This file is part of jpegxl-rs.
3
4jpegxl-rs 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-rs 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-rs.  If not, see <https://www.gnu.org/licenses/>.
16*/
17
18use half::f16;
19use jpegxl_sys::common::types::{JxlDataType, JxlPixelFormat};
20
21use super::Orientation;
22use crate::common::PixelType;
23
24/// Result of decoding
25#[derive(Debug)]
26pub struct Metadata {
27    /// Width of the image
28    pub width: u32,
29    /// Height of the image
30    pub height: u32,
31    /// Upper bound on the intensity level present in the image in nits
32    pub intensity_target: f32,
33    /// Lower bound on the intensity level present in the image
34    pub min_nits: f32,
35    /// Orientation
36    pub orientation: Orientation,
37    /// Number of color channels per pixel _without_ alpha channel, from metadata
38    pub num_color_channels: u32,
39    /// Whether the image has an alpha channel, from metadata
40    pub has_alpha_channel: bool,
41    /// Intrinsic width of the image.
42    /// Applications are advised to resample the decoded image to the intrinsic dimensions
43    pub intrinsic_width: u32,
44    /// Intrinsic height of the image.
45    /// Applications are advised to resample the decoded image to the intrinsic dimensions
46    pub intrinsic_height: u32,
47    /// ICC profile
48    pub icc_profile: Option<Vec<u8>>,
49}
50
51/// Pixels returned from the decoder
52#[derive(Debug)]
53pub enum Pixels {
54    /// `f32` pixels
55    Float(Vec<f32>),
56    /// `u8` pixels
57    Uint8(Vec<u8>),
58    /// `u16` pixels
59    Uint16(Vec<u16>),
60    /// `f16` pixels
61    Float16(Vec<f16>),
62}
63
64impl Pixels {
65    pub(crate) fn new(data: Vec<u8>, pixel_format: &JxlPixelFormat) -> Self {
66        match pixel_format.data_type {
67            JxlDataType::Float => Self::Float(f32::convert(&data, pixel_format)),
68            JxlDataType::Uint8 => Self::Uint8(data),
69            JxlDataType::Uint16 => Self::Uint16(u16::convert(&data, pixel_format)),
70            JxlDataType::Float16 => Self::Float16(f16::convert(&data, pixel_format)),
71        }
72    }
73}
74
75/// Reconstruction result
76pub enum Data {
77    /// JPEG  
78    Jpeg(Vec<u8>),
79    /// Pixels
80    Pixels(Pixels),
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    #[cfg_attr(coverage_nightly, coverage(off))]
89    fn test_derive() {
90        println!(
91            "{:?}",
92            Metadata {
93                width: 0,
94                height: 0,
95                intensity_target: 0.0,
96                min_nits: 0.0,
97                orientation: Orientation::Identity,
98                num_color_channels: 0,
99                has_alpha_channel: false,
100                intrinsic_width: 0,
101                intrinsic_height: 0,
102                icc_profile: None,
103            }
104        );
105
106        println!("{:?}", Pixels::Float(vec![]));
107    }
108}