Skip to content

ImageBuffer from FFI *mut c_uchar data without copying #2401

Answered by fintelia
raphaelmenges asked this question in Q&A
Discussion options

You must be logged in to vote

The second generic argument for ImageBuffer is a container type that dereferences to a &[u8] (assuming the image is 8-bits per pixel). We almost always use Vec<u8> for it, but technically it can be any type. I think you should be able to do something along the lines of:

image::ImageBuffer::<image::Rgb<u8>, &mut [u8]>::from_raw(
    width,
    height,
    unsafe { slice::from_raw_parts_mut(data, width as usize * height as usize * 3) }
);

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@raphaelmenges
Comment options

@fintelia
Comment options

@raphaelmenges
Comment options

Answer selected by raphaelmenges
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants