zenraw 0.1.2

Camera RAW and DNG decoder with zenpixels integration
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
//! Per-format integration tests using raw.pixls.us sample corpus.
//!
//! Each test exercises a specific RAW format with probe, decode, and
//! optional EXIF extraction. Tests are skipped if the sample file is
//! not present (run `just fetch-samples` first).

use enough::Unstoppable;
use zenraw::{OutputMode, RawDecodeConfig, RawDecodeOutput};

const SAMPLES_DIR: &str = "/mnt/v/input/raw-samples";

/// Try to read a sample file, returning None if it doesn't exist.
fn load_sample(name: &str) -> Option<Vec<u8>> {
    let path = format!("{SAMPLES_DIR}/{name}");
    std::fs::read(&path).ok()
}

/// Decode with linear f32 config (scene-referred linear f32).
fn decode_linear(data: &[u8]) -> RawDecodeOutput {
    let config = RawDecodeConfig::new().with_output(OutputMode::Linear);
    zenraw::decode(data, &config, &Unstoppable).expect("decode should succeed")
}

/// Decode with sRGB (display-referred u16).
fn decode_srgb(data: &[u8]) -> RawDecodeOutput {
    let config = RawDecodeConfig::new().with_output(OutputMode::Develop);
    zenraw::decode(data, &config, &Unstoppable).expect("sRGB decode should succeed")
}

/// Verify basic output sanity: non-zero dimensions, reasonable pixel values.
fn verify_output(output: &RawDecodeOutput, format_name: &str) {
    assert!(output.info.width > 0, "{format_name}: zero width");
    assert!(output.info.height > 0, "{format_name}: zero height");
    assert!(
        !output.info.make.is_empty(),
        "{format_name}: empty camera make"
    );
    assert!(
        !output.info.model.is_empty(),
        "{format_name}: empty camera model"
    );

    let bytes = output.pixels.copy_to_contiguous_bytes();
    assert!(!bytes.is_empty(), "{format_name}: empty pixel data");

    eprintln!(
        "  {format_name}: {}x{} {} {} (CFA={}, orientation={})",
        output.info.width,
        output.info.height,
        output.info.make,
        output.info.model,
        output.info.cfa_pattern,
        output.info.orientation
    );
}

/// Verify linear f32 output has reasonable pixel statistics.
fn verify_linear_stats(output: &RawDecodeOutput, format_name: &str) {
    assert_eq!(
        output.pixels.descriptor(),
        zenpixels::PixelDescriptor::RGBF32_LINEAR,
        "{format_name}: expected RGBF32_LINEAR"
    );

    let bytes = output.pixels.copy_to_contiguous_bytes();
    let floats: Vec<f32> = bytes
        .chunks_exact(4)
        .map(|c| f32::from_ne_bytes([c[0], c[1], c[2], c[3]]))
        .collect();

    let mean: f32 = floats.iter().sum::<f32>() / floats.len() as f32;
    let max: f32 = floats.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
    let min: f32 = floats.iter().cloned().fold(f32::INFINITY, f32::min);

    eprintln!("  {format_name} stats: mean={mean:.4} min={min:.4} max={max:.4}");

    assert!(
        mean > 0.001,
        "{format_name}: output is all black (mean={mean})"
    );
    assert!(max <= 1.0, "{format_name}: values exceed 1.0 (max={max})");
    assert!(min >= 0.0, "{format_name}: negative values (min={min})");
}

/// Verify sRGB u16 output.
fn verify_srgb_output(output: &RawDecodeOutput, format_name: &str) {
    assert_eq!(
        output.pixels.descriptor(),
        zenpixels::PixelDescriptor::RGB16_SRGB,
        "{format_name}: expected RGB16_SRGB"
    );

    let bytes = output.pixels.copy_to_contiguous_bytes();
    let pixel_count = output.info.width as usize * output.info.height as usize;
    assert_eq!(
        bytes.len(),
        pixel_count * 6, // 3 channels × 2 bytes per u16
        "{format_name}: wrong byte count for sRGB u16"
    );

    // Check not all black or all white — interpret as u16
    let samples: Vec<u16> = bytes
        .chunks_exact(2)
        .map(|c| u16::from_ne_bytes([c[0], c[1]]))
        .collect();
    let sum: u64 = samples.iter().map(|&s| s as u64).sum();
    let mean = sum as f64 / samples.len() as f64;
    eprintln!("  {format_name} sRGB: mean={mean:.1}/65535");
    assert!(
        mean > 256.0,
        "{format_name}: sRGB output is all black (mean={mean})"
    );
    assert!(
        mean < 65279.0,
        "{format_name}: sRGB output is all white (mean={mean})"
    );
}

// ── Per-format tests ─────────────────────────────────────────────────

#[test]
fn format_nikon_nef() {
    let Some(data) = load_sample("nikon_d40.nef") else {
        eprintln!("Skipping: nikon_d40.nef not found (run `just fetch-samples`)");
        return;
    };

    // Probe
    let info = zenraw::probe(&data, &Unstoppable).expect("probe NEF");
    assert!(info.width > 0);
    assert!(info.height > 0);
    assert!(!info.is_dng);
    eprintln!(
        "NEF probe: {}x{} {} {}",
        info.width, info.height, info.make, info.model
    );

    // Linear decode
    let output = decode_linear(&data);
    verify_output(&output, "NEF");
    verify_linear_stats(&output, "NEF");

    // sRGB decode
    let srgb = decode_srgb(&data);
    verify_srgb_output(&srgb, "NEF");
}

#[test]
fn format_canon_cr2() {
    let Some(data) = load_sample("canon_350d.cr2") else {
        eprintln!("Skipping: canon_350d.cr2 not found");
        return;
    };

    let info = zenraw::probe(&data, &Unstoppable).expect("probe CR2");
    assert!(!info.is_dng);
    eprintln!(
        "CR2 probe: {}x{} {} {}",
        info.width, info.height, info.make, info.model
    );

    let output = decode_linear(&data);
    verify_output(&output, "CR2");
    verify_linear_stats(&output, "CR2");

    let srgb = decode_srgb(&data);
    verify_srgb_output(&srgb, "CR2");
}

#[test]
fn format_sony_arw() {
    let Some(data) = load_sample("sony_nex3.arw") else {
        eprintln!("Skipping: sony_nex3.arw not found");
        return;
    };

    let info = zenraw::probe(&data, &Unstoppable).expect("probe ARW");
    assert!(!info.is_dng);
    eprintln!(
        "ARW probe: {}x{} {} {}",
        info.width, info.height, info.make, info.model
    );

    let output = decode_linear(&data);
    verify_output(&output, "ARW");
    verify_linear_stats(&output, "ARW");

    let srgb = decode_srgb(&data);
    verify_srgb_output(&srgb, "ARW");
}

#[test]
fn format_olympus_orf() {
    let Some(data) = load_sample("olympus_c5050z.orf") else {
        eprintln!("Skipping: olympus_c5050z.orf not found");
        return;
    };

    let info = zenraw::probe(&data, &Unstoppable).expect("probe ORF");
    assert!(!info.is_dng);
    eprintln!(
        "ORF probe: {}x{} {} {}",
        info.width, info.height, info.make, info.model
    );

    let output = decode_linear(&data);
    verify_output(&output, "ORF");
    verify_linear_stats(&output, "ORF");

    let srgb = decode_srgb(&data);
    verify_srgb_output(&srgb, "ORF");
}

#[test]
fn format_panasonic_rw2() {
    let Some(data) = load_sample("panasonic_gf1.rw2") else {
        eprintln!("Skipping: panasonic_gf1.rw2 not found");
        return;
    };

    let info = zenraw::probe(&data, &Unstoppable).expect("probe RW2");
    assert!(!info.is_dng);
    eprintln!(
        "RW2 probe: {}x{} {} {}",
        info.width, info.height, info.make, info.model
    );

    let output = decode_linear(&data);
    verify_output(&output, "RW2");
    verify_linear_stats(&output, "RW2");

    let srgb = decode_srgb(&data);
    verify_srgb_output(&srgb, "RW2");
}

#[test]
fn format_dng_iphone() {
    let Some(data) = load_sample("iphone12pro.dng") else {
        eprintln!("Skipping: iphone12pro.dng not found");
        return;
    };

    // iPhone ProRAW DNGs use LJPEG predictor 7 which rawloader doesn't support.
    // This test verifies we return a clean error rather than panicking.
    match zenraw::probe(&data, &Unstoppable) {
        Ok(info) => {
            assert!(info.is_dng);
            eprintln!(
                "iPhone DNG probe: {}x{} {} {}",
                info.width, info.height, info.make, info.model
            );

            let output = decode_linear(&data);
            verify_output(&output, "iPhone DNG");
            verify_linear_stats(&output, "iPhone DNG");

            let srgb = decode_srgb(&data);
            verify_srgb_output(&srgb, "iPhone DNG");
        }
        Err(e) => {
            eprintln!("iPhone DNG probe failed (expected without rawler): {e}");
        }
    }
}

#[test]
fn format_apple_proraw_batch() {
    // Test all Apple ProRAW DNGs from /mnt/v/heic/
    let dir = "/mnt/v/heic/";
    let Ok(entries) = std::fs::read_dir(dir) else {
        eprintln!("Skipping: /mnt/v/heic/ not found");
        return;
    };

    let mut tested = 0;
    for entry in entries.filter_map(|e| e.ok()) {
        let path = entry.path();
        if !path
            .extension()
            .is_some_and(|e| e.eq_ignore_ascii_case("dng"))
        {
            continue;
        }

        let Ok(data) = std::fs::read(&path) else {
            continue;
        };
        let name = path.file_name().unwrap().to_str().unwrap().to_string();

        // Skip tiny stubs
        if data.len() < 1024 {
            eprintln!("{name}: skipping (only {} bytes)", data.len());
            continue;
        }

        // Check if it's actually a TIFF-based DNG (not JPEG-wrapped AMPF)
        if !zenraw::is_raw_file(&data) {
            eprintln!("{name}: not a RAW/DNG file (JPEG wrapper?)");
            continue;
        }

        // Probe
        match zenraw::probe(&data, &Unstoppable) {
            Ok(info) => {
                assert!(info.is_dng, "{name}: expected DNG");
                eprintln!(
                    "{name}: {}x{} {} {} (sensor={}x{}, orient={})",
                    info.width,
                    info.height,
                    info.make,
                    info.model,
                    info.sensor_width,
                    info.sensor_height,
                    info.orientation
                );

                // Full decode
                let output = decode_linear(&data);
                verify_output(&output, &name);
                verify_linear_stats(&output, &name);

                // sRGB decode
                let srgb = decode_srgb(&data);
                verify_srgb_output(&srgb, &name);

                tested += 1;
            }
            Err(e) => {
                eprintln!("{name}: probe failed: {e}");
            }
        }
    }

    if tested == 0 {
        eprintln!("Skipping: no decodable Apple ProRAW DNGs found");
    } else {
        eprintln!("Decoded {tested} Apple ProRAW DNGs successfully");
    }
}

#[test]
fn format_android_dng() {
    let path = std::path::Path::new("/mnt/v/heic/android/20260220_093521.dng");
    let Ok(data) = std::fs::read(path) else {
        eprintln!("Skipping: Android DNG not found");
        return;
    };

    if !zenraw::is_raw_file(&data) {
        eprintln!("Skipping: not a RAW file");
        return;
    }

    let info = zenraw::probe(&data, &Unstoppable).expect("probe Android DNG");
    assert!(info.is_dng);
    eprintln!(
        "Android DNG: {}x{} {} {}",
        info.width, info.height, info.make, info.model
    );

    let output = decode_linear(&data);
    verify_output(&output, "Android DNG");
    verify_linear_stats(&output, "Android DNG");

    let srgb = decode_srgb(&data);
    verify_srgb_output(&srgb, "Android DNG");
}

#[test]
fn format_fivek_dng() {
    // Test with a FiveK corpus DNG
    let dirs = ["/mnt/v/input/fivek/dng/"];
    let mut data = None;
    for dir in &dirs {
        let Ok(entries) = std::fs::read_dir(dir) else {
            continue;
        };
        for entry in entries.filter_map(|e| e.ok()).take(1) {
            let path = entry.path();
            if path
                .extension()
                .is_some_and(|e| e.eq_ignore_ascii_case("dng"))
            {
                data = std::fs::read(&path).ok().map(|d| (path, d));
                break;
            }
        }
    }

    let Some((path, data)) = data else {
        eprintln!("Skipping: no FiveK DNG files found");
        return;
    };

    let info = zenraw::probe(&data, &Unstoppable).expect("probe FiveK DNG");
    assert!(info.is_dng);
    eprintln!(
        "FiveK DNG probe: {} {}x{} {} {}",
        path.file_name().unwrap().to_str().unwrap(),
        info.width,
        info.height,
        info.make,
        info.model
    );

    let output = decode_linear(&data);
    verify_output(&output, "FiveK DNG");
    verify_linear_stats(&output, "FiveK DNG");
}

// ── Formats that need special handling ───────────────────────────────

#[test]
fn format_fuji_raf_xtrans() {
    let Some(data) = load_sample("fuji_xt1.raf") else {
        eprintln!("Skipping: fuji_xt1.raf not found");
        return;
    };

    // X-Trans sensor — both probe and decode should work
    let info = zenraw::probe(&data, &Unstoppable).expect("probe RAF");
    assert!(!info.is_dng);
    eprintln!(
        "RAF probe: {}x{} {} {} CFA={}",
        info.width, info.height, info.make, info.model, info.cfa_pattern
    );

    // Decode X-Trans with bilinear demosaic
    let config = RawDecodeConfig::default();
    let output = zenraw::decode(&data, &config, &Unstoppable).expect("decode RAF X-Trans");
    eprintln!(
        "RAF decoded: {}x{} orient={}",
        output.info.width, output.info.height, output.info.orientation
    );
}

#[test]
fn format_canon_cr3() {
    let Some(data) = load_sample("canon_eosr_craw.cr3") else {
        eprintln!("Skipping: canon_eosr_craw.cr3 not found");
        return;
    };

    // CR3 requires rawler (rawloader doesn't support it)
    // Probe may or may not work depending on backend
    match zenraw::probe(&data, &Unstoppable) {
        Ok(info) => {
            eprintln!(
                "CR3 probe: {}x{} {} {}",
                info.width, info.height, info.make, info.model
            );

            let output = decode_linear(&data);
            verify_output(&output, "CR3");
            verify_linear_stats(&output, "CR3");

            let srgb = decode_srgb(&data);
            verify_srgb_output(&srgb, "CR3");
        }
        Err(e) => {
            eprintln!("CR3 probe failed (expected without rawler): {e}");
        }
    }
}

// ── Cross-format consistency tests ───────────────────────────────────

#[test]
fn all_formats_probe_consistency() {
    let samples = [
        "nikon_d40.nef",
        "canon_350d.cr2",
        "sony_nex3.arw",
        "olympus_c5050z.orf",
        "panasonic_gf1.rw2",
        "iphone12pro.dng",
    ];

    let mut probed = 0;
    for name in &samples {
        let Some(data) = load_sample(name) else {
            continue;
        };

        let info = match zenraw::probe(&data, &Unstoppable) {
            Ok(info) => info,
            Err(e) => {
                // Some formats (e.g., iPhone DNG with LJPEG predictor 7)
                // may not be supported by the current backend.
                eprintln!("{name}: probe failed (skipping): {e}");
                continue;
            }
        };

        // All should have valid dimensions
        assert!(
            info.width >= 100,
            "{name}: width too small ({})",
            info.width
        );
        assert!(
            info.height >= 100,
            "{name}: height too small ({})",
            info.height
        );

        // Sensor dimensions should be >= output dimensions
        assert!(
            info.sensor_width >= info.width,
            "{name}: sensor_width < width"
        );
        assert!(
            info.sensor_height >= info.height,
            "{name}: sensor_height < height"
        );

        // Orientation should be valid EXIF (1-8)
        assert!(
            info.orientation >= 1 && info.orientation <= 8,
            "{name}: invalid orientation {}",
            info.orientation
        );

        probed += 1;
    }

    if probed == 0 {
        eprintln!("Skipping: no sample files available");
    } else {
        eprintln!("Probed {probed}/{} formats successfully", samples.len());
    }
}

#[test]
fn all_formats_bilinear_vs_malvar() {
    let samples = [
        "nikon_d40.nef",
        "canon_350d.cr2",
        "sony_nex3.arw",
        "olympus_c5050z.orf",
        "panasonic_gf1.rw2",
    ];

    for name in &samples {
        let Some(data) = load_sample(name) else {
            continue;
        };

        // Decode with both demosaic methods (Develop mode → u16 sRGB)
        let config_malvar = RawDecodeConfig::new();
        let config_bilinear =
            RawDecodeConfig::new().with_demosaic(zenraw::DemosaicMethod::Bilinear);

        let Ok(malvar) = zenraw::decode(&data, &config_malvar, &Unstoppable) else {
            continue;
        };
        let Ok(bilinear) = zenraw::decode(&data, &config_bilinear, &Unstoppable) else {
            continue;
        };

        // Same dimensions
        assert_eq!(
            malvar.info.width, bilinear.info.width,
            "{name}: width mismatch"
        );
        assert_eq!(
            malvar.info.height, bilinear.info.height,
            "{name}: height mismatch"
        );

        // Both should be sRGB u16
        assert_eq!(
            malvar.pixels.descriptor(),
            zenpixels::PixelDescriptor::RGB16_SRGB
        );
        assert_eq!(
            bilinear.pixels.descriptor(),
            zenpixels::PixelDescriptor::RGB16_SRGB
        );

        // They should differ (different algorithms) but not drastically
        let m_bytes = malvar.pixels.copy_to_contiguous_bytes();
        let b_bytes = bilinear.pixels.copy_to_contiguous_bytes();
        assert_eq!(m_bytes.len(), b_bytes.len());

        // Compare as u16 samples
        let m_samples: Vec<u16> = m_bytes
            .chunks_exact(2)
            .map(|c| u16::from_ne_bytes([c[0], c[1]]))
            .collect();
        let b_samples: Vec<u16> = b_bytes
            .chunks_exact(2)
            .map(|c| u16::from_ne_bytes([c[0], c[1]]))
            .collect();

        let diff: u64 = m_samples
            .iter()
            .zip(b_samples.iter())
            .map(|(&a, &b)| (a as i32 - b as i32).unsigned_abs() as u64)
            .sum();
        let mad = diff as f64 / m_samples.len() as f64;

        eprintln!("{name}: Malvar vs Bilinear MAD = {mad:.2} (u16 scale)");

        // They should be similar but not identical (scaled from u8 threshold: 20 * 257 ≈ 5140)
        assert!(
            mad < 5200.0,
            "{name}: demosaic methods differ too much (MAD={mad})"
        );
    }
}

// ── EXIF extraction tests ────────────────────────────────────────────

#[cfg(feature = "exif")]
mod exif_tests {
    use super::*;

    #[test]
    fn exif_nikon_nef() {
        let Some(data) = load_sample("nikon_d40.nef") else {
            eprintln!("Skipping: nikon_d40.nef not found");
            return;
        };

        let meta = zenraw::exif::read_metadata(&data).expect("EXIF from NEF");
        assert!(meta.make.is_some());
        assert!(meta.model.is_some());
        eprintln!("NEF EXIF: make={:?} model={:?}", meta.make, meta.model);
        eprintln!(
            "  ISO={:?} exposure={:?} f_number={:?}",
            meta.iso, meta.exposure_time, meta.f_number
        );
        eprintln!(
            "  orientation={:?} bits_per_sample={:?}",
            meta.orientation, meta.bits_per_sample
        );

        // NEF should NOT have DNG version
        assert!(meta.dng_version.is_none(), "NEF should not be DNG");
    }

    #[test]
    fn exif_canon_cr2() {
        let Some(data) = load_sample("canon_350d.cr2") else {
            eprintln!("Skipping: canon_350d.cr2 not found");
            return;
        };

        let meta = zenraw::exif::read_metadata(&data).expect("EXIF from CR2");
        assert!(meta.make.is_some());
        let make = meta.make.as_deref().unwrap_or("");
        assert!(
            make.to_lowercase().contains("canon"),
            "expected Canon make, got: {make}"
        );
        eprintln!("CR2 EXIF: make={:?} model={:?}", meta.make, meta.model);
        assert!(meta.dng_version.is_none());
    }

    #[test]
    fn exif_sony_arw() {
        let Some(data) = load_sample("sony_nex3.arw") else {
            eprintln!("Skipping: sony_nex3.arw not found");
            return;
        };

        let meta = zenraw::exif::read_metadata(&data).expect("EXIF from ARW");
        assert!(meta.make.is_some());
        let make = meta.make.as_deref().unwrap_or("");
        assert!(
            make.to_lowercase().contains("sony"),
            "expected Sony make, got: {make}"
        );
        eprintln!("ARW EXIF: make={:?} model={:?}", meta.make, meta.model);
    }

    #[test]
    fn exif_olympus_orf() {
        let Some(data) = load_sample("olympus_c5050z.orf") else {
            eprintln!("Skipping: olympus_c5050z.orf not found");
            return;
        };

        // kamadak-exif can't parse some ORF files (non-standard TIFF variant)
        match zenraw::exif::read_metadata(&data) {
            Some(meta) => {
                assert!(meta.make.is_some());
                eprintln!("ORF EXIF: make={:?} model={:?}", meta.make, meta.model);
            }
            None => {
                eprintln!("ORF EXIF: kamadak-exif cannot parse this ORF (expected limitation)");
            }
        }
    }

    #[test]
    fn exif_panasonic_rw2() {
        let Some(data) = load_sample("panasonic_gf1.rw2") else {
            eprintln!("Skipping: panasonic_gf1.rw2 not found");
            return;
        };

        // kamadak-exif can't parse some RW2 files (non-standard TIFF variant)
        match zenraw::exif::read_metadata(&data) {
            Some(meta) => {
                assert!(meta.make.is_some());
                let make = meta.make.as_deref().unwrap_or("");
                assert!(
                    make.to_lowercase().contains("panasonic"),
                    "expected Panasonic make, got: {make}"
                );
                eprintln!("RW2 EXIF: make={:?} model={:?}", meta.make, meta.model);
            }
            None => {
                eprintln!("RW2 EXIF: kamadak-exif cannot parse this RW2 (expected limitation)");
            }
        }
    }

    #[test]
    fn exif_iphone_dng() {
        let Some(data) = load_sample("iphone12pro.dng") else {
            eprintln!("Skipping: iphone12pro.dng not found");
            return;
        };

        let meta = zenraw::exif::read_metadata(&data).expect("EXIF from iPhone DNG");
        assert!(meta.make.is_some());
        let make = meta.make.as_deref().unwrap_or("");
        assert!(
            make.to_lowercase().contains("apple"),
            "expected Apple make, got: {make}"
        );

        // iPhone DNG should have DNG-specific fields
        assert!(
            meta.dng_version.is_some(),
            "iPhone DNG should have DNG version"
        );
        eprintln!(
            "iPhone DNG EXIF: make={:?} model={:?}",
            meta.make, meta.model
        );
        eprintln!("  DNG version: {:?}", meta.dng_version);
        eprintln!("  ColorMatrix1: {:?}", meta.color_matrix_1);
        eprintln!("  AsShotNeutral: {:?}", meta.as_shot_neutral);
        eprintln!(
            "  GPS: lat={:?} lon={:?}",
            meta.gps_latitude, meta.gps_longitude
        );
    }

    #[test]
    fn exif_apple_proraw() {
        let dir = "/mnt/v/heic/";
        let Ok(entries) = std::fs::read_dir(dir) else {
            eprintln!("Skipping: /mnt/v/heic/ not found");
            return;
        };

        for entry in entries.filter_map(|e| e.ok()) {
            let path = entry.path();
            if !path
                .extension()
                .is_some_and(|e| e.eq_ignore_ascii_case("dng"))
            {
                continue;
            }

            let Ok(data) = std::fs::read(&path) else {
                continue;
            };
            let name = path.file_name().unwrap().to_str().unwrap().to_string();

            if data.len() < 1024 || !zenraw::is_raw_file(&data) {
                continue;
            }

            match zenraw::exif::read_metadata(&data) {
                Some(meta) => {
                    assert!(meta.make.is_some());
                    let make = meta.make.as_deref().unwrap_or("");
                    assert!(
                        make.to_lowercase().contains("apple"),
                        "{name}: expected Apple make, got: {make}"
                    );
                    assert!(meta.dng_version.is_some(), "{name}: expected DNG version");
                    eprintln!(
                        "{name}: make={:?} model={:?} DNG={:?}",
                        meta.make, meta.model, meta.dng_version
                    );
                    eprintln!(
                        "  GPS: lat={:?} lon={:?} alt={:?}",
                        meta.gps_latitude, meta.gps_longitude, meta.gps_altitude
                    );
                    eprintln!(
                        "  ISO={:?} focal={:?} f_number={:?}",
                        meta.iso, meta.focal_length, meta.f_number
                    );
                }
                None => {
                    eprintln!("{name}: EXIF extraction failed");
                }
            }
        }
    }

    #[test]
    fn exif_fivek_batch() {
        let dir = "/mnt/v/input/fivek/dng/";
        let Ok(entries) = std::fs::read_dir(dir) else {
            eprintln!("Skipping: FiveK DNG directory not found");
            return;
        };

        let mut success = 0;
        let mut fail = 0;
        let mut cameras: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();

        for entry in entries.filter_map(|e| e.ok()).take(50) {
            let path = entry.path();
            if !path
                .extension()
                .is_some_and(|e| e.eq_ignore_ascii_case("dng"))
            {
                continue;
            }

            let Ok(data) = std::fs::read(&path) else {
                continue;
            };

            match zenraw::exif::read_metadata(&data) {
                Some(meta) => {
                    success += 1;
                    assert!(meta.dng_version.is_some());
                    assert!(meta.color_matrix_1.is_some());
                    if let (Some(make), Some(model)) = (&meta.make, &meta.model) {
                        cameras.insert(format!("{make} {model}"));
                    }
                }
                None => {
                    fail += 1;
                    eprintln!(
                        "WARN: failed EXIF for {}",
                        path.file_name().unwrap().to_str().unwrap()
                    );
                }
            }
        }

        eprintln!("FiveK EXIF batch: {success} ok, {fail} failed");
        eprintln!("Cameras found: {:?}", cameras);
        assert!(success > 0, "no DNG files had valid EXIF");
        assert_eq!(fail, 0, "some DNG files failed EXIF extraction");
    }
}

// ── XMP extraction tests ─────────────────────────────────────────────

#[cfg(feature = "xmp")]
mod xmp_tests {
    use super::*;

    #[test]
    fn xmp_fivek_dng() {
        let dir = "/mnt/v/input/fivek/dng/";
        let Ok(entries) = std::fs::read_dir(dir) else {
            eprintln!("Skipping: FiveK DNG directory not found");
            return;
        };

        let mut with_xmp = 0;
        let mut without_xmp = 0;
        let mut white_balances: std::collections::BTreeSet<String> =
            std::collections::BTreeSet::new();

        for entry in entries.filter_map(|e| e.ok()).take(50) {
            let path = entry.path();
            if !path
                .extension()
                .is_some_and(|e| e.eq_ignore_ascii_case("dng"))
            {
                continue;
            }

            let Ok(data) = std::fs::read(&path) else {
                continue;
            };

            match zenraw::xmp::read_xmp_metadata(&data) {
                Some(meta) => {
                    with_xmp += 1;
                    if let Some(ref wb) = meta.white_balance {
                        white_balances.insert(wb.clone());
                    }
                }
                None => {
                    without_xmp += 1;
                }
            }
        }

        eprintln!("FiveK XMP: {with_xmp} with XMP, {without_xmp} without");
        eprintln!("White balances: {:?}", white_balances);
        assert!(with_xmp > 0, "no DNG files had XMP");
    }

    #[test]
    fn xmp_consistency_with_exif() {
        // For DNG files that have both XMP and EXIF, verify they agree on make/model
        let dir = "/mnt/v/input/fivek/dng/";
        let Ok(entries) = std::fs::read_dir(dir) else {
            eprintln!("Skipping: FiveK DNG directory not found");
            return;
        };

        let mut compared = 0;

        for entry in entries.filter_map(|e| e.ok()).take(20) {
            let path = entry.path();
            if !path
                .extension()
                .is_some_and(|e| e.eq_ignore_ascii_case("dng"))
            {
                continue;
            }

            let Ok(data) = std::fs::read(&path) else {
                continue;
            };

            #[cfg(feature = "exif")]
            {
                let exif = zenraw::exif::read_metadata(&data);
                let xmp = zenraw::xmp::read_xmp_metadata(&data);

                if let (Some(exif), Some(xmp)) = (exif, xmp) {
                    // If both have make, they should agree
                    if let (Some(exif_make), Some(xmp_make)) = (&exif.make, &xmp.tiff_make) {
                        assert_eq!(
                            exif_make,
                            xmp_make,
                            "EXIF/XMP make mismatch in {}",
                            path.file_name().unwrap().to_str().unwrap()
                        );
                    }
                    compared += 1;
                }
            }
        }

        if compared > 0 {
            eprintln!("Compared EXIF vs XMP for {compared} files — all consistent");
        } else {
            eprintln!("Skipping: no files with both EXIF and XMP found");
        }
    }

    #[test]
    fn xmp_raw_samples() {
        let samples = [
            ("nikon_d40.nef", false),
            ("canon_350d.cr2", false),
            ("sony_nex3.arw", false),
            ("olympus_c5050z.orf", false),
            ("panasonic_gf1.rw2", false),
            ("iphone12pro.dng", false), // iPhone DNG may or may not have XMP
            ("canon_eosr_craw.cr3", true), // CR3 typically has XMP
        ];

        for (name, expect_xmp) in &samples {
            let Some(data) = load_sample(name) else {
                continue;
            };

            let xmp = zenraw::xmp::extract_xmp(&data);
            let has_xmp = xmp.is_some();

            if *expect_xmp {
                assert!(has_xmp, "{name}: expected XMP but none found");
            }

            eprintln!("{name}: XMP={}", if has_xmp { "yes" } else { "no" });

            if let Some(meta) = zenraw::xmp::read_xmp_metadata(&data) {
                if let Some(ref make) = meta.tiff_make {
                    eprintln!("  tiff:Make = {make}");
                }
                if let Some(ref wb) = meta.white_balance {
                    eprintln!("  crs:WhiteBalance = {wb}");
                }
            }
        }
    }
}