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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! Small BMP format image parser supporting no-std environments. Specifically designed to work with
//! [embedded-graphics]
//!
//! # Examples
//!
//! ## Load a BMP image and check its [`Header`] and returned pixels.
//!
//! ```rust
//! use tinybmp::{Bmp, FileType, Header, Pixel};
//!
//! let bmp = Bmp::from_slice(include_bytes!("../tests/chessboard-8px-24bit.bmp"))
//!     .expect("Failed to parse BMP image");
//!
//! // Read the BMP header
//! assert_eq!(
//!     bmp.header,
//!     Header {
//!         file_type: FileType::BM,
//!         file_size: 314,
//!         reserved_1: 0,
//!         reserved_2: 0,
//!         image_data_start: 122,
//!         bpp: 24,
//!         image_width: 8,
//!         image_height: 8,
//!         image_data_len: 192
//!     }
//! );
//!
//! // Check that raw image data slice is the correct length (according to parsed header)
//! assert_eq!(bmp.image_data().len(), bmp.header.image_data_len as usize);
//!
//! // Get an iterator over the pixel coordinates and values in this image and load into a vec
//! let pixels: Vec<Pixel> = bmp.into_iter().collect();
//!
//! // Loaded example image is 8x8px
//! assert_eq!(pixels.len(), 8 * 8);
//! ```
//!
//! ## Integrate with `embedded-graphics`
//!
//! This example loads a 16BPP image and draws it to an [embedded-graphics] compatible display.
//!
//! The `graphics` feature must be enabled for embedded-graphics support.
//!
//! ```rust
//! # #[cfg(feature = "graphics")] { fn main() -> Result<(), core::convert::Infallible> {
//! use embedded_graphics::{image::Image, prelude::*};
//! use tinybmp::Bmp;
//! # use embedded_graphics::mock_display::MockDisplay;
//! # use embedded_graphics::pixelcolor::Rgb565;
//! # let mut display: MockDisplay<Rgb565> = MockDisplay::default();
//!
//! // Load 16BPP 8x8px image
//! let bmp = Bmp::from_slice(include_bytes!("../tests/chessboard-8px-color-16bit.bmp")).unwrap();
//!
//! let image = Image::new(&bmp, Point::zero());
//!
//! image.draw(&mut display)?;
//! # Ok::<(), core::convert::Infallible>(())
//! # } }
//! ```
//!
//! [embedded-graphics]: https://crates.io/crates/embedded-graphics
//! [`Header`]: ./header/struct.Header.html

#![no_std]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]

mod check_readme;
mod header;
mod pixel;

use crate::header::parse_header;
pub use crate::{
    header::{FileType, Header},
    pixel::Pixel,
};

/// A BMP-format bitmap
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Bmp<'a> {
    /// Image header
    pub header: Header,

    image_data: &'a [u8],
}

impl<'a> Bmp<'a> {
    /// Create a bitmap object from a byte array
    ///
    /// This method keeps a slice of the original input and does not dynamically allocate memory.
    /// The input data must live for as long as this BMP instance does.
    pub fn from_slice(bytes: &'a [u8]) -> Result<Self, ()> {
        let (_remaining, header) = parse_header(bytes).map_err(|_| ())?;

        let image_data = &bytes[header.image_data_start..];

        Ok(Bmp { header, image_data })
    }

    /// Get a reference to the range of bytes that represents the pixel data in the image
    pub fn image_data(&'a self) -> &'a [u8] {
        self.image_data
    }

    /// Get the image width in pixels
    pub fn width(&self) -> u32 {
        self.header.image_width
    }

    /// Get the image height in pixels
    pub fn height(&self) -> u32 {
        self.header.image_height
    }

    /// Get image dimensions as `(width, height)` in pixels
    pub fn dimensions(&self) -> (u32, u32) {
        (self.header.image_width, self.header.image_height)
    }

    /// Get the BPP (bits per pixel) for this image
    pub fn bpp(&self) -> u32 {
        u32::from(self.header.bpp)
    }

    /// Returns the row length in bytes.
    ///
    /// Each row in a BMP file is a multiple of 4 bytes long.
    fn bytes_per_row(&self) -> usize {
        let bits_per_row = self.width() as usize * self.bpp() as usize;

        (bits_per_row + 31) / 32 * (32 / 8)
    }
}

impl<'a> IntoIterator for &'a Bmp<'a> {
    type Item = Pixel;
    type IntoIter = BmpIterator<'a>;

    fn into_iter(self) -> Self::IntoIter {
        let pixel_stride = match self.bpp() {
            1 | 8 | 16 | 24 | 32 => self.bpp() as usize,
            depth => panic!("Bit depth {} not supported", depth),
        };

        BmpIterator {
            bmp: self,
            pixel_data: self.image_data(),
            pixel_stride,
            x: 0,
            y: 0,
            bit_idx: 0,
        }
    }
}

/// Iterator over individual BMP pixels
///
/// Each pixel is returned as a `u32` regardless of the bit depth of the source image.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct BmpIterator<'a> {
    /// Reference to original BMP image
    bmp: &'a Bmp<'a>,

    /// Image pixel data as a byte slice, little endian ordering
    pixel_data: &'a [u8],

    /// Number of bits per pixel
    pixel_stride: usize,

    /// Current X position
    x: u32,

    /// Current Y position
    y: u32,

    /// Start bit index for the current pixel.
    ///
    /// This is incremented by `pixel_stride` bits every iteration.
    bit_idx: usize,
}

impl<'a> Iterator for BmpIterator<'a> {
    type Item = Pixel;

    fn next(&mut self) -> Option<Self::Item> {
        let px = self.pixel_data;

        if self.y < self.bmp.height() {
            let x = self.x;
            let y = self.y;

            if self.x == 0 {
                let row_index = (self.bmp.height() - 1) - self.y;
                let row_start = self.bmp.bytes_per_row() * row_index as usize;

                self.bit_idx = row_start * 8;
            }

            self.x += 1;
            if self.x >= self.bmp.width() {
                self.y += 1;
                self.x = 0;
            }

            let byte_idx = self.bit_idx / 8;

            let pixel_value = match self.pixel_stride {
                1 => {
                    let mask = 0b_1000_0000 >> self.bit_idx % 8;
                    (px[byte_idx] & mask != 0) as u32
                }
                8 => u32::from(px[byte_idx]),
                16 => u32::from_le_bytes([px[byte_idx], px[byte_idx + 1], 0, 0]),
                24 => u32::from_le_bytes([px[byte_idx], px[byte_idx + 1], px[byte_idx + 2], 0]),
                32 => u32::from_le_bytes([
                    px[byte_idx],
                    px[byte_idx + 1],
                    px[byte_idx + 2],
                    px[byte_idx + 3],
                ]),
                _ => unreachable!(),
            };

            self.bit_idx += self.pixel_stride;

            Some(Pixel {
                x,
                y,
                color: pixel_value,
            })
        } else {
            None
        }
    }
}

#[cfg(feature = "graphics")]
mod e_g {
    use super::*;
    use core::marker::PhantomData;
    use embedded_graphics::{
        drawable::Pixel as EgPixel,
        geometry::Point,
        image::{ImageDimensions, IntoPixelIter},
        pixelcolor::{raw::RawData, PixelColor},
    };

    /// A thin wrapper over [`BmpIterator`] to support [`embedded-graphics`] integration
    ///
    /// [`BmpIterator`]: ./struct.BmpIterator.html
    /// [`embedded-graphics`]: https://docs.rs/embedded-graphics
    #[derive(Debug)]
    pub struct EgPixelIterator<'a, C> {
        it: BmpIterator<'a>,
        c: PhantomData<C>,
    }

    impl<'a, C> Iterator for EgPixelIterator<'a, C>
    where
        C: PixelColor + From<<C as PixelColor>::Raw>,
    {
        type Item = EgPixel<C>;

        fn next(&mut self) -> Option<Self::Item> {
            self.it.next().map(|p| {
                let raw = C::Raw::from_u32(p.color);
                EgPixel(Point::new(p.x as i32, p.y as i32), raw.into())
            })
        }
    }

    impl ImageDimensions for Bmp<'_> {
        fn width(&self) -> u32 {
            Bmp::width(&self)
        }

        fn height(&self) -> u32 {
            Bmp::height(&self)
        }
    }

    impl<'a, C> IntoPixelIter<C> for &'a Bmp<'_>
    where
        C: PixelColor + From<<C as PixelColor>::Raw>,
    {
        type PixelIterator = EgPixelIterator<'a, C>;

        fn pixel_iter(self) -> Self::PixelIterator {
            EgPixelIterator {
                it: self.into_iter(),
                c: PhantomData,
            }
        }
    }
}

#[cfg(feature = "graphics")]
pub use e_g::*;