Skip to content

Truly independent web browser

LibImageDecoders: Fix AND masks of RLE and bitfield BMPs in ICOs

Andreas Klingf7af50da9fae
diff --git a/Libraries/LibImageDecoders/Rust/src/bmp.rs b/Libraries/LibImageDecoders/Rust/src/bmp.rs
index 8cef89ba393..cc524c9f0b6 100644
--- a/Libraries/LibImageDecoders/Rust/src/bmp.rs
+++ b/Libraries/LibImageDecoders/Rust/src/bmp.rs
@@ -599,9 +599,11 @@ impl BMPDecoder {
}
}
- // Returns the decoded pixel data, and which of its pixels are set. The other pixels were skipped and are transparent.
- fn decode_rle_data(&self, data: &[u8], bits_per_pixel: u16) -> Option<(Vec<u8>, Vec<bool>)> {
- let mut reader = Reader::new(data.get(self.data_offset..)?);
+ // Returns the decoded pixel data, which of its pixels are set, and the offset in `data` where the RLE data ends.
+ // The pixels that are not set were skipped and are transparent.
+ fn decode_rle_data(&self, data: &[u8], bits_per_pixel: u16) -> Option<(Vec<u8>, Vec<bool>, usize)> {
+ let rle_data = data.get(self.data_offset..)?;
+ let mut reader = Reader::new(rle_data);
let compression = self.compression;
let width = self.width;
let total_rows = self.height;
@@ -729,7 +731,8 @@ impl BMPDecoder {
}
}
- Some((buffer, pixel_is_set))
+ let end_offset = self.data_offset + (rle_data.len() - reader.remaining());
+ Some((buffer, pixel_is_set, end_offset))
}
fn masked_color(&self, data: u32) -> u32 {
@@ -747,14 +750,21 @@ impl BMPDecoder {
let color_table = self.decode_color_table(data)?;
let rle_data;
- let (pixel_data, bits_per_pixel, rle_pixel_is_set) = match self.rle_bits_per_pixel() {
+ // NB: The AND mask of an RLE image follows its compressed data, not the decoded pixel data.
+ let (pixel_data, bits_per_pixel, rle_pixel_is_set, rle_end_offset) = match self.rle_bits_per_pixel() {
Some(bits_per_pixel) => {
rle_data = self.decode_rle_data(data, bits_per_pixel)?;
- (rle_data.0.as_slice(), bits_per_pixel, rle_data.1.as_slice())
+ (
+ rle_data.0.as_slice(),
+ bits_per_pixel,
+ rle_data.1.as_slice(),
+ Some(rle_data.2),
+ )
}
- None => (data.get(self.data_offset..)?, self.bpp, &[][..]),
+ None => (data.get(self.data_offset..)?, self.bpp, &[][..], None),
};
let mut reader = Reader::new(pixel_data);
+ let has_alpha_mask = self.masks.len() == 4;
let mut has_alpha_values = false;
let width = self.width;
@@ -808,8 +818,9 @@ impl BMPDecoder {
column += 1;
}
16 => {
- let value = reader.read_u16()?;
- set_pixel(column, self.masked_color(u32::from(value)));
+ let color = self.masked_color(u32::from(reader.read_u16()?));
+ has_alpha_values |= has_alpha_mask && color >> 24 != 0;
+ set_pixel(column, color);
column += 1;
}
24 => {
@@ -836,7 +847,9 @@ impl BMPDecoder {
// INTEROP: Without an alpha mask, the fourth byte of a standalone BMP is not alpha.
set_pixel(column, value | 0xff00_0000);
} else {
- set_pixel(column, self.masked_color(value));
+ let color = self.masked_color(value);
+ has_alpha_values |= has_alpha_mask && color >> 24 != 0;
+ set_pixel(column, color);
}
column += 1;
}
@@ -870,19 +883,29 @@ impl BMPDecoder {
}
// An ICO image is followed by an AND mask with one bit per pixel, which makes pixels transparent.
- if self.included_in_ico && !(may_have_alpha_values && has_alpha_values) && !reader.is_at_end() {
+ // INTEROP: An image with alpha values ignores its AND mask.
+ let mut mask_reader = match rle_end_offset {
+ Some(offset) => Reader::new(data.get(offset..)?),
+ None => reader,
+ };
+ if self.included_in_ico && !has_alpha_values && !mask_reader.is_at_end() {
for row in 0..height {
let row_start = output_row(row);
let pixels = &mut output[row_start..row_start + row_bytes];
- let remaining_before_row = reader.remaining();
+ let remaining_before_row = mask_reader.remaining();
let mut column = 0;
while column < width {
- let byte = reader.read_u8()?;
+ let byte = mask_reader.read_u8()?;
for bit in (0..8).rev() {
if column >= width {
break;
}
+ // Pixels skipped by RLE data stay transparent.
+ if !rle_pixel_is_set.is_empty() && !rle_pixel_is_set[row * width + column] {
+ column += 1;
+ continue;
+ }
let pixel_bytes = &mut pixels[column * 4..column * 4 + 4];
let mut pixel = u32::from_ne_bytes(pixel_bytes.try_into().ok()?);
if (byte >> bit) & 1 != 0 {
@@ -895,9 +918,9 @@ impl BMPDecoder {
}
}
- let consumed = remaining_before_row - reader.remaining();
+ let consumed = remaining_before_row - mask_reader.remaining();
- skip_row_padding(&mut reader, consumed)?;
+ skip_row_padding(&mut mask_reader, consumed)?;
}
}
diff --git a/Tests/LibGfx/TestImageDecoder.cpp b/Tests/LibGfx/TestImageDecoder.cpp
index ec05f80a797..c22c798fe94 100644
--- a/Tests/LibGfx/TestImageDecoder.cpp
+++ b/Tests/LibGfx/TestImageDecoder.cpp
@@ -672,6 +672,44 @@ TEST_CASE(test_32bit_bmp_with_alpha_values_in_ico_ignores_and_mask)
EXPECT_EQ(frame.image->get_pixel(1, 0), Gfx::Color(0x44, 0x55, 0x66));
}
+TEST_CASE(test_bitfields_bmp_with_alpha_values_in_ico_ignores_and_mask)
+{
+ // 1x1 image with an alpha mask and an alpha value, followed by an AND mask that would make the pixel transparent.
+ Vector<u8> dib;
+ append_bitmap_info_header(dib, 56, 1, 2, 32, 3 /* BI_BITFIELDS */, 0);
+ append_u32(dib, 0x00ff0000);
+ append_u32(dib, 0x0000ff00);
+ append_u32(dib, 0x000000ff);
+ append_u32(dib, 0xff000000);
+ Array<u8, 8> pixel_and_mask { 0x33, 0x22, 0x11, 0x80, 0b1000'0000, 0, 0, 0 };
+ dib.append(pixel_and_mask.data(), pixel_and_mask.size());
+ auto ico_data = make_ico_file(1, 1, 32, dib);
+
+ auto plugin_decoder = TRY_OR_FAIL(Gfx::ICOImageDecoderPlugin::create(ico_data));
+ auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1, 1 }));
+ EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0x11, 0x22, 0x33, 0x80));
+}
+
+TEST_CASE(test_rle8_bmp_in_ico_uses_and_mask)
+{
+ // 8x1 image whose RLE data sets the first four pixels and skips the rest, followed by an AND mask that makes the
+ // first pixel transparent.
+ Vector<u8> dib;
+ append_bitmap_info_header(dib, 40, 8, 2, 8, 1 /* BI_RLE8 */, 1);
+ append_u32(dib, 0xff0000); // Red.
+ Array<u8, 8> rle_data_and_mask { 4, 0, 0, 1, 0b1000'0000, 0, 0, 0 };
+ dib.append(rle_data_and_mask.data(), rle_data_and_mask.size());
+ auto ico_data = make_ico_file(8, 1, 8, dib);
+
+ auto plugin_decoder = TRY_OR_FAIL(Gfx::ICOImageDecoderPlugin::create(ico_data));
+ auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 8, 1 }));
+ EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 0);
+ EXPECT_EQ(frame.image->get_pixel(1, 0), Gfx::Color::NamedColor::Red);
+ EXPECT_EQ(frame.image->get_pixel(3, 0), Gfx::Color::NamedColor::Red);
+ EXPECT_EQ(frame.image->get_pixel(4, 0).alpha(), 0);
+ EXPECT_EQ(frame.image->get_pixel(7, 0).alpha(), 0);
+}
+
TEST_CASE(test_malformed_maskless_ico)
{
auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("ico/malformed_maskless.ico"sv)));