From b8eae5188837f3219dd1db416271595374629f1f Mon Sep 17 00:00:00 2001 From: relrelb Date: Sun, 6 Jun 2021 00:29:55 +0300 Subject: [PATCH] swf: Move num_colors into BitmapFormat::ColorMap8 --- core/src/backend/render.rs | 12 ++++++------ swf/src/read.rs | 17 +++++++---------- swf/src/test_data.rs | 2 -- swf/src/types.rs | 3 +-- swf/src/write.rs | 8 ++++---- 5 files changed, 18 insertions(+), 24 deletions(-) diff --git a/core/src/backend/render.rs b/core/src/backend/render.rs index af812535b..25b149c01 100644 --- a/core/src/backend/render.rs +++ b/core/src/backend/render.rs @@ -440,12 +440,12 @@ pub fn decode_define_bits_lossless( } decoded_data } - (1, swf::BitmapFormat::ColorMap8) => { + (1, swf::BitmapFormat::ColorMap8 { num_colors }) => { let mut i = 0; let padded_width = (swf_tag.width + 0b11) & !0b11; - let mut palette = Vec::with_capacity(swf_tag.num_colors as usize + 1); - for _ in 0..=swf_tag.num_colors { + let mut palette = Vec::with_capacity(num_colors as usize + 1); + for _ in 0..=num_colors { palette.push(Color { r: decoded_data[i], g: decoded_data[i + 1], @@ -476,12 +476,12 @@ pub fn decode_define_bits_lossless( } out_data } - (2, swf::BitmapFormat::ColorMap8) => { + (2, swf::BitmapFormat::ColorMap8 { num_colors }) => { let mut i = 0; let padded_width = (swf_tag.width + 0b11) & !0b11; - let mut palette = Vec::with_capacity(swf_tag.num_colors as usize + 1); - for _ in 0..=swf_tag.num_colors { + let mut palette = Vec::with_capacity(num_colors as usize + 1); + for _ in 0..=num_colors { palette.push(Color { r: decoded_data[i], g: decoded_data[i + 1], diff --git a/swf/src/read.rs b/swf/src/read.rs index bf8ad8b1d..503b5e973 100644 --- a/swf/src/read.rs +++ b/swf/src/read.rs @@ -2554,19 +2554,17 @@ impl<'a> Reader<'a> { pub fn read_define_bits_lossless(&mut self, version: u8) -> Result> { let id = self.read_character_id()?; - let format = match self.read_u8()? { - 3 => BitmapFormat::ColorMap8, + let format = self.read_u8()?; + let width = self.read_u16()?; + let height = self.read_u16()?; + let format = match format { + 3 => BitmapFormat::ColorMap8 { + num_colors: self.read_u8()?, + }, 4 if version == 1 => BitmapFormat::Rgb15, 5 => BitmapFormat::Rgb32, _ => return Err(Error::invalid_data("Invalid bitmap format.")), }; - let width = self.read_u16()?; - let height = self.read_u16()?; - let num_colors = if format == BitmapFormat::ColorMap8 { - self.read_u8()? - } else { - 0 - }; let data = self.read_slice_to_end(); Ok(DefineBitsLossless { version, @@ -2574,7 +2572,6 @@ impl<'a> Reader<'a> { format, width, height, - num_colors, data, }) } diff --git a/swf/src/test_data.rs b/swf/src/test_data.rs index 616b68577..8da7efa96 100644 --- a/swf/src/test_data.rs +++ b/swf/src/test_data.rs @@ -165,7 +165,6 @@ pub fn tag_tests() -> Vec { format: BitmapFormat::Rgb32, width: 8, height: 8, - num_colors: 0, data: &[ 120, 218, 251, 207, 192, 240, 255, 255, 8, 198, 0, 4, 128, 127, 129, ], @@ -183,7 +182,6 @@ pub fn tag_tests() -> Vec { format: BitmapFormat::Rgb32, width: 8, height: 8, - num_colors: 0, data: &[ 120, 218, 107, 96, 96, 168, 107, 24, 193, 24, 0, 227, 81, 63, 129, ], diff --git a/swf/src/types.rs b/swf/src/types.rs index 9125d4526..ffb803833 100644 --- a/swf/src/types.rs +++ b/swf/src/types.rs @@ -1408,13 +1408,12 @@ pub struct DefineBitsLossless<'a> { pub format: BitmapFormat, pub width: u16, pub height: u16, - pub num_colors: u8, pub data: &'a [u8], } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum BitmapFormat { - ColorMap8, + ColorMap8 { num_colors: u8 }, Rgb15, Rgb32, } diff --git a/swf/src/write.rs b/swf/src/write.rs index c27f9d797..a8ba7e3f9 100644 --- a/swf/src/write.rs +++ b/swf/src/write.rs @@ -555,7 +555,7 @@ impl Writer { Tag::DefineBitsLossless(ref tag) => { let mut length = 7 + tag.data.len(); - if tag.format == BitmapFormat::ColorMap8 { + if let BitmapFormat::ColorMap8 { .. } = tag.format { length += 1; } // TODO(Herschel): Throw error if RGB15 in tag version 2. @@ -567,15 +567,15 @@ impl Writer { self.write_tag_header(tag_code, length as u32)?; self.write_character_id(tag.id)?; let format_id = match tag.format { - BitmapFormat::ColorMap8 => 3, + BitmapFormat::ColorMap8 { .. } => 3, BitmapFormat::Rgb15 => 4, BitmapFormat::Rgb32 => 5, }; self.write_u8(format_id)?; self.write_u16(tag.width)?; self.write_u16(tag.height)?; - if tag.format == BitmapFormat::ColorMap8 { - self.write_u8(tag.num_colors)?; + if let BitmapFormat::ColorMap8 { num_colors } = tag.format { + self.write_u8(num_colors)?; } self.output.write_all(tag.data)?; }