diff --git a/crates/ec-image/src/bmp.rs b/crates/ec-image/src/bmp.rs index ba50e0d4..932c320e 100644 --- a/crates/ec-image/src/bmp.rs +++ b/crates/ec-image/src/bmp.rs @@ -26,7 +26,7 @@ struct Header { /// Rows are stored bottom-up unless the height was negative. top_down: bool, bits: u16, - compression: u32, + compression: Compression, /// Palette entries actually present in the file. colours: usize, /// Bytes per palette entry: 4 in a Windows header, 3 in an OS/2 one. @@ -37,13 +37,31 @@ struct Header { masks: [u32; 4], } -const BI_RGB: u32 = 0; -const BI_RLE8: u32 = 1; -const BI_RLE4: u32 = 2; -const BI_BITFIELDS: u32 = 3; -const BI_JPEG: u32 = 4; -const BI_PNG: u32 = 5; -const BI_ALPHABITFIELDS: u32 = 6; +#[derive(Clone, Copy, PartialEq, Eq)] +enum Compression { + Rgb, + Rle8, + Rle4, + Bitfields, + Jpeg, + Png, + AlphaBitfields, +} + +impl Compression { + fn from_raw(raw: u32) -> Option { + Some(match raw { + 0 => Compression::Rgb, + 1 => Compression::Rle8, + 2 => Compression::Rle4, + 3 => Compression::Bitfields, + 4 => Compression::Jpeg, + 5 => Compression::Png, + 6 => Compression::AlphaBitfields, + _ => return None, + }) + } +} fn u16_at(data: &[u8], at: usize) -> Result { data.get(at..at + 2) @@ -72,7 +90,7 @@ fn header(data: &[u8]) -> Result
{ u32::from(u16_at(data, 20)?), false, u16_at(data, 24)?, - BI_RGB, + 0, 0usize, ) } else if (16..40).contains(&info_size) { @@ -84,7 +102,7 @@ fn header(data: &[u8]) -> Result
{ raw_height.unsigned_abs(), raw_height < 0, u16_at(data, 28)?, - BI_RGB, + 0, 0usize, ) } else if info_size >= 40 { @@ -106,16 +124,11 @@ fn header(data: &[u8]) -> Result
{ if !matches!(bits, 1 | 2 | 4 | 8 | 16 | 24 | 32) { return Err(Error::corrupt(format!("BMP: {bits} bits per pixel"))); } - match compression { - // Handled before the header is read; reaching here means the caller - // asked for the fields of a file that has none of them. - BI_JPEG | BI_PNG => {} - BI_RGB | BI_RLE8 | BI_RLE4 | BI_BITFIELDS | BI_ALPHABITFIELDS => {} - other => { - return Err(Error::corrupt(format!("BMP: compression {other}"))); - } - } - if (compression == BI_RLE8 && bits != 8) || (compression == BI_RLE4 && bits != 4) { + let compression = Compression::from_raw(compression) + .ok_or_else(|| Error::corrupt(format!("BMP: compression {compression}")))?; + if (compression == Compression::Rle8 && bits != 8) + || (compression == Compression::Rle4 && bits != 4) + { return Err(Error::corrupt(format!( "BMP: run-length encoding with {bits} bits per pixel" ))); @@ -123,18 +136,21 @@ fn header(data: &[u8]) -> Result
{ // Under BI_BITFIELDS the masks follow the 40-byte header in a v3 file and // live inside a v4/v5 one; either way they start at offset 54. - let bitfields = compression == BI_BITFIELDS || compression == BI_ALPHABITFIELDS; + let bitfields = matches!( + compression, + Compression::Bitfields | Compression::AlphaBitfields + ); let mut masks = [0u32; 4]; let mut palette_at = 14 + info_size; if bitfields && info_size == 40 { for (i, mask) in masks.iter_mut().enumerate().take(3) { *mask = u32_at(data, 54 + i * 4)?; } - if compression == BI_ALPHABITFIELDS { + if compression == Compression::AlphaBitfields { masks[3] = u32_at(data, 66)?; } palette_at += 12 - + if compression == BI_ALPHABITFIELDS { + + if compression == Compression::AlphaBitfields { 4 } else { 0 @@ -216,9 +232,9 @@ fn embedded(data: &[u8]) -> Result> { if !data.starts_with(b"BM") || u32_at(data, 14)? < 40 { return Ok(None); } - let format = match u32_at(data, 30)? { - BI_PNG => ImageFormat::Png, - BI_JPEG => ImageFormat::Jpeg, + let format = match Compression::from_raw(u32_at(data, 30)?) { + Some(Compression::Png) => ImageFormat::Png, + Some(Compression::Jpeg) => ImageFormat::Jpeg, _ => return Ok(None), }; let offset = u32_at(data, 10)? as usize; @@ -289,7 +305,7 @@ pub fn decode(data: &[u8], limits: Limits) -> Result { // Indices first, then colour: every form below produces either palette // indices or direct samples, and the bottom-up flip is the same either way. let has_alpha = header.masks[3] != 0 - || (header.bits == 32 && header.compression == BI_RGB && palette.is_empty()); + || (header.bits == 32 && header.compression == Compression::Rgb && palette.is_empty()); let channels = if has_alpha { 4 } else { 3 }; // The output is checked before anything else sizes from the header; the RLE index buffer // below is width * height, which this bound already covers. @@ -298,7 +314,7 @@ pub fn decode(data: &[u8], limits: Limits) -> Result { let row_of = |y: usize| if header.top_down { y } else { height - 1 - y }; match header.compression { - BI_RLE8 | BI_RLE4 => { + Compression::Rle8 | Compression::Rle4 => { let indices = rle(body, &header)?; for y in 0..height { for x in 0..width { @@ -427,7 +443,7 @@ fn rle(body: &[u8], header: &Header) -> Result> { let mut out = vec![0u8; width * height]; let (mut x, mut y) = (0usize, 0usize); let mut at = 0usize; - let four_bit = header.compression == BI_RLE4; + let four_bit = header.compression == Compression::Rle4; let put = |out: &mut Vec, x: &mut usize, y: usize, index: u8| { if *x < width && y < height {