moxcms/conversions/katana/
finalizers.rs1use 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}