From 1ddc5a43af790231672ad5250e6818c5fc0ec336 Mon Sep 17 00:00:00 2001 From: Marrub Date: Tue, 12 Mar 2019 16:29:03 -0400 Subject: [PATCH] internal cleanup --- source/marathon/pict.rs | 64 ++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/source/marathon/pict.rs b/source/marathon/pict.rs index 8706834..7214434 100644 --- a/source/marathon/pict.rs +++ b/source/marathon/pict.rs @@ -21,6 +21,7 @@ fn read_pm_header<'a>(b: &'a [u8], } let pack_t = PackType::from_repr(pack_t)?; + let depth = Depth::from_repr(depth)?; if pt_fl & 0x8000 == 0 { bail!("PICT1 not supported"); @@ -48,8 +49,8 @@ fn read_pm_header<'a>(b: &'a [u8], } let rle = pack_t == PackType::Default || - pack_t == PackType::Rle16 && depth == 16 || - pack_t == PackType::Rle32 && depth == 32; + pack_t == PackType::Rle16 && depth == Depth::Bits16 || + pack_t == PackType::Rle32 && depth == Depth::Bits32; let pitch = usize::from(pt_fl & 0x3FFF); @@ -62,7 +63,7 @@ fn read_pm_ind(mut im: Image8, b: &[u8], hdr: Header) -> ResultS let clut = ok!(hdr.clut, "no CLUT in indexed mode")?; let mut p = 0; - if hdr.pitch < 8 && hdr.depth == 8 { + if hdr.pitch < 8 && hdr.depth == Depth::Bits8 { // uncompressed 8-bit colormap indices for _ in 0..im.h() { for _ in 0..im.w() { @@ -80,7 +81,11 @@ fn read_pm_ind(mut im: Image8, b: &[u8], hdr: Header) -> ResultS for _ in 0..im.h() { let (d, pp) = read_rle::(&b[p..], hdr.pitch)?; - let d = if hdr.depth < 8 {expand_data(d, hdr.depth)?} else {d}; + let d = if hdr.depth < Depth::Bits8 { + expand_data(d, hdr.depth)? + } else { + d + }; p += pp; @@ -191,10 +196,12 @@ fn read_pm_area(im: Image8, b: &[u8], pack: bool, clip: bool) let (b, hdr) = read_pm_header(&b[p..], pack, clip, &im)?; match hdr.depth { - 1 | 2 | 4 | 8 => read_pm_ind(im, b, hdr), - 16 => read_pm_16(im, b, hdr), - 32 => read_pm_32(im, b, hdr), - _ => bail!("invalid bit depth"), + Depth::Bits1 | + Depth::Bits2 | + Depth::Bits4 | + Depth::Bits8 => read_pm_ind(im, b, hdr), + Depth::Bits16 => read_pm_16(im, b, hdr), + Depth::Bits32 => read_pm_32(im, b, hdr), } } @@ -429,20 +436,32 @@ impl ReadRleData for u8 } /// Expand packed pixel data based on bit depth. -fn expand_data(b: Vec, depth: u16) -> ResultS> +fn expand_data(b: Vec, depth: Depth) -> ResultS> { let mut o = Vec::with_capacity(match depth { - 4 => b.len() * 2, - 2 => b.len() * 4, - 1 => b.len() * 8, + Depth::Bits4 => b.len() * 2, + Depth::Bits2 => b.len() * 4, + Depth::Bits1 => b.len() * 8, _ => bail!("invalid bit depth"), }); for ch in b { match depth { - 4 => {for i in (0..=1).rev() {o.push(ch >> (i * 4) & 0xF_u8);}} - 2 => {for i in (0..=3).rev() {o.push(ch >> (i * 2) & 0x3_u8);}} - 1 => {for i in (0..=7).rev() {o.push(ch >> i & 0x1_u8);}} + Depth::Bits4 => { + for i in (0..=1).rev() { + o.push(ch >> (i * 4) & 0xF_u8); + } + } + Depth::Bits2 => { + for i in (0..=3).rev() { + o.push(ch >> (i * 2) & 0x3_u8); + } + } + Depth::Bits1 => { + for i in (0..=7).rev() { + o.push(ch >> i & 0x1_u8); + } + } _ => bail!("invalid bit depth"), } } @@ -454,11 +473,24 @@ struct Header { pitch: usize, pack_t: PackType, - depth: u16, + depth: Depth, clut: Option>, rle: bool, } +c_enum! { + #[derive(PartialEq, PartialOrd)] + enum Depth: u16 + { + 1 => Bits1, + 2 => Bits2, + 4 => Bits4, + 8 => Bits8, + 16 => Bits16, + 32 => Bits32, + } +} + c_enum! { #[derive(PartialEq)] enum PackType: u16