1#![allow(clippy::module_name_repetitions)]
2use std::ops::Deref;
3
4use image::{GenericImageView, Pixel};
5
6use crate::BinaryImage;
7
8#[derive(Debug, Clone, Copy)]
9pub enum BinaryView<'a, I: GenericImageView> {
10 Ref(&'a I),
11 Image(I),
12}
13
14impl<I, P> GenericImageView for BinaryView<'_, I>
15where
16 I: GenericImageView<Pixel = P>,
17 P: Pixel,
18 crate::Bit: From<P>,
19{
20 type Pixel = crate::Bit;
21 #[inline]
22 unsafe fn unsafe_get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
23 crate::Bit::from(self.deref().unsafe_get_pixel(x, y))
24 }
25 #[inline]
26 fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
27 debug_assert!(self.deref().in_bounds(x, y), "Pixel out of bounds");
28 unsafe { self.unsafe_get_pixel(x, y) }
29 }
30 #[inline]
31 fn dimensions(&self) -> (u32, u32) {
32 self.deref().dimensions()
33 }
34 #[inline]
35 fn height(&self) -> u32 {
36 self.deref().height()
37 }
38 #[inline]
39 fn width(&self) -> u32 {
40 self.deref().width()
41 }
42}
43
44impl<I, P> Deref for BinaryView<'_, I>
45where
46 I: GenericImageView<Pixel = P>,
47 P: Pixel,
48 crate::Bit: From<P>,
49{
50 type Target = I;
51 fn deref(&self) -> &Self::Target {
52 match self {
53 Self::Ref(image) => image,
54 Self::Image(image) => image,
55 }
56 }
57}
58
59impl<'a, I, P> From<BinaryView<'a, I>> for crate::BinaryImage
60where
61 I: GenericImageView<Pixel = P>,
62 P: Pixel,
63 crate::Bit: From<P>,
64{
65 fn from(view: BinaryView<'a, I>) -> BinaryImage {
66 BinaryImage {
67 height: view.height(),
68 width: view.width(),
69 buffer: view.pixels().map(|(_, _, pixel)| *pixel).collect(),
70 }
71 }
72}