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
236
237
238
/*
* 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
*/
//! (BROKEN): Resize an image to a new width and height
//!
//! (BROKEN): Do not use, **IT DOESN'T WORK**
use zune_core::bit_depth::BitType;
use zune_image::channel::Channel;
use zune_image::errors::ImageErrors;
use zune_image::image::Image;
use zune_image::traits::OperationsTrait;
use crate::traits::NumOps;
mod bicubic;
mod bilinear;
#[derive(Copy, Clone, Debug)]
pub enum ResizeMethod {
Bilinear //Bicubic
}
/// Resize an image to a new width and height
/// using the resize method specified
#[derive(Copy, Clone)]
pub struct Resize {
new_width: usize,
new_height: usize,
method: ResizeMethod
}
impl Resize {
/// Create a new resize operation
///
/// # Argument
/// - new_width: The new image width
/// - new_height: The new image height.
/// - method: The resize method to use
#[must_use]
pub fn new(new_width: usize, new_height: usize, method: ResizeMethod) -> Resize {
Resize {
new_width,
new_height,
method
}
}
}
impl OperationsTrait for Resize {
fn name(&self) -> &'static str {
"Resize"
}
#[allow(clippy::too_many_lines)]
fn execute_impl(&self, image: &mut Image) -> Result<(), ImageErrors> {
let (old_w, old_h) = image.dimensions();
let depth = image.depth().bit_type();
let new_length = self.new_width * self.new_height * image.depth().size_of();
#[cfg(feature = "threads")]
{
std::thread::scope(|f| {
let mut errors = vec![];
for old_channel in image.channels_mut(false) {
let result = f.spawn(|| {
let mut new_channel = Channel::new_with_bit_type(new_length, depth);
match depth {
BitType::U8 => resize::<u8>(
old_channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?,
self.method,
old_w,
old_h,
self.new_width,
self.new_height
),
BitType::U16 => resize::<u16>(
old_channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?,
self.method,
old_w,
old_h,
self.new_width,
self.new_height
),
BitType::F32 => {
resize::<f32>(
old_channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?,
self.method,
old_w,
old_h,
self.new_width,
self.new_height
);
}
d => return Err(ImageErrors::ImageOperationNotImplemented("resize", d))
}
*old_channel = new_channel;
Ok(())
});
errors.push(result);
}
errors
.into_iter()
.map(|x| x.join().unwrap())
.collect::<Result<Vec<()>, ImageErrors>>()
})?;
}
#[cfg(not(feature = "threads"))]
{
for old_channel in image.channels_mut(false) {
let mut new_channel = Channel::new_with_bit_type(new_length, depth);
match depth {
BitType::U8 => resize::<u8>(
old_channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?,
self.method,
old_w,
old_h,
self.new_width,
self.new_height
),
BitType::U16 => resize::<u16>(
old_channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?,
self.method,
old_w,
old_h,
self.new_width,
self.new_height
),
BitType::F32 => {
resize::<f32>(
old_channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?,
self.method,
old_w,
old_h,
self.new_width,
self.new_height
);
}
d => return Err(ImageErrors::ImageOperationNotImplemented("resize", d))
}
*old_channel = new_channel;
}
}
image.set_dimensions(self.new_width, self.new_height);
Ok(())
}
fn supported_types(&self) -> &'static [BitType] {
&[BitType::U8, BitType::U16, BitType::F32]
}
}
/// Return the image resize dimensions that would not cause a distortion
/// taking into consideration the smaller dimension
#[must_use]
#[allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
pub fn ratio_dimensions_smaller(
old_w: usize, old_h: usize, new_w: usize, new_h: usize
) -> (usize, usize) {
let ratio_w = old_w as f64 / new_w as f64;
let ratio_h = old_h as f64 / new_h as f64;
let percent = if ratio_h < ratio_w { ratio_w } else { ratio_h };
let t = (old_w as f64 / percent) as usize;
let u = (old_h as f64 / percent) as usize;
(t, u)
}
/// Return the image resize dimensions that would not cause a distortion
/// taking into consideration the larger dimension
#[must_use]
#[allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
pub fn ratio_dimensions_larger(
old_w: usize, old_h: usize, new_w: usize, new_h: usize
) -> (usize, usize) {
let ratio_w = old_w as f64 / new_w as f64;
let ratio_h = old_h as f64 / new_h as f64;
let percent = if ratio_h < ratio_w { ratio_w } else { ratio_h };
let t = (old_w as f64 / percent) as usize;
let u = (old_h as f64 / percent) as usize;
(t, u)
}
/// Resize an image **channel** to new dimensions
///
/// # Arguments
/// - in_image: A contiguous slice of a single channel of an image
/// - out_image: Where we will store the new resized pixels
/// - method: The resizing method to use
/// - in_width: `in_image`'s width
/// - in_height: `in_image`'s height.
/// - out_width: The expected width
/// - out_height: The expected height.
/// # Panics
/// - `in_width*in_height` do not match `in_image.len()`.
/// - `out_width*out_height` do not match `out_image.len()`.
pub fn resize<T>(
in_image: &[T], out_image: &mut [T], method: ResizeMethod, in_width: usize, in_height: usize,
out_width: usize, out_height: usize
) where
T: Copy + NumOps<T>,
f32: std::convert::From<T>
{
match method {
ResizeMethod::Bilinear => {
bilinear::bilinear_impl(
in_image, out_image, in_width, in_height, out_width, out_height
);
} // ResizeMethod::Bicubic => {
// bicubic::resize_image_bicubic(
// in_image, out_image, in_width, in_height, out_width, out_height
// );
// }
}
}