99
1010A 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
2423use 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
4039To 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
4544use 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
6384For 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
6687by 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
7094use embedded_graphics :: prelude :: * ;
7195use tinybmp :: {RawBmp , Bpp , Header , RawPixel , RowOrder };
@@ -92,17 +116,25 @@ let pixels: Vec<RawPixel> = bmp.pixels().collect();
92116
93117// Loaded example image is 8x8px
94118assert_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
0 commit comments