1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Copyright (c) 2023.
*
* This software is free software;
*
* You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
*/
//! Rotate an image
//!
//! # WARNING
//! - This only works for rotating 180 degrees.
//!
//! It doesn't work for other rotate angles, this will be fixed later
//!
use zune_core::bit_depth::BitType;
use zune_core::log::trace;
use zune_image::channel::Channel;
use zune_image::errors::ImageErrors;
use zune_image::image::Image;
use zune_image::traits::OperationsTrait;
pub struct Rotate {
angle: f32
}
impl Rotate {
#[must_use]
pub fn new(angle: f32) -> Rotate {
Rotate { angle }
}
}
impl OperationsTrait for Rotate {
fn name(&self) -> &'static str {
"Rotate"
}
fn execute_impl(&self, image: &mut Image) -> Result<(), ImageErrors> {
let im_type = image.depth().bit_type();
let (width, height) = image.dimensions();
let will_change_dims = (self.angle - 180.0).abs() > f32::EPSILON;
#[cfg(feature = "threads")]
{
trace!("Running rotate in multithreaded mode");
std::thread::scope(|s| {
let mut errors = vec![];
// blur each channel on a separate thread
for channel in image.channels_mut(false) {
let result = s.spawn(|| {
let mut new_channel =
Channel::new_with_length_and_type(channel.len(), channel.get_type_id());
match im_type {
BitType::U8 => {
rotate::<u8>(
self.angle,
width,
height,
channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?
);
}
BitType::U16 => {
rotate::<u16>(
self.angle,
width,
height,
channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?
);
}
BitType::F32 => rotate::<f32>(
self.angle,
width,
height,
channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?
),
d => {
return Err(ImageErrors::ImageOperationNotImplemented(
self.name(),
d
))
}
};
*channel = new_channel;
Ok(())
});
errors.push(result);
}
errors
.into_iter()
.map(|x| x.join().unwrap())
.collect::<Result<Vec<()>, ImageErrors>>()
})?;
}
#[cfg(not(feature = "threads"))]
{
trace!("Running rotate in single-threaded mode");
for channel in image.channels_mut(false) {
let mut new_channel =
Channel::new_with_length_and_type(channel.len(), channel.get_type_id());
match im_type {
BitType::U8 => {
rotate::<u8>(
self.angle,
width,
height,
channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?
);
}
BitType::U16 => {
rotate::<u16>(
self.angle,
width,
height,
channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?
);
}
BitType::F32 => rotate::<f32>(
self.angle,
width,
height,
channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?
),
d => return Err(ImageErrors::ImageOperationNotImplemented(self.name(), d))
};
*channel = new_channel;
}
}
if will_change_dims {
change_image_dims(image, self.angle);
}
Ok(())
}
fn supported_types(&self) -> &'static [BitType] {
&[BitType::U8, BitType::U16, BitType::F32]
}
}
fn change_image_dims(image: &mut Image, angle: f32) {
let (ow, oh) = image.dimensions();
if (angle - 90.0).abs() < f32::EPSILON {
image.set_dimensions(oh, ow);
}
if (angle - 270.0).abs() < f32::EPSILON {
image.set_dimensions(oh, ow);
}
}
pub fn rotate<T: Copy>(
angle: f32, width: usize, height: usize, in_image: &[T], out_image: &mut [T]
) {
let angle = angle % 360.0;
if (angle - 180.0).abs() < f32::EPSILON {
// copy in image to out image
out_image.copy_from_slice(in_image);
rotate_180(out_image, width);
}
if (angle - 90.0).abs() < f32::EPSILON {
rotate_90(in_image, out_image, width, height);
}
if (angle - 270.0).abs() < f32::EPSILON {
rotate_270(in_image, out_image, width, height);
}
}
fn rotate_180<T: Copy>(in_out_image: &mut [T], width: usize) {
let half = in_out_image.len() / 2;
let (top, bottom) = in_out_image.split_at_mut(half);
for (top_chunk, bottom_chunk) in top
.chunks_exact_mut(width)
.zip(bottom.chunks_exact_mut(width).rev())
{
for (a, b) in top_chunk.iter_mut().zip(bottom_chunk.iter_mut()) {
core::mem::swap(a, b);
}
}
}
fn rotate_90<T: Copy>(in_image: &[T], out_image: &mut [T], width: usize, height: usize) {
for (y, pixels) in in_image.chunks_exact(width).enumerate() {
let idx = height - y - 1;
for (x, pix) in pixels.iter().enumerate() {
if let Some(c) = out_image.get_mut((x * height) + idx) {
*c = *pix;
}
}
}
}
fn rotate_270<T: Copy>(in_image: &[T], out_image: &mut [T], width: usize, height: usize) {
for (y, pixels) in in_image.chunks_exact(width).enumerate() {
for (x, pix) in pixels.iter().enumerate() {
let y_idx = (width - x - 1) * height;
if let Some(c) = out_image.get_mut(y_idx + y) {
*c = *pix;
}
}
}
}
// #[cfg(test)]
// mod tests {
// use zune_image::image::Image;
// use zune_image::traits::OperationsTrait;
//
// use crate::rotate::Rotate;
//
// #[test]
// fn rotate_over() {
// let mut dst_image = Image::open("/home/caleb/Pictures/ANIME/418724.png").unwrap();
// println!("{:?}", dst_image.dimensions());
//
// Rotate::new(270.0).execute(&mut dst_image).unwrap();
//
// println!("{:?}", dst_image.dimensions());
// dst_image.save("./composite.jpg").unwrap();
// }
// }