Skip to content

Commit 5e71cb5

Browse files
committed
rudimentary support for BMPs with alpha masks
"rudimentary" in the sense that the format will be allowed and parsed, but the alpha channel is thrown away when drawing and getting pixels.
1 parent 2477185 commit 5e71cb5

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

src/header/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ impl ChannelMasks {
201201
blue: 0x0000FF,
202202
alpha: 0,
203203
};
204+
205+
/// Argb888 color masks.
206+
pub const ARGB888: Self = Self {
207+
red: 0xFF000000,
208+
green: 0xFF0000,
209+
blue: 0xFF00,
210+
alpha: 0xFF,
211+
};
204212
}
205213

206214
/// Describes how the BMP file is compressed.

src/iter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ where
5656
ColorType::Rgb555 => Rgb555::from(RawU16::from_u32(color)).into(),
5757
ColorType::Rgb565 => Rgb565::from(RawU16::from_u32(color)).into(),
5858
ColorType::Rgb888 | ColorType::Xrgb8888 => Rgb888::from(RawU24::from_u32(color)).into(),
59+
ColorType::Argb8888 => Rgb888::from(RawU24::from_u32(color >> 8)).into(),
5960
};
6061

6162
Some(Pixel(position, color))

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,11 @@ where
362362
RawColors::<RawU32>::new(&self.raw_bmp)
363363
.map(|raw| Rgb888::from(RawU24::new(raw.into_inner())).into()),
364364
),
365+
ColorType::Argb8888 => target.fill_contiguous(
366+
&area,
367+
RawColors::<RawU32>::new(&self.raw_bmp)
368+
.map(|raw| Rgb888::from(RawU24::new(raw.into_inner() >> 8)).into()),
369+
),
365370
}
366371
}
367372

@@ -421,6 +426,10 @@ where
421426
.raw_bmp
422427
.pixel(p)
423428
.map(|raw| Rgb888::from(RawU24::from_u32(raw)).into()),
429+
ColorType::Argb8888 => self
430+
.raw_bmp
431+
.pixel(p)
432+
.map(|raw| Rgb888::from(RawU24::from_u32(raw >> 8)).into()),
424433
}
425434
}
426435
}

src/raw_bmp.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ pub enum ColorType {
157157
Rgb565,
158158
Rgb888,
159159
Xrgb8888,
160+
Argb8888,
160161
}
161162

162163
impl ColorType {
@@ -184,6 +185,8 @@ impl ColorType {
184185
if let Some(masks) = header.channel_masks {
185186
if masks == ChannelMasks::RGB888 {
186187
ColorType::Xrgb8888
188+
} else if masks == ChannelMasks::ARGB888 {
189+
ColorType::Argb8888
187190
} else {
188191
return Err(ParseError::UnsupportedChannelMasks);
189192
}

0 commit comments

Comments
 (0)