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
125pub struct ColorProfileNCLX {
126    pub(crate) inner: *mut lh::heif_color_profile_nclx,
127}
128
129impl Drop for ColorProfileNCLX {
130    fn drop(&mut self) {
131        unsafe {
132            lh::heif_nclx_color_profile_free(self.inner);
133        }
134    }
135}
136
137impl ColorProfile for ColorProfileNCLX {
138    fn profile_type(&self) -> ColorProfileType {
139        color_profile_types::NCLX
140    }
141}
142
143impl ColorProfileNCLX {
144    pub fn new() -> Option<Self> {
145        let inner = unsafe { lh::heif_nclx_color_profile_alloc() };
146        (!inner.is_null()).then_some(Self { inner })
147    }
148
149    #[inline(always)]
150    fn inner_ref(&self) -> &lh::heif_color_profile_nclx {
151        unsafe { &(*self.inner) }
152    }
153
154    #[inline(always)]
155    fn inner_mut(&mut self) -> &mut lh::heif_color_profile_nclx {
156        unsafe { &mut (*self.inner) }
157    }
158
159    pub fn version(&self) -> u8 {
160        self.inner_ref().version
161    }
162
163    pub fn color_primaries(&self) -> ColorPrimaries {
164        ColorPrimaries::n(self.inner_ref().color_primaries).unwrap_or(ColorPrimaries::Unknown)
165    }
166
167    pub fn set_color_primaries(&mut self, v: ColorPrimaries) {
168        if v != ColorPrimaries::Unknown {
169            self.inner_mut().color_primaries = v as _;
170        }
171    }
172
173    pub fn transfer_characteristics(&self) -> TransferCharacteristics {
174        TransferCharacteristics::n(self.inner_ref().transfer_characteristics)
175            .unwrap_or(TransferCharacteristics::Unknown)
176    }
177
178    pub fn matrix_coefficients(&self) -> MatrixCoefficients {
179        MatrixCoefficients::n(self.inner_ref().matrix_coefficients)
180            .unwrap_or(MatrixCoefficients::Unknown)
181    }
182
183    pub fn full_range_flag(&self) -> u8 {
184        self.inner_ref().full_range_flag
185    }
186
187    pub fn color_primary_red_x(&self) -> f32 {
188        self.inner_ref().color_primary_red_x
189    }
190
191    pub fn color_primary_red_y(&self) -> f32 {
192        self.inner_ref().color_primary_red_y
193    }
194
195    pub fn color_primary_green_x(&self) -> f32 {
196        self.inner_ref().color_primary_green_x
197    }
198
199    pub fn color_primary_green_y(&self) -> f32 {
200        self.inner_ref().color_primary_green_y
201    }
202
203    pub fn color_primary_blue_x(&self) -> f32 {
204        self.inner_ref().color_primary_blue_x
205    }
206
207    pub fn color_primary_blue_y(&self) -> f32 {
208        self.inner_ref().color_primary_blue_y
209    }
210
211    pub fn color_primary_white_x(&self) -> f32 {
212        self.inner_ref().color_primary_white_x
213    }
214
215    pub fn color_primary_white_y(&self) -> f32 {
216        self.inner_ref().color_primary_white_y
217    }
218}