add png loading tests
|
@ -220,7 +220,7 @@ impl PixelReader<u8> for PixelDecoder<u8> {
|
|||
ColorFormat::IndexedColor => {
|
||||
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()?;
|
||||
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)]
|
||||
pub mod tests {
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use byteorder::LittleEndian;
|
||||
use claim::*;
|
||||
|
||||
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]
|
||||
pub fn foo() -> Result<(), PngError> {
|
||||
let _ = IndexedBitmap::load_png_file(Path::new("./test-assets/test_indexed.png"))?;
|
||||
pub fn loads_indexed_256_color() -> Result<(), PngError> {
|
||||
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(())
|
||||
}
|
||||
}
|
BIN
ggdt/test-assets/png/filter_0_rgb.bin
Normal file
BIN
ggdt/test-assets/png/filter_0_rgb.png
Normal file
After Width: | Height: | Size: 2.4 KiB |
BIN
ggdt/test-assets/png/filter_1_rgb.bin
Normal file
BIN
ggdt/test-assets/png/filter_1_rgb.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
ggdt/test-assets/png/filter_2_rgb.bin
Normal file
BIN
ggdt/test-assets/png/filter_2_rgb.png
Normal file
After Width: | Height: | Size: 1.7 KiB |
BIN
ggdt/test-assets/png/filter_3_rgb.bin
Normal file
BIN
ggdt/test-assets/png/filter_3_rgb.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
ggdt/test-assets/png/filter_4_rgb.bin
Normal file
BIN
ggdt/test-assets/png/filter_4_rgb.png
Normal file
After Width: | Height: | Size: 985 B |
BIN
ggdt/test-assets/png/indexed_8.bin
Normal file
BIN
ggdt/test-assets/png/indexed_8.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
ggdt/test-assets/png/indexed_8_rgba.bin
Normal file
12
ggdt/test-assets/png/large_1_indexed.bin
Normal file
BIN
ggdt/test-assets/png/large_1_indexed.png
Normal file
After Width: | Height: | Size: 8.3 KiB |
BIN
ggdt/test-assets/png/large_1_rgb.png
Normal file
After Width: | Height: | Size: 14 KiB |
1
ggdt/test-assets/png/large_1_rgba.bin
Normal file
BIN
ggdt/test-assets/png/large_2_indexed.bin
Normal file
BIN
ggdt/test-assets/png/large_2_indexed.png
Normal file
After Width: | Height: | Size: 25 KiB |
BIN
ggdt/test-assets/png/large_2_rgb.png
Normal file
After Width: | Height: | Size: 54 KiB |
BIN
ggdt/test-assets/png/large_2_rgba.bin
Normal file
BIN
ggdt/test-assets/png/rgb.bin
Normal file
BIN
ggdt/test-assets/png/rgb.png
Normal file
After Width: | Height: | Size: 145 B |
BIN
ggdt/test-assets/png/rgba.bin
Normal file
BIN
ggdt/test-assets/png/rgba.png
Normal file
After Width: | Height: | Size: 184 B |
BIN
ggdt/test-assets/png/unsupported_alpha_8bit.png
Normal file
After Width: | Height: | Size: 126 B |
BIN
ggdt/test-assets/png/unsupported_greyscale_8bit.png
Normal file
After Width: | Height: | Size: 138 B |
BIN
ggdt/test-assets/png/unsupported_indexed_16col.png
Normal file
After Width: | Height: | Size: 216 B |
BIN
ggdt/test-assets/png/unsupported_rgb_16bit.png
Normal file
After Width: | Height: | Size: 302 B |
BIN
ggdt/test-assets/png/unsupported_rgba_16bit.png
Normal file
After Width: | Height: | Size: 3.4 KiB |