| 1 | #![cfg(feature = "image")] |
| 2 | |
| 3 | use craterust-analyzer cargo qrcode 0.14.1 crate/::renderrust-analyzer cargo qrcode 0.14.1 render/::{Canvasrust-analyzer cargo qrcode 0.14.1 render/Canvas#, Pixelrust-analyzer cargo qrcode 0.14.1 render/Pixel#}; |
| 4 | use craterust-analyzer cargo qrcode 0.14.1 crate/::typesrust-analyzer cargo qrcode 0.14.1 types/::Colorrust-analyzer cargo qrcode 0.14.1 types/Color#; |
| 5 | |
| 6 | use imagerust-analyzer cargo image 0.25.0 crate/::{ImageBufferrust-analyzer cargo image 0.25.0 buffer_/ImageBuffer#, Lumarust-analyzer cargo image 0.25.0 color/Luma#, LumaArust-analyzer cargo image 0.25.0 color/LumaA#, Primitiverust-analyzer cargo image 0.25.0 traits/Primitive#, Rgbrust-analyzer cargo image 0.25.0 color/Rgb#, Rgbarust-analyzer cargo image 0.25.0 color/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_pixelrust-analyzer cargo qrcode 0.14.1 render/image/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_pixelrust-analyzer cargo qrcode 0.14.1 render/image/impl_pixel_for_image_pixel!! { Lumarust-analyzer cargo image 0.25.0 color/Luma#<S>: p => [p] } |
| 30 | impl_pixel_for_image_pixelrust-analyzer cargo qrcode 0.14.1 render/image/impl_pixel_for_image_pixel!! { LumaArust-analyzer cargo image 0.25.0 color/LumaA#<S>: p => [p, S::max_valuerust-analyzer cargo num-traits 0.2.17 bounds/Bounded#max_value().()] } |
| 31 | impl_pixel_for_image_pixelrust-analyzer cargo qrcode 0.14.1 render/image/impl_pixel_for_image_pixel!! { Rgbrust-analyzer cargo image 0.25.0 color/Rgb#<S>: p => [p, p, p] } |
| 32 | impl_pixel_for_image_pixelrust-analyzer cargo qrcode 0.14.1 render/image/impl_pixel_for_image_pixel!! { Rgbarust-analyzer cargo image 0.25.0 color/Rgba#<S>: p => [p, p, p, S::max_valuerust-analyzer cargo num-traits 0.2.17 bounds/Bounded#max_value().()] } |
| 33 | |
| 34 | impl<P: imagerust-analyzer cargo image 0.25.0 crate/::Pixelrust-analyzer cargo image 0.25.0 traits/Pixel# + 'static> Canvasrust-analyzer cargo qrcode 0.14.1 render/Canvas# for (P, ImageBufferrust-analyzer cargo image 0.25.0 buffer_/ImageBuffer#<P, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<P::Subpixelrust-analyzer cargo image 0.25.0 traits/Pixel#Subpixel#>>) { |
| 35 | type Pixelrust-analyzer cargo qrcode 0.14.1 render/image/impl#[`(P, ImageBuffer<P, Vec<<P as Pixel>::Subpixel>>)`][Canvas]Pixel# = P; |
| 36 | type Imagerust-analyzer cargo qrcode 0.14.1 render/image/impl#[`(P, ImageBuffer<P, Vec<<P as Pixel>::Subpixel>>)`][Canvas]Image# = ImageBufferrust-analyzer cargo image 0.25.0 buffer_/ImageBuffer#<P, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<P::Subpixelrust-analyzer cargo image 0.25.0 traits/Pixel#Subpixel#>>; |
| 37 | |
| 38 | fn newrust-analyzer cargo qrcode 0.14.1 render/image/impl#[`(P, ImageBuffer<P, Vec<<P as Pixel>::Subpixel>>)`][Canvas]new().(width: u32, height: u32, dark_pixel: P, light_pixel: P) -> Selfrust-analyzer cargo qrcode 0.14.1 render/image/impl#[`(P, ImageBuffer<P, Vec<<P as Pixel>::Subpixel>>)`][Canvas] { |
| 39 | (dark_pixel, ImageBufferrust-analyzer cargo image 0.25.0 buffer_/ImageBuffer#::from_pixelrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Vec<<P as Pixel>::Subpixel>>`]from_pixel().(width, height, light_pixel)) |
| 40 | } |
| 41 | |
| 42 | fn draw_dark_pixelrust-analyzer cargo qrcode 0.14.1 render/image/impl#[`(P, ImageBuffer<P, Vec<<P as Pixel>::Subpixel>>)`][Canvas]draw_dark_pixel().(&mut self, x: u32, y: u32) { |
| 43 | self.1.put_pixelrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]put_pixel().(x, y, self.0); |
| 44 | } |
| 45 | |
| 46 | fn into_imagerust-analyzer cargo qrcode 0.14.1 render/image/impl#[`(P, ImageBuffer<P, Vec<<P as Pixel>::Subpixel>>)`][Canvas]into_image().(self) -> ImageBufferrust-analyzer cargo image 0.25.0 buffer_/ImageBuffer#<P, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<P::Subpixelrust-analyzer cargo image 0.25.0 traits/Pixel#Subpixel#>> { |
| 47 | self.1 |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | #[cfg(test)] |
| 52 | mod render_testsrust-analyzer cargo qrcode 0.14.1 render/image/render_tests/ { |
| 53 | use craterust-analyzer cargo qrcode 0.14.1 crate/::renderrust-analyzer cargo qrcode 0.14.1 render/::Rendererrust-analyzer cargo qrcode 0.14.1 render/Renderer#; |
| 54 | use craterust-analyzer cargo qrcode 0.14.1 crate/::typesrust-analyzer cargo qrcode 0.14.1 types/::Colorrust-analyzer cargo qrcode 0.14.1 types/Color#; |
| 55 | use imagerust-analyzer cargo image 0.25.0 crate/::{Lumarust-analyzer cargo image 0.25.0 color/Luma#, Rgbarust-analyzer cargo image 0.25.0 color/Rgba#}; |
| 56 | |
| 57 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 58 | fn test_render_luma8_unsizedrust-analyzer cargo qrcode 0.14.1 render/image/render_tests/test_render_luma8_unsized().() { |
| 59 | let image = Rendererrust-analyzer cargo qrcode 0.14.1 render/Renderer#::<Lumarust-analyzer cargo image 0.25.0 color/Luma#<u8>>::newrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]new().( |
| 60 | &[ |
| 61 | Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#, |
| 62 | Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Darkrust-analyzer cargo qrcode 0.14.1 types/Color#Dark#, |
| 63 | Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Darkrust-analyzer cargo qrcode 0.14.1 types/Color#Dark#, |
| 64 | // |
| 65 | Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Darkrust-analyzer cargo qrcode 0.14.1 types/Color#Dark#, |
| 66 | Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#, |
| 67 | Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#, |
| 68 | // |
| 69 | Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#, |
| 70 | Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Darkrust-analyzer cargo qrcode 0.14.1 types/Color#Dark#, |
| 71 | Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#, |
| 72 | ], |
| 73 | 3, |
| 74 | 1, |
| 75 | ) |
| 76 | .module_dimensionsrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]module_dimensions().(1, 1) |
| 77 | .buildrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]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_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(image.into_rawrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]into_raw().(), expected); |
| 88 | } |
| 89 | |
| 90 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 91 | fn test_render_rgba_unsizedrust-analyzer cargo qrcode 0.14.1 render/image/render_tests/test_render_rgba_unsized().() { |
| 92 | let image = Rendererrust-analyzer cargo qrcode 0.14.1 render/Renderer#::<Rgbarust-analyzer cargo image 0.25.0 color/Rgba#<u8>>::newrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]new().(&[Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#, Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Darkrust-analyzer cargo qrcode 0.14.1 types/Color#Dark#, Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Darkrust-analyzer cargo qrcode 0.14.1 types/Color#Dark#, Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Darkrust-analyzer cargo qrcode 0.14.1 types/Color#Dark#], 2, 1) |
| 93 | .module_dimensionsrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]module_dimensions().(1, 1) |
| 94 | .buildrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]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_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(image.into_rawrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]into_raw().(), expected); |
| 105 | } |
| 106 | |
| 107 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 108 | fn test_render_resized_minrust-analyzer cargo qrcode 0.14.1 render/image/render_tests/test_render_resized_min().() { |
| 109 | let image = Rendererrust-analyzer cargo qrcode 0.14.1 render/Renderer#::<Lumarust-analyzer cargo image 0.25.0 color/Luma#<u8>>::newrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]new().(&[Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Darkrust-analyzer cargo qrcode 0.14.1 types/Color#Dark#, Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#, Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#, Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Darkrust-analyzer cargo qrcode 0.14.1 types/Color#Dark#], 2, 1) |
| 110 | .min_dimensionsrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]min_dimensions().(10, 10) |
| 111 | .buildrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]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_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(image.dimensionsrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]dimensions().(), (12, 12)); |
| 133 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(image.into_rawrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]into_raw().(), expected); |
| 134 | } |
| 135 | |
| 136 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 137 | fn test_render_resized_maxrust-analyzer cargo qrcode 0.14.1 render/image/render_tests/test_render_resized_max().() { |
| 138 | let image = Rendererrust-analyzer cargo qrcode 0.14.1 render/Renderer#::<Lumarust-analyzer cargo image 0.25.0 color/Luma#<u8>>::newrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]new().(&[Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Darkrust-analyzer cargo qrcode 0.14.1 types/Color#Dark#, Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#, Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#, Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Darkrust-analyzer cargo qrcode 0.14.1 types/Color#Dark#], 2, 1) |
| 139 | .max_dimensionsrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]max_dimensions().(10, 5) |
| 140 | .buildrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]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_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(image.dimensionsrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]dimensions().(), (8, 4)); |
| 154 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(image.into_rawrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]into_raw().(), expected); |
| 155 | } |
| 156 | } |