Skip to content

Commit

Permalink
Improve encode_gray performance in BmpEncoder (#2193)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattatz committed Apr 8, 2024
1 parent ba78cba commit 1c95b86
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/codecs/bmp/encoder.rs
Expand Up @@ -244,11 +244,18 @@ impl<'a, W: Write + 'a> BmpEncoder<'a, W> {
for row in (0..height).rev() {
// from the bottom up
let row_start = row * y_stride;
for col in 0..width {
let pixel_start = (row_start + (col * x_stride)) as usize;
// color value is equal to the palette index
self.writer.write_u8(image[pixel_start])?;
// alpha is never written as it's not widely supported

// color value is equal to the palette index
if x_stride == 1 {
// improve performance by writing the whole row at once
self.writer
.write_all(&image[row_start as usize..][..y_stride as usize])?;
} else {
for col in 0..width {
let pixel_start = (row_start + (col * x_stride)) as usize;
self.writer.write_u8(image[pixel_start])?;
// alpha is never written as it's not widely supported
}
}

self.write_row_pad(row_pad_size)?;
Expand Down

0 comments on commit 1c95b86

Please sign in to comment.