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 commitac6f8033cd2f841697cebc486a75a28375c46d41Parent: 3582d7f1 changed file +45 -29
- crates/ec-image/src/bmp.rs +45 -29
crates/ec-image/src/bmp.rsmodified+45 -29
| @@ -26,7 +26,7 @@ struct Header { | ||
| 26 | 26 | /// Rows are stored bottom-up unless the height was negative. |
| 27 | 27 | top_down: bool, |
| 28 | 28 | bits: u16, |
| 29 | - compression: u32, | |
| 29 | + compression: Compression, | |
| 30 | 30 | /// Palette entries actually present in the file. |
| 31 | 31 | colours: usize, |
| 32 | 32 | /// Bytes per palette entry: 4 in a Windows header, 3 in an OS/2 one. |
| @@ -37,13 +37,31 @@ struct Header { | ||
| 37 | 37 | masks: [u32; 4], |
| 38 | 38 | } |
| 39 | 39 | |
| 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 | +} | |
| 47 | 65 | |
| 48 | 66 | fn u16_at(data: &[u8], at: usize) -> Result<u16> { |
| 49 | 67 | data.get(at..at + 2) |
| @@ -72,7 +90,7 @@ fn header(data: &[u8]) -> Result<Header> { | ||
| 72 | 90 | u32::from(u16_at(data, 20)?), |
| 73 | 91 | false, |
| 74 | 92 | u16_at(data, 24)?, |
| 75 | - BI_RGB, | |
| 93 | + 0, | |
| 76 | 94 | 0usize, |
| 77 | 95 | ) |
| 78 | 96 | } else if (16..40).contains(&info_size) { |
| @@ -84,7 +102,7 @@ fn header(data: &[u8]) -> Result<Header> { | ||
| 84 | 102 | raw_height.unsigned_abs(), |
| 85 | 103 | raw_height < 0, |
| 86 | 104 | u16_at(data, 28)?, |
| 87 | - BI_RGB, | |
| 105 | + 0, | |
| 88 | 106 | 0usize, |
| 89 | 107 | ) |
| 90 | 108 | } else if info_size >= 40 { |
| @@ -106,16 +124,11 @@ fn header(data: &[u8]) -> Result<Header> { | ||
| 106 | 124 | if !matches!(bits, 1 | 2 | 4 | 8 | 16 | 24 | 32) { |
| 107 | 125 | return Err(Error::corrupt(format!("BMP: {bits} bits per pixel"))); |
| 108 | 126 | } |
| 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 | + { | |
| 119 | 132 | return Err(Error::corrupt(format!( |
| 120 | 133 | "BMP: run-length encoding with {bits} bits per pixel" |
| 121 | 134 | ))); |
| @@ -123,18 +136,21 @@ fn header(data: &[u8]) -> Result<Header> { | ||
| 123 | 136 | |
| 124 | 137 | // Under BI_BITFIELDS the masks follow the 40-byte header in a v3 file and |
| 125 | 138 | // 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 | + ); | |
| 127 | 143 | let mut masks = [0u32; 4]; |
| 128 | 144 | let mut palette_at = 14 + info_size; |
| 129 | 145 | if bitfields && info_size == 40 { |
| 130 | 146 | for (i, mask) in masks.iter_mut().enumerate().take(3) { |
| 131 | 147 | *mask = u32_at(data, 54 + i * 4)?; |
| 132 | 148 | } |
| 133 | - if compression == BI_ALPHABITFIELDS { | |
| 149 | + if compression == Compression::AlphaBitfields { | |
| 134 | 150 | masks[3] = u32_at(data, 66)?; |
| 135 | 151 | } |
| 136 | 152 | palette_at += 12 |
| 137 | - + if compression == BI_ALPHABITFIELDS { | |
| 153 | + + if compression == Compression::AlphaBitfields { | |
| 138 | 154 | 4 |
| 139 | 155 | } else { |
| 140 | 156 | 0 |
| @@ -216,9 +232,9 @@ fn embedded(data: &[u8]) -> Result<Option<(ImageFormat, &[u8])>> { | ||
| 216 | 232 | if !data.starts_with(b"BM") || u32_at(data, 14)? < 40 { |
| 217 | 233 | return Ok(None); |
| 218 | 234 | } |
| 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, | |
| 222 | 238 | _ => return Ok(None), |
| 223 | 239 | }; |
| 224 | 240 | let offset = u32_at(data, 10)? as usize; |
| @@ -289,7 +305,7 @@ pub fn decode(data: &[u8], limits: Limits) -> Result<Image> { | ||
| 289 | 305 | // Indices first, then colour: every form below produces either palette |
| 290 | 306 | // indices or direct samples, and the bottom-up flip is the same either way. |
| 291 | 307 | 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()); | |
| 293 | 309 | let channels = if has_alpha { 4 } else { 3 }; |
| 294 | 310 | // The output is checked before anything else sizes from the header; the RLE index buffer |
| 295 | 311 | // below is width * height, which this bound already covers. |
| @@ -298,7 +314,7 @@ pub fn decode(data: &[u8], limits: Limits) -> Result<Image> { | ||
| 298 | 314 | let row_of = |y: usize| if header.top_down { y } else { height - 1 - y }; |
| 299 | 315 | |
| 300 | 316 | match header.compression { |
| 301 | - BI_RLE8 | BI_RLE4 => { | |
| 317 | + Compression::Rle8 | Compression::Rle4 => { | |
| 302 | 318 | let indices = rle(body, &header)?; |
| 303 | 319 | for y in 0..height { |
| 304 | 320 | for x in 0..width { |
| @@ -427,7 +443,7 @@ fn rle(body: &[u8], header: &Header) -> Result<Vec<u8>> { | ||
| 427 | 443 | let mut out = vec![0u8; width * height]; |
| 428 | 444 | let (mut x, mut y) = (0usize, 0usize); |
| 429 | 445 | let mut at = 0usize; |
| 430 | - let four_bit = header.compression == BI_RLE4; | |
| 446 | + let four_bit = header.compression == Compression::Rle4; | |
| 431 | 447 | |
| 432 | 448 | let put = |out: &mut Vec<u8>, x: &mut usize, y: usize, index: u8| { |
| 433 | 449 | if *x < width && y < height { |