Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tuzz opened this issue Dec 5, 2023 · 2 comments
Closed

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

tuzz opened this issue Dec 5, 2023 · 2 comments

Comments

@tuzz
Copy link

tuzz commented Dec 5, 2023

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.

@tuzz tuzz changed the title Allow passing an immutable slice to fast_image_resize::from_slice_u8 Allow passing an immutable slice to Image::from_slice_u8 Dec 5, 2023
@Cykooz
Copy link
Owner

Cykooz commented Dec 5, 2023

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
Copy link
Author

tuzz commented Dec 5, 2023

Perfect, thanks! I'll close this now.

@tuzz tuzz closed this as completed Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants