Skip to main content

singe_npp/image/
geometry_transform.rs

1use super::*;
2
3impl_rotate!(rotate_u8_c1, u8, C1, nppiRotate_8u_C1R_Ctx);
4impl_rotate!(rotate_u8_c3, u8, C3, nppiRotate_8u_C3R_Ctx);
5impl_rotate!(rotate_u8_c4, u8, C4, nppiRotate_8u_C4R_Ctx);
6impl_rotate!(rotate_u8_ac4, u8, AC4, nppiRotate_8u_AC4R_Ctx);
7impl_rotate!(rotate_u16_c1, u16, C1, nppiRotate_16u_C1R_Ctx);
8impl_rotate!(rotate_u16_c3, u16, C3, nppiRotate_16u_C3R_Ctx);
9impl_rotate!(rotate_u16_c4, u16, C4, nppiRotate_16u_C4R_Ctx);
10impl_rotate!(rotate_u16_ac4, u16, AC4, nppiRotate_16u_AC4R_Ctx);
11impl_rotate!(rotate_f32_c1, f32, C1, nppiRotate_32f_C1R_Ctx);
12impl_rotate!(rotate_f32_c3, f32, C3, nppiRotate_32f_C3R_Ctx);
13impl_rotate!(rotate_f32_c4, f32, C4, nppiRotate_32f_C4R_Ctx);
14impl_rotate!(rotate_f32_ac4, f32, AC4, nppiRotate_32f_AC4R_Ctx);
15
16pub trait RotateOperation<L: ChannelLayout>: DataTypeLike {
17    fn rotate(
18        stream_context: &StreamContext,
19        rotate: &Rotate,
20        source: &ImageView<'_, Self, L>,
21        destination: &mut ImageViewMut<'_, Self, L>,
22    ) -> Result<()>;
23}
24
25macro_rules! impl_rotate_operation {
26    ($ty:ty, $layout:ty, $function:ident) => {
27        impl RotateOperation<$layout> for $ty {
28            fn rotate(
29                stream_context: &StreamContext,
30                rotate: &Rotate,
31                source: &ImageView<'_, Self, $layout>,
32                destination: &mut ImageViewMut<'_, Self, $layout>,
33            ) -> Result<()> {
34                $function(stream_context, rotate, source, destination)
35            }
36        }
37    };
38}
39
40impl_rotate_operation!(u8, C1, rotate_u8_c1);
41impl_rotate_operation!(u8, C3, rotate_u8_c3);
42impl_rotate_operation!(u8, C4, rotate_u8_c4);
43impl_rotate_operation!(u8, AC4, rotate_u8_ac4);
44impl_rotate_operation!(u16, C1, rotate_u16_c1);
45impl_rotate_operation!(u16, C3, rotate_u16_c3);
46impl_rotate_operation!(u16, C4, rotate_u16_c4);
47impl_rotate_operation!(u16, AC4, rotate_u16_ac4);
48impl_rotate_operation!(f32, C1, rotate_f32_c1);
49impl_rotate_operation!(f32, C3, rotate_f32_c3);
50impl_rotate_operation!(f32, C4, rotate_f32_c4);
51impl_rotate_operation!(f32, AC4, rotate_f32_ac4);
52
53pub fn rotate<T, L>(
54    stream_context: &StreamContext,
55    rotate: &Rotate,
56    source: &ImageView<'_, T, L>,
57    destination: &mut ImageViewMut<'_, T, L>,
58) -> Result<()>
59where
60    T: RotateOperation<L>,
61    L: ChannelLayout,
62{
63    T::rotate(stream_context, rotate, source, destination)
64}
65
66impl_remap!(remap_u8_c1, u8, f32, C1, nppiRemap_8u_C1R_Ctx);
67impl_remap!(remap_u8_c3, u8, f32, C3, nppiRemap_8u_C3R_Ctx);
68impl_remap!(remap_u8_c4, u8, f32, C4, nppiRemap_8u_C4R_Ctx);
69impl_remap!(remap_u8_ac4, u8, f32, AC4, nppiRemap_8u_AC4R_Ctx);
70impl_remap!(remap_u16_c1, u16, f32, C1, nppiRemap_16u_C1R_Ctx);
71impl_remap!(remap_u16_c3, u16, f32, C3, nppiRemap_16u_C3R_Ctx);
72impl_remap!(remap_u16_c4, u16, f32, C4, nppiRemap_16u_C4R_Ctx);
73impl_remap!(remap_u16_ac4, u16, f32, AC4, nppiRemap_16u_AC4R_Ctx);
74impl_remap!(remap_i16_c1, i16, f32, C1, nppiRemap_16s_C1R_Ctx);
75impl_remap!(remap_i16_c3, i16, f32, C3, nppiRemap_16s_C3R_Ctx);
76impl_remap!(remap_i16_c4, i16, f32, C4, nppiRemap_16s_C4R_Ctx);
77impl_remap!(remap_i16_ac4, i16, f32, AC4, nppiRemap_16s_AC4R_Ctx);
78impl_remap!(remap_f32_c1, f32, f32, C1, nppiRemap_32f_C1R_Ctx);
79impl_remap!(remap_f32_c3, f32, f32, C3, nppiRemap_32f_C3R_Ctx);
80impl_remap!(remap_f32_c4, f32, f32, C4, nppiRemap_32f_C4R_Ctx);
81impl_remap!(remap_f32_ac4, f32, f32, AC4, nppiRemap_32f_AC4R_Ctx);
82impl_remap!(remap_f64_c1, f64, f64, C1, nppiRemap_64f_C1R_Ctx);
83impl_remap!(remap_f64_c3, f64, f64, C3, nppiRemap_64f_C3R_Ctx);
84impl_remap!(remap_f64_c4, f64, f64, C4, nppiRemap_64f_C4R_Ctx);
85impl_remap!(remap_f64_ac4, f64, f64, AC4, nppiRemap_64f_AC4R_Ctx);
86
87pub trait RemapOperation<L: ChannelLayout>: DataTypeLike {
88    type Map: DataTypeLike;
89
90    fn remap(
91        stream_context: &StreamContext,
92        remap: &Remap,
93        source: &ImageView<'_, Self, L>,
94        x_map: &ImageView<'_, Self::Map, C1>,
95        y_map: &ImageView<'_, Self::Map, C1>,
96        destination: &mut ImageViewMut<'_, Self, L>,
97    ) -> Result<()>;
98}
99
100macro_rules! impl_remap_operation {
101    ($ty:ty, $map_ty:ty, $layout:ty, $function:ident) => {
102        impl RemapOperation<$layout> for $ty {
103            type Map = $map_ty;
104
105            fn remap(
106                stream_context: &StreamContext,
107                remap: &Remap,
108                source: &ImageView<'_, Self, $layout>,
109                x_map: &ImageView<'_, Self::Map, C1>,
110                y_map: &ImageView<'_, Self::Map, C1>,
111                destination: &mut ImageViewMut<'_, Self, $layout>,
112            ) -> Result<()> {
113                $function(stream_context, remap, source, x_map, y_map, destination)
114            }
115        }
116    };
117}
118
119impl_remap_operation!(u8, f32, C1, remap_u8_c1);
120impl_remap_operation!(u8, f32, C3, remap_u8_c3);
121impl_remap_operation!(u8, f32, C4, remap_u8_c4);
122impl_remap_operation!(u8, f32, AC4, remap_u8_ac4);
123impl_remap_operation!(u16, f32, C1, remap_u16_c1);
124impl_remap_operation!(u16, f32, C3, remap_u16_c3);
125impl_remap_operation!(u16, f32, C4, remap_u16_c4);
126impl_remap_operation!(u16, f32, AC4, remap_u16_ac4);
127impl_remap_operation!(i16, f32, C1, remap_i16_c1);
128impl_remap_operation!(i16, f32, C3, remap_i16_c3);
129impl_remap_operation!(i16, f32, C4, remap_i16_c4);
130impl_remap_operation!(i16, f32, AC4, remap_i16_ac4);
131impl_remap_operation!(f32, f32, C1, remap_f32_c1);
132impl_remap_operation!(f32, f32, C3, remap_f32_c3);
133impl_remap_operation!(f32, f32, C4, remap_f32_c4);
134impl_remap_operation!(f32, f32, AC4, remap_f32_ac4);
135impl_remap_operation!(f64, f64, C1, remap_f64_c1);
136impl_remap_operation!(f64, f64, C3, remap_f64_c3);
137impl_remap_operation!(f64, f64, C4, remap_f64_c4);
138impl_remap_operation!(f64, f64, AC4, remap_f64_ac4);
139
140pub fn remap<T, L>(
141    stream_context: &StreamContext,
142    remap: &Remap,
143    source: &ImageView<'_, T, L>,
144    x_map: &ImageView<'_, T::Map, C1>,
145    y_map: &ImageView<'_, T::Map, C1>,
146    destination: &mut ImageViewMut<'_, T, L>,
147) -> Result<()>
148where
149    T: RemapOperation<L>,
150    L: ChannelLayout,
151{
152    T::remap(stream_context, remap, source, x_map, y_map, destination)
153}
154
155impl_remap_planar!(remap_u8_p3, u8, f32, 3, nppiRemap_8u_P3R_Ctx);
156impl_remap_planar!(remap_u8_p4, u8, f32, 4, nppiRemap_8u_P4R_Ctx);
157impl_remap_planar!(remap_u16_p3, u16, f32, 3, nppiRemap_16u_P3R_Ctx);
158impl_remap_planar!(remap_u16_p4, u16, f32, 4, nppiRemap_16u_P4R_Ctx);
159impl_remap_planar!(remap_i16_p3, i16, f32, 3, nppiRemap_16s_P3R_Ctx);
160impl_remap_planar!(remap_i16_p4, i16, f32, 4, nppiRemap_16s_P4R_Ctx);
161impl_remap_planar!(remap_f32_p3, f32, f32, 3, nppiRemap_32f_P3R_Ctx);
162impl_remap_planar!(remap_f32_p4, f32, f32, 4, nppiRemap_32f_P4R_Ctx);
163impl_remap_planar!(remap_f64_p3, f64, f64, 3, nppiRemap_64f_P3R_Ctx);
164impl_remap_planar!(remap_f64_p4, f64, f64, 4, nppiRemap_64f_P4R_Ctx);
165
166pub trait RemapPlanar<const PLANES: usize>: DataTypeLike {
167    type Map: DataTypeLike;
168
169    fn remap_planar(
170        stream_context: &StreamContext,
171        remap: &Remap,
172        source: &PlanarImageView<'_, Self, PLANES>,
173        x_map: &ImageView<'_, Self::Map, C1>,
174        y_map: &ImageView<'_, Self::Map, C1>,
175        destination: &mut PlanarImageViewMut<'_, Self, PLANES>,
176    ) -> Result<()>;
177}
178
179macro_rules! impl_remap_planar_dispatch {
180    ($ty:ty, $map_ty:ty, $planes:literal, $function:ident) => {
181        impl RemapPlanar<$planes> for $ty {
182            type Map = $map_ty;
183
184            fn remap_planar(
185                stream_context: &StreamContext,
186                remap: &Remap,
187                source: &PlanarImageView<'_, Self, $planes>,
188                x_map: &ImageView<'_, Self::Map, C1>,
189                y_map: &ImageView<'_, Self::Map, C1>,
190                destination: &mut PlanarImageViewMut<'_, Self, $planes>,
191            ) -> Result<()> {
192                $function(stream_context, remap, source, x_map, y_map, destination)
193            }
194        }
195    };
196}
197
198impl_remap_planar_dispatch!(u8, f32, 3, remap_u8_p3);
199impl_remap_planar_dispatch!(u8, f32, 4, remap_u8_p4);
200impl_remap_planar_dispatch!(u16, f32, 3, remap_u16_p3);
201impl_remap_planar_dispatch!(u16, f32, 4, remap_u16_p4);
202impl_remap_planar_dispatch!(i16, f32, 3, remap_i16_p3);
203impl_remap_planar_dispatch!(i16, f32, 4, remap_i16_p4);
204impl_remap_planar_dispatch!(f32, f32, 3, remap_f32_p3);
205impl_remap_planar_dispatch!(f32, f32, 4, remap_f32_p4);
206impl_remap_planar_dispatch!(f64, f64, 3, remap_f64_p3);
207impl_remap_planar_dispatch!(f64, f64, 4, remap_f64_p4);
208
209pub fn remap_planar<T, const PLANES: usize>(
210    stream_context: &StreamContext,
211    remap: &Remap,
212    source: &PlanarImageView<'_, T, PLANES>,
213    x_map: &ImageView<'_, T::Map, C1>,
214    y_map: &ImageView<'_, T::Map, C1>,
215    destination: &mut PlanarImageViewMut<'_, T, PLANES>,
216) -> Result<()>
217where
218    T: RemapPlanar<PLANES>,
219{
220    T::remap_planar(stream_context, remap, source, x_map, y_map, destination)
221}