forge
Explore
Log in

Don't you dare go Hollow

S13: BMP compression is an enum; the raw value is only kept for the unknown-compression message (same arms, same bits check order)

Browse files at this commit

ac6f8033cd2f841697cebc486a75a28375c46d41Ahmet Kaan GümüşParent: 3582d7f
crates/ec-image/src/bmp.rsmodified+45 -29
@@ -26,7 +26,7 @@ struct Header {
2626 /// Rows are stored bottom-up unless the height was negative.
2727 top_down: bool,
2828 bits: u16,
29- compression: u32,
29+ compression: Compression,
3030 /// Palette entries actually present in the file.
3131 colours: usize,
3232 /// Bytes per palette entry: 4 in a Windows header, 3 in an OS/2 one.
@@ -37,13 +37,31 @@ struct Header {
3737 masks: [u32; 4],
3838 }
3939
40-const BI_RGB: u32 = 0;
41-const BI_RLE8: u32 = 1;
42-const BI_RLE4: u32 = 2;
43-const BI_BITFIELDS: u32 = 3;
44-const BI_JPEG: u32 = 4;
45-const BI_PNG: u32 = 5;
46-const BI_ALPHABITFIELDS: u32 = 6;
40+#[derive(Clone, Copy, PartialEq, Eq)]
41+enum Compression {
42+ Rgb,
43+ Rle8,
44+ Rle4,
45+ Bitfields,
46+ Jpeg,
47+ Png,
48+ AlphaBitfields,
49+}
50+
51+impl Compression {
52+ fn from_raw(raw: u32) -> Option<Self> {
53+ Some(match raw {
54+ 0 => Compression::Rgb,
55+ 1 => Compression::Rle8,
56+ 2 => Compression::Rle4,
57+ 3 => Compression::Bitfields,
58+ 4 => Compression::Jpeg,
59+ 5 => Compression::Png,
60+ 6 => Compression::AlphaBitfields,
61+ _ => return None,
62+ })
63+ }
64+}
4765
4866 fn u16_at(data: &[u8], at: usize) -> Result<u16> {
4967 data.get(at..at + 2)
@@ -72,7 +90,7 @@ fn header(data: &[u8]) -> Result<Header> {
7290 u32::from(u16_at(data, 20)?),
7391 false,
7492 u16_at(data, 24)?,
75- BI_RGB,
93+ 0,
7694 0usize,
7795 )
7896 } else if (16..40).contains(&info_size) {
@@ -84,7 +102,7 @@ fn header(data: &[u8]) -> Result<Header> {
84102 raw_height.unsigned_abs(),
85103 raw_height < 0,
86104 u16_at(data, 28)?,
87- BI_RGB,
105+ 0,
88106 0usize,
89107 )
90108 } else if info_size >= 40 {
@@ -106,16 +124,11 @@ fn header(data: &[u8]) -> Result<Header> {
106124 if !matches!(bits, 1 | 2 | 4 | 8 | 16 | 24 | 32) {
107125 return Err(Error::corrupt(format!("BMP: {bits} bits per pixel")));
108126 }
109- match compression {
110- // Handled before the header is read; reaching here means the caller
111- // asked for the fields of a file that has none of them.
112- BI_JPEG | BI_PNG => {}
113- BI_RGB | BI_RLE8 | BI_RLE4 | BI_BITFIELDS | BI_ALPHABITFIELDS => {}
114- other => {
115- return Err(Error::corrupt(format!("BMP: compression {other}")));
116- }
117- }
118- if (compression == BI_RLE8 && bits != 8) || (compression == BI_RLE4 && bits != 4) {
127+ let compression = Compression::from_raw(compression)
128+ .ok_or_else(|| Error::corrupt(format!("BMP: compression {compression}")))?;
129+ if (compression == Compression::Rle8 && bits != 8)
130+ || (compression == Compression::Rle4 && bits != 4)
131+ {
119132 return Err(Error::corrupt(format!(
120133 "BMP: run-length encoding with {bits} bits per pixel"
121134 )));
@@ -123,18 +136,21 @@ fn header(data: &[u8]) -> Result<Header> {
123136
124137 // Under BI_BITFIELDS the masks follow the 40-byte header in a v3 file and
125138 // live inside a v4/v5 one; either way they start at offset 54.
126- let bitfields = compression == BI_BITFIELDS || compression == BI_ALPHABITFIELDS;
139+ let bitfields = matches!(
140+ compression,
141+ Compression::Bitfields | Compression::AlphaBitfields
142+ );
127143 let mut masks = [0u32; 4];
128144 let mut palette_at = 14 + info_size;
129145 if bitfields && info_size == 40 {
130146 for (i, mask) in masks.iter_mut().enumerate().take(3) {
131147 *mask = u32_at(data, 54 + i * 4)?;
132148 }
133- if compression == BI_ALPHABITFIELDS {
149+ if compression == Compression::AlphaBitfields {
134150 masks[3] = u32_at(data, 66)?;
135151 }
136152 palette_at += 12
137- + if compression == BI_ALPHABITFIELDS {
153+ + if compression == Compression::AlphaBitfields {
138154 4
139155 } else {
140156 0
@@ -216,9 +232,9 @@ fn embedded(data: &[u8]) -> Result<Option<(ImageFormat, &[u8])>> {
216232 if !data.starts_with(b"BM") || u32_at(data, 14)? < 40 {
217233 return Ok(None);
218234 }
219- let format = match u32_at(data, 30)? {
220- BI_PNG => ImageFormat::Png,
221- BI_JPEG => ImageFormat::Jpeg,
235+ let format = match Compression::from_raw(u32_at(data, 30)?) {
236+ Some(Compression::Png) => ImageFormat::Png,
237+ Some(Compression::Jpeg) => ImageFormat::Jpeg,
222238 _ => return Ok(None),
223239 };
224240 let offset = u32_at(data, 10)? as usize;
@@ -289,7 +305,7 @@ pub fn decode(data: &[u8], limits: Limits) -> Result<Image> {
289305 // Indices first, then colour: every form below produces either palette
290306 // indices or direct samples, and the bottom-up flip is the same either way.
291307 let has_alpha = header.masks[3] != 0
292- || (header.bits == 32 && header.compression == BI_RGB && palette.is_empty());
308+ || (header.bits == 32 && header.compression == Compression::Rgb && palette.is_empty());
293309 let channels = if has_alpha { 4 } else { 3 };
294310 // The output is checked before anything else sizes from the header; the RLE index buffer
295311 // below is width * height, which this bound already covers.
@@ -298,7 +314,7 @@ pub fn decode(data: &[u8], limits: Limits) -> Result<Image> {
298314 let row_of = |y: usize| if header.top_down { y } else { height - 1 - y };
299315
300316 match header.compression {
301- BI_RLE8 | BI_RLE4 => {
317+ Compression::Rle8 | Compression::Rle4 => {
302318 let indices = rle(body, &header)?;
303319 for y in 0..height {
304320 for x in 0..width {
@@ -427,7 +443,7 @@ fn rle(body: &[u8], header: &Header) -> Result<Vec<u8>> {
427443 let mut out = vec![0u8; width * height];
428444 let (mut x, mut y) = (0usize, 0usize);
429445 let mut at = 0usize;
430- let four_bit = header.compression == BI_RLE4;
446+ let four_bit = header.compression == Compression::Rle4;
431447
432448 let put = |out: &mut Vec<u8>, x: &mut usize, y: usize, index: u8| {
433449 if *x < width && y < height {