Skip to content

Commit 016e3ad

Browse files
committed
fix: review comments
1 parent 203b56e commit 016e3ad

3 files changed

Lines changed: 74 additions & 69 deletions

File tree

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ where
293293
let fallback_color = C::from(Rgb888::BLACK);
294294
if let Some(color_table) = self.raw_bmp.color_table() {
295295
if header.compression_method == CompressionMethod::Rle4 {
296-
let (mut colors, _points) = Rle4Colors::new(&self.raw_bmp);
296+
let mut colors = Rle4Colors::new(&self.raw_bmp);
297297
let map_color = |color: RawU4| {
298298
color_table
299299
.get(color.into_inner() as u32)
@@ -302,7 +302,7 @@ where
302302
};
303303
// RLE produces pixels in bottom-up order, so we draw them line by line rather than the entire bitmap at once.
304304
for y in (0..area.size.height).rev() {
305-
colors.start_a_line();
305+
colors.start_row();
306306

307307
let row = Rectangle::new(Point::new(0, y as i32), slice_size);
308308
let colors = colors.by_ref().map(map_color);
@@ -328,7 +328,7 @@ where
328328
let fallback_color = C::from(Rgb888::BLACK);
329329
if let Some(color_table) = self.raw_bmp.color_table() {
330330
if header.compression_method == CompressionMethod::Rle8 {
331-
let (mut colors, _points) = Rle8Colors::new(&self.raw_bmp);
331+
let mut colors = Rle8Colors::new(&self.raw_bmp);
332332
let map_color = |color: RawU8| {
333333
color_table
334334
.get(color.into_inner() as u32)
@@ -337,7 +337,7 @@ where
337337
};
338338
// RLE produces pixels in bottom-up order, so we draw them line by line rather than the entire bitmap at once.
339339
for y in (0..area.size.height).rev() {
340-
colors.start_a_line();
340+
colors.start_row();
341341

342342
let row = Rectangle::new(Point::new(0, y as i32), slice_size);
343343
let colors = colors.by_ref().map(map_color);

src/raw_bmp.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ impl<'a> RawBmp<'a> {
103103
RawPixels::new(self)
104104
}
105105

106-
/// Return an iterator over the raw colors in the image. Positions are dependent on the row order.
106+
/// Returns an iterator over the raw colors in the image.
107+
///
108+
/// The iterator returns the color value in the order the pixels are stored in the file.
109+
/// Use [`row_order`](DynamicRawColors::row_order) to determine the correct
110+
/// pixel arrangement.
107111
pub fn colors(&self) -> DynamicRawColors<'_> {
108112
self.pixels().colors
109113
}

src/raw_iter.rs

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ where
2828
RawDataSlice<'a, R, LittleEndian>: IntoIterator<Item = R>,
2929
{
3030
/// Create a new raw color iterator.
31-
pub fn new(raw_bmp: &'a RawBmp<'a>) -> Self {
31+
pub(crate) fn new(raw_bmp: &'a RawBmp<'a>) -> Self {
3232
let header = raw_bmp.header();
3333

3434
let width = header.image_size.width as usize;
@@ -62,24 +62,25 @@ where
6262
}
6363
}
6464

65-
/// Iterator over dynamic color. Positions are dependent on the row order.
66-
#[allow(missing_debug_implementations)]
65+
/// Iterator over the raw colors in the image.
66+
///
67+
/// See [`RawBmp::colors`](RawBmp::colors) for more information.
6768
pub enum DynamicRawColors<'a> {
6869
/// 1 bit per pixel
6970
Bpp1(RawColors<'a, RawU1>),
70-
/// 4 bit per pixel
71+
/// 4 bits per pixel
7172
Bpp4(RawColors<'a, RawU4>),
72-
/// 8 bit per pixel
73+
/// 8 bits per pixel
7374
Bpp8(RawColors<'a, RawU8>),
74-
/// 16 bit per pixel
75+
/// 16 bits per pixel
7576
Bpp16(RawColors<'a, RawU16>),
76-
/// 24 bit per pixel
77+
/// 24 bits per pixel
7778
Bpp24(RawColors<'a, RawU24>),
78-
/// 32 bit per pixel
79+
/// 32 bits per pixel
7980
Bpp32(RawColors<'a, RawU32>),
80-
/// 4 bit per pixel RLE encoded
81+
/// RLE encoded with 4 bits per pixel
8182
Bpp4Rle(Rle4Colors<'a>),
82-
/// 8 bit per pixel RLE encoded
83+
/// RLE encoded with 8 bits per pixel
8384
Bpp8Rle(Rle8Colors<'a>),
8485
}
8586

@@ -102,13 +103,14 @@ impl DynamicRawColors<'_> {
102103
/// Get the row order of the bitmap.
103104
pub fn row_order(&self) -> RowOrder {
104105
match self {
105-
DynamicRawColors::Bpp1(_)
106-
| DynamicRawColors::Bpp4(_)
107-
| DynamicRawColors::Bpp8(_)
108-
| DynamicRawColors::Bpp16(_)
109-
| DynamicRawColors::Bpp24(_)
110-
| DynamicRawColors::Bpp32(_) => RowOrder::TopDown,
111-
DynamicRawColors::Bpp4Rle(_) | DynamicRawColors::Bpp8Rle(_) => RowOrder::BottomUp,
106+
DynamicRawColors::Bpp1(colors) => colors.row_order,
107+
DynamicRawColors::Bpp4(colors) => colors.row_order,
108+
DynamicRawColors::Bpp8(colors) => colors.row_order,
109+
DynamicRawColors::Bpp16(colors) => colors.row_order,
110+
DynamicRawColors::Bpp24(colors) => colors.row_order,
111+
DynamicRawColors::Bpp32(colors) => colors.row_order,
112+
DynamicRawColors::Bpp4Rle(_) => RowOrder::BottomUp,
113+
DynamicRawColors::Bpp8Rle(_) => RowOrder::BottomUp,
112114
}
113115
}
114116
}
@@ -124,8 +126,8 @@ impl Iterator for DynamicRawColors<'_> {
124126
DynamicRawColors::Bpp16(colors) => colors.next().map(|r| u32::from(r.into_inner())),
125127
DynamicRawColors::Bpp24(colors) => colors.next().map(|r| r.into_inner()),
126128
DynamicRawColors::Bpp32(colors) => colors.next().map(|r| r.into_inner()),
127-
DynamicRawColors::Bpp4Rle(state) => state.next().map(|r| u32::from(r.into_inner())),
128-
DynamicRawColors::Bpp8Rle(state) => state.next().map(|r| u32::from(r.into_inner())),
129+
DynamicRawColors::Bpp4Rle(colors) => colors.next().map(|r| u32::from(r.into_inner())),
130+
DynamicRawColors::Bpp8Rle(colors) => colors.next().map(|r| u32::from(r.into_inner())),
129131
}
130132
}
131133
}
@@ -152,25 +154,24 @@ enum RleState {
152154
}
153155

154156
pub struct PixelPoints {
155-
/// The location of the next pixel
157+
/// The location of the next pixel.
156158
next_pixel: Point,
157-
/// The width of a line in pixels
159+
/// The number of pixels in a row.
158160
width: u32,
159161
}
160162

161-
/// Iterator over individual BMP RLE8 encoded pixels.
162-
///
163-
/// Each pixel is returned as a `u32` regardless of the bit depth of the source image.
164-
#[derive(Debug)]
165-
pub struct Rle8Colors<'a> {
166-
/// Our source data
167-
data: &'a [u8],
168-
/// Our state
169-
rle_state: RleState,
170-
start_of_line: bool,
171-
}
172-
173163
impl PixelPoints {
164+
pub(crate) fn new(image_size: Size, row_order: RowOrder) -> Self {
165+
let next_pixel = match row_order {
166+
RowOrder::TopDown => Point::new(0, 0),
167+
RowOrder::BottomUp => Point::new(0, (image_size.height - 1) as i32),
168+
};
169+
Self {
170+
next_pixel,
171+
width: image_size.width,
172+
}
173+
}
174+
174175
fn next_up_dir(&mut self) -> Point {
175176
let old_position = self.next_pixel;
176177
self.next_pixel.x += 1;
@@ -192,26 +193,31 @@ impl PixelPoints {
192193
}
193194
}
194195

196+
/// Iterator over individual BMP RLE8 encoded pixels.
197+
///
198+
/// Each pixel is returned as a `u32` regardless of the bit depth of the source image.
199+
#[derive(Debug)]
200+
pub struct Rle8Colors<'a> {
201+
/// Our source data
202+
data: &'a [u8],
203+
/// Our state
204+
rle_state: RleState,
205+
start_of_row: bool,
206+
}
207+
195208
impl<'a> Rle8Colors<'a> {
196209
/// Create a new RLE pixel iterator.
197-
pub(crate) fn new(raw_bmp: &RawBmp<'a>) -> (Rle8Colors<'a>, PixelPoints) {
198-
let header = raw_bmp.header();
199-
let points = PixelPoints {
200-
width: header.image_size.width,
201-
// RLE encoded bitmaps are upside down
202-
next_pixel: Point::new(0, (header.image_size.height - 1) as i32),
203-
};
204-
let this = Rle8Colors {
210+
pub(crate) fn new(raw_bmp: &RawBmp<'a>) -> Rle8Colors<'a> {
211+
Rle8Colors {
205212
data: raw_bmp.image_data(),
206213
rle_state: RleState::Starting,
207-
start_of_line: false,
208-
};
209-
(this, points)
214+
start_of_row: false,
215+
}
210216
}
211217

212218
/// Indicate that a new line is starting. Required for correct RLE decoding.
213-
pub fn start_a_line(&mut self) {
214-
self.start_of_line = true;
219+
pub fn start_row(&mut self) {
220+
self.start_of_row = true;
215221
}
216222
}
217223

@@ -276,7 +282,7 @@ impl<'a> Iterator for Rle8Colors<'a> {
276282
// the pair, which can be one of the following values.
277283
match param {
278284
0 => {
279-
if !self.start_of_line {
285+
if !self.start_of_row {
280286
return None;
281287
}
282288
}
@@ -323,29 +329,22 @@ pub struct Rle4Colors<'a> {
323329
data: &'a [u8],
324330
/// Our state
325331
rle_state: RleState,
326-
start_of_line: bool,
332+
start_of_row: bool,
327333
}
328334

329335
impl<'a> Rle4Colors<'a> {
330336
/// Create a new RLE pixel iterator.
331-
pub(crate) fn new(raw_bmp: &RawBmp<'a>) -> (Rle4Colors<'a>, PixelPoints) {
332-
let header = raw_bmp.header();
333-
let points = PixelPoints {
334-
width: header.image_size.width,
335-
// RLE encoded bitmaps are upside down
336-
next_pixel: Point::new(0, (header.image_size.height - 1) as i32),
337-
};
338-
let this = Rle4Colors {
337+
pub(crate) fn new(raw_bmp: &RawBmp<'a>) -> Rle4Colors<'a> {
338+
Rle4Colors {
339339
data: raw_bmp.image_data(),
340340
rle_state: RleState::Starting,
341-
start_of_line: false,
342-
};
343-
(this, points)
341+
start_of_row: false,
342+
}
344343
}
345344

346345
/// Indicate that a new line is starting. Required for correct RLE decoding.
347-
pub fn start_a_line(&mut self) {
348-
self.start_of_line = true;
346+
pub fn start_row(&mut self) {
347+
self.start_of_row = true;
349348
}
350349
}
351350

@@ -449,7 +448,7 @@ impl<'a> Iterator for Rle4Colors<'a> {
449448
// the pair, which can be one of the following values.
450449
match param {
451450
0 => {
452-
if !self.start_of_line {
451+
if !self.start_of_row {
453452
return None;
454453
}
455454
}
@@ -502,14 +501,16 @@ impl<'a> RawPixels<'a> {
502501
let header = raw_bmp.header();
503502
match header.compression_method {
504503
CompressionMethod::Rle4 => {
505-
let (colors, points) = Rle4Colors::new(raw_bmp);
504+
let colors = Rle4Colors::new(raw_bmp);
505+
let points = PixelPoints::new(header.image_size, RowOrder::BottomUp);
506506
Self {
507507
colors: DynamicRawColors::Bpp4Rle(colors),
508508
points,
509509
}
510510
}
511511
CompressionMethod::Rle8 => {
512-
let (colors, points) = Rle8Colors::new(raw_bmp);
512+
let colors = Rle8Colors::new(raw_bmp);
513+
let points = PixelPoints::new(header.image_size, RowOrder::BottomUp);
513514
Self {
514515
colors: DynamicRawColors::Bpp8Rle(colors),
515516
points,

0 commit comments

Comments
 (0)