oxedyne/fe2o3/fe2o3_graphics/tests/raster_matrix.rs
5.8 KiB, 1 run
created by r1870400018:75857, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Decode BMP, GIF and TIFF fixtures written by ImageMagick and Pillow, and check the pixels against |
| 2 | //! ImageMagick's own reading of each file. |
| 3 | //! |
| 4 | //! `tests/raster/gen.sh` writes every fixture and, beside it, `<name>.png`: ImageMagick's decode of |
| 5 | //! that fixture as eight-bit RGBA (`<name>.p<n>.png` for page `n` of a multipage TIFF). That PNG is |
| 6 | //! read here with this crate's PNG decoder, which `png_matrix.rs` holds to the same independent |
| 7 | //! standard, so nothing in a comparison originates in the codec under test. |
| 8 | //! |
| 9 | //! Where both readers agree a pixel is fully transparent, its colour is not compared: neither format |
| 10 | //! defines it and the two readers fill it differently. |
| 11 | //! |
| 12 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 13 | //! Anthropic Claude |
| 14 | |
| 15 | use oxedyne_fe2o3_core::prelude::*; |
| 16 | use oxedyne_fe2o3_graphics::{ |
| 17 | convert, |
| 18 | pixmap::Pixmap, |
| 19 | png, |
| 20 | }; |
| 21 | use oxedyne_fe2o3_stds::media::Media; |
| 22 | |
| 23 | use std::{ |
| 24 | fs, |
| 25 | path::PathBuf, |
| 26 | }; |
| 27 | |
| 28 | // A fixture: its file name, its page count, and the largest difference a channel may show. |
| 29 | type Case = (&'static str, usize, u8); |
| 30 | |
| 31 | const CASES: &[Case] = &[ |
| 32 | // BMP |
| 33 | ("bmp1_text.bmp", 1, 0), |
| 34 | ("bmp1_text_pil.bmp", 1, 0), |
| 35 | ("bmp4.bmp", 1, 0), |
| 36 | ("bmp4_rle.bmp", 1, 0), |
| 37 | ("bmp8.bmp", 1, 0), |
| 38 | ("bmp8_rle.bmp", 1, 0), |
| 39 | ("bmp16_555.bmp", 1, 1), // five bits scaled to eight, rounded differently |
| 40 | ("bmp16_565.bmp", 1, 1), |
| 41 | ("bmp24.bmp", 1, 0), |
| 42 | ("bmp24_os2.bmp", 1, 0), |
| 43 | ("bmp24_topdown.bmp", 1, 0), |
| 44 | ("bmp32_v5.bmp", 1, 0), |
| 45 | // GIF |
| 46 | ("gif_plain.gif", 1, 0), |
| 47 | ("gif_interlaced.gif", 1, 0), |
| 48 | ("gif_transparent.gif", 1, 0), |
| 49 | ("gif_text.gif", 1, 0), |
| 50 | // TIFF, one bit |
| 51 | ("tif_g1_none.tif", 1, 0), |
| 52 | ("tif_g1_packbits.tif", 1, 0), |
| 53 | ("tif_g1_lzw.tif", 1, 0), |
| 54 | ("tif_fax_mh.tif", 1, 0), |
| 55 | ("tif_fax_g3_1d.tif", 1, 0), |
| 56 | ("tif_fax_g3_2d.tif", 1, 0), |
| 57 | ("tif_fax_g3_2d_fill.tif", 1, 0), |
| 58 | ("tif_fax_g3_wide.tif", 1, 0), |
| 59 | ("tif_fax_g4.tif", 1, 0), |
| 60 | ("tif_fax_g4_wide.tif", 1, 0), |
| 61 | ("tif_fax_g4_lsb.tif", 1, 0), |
| 62 | ("tif_fax_g4_strips.tif", 1, 0), |
| 63 | // TIFF, grey and palette |
| 64 | ("tif_grey2.tif", 1, 0), |
| 65 | ("tif_grey4.tif", 1, 0), |
| 66 | ("tif_grey8_lzw.tif", 1, 0), |
| 67 | ("tif_grey8_miniswhite.tif", 1, 0), |
| 68 | ("tif_grey16_deflate.tif", 1, 1), // the two reductions to eight bits differ by one |
| 69 | ("tif_grey16_be.tif", 1, 1), |
| 70 | ("tif_palette4.tif", 1, 0), |
| 71 | ("tif_palette_lzw.tif", 1, 1), // Pillow writes v * 256 and ImageMagick truncates v / 257 |
| 72 | // TIFF, colour |
| 73 | ("tif_rgb_none.tif", 1, 0), |
| 74 | ("tif_rgb_packbits.tif", 1, 0), |
| 75 | ("tif_rgb_lzw_pred.tif", 1, 0), |
| 76 | ("tif_rgb_deflate.tif", 1, 0), |
| 77 | ("tif_rgb_jpeg.tif", 1, 8), // two JPEG decoders' transforms and upsampling |
| 78 | ("tif_rgb_tiled.tif", 1, 0), |
| 79 | ("tif_rgb_planar.tif", 1, 0), |
| 80 | ("tif_rgba_lzw.tif", 1, 0), |
| 81 | ("tif_cmyk.tif", 1, 1), // the naive CMYK to RGB, rounded differently |
| 82 | ("tif_multipage.tif", 3, 0), |
| 83 | ]; |
| 84 | |
| 85 | fn dir() -> PathBuf { |
| 86 | PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("raster") |
| 87 | } |
| 88 | |
| 89 | /// The largest channel difference between two pixmaps of one size, and where it is. |
| 90 | fn worst(got: &Pixmap, want: &Pixmap) -> (u8, usize, usize) { |
| 91 | let w = got.width(); |
| 92 | let mut out = (0u8, 0usize, 0usize); |
| 93 | for (i, (a, b)) in got.data().chunks_exact(4).zip(want.data().chunks_exact(4)).enumerate() { |
| 94 | if a[3] == 0 && b[3] == 0 { |
| 95 | continue; |
| 96 | } |
| 97 | for c in 0..4 { |
| 98 | let d = a[c].abs_diff(b[c]); |
| 99 | if d > out.0 { |
| 100 | out = (d, i % w, i / w); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | out |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn test_every_fixture_matches_imagemagick_00() -> Outcome<()> { |
| 109 | let mut fails = Vec::new(); |
| 110 | for &(name, pages, tol) in CASES { |
| 111 | let path = dir().join(name); |
| 112 | let bytes = res!(fs::read(&path)); |
| 113 | assert!(convert::decodable(Media::sniff(&bytes)), "{}: not sniffed as decodable", name); |
| 114 | let got = match convert::decode_pages(&bytes) { |
| 115 | Ok(g) => g, |
| 116 | Err(e) => { |
| 117 | fails.push(fmt!("{}: {}", name, e)); |
| 118 | continue; |
| 119 | } |
| 120 | }; |
| 121 | if got.len() != pages { |
| 122 | fails.push(fmt!("{}: {} pages, wanted {}", name, got.len(), pages)); |
| 123 | continue; |
| 124 | } |
| 125 | let stem = &name[..name.rfind('.').unwrap_or(name.len())]; |
| 126 | for (i, pm) in got.iter().enumerate() { |
| 127 | let refname = if pages > 1 { fmt!("{}.p{}.png", stem, i) } else { fmt!("{}.png", stem) }; |
| 128 | let want = res!(png::decode(&res!(fs::read(dir().join(&refname))))); |
| 129 | if (pm.width(), pm.height()) != (want.width(), want.height()) { |
| 130 | fails.push(fmt!("{} page {}: {}x{}, wanted {}x{}", name, i, |
| 131 | pm.width(), pm.height(), want.width(), want.height())); |
| 132 | continue; |
| 133 | } |
| 134 | let (d, x, y) = worst(pm, &want); |
| 135 | if d > tol { |
| 136 | fails.push(fmt!("{} page {}: channel off by {} at ({}, {}): got {:?}, wanted {:?}", |
| 137 | name, i, d, x, y, pm.pixel(x, y), want.pixel(x, y))); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | assert!(fails.is_empty(), "{} of {} fixtures differ:\n{}", fails.len(), CASES.len(), |
| 142 | fails.join("\n")); |
| 143 | Ok(()) |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn test_every_fixture_converts_to_a_common_format_00() -> Outcome<()> { |
| 148 | for &(name, pages, _) in CASES { |
| 149 | let bytes = res!(fs::read(dir().join(name))); |
| 150 | let out = res!(convert::to_common(&bytes)); |
| 151 | assert_eq!(out.len(), pages, "{}", name); |
| 152 | for c in &out { |
| 153 | assert!(matches!(c.media, Media::Png | Media::Jpeg), "{}: {:?}", name, c.media); |
| 154 | assert_eq!(Media::sniff(&c.bytes), c.media, "{}", name); |
| 155 | } |
| 156 | } |
| 157 | Ok(()) |
| 158 | } |
| 159 | |
| 160 | /// A read-only check on a real scan, named by `FE2O3_RASTER_SCAN`, which nothing here commits. |
| 161 | /// It prints the size of the PNG the scan becomes, and checks that decoding the PNG gives back the |
| 162 | /// scan's pixels. |
| 163 | #[test] |
| 164 | fn test_a_real_scan_named_in_the_environment_00() -> Outcome<()> { |
| 165 | let path = match std::env::var("FE2O3_RASTER_SCAN") { |
| 166 | Ok(p) => p, |
| 167 | Err(_) => return Ok(()), |
| 168 | }; |
| 169 | let bytes = res!(fs::read(&path)); |
| 170 | let pm = res!(convert::decode(&bytes)); |
| 171 | let out = res!(convert::to_common(&bytes)); |
| 172 | assert_eq!(out.len(), 1); |
| 173 | println!("{}: {} bytes, {}x{}, becomes a {} of {} bytes", path, bytes.len(), pm.width(), |
| 174 | pm.height(), out[0].media.label(), out[0].bytes.len()); |
| 175 | assert_eq!(res!(png::decode(&out[0].bytes)), pm); |
| 176 | Ok(()) |
| 177 | } |