Created
          June 18, 2025 20:07 
        
      - 
      
 - 
        
Save CptLemming/e3774ed202f8c2025ba6b370d8299135 to your computer and use it in GitHub Desktop.  
    imageproc map_pixels_mut from slice
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | use image::{ImageBuffer, Rgba}; | |
| use imageproc::{map::map_pixels_mut, window::display_image}; | |
| const WIDTH: u32 = 500; | |
| const HEIGHT: u32 = 500; | |
| const NUM_PIXELS: u32 = WIDTH * HEIGHT; | |
| const PIXEL_SIZE: u32 = 4; | |
| const NUM_BYTES: u32 = NUM_PIXELS * PIXEL_SIZE; | |
| fn main() { | |
| // let mut data = [0u8; NUM_BYTES as usize]; | |
| // let buf = &mut data[..]; | |
| let buf = unsafe { | |
| let x = 1u8; | |
| let ptr = x as *mut _; | |
| std::slice::from_raw_parts_mut(ptr, NUM_BYTES as usize) | |
| }; | |
| let mut image: ImageBuffer<Rgba<u8>, &mut [u8]> = | |
| ImageBuffer::from_raw(WIDTH, HEIGHT, buf).expect("No image"); | |
| map_pixels_mut(&mut image, |x, y, p| Rgba([100, p.0[1], p.0[2], 255])); | |
| display_image("", &image, WIDTH, HEIGHT); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment