Skip to content

Commit

Permalink
feat: added threading to resize operation
Browse files Browse the repository at this point in the history
  • Loading branch information
SalOne22 committed Mar 13, 2024
1 parent 569b0ab commit 1e89b34
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/operations/resize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,58 @@ impl OperationsTrait for Resize {
let dst_width = NonZeroU32::new(dst_width as u32).unwrap();
let dst_height = NonZeroU32::new(dst_height as u32).unwrap();

#[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);

let src_image = fr::Image::from_slice_u8(
width,
height,
unsafe { old_channel.alias_mut() },
match depth {
BitType::U8 => fr::PixelType::U8,
BitType::U16 => fr::PixelType::U16,
BitType::F32 => fr::PixelType::F32,

d => {
return Err(ImageErrors::ImageOperationNotImplemented("resize", d))
}
},
)
.map_err(|e| ImageOperationsErrors::GenericString(e.to_string()))?;

let mut dst_image =
fr::Image::new(dst_width, dst_height, src_image.pixel_type());

let mut dst_view = dst_image.view_mut();

let mut resizer = fr::Resizer::new(self.algorithm);

resizer
.resize(&src_image.view(), &mut dst_view)
.map_err(|e| ImageOperationsErrors::GenericString(e.to_string()))?;

unsafe {
new_channel.alias_mut().copy_from_slice(dst_image.buffer());
}

*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);

Expand Down Expand Up @@ -85,6 +137,7 @@ impl OperationsTrait for Resize {

*old_channel = new_channel;
}

image.set_dimensions(dst_width.get() as usize, dst_height.get() as usize);

Ok(())
Expand Down

0 comments on commit 1e89b34

Please sign in to comment.