|
| 1 | +use pixman::{Color, Filter, FormatCode, Image, Operation, Repeat}; |
| 2 | + |
| 3 | +const WIDTH: usize = 400; |
| 4 | +const HEIGHT: usize = 400; |
| 5 | +const TILE_SIZE: usize = 25; |
| 6 | + |
| 7 | +pub fn main() { |
| 8 | + let mut checkerboard = Image::new(FormatCode::A8R8G8B8, WIDTH, HEIGHT, false).unwrap(); |
| 9 | + let mut destination = Image::new(FormatCode::A8R8G8B8, WIDTH, HEIGHT, false).unwrap(); |
| 10 | + |
| 11 | + // let transform = Transform::new([ |
| 12 | + // [-1.96830, -1.82250, 512.12250], |
| 13 | + // [0.00000, -7.29000, 1458.00000], |
| 14 | + // [0.00000, -0.00911, 0.59231], |
| 15 | + // ]); |
| 16 | + |
| 17 | + for i in 0..(HEIGHT / TILE_SIZE) { |
| 18 | + for j in 0..(WIDTH / TILE_SIZE) { |
| 19 | + let u = (j + 1) as f64 / (WIDTH / TILE_SIZE) as f64; |
| 20 | + let v = (i + 1) as f64 / (HEIGHT / TILE_SIZE) as f64; |
| 21 | + let black = Color::new(0, 0, 0, 0xffff); |
| 22 | + let white = Color::new( |
| 23 | + (v * 0xffff as f64) as u16, |
| 24 | + (u * 0xffff as f64) as u16, |
| 25 | + ((1.0 - (u as f64)) * 0xffff as f64) as u16, |
| 26 | + 0xffff, |
| 27 | + ); |
| 28 | + |
| 29 | + let c = if (j & 1) != (i & 1) { black } else { white }; |
| 30 | + |
| 31 | + let fill = Image::solid_fill(c).unwrap(); |
| 32 | + |
| 33 | + checkerboard.composite( |
| 34 | + Operation::Src, |
| 35 | + &fill, |
| 36 | + None, |
| 37 | + 0, |
| 38 | + 0, |
| 39 | + 0, |
| 40 | + 0, |
| 41 | + (j * TILE_SIZE) as i16, |
| 42 | + (i * TILE_SIZE) as i16, |
| 43 | + TILE_SIZE as u16, |
| 44 | + TILE_SIZE as u16, |
| 45 | + ); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // NOTE: The transform from the original demo completely breaks the image |
| 50 | + // checkerboard.set_transform(transform).unwrap(); |
| 51 | + checkerboard.set_filter(Filter::Best, &[]).unwrap(); |
| 52 | + checkerboard.set_repeat(Repeat::None); |
| 53 | + |
| 54 | + destination.composite( |
| 55 | + Operation::Src, |
| 56 | + &checkerboard, |
| 57 | + None, |
| 58 | + 0, |
| 59 | + 0, |
| 60 | + 0, |
| 61 | + 0, |
| 62 | + 0, |
| 63 | + 0, |
| 64 | + WIDTH as u16, |
| 65 | + HEIGHT as u16, |
| 66 | + ); |
| 67 | + |
| 68 | + let mut out_img = Image::new( |
| 69 | + FormatCode::A8B8G8R8, |
| 70 | + destination.width(), |
| 71 | + destination.height(), |
| 72 | + false, |
| 73 | + ) |
| 74 | + .unwrap(); |
| 75 | + out_img.composite( |
| 76 | + Operation::Src, |
| 77 | + &destination, |
| 78 | + None, |
| 79 | + 0, |
| 80 | + 0, |
| 81 | + 0, |
| 82 | + 0, |
| 83 | + 0, |
| 84 | + 0, |
| 85 | + destination.width() as u16, |
| 86 | + destination.height() as u16, |
| 87 | + ); |
| 88 | + |
| 89 | + let out_data = out_img.data().unwrap(); |
| 90 | + let image_buffer = image::ImageBuffer::<image::Rgba<u8>, _>::from_raw( |
| 91 | + out_img.width() as u32, |
| 92 | + out_img.height() as u32, |
| 93 | + out_data, |
| 94 | + ) |
| 95 | + .unwrap(); |
| 96 | + image_buffer |
| 97 | + .save_with_format("out.png", image::ImageFormat::Png) |
| 98 | + .unwrap(); |
| 99 | +} |
0 commit comments