Skip to content

Commit 1329673

Browse files
jamwafflesrfuest
andauthored
Add pixel getter examples (#37)
* Add pixel getter examples * Apply suggestions from code review Co-authored-by: Ralf Fuest <mail@rfuest.de>
1 parent c64e35a commit 1329673

2 files changed

Lines changed: 84 additions & 18 deletions

File tree

README.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99

1010
A small BMP parser primarily for embedded, no-std environments but usable anywhere.
1111

12-
This crate is primarily targeted at drawing BMP images to [`embedded_graphics`]
13-
[`DrawTarget`]s, but can also be used to parse BMP
14-
files for other applications.
12+
This crate is primarily targeted at drawing BMP images to [`embedded_graphics`] [`DrawTarget`]s,
13+
but can also be used to parse BMP files for other applications.
1514

1615
## Examples
1716

1817
### Draw a BMP image to an embedded-graphics draw target
1918

20-
The [`Bmp`] struct is used together with [`embedded_graphics`]' [`Image`] struct to display
21-
BMP files on any draw target.
19+
The [`Bmp`] struct is used together with [`embedded_graphics`]' [`Image`] struct to display BMP
20+
files on any draw target.
2221

2322
```rust
2423
use embedded_graphics::{image::Image, prelude::*};
@@ -38,8 +37,8 @@ Image::new(&bmp, Point::new(10, 20)).draw(&mut display)?;
3837
### Using the pixel iterator
3938

4039
To access the image data for other applications the [`Bmp::pixels`] method returns an iterator
41-
over all pixels in the BMP file. The colors inside the BMP file will automatically converted
42-
to one of the [color types] in [`embedded_graphics`].
40+
over all pixels in the BMP file. The colors inside the BMP file will automatically converted to
41+
one of the [color types] in [`embedded_graphics`].
4342

4443
```rust
4544
use embedded_graphics::{pixelcolor::Rgb888, prelude::*};
@@ -58,6 +57,28 @@ for Pixel(position, color) in bmp.pixels() {
5857
}
5958
```
6059

60+
### Accessing individual pixels
61+
62+
[`Bmp::pixel`] can be used to get the color of individual pixels. The returned color will be automatically
63+
converted to one of the [color types] in [`embedded_graphics`].
64+
65+
```rust
66+
use embedded_graphics::{pixelcolor::Rgb888, prelude::*};
67+
use tinybmp::Bmp;
68+
69+
// Include the BMP file data.
70+
let bmp_data = include_bytes!("../tests/chessboard-8px-24bit.bmp");
71+
72+
// Parse the BMP file.
73+
// Note that it is necessary to explicitly specify the color type which the colors in the BMP
74+
// file will be converted into.
75+
let bmp = Bmp::<Rgb888>::from_slice(bmp_data).unwrap();
76+
77+
let pixel = bmp.pixel(Point::new(3, 2));
78+
79+
assert_eq!(pixel, Some(Rgb888::WHITE));
80+
```
81+
6182
### Accessing the raw image data
6283

6384
For most applications the higher level access provided by [`Bmp`] is sufficient. But in case
@@ -66,6 +87,9 @@ information] and the [color table]. A [`RawBmp`] object can be created directly
6687
by using [`from_slice`] or by accessing the underlying raw object of a [`Bmp`] object with
6788
[`Bmp::as_raw`].
6889

90+
Similar to [`Bmp::pixel`], [`RawBmp::pixel`] can be used to get raw pixel color values as a
91+
`u32`.
92+
6993
```rust
7094
use embedded_graphics::prelude::*;
7195
use tinybmp::{RawBmp, Bpp, Header, RawPixel, RowOrder};
@@ -92,17 +116,25 @@ let pixels: Vec<RawPixel> = bmp.pixels().collect();
92116

93117
// Loaded example image is 8x8px
94118
assert_eq!(pixels.len(), 8 * 8);
119+
120+
// Individual raw pixel values can also be read
121+
let pixel = bmp.pixel(Point::new(3, 2));
122+
123+
// The raw value for a white pixel in the source image
124+
assert_eq!(pixel, Some(0xFFFFFFu32));
95125
```
96126

97127
## Minimum supported Rust version
98128

99-
The minimum supported Rust version for tinybmp is `1.61` or greater.
100-
Ensure you have the correct version of Rust installed, preferably through <https://rustup.rs>.
129+
The minimum supported Rust version for tinybmp is `1.61` or greater. Ensure you have the correct
130+
version of Rust installed, preferably through <https://rustup.rs>.
101131

102132
[`Bmp`]: https://docs.rs/tinybmp/latest/tinybmp/struct.Bmp.html
103133
[`Bmp::pixels`]: https://docs.rs/tinybmp/latest/tinybmp/struct.Bmp.html#method.pixels
134+
[`Bmp::pixel`]: https://docs.rs/tinybmp/latest/tinybmp/struct.Bmp.html#method.pixel
104135
[`Bmp::as_raw`]: https://docs.rs/tinybmp/latest/tinybmp/struct.Bmp.html#method.as_raw
105136
[`RawBmp`]: https://docs.rs/tinybmp/latest/tinybmp/struct.RawBmp.html
137+
[`RawBmp::pixel`]: https://docs.rs/tinybmp/latest/tinybmp/struct.RawBmp.html#method.pixel
106138
[header information]: https://docs.rs/tinybmp/latest/tinybmp/struct.RawBmp.html#method.header
107139
[color table]: https://docs.rs/tinybmp/latest/tinybmp/struct.RawBmp.html#method.color_table
108140
[`from_slice`]: https://docs.rs/tinybmp/latest/tinybmp/struct.RawBmp.html#method.from_slice

src/lib.rs

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
//! A small BMP parser primarily for embedded, no-std environments but usable anywhere.
22
//!
3-
//! This crate is primarily targeted at drawing BMP images to [`embedded_graphics`]
4-
//! [`DrawTarget`]s, but can also be used to parse BMP
5-
//! files for other applications.
3+
//! This crate is primarily targeted at drawing BMP images to [`embedded_graphics`] [`DrawTarget`]s,
4+
//! but can also be used to parse BMP files for other applications.
65
//!
76
//! # Examples
87
//!
98
//! ## Draw a BMP image to an embedded-graphics draw target
109
//!
11-
//! The [`Bmp`] struct is used together with [`embedded_graphics`]' [`Image`] struct to display
12-
//! BMP files on any draw target.
10+
//! The [`Bmp`] struct is used together with [`embedded_graphics`]' [`Image`] struct to display BMP
11+
//! files on any draw target.
1312
//!
1413
//! ```
1514
//! # fn main() -> Result<(), core::convert::Infallible> {
@@ -34,8 +33,8 @@
3433
//! ## Using the pixel iterator
3534
//!
3635
//! To access the image data for other applications the [`Bmp::pixels`] method returns an iterator
37-
//! over all pixels in the BMP file. The colors inside the BMP file will automatically converted
38-
//! to one of the [color types] in [`embedded_graphics`].
36+
//! over all pixels in the BMP file. The colors inside the BMP file will automatically converted to
37+
//! one of the [color types] in [`embedded_graphics`].
3938
//!
4039
//! ```
4140
//! # fn main() -> Result<(), core::convert::Infallible> {
@@ -56,6 +55,30 @@
5655
//! # Ok::<(), core::convert::Infallible>(()) }
5756
//! ```
5857
//!
58+
//! ## Accessing individual pixels
59+
//!
60+
//! [`Bmp::pixel`] can be used to get the color of individual pixels. The returned color will be automatically
61+
//! converted to one of the [color types] in [`embedded_graphics`].
62+
//!
63+
//! ```
64+
//! # fn main() -> Result<(), core::convert::Infallible> {
65+
//! use embedded_graphics::{pixelcolor::Rgb888, prelude::*};
66+
//! use tinybmp::Bmp;
67+
//!
68+
//! // Include the BMP file data.
69+
//! let bmp_data = include_bytes!("../tests/chessboard-8px-24bit.bmp");
70+
//!
71+
//! // Parse the BMP file.
72+
//! // Note that it is necessary to explicitly specify the color type which the colors in the BMP
73+
//! // file will be converted into.
74+
//! let bmp = Bmp::<Rgb888>::from_slice(bmp_data).unwrap();
75+
//!
76+
//! let pixel = bmp.pixel(Point::new(3, 2));
77+
//!
78+
//! assert_eq!(pixel, Some(Rgb888::WHITE));
79+
//! # Ok::<(), core::convert::Infallible>(()) }
80+
//! ```
81+
//!
5982
//! ## Accessing the raw image data
6083
//!
6184
//! For most applications the higher level access provided by [`Bmp`] is sufficient. But in case
@@ -64,6 +87,9 @@
6487
//! by using [`from_slice`] or by accessing the underlying raw object of a [`Bmp`] object with
6588
//! [`Bmp::as_raw`].
6689
//!
90+
//! Similar to [`Bmp::pixel`], [`RawBmp::pixel`] can be used to get raw pixel color values as a
91+
//! `u32`.
92+
//!
6793
//! ```
6894
//! use embedded_graphics::prelude::*;
6995
//! use tinybmp::{RawBmp, Bpp, Header, RawPixel, RowOrder};
@@ -92,18 +118,26 @@
92118
//!
93119
//! // Loaded example image is 8x8px
94120
//! assert_eq!(pixels.len(), 8 * 8);
121+
//!
122+
//! // Individual raw pixel values can also be read
123+
//! let pixel = bmp.pixel(Point::new(3, 2));
124+
//!
125+
//! // The raw value for a white pixel in the source image
126+
//! assert_eq!(pixel, Some(0xFFFFFFu32));
95127
//! ```
96128
//!
97129
//! # Minimum supported Rust version
98130
//!
99-
//! The minimum supported Rust version for tinybmp is `1.61` or greater.
100-
//! Ensure you have the correct version of Rust installed, preferably through <https://rustup.rs>.
131+
//! The minimum supported Rust version for tinybmp is `1.61` or greater. Ensure you have the correct
132+
//! version of Rust installed, preferably through <https://rustup.rs>.
101133
//!
102134
//! <!-- README-LINKS
103135
//! [`Bmp`]: https://docs.rs/tinybmp/latest/tinybmp/struct.Bmp.html
104136
//! [`Bmp::pixels`]: https://docs.rs/tinybmp/latest/tinybmp/struct.Bmp.html#method.pixels
137+
//! [`Bmp::pixel`]: https://docs.rs/tinybmp/latest/tinybmp/struct.Bmp.html#method.pixel
105138
//! [`Bmp::as_raw`]: https://docs.rs/tinybmp/latest/tinybmp/struct.Bmp.html#method.as_raw
106139
//! [`RawBmp`]: https://docs.rs/tinybmp/latest/tinybmp/struct.RawBmp.html
140+
//! [`RawBmp::pixel`]: https://docs.rs/tinybmp/latest/tinybmp/struct.RawBmp.html#method.pixel
107141
//! [header information]: https://docs.rs/tinybmp/latest/tinybmp/struct.RawBmp.html#method.header
108142
//! [color table]: https://docs.rs/tinybmp/latest/tinybmp/struct.RawBmp.html#method.color_table
109143
//! [`from_slice`]: https://docs.rs/tinybmp/latest/tinybmp/struct.RawBmp.html#method.from_slice

0 commit comments

Comments
 (0)