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