Cykooz / fast_image_resize

Rust library for fast image resizing with using of SIMD instructions.
Apache License 2.0
310 stars 29 forks source link

Allow passing an immutable slice to Image::from_slice_u8 #18

Closed tuzz closed 11 months ago

tuzz commented 11 months ago

Currently, this method takes a mutable slice, but it does not need to if the Image is used as the source.

For my use case, I do not have mutable ownership of the source image because I am using the same source image across multiple threads. I am therefore having to unsafely cast the immutable slice to a mutable slice to adhere to the interface:

let mut_ptr = image.bytes.as_ptr() as *mut u8;
let mut_slice = unsafe { std::slice::from_raw_parts_mut(mut_ptr, image.bytes.len()) };

let width = NonZeroU32::new(image.bytes_per_row / 4).unwrap();
let height = NonZeroU32::new(image.height).unwrap();
let source = fr::Image::from_slice_u8(width, height, mut_slice, fr::PixelType::U8x4).unwrap();

Please would you be able to change the interface so that from_slice_u8 can be called with an immutable slice, or provide an alternative method for this? Thank you for maintaining a very useful crate.

Cykooz commented 11 months ago

Image is optional helper. It is mainly useful to create a destination image. Resizer::resize() accepts &DynamicImageView. You can create DynamicImageView directly from immutable slice:

let source_view = DynamicImageView::U8x4(
    ImageView::from_buffer(width, height, slice_u8).unwrap()
);
tuzz commented 11 months ago

Perfect, thanks! I'll close this now.