Skip to content

Commit

Permalink
Extend animated tests to apng
Browse files Browse the repository at this point in the history
  • Loading branch information
HeroicKatora committed Jun 17, 2020
1 parent da78e55 commit 46df994
Show file tree
Hide file tree
Showing 22 changed files with 51 additions and 13 deletions.
Binary file added tests/images/png/apng/ball.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 51 additions & 13 deletions tests/reference_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ enum ReferenceTestKind {
/// against the reference image.
SingleImage,

/// From the test image file, a single frame is extracted using
/// `image::gif::Decoder`, and the result is compared against the reference
/// image.
AnimatedGifFrame {
/// From the test image file, a single frame is extracted using a fitting animation decoder and
/// the result is compared against the reference image.
AnimatedFrame {
/// A zero-based frame number.
frame: usize,
},
Expand Down Expand Up @@ -125,7 +124,7 @@ impl std::str::FromStr for ReferenceTestCase {
// `anim_FRAME_CRC`
crc = parse_crc(&meta[2]).ok_or("malformed CRC")?;
let frame: usize = meta[1].parse().map_err(|_| "malformed frame number")?;
kind = ReferenceTestKind::AnimatedGifFrame {
kind = ReferenceTestKind::AnimatedFrame {
frame: frame.checked_sub(1).ok_or("frame number must be 1-based")?,
};
} else {
Expand Down Expand Up @@ -181,12 +180,18 @@ fn check_references() {
img_path.push(case.orig_filename);

// Load the test image
let test_img;
let mut test_img = None;

match case.kind {
ReferenceTestKind::AnimatedGifFrame { frame: frame_num } => {
ReferenceTestKind::AnimatedFrame { frame: frame_num } => {
let format = image::io::Reader::open(&img_path)
.unwrap()
.with_guessed_format()
.unwrap()
.format();

#[cfg(feature = "gif")]
{
if format == Some(image::ImageFormat::Gif) {
// Interpret the input file as an animation file
use image::AnimationDecoder;
let stream = io::BufReader::new(fs::File::open(&img_path).unwrap());
Expand All @@ -211,11 +216,39 @@ fn check_references() {
let frame = frames.drain(frame_num..).nth(0).unwrap();

// Convert the frame to a`RgbaImage`
test_img = frame.into_buffer();
test_img = Some(frame.into_buffer());
}

#[cfg(feature = "png")]
if format == Some(image::ImageFormat::Png) {
// Interpret the input file as an animation file
use image::AnimationDecoder;
let stream = io::BufReader::new(fs::File::open(&img_path).unwrap());
let decoder = match image::png::PngDecoder::new(stream) {
Ok(decoder) => decoder.apng(),
Err(image::ImageError::Unsupported(_)) => return,
Err(err) => {
panic!(format!("decoding of {:?} failed with: {}", img_path, err))
}
};

let mut frames = match decoder.into_frames().collect_frames() {
Ok(frames) => frames,
Err(image::ImageError::Unsupported(_)) => return,
Err(err) => panic!(format!(
"collecting frames of {:?} failed with: {}",
img_path, err
)),
};

// Select a single frame
let frame = frames.drain(frame_num..).nth(0).unwrap();

// Convert the frame to a`RgbaImage`
test_img = Some(frame.into_buffer());
}

#[cfg(not(feature = "gif"))]
{
if test_img.is_none() {
println!("Skipping - GIF codec is not enabled");
return;
}
Expand All @@ -224,7 +257,7 @@ fn check_references() {
ReferenceTestKind::SingleImage => {
// Read the input file as a single image
match image::open(&img_path) {
Ok(img) => test_img = img.to_rgba(),
Ok(img) => test_img = Some(img.to_rgba()),
// Do not fail on unsupported error
// This might happen because the testsuite contains unsupported images
// or because a specific decoder included via a feature.
Expand All @@ -234,6 +267,11 @@ fn check_references() {
}
}

let test_img = match test_img.as_ref() {
Some(img) => img,
None => return,
};

let test_crc_actual = {
let mut hasher = Crc32::new();
hasher.update(&*test_img);
Expand All @@ -247,7 +285,7 @@ fn check_references() {
);
}

if *ref_img != *test_img {
if *ref_img != **test_img {
panic!("Reference rendering does not match.");
}
})
Expand Down

0 comments on commit 46df994

Please sign in to comment.