|
1 |
| -use std::{fs::File, io::BufReader, path::PathBuf}; |
| 1 | +use std::{ |
| 2 | + fs::{self, File}, |
| 3 | + io::{BufReader, Cursor}, |
| 4 | + path::PathBuf, |
| 5 | +}; |
| 6 | + |
| 7 | +#[cfg(feature = "webp")] |
| 8 | +use image::{codecs::webp::WebPDecoder, AnimationDecoder}; |
2 | 9 |
|
3 | 10 | const BASE_PATH: [&str; 2] = [".", "tests"];
|
4 | 11 | const IMAGE_DIR: &str = "images";
|
@@ -37,6 +44,30 @@ fn check_regressions() {
|
37 | 44 | let _ = image::open(path);
|
38 | 45 | })
|
39 | 46 | }
|
| 47 | +/// Check that the WEBP frames iterator returns the right amount of frames. |
| 48 | +#[test] |
| 49 | +#[cfg(feature = "webp")] |
| 50 | +fn check_webp_frames_regressions() { |
| 51 | + let path: PathBuf = BASE_PATH |
| 52 | + .iter() |
| 53 | + .collect::<PathBuf>() |
| 54 | + .join(IMAGE_DIR) |
| 55 | + .join("webp/extended_images") |
| 56 | + .join("*.webp"); |
| 57 | + let pattern = &*format!("{}", path.display()); |
| 58 | + for path in glob::glob(pattern).unwrap().filter_map(Result::ok) { |
| 59 | + let bytes = fs::read(path).unwrap(); |
| 60 | + let cursor = Cursor::new(&bytes); |
| 61 | + let frame_count = image_webp::WebPDecoder::new(cursor.clone()) |
| 62 | + .unwrap() |
| 63 | + .num_frames() as usize; |
| 64 | + let decoder = WebPDecoder::new(cursor).unwrap(); |
| 65 | + // The `take` guards against a potentially infinitely running iterator. |
| 66 | + // Since we take `frame_count + 1`, we can assume that the last iteration already returns `None`. |
| 67 | + let actual_frame_count = decoder.into_frames().take(frame_count + 1).count(); |
| 68 | + assert_eq!(actual_frame_count, frame_count); |
| 69 | + } |
| 70 | +} |
40 | 71 |
|
41 | 72 | /// Check that BMP files with large values could cause OOM issues are rejected.
|
42 | 73 | ///
|
|
0 commit comments