add png loading tests

This commit is contained in:
Gered 2023-03-20 12:02:55 -04:00
parent 6e09c84c4a
commit 76d0289a0f
31 changed files with 182 additions and 4 deletions

View file

@ -220,7 +220,7 @@ impl PixelReader<u8> for PixelDecoder<u8> {
ColorFormat::IndexedColor => { ColorFormat::IndexedColor => {
Ok(reader.read_u8()?) Ok(reader.read_u8()?)
} }
_ => return Err(PngError::BadFile(format!("Unsupported color format: {:?}", self.header.format))), _ => return Err(PngError::BadFile(format!("Unsupported color format for this PixelReader: {:?}", self.header.format))),
} }
} }
} }
@ -249,7 +249,7 @@ impl PixelReader<u32> for PixelDecoder<u32> {
let a = reader.read_u8()?; let a = reader.read_u8()?;
Ok(to_argb32(a, r, g, b)) Ok(to_argb32(a, r, g, b))
} }
_ => return Err(PngError::BadFile(format!("Unsupported color format: {:?}", self.header.format))), _ => return Err(PngError::BadFile(format!("Unsupported color format for this PixelReader: {:?}", self.header.format))),
} }
} }
} }
@ -468,11 +468,176 @@ impl RgbaBitmap {
#[cfg(test)] #[cfg(test)]
pub mod tests { pub mod tests {
use std::io::Read;
use std::path::PathBuf;
use byteorder::LittleEndian;
use claim::*;
use super::*; use super::*;
const BASE_PATH: &str = "./test-assets/png/";
fn path_to_file(file: &Path) -> PathBuf {
PathBuf::from(BASE_PATH).join(file)
}
fn load_raw_indexed(bin_file: &Path) -> Result<Box<[u8]>, io::Error> {
let f = File::open(bin_file)?;
let mut reader = BufReader::new(f);
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer)?;
Ok(buffer.into_boxed_slice())
}
fn load_raw_argb(bin_file: &Path) -> Result<Box<[u32]>, io::Error> {
let f = File::open(bin_file)?;
let mut reader = BufReader::new(f);
let mut buffer = Vec::new();
loop {
buffer.push(match reader.read_u32::<LittleEndian>() {
Ok(value) => value,
Err(err) if err.kind() == io::ErrorKind::UnexpectedEof => break,
Err(err) => return Err(err),
});
}
Ok(buffer.into_boxed_slice())
}
#[test] #[test]
pub fn foo() -> Result<(), PngError> { pub fn loads_indexed_256_color() -> Result<(), PngError> {
let _ = IndexedBitmap::load_png_file(Path::new("./test-assets/test_indexed.png"))?; let ref_bytes = load_raw_indexed(path_to_file(Path::new("indexed_8.bin")).as_path())?;
let (bmp, palette) = IndexedBitmap::load_png_file(path_to_file(Path::new("indexed_8.png")).as_path())?;
assert!(palette.is_some());
assert_eq!(ref_bytes, bmp.pixels);
Ok(())
}
#[test]
pub fn loads_indexed_256_color_to_rgba_destination() -> Result<(), PngError> {
let ref_bytes = load_raw_argb(path_to_file(Path::new("indexed_8_rgba.bin")).as_path())?;
let (bmp, palette) = RgbaBitmap::load_png_file(path_to_file(Path::new("indexed_8.png")).as_path())?;
assert!(palette.is_some());
assert_eq!(ref_bytes, bmp.pixels);
Ok(())
}
#[test]
pub fn loads_rgb_color() -> Result<(), PngError> {
let ref_bytes = load_raw_argb(path_to_file(Path::new("rgb.bin")).as_path())?;
let (bmp, palette) = RgbaBitmap::load_png_file(path_to_file(Path::new("rgb.png")).as_path())?;
assert!(palette.is_none());
assert_eq!(ref_bytes, bmp.pixels);
Ok(())
}
#[test]
pub fn loads_rgba_color() -> Result<(), PngError> {
let ref_bytes = load_raw_argb(path_to_file(Path::new("rgba.bin")).as_path())?;
let (bmp, palette) = RgbaBitmap::load_png_file(path_to_file(Path::new("rgba.png")).as_path())?;
assert!(palette.is_none());
assert_eq!(ref_bytes, bmp.pixels);
Ok(())
}
#[test]
pub fn loads_filter_0() -> Result<(), PngError> {
let ref_bytes = load_raw_argb(path_to_file(Path::new("filter_0_rgb.bin")).as_path())?;
let (bmp, palette) = RgbaBitmap::load_png_file(path_to_file(Path::new("filter_0_rgb.png")).as_path())?;
assert!(palette.is_none());
assert_eq!(ref_bytes, bmp.pixels);
Ok(())
}
#[test]
pub fn loads_filter_1() -> Result<(), PngError> {
let ref_bytes = load_raw_argb(path_to_file(Path::new("filter_1_rgb.bin")).as_path())?;
let (bmp, palette) = RgbaBitmap::load_png_file(path_to_file(Path::new("filter_1_rgb.png")).as_path())?;
assert!(palette.is_none());
assert_eq!(ref_bytes, bmp.pixels);
Ok(())
}
#[test]
pub fn loads_filter_2() -> Result<(), PngError> {
let ref_bytes = load_raw_argb(path_to_file(Path::new("filter_2_rgb.bin")).as_path())?;
let (bmp, palette) = RgbaBitmap::load_png_file(path_to_file(Path::new("filter_2_rgb.png")).as_path())?;
assert!(palette.is_none());
assert_eq!(ref_bytes, bmp.pixels);
Ok(())
}
#[test]
pub fn loads_filter_3() -> Result<(), PngError> {
let ref_bytes = load_raw_argb(path_to_file(Path::new("filter_3_rgb.bin")).as_path())?;
let (bmp, palette) = RgbaBitmap::load_png_file(path_to_file(Path::new("filter_3_rgb.png")).as_path())?;
assert!(palette.is_none());
assert_eq!(ref_bytes, bmp.pixels);
Ok(())
}
#[test]
pub fn loads_filter_4() -> Result<(), PngError> {
let ref_bytes = load_raw_argb(path_to_file(Path::new("filter_4_rgb.bin")).as_path())?;
let (bmp, palette) = RgbaBitmap::load_png_file(path_to_file(Path::new("filter_4_rgb.png")).as_path())?;
assert!(palette.is_none());
assert_eq!(ref_bytes, bmp.pixels);
Ok(())
}
#[test]
pub fn loads_larger_indexed_256color_images() -> Result<(), PngError> {
let ref_bytes = load_raw_indexed(path_to_file(Path::new("large_1_indexed.bin")).as_path())?;
let (bmp, palette) = IndexedBitmap::load_png_file(path_to_file(Path::new("large_1_indexed.png")).as_path())?;
assert!(palette.is_some());
assert_eq!(ref_bytes, bmp.pixels);
let ref_bytes = load_raw_indexed(path_to_file(Path::new("large_2_indexed.bin")).as_path())?;
let (bmp, palette) = IndexedBitmap::load_png_file(path_to_file(Path::new("large_2_indexed.png")).as_path())?;
assert!(palette.is_some());
assert_eq!(ref_bytes, bmp.pixels);
Ok(())
}
#[test]
pub fn loads_larger_rgb_images() -> Result<(), PngError> {
let ref_bytes = load_raw_argb(path_to_file(Path::new("large_1_rgba.bin")).as_path())?;
let (bmp, palette) = RgbaBitmap::load_png_file(path_to_file(Path::new("large_1_rgb.png")).as_path())?;
assert!(palette.is_none());
assert_eq!(ref_bytes, bmp.pixels);
let ref_bytes = load_raw_argb(path_to_file(Path::new("large_2_rgba.bin")).as_path())?;
let (bmp, palette) = RgbaBitmap::load_png_file(path_to_file(Path::new("large_2_rgb.png")).as_path())?;
assert!(palette.is_none());
assert_eq!(ref_bytes, bmp.pixels);
Ok(())
}
#[test]
pub fn load_fails_on_unsupported_formats() -> Result<(), PngError> {
assert_matches!(
RgbaBitmap::load_png_file(path_to_file(Path::new("unsupported_alpha_8bit.png")).as_path()),
Err(PngError::BadFile(..))
);
assert_matches!(
RgbaBitmap::load_png_file(path_to_file(Path::new("unsupported_greyscale_8bit.png")).as_path()),
Err(PngError::BadFile(..))
);
assert_matches!(
RgbaBitmap::load_png_file(path_to_file(Path::new("unsupported_indexed_16col.png")).as_path()),
Err(PngError::BadFile(..))
);
assert_matches!(
RgbaBitmap::load_png_file(path_to_file(Path::new("unsupported_rgb_16bit.png")).as_path()),
Err(PngError::BadFile(..))
);
assert_matches!(
RgbaBitmap::load_png_file(path_to_file(Path::new("unsupported_rgba_16bit.png")).as_path()),
Err(PngError::BadFile(..))
);
Ok(()) Ok(())
} }
} }

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 985 B

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 B

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 302 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB