■ ImageBuffer<P, Container> 구조체의 from_fn 연관 함수를 사용해 이미지를 생성하는 방법을 보여준다.
▶ Cargo.toml
1 2 3 4 5 6 7 8 9 10 11 |
[package] name = "test_project" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] image = "0.23.14" |
▶ src/main.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
use image; fn main() { let white_rgb : image::Rgb<u8> = image::Rgb::<u8>([255, 255, 255]); let red_rgb : image::Rgb<u8> = image::Rgb::<u8>([255, 90, 90]); let checker_width : u32 = 64; let draw = |x, y| { let (x_index, y_index) = (x / checker_width, y / checker_width); match (x_index % 2, y_index % 2) { (0, 0) => white_rgb, (1, 0) => red_rgb, (0, 1) => red_rgb, (1, 1) => white_rgb, (_, _) => panic!("error"), } }; let image_buffer : image::ImageBuffer<image::Rgb<u8>, Vec<u8>> = image::ImageBuffer::from_fn(512, 512, draw); image_buffer.save("d:/checkerboard.png").unwrap(); } |