Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/codecs/ico/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,22 @@ impl<R: BufRead + Seek> ImageDecoder for IcoDecoder<R> {

decoder.read_image_data(buf)?;

// Detect 0RGB.
// 0RGB is a 32bpp RGB format where the fourth channel is set to 0 and ignored.
// This channel is NOT alpha/transparency, but our BMP decoder doesn't know that
// and decodes it as RGBA. So we detect this and manually set alpha to 255.
// See: https://devblogs.microsoft.com/oldnewthing/20101019-00/?p=12503
let rgba = buf.as_chunks_mut::<4>().0;
let is_0rgb = self.selected_entry.bits_per_pixel == 32
&& rgba.iter().all(|rgba| rgba[3] == 0);
if is_0rgb {
rgba.iter_mut().for_each(|rgba| rgba[3] = 255);
}

// The AND mask is applied when the image is <32bpp or in 0RGB format.
// See: https://devblogs.microsoft.com/oldnewthing/20101021-00/?p=12483
let should_apply_and_mask = is_0rgb || self.selected_entry.bits_per_pixel < 32;

let r = decoder.reader();
let image_end = r.stream_position()?;
let data_end = self.reader_offset
Expand All @@ -392,11 +408,7 @@ impl<R: BufRead + Seek> ImageDecoder for IcoDecoder<R> {
return Ok(DecodedImageAttributes::default());
}

// 32bpp BMPs already have a native alpha channel, so the
// AND mask is ignored.
// For lower bit depths, read and apply the AND mask.
if self.selected_entry.bits_per_pixel < 32 {
let rgba = buf.as_chunks_mut::<4>().0;
if should_apply_and_mask {
let rows = rgba.chunks_exact_mut(width as usize);

if rows.len() != height as usize {
Expand Down Expand Up @@ -431,8 +443,7 @@ impl<R: BufRead + Seek> ImageDecoder for IcoDecoder<R> {
} else if data_end == image_end {
// accept images with no mask data
Ok(DecodedImageAttributes::default())
} else if self.spec_strictness == SpecCompliance::Lenient
&& self.selected_entry.bits_per_pixel >= 32
} else if self.spec_strictness == SpecCompliance::Lenient && !should_apply_and_mask
{
// In lenient mode, we accept truncated mask data for 32bpp images
// since they already have an alpha channel and we ignore the AND mask anyway.
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading