|
| 1 | +//! Test: do mozjpeg and zenjpeg produce the same pixels for the same JPEG? |
| 2 | +//! |
| 3 | +//! This isolates JPEG decoder differences from CMS, pipeline, and encoder. |
| 4 | +//! If decoders produce different pixels, ICC test failures are decoder-caused. |
| 5 | +
|
| 6 | +use std::path::Path; |
| 7 | + |
| 8 | +/// Decode a JPEG with mozjpeg (v2 decoder) and return raw BGRA pixels + dimensions. |
| 9 | +#[cfg(feature = "c-codecs")] |
| 10 | +fn decode_mozjpeg(jpeg_bytes: &[u8]) -> (Vec<u8>, u32, u32) { |
| 11 | + let mut ctx = imageflow_core::Context::create().unwrap(); |
| 12 | + ctx.add_copied_input_buffer(0, jpeg_bytes).unwrap(); |
| 13 | + |
| 14 | + // Force v2 backend |
| 15 | + ctx.force_backend = Some(imageflow_core::Backend::V2); |
| 16 | + |
| 17 | + let steps = vec![ |
| 18 | + imageflow_types::Node::Decode { io_id: 0, commands: None }, |
| 19 | + imageflow_types::Node::CaptureBitmapKey { capture_id: 0 }, |
| 20 | + ]; |
| 21 | + |
| 22 | + let _ = ctx.build_1(imageflow_types::Build001 { |
| 23 | + builder_config: None, |
| 24 | + io: vec![imageflow_types::IoObject { |
| 25 | + io_id: 0, |
| 26 | + direction: imageflow_types::IoDirection::In, |
| 27 | + io: imageflow_types::IoEnum::ByteArray(jpeg_bytes.to_vec()), |
| 28 | + }], |
| 29 | + framewise: imageflow_types::Framewise::Steps(steps), |
| 30 | + }).unwrap(); |
| 31 | + |
| 32 | + let bitmap_key = ctx.get_captured_bitmap_key(0).unwrap(); |
| 33 | + let bitmaps = ctx.borrow_bitmaps().unwrap(); |
| 34 | + let mut bm = bitmaps.try_borrow_mut(bitmap_key).unwrap(); |
| 35 | + let window = bm.get_window_u8().unwrap(); |
| 36 | + let w = window.w(); |
| 37 | + let h = window.h(); |
| 38 | + let stride = window.info().t_stride() as usize; |
| 39 | + let bpp = 4; // BGRA |
| 40 | + |
| 41 | + // Copy pixel data row by row (stride may differ from w*bpp) |
| 42 | + let mut pixels = Vec::with_capacity(w as usize * h as usize * bpp); |
| 43 | + for y in 0..h { |
| 44 | + let row_start = y as usize * stride; |
| 45 | + let row_end = row_start + w as usize * bpp; |
| 46 | + pixels.extend_from_slice(&window.get_slice()[row_start..row_end]); |
| 47 | + } |
| 48 | + (pixels, w, h) |
| 49 | +} |
| 50 | + |
| 51 | +/// Decode a JPEG with zenjpeg (zen decoder) and return raw RGB pixels + dimensions. |
| 52 | +#[cfg(feature = "zen-pipeline")] |
| 53 | +fn decode_zenjpeg(jpeg_bytes: &[u8]) -> (Vec<u8>, u32, u32) { |
| 54 | + let registry = zencodecs::AllowedFormats::all(); |
| 55 | + let output = zencodecs::DecodeRequest::new(jpeg_bytes) |
| 56 | + .with_registry(®istry) |
| 57 | + .decode() |
| 58 | + .unwrap(); |
| 59 | + |
| 60 | + let w = output.width(); |
| 61 | + let h = output.height(); |
| 62 | + let desc = output.descriptor(); |
| 63 | + let bpp = desc.bytes_per_pixel(); |
| 64 | + eprintln!("[zenjpeg] {}x{} {}bpp {:?}", w, h, bpp, desc); |
| 65 | + let pixels = output.pixels(); |
| 66 | + (pixels.contiguous_bytes().to_vec(), w, h) |
| 67 | +} |
| 68 | + |
| 69 | +#[test] |
| 70 | +#[cfg(all(feature = "c-codecs", feature = "zen-pipeline"))] |
| 71 | +fn jpeg_decoder_raw_pixel_comparison() { |
| 72 | + // Load a test JPEG |
| 73 | + let jpeg_path = Path::new(env!("CARGO_MANIFEST_DIR")) |
| 74 | + .parent().unwrap() |
| 75 | + .join(".image-cache/sources/imageflow-resources/test_inputs/wide-gamut/rec-2020-pq/flickr_2a68670c58131566.jpg"); |
| 76 | + |
| 77 | + if !jpeg_path.exists() { |
| 78 | + eprintln!("skipping: test image not cached at {}", jpeg_path.display()); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + let jpeg_bytes = std::fs::read(&jpeg_path).unwrap(); |
| 83 | + |
| 84 | + let (mozjpeg_pixels, mw, mh) = decode_mozjpeg(&jpeg_bytes); |
| 85 | + let (zenjpeg_pixels, zw, zh) = decode_zenjpeg(&jpeg_bytes); |
| 86 | + |
| 87 | + assert_eq!((mw, mh), (zw, zh), "dimensions differ"); |
| 88 | + |
| 89 | + let w = mw as usize; |
| 90 | + let h = mh as usize; |
| 91 | + |
| 92 | + // mozjpeg is BGRA (4 bytes), zenjpeg is RGB (3 bytes) |
| 93 | + // Compare R,G,B channels only |
| 94 | + let moz_bpp = 4; |
| 95 | + let zen_bpp = zenjpeg_pixels.len() / (w * h); |
| 96 | + eprintln!("mozjpeg: {} bytes ({}bpp), zenjpeg: {} bytes ({}bpp), {}x{}", |
| 97 | + mozjpeg_pixels.len(), moz_bpp, zenjpeg_pixels.len(), zen_bpp, w, h); |
| 98 | + |
| 99 | + let mut max_delta = [0u8; 3]; |
| 100 | + let mut sum_delta = [0u64; 3]; |
| 101 | + let mut diff_count = 0u64; |
| 102 | + let total = (w * h) as u64; |
| 103 | + |
| 104 | + for y in 0..h { |
| 105 | + for x in 0..w { |
| 106 | + let moff = (y * w + x) * moz_bpp; |
| 107 | + let zoff = (y * w + x) * zen_bpp; |
| 108 | + |
| 109 | + // mozjpeg is BGRA: [B, G, R, A] |
| 110 | + let mr = mozjpeg_pixels[moff + 2]; |
| 111 | + let mg = mozjpeg_pixels[moff + 1]; |
| 112 | + let mb = mozjpeg_pixels[moff + 0]; |
| 113 | + |
| 114 | + // zenjpeg is RGB: [R, G, B] |
| 115 | + let zr = zenjpeg_pixels[zoff + 0]; |
| 116 | + let zg = zenjpeg_pixels[zoff + 1]; |
| 117 | + let zb = zenjpeg_pixels[zoff + 2]; |
| 118 | + |
| 119 | + let dr = mr.abs_diff(zr); |
| 120 | + let dg = mg.abs_diff(zg); |
| 121 | + let db = mb.abs_diff(zb); |
| 122 | + |
| 123 | + if dr > max_delta[0] { max_delta[0] = dr; } |
| 124 | + if dg > max_delta[1] { max_delta[1] = dg; } |
| 125 | + if db > max_delta[2] { max_delta[2] = db; } |
| 126 | + |
| 127 | + sum_delta[0] += dr as u64; |
| 128 | + sum_delta[1] += dg as u64; |
| 129 | + sum_delta[2] += db as u64; |
| 130 | + |
| 131 | + if dr > 1 || dg > 1 || db > 1 { |
| 132 | + diff_count += 1; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + let avg_r = sum_delta[0] as f64 / total as f64; |
| 138 | + let avg_g = sum_delta[1] as f64 / total as f64; |
| 139 | + let avg_b = sum_delta[2] as f64 / total as f64; |
| 140 | + |
| 141 | + eprintln!("=== JPEG DECODER COMPARISON (before CMS) ==="); |
| 142 | + eprintln!("Max delta: R={} G={} B={}", max_delta[0], max_delta[1], max_delta[2]); |
| 143 | + eprintln!("Avg delta: R={:.2} G={:.2} B={:.2}", avg_r, avg_g, avg_b); |
| 144 | + eprintln!("Pixels > 1: {}/{} ({:.1}%)", diff_count, total, diff_count as f64 / total as f64 * 100.0); |
| 145 | + |
| 146 | + // This test DOCUMENTS the decoder difference — it's expected to show |
| 147 | + // some delta. The key question is whether the delta explains the |
| 148 | + // post-CMS ICC test failures (delta ~186 for Rec.2020). |
| 149 | + // |
| 150 | + // If max_delta here is ~1-2, then the CMS amplifies it to 186. |
| 151 | + // If max_delta here is ~50+, then the decoder diff is the primary cause. |
| 152 | +} |
0 commit comments