jpegli-rs 0.12.0

Pure Rust JPEG encoder/decoder - port of Google's jpegli with perceptual optimizations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! Multi-decoder compatibility test.
//!
//! Tests that JPEGs encoded by jpegli-rs can be correctly decoded by major
//! JPEG decoders in the Rust ecosystem, and that the quality metrics are
//! appropriate for the encoding quality level.
//!
//! Decoders tested:
//! - jpegli-rs (our decoder, 12-bit precision f32 pipeline)
//! - zune-jpeg (fastest pure Rust decoder, integer IDCT)
//!
//! Run with:
//! ```
//! cargo test --test multi_decoder_compatibility -- --nocapture
//! ```

use butteraugli::{compute_butteraugli, ButteraugliParams};
use dssim::Dssim;
use jpegli::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};
use rgb::RGBA8;
use std::collections::HashMap;

/// Test configuration for a single encoding
#[derive(Clone, Debug)]
struct TestEncodingConfig {
    quality: u8,
    subsampling: ChromaSubsampling,
    progressive: bool,
    name: String,
}

/// Helper to encode RGB data with v2 encoder API
fn encode_rgb(
    width: u32,
    height: u32,
    data: &[u8],
    config: &EncoderConfig,
) -> jpegli::encoder::Result<Vec<u8>> {
    let mut enc = config.encode_from_bytes(width, height, PixelLayout::Rgb8Srgb)?;
    enc.push_packed(data, enough::Unstoppable)?;
    enc.finish()
}

/// Result from decoding with a specific decoder
struct DecoderResult {
    decoder_name: String,
    pixels: Vec<u8>,
    width: usize,
    height: usize,
    decode_time_us: u64,
}

/// Quality thresholds based on encoding quality level
/// Lower quality = higher allowed butteraugli score
/// Note: jpegli uses adaptive quantization which can produce higher butteraugli
/// scores than raw quality suggests, especially for synthetic test images.
fn max_butteraugli_for_quality(quality: u8) -> f64 {
    match quality {
        95..=100 => 1.5, // Very high quality
        90..=94 => 2.0,  // High quality
        80..=89 => 2.5,  // Good quality
        70..=79 => 3.5,  // Medium quality
        50..=69 => 5.0,  // Low quality
        _ => 8.0,        // Very low quality
    }
}

/// Maximum allowed pixel difference between decoders (per channel)
/// Small differences are expected due to rounding in IDCT implementations.
/// jpegli uses 12-bit f32 IDCT while others use integer IDCT.
const MAX_PIXEL_DIFF: u8 = 4;

/// Maximum allowed DSSIM between different decoders for same JPEG
/// Should be very close since they're decoding the same bitstream
const MAX_INTER_DECODER_DSSIM: f64 = 0.0005;

fn rgb_to_rgba(data: &[u8]) -> Vec<RGBA8> {
    data.chunks(3)
        .map(|c| RGBA8::new(c[0], c[1], c[2], 255))
        .collect()
}

fn compute_dssim(a: &[u8], b: &[u8], width: usize, height: usize) -> f64 {
    let attr = Dssim::new();
    let a_rgba = rgb_to_rgba(a);
    let b_rgba = rgb_to_rgba(b);
    let a_img = attr.create_image_rgba(&a_rgba, width, height).unwrap();
    let b_img = attr.create_image_rgba(&b_rgba, width, height).unwrap();
    let (dssim, _) = attr.compare(&a_img, b_img);
    dssim.into()
}

fn compute_butteraugli_score(original: &[u8], decoded: &[u8], width: usize, height: usize) -> f64 {
    let params = ButteraugliParams::default();
    match compute_butteraugli(original, decoded, width, height, &params) {
        Ok(result) => result.score,
        Err(_) => 99.0, // Return high score on error
    }
}

fn max_pixel_diff(a: &[u8], b: &[u8]) -> u8 {
    a.iter()
        .zip(b.iter())
        .map(|(&x, &y)| x.abs_diff(y))
        .max()
        .unwrap_or(0)
}

// Decoder implementations

fn decode_jpegli(data: &[u8]) -> Option<DecoderResult> {
    let start = std::time::Instant::now();
    let decoder = jpegli::decoder::Decoder::new();
    match decoder.decode(data) {
        Ok(img) => Some(DecoderResult {
            decoder_name: "jpegli-rs".to_string(),
            pixels: img.data,
            width: img.width as usize,
            height: img.height as usize,
            decode_time_us: start.elapsed().as_micros() as u64,
        }),
        Err(e) => {
            eprintln!("jpegli-rs decode failed: {}", e);
            None
        }
    }
}

fn decode_zune_jpeg(data: &[u8]) -> Option<DecoderResult> {
    use zune_jpeg::zune_core::bytestream::ZCursor;
    use zune_jpeg::JpegDecoder;

    let start = std::time::Instant::now();
    let cursor = ZCursor::new(data);
    let mut decoder = JpegDecoder::new(cursor);
    match decoder.decode() {
        Ok(pixels) => {
            let (width, height) = decoder.dimensions().unwrap();
            Some(DecoderResult {
                decoder_name: "zune-jpeg".to_string(),
                pixels,
                width,
                height,
                decode_time_us: start.elapsed().as_micros() as u64,
            })
        }
        Err(e) => {
            eprintln!("zune-jpeg decode failed: {:?}", e);
            None
        }
    }
}

/// Generate a test image with gradients and patterns
fn generate_test_image(width: usize, height: usize) -> Vec<u8> {
    let mut pixels = vec![0u8; width * height * 3];
    for y in 0..height {
        for x in 0..width {
            let idx = (y * width + x) * 3;
            // Horizontal gradient for R
            pixels[idx] = (x * 255 / width) as u8;
            // Vertical gradient for G
            pixels[idx + 1] = (y * 255 / height) as u8;
            // Diagonal pattern for B
            pixels[idx + 2] = ((x + y) * 127 / (width + height)) as u8;
        }
    }
    pixels
}

/// Generate a more complex test image with high-frequency content
fn generate_complex_test_image(width: usize, height: usize) -> Vec<u8> {
    let mut pixels = vec![0u8; width * height * 3];
    for y in 0..height {
        for x in 0..width {
            let idx = (y * width + x) * 3;
            // Checkerboard pattern with gradients
            let checker = ((x / 8) + (y / 8)) % 2 == 0;
            let base = if checker { 200 } else { 55 };

            pixels[idx] = (base + (x % 56)) as u8;
            pixels[idx + 1] = (base + (y % 56)) as u8;
            pixels[idx + 2] = (base + ((x + y) % 56)) as u8;
        }
    }
    pixels
}

/// Test all decoders against a single encoded JPEG
fn test_all_decoders(
    original: &[u8],
    jpeg_data: &[u8],
    width: usize,
    height: usize,
    config: &TestEncodingConfig,
) -> HashMap<String, (f64, f64, u64)> {
    // (butteraugli, inter_decoder_dssim, decode_time_us)
    let mut results: HashMap<String, (f64, f64, u64)> = HashMap::new();

    // Collect all decoder results
    let decoder_fns: Vec<(&str, fn(&[u8]) -> Option<DecoderResult>)> = vec![
        ("jpegli-rs", decode_jpegli),
        ("zune-jpeg", decode_zune_jpeg),
    ];

    let mut decoder_results: Vec<DecoderResult> = Vec::new();

    for (name, decoder_fn) in &decoder_fns {
        match decoder_fn(jpeg_data) {
            Some(result) => {
                assert_eq!(
                    result.width, width,
                    "{}: width mismatch for {}",
                    name, config.name
                );
                assert_eq!(
                    result.height, height,
                    "{}: height mismatch for {}",
                    name, config.name
                );
                decoder_results.push(result);
            }
            None => {
                eprintln!("  {} failed to decode {}", name, config.name);
            }
        }
    }

    if decoder_results.is_empty() {
        panic!("No decoder could decode {}", config.name);
    }

    // Use first successful decoder as reference for inter-decoder comparison
    let reference = &decoder_results[0];

    for result in &decoder_results {
        // Compute butteraugli against original
        let butteraugli = compute_butteraugli_score(original, &result.pixels, width, height);

        // Compute DSSIM against reference decoder
        let inter_dssim = if result.decoder_name == reference.decoder_name {
            0.0
        } else {
            compute_dssim(&reference.pixels, &result.pixels, width, height)
        };

        // Check max pixel difference against reference
        if result.decoder_name != reference.decoder_name {
            let max_diff = max_pixel_diff(&reference.pixels, &result.pixels);
            if max_diff > MAX_PIXEL_DIFF {
                eprintln!(
                    "  WARNING: {} vs {} max pixel diff: {} (threshold: {})",
                    result.decoder_name, reference.decoder_name, max_diff, MAX_PIXEL_DIFF
                );
            }
        }

        results.insert(
            result.decoder_name.clone(),
            (butteraugli, inter_dssim, result.decode_time_us),
        );
    }

    results
}

/// Main test: encode with various configurations, decode with all decoders
#[test]
fn test_multi_decoder_compatibility() {
    let width = 256;
    let height = 256;
    let original = generate_test_image(width, height);

    let configs = vec![
        TestEncodingConfig {
            quality: 95,
            subsampling: ChromaSubsampling::None,
            progressive: false,
            name: "Q95_444_baseline".to_string(),
        },
        TestEncodingConfig {
            quality: 90,
            subsampling: ChromaSubsampling::None,
            progressive: false,
            name: "Q90_444_baseline".to_string(),
        },
        TestEncodingConfig {
            quality: 80,
            subsampling: ChromaSubsampling::None,
            progressive: false,
            name: "Q80_444_baseline".to_string(),
        },
        TestEncodingConfig {
            quality: 70,
            subsampling: ChromaSubsampling::Quarter,
            progressive: false,
            name: "Q70_420_baseline".to_string(),
        },
        TestEncodingConfig {
            quality: 50,
            subsampling: ChromaSubsampling::Quarter,
            progressive: false,
            name: "Q50_420_baseline".to_string(),
        },
        TestEncodingConfig {
            quality: 90,
            subsampling: ChromaSubsampling::None,
            progressive: true,
            name: "Q90_444_progressive".to_string(),
        },
        TestEncodingConfig {
            quality: 80,
            subsampling: ChromaSubsampling::HalfHorizontal,
            progressive: false,
            name: "Q80_422_baseline".to_string(),
        },
        TestEncodingConfig {
            quality: 80,
            subsampling: ChromaSubsampling::HalfVertical,
            progressive: false,
            name: "Q80_440_baseline".to_string(),
        },
    ];

    println!("\n=== Multi-Decoder Compatibility Test ===\n");
    println!("{:<25} {:>12} {:>12}", "Config", "jpegli-rs", "zune-jpeg");
    println!("{}", "-".repeat(55));

    let mut all_passed = true;

    for config in &configs {
        // Encode with jpegli-rs
        let encoder_config = EncoderConfig::ycbcr(config.quality as f32, config.subsampling)
            .progressive(config.progressive);
        let jpeg_data = encode_rgb(width as u32, height as u32, &original, &encoder_config)
            .expect("jpegli encode failed");

        let results = test_all_decoders(&original, &jpeg_data, width, height, config);

        // Print butteraugli scores
        let max_allowed = max_butteraugli_for_quality(config.quality);

        print!("{:<25}", config.name);
        for decoder_name in &["jpegli-rs", "zune-jpeg"] {
            if let Some((butteraugli, _, _)) = results.get(*decoder_name) {
                let status = if *butteraugli <= max_allowed {
                    "OK"
                } else {
                    "FAIL"
                };
                print!(" {:>10.3} {}", butteraugli, status);
                if *butteraugli > max_allowed {
                    all_passed = false;
                }
            } else {
                print!(" {:>12}", "N/A");
            }
        }
        println!();

        // Check inter-decoder consistency
        for (decoder_name, (_, inter_dssim, _)) in &results {
            if *inter_dssim > MAX_INTER_DECODER_DSSIM {
                eprintln!(
                    "  WARNING: {} inter-decoder DSSIM {:.6} > threshold {:.6} for {}",
                    decoder_name, inter_dssim, MAX_INTER_DECODER_DSSIM, config.name
                );
            }
        }
    }

    println!("\n{}", "-".repeat(85));
    println!(
        "Butteraugli thresholds by quality: Q95={}, Q90={}, Q80={}, Q70={}, Q50={}",
        max_butteraugli_for_quality(95),
        max_butteraugli_for_quality(90),
        max_butteraugli_for_quality(80),
        max_butteraugli_for_quality(70),
        max_butteraugli_for_quality(50),
    );

    assert!(
        all_passed,
        "Some decoder/quality combinations failed butteraugli threshold"
    );
}

/// Test with a more complex image that stresses the decoders
#[test]
fn test_multi_decoder_complex_image() {
    let width = 512;
    let height = 384;
    let original = generate_complex_test_image(width, height);

    println!("\n=== Complex Image Decoder Test ===\n");

    let configs = vec![
        TestEncodingConfig {
            quality: 90,
            subsampling: ChromaSubsampling::None,
            progressive: false,
            name: "complex_Q90_444".to_string(),
        },
        TestEncodingConfig {
            quality: 75,
            subsampling: ChromaSubsampling::Quarter,
            progressive: false,
            name: "complex_Q75_420".to_string(),
        },
    ];

    for config in &configs {
        let encoder_config = EncoderConfig::ycbcr(config.quality as f32, config.subsampling)
            .progressive(config.progressive);
        let jpeg_data = encode_rgb(width as u32, height as u32, &original, &encoder_config)
            .expect("jpegli encode failed");

        println!("{}: {} bytes", config.name, jpeg_data.len());

        let results = test_all_decoders(&original, &jpeg_data, width, height, config);
        let max_allowed = max_butteraugli_for_quality(config.quality);

        for (decoder_name, (butteraugli, inter_dssim, time_us)) in &results {
            let status = if *butteraugli <= max_allowed {
                "OK"
            } else {
                "FAIL"
            };
            println!(
                "  {:<15}: butteraugli={:.3} {} inter_dssim={:.6} time={}us",
                decoder_name, butteraugli, status, inter_dssim, time_us
            );
            assert!(
                *butteraugli <= max_allowed,
                "{} butteraugli {:.3} > threshold {:.3} for {}",
                decoder_name,
                butteraugli,
                max_allowed,
                config.name
            );
        }
    }
}

/// Benchmark decoder speeds
#[test]
#[ignore] // Run with --ignored for benchmark
fn benchmark_decoders() {
    let width = 1024;
    let height = 768;
    let original = generate_test_image(width, height);

    // Encode at Q85
    let encoder_config = EncoderConfig::ycbcr(85.0, ChromaSubsampling::Quarter);
    let jpeg_data =
        encode_rgb(width as u32, height as u32, &original, &encoder_config).expect("encode failed");

    println!(
        "\n=== Decoder Benchmark ({}x{}, {} bytes) ===\n",
        width,
        height,
        jpeg_data.len()
    );

    let iterations = 50;
    let decoder_fns: Vec<(&str, fn(&[u8]) -> Option<DecoderResult>)> = vec![
        ("jpegli-rs", decode_jpegli),
        ("zune-jpeg", decode_zune_jpeg),
    ];

    for (name, decoder_fn) in &decoder_fns {
        let start = std::time::Instant::now();
        for _ in 0..iterations {
            let _ = decoder_fn(&jpeg_data);
        }
        let elapsed = start.elapsed();
        let avg_us = elapsed.as_micros() as f64 / iterations as f64;
        let pixels = width * height;
        let mp_per_sec = (pixels as f64 / avg_us) * 1_000_000.0 / 1_000_000.0;

        println!(
            "{:<15}: {:>8.1} us/decode  {:>6.1} MP/s",
            name, avg_us, mp_per_sec
        );
    }
}

/// Test grayscale encoding/decoding compatibility
#[test]
fn test_grayscale_compatibility() {
    let width = 128;
    let height = 128;

    // Generate grayscale image (stored as RGB with R=G=B)
    let mut original = vec![0u8; width * height * 3];
    for y in 0..height {
        for x in 0..width {
            let idx = (y * width + x) * 3;
            let gray = ((x * 255 / width) + (y * 255 / height)) as u8 / 2;
            original[idx] = gray;
            original[idx + 1] = gray;
            original[idx + 2] = gray;
        }
    }

    let encoder_config = EncoderConfig::ycbcr(90.0, ChromaSubsampling::Quarter);
    let jpeg_data =
        encode_rgb(width as u32, height as u32, &original, &encoder_config).expect("encode failed");

    println!("\n=== Grayscale Compatibility Test ===\n");

    // All decoders should handle RGB that happens to be grayscale
    let decoder_fns: Vec<(&str, fn(&[u8]) -> Option<DecoderResult>)> = vec![
        ("jpegli-rs", decode_jpegli),
        ("zune-jpeg", decode_zune_jpeg),
    ];

    for (name, decoder_fn) in &decoder_fns {
        match decoder_fn(&jpeg_data) {
            Some(result) => {
                assert_eq!(result.width, width);
                assert_eq!(result.height, height);
                let butteraugli =
                    compute_butteraugli_score(&original, &result.pixels, width, height);
                println!("{:<15}: butteraugli={:.3}", name, butteraugli);
                assert!(
                    butteraugli < 2.0,
                    "{} butteraugli too high for Q90 grayscale",
                    name
                );
            }
            None => {
                panic!("{} failed to decode grayscale JPEG", name);
            }
        }
    }
}