File: checkerboard.rs

package info (click to toggle)
rust-pixman 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296 kB
  • sloc: makefile: 2
file content (91 lines) | stat: -rw-r--r-- 2,673 bytes parent folder | download
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use pixman::{Color, Filter, FormatCode, Image, Operation, Repeat, Solid};

const WIDTH: usize = 400;
const HEIGHT: usize = 400;
const TILE_SIZE: usize = 25;

pub fn main() {
    let mut checkerboard = Image::new(FormatCode::A8R8G8B8, WIDTH, HEIGHT, false).unwrap();
    let mut destination = Image::new(FormatCode::A8R8G8B8, WIDTH, HEIGHT, false).unwrap();

    // let transform = Transform::new([
    //     [-1.96830, -1.82250, 512.12250],
    //     [0.00000, -7.29000, 1458.00000],
    //     [0.00000, -0.00911, 0.59231],
    // ]);

    for i in 0..(HEIGHT / TILE_SIZE) {
        for j in 0..(WIDTH / TILE_SIZE) {
            let u = (j + 1) as f64 / (WIDTH / TILE_SIZE) as f64;
            let v = (i + 1) as f64 / (HEIGHT / TILE_SIZE) as f64;
            let black = Color::new(0, 0, 0, 0xffff);
            let white = Color::new(
                (v * 0xffff as f64) as u16,
                (u * 0xffff as f64) as u16,
                ((1.0 - u) * 0xffff as f64) as u16,
                0xffff,
            );

            let c = if (j & 1) != (i & 1) { black } else { white };

            let fill = Solid::new(c).unwrap();

            checkerboard.composite(
                Operation::Src,
                &fill,
                None,
                (0, 0),
                (0, 0),
                ((j * TILE_SIZE) as i16, (i * TILE_SIZE) as i16),
                (TILE_SIZE as u16, TILE_SIZE as u16),
            );
        }
    }

    // NOTE: The transform from the original demo completely breaks the image
    // checkerboard.set_transform(transform).unwrap();
    checkerboard.set_filter(Filter::Best, &[]).unwrap();
    checkerboard.set_repeat(Repeat::None);

    destination.composite(
        Operation::Src,
        &checkerboard,
        None,
        (0, 0),
        (0, 0),
        (0, 0),
        (WIDTH as u16, HEIGHT as u16),
    );

    let mut out_img = Image::new(
        FormatCode::A8B8G8R8,
        destination.width(),
        destination.height(),
        false,
    )
    .unwrap();
    out_img.composite(
        Operation::Src,
        &destination,
        None,
        (0, 0),
        (0, 0),
        (0, 0),
        (destination.width() as u16, destination.height() as u16),
    );

    let image_buffer = image::ImageBuffer::<image::Rgba<u8>, _>::from_raw(
        out_img.width() as u32,
        out_img.height() as u32,
        unsafe {
            std::slice::from_raw_parts(
                out_img.data() as *const u8,
                out_img.stride() * out_img.height(),
            )
        },
    )
    .unwrap();
    image_buffer
        .save_with_format("out.png", image::ImageFormat::Png)
        .unwrap();
}