Debian Codegraph @

debian/missing-sources/moxcms/src/conversions/katana/xyz_rgb.rs

raw
1 /*
2 * // Copyright (c) Radzivon Bartoshyk 6/2025. All rights reserved.
3 * //
4 * // Redistribution and use in source and binary forms, with or without modification,
5 * // are permitted provided that the following conditions are met:
6 * //
7 * // 1. Redistributions of source code must retain the above copyright notice, this
8 * // list of conditions and the following disclaimer.
9 * //
10 * // 2. Redistributions in binary form must reproduce the above copyright notice,
11 * // this list of conditions and the following disclaimer in the documentation
12 * // and/or other materials provided with the distribution.
13 * //
14 * // 3. Neither the name of the copyright holder nor the names of its
15 * // contributors may be used to endorse or promote products derived from
16 * // this software without specific prior written permission.
17 * //
18 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 use crate::conversions::katana::pcs_stages::KatanaMatrixStage;
30 use crate::conversions::katana::{
31 KatanaDefaultIntermediate, KatanaFinalStage, KatanaIntermediateStage,
32 };
33 use crate::mlaf::mlaf;
34 use crate::{
35 CmsError, ColorProfile, GammaLutInterpolate, Layout, Matrix3f, PointeeSizeExpressible, Rgb,
36 TransformOptions,
37 };
38 use num_traits::AsPrimitive;
39
40 pub(crate) struct KatanaXyzToRgbStage<T: Clone, const LAYOUT: u8> {
41 pub(crate) r_gamma: Box<[T; 65536]>,
42 pub(crate) g_gamma: Box<[T; 65536]>,
43 pub(crate) b_gamma: Box<[T; 65536]>,
44 pub(crate) bit_depth: usize,
45 pub(crate) gamma_lut: usize,
46 }
47
48 impl<T: Clone + AsPrimitive<f32> + PointeeSizeExpressible, const LAYOUT: u8>
49 KatanaFinalStage<f32, T> for KatanaXyzToRgbStage<T, LAYOUT>
50 where
51 u32: AsPrimitive<T>,
52 f32: AsPrimitive<T>,
53 {
54 fn to_output(&self, src: &mut [f32], dst: &mut [T]) -> Result<(), CmsError> {
55 let dst_cn = Layout::from(LAYOUT);
56 let dst_channels = dst_cn.channels();
57 if src.len() % 3 != 0 {
58 return Err(CmsError::LaneMultipleOfChannels);
59 }
60 if dst.len() % dst_channels != 0 {
61 return Err(CmsError::LaneMultipleOfChannels);
62 }
63 let src_chunks = src.len() / 3;
64 let dst_chunks = dst.len() / dst_channels;
65 if src_chunks != dst_chunks {
66 return Err(CmsError::LaneSizeMismatch);
67 }
68
69 let max_colors: T = (if T::FINITE {
70 ((1u32 << self.bit_depth) - 1) as f32
71 } else {
72 1.0
73 })
74 .as_();
75 let lut_cap = (self.gamma_lut - 1) as f32;
76
77 for (src, dst) in src.chunks_exact(3).zip(dst.chunks_exact_mut(dst_channels)) {
78 let rgb = Rgb::new(src[0], src[1], src[2]);
79 let r = mlaf(0.5, rgb.r, lut_cap).min(lut_cap).max(0.) as u16;
80 let g = mlaf(0.5, rgb.g, lut_cap).min(lut_cap).max(0.) as u16;
81 let b = mlaf(0.5, rgb.b, lut_cap).min(lut_cap).max(0.) as u16;
82
83 dst[0] = self.r_gamma[r as usize];
84 dst[1] = self.g_gamma[g as usize];
85 dst[2] = self.b_gamma[b as usize];
86 if dst_cn == Layout::Rgba {
87 dst[3] = max_colors;
88 }
89 }
90 Ok(())
91 }
92 }
93
94 pub(crate) struct KatanaXyzRgbState<T> {
95 pub(crate) stages: Vec<Box<dyn KatanaIntermediateStage<f32> + Send + Sync>>,
96 pub(crate) final_stage: Box<dyn KatanaFinalStage<f32, T> + Send + Sync>,
97 }
98
99 pub(crate) fn katana_prepare_inverse_lut_rgb_xyz<
100 T: Copy
101 + Default
102 + AsPrimitive<f32>
103 + Send
104 + Sync
105 + AsPrimitive<usize>
106 + PointeeSizeExpressible
107 + GammaLutInterpolate,
108 const BIT_DEPTH: usize,
109 const GAMMA_LUT: usize,
110 >(
111 dest: &ColorProfile,
112 dest_layout: Layout,
113 options: TransformOptions,
114 ) -> Result<KatanaXyzRgbState<T>, CmsError>
115 where
116 f32: AsPrimitive<T>,
117 u32: AsPrimitive<T>,
118 {
119 // if !T::FINITE {
120 // if let Some(extended_gamma) = dest.try_extended_gamma_evaluator() {
121 // let xyz_to_rgb = dest.rgb_to_xyz_matrix().inverse();
122 //
123 // let mut matrices = vec![Matrix3f {
124 // v: [
125 // [65535.0 / 32768.0, 0.0, 0.0],
126 // [0.0, 65535.0 / 32768.0, 0.0],
127 // [0.0, 0.0, 65535.0 / 32768.0],
128 // ],
129 // }];
130 //
131 // matrices.push(xyz_to_rgb.to_f32());
132 // let xyz_to_rgb_stage = XyzToRgbStageExtended::<T> {
133 // gamma_evaluator: extended_gamma,
134 // matrices,
135 // phantom_data: PhantomData,
136 // };
137 // xyz_to_rgb_stage.transform(lut)?;
138 // return Ok(());
139 // }
140 // }
141 let gamma_map_r = dest.build_gamma_table::<T, 65536, GAMMA_LUT, BIT_DEPTH>(
142 &dest.red_trc,
143 options.allow_use_cicp_transfer,
144 )?;
145 let gamma_map_g = dest.build_gamma_table::<T, 65536, GAMMA_LUT, BIT_DEPTH>(
146 &dest.green_trc,
147 options.allow_use_cicp_transfer,
148 )?;
149 let gamma_map_b = dest.build_gamma_table::<T, 65536, GAMMA_LUT, BIT_DEPTH>(
150 &dest.blue_trc,
151 options.allow_use_cicp_transfer,
152 )?;
153
154 let xyz_to_rgb = dest.rgb_to_xyz_matrix().inverse();
155
156 let mut matrices: Vec<Box<KatanaDefaultIntermediate>> =
157 vec![Box::new(KatanaMatrixStage::new(Matrix3f {
158 v: [
159 [65535.0 / 32768.0, 0.0, 0.0],
160 [0.0, 65535.0 / 32768.0, 0.0],
161 [0.0, 0.0, 65535.0 / 32768.0],
162 ],
163 }))];
164
165 matrices.push(Box::new(KatanaMatrixStage::new(xyz_to_rgb.to_f32())));
166 match dest_layout {
167 Layout::Rgb => {
168 let xyz_to_rgb_stage = KatanaXyzToRgbStage::<T, { Layout::Rgb as u8 }> {
169 r_gamma: gamma_map_r,
170 g_gamma: gamma_map_g,
171 b_gamma: gamma_map_b,
172 bit_depth: BIT_DEPTH,
173 gamma_lut: GAMMA_LUT,
174 };
175 Ok(KatanaXyzRgbState {
176 stages: matrices,
177 final_stage: Box::new(xyz_to_rgb_stage),
178 })
179 }
180 Layout::Rgba => {
181 let xyz_to_rgb_stage = KatanaXyzToRgbStage::<T, { Layout::Rgba as u8 }> {
182 r_gamma: gamma_map_r,
183 g_gamma: gamma_map_g,
184 b_gamma: gamma_map_b,
185 bit_depth: BIT_DEPTH,
186 gamma_lut: GAMMA_LUT,
187 };
188 Ok(KatanaXyzRgbState {
189 stages: matrices,
190 final_stage: Box::new(xyz_to_rgb_stage),
191 })
192 }
193 Layout::Gray => unreachable!("Gray layout must not be called on Rgb/Rgba path"),
194 Layout::GrayAlpha => unreachable!("Gray layout must not be called on Rgb/Rgba path"),
195 _ => unreachable!(
196 "layout {:?} should not be called on xyz->rgb path",
197 dest_layout
198 ),
199 }
200 }