From a087c3502497428b0c7dc19aedf3f4952a7f7e81 Mon Sep 17 00:00:00 2001 From: gered Date: Fri, 24 Mar 2023 20:11:08 -0400 Subject: [PATCH] add pixel format option to RgbaBitmap png saving --- ggdt/src/graphics/bitmap/png.rs | 39 ++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/ggdt/src/graphics/bitmap/png.rs b/ggdt/src/graphics/bitmap/png.rs index 3010195..2076a12 100644 --- a/ggdt/src/graphics/bitmap/png.rs +++ b/ggdt/src/graphics/bitmap/png.rs @@ -38,6 +38,12 @@ pub enum PngError { IOError(#[from] std::io::Error), } +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub enum PngFormat { + RGB, + RGBA, +} + #[derive(Debug, Copy, Clone, Eq, PartialEq)] enum ColorFormat { Grayscale = 0, @@ -477,6 +483,7 @@ where fn write_png_bytes( writer: &mut Writer, bitmap: &Bitmap, + format: ColorFormat, palette: Option<&Palette>, ) -> Result<(), PngError> where @@ -484,12 +491,6 @@ where PixelType: Pixel, ScanlineBuffer: ScanlinePixelConverter, { - let format = if Bitmap::::PIXEL_SIZE == 1 { - ColorFormat::IndexedColor - } else { - ColorFormat::RGBA - }; - // magic PNG header writer.write_all(&PNG_HEADER)?; @@ -579,7 +580,7 @@ impl IndexedBitmap { writer: &mut T, palette: &Palette, ) -> Result<(), PngError> { - write_png_bytes(writer, &self, Some(palette)) + write_png_bytes(writer, &self, ColorFormat::IndexedColor, Some(palette)) } pub fn to_png_file(&self, path: &Path, palette: &Palette) -> Result<(), PngError> { @@ -605,14 +606,26 @@ impl RgbaBitmap { pub fn to_png_bytes( &self, writer: &mut T, + format: PngFormat, ) -> Result<(), PngError> { - write_png_bytes(writer, &self, None) + write_png_bytes( + writer, + &self, + match format { + PngFormat::RGB => ColorFormat::RGB, + PngFormat::RGBA => ColorFormat::RGBA, + }, + None, + ) } - pub fn to_png_file(&self, path: &Path) -> Result<(), PngError> { + pub fn to_png_file( + &self, path: &Path, + format: PngFormat, + ) -> Result<(), PngError> { let f = File::create(path)?; let mut writer = BufWriter::new(f); - self.to_png_bytes(&mut writer) + self.to_png_bytes(&mut writer, format) } } @@ -823,7 +836,7 @@ pub mod tests { assert!(palette.is_none()); let save_path = tmp_dir.path().join("test_save.png"); - bmp.to_png_file(&save_path)?; + bmp.to_png_file(&save_path, PngFormat::RGB)?; let (reloaded_bmp, reloaded_palette) = RgbaBitmap::load_png_file(&save_path)?; assert_eq!(32, reloaded_bmp.width()); assert_eq!(32, reloaded_bmp.height()); @@ -848,7 +861,7 @@ pub mod tests { assert!(palette.is_none()); let save_path = tmp_dir.path().join("test_save.png"); - bmp.to_png_file(&save_path)?; + bmp.to_png_file(&save_path, PngFormat::RGB)?; let (reloaded_bmp, reloaded_palette) = RgbaBitmap::load_png_file(&save_path)?; assert_eq!(320, reloaded_bmp.width()); assert_eq!(200, reloaded_bmp.height()); @@ -866,7 +879,7 @@ pub mod tests { assert!(palette.is_none()); let save_path = tmp_dir.path().join("test_save.png"); - bmp.to_png_file(&save_path)?; + bmp.to_png_file(&save_path, PngFormat::RGB)?; let (reloaded_bmp, reloaded_palette) = RgbaBitmap::load_png_file(&save_path)?; assert_eq!(320, reloaded_bmp.width()); assert_eq!(200, reloaded_bmp.height());