moxcms/conversions/katana/
finalizers.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 8/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 */
29use crate::conversions::katana::KatanaPostFinalizationStage;
30use crate::{CmsError, DataColorSpace, Layout, PointeeSizeExpressible};
31use num_traits::AsPrimitive;
32use std::marker::PhantomData;
33
34pub(crate) struct InjectAlphaStage<I> {
35    pub(crate) dst_layout: Layout,
36    pub(crate) target_color_space: DataColorSpace,
37    pub(crate) _phantom: PhantomData<I>,
38    pub(crate) bit_depth: usize,
39}
40
41pub(crate) struct CopyAlphaStage<I> {
42    pub(crate) src_layout: Layout,
43    pub(crate) dst_layout: Layout,
44    pub(crate) target_color_space: DataColorSpace,
45    pub(crate) _phantom: PhantomData<I>,
46}
47
48impl<T: Copy + Default + AsPrimitive<f32> + PointeeSizeExpressible + Send + Sync>
49    KatanaPostFinalizationStage<T> for InjectAlphaStage<T>
50where
51    f32: AsPrimitive<T>,
52{
53    fn finalize(&self, _: &[T], dst: &mut [T]) -> Result<(), CmsError> {
54        let norm_value: T = (if T::FINITE {
55            ((1u32 << self.bit_depth) - 1) as f32
56        } else {
57            1.0
58        })
59        .as_();
60        if self.dst_layout == Layout::Rgba && self.target_color_space == DataColorSpace::Rgb {
61            for dst in dst.chunks_exact_mut(self.dst_layout.channels()) {
62                dst[3] = norm_value;
63            }
64        } else if self.dst_layout == Layout::GrayAlpha
65            && self.target_color_space == DataColorSpace::Gray
66        {
67            for dst in dst.chunks_exact_mut(self.dst_layout.channels()) {
68                dst[1] = norm_value;
69            }
70        }
71        Ok(())
72    }
73}
74
75impl<T: Copy + Default + AsPrimitive<f32> + PointeeSizeExpressible + Send + Sync>
76    KatanaPostFinalizationStage<T> for CopyAlphaStage<T>
77where
78    f32: AsPrimitive<T>,
79{
80    fn finalize(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
81        if self.dst_layout == Layout::Rgba && self.target_color_space == DataColorSpace::Rgb {
82            if self.src_layout == Layout::Rgba {
83                for (src, dst) in src
84                    .chunks_exact(self.src_layout.channels())
85                    .zip(dst.chunks_exact_mut(self.dst_layout.channels()))
86                {
87                    dst[3] = src[3];
88                }
89            } else if self.src_layout == Layout::GrayAlpha {
90                for (src, dst) in src
91                    .chunks_exact(self.src_layout.channels())
92                    .zip(dst.chunks_exact_mut(self.dst_layout.channels()))
93                {
94                    dst[3] = src[1];
95                }
96            }
97        } else if self.dst_layout == Layout::GrayAlpha
98            && self.target_color_space == DataColorSpace::Gray
99        {
100            if self.src_layout == Layout::Rgba {
101                for (src, dst) in src
102                    .chunks_exact(self.src_layout.channels())
103                    .zip(dst.chunks_exact_mut(self.dst_layout.channels()))
104                {
105                    dst[1] = src[3];
106                }
107            } else if self.src_layout == Layout::GrayAlpha {
108                for (src, dst) in src
109                    .chunks_exact(self.src_layout.channels())
110                    .zip(dst.chunks_exact_mut(self.dst_layout.channels()))
111                {
112                    dst[1] = src[1];
113                }
114            }
115        }
116        Ok(())
117    }
118}