Skip to main content

xberg_libheif/
color_profile.rs

1use four_cc::FourCC;
2use libheif_sys as lh;
3
4pub type ColorProfileType = FourCC;
5
6pub mod color_profile_types {
7    use super::{ColorProfileType, FourCC};
8
9    /// Color profile is not present inside image
10    pub const NOT_PRESENT: ColorProfileType = FourCC(0u32.to_le_bytes());
11    /// NCLX color profile
12    pub const NCLX: ColorProfileType = FourCC(*b"nclx");
13    /// Restricted ICC color profile
14    pub const R_ICC: ColorProfileType = FourCC(*b"rICC");
15    /// Prof color profile
16    pub const PROF: ColorProfileType = FourCC(*b"prof");
17}
18
19#[allow(non_camel_case_types)]
20#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, enumn::N)]
21#[repr(C)]
22pub enum ColorPrimaries {
23    Unspecified = lh::heif_color_primaries_heif_color_primaries_unspecified as _,
24    ITU_R_BT_470_6_System_M = lh::heif_color_primaries_heif_color_primaries_ITU_R_BT_470_6_System_M as _,
25    ITU_R_BT_470_6_System_B_G = lh::heif_color_primaries_heif_color_primaries_ITU_R_BT_470_6_System_B_G as _,
26    ITU_R_BT_601_6 = lh::heif_color_primaries_heif_color_primaries_ITU_R_BT_601_6 as _,
27    ITU_R_BT_709_5 = lh::heif_color_primaries_heif_color_primaries_ITU_R_BT_709_5 as _,
28    ITU_R_BT_2020_2_and_2100_0 = lh::heif_color_primaries_heif_color_primaries_ITU_R_BT_2020_2_and_2100_0 as _,
29    GenericFilm = lh::heif_color_primaries_heif_color_primaries_generic_film as _,
30    SMPTE_240M = lh::heif_color_primaries_heif_color_primaries_SMPTE_240M as _,
31    SMPTE_ST_428_1 = lh::heif_color_primaries_heif_color_primaries_SMPTE_ST_428_1 as _,
32    SMPTE_RP_431_2 = lh::heif_color_primaries_heif_color_primaries_SMPTE_RP_431_2 as _,
33    SMPTE_EG_432_1 = lh::heif_color_primaries_heif_color_primaries_SMPTE_EG_432_1 as _,
34    EBU_Tech_3213_E = lh::heif_color_primaries_heif_color_primaries_EBU_Tech_3213_E as _,
35    /// This value is used when library `libheif` returns unknown value of color primaries.
36    Unknown,
37}
38
39#[allow(non_camel_case_types)]
40#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, enumn::N)]
41#[repr(C)]
42pub enum TransferCharacteristics {
43    ITU_R_BT_709_5 = lh::heif_transfer_characteristics_heif_transfer_characteristic_ITU_R_BT_709_5 as _,
44    Unspecified = lh::heif_transfer_characteristics_heif_transfer_characteristic_unspecified as _,
45    ITU_R_BT_470_6_System_M =
46        lh::heif_transfer_characteristics_heif_transfer_characteristic_ITU_R_BT_470_6_System_M as _,
47    ITU_R_BT_470_6_System_B_G =
48        lh::heif_transfer_characteristics_heif_transfer_characteristic_ITU_R_BT_470_6_System_B_G as _,
49    ITU_R_BT_601_6 = lh::heif_transfer_characteristics_heif_transfer_characteristic_ITU_R_BT_601_6 as _,
50    SMPTE_240M = lh::heif_transfer_characteristics_heif_transfer_characteristic_SMPTE_240M as _,
51    Linear = lh::heif_transfer_characteristics_heif_transfer_characteristic_linear as _,
52    Logarithmic100 = lh::heif_transfer_characteristics_heif_transfer_characteristic_logarithmic_100 as _,
53    Logarithmic100Sqrt10 = lh::heif_transfer_characteristics_heif_transfer_characteristic_logarithmic_100_sqrt10 as _,
54    IEC_61966_2_4 = lh::heif_transfer_characteristics_heif_transfer_characteristic_IEC_61966_2_4 as _,
55    ITU_R_BT_1361 = lh::heif_transfer_characteristics_heif_transfer_characteristic_ITU_R_BT_1361 as _,
56    IEC_61966_2_1 = lh::heif_transfer_characteristics_heif_transfer_characteristic_IEC_61966_2_1 as _,
57    ITU_R_BT_2020_2_10bit = lh::heif_transfer_characteristics_heif_transfer_characteristic_ITU_R_BT_2020_2_10bit as _,
58    ITU_R_BT_2020_2_12bit = lh::heif_transfer_characteristics_heif_transfer_characteristic_ITU_R_BT_2020_2_12bit as _,
59    ITU_R_BT_2100_0_PQ = lh::heif_transfer_characteristics_heif_transfer_characteristic_ITU_R_BT_2100_0_PQ as _,
60    SMPTE_ST_428_1 = lh::heif_transfer_characteristics_heif_transfer_characteristic_SMPTE_ST_428_1 as _,
61    ITU_R_BT_2100_0_HLG = lh::heif_transfer_characteristics_heif_transfer_characteristic_ITU_R_BT_2100_0_HLG as _,
62    /// This value is used when library `libheif` returns unknown value of transfer characteristics.
63    Unknown,
64}
65
66#[allow(non_camel_case_types)]
67#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, enumn::N)]
68#[repr(C)]
69pub enum MatrixCoefficients {
70    RGB_GBR = lh::heif_matrix_coefficients_heif_matrix_coefficients_RGB_GBR as _,
71    ITU_R_BT_709_5 = lh::heif_matrix_coefficients_heif_matrix_coefficients_ITU_R_BT_709_5 as _,
72    Unspecified = lh::heif_matrix_coefficients_heif_matrix_coefficients_unspecified as _,
73    US_FCC_T47 = lh::heif_matrix_coefficients_heif_matrix_coefficients_US_FCC_T47 as _,
74    ITU_R_BT_470_6_System_B_G = lh::heif_matrix_coefficients_heif_matrix_coefficients_ITU_R_BT_470_6_System_B_G as _,
75    ITU_R_BT_601_6 = lh::heif_matrix_coefficients_heif_matrix_coefficients_ITU_R_BT_601_6 as _,
76    SMPTE_240M = lh::heif_matrix_coefficients_heif_matrix_coefficients_SMPTE_240M as _,
77    YCgCo = lh::heif_matrix_coefficients_heif_matrix_coefficients_YCgCo as _,
78    ITU_R_BT_2020_2_NonConstantLuminance =
79        lh::heif_matrix_coefficients_heif_matrix_coefficients_ITU_R_BT_2020_2_non_constant_luminance as _,
80    ITU_R_BT_2020_2_ConstantLuminance =
81        lh::heif_matrix_coefficients_heif_matrix_coefficients_ITU_R_BT_2020_2_constant_luminance as _,
82    SMPTE_ST_2085 = lh::heif_matrix_coefficients_heif_matrix_coefficients_SMPTE_ST_2085 as _,
83    ChromaticityDerivedNonConstantLuminance =
84        lh::heif_matrix_coefficients_heif_matrix_coefficients_chromaticity_derived_non_constant_luminance as _,
85    ChromaticityDerivedConstantLuminance =
86        lh::heif_matrix_coefficients_heif_matrix_coefficients_chromaticity_derived_constant_luminance as _,
87    ICtCp = lh::heif_matrix_coefficients_heif_matrix_coefficients_ICtCp as _,
88    /// This value is used when library `libheif` returns unknown value of matrix coefficients.
89    Unknown,
90}
91
92pub trait ColorProfile {
93    fn profile_type(&self) -> ColorProfileType;
94}
95
96pub struct ColorProfileRaw {
97    pub(crate) typ: ColorProfileType,
98    pub data: Vec<u8>,
99}
100
101impl ColorProfileRaw {
102    pub fn new(typ: ColorProfileType, data: Vec<u8>) -> Self {
103        Self { typ, data }
104    }
105}
106
107impl ColorProfile for ColorProfileRaw {
108    fn profile_type(&self) -> ColorProfileType {
109        self.typ
110    }
111}
112
113#[derive(Debug)]
114pub struct ColorProfileNCLX {
115    pub(crate) inner: *mut lh::heif_color_profile_nclx,
116}
117
118#[allow(unsafe_code)]
119impl Drop for ColorProfileNCLX {
120    fn drop(&mut self) {
121        unsafe {
122            lh::heif_nclx_color_profile_free(self.inner);
123        }
124    }
125}
126
127impl ColorProfile for ColorProfileNCLX {
128    fn profile_type(&self) -> ColorProfileType {
129        color_profile_types::NCLX
130    }
131}
132
133#[allow(unsafe_code)]
134impl ColorProfileNCLX {
135    pub fn new() -> Option<Self> {
136        let inner = unsafe { lh::heif_nclx_color_profile_alloc() };
137        (!inner.is_null()).then_some(Self { inner })
138    }
139
140    #[inline(always)]
141    #[allow(unsafe_code)]
142    fn inner_ref(&self) -> &lh::heif_color_profile_nclx {
143        unsafe { &(*self.inner) }
144    }
145
146    #[inline(always)]
147    #[allow(unsafe_code)]
148    fn inner_mut(&mut self) -> &mut lh::heif_color_profile_nclx {
149        unsafe { &mut (*self.inner) }
150    }
151
152    pub fn version(&self) -> u8 {
153        self.inner_ref().version
154    }
155
156    pub fn color_primaries(&self) -> ColorPrimaries {
157        ColorPrimaries::n(self.inner_ref().color_primaries).unwrap_or(ColorPrimaries::Unknown)
158    }
159
160    pub fn set_color_primaries(&mut self, v: ColorPrimaries) {
161        if v != ColorPrimaries::Unknown {
162            self.inner_mut().color_primaries = v as _;
163        }
164    }
165
166    pub fn transfer_characteristics(&self) -> TransferCharacteristics {
167        TransferCharacteristics::n(self.inner_ref().transfer_characteristics)
168            .unwrap_or(TransferCharacteristics::Unknown)
169    }
170
171    pub fn matrix_coefficients(&self) -> MatrixCoefficients {
172        MatrixCoefficients::n(self.inner_ref().matrix_coefficients).unwrap_or(MatrixCoefficients::Unknown)
173    }
174
175    pub fn full_range_flag(&self) -> u8 {
176        self.inner_ref().full_range_flag
177    }
178
179    pub fn color_primary_red_x(&self) -> f32 {
180        self.inner_ref().color_primary_red_x
181    }
182
183    pub fn color_primary_red_y(&self) -> f32 {
184        self.inner_ref().color_primary_red_y
185    }
186
187    pub fn color_primary_green_x(&self) -> f32 {
188        self.inner_ref().color_primary_green_x
189    }
190
191    pub fn color_primary_green_y(&self) -> f32 {
192        self.inner_ref().color_primary_green_y
193    }
194
195    pub fn color_primary_blue_x(&self) -> f32 {
196        self.inner_ref().color_primary_blue_x
197    }
198
199    pub fn color_primary_blue_y(&self) -> f32 {
200        self.inner_ref().color_primary_blue_y
201    }
202
203    pub fn color_primary_white_x(&self) -> f32 {
204        self.inner_ref().color_primary_white_x
205    }
206
207    pub fn color_primary_white_y(&self) -> f32 {
208        self.inner_ref().color_primary_white_y
209    }
210}