Skip to main content

pdfrum_page/image/
bitimage.rs

1//! A packed one-bit image: what JBIG2 and a stencil mask decode to.
2
3/// A one-bit-per-pixel image, packed MSB-first with each row starting on a
4/// byte boundary.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct BitImage {
7    /// Width in pixels.
8    pub width: u32,
9    /// Height in pixels.
10    pub height: u32,
11    /// Bytes per row, `width.div_ceil(8)`.
12    pub row_bytes: usize,
13    /// The packed bits. A set bit is **black**, matching the JBIG2 convention
14    /// and PDF's default `/Decode` for a one-bit image.
15    pub bits: Vec<u8>,
16}
17
18impl BitImage {
19    /// Whether the pixel at `(x, y)` is black.
20    #[must_use]
21    pub fn pixel(&self, x: u32, y: u32) -> bool {
22        let Ok(row) = usize::try_from(y) else {
23            return false;
24        };
25        let Ok(col) = usize::try_from(x) else {
26            return false;
27        };
28        let Some(byte) = self.bits.get(row * self.row_bytes + col / 8) else {
29            return false;
30        };
31        (byte >> (7 - (col % 8))) & 1 == 1
32    }
33}