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