Debian Codegraph @

src/render/image.rs

raw symbols SCIP
1 #![cfg(feature = "image")]
2
3 use crate::render::{Canvas, Pixel};
4 use crate::types::Color;
5
6 use image::{ImageBuffer, Luma, LumaA, Primitive, Rgb, Rgba};
7
8 // need to keep using this macro to implement Pixel separately for each color model,
9 // otherwise we'll have conflicting impl with `impl Pixel for impl Element` 🤷
10 macro_rules! impl_pixel_for_image_pixel {
11 ($p:ident<$s:ident>: $c:pat => $d:expr) => {
12 impl<$s> Pixel for $p<$s>
13 where
14 $s: Primitive + 'static,
15 $p<$s>: image::Pixel<Subpixel = $s>,
16 {
17 type Image = ImageBuffer<Self, Vec<$s>>;
18 type Canvas = (Self, Self::Image);
19
20 fn default_color(color: Color) -> Self {
21 match color.select($s::zero(), $s::max_value()) {
22 $c => $p($d),
23 }
24 }
25 }
26 };
27 }
28
29 impl_pixel_for_image_pixel! { Luma<S>: p => [p] }
30 impl_pixel_for_image_pixel! { LumaA<S>: p => [p, S::max_value()] }
31 impl_pixel_for_image_pixel! { Rgb<S>: p => [p, p, p] }
32 impl_pixel_for_image_pixel! { Rgba<S>: p => [p, p, p, S::max_value()] }
33
34 impl<P: image::Pixel + 'static> Canvas for (P, ImageBuffer<P, Vec<P::Subpixel>>) {
35 type Pixel = P;
36 type Image = ImageBuffer<P, Vec<P::Subpixel>>;
37
38 fn new(width: u32, height: u32, dark_pixel: P, light_pixel: P) -> Self {
39 (dark_pixel, ImageBuffer::from_pixel(width, height, light_pixel))
40 }
41
42 fn draw_dark_pixel(&mut self, x: u32, y: u32) {
43 self.1.put_pixel(x, y, self.0);
44 }
45
46 fn into_image(self) -> ImageBuffer<P, Vec<P::Subpixel>> {
47 self.1
48 }
49 }
50
51 #[cfg(test)]
52 mod render_tests {
53 use crate::render::Renderer;
54 use crate::types::Color;
55 use image::{Luma, Rgba};
56
57 #[test]
58 fn test_render_luma8_unsized() {
59 let image = Renderer::<Luma<u8>>::new(
60 &[
61 Color::Light,
62 Color::Dark,
63 Color::Dark,
64 //
65 Color::Dark,
66 Color::Light,
67 Color::Light,
68 //
69 Color::Light,
70 Color::Dark,
71 Color::Light,
72 ],
73 3,
74 1,
75 )
76 .module_dimensions(1, 1)
77 .build();
78
79 #[rustfmt::skip]
80 let expected = [
81 255, 255, 255, 255, 255,
82 255, 255, 0, 0, 255,
83 255, 0, 255, 255, 255,
84 255, 255, 0, 255, 255,
85 255, 255, 255, 255, 255,
86 ];
87 assert_eq!(image.into_raw(), expected);
88 }
89
90 #[test]
91 fn test_render_rgba_unsized() {
92 let image = Renderer::<Rgba<u8>>::new(&[Color::Light, Color::Dark, Color::Dark, Color::Dark], 2, 1)
93 .module_dimensions(1, 1)
94 .build();
95
96 #[rustfmt::skip]
97 let expected: &[u8] = &[
98 255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
99 255,255,255,255, 255,255,255,255, 0, 0, 0,255, 255,255,255,255,
100 255,255,255,255, 0, 0, 0,255, 0, 0, 0,255, 255,255,255,255,
101 255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
102 ];
103
104 assert_eq!(image.into_raw(), expected);
105 }
106
107 #[test]
108 fn test_render_resized_min() {
109 let image = Renderer::<Luma<u8>>::new(&[Color::Dark, Color::Light, Color::Light, Color::Dark], 2, 1)
110 .min_dimensions(10, 10)
111 .build();
112
113 #[rustfmt::skip]
114 let expected: &[u8] = &[
115 255,255,255, 255,255,255, 255,255,255, 255,255,255,
116 255,255,255, 255,255,255, 255,255,255, 255,255,255,
117 255,255,255, 255,255,255, 255,255,255, 255,255,255,
118
119 255,255,255, 0, 0, 0, 255,255,255, 255,255,255,
120 255,255,255, 0, 0, 0, 255,255,255, 255,255,255,
121 255,255,255, 0, 0, 0, 255,255,255, 255,255,255,
122
123 255,255,255, 255,255,255, 0, 0, 0, 255,255,255,
124 255,255,255, 255,255,255, 0, 0, 0, 255,255,255,
125 255,255,255, 255,255,255, 0, 0, 0, 255,255,255,
126
127 255,255,255, 255,255,255, 255,255,255, 255,255,255,
128 255,255,255, 255,255,255, 255,255,255, 255,255,255,
129 255,255,255, 255,255,255, 255,255,255, 255,255,255,
130 ];
131
132 assert_eq!(image.dimensions(), (12, 12));
133 assert_eq!(image.into_raw(), expected);
134 }
135
136 #[test]
137 fn test_render_resized_max() {
138 let image = Renderer::<Luma<u8>>::new(&[Color::Dark, Color::Light, Color::Light, Color::Dark], 2, 1)
139 .max_dimensions(10, 5)
140 .build();
141
142 #[rustfmt::skip]
143 let expected: &[u8] = &[
144 255,255, 255,255, 255,255, 255,255,
145
146 255,255, 0, 0, 255,255, 255,255,
147
148 255,255, 255,255, 0, 0, 255,255,
149
150 255,255, 255,255, 255,255, 255,255,
151 ];
152
153 assert_eq!(image.dimensions(), (8, 4));
154 assert_eq!(image.into_raw(), expected);
155 }
156 }