binary_image/
lib.rs

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#![doc = include_str!("../README.md")]

use derive_more::{AsMut, AsRef, Deref, DerefMut, Display, From, Into};
use image::{GenericImageView, Pixel};
use num_traits::Zero;
pub use pixel::Bit;

mod pixel;
#[cfg(test)]
mod tests;
mod utils;

bitflags::bitflags! {
    /// Neighbor constants for 8-connectivity pixel access.
    #[repr(transparent)]
    #[derive(
        Clone,
        Copy,
        Debug,
        Display,
        Default,
        PartialEq,
        Eq,
        PartialOrd,
        Ord,
        Hash,
        Deref,
        DerefMut,
        AsMut,
        AsRef,
        Into,
        From,
    )]
    pub struct Neighbors: u8  {
        const NORTH     = 1 << 7;
        const SOUTH     = 1 << 6;
        const EAST      = 1 << 5;
        const WEST      = 1 << 4;
        const NORTHEAST = 1 << 3;
        const NORTHWEST = 1 << 2;
        const SOUTHEAST = 1 << 1;
        const SOUTHWEST = 1;
    }
}

impl Neighbors {
    #[must_use]
    pub fn get_neighbors<I>(image: &I, x: u32, y: u32) -> Self
    where
        I: GenericImageView<Pixel = Bit>,
    {
        let mut neighbors = Neighbors::empty();
        if y < u32::MAX && *image.get_pixel(x, y + 1) {
            neighbors |= Neighbors::NORTH;
        }
        if y > u32::MIN && *image.get_pixel(x, y - 1) {
            neighbors |= Neighbors::SOUTH;
        }
        if x < u32::MAX && *image.get_pixel(x + 1, y) {
            neighbors |= Neighbors::EAST;
        }
        if x > u32::MIN && *image.get_pixel(x - 1, y) {
            neighbors |= Neighbors::WEST;
        }
        if x < u32::MAX && y < u32::MAX && *image.get_pixel(x + 1, y + 1) {
            neighbors |= Neighbors::NORTHEAST;
        }
        if x > u32::MIN && y < u32::MAX && *image.get_pixel(x - 1, y + 1) {
            neighbors |= Neighbors::NORTHWEST;
        }
        if x < u32::MAX && y > u32::MIN && *image.get_pixel(x + 1, y - 1) {
            neighbors |= Neighbors::SOUTHEAST;
        }
        if x > u32::MIN && y > u32::MIN && *image.get_pixel(x - 1, y - 1) {
            neighbors |= Neighbors::SOUTHWEST;
        }
        neighbors
    }

    #[inline]
    #[must_use]
    pub fn is_corner<I>(image: &I, x: u32, y: u32) -> bool
    where
        I: GenericImageView<Pixel = Bit>,
    {
        *image.get_pixel(x, y) && utils::is_corner(Self::get_neighbors(image, x, y).bits())
    }
}

#[derive(Debug, Clone)]
pub struct BinaryImage {
    data: bit_vec::BitVec,
    height: u32,
    width: u32,
}

impl BinaryImage {
    #[must_use]
    pub fn from_raw<T>(height: u32, width: u32, data: &[T]) -> Self
    where
        T: num_traits::Zero,
    {
        debug_assert!(
            data.len() >= (height * width) as usize,
            "Data must not be smaller than image dimensions"
        );
        let compress_step = data.len() / (height * width) as usize;
        Self {
            data: data
                .chunks(compress_step)
                .map(|pixel| pixel.iter().any(|channel| !channel.is_zero()))
                .collect(),
            height,
            width,
        }
    }
}

impl GenericImageView for BinaryImage {
    type Pixel = pixel::Bit;
    #[inline]
    unsafe fn unsafe_get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
        Bit::from(self.data.get_unchecked((y * self.width + x) as usize))
    }
    #[inline]
    fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
        debug_assert!(self.in_bounds(x, y));
        unsafe { self.unsafe_get_pixel(x, y) }
    }
    #[inline]
    fn dimensions(&self) -> (u32, u32) {
        (self.width(), self.height())
    }
    #[inline]
    fn height(&self) -> u32 {
        self.height
    }
    #[inline]
    fn width(&self) -> u32 {
        self.width
    }
}

impl image::GenericImage for BinaryImage {
    #[inline]
    unsafe fn unsafe_put_pixel(&mut self, x: u32, y: u32, pixel: Self::Pixel) {
        self.data.set((y * self.width + x) as usize, *pixel);
    }
    #[inline]
    fn put_pixel(&mut self, x: u32, y: u32, pixel: Self::Pixel) {
        debug_assert!(self.in_bounds(x, y));
        unsafe { self.unsafe_put_pixel(x, y, pixel) }
    }
    fn blend_pixel(&mut self, _: u32, _: u32, _: Self::Pixel) {
        unimplemented!()
    }
    fn get_pixel_mut(&mut self, _: u32, _: u32) -> &mut Self::Pixel {
        unimplemented!()
    }
}

#[derive(Debug, Clone, DerefMut, Deref)]
pub struct BinaryView<'a, I>(pub &'a I)
where
    I: GenericImageView;

impl<'a, I> GenericImageView for BinaryView<'a, I>
where
    I: GenericImageView,
{
    type Pixel = Bit;
    #[inline]
    unsafe fn unsafe_get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
        let pixel = self.0.unsafe_get_pixel(x, y);
        let (mut channels, mut alphas, mut alpha_exist) = (false, false, false);
        pixel.map_with_alpha(
            |channel| {
                channels |= !channel.is_zero();
                channel
            },
            |alpha| {
                alpha_exist = true;
                alphas |= !alpha.is_zero();
                alpha
            },
        );
        pixel::Bit::from(channels ^ alpha_exist | alphas)
    }
    #[inline]
    fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
        debug_assert!(self.0.in_bounds(x, y));
        unsafe { self.unsafe_get_pixel(x, y) }
    }
    #[inline]
    fn dimensions(&self) -> (u32, u32) {
        self.0.dimensions()
    }
    #[inline]
    fn height(&self) -> u32 {
        self.0.height()
    }
    #[inline]
    fn width(&self) -> u32 {
        self.0.width()
    }
}

#[derive(Debug, Clone)]
pub struct BinaryRawView<'a, T>
where
    T: num_traits::Zero,
{
    data: &'a [T],
    height: u32,
    width: u32,
}

impl<'a, T> BinaryRawView<'a, T>
where
    T: num_traits::Zero,
{
    pub fn new(height: u32, width: u32, data: &'a [T]) -> Self {
        Self {
            data,
            height,
            width,
        }
    }
}

impl<'a, T> GenericImageView for BinaryRawView<'a, T>
where
    T: num_traits::Zero,
{
    type Pixel = pixel::Bit;
    #[inline]
    unsafe fn unsafe_get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
        let size = self.data.len() / (self.height * self.width) as usize;
        let start = (y * self.width + x) as usize;
        let end = start + size;
        pixel::Bit::from(
            self.data
                .get_unchecked(start..end)
                .iter()
                .any(|channel| !channel.is_zero()),
        )
    }
    #[inline]
    fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
        debug_assert!(self.in_bounds(x, y));
        unsafe { self.unsafe_get_pixel(x, y) }
    }
    #[inline]
    fn dimensions(&self) -> (u32, u32) {
        (self.width(), self.height())
    }
    #[inline]
    fn height(&self) -> u32 {
        self.height
    }
    #[inline]
    fn width(&self) -> u32 {
        self.width
    }
}