Debian Codegraph @

debian/missing-sources/moxcms/src/conversions/rgb2gray_extended.rs

raw
1 /*
2 * // Copyright (c) Radzivon Bartoshyk 2/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 #![cfg(feature = "extended_range")]
30 use crate::mlaf::mlaf;
31 use crate::transform::PointeeSizeExpressible;
32 use crate::trc::ToneCurveEvaluator;
33 use crate::{CmsError, Layout, Rgb, TransformExecutor, Vector3f};
34 use num_traits::AsPrimitive;
35 use std::marker::PhantomData;
36 use std::sync::Arc;
37
38 struct TransformRgbToGrayExtendedExecutor<T, const SRC_LAYOUT: u8, const DST_LAYOUT: u8> {
39 linear_eval: Box<dyn ToneCurveEvaluator + Send + Sync>,
40 gamma_eval: Box<dyn ToneCurveEvaluator + Send + Sync>,
41 weights: Vector3f,
42 _phantom: PhantomData<T>,
43 bit_depth: usize,
44 }
45
46 pub(crate) fn make_rgb_to_gray_extended<
47 T: Copy + Default + PointeeSizeExpressible + Send + Sync + 'static + AsPrimitive<f32>,
48 >(
49 src_layout: Layout,
50 dst_layout: Layout,
51 linear_eval: Box<dyn ToneCurveEvaluator + Send + Sync>,
52 gamma_eval: Box<dyn ToneCurveEvaluator + Send + Sync>,
53 weights: Vector3f,
54 bit_depth: usize,
55 ) -> Result<Arc<dyn TransformExecutor<T> + Send + Sync>, CmsError>
56 where
57 u32: AsPrimitive<T>,
58 f32: AsPrimitive<T>,
59 {
60 match src_layout {
61 Layout::Rgb => match dst_layout {
62 Layout::Rgb => Err(CmsError::UnsupportedProfileConnection),
63 Layout::Rgba => Err(CmsError::UnsupportedProfileConnection),
64 Layout::Gray => Ok(Arc::new(TransformRgbToGrayExtendedExecutor::<
65 T,
66 { Layout::Rgb as u8 },
67 { Layout::Gray as u8 },
68 > {
69 linear_eval,
70 gamma_eval,
71 weights,
72 _phantom: PhantomData,
73 bit_depth,
74 })),
75 Layout::GrayAlpha => Ok(Arc::new(TransformRgbToGrayExtendedExecutor::<
76 T,
77 { Layout::Rgb as u8 },
78 { Layout::GrayAlpha as u8 },
79 > {
80 linear_eval,
81 gamma_eval,
82 weights,
83 _phantom: PhantomData,
84 bit_depth,
85 })),
86 _ => Err(CmsError::UnsupportedProfileConnection),
87 },
88 Layout::Rgba => match dst_layout {
89 Layout::Rgb => Err(CmsError::UnsupportedProfileConnection),
90 Layout::Rgba => Err(CmsError::UnsupportedProfileConnection),
91 Layout::Gray => Ok(Arc::new(TransformRgbToGrayExtendedExecutor::<
92 T,
93 { Layout::Rgba as u8 },
94 { Layout::Gray as u8 },
95 > {
96 linear_eval,
97 gamma_eval,
98 weights,
99 _phantom: PhantomData,
100 bit_depth,
101 })),
102 Layout::GrayAlpha => Ok(Arc::new(TransformRgbToGrayExtendedExecutor::<
103 T,
104 { Layout::Rgba as u8 },
105 { Layout::GrayAlpha as u8 },
106 > {
107 linear_eval,
108 gamma_eval,
109 weights,
110 _phantom: PhantomData,
111 bit_depth,
112 })),
113 _ => Err(CmsError::UnsupportedProfileConnection),
114 },
115 Layout::Gray => Err(CmsError::UnsupportedProfileConnection),
116 Layout::GrayAlpha => Err(CmsError::UnsupportedProfileConnection),
117 _ => Err(CmsError::UnsupportedProfileConnection),
118 }
119 }
120
121 impl<
122 T: Copy + Default + PointeeSizeExpressible + 'static + AsPrimitive<f32>,
123 const SRC_LAYOUT: u8,
124 const DST_LAYOUT: u8,
125 > TransformExecutor<T> for TransformRgbToGrayExtendedExecutor<T, SRC_LAYOUT, DST_LAYOUT>
126 where
127 u32: AsPrimitive<T>,
128 f32: AsPrimitive<T>,
129 {
130 fn transform(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
131 let src_cn = Layout::from(SRC_LAYOUT);
132 let dst_cn = Layout::from(DST_LAYOUT);
133 let src_channels = src_cn.channels();
134 let dst_channels = dst_cn.channels();
135
136 if src.len() / src_channels != dst.len() / dst_channels {
137 return Err(CmsError::LaneSizeMismatch);
138 }
139 if src.len() % src_channels != 0 {
140 return Err(CmsError::LaneMultipleOfChannels);
141 }
142 if dst.len() % dst_channels != 0 {
143 return Err(CmsError::LaneMultipleOfChannels);
144 }
145
146 let max_value = ((1u32 << self.bit_depth) - 1).as_();
147
148 for (src, dst) in src
149 .chunks_exact(src_channels)
150 .zip(dst.chunks_exact_mut(dst_channels))
151 {
152 let in_tristimulus = Rgb::<f32>::new(
153 src[src_cn.r_i()].as_(),
154 src[src_cn.g_i()].as_(),
155 src[src_cn.b_i()].as_(),
156 );
157 let lin_tristimulus = self.linear_eval.evaluate_tristimulus(in_tristimulus);
158 let a = if src_channels == 4 {
159 src[src_cn.a_i()]
160 } else {
161 max_value
162 };
163 let grey = mlaf(
164 mlaf(
165 self.weights.v[0] * lin_tristimulus.r,
166 self.weights.v[1],
167 lin_tristimulus.g,
168 ),
169 self.weights.v[2],
170 lin_tristimulus.b,
171 )
172 .min(1.)
173 .max(0.);
174 let gamma_value = self.gamma_eval.evaluate_value(grey);
175 dst[0] = gamma_value.as_();
176 if dst_channels == 2 {
177 dst[1] = a;
178 }
179 }
180
181 Ok(())
182 }
183 }