zenpixels-convert 0.2.9

Transfer-function-aware pixel conversion, gamut mapping, and codec format negotiation for zenpixels
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
#![cfg(feature = "pipeline")]
//! Path validation tests — verify the path solver produces correct results.
//!
//! These tests check that:
//! 1. The solver returns paths for common (source, op, output) triples
//! 2. Working formats satisfy operation requirements
//! 3. Cost predictions are consistent with calibrated loss buckets
//! 4. The full matrix produces sensible results

use zenpixels_convert::pipeline::registry;
use zenpixels_convert::{AlphaMode, ChannelType, PixelDescriptor, TransferFunction};
use zenpixels_convert::{
    CodecFormats, ConversionPath, LossBucket, OpCategory, Provenance, QualityThreshold,
    generate_path_matrix, matrix_stats, optimal_path,
};

// ═══════════════════════════════════════════════════════════════════════
// Common triples: JPEG → op → JPEG
// ═══════════════════════════════════════════════════════════════════════

fn jpeg_path(op: OpCategory, threshold: QualityThreshold) -> Option<ConversionPath> {
    optimal_path(
        PixelDescriptor::RGB8_SRGB,
        Provenance::with_origin_depth(ChannelType::U8),
        op,
        PixelDescriptor::RGB8_SRGB,
        threshold,
    )
}

#[test]
fn jpeg_passthrough_jpeg_is_lossless() {
    let path = jpeg_path(OpCategory::Passthrough, QualityThreshold::Lossless);
    assert!(path.is_some(), "JPEG passthrough should find a path");
    let path = path.unwrap();
    assert_eq!(path.total_loss, 0);
    assert_eq!(path.working_format, PixelDescriptor::RGB8_SRGB);
}

#[test]
fn jpeg_resize_gentle_jpeg() {
    let path = jpeg_path(
        OpCategory::ResizeGentle,
        QualityThreshold::MaxBucket(LossBucket::Moderate),
    );
    assert!(path.is_some(), "JPEG resize Mitchell should find a path");
    let path = path.unwrap();
    // Resize gentle prefers linear f32.
    assert_eq!(path.working_format.transfer(), TransferFunction::Linear);
}

#[test]
fn jpeg_resize_sharp_jpeg() {
    let path = jpeg_path(
        OpCategory::ResizeSharp,
        QualityThreshold::MaxBucket(LossBucket::Moderate),
    );
    assert!(path.is_some(), "JPEG resize Lanczos should find a path");
    let path = path.unwrap();
    // Sharp resize requires f32 linear.
    assert_eq!(path.working_format.channel_type(), ChannelType::F32);
    assert_eq!(path.working_format.transfer(), TransferFunction::Linear);
}

#[test]
fn jpeg_blur_jpeg() {
    let path = jpeg_path(
        OpCategory::Blur,
        QualityThreshold::MaxBucket(LossBucket::Moderate),
    );
    assert!(path.is_some());
}

#[test]
fn jpeg_sharpen_jpeg() {
    let path = jpeg_path(
        OpCategory::Sharpen,
        QualityThreshold::MaxBucket(LossBucket::Moderate),
    );
    assert!(path.is_some());
}

#[test]
fn jpeg_color_matrix_jpeg() {
    let path = jpeg_path(
        OpCategory::ColorMatrix,
        QualityThreshold::MaxBucket(LossBucket::Moderate),
    );
    assert!(path.is_some(), "JPEG color matrix should find a path");
    let path = path.unwrap();
    // ColorMatrix works in sRGB u8 — should use source format directly.
    assert_eq!(path.working_format, PixelDescriptor::RGB8_SRGB);
}

// ═══════════════════════════════════════════════════════════════════════
// Common triples: PNG RGBA → op → WebP
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn png_rgba_composite_webp() {
    let path = optimal_path(
        PixelDescriptor::RGBA8_SRGB,
        Provenance::with_origin_depth(ChannelType::U8),
        OpCategory::Composite,
        PixelDescriptor::RGBA8_SRGB,
        QualityThreshold::MaxBucket(LossBucket::Moderate),
    );
    assert!(path.is_some(), "PNG RGBA composite should find a path");
    let path = path.unwrap();
    // Compositing requires premultiplied alpha in f32 linear.
    assert_eq!(path.working_format.alpha(), Some(AlphaMode::Premultiplied));
    assert_eq!(path.working_format.channel_type(), ChannelType::F32);
    assert_eq!(path.working_format.transfer(), TransferFunction::Linear);
}

#[test]
fn png_rgb_resize_webp() {
    let path = optimal_path(
        PixelDescriptor::RGB8_SRGB,
        Provenance::with_origin_depth(ChannelType::U8),
        OpCategory::ResizeGentle,
        PixelDescriptor::RGB8_SRGB,
        QualityThreshold::MaxBucket(LossBucket::Moderate),
    );
    assert!(path.is_some());
}

// ═══════════════════════════════════════════════════════════════════════
// Cross-codec paths
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn png16_passthrough_png16() {
    // 16-bit PNG passthrough should be lossless.
    let path = optimal_path(
        PixelDescriptor::RGB16_SRGB,
        Provenance::with_origin_depth(ChannelType::U16),
        OpCategory::Passthrough,
        PixelDescriptor::RGB16_SRGB,
        QualityThreshold::Lossless,
    );
    assert!(path.is_some());
    assert_eq!(path.unwrap().total_loss, 0);
}

#[test]
fn png16_to_jpeg_passthrough() {
    // 16-bit PNG → JPEG (u8): lossy (depth truncation).
    let path = optimal_path(
        PixelDescriptor::RGB16_SRGB,
        Provenance::with_origin_depth(ChannelType::U16),
        OpCategory::Passthrough,
        PixelDescriptor::RGB8_SRGB,
        QualityThreshold::Lossless,
    );
    assert!(path.is_none(), "u16→u8 should not be lossless");

    // But should work with relaxed threshold.
    let path = optimal_path(
        PixelDescriptor::RGB16_SRGB,
        Provenance::with_origin_depth(ChannelType::U16),
        OpCategory::Passthrough,
        PixelDescriptor::RGB8_SRGB,
        QualityThreshold::NearLossless,
    );
    assert!(
        path.is_some(),
        "u16→u8 should work with NearLossless threshold"
    );
}

#[test]
fn jxl_f32_resize_jxl_f32() {
    // JXL f32 → resize → JXL f32: working in f32 linear is identity for source.
    let path = optimal_path(
        PixelDescriptor::RGBF32_LINEAR,
        Provenance::with_origin_depth(ChannelType::F32),
        OpCategory::ResizeSharp,
        PixelDescriptor::RGBF32_LINEAR,
        QualityThreshold::SubPerceptual,
    );
    assert!(path.is_some());
    let path = path.unwrap();
    // Source is already f32 linear, so working format should be f32 linear.
    assert_eq!(path.working_format, PixelDescriptor::RGBF32_LINEAR);
    // No conversion cost (source already matches working format).
    assert_eq!(path.source_to_working.effort, 0);
    assert_eq!(path.source_to_working.loss, 0);
}

// ═══════════════════════════════════════════════════════════════════════
// Working format satisfies operation requirements
// ═══════════════════════════════════════════════════════════════════════

fn assert_working_satisfies_op(path: &ConversionPath, op: OpCategory) {
    let req = op.requirement();

    if let Some(tf) = req.transfer {
        assert_eq!(
            path.working_format.transfer(),
            tf,
            "working format transfer {:?} doesn't match requirement {:?} for {:?}",
            path.working_format.transfer(),
            tf,
            op
        );
    }

    if req.requires_float {
        assert_eq!(
            path.working_format.channel_type(),
            ChannelType::F32,
            "working format should be f32 for {:?}",
            op
        );
    }

    if let Some(alpha) = req.alpha
        && path.working_format.layout().has_alpha()
    {
        assert_eq!(
            path.working_format.alpha(),
            Some(alpha),
            "working format alpha {:?} doesn't match requirement {:?} for {:?}",
            path.working_format.alpha(),
            alpha,
            op
        );
    }
}

#[test]
fn all_ops_satisfy_requirements_for_jpeg() {
    let ops = [
        OpCategory::Passthrough,
        OpCategory::ResizeGentle,
        OpCategory::ResizeSharp,
        OpCategory::Blur,
        OpCategory::Sharpen,
        OpCategory::ColorMatrix,
        OpCategory::Arithmetic,
    ];

    for &op in &ops {
        let path = jpeg_path(op, QualityThreshold::MaxBucket(LossBucket::High));
        if let Some(ref p) = path {
            assert_working_satisfies_op(p, op);
        }
    }
}

#[test]
fn composite_op_satisfies_for_rgba() {
    let path = optimal_path(
        PixelDescriptor::RGBA8_SRGB,
        Provenance::from_source(PixelDescriptor::RGBA8_SRGB),
        OpCategory::Composite,
        PixelDescriptor::RGBA8_SRGB,
        QualityThreshold::MaxBucket(LossBucket::High),
    );
    assert!(path.is_some());
    assert_working_satisfies_op(path.as_ref().unwrap(), OpCategory::Composite);
}

#[test]
fn tonemap_op_satisfies() {
    let path = optimal_path(
        PixelDescriptor::RGBF32_LINEAR,
        Provenance::from_source(PixelDescriptor::RGBF32_LINEAR),
        OpCategory::Tonemap,
        PixelDescriptor::RGB8_SRGB,
        QualityThreshold::MaxBucket(LossBucket::High),
    );
    assert!(path.is_some());
    assert_working_satisfies_op(path.as_ref().unwrap(), OpCategory::Tonemap);
}

// ═══════════════════════════════════════════════════════════════════════
// Provenance-aware cost predictions
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn u8_origin_roundtrip_is_lossless_passthrough() {
    // JPEG u8 → f32 (for resize) → u8 (for JPEG encode).
    // With u8 provenance, the f32→u8 step should be lossless.
    let path = optimal_path(
        PixelDescriptor::RGB8_SRGB,
        Provenance::with_origin_depth(ChannelType::U8),
        OpCategory::ResizeGentle,
        PixelDescriptor::RGB8_SRGB,
        QualityThreshold::MaxBucket(LossBucket::NearLossless),
    );
    assert!(path.is_some());
    let path = path.unwrap();
    // The working→output conversion should have 0 loss (u8 origin round-trip).
    assert_eq!(
        path.working_to_output.loss, 0,
        "u8 origin → f32 → u8 should report 0 loss, got {}",
        path.working_to_output.loss
    );
}

#[test]
fn f32_origin_to_u8_is_lossy() {
    // True f32 data → u8 should report loss.
    let path = optimal_path(
        PixelDescriptor::RGBF32_LINEAR,
        Provenance::with_origin_depth(ChannelType::F32),
        OpCategory::Passthrough,
        PixelDescriptor::RGB8_SRGB,
        QualityThreshold::MaxBucket(LossBucket::Moderate),
    );
    assert!(path.is_some());
    let path = path.unwrap();
    assert!(
        path.total_loss > 0,
        "f32→u8 with f32 origin should be lossy"
    );
}

// ═══════════════════════════════════════════════════════════════════════
// Full matrix generation
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn full_matrix_with_all_codecs_and_three_ops() {
    let ops = [
        OpCategory::Passthrough,
        OpCategory::ResizeGentle,
        OpCategory::ResizeSharp,
    ];
    let codecs: Vec<&CodecFormats> = registry::ALL_CODECS.to_vec();
    let matrix = generate_path_matrix(
        &codecs,
        &ops,
        &codecs,
        QualityThreshold::MaxBucket(LossBucket::High),
    );
    let stats = matrix_stats(&matrix);

    // Sanity checks.
    assert!(
        stats.total_triples > 1000,
        "expected >1000 triples, got {}",
        stats.total_triples
    );
    assert!(
        stats.paths_found > 500,
        "expected >500 paths, got {}",
        stats.paths_found
    );

    // Most passthrough identity triples should be lossless.
    let passthrough_lossless = matrix
        .iter()
        .filter(|e| e.operation == OpCategory::Passthrough)
        .filter(|e| e.source_format == e.output_format)
        .filter(|e| e.path.as_ref().is_some_and(|p| p.total_loss == 0))
        .count();
    assert!(
        passthrough_lossless > 0,
        "some passthrough identities should be lossless"
    );

    // Print summary for debugging.
    println!(
        "Full matrix: {} triples, {} paths found, {} no path",
        stats.total_triples, stats.paths_found, stats.no_path
    );
    println!(
        "  By bucket: Lossless={}, NearLossless={}, LowLoss={}, Moderate={}, High={}",
        stats.by_bucket[0],
        stats.by_bucket[1],
        stats.by_bucket[2],
        stats.by_bucket[3],
        stats.by_bucket[4]
    );
    println!(
        "  Distinct working formats: {}",
        stats.distinct_working_formats
    );
}

#[test]
fn full_matrix_all_13_ops() {
    let all_ops = [
        OpCategory::Passthrough,
        OpCategory::ResizeGentle,
        OpCategory::ResizeSharp,
        OpCategory::Blur,
        OpCategory::Sharpen,
        OpCategory::OklabSharpen,
        OpCategory::Composite,
        OpCategory::OklabAdjust,
        OpCategory::ColorMatrix,
        OpCategory::Tonemap,
        OpCategory::IccTransform,
        OpCategory::Quantize,
        OpCategory::Arithmetic,
    ];

    // Use a subset of codecs for speed (JPEG, PNG, WebP cover the main cases).
    let codecs: Vec<&CodecFormats> = vec![&registry::JPEG, &registry::PNG, &registry::WEBP];
    let matrix = generate_path_matrix(
        &codecs,
        &all_ops,
        &codecs,
        QualityThreshold::MaxBucket(LossBucket::High),
    );
    let stats = matrix_stats(&matrix);

    println!(
        "All ops matrix: {} triples, {} paths, {} no path",
        stats.total_triples, stats.paths_found, stats.no_path
    );

    // Every operation should find at least some valid paths.
    for &op in &all_ops {
        let op_paths = matrix
            .iter()
            .filter(|e| e.operation == op)
            .filter(|e| e.path.is_some())
            .count();
        assert!(
            op_paths > 0,
            "OpCategory {:?} should find at least one valid path",
            op
        );
    }
}

// ═══════════════════════════════════════════════════════════════════════
// Regression: loss bucket consistency
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn lossless_paths_have_zero_total_loss() {
    let codecs: Vec<&CodecFormats> = registry::ALL_CODECS.to_vec();
    let matrix = generate_path_matrix(
        &codecs,
        &[OpCategory::Passthrough],
        &codecs,
        QualityThreshold::Lossless,
    );

    for entry in &matrix {
        if let Some(ref path) = entry.path {
            assert_eq!(
                path.total_loss,
                0,
                "path from {} {:?} → {} {:?} marked lossless but has loss {}",
                entry.source_codec,
                entry.source_format,
                entry.output_codec,
                entry.output_format,
                path.total_loss
            );
        }
    }
}

#[test]
fn sub_perceptual_paths_within_threshold() {
    let matrix = generate_path_matrix(
        &[&registry::JPEG, &registry::PNG],
        &[OpCategory::Passthrough, OpCategory::ResizeGentle],
        &[&registry::JPEG, &registry::PNG],
        QualityThreshold::SubPerceptual,
    );

    for entry in &matrix {
        if let Some(ref path) = entry.path {
            assert!(
                path.total_loss <= 10,
                "sub-perceptual path has loss {} > 10 for {:?} → {:?} → {:?}",
                path.total_loss,
                entry.source_format,
                entry.operation,
                entry.output_format
            );
        }
    }
}