zenjpeg 0.8.3

Pure Rust JPEG encoder/decoder 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
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
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
//! Depth map extraction from JPEG files.
//!
//! Supports two depth map embedding formats found in smartphone photos:
//!
//! - **iPhone (MPF Disparity)**: Depth stored as a secondary JPEG image in
//!   the MPF (Multi-Picture Format) directory with type code 0x04 (Disparity).
//!   The [`PreserveConfig`] must have `mpf_depth` enabled for this to be extracted.
//!
//! - **Android GDepth (XMP)**: Depth stored as base64-encoded image data in
//!   the `GDepth` XMP namespace. May use Extended XMP if the data exceeds 64KB.
//!   The XMP string must be preserved (`PreserveConfig.xmp = true`).
//!
//! - **Android Dynamic Depth Format (DDF)**: A newer Google format where depth
//!   images are appended after the primary JPEG, with a container directory in
//!   XMP describing item offsets and sizes.
//!
//! # Usage
//!
//! ```ignore
//! use zenjpeg::decode::{Decoder, PreserveConfig};
//!
//! let config = Decoder::new()
//!     .preserve(PreserveConfig::all());
//!
//! let result = config.decode(&jpeg_data, enough::Unstoppable)?;
//! let extras = result.extras().unwrap();
//!
//! if let Some(depth) = extras.extract_depth_map(Some(&jpeg_data)) {
//!     println!("Depth source: {:?}", depth.source);
//!     println!("Image bytes: {}", depth.data.len());
//! }
//! ```

use alloc::string::String;
use alloc::vec::Vec;

use super::extras::{DecodedExtras, MpfImageTypeExt};

// ============================================================================
// Public types
// ============================================================================

/// Unified depth map data from any extraction source.
#[derive(Clone, Debug)]
pub struct DepthMapData {
    /// How the depth map was found.
    pub source: DepthSource,
    /// Raw image bytes (JPEG or PNG) of the depth map.
    pub data: Vec<u8>,
    /// MIME type of the depth image data.
    pub mime: String,
    /// Optional GDepth metadata (only present for GDepth/DDF sources).
    pub metadata: Option<GDepthMetadata>,
    /// Optional confidence map bytes.
    pub confidence: Option<Vec<u8>>,
    /// MIME type of the confidence map (if present).
    pub confidence_mime: Option<String>,
}

/// Where the depth map was extracted from.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DepthSource {
    /// iPhone MPF secondary image with Disparity type code (0x04).
    MpfDisparity,
    /// Android GDepth XMP namespace (standard or extended XMP).
    GDepthXmp,
    /// Android Dynamic Depth Format (DDF) container directory.
    DynamicDepth,
}

/// Metadata parsed from GDepth/DDF XMP.
#[derive(Clone, Debug)]
pub struct GDepthMetadata {
    /// Depth map format.
    pub format: GDepthFormat,
    /// Near plane distance.
    pub near: f32,
    /// Far plane distance.
    pub far: f32,
    /// Distance units.
    pub units: GDepthUnits,
    /// Measurement type.
    pub measure_type: GDepthMeasureType,
}

/// Depth map value encoding format.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum GDepthFormat {
    /// Linear depth: pixel_value = (depth - near) / (far - near).
    #[default]
    RangeLinear,
    /// Inverse depth: pixel_value = (1/depth - 1/far) / (1/near - 1/far).
    RangeInverse,
}

/// Units for near/far plane distances.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum GDepthUnits {
    /// Meters (default per Google spec).
    #[default]
    Meters,
    /// Diopters (1/meters).
    Diopters,
}

/// How depth is measured from the camera.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum GDepthMeasureType {
    /// Depth along the optical axis (perpendicular to sensor plane).
    #[default]
    OpticalAxis,
    /// Depth along the optic ray (Euclidean distance from camera center).
    OpticRay,
}

// ============================================================================
// GDepth XMP parsing
// ============================================================================

/// Parse GDepth metadata and data from an XMP string.
///
/// Handles the Google Depth Map XMP namespace (`http://ns.google.com/photos/1.0/depthmap/`).
/// The depth image data is base64-encoded within the XMP.
///
/// Returns `None` if no GDepth namespace is found.
pub(crate) fn parse_gdepth_xmp(xmp: &str) -> Option<DepthMapData> {
    // Check for GDepth namespace
    if !xmp.contains("GDepth:") && !xmp.contains("gdepth:") {
        return None;
    }

    // Extract metadata fields
    let format = extract_xmp_attr(xmp, "GDepth:Format")
        .or_else(|| extract_xmp_attr(xmp, "gdepth:Format"))
        .and_then(|v| match v.as_str() {
            "RangeLinear" => Some(GDepthFormat::RangeLinear),
            "RangeInverse" => Some(GDepthFormat::RangeInverse),
            _ => None,
        })
        .unwrap_or_default();

    let near = extract_xmp_attr(xmp, "GDepth:Near")
        .or_else(|| extract_xmp_attr(xmp, "gdepth:Near"))
        .and_then(|v| v.parse::<f32>().ok())
        .unwrap_or(0.0);

    let far = extract_xmp_attr(xmp, "GDepth:Far")
        .or_else(|| extract_xmp_attr(xmp, "gdepth:Far"))
        .and_then(|v| v.parse::<f32>().ok())
        .unwrap_or(0.0);

    let mime = extract_xmp_attr(xmp, "GDepth:Mime")
        .or_else(|| extract_xmp_attr(xmp, "gdepth:Mime"))
        .unwrap_or_else(|| String::from("image/jpeg"));

    let units = extract_xmp_attr(xmp, "GDepth:Units")
        .or_else(|| extract_xmp_attr(xmp, "gdepth:Units"))
        .and_then(|v| match v.as_str() {
            "Meters" | "meters" => Some(GDepthUnits::Meters),
            "Diopters" | "diopters" => Some(GDepthUnits::Diopters),
            _ => None,
        })
        .unwrap_or_default();

    let measure_type = extract_xmp_attr(xmp, "GDepth:MeasureType")
        .or_else(|| extract_xmp_attr(xmp, "gdepth:MeasureType"))
        .and_then(|v| match v.as_str() {
            "OpticalAxis" => Some(GDepthMeasureType::OpticalAxis),
            "OpticRay" => Some(GDepthMeasureType::OpticRay),
            _ => None,
        })
        .unwrap_or_default();

    // Extract and decode base64 depth data
    let data_b64 = extract_xmp_attr(xmp, "GDepth:Data")
        .or_else(|| extract_xmp_attr(xmp, "gdepth:Data"))
        .or_else(|| extract_xmp_element(xmp, "GDepth:Data"))
        .or_else(|| extract_xmp_element(xmp, "gdepth:Data"))?;

    let data = base64_decode(&data_b64)?;

    // Extract optional confidence map
    let confidence_b64 = extract_xmp_attr(xmp, "GDepth:Confidence")
        .or_else(|| extract_xmp_attr(xmp, "gdepth:Confidence"))
        .or_else(|| extract_xmp_element(xmp, "GDepth:Confidence"))
        .or_else(|| extract_xmp_element(xmp, "gdepth:Confidence"));
    let confidence = confidence_b64.and_then(|b64| base64_decode(&b64));

    let confidence_mime = extract_xmp_attr(xmp, "GDepth:ConfidenceMime")
        .or_else(|| extract_xmp_attr(xmp, "gdepth:ConfidenceMime"));

    Some(DepthMapData {
        source: DepthSource::GDepthXmp,
        data,
        mime,
        metadata: Some(GDepthMetadata {
            format,
            near,
            far,
            units,
            measure_type,
        }),
        confidence,
        confidence_mime,
    })
}

// ============================================================================
// Dynamic Depth Format (DDF) parsing
// ============================================================================

/// Parse Dynamic Depth Format container directory from XMP and extract depth.
///
/// DDF stores metadata in the `http://ns.google.com/photos/dd/1.0/` namespace.
/// Images are appended after the primary JPEG, with lengths and types in XMP.
///
/// `file_data` is the complete JPEG file (including appended images).
pub(crate) fn parse_ddf(xmp: &str, file_data: &[u8]) -> Option<DepthMapData> {
    use ultrahdr_core::metadata::container::{ItemSemantic, parse_container_items};

    // Check for Dynamic Depth namespace
    if !xmp.contains("Container:Directory") && !xmp.contains("http://ns.google.com/photos/dd/1.0/")
    {
        return None;
    }

    // Parse container items from XMP (using shared ultrahdr-core parser)
    let items = parse_container_items(xmp);
    if items.is_empty() {
        return None;
    }

    // Parse DepthMap metadata from XMP (separate from container directory)
    let format = extract_xmp_attr(xmp, "GDepth:Format")
        .or_else(|| extract_xmp_attr(xmp, "DepthMap:Format"))
        .and_then(|v| match v.as_str() {
            "RangeLinear" => Some(GDepthFormat::RangeLinear),
            "RangeInverse" => Some(GDepthFormat::RangeInverse),
            _ => None,
        })
        .unwrap_or_default();

    let near = extract_xmp_attr(xmp, "GDepth:Near")
        .or_else(|| extract_xmp_attr(xmp, "DepthMap:Near"))
        .and_then(|v| v.parse::<f32>().ok())
        .unwrap_or(0.0);

    let far = extract_xmp_attr(xmp, "GDepth:Far")
        .or_else(|| extract_xmp_attr(xmp, "DepthMap:Far"))
        .and_then(|v| v.parse::<f32>().ok())
        .unwrap_or(0.0);

    let units = extract_xmp_attr(xmp, "GDepth:Units")
        .or_else(|| extract_xmp_attr(xmp, "DepthMap:Units"))
        .and_then(|v| match v.as_str() {
            "Meters" | "meters" => Some(GDepthUnits::Meters),
            "Diopters" | "diopters" => Some(GDepthUnits::Diopters),
            _ => None,
        })
        .unwrap_or_default();

    let measure_type = extract_xmp_attr(xmp, "GDepth:MeasureType")
        .or_else(|| extract_xmp_attr(xmp, "DepthMap:MeasureType"))
        .and_then(|v| match v.as_str() {
            "OpticalAxis" => Some(GDepthMeasureType::OpticalAxis),
            "OpticRay" => Some(GDepthMeasureType::OpticRay),
            _ => None,
        })
        .unwrap_or_default();

    // Find the primary JPEG's end (EOI marker)
    let primary_end = find_primary_eoi(file_data)?;

    // Walk the container directory, calculating offsets for appended items.
    // The first item is always the primary JPEG (already decoded).
    // Subsequent items are appended sequentially after the primary's EOI.
    let mut offset = primary_end;
    let mut depth_data: Option<Vec<u8>> = None;
    let mut depth_mime = String::from("image/jpeg");
    let mut confidence_data: Option<Vec<u8>> = None;
    let mut confidence_mime: Option<String> = None;

    for (i, item) in items.iter().enumerate() {
        if i == 0 {
            // First item is the primary JPEG, skip it
            continue;
        }

        let length = item.length.unwrap_or(0);
        let end = offset.saturating_add(length);
        if end > file_data.len() {
            break;
        }

        let is_depth = matches!(item.semantic, ItemSemantic::DepthMap);
        let is_confidence = matches!(item.semantic, ItemSemantic::ConfidenceMap);

        if is_depth && depth_data.is_none() {
            depth_data = Some(file_data[offset..end].to_vec());
            depth_mime = item.mime.clone();
        } else if is_confidence && confidence_data.is_none() {
            confidence_data = Some(file_data[offset..end].to_vec());
            confidence_mime = Some(item.mime.clone());
        }

        offset = end;
    }

    let data = depth_data?;

    Some(DepthMapData {
        source: DepthSource::DynamicDepth,
        data,
        mime: depth_mime,
        metadata: Some(GDepthMetadata {
            format,
            near,
            far,
            units,
            measure_type,
        }),
        confidence: confidence_data,
        confidence_mime,
    })
}

// ============================================================================
// XMP attribute extraction helpers
// ============================================================================

/// Extract an XMP attribute value: `name="value"`.
fn extract_xmp_attr(xmp: &str, name: &str) -> Option<String> {
    extract_xml_attr(xmp, name)
}

/// Extract XML attribute value: `name="value"`.
fn extract_xml_attr(xml: &str, name: &str) -> Option<String> {
    // Pattern: name="value"
    let pattern = alloc::format!("{}=\"", name);
    if let Some(start) = xml.find(&pattern) {
        let value_start = start + pattern.len();
        let remaining = &xml[value_start..];
        if let Some(end) = remaining.find('"') {
            return Some(remaining[..end].to_string());
        }
    }

    // Also try single quotes: name='value'
    let pattern_sq = alloc::format!("{}='", name);
    if let Some(start) = xml.find(&pattern_sq) {
        let value_start = start + pattern_sq.len();
        let remaining = &xml[value_start..];
        if let Some(end) = remaining.find('\'') {
            return Some(remaining[..end].to_string());
        }
    }

    None
}

/// Extract XMP element content: `<name>content</name>`.
fn extract_xmp_element(xmp: &str, name: &str) -> Option<String> {
    let open_tag = alloc::format!("<{}>", name);
    let close_tag = alloc::format!("</{}>", name);

    let start = xmp.find(&open_tag)?;
    let content_start = start + open_tag.len();
    let remaining = &xmp[content_start..];
    let end = remaining.find(&close_tag)?;
    Some(remaining[..end].to_string())
}

// ============================================================================
// Base64 decoder (minimal, no external dependency)
// ============================================================================

/// Decode a base64 string, ignoring whitespace and newlines.
///
/// Supports both standard and URL-safe base64 alphabets, with
/// optional padding. Returns `None` on invalid input.
pub(crate) fn base64_decode(input: &str) -> Option<Vec<u8>> {
    // Strip whitespace
    let clean: Vec<u8> = input.bytes().filter(|b| !b.is_ascii_whitespace()).collect();

    if clean.is_empty() {
        return None;
    }

    let mut output = Vec::with_capacity(clean.len() * 3 / 4);
    let mut buf: u32 = 0;
    let mut bits: u32 = 0;

    for &byte in &clean {
        let val = match byte {
            b'A'..=b'Z' => byte - b'A',
            b'a'..=b'z' => byte - b'a' + 26,
            b'0'..=b'9' => byte - b'0' + 52,
            b'+' | b'-' => 62, // + (standard) or - (URL-safe)
            b'/' | b'_' => 63, // / (standard) or _ (URL-safe)
            b'=' => continue,  // Padding
            _ => return None,  // Invalid character
        };

        buf = (buf << 6) | val as u32;
        bits += 6;

        if bits >= 8 {
            bits -= 8;
            output.push((buf >> bits) as u8);
            buf &= (1 << bits) - 1;
        }
    }

    Some(output)
}

/// Encode bytes as standard base64.
#[cfg(test)]
pub(crate) fn base64_encode(input: &[u8]) -> String {
    const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    let mut output = String::with_capacity((input.len() + 2) / 3 * 4);

    for chunk in input.chunks(3) {
        let b0 = chunk[0] as u32;
        let b1 = chunk.get(1).copied().unwrap_or(0) as u32;
        let b2 = chunk.get(2).copied().unwrap_or(0) as u32;
        let triple = (b0 << 16) | (b1 << 8) | b2;

        output.push(ALPHABET[((triple >> 18) & 0x3F) as usize] as char);
        output.push(ALPHABET[((triple >> 12) & 0x3F) as usize] as char);
        if chunk.len() > 1 {
            output.push(ALPHABET[((triple >> 6) & 0x3F) as usize] as char);
        } else {
            output.push('=');
        }
        if chunk.len() > 2 {
            output.push(ALPHABET[(triple & 0x3F) as usize] as char);
        } else {
            output.push('=');
        }
    }

    output
}

// ============================================================================
// JPEG boundary scanner (shared with gainmap)
// ============================================================================

/// Find the end position of the primary JPEG (position after EOI marker).
///
/// Properly skips entropy-coded segments by parsing marker structure.
fn find_primary_eoi(data: &[u8]) -> Option<usize> {
    if data.len() < 4 || data[0] != 0xFF || data[1] != 0xD8 {
        return None;
    }

    let mut pos = 2; // Skip SOI

    while pos < data.len().saturating_sub(1) {
        if data[pos] != 0xFF {
            pos += 1;
            continue;
        }

        let marker = data[pos + 1];

        match marker {
            0xD9 => {
                // EOI found
                return Some(pos + 2);
            }
            0x00 => {
                // Byte stuffing, skip
                pos += 2;
            }
            0xFF => {
                // Fill byte
                pos += 1;
            }
            0xDA => {
                // SOS — skip the marker segment, then scan entropy data
                if pos + 4 > data.len() {
                    return None;
                }
                let len = u16::from_be_bytes([data[pos + 2], data[pos + 3]]) as usize;
                pos += 2 + len;

                // Scan entropy-coded data for the next marker
                while pos < data.len().saturating_sub(1) {
                    if data[pos] == 0xFF {
                        let next = data[pos + 1];
                        if next == 0x00 {
                            pos += 2;
                        } else if next == 0xFF {
                            pos += 1;
                        } else if (0xD0..=0xD7).contains(&next) {
                            pos += 2;
                        } else {
                            break;
                        }
                    } else {
                        pos += 1;
                    }
                }
            }
            m if (0xD0..=0xD7).contains(&m) => {
                pos += 2;
            }
            _ => {
                // Marker with length field
                if pos + 4 > data.len() {
                    return None;
                }
                let len = u16::from_be_bytes([data[pos + 2], data[pos + 3]]) as usize;
                pos += 2 + len;
            }
        }
    }

    None
}

// ============================================================================
// Integration with DecodedExtras
// ============================================================================

impl DecodedExtras {
    /// Extract a depth map from any available source.
    ///
    /// Tries sources in priority order:
    /// 1. GDepth XMP (most metadata-rich)
    /// 2. Dynamic Depth Format (DDF)
    /// 3. MPF Disparity secondary image (least metadata)
    ///
    /// For MPF extraction, `file_data` is not needed (data is already preserved).
    /// For GDepth/DDF, `file_data` is needed for DDF (appended images by offset).
    ///
    /// Pass the original JPEG bytes as `file_data` for DDF support.
    /// Pass `None` if you only need MPF and GDepth (inline base64) sources.
    #[must_use]
    pub fn extract_depth_map(&self, file_data: Option<&[u8]>) -> Option<DepthMapData> {
        // 1. Try GDepth XMP (base64-encoded depth in XMP, most metadata)
        if let Some(xmp) = self.xmp() {
            if let Some(depth) = parse_gdepth_xmp(xmp) {
                return Some(depth);
            }

            // 2. Try Dynamic Depth Format (DDF)
            if let Some(data) = file_data
                && let Some(depth) = parse_ddf(xmp, data)
            {
                return Some(depth);
            }
        }

        // 3. Fall back to MPF Disparity secondary image
        self.secondary_images
            .iter()
            .find(|img| img.image_type.is_depth())
            .map(|img| DepthMapData {
                source: DepthSource::MpfDisparity,
                data: img.data.clone(),
                mime: String::from("image/jpeg"),
                metadata: None,
                confidence: None,
                confidence_mime: None,
            })
    }

    /// Check if any depth map data is available without extracting it.
    ///
    /// This is cheaper than `extract_depth_map()` since it skips base64 decoding.
    #[must_use]
    pub fn has_depth_map(&self) -> bool {
        // Check MPF secondary images
        if self
            .secondary_images
            .iter()
            .any(|img| img.image_type.is_depth())
        {
            return true;
        }

        // Check XMP for GDepth namespace
        if let Some(xmp) = self.xmp() {
            if xmp.contains("GDepth:") || xmp.contains("gdepth:") {
                return true;
            }
            if xmp.contains("Container:Directory") && xmp.contains("Depth") {
                return true;
            }
        }

        false
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    // === Base64 roundtrip ===

    #[test]
    fn base64_roundtrip_empty() {
        assert_eq!(base64_decode(""), None);
    }

    #[test]
    fn base64_roundtrip_simple() {
        let original = b"Hello, World!";
        let encoded = base64_encode(original);
        let decoded = base64_decode(&encoded).unwrap();
        assert_eq!(decoded, original);
    }

    #[test]
    fn base64_roundtrip_binary() {
        let original: Vec<u8> = (0..=255).collect();
        let encoded = base64_encode(&original);
        let decoded = base64_decode(&encoded).unwrap();
        assert_eq!(decoded, original);
    }

    #[test]
    fn base64_with_whitespace() {
        let encoded = "SGVs\nbG8s\nIFdv\ncmxk\nIQ==";
        let decoded = base64_decode(encoded).unwrap();
        assert_eq!(decoded, b"Hello, World!");
    }

    #[test]
    fn base64_url_safe() {
        // URL-safe uses - instead of + and _ instead of /
        let standard = base64_encode(&[0xFB, 0xEF, 0xBE]);
        let url_safe = standard.replace('+', "-").replace('/', "_");
        let decoded = base64_decode(&url_safe).unwrap();
        assert_eq!(decoded, &[0xFB, 0xEF, 0xBE]);
    }

    #[test]
    fn base64_invalid_char() {
        assert!(base64_decode("SGVsbG8#").is_none());
    }

    #[test]
    fn base64_no_padding() {
        // Some encoders omit padding
        let original = b"Hi";
        let encoded = base64_encode(original);
        let no_pad = encoded.trim_end_matches('=');
        let decoded = base64_decode(no_pad).unwrap();
        assert_eq!(decoded, original);
    }

    // === XMP attribute extraction ===

    #[test]
    fn extract_attr_double_quotes() {
        let xmp = r#"<rdf:Description GDepth:Format="RangeLinear"/>"#;
        assert_eq!(
            extract_xmp_attr(xmp, "GDepth:Format"),
            Some("RangeLinear".to_string())
        );
    }

    #[test]
    fn extract_attr_single_quotes() {
        let xmp = "<rdf:Description GDepth:Near='0.5'/>";
        assert_eq!(
            extract_xmp_attr(xmp, "GDepth:Near"),
            Some("0.5".to_string())
        );
    }

    #[test]
    fn extract_attr_missing() {
        let xmp = r#"<rdf:Description GDepth:Format="RangeLinear"/>"#;
        assert_eq!(extract_xmp_attr(xmp, "GDepth:Near"), None);
    }

    #[test]
    fn extract_element_content() {
        let xmp = "<GDepth:Data>SGVsbG8=</GDepth:Data>";
        assert_eq!(
            extract_xmp_element(xmp, "GDepth:Data"),
            Some("SGVsbG8=".to_string())
        );
    }

    // === GDepth XMP parsing ===

    #[test]
    fn parse_gdepth_full_metadata() {
        let depth_jpeg = vec![0xFF, 0xD8, 0xFF, 0xD9]; // Minimal JPEG
        let b64_data = base64_encode(&depth_jpeg);
        let xmp = alloc::format!(
            r#"<x:xmpmeta xmlns:x="adobe:ns:meta/">
  <rdf:RDF>
    <rdf:Description
      GDepth:Format="RangeInverse"
      GDepth:Near="0.15"
      GDepth:Far="100.0"
      GDepth:Mime="image/jpeg"
      GDepth:Units="Meters"
      GDepth:MeasureType="OpticRay"
      GDepth:Data="{b64_data}"/>
  </rdf:RDF>
</x:xmpmeta>"#
        );

        let result = parse_gdepth_xmp(&xmp).expect("should parse GDepth");
        assert_eq!(result.source, DepthSource::GDepthXmp);
        assert_eq!(result.mime, "image/jpeg");
        assert_eq!(result.data, depth_jpeg);

        let meta = result.metadata.unwrap();
        assert_eq!(meta.format, GDepthFormat::RangeInverse);
        assert!((meta.near - 0.15).abs() < 0.001);
        assert!((meta.far - 100.0).abs() < 0.001);
        assert_eq!(meta.units, GDepthUnits::Meters);
        assert_eq!(meta.measure_type, GDepthMeasureType::OpticRay);
    }

    #[test]
    fn parse_gdepth_minimal() {
        let depth_png = vec![0x89, 0x50, 0x4E, 0x47]; // PNG magic
        let b64_data = base64_encode(&depth_png);
        let xmp = alloc::format!(
            r#"<rdf:Description
      GDepth:Format="RangeLinear"
      GDepth:Mime="image/png"
      GDepth:Data="{b64_data}"/>"#
        );

        let result = parse_gdepth_xmp(&xmp).expect("should parse GDepth");
        assert_eq!(result.data, depth_png);
        assert_eq!(result.mime, "image/png");

        let meta = result.metadata.unwrap();
        assert_eq!(meta.format, GDepthFormat::RangeLinear);
        // Defaults when not specified
        assert_eq!(meta.units, GDepthUnits::Meters);
        assert_eq!(meta.measure_type, GDepthMeasureType::OpticalAxis);
    }

    #[test]
    fn parse_gdepth_lowercase_prefix() {
        let data = vec![1, 2, 3, 4];
        let b64 = base64_encode(&data);
        let xmp =
            alloc::format!(r#"<rdf:Description gdepth:Format="RangeLinear" gdepth:Data="{b64}"/>"#);

        let result = parse_gdepth_xmp(&xmp).expect("should parse lowercase gdepth");
        assert_eq!(result.data, data);
    }

    #[test]
    fn parse_gdepth_element_data() {
        let data = vec![0xFF, 0xD8, 0xFF, 0xD9];
        let b64 = base64_encode(&data);
        let xmp = alloc::format!(
            r#"<rdf:Description GDepth:Format="RangeLinear">
  <GDepth:Data>{b64}</GDepth:Data>
</rdf:Description>"#
        );

        let result = parse_gdepth_xmp(&xmp).expect("should parse element data");
        assert_eq!(result.data, data);
    }

    #[test]
    fn parse_gdepth_with_confidence() {
        let depth_data = vec![1, 2, 3];
        let conf_data = vec![4, 5, 6];
        let depth_b64 = base64_encode(&depth_data);
        let conf_b64 = base64_encode(&conf_data);
        let xmp = alloc::format!(
            r#"<rdf:Description
      GDepth:Format="RangeLinear"
      GDepth:Data="{depth_b64}"
      GDepth:Confidence="{conf_b64}"
      GDepth:ConfidenceMime="image/png"/>"#
        );

        let result = parse_gdepth_xmp(&xmp).expect("should parse with confidence");
        assert_eq!(result.data, depth_data);
        assert_eq!(result.confidence.unwrap(), conf_data);
        assert_eq!(result.confidence_mime.unwrap(), "image/png");
    }

    #[test]
    fn parse_gdepth_no_namespace() {
        let xmp = r#"<rdf:Description hdrgm:Version="1.0"/>"#;
        assert!(parse_gdepth_xmp(xmp).is_none());
    }

    #[test]
    fn parse_gdepth_missing_data() {
        let xmp = r#"<rdf:Description GDepth:Format="RangeLinear" GDepth:Near="0.5"/>"#;
        // No GDepth:Data attribute
        assert!(parse_gdepth_xmp(xmp).is_none());
    }

    #[test]
    fn parse_gdepth_invalid_base64() {
        let xmp =
            r#"<rdf:Description GDepth:Format="RangeLinear" GDepth:Data="not{valid}base64!"/>"#;
        assert!(parse_gdepth_xmp(xmp).is_none());
    }

    // === DDF parsing ===

    #[test]
    fn parse_ddf_container_directory() {
        use ultrahdr_core::metadata::container::{ItemSemantic, parse_container_items};

        let xmp = r#"<x:xmpmeta>
  <rdf:RDF>
    <rdf:Description
      xmlns:Container="http://ns.google.com/photos/dd/1.0/container/"
      xmlns:Item="http://ns.google.com/photos/dd/1.0/item/">
      <Container:Directory>
        <rdf:Seq>
          <rdf:li>
            <Container:Item Item:Mime="image/jpeg" Item:Semantic="Primary" Item:Length="0"/>
          </rdf:li>
          <rdf:li>
            <Container:Item Item:Mime="image/jpeg" Item:Semantic="DepthMap" Item:Length="5000"/>
          </rdf:li>
          <rdf:li>
            <Container:Item Item:Mime="image/png" Item:Semantic="ConfidenceMap" Item:Length="3000"/>
          </rdf:li>
        </rdf:Seq>
      </Container:Directory>
    </rdf:Description>
  </rdf:RDF>
</x:xmpmeta>"#;

        let items = parse_container_items(xmp);
        assert_eq!(items.len(), 3);
        assert_eq!(items[0].semantic, ItemSemantic::Primary);
        assert_eq!(items[0].mime, "image/jpeg");
        assert_eq!(items[1].semantic, ItemSemantic::DepthMap);
        assert_eq!(items[1].length, Some(5000));
        assert_eq!(items[2].semantic, ItemSemantic::ConfidenceMap);
        assert_eq!(items[2].length, Some(3000));
        assert_eq!(items[2].mime, "image/png");
    }

    #[test]
    fn parse_ddf_extracts_depth() {
        // Build a fake DDF file: primary JPEG + depth JPEG + confidence PNG
        let primary_jpeg = vec![0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x02, 0xFF, 0xD9]; // 8 bytes
        let depth_jpeg = vec![0xFF, 0xD8, 0x01, 0x02, 0x03, 0xFF, 0xD9]; // 7 bytes
        let conf_png = vec![0x89, 0x50, 0x4E, 0x47, 0x0D]; // 5 bytes

        let mut file_data = primary_jpeg.clone();
        file_data.extend_from_slice(&depth_jpeg);
        file_data.extend_from_slice(&conf_png);

        let xmp = alloc::format!(
            r#"<x:xmpmeta>
  <rdf:RDF>
    <rdf:Description
      xmlns:Container="http://ns.google.com/photos/dd/1.0/container/"
      xmlns:Item="http://ns.google.com/photos/dd/1.0/item/"
      GDepth:Format="RangeInverse"
      GDepth:Near="0.2"
      GDepth:Far="50.0"
      Container:Directory="true">
      <Container:Directory>
        <rdf:Seq>
          <rdf:li>
            <Container:Item Item:Mime="image/jpeg" Item:Semantic="Primary" Item:Length="0"/>
          </rdf:li>
          <rdf:li>
            <Container:Item Item:Mime="image/jpeg" Item:Semantic="DepthMap" Item:Length="{depth_len}"/>
          </rdf:li>
          <rdf:li>
            <Container:Item Item:Mime="image/png" Item:Semantic="ConfidenceMap" Item:Length="{conf_len}"/>
          </rdf:li>
        </rdf:Seq>
      </Container:Directory>
    </rdf:Description>
  </rdf:RDF>
</x:xmpmeta>"#,
            depth_len = depth_jpeg.len(),
            conf_len = conf_png.len()
        );

        let result = parse_ddf(&xmp, &file_data).expect("should parse DDF");
        assert_eq!(result.source, DepthSource::DynamicDepth);
        assert_eq!(result.data, depth_jpeg);
        assert_eq!(result.mime, "image/jpeg");

        let meta = result.metadata.unwrap();
        assert_eq!(meta.format, GDepthFormat::RangeInverse);
        assert!((meta.near - 0.2).abs() < 0.001);
        assert!((meta.far - 50.0).abs() < 0.001);

        // Confidence map
        assert_eq!(result.confidence.unwrap(), conf_png);
        assert_eq!(result.confidence_mime.unwrap(), "image/png");
    }

    #[test]
    fn parse_ddf_no_directory() {
        let xmp = r#"<rdf:Description hdrgm:Version="1.0"/>"#;
        assert!(parse_ddf(xmp, &[0xFF, 0xD8, 0xFF, 0xD9]).is_none());
    }

    #[test]
    fn parse_ddf_truncated_file() {
        let primary_jpeg = vec![0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x02, 0xFF, 0xD9];
        // File has no appended data, but directory claims 5000 bytes
        let xmp = r#"<x:xmpmeta>
  <rdf:RDF>
    <rdf:Description
      xmlns:Container="http://ns.google.com/photos/dd/1.0/container/"
      xmlns:Item="http://ns.google.com/photos/dd/1.0/item/"
      Container:Directory="true">
      <Container:Directory>
        <rdf:Seq>
          <rdf:li>
            <Container:Item Item:Mime="image/jpeg" Item:Semantic="Primary" Item:Length="0"/>
          </rdf:li>
          <rdf:li>
            <Container:Item Item:Mime="image/jpeg" Item:Semantic="DepthMap" Item:Length="5000"/>
          </rdf:li>
        </rdf:Seq>
      </Container:Directory>
    </rdf:Description>
  </rdf:RDF>
</x:xmpmeta>"#;

        // Should return None since depth data is beyond file boundary
        assert!(parse_ddf(xmp, &primary_jpeg).is_none());
    }

    // === MPF type code parsing ===

    #[test]
    fn mpf_type_disparity() {
        use crate::encode::extras::{MpfImageType, MpfImageTypeExt};
        let typ = MpfImageType::from_type_code(0x020002);
        assert_eq!(typ, MpfImageType::Disparity);
        assert!(typ.is_depth());
    }

    #[test]
    fn mpf_type_roundtrip() {
        use crate::encode::extras::{MpfImageType, MpfImageTypeExt};
        let types = [
            MpfImageType::Undefined,
            MpfImageType::LargeThumbnailVga,
            MpfImageType::LargeThumbnailFullHd,
            MpfImageType::Panorama,
            MpfImageType::Disparity,
            MpfImageType::MultiAngle,
            MpfImageType::BaselinePrimary,
        ];
        for typ in types {
            let code = typ.to_type_code();
            let back = MpfImageType::from_type_code(code);
            assert_eq!(
                back, typ,
                "roundtrip failed for {typ:?} (code=0x{code:06X})"
            );
        }
    }

    #[test]
    fn mpf_attr_masking() {
        // The attribute u32 has bits 31-24 for flags, bits 23-0 for type code.
        // The parser uses `attr & 0x00FFFFFF` to extract the type code.
        use crate::encode::extras::MpfImageType;

        // Simulate an MP entry attribute with flags set:
        // Dependent=1 (bit 31), data format=JPEG (bits 26-24 = 0)
        let attr: u32 = 0x80020002; // Dependent + Disparity type
        let type_code = attr & 0x00FFFFFF;
        assert_eq!(
            MpfImageType::from_type_code(type_code),
            MpfImageType::Disparity
        );
    }

    // === DecodedExtras integration ===

    #[test]
    fn extras_has_depth_map_false_when_empty() {
        let extras = DecodedExtras::new();
        assert!(!extras.has_depth_map());
    }

    #[test]
    fn extras_extract_depth_map_mpf() {
        use super::super::extras::PreservedMpfImage;
        use crate::encode::extras::MpfImageType;

        let mut extras = DecodedExtras::new();
        let depth_jpeg = vec![0xFF, 0xD8, 0x42, 0xFF, 0xD9];
        extras.secondary_images.push(PreservedMpfImage {
            mpf_index: 1,
            image_type: MpfImageType::Disparity,
            data: depth_jpeg.clone(),
        });

        assert!(extras.has_depth_map());

        let depth = extras.extract_depth_map(None).unwrap();
        assert_eq!(depth.source, DepthSource::MpfDisparity);
        assert_eq!(depth.data, depth_jpeg);
        assert_eq!(depth.mime, "image/jpeg");
        assert!(depth.metadata.is_none()); // MPF has no GDepth metadata
    }

    #[test]
    fn extras_extract_depth_map_gdepth_priority() {
        // When both GDepth XMP and MPF disparity are present,
        // GDepth should take priority (more metadata).
        use super::super::extras::{PreservedMpfImage, PreservedSegment, SegmentType};
        use crate::encode::extras::MpfImageType;

        let mut extras = DecodedExtras::new();

        // Add MPF depth image
        extras.secondary_images.push(PreservedMpfImage {
            mpf_index: 1,
            image_type: MpfImageType::Disparity,
            data: vec![0xFF, 0xD8, 0x01, 0xFF, 0xD9],
        });

        // Add XMP with GDepth
        let depth_data = vec![0xFF, 0xD8, 0x02, 0xFF, 0xD9];
        let b64 = base64_encode(&depth_data);
        // The XMP segment stored by the parser includes the namespace prefix
        let xmp_ns = b"http://ns.adobe.com/xap/1.0/\0";
        let mut xmp_data = xmp_ns.to_vec();
        let xmp_content = alloc::format!(
            r#"<rdf:Description GDepth:Format="RangeLinear" GDepth:Near="0.5" GDepth:Far="10.0" GDepth:Data="{b64}"/>"#
        );
        xmp_data.extend_from_slice(xmp_content.as_bytes());

        extras.segments.push(PreservedSegment {
            marker: 0xE1,
            data: xmp_data,
            segment_type: SegmentType::Xmp,
        });

        let depth = extras.extract_depth_map(None).unwrap();
        assert_eq!(depth.source, DepthSource::GDepthXmp);
        assert_eq!(depth.data, depth_data);
        assert!(depth.metadata.is_some());
    }

    // === find_primary_eoi ===

    #[test]
    fn find_eoi_simple() {
        let jpeg = vec![0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x02, 0xFF, 0xD9];
        assert_eq!(find_primary_eoi(&jpeg), Some(8));
    }

    #[test]
    fn find_eoi_with_entropy() {
        // JPEG with SOS marker followed by entropy data then EOI
        let jpeg = vec![
            0xFF, 0xD8, // SOI
            0xFF, 0xDA, 0x00, 0x03, 0x00, // SOS (length=3, 1 byte data spec)
            0xAB, 0xCD, 0xEF, // Entropy data
            0xFF, 0xD9, // EOI
        ];
        assert_eq!(find_primary_eoi(&jpeg), Some(12));
    }

    #[test]
    fn find_eoi_not_jpeg() {
        let data = vec![0x89, 0x50, 0x4E, 0x47]; // PNG
        assert_eq!(find_primary_eoi(&data), None);
    }

    // === GDepth metadata field parsing ===

    #[test]
    fn gdepth_format_range_linear() {
        let data = vec![1, 2, 3];
        let b64 = base64_encode(&data);
        let xmp =
            alloc::format!(r#"<rdf:Description GDepth:Format="RangeLinear" GDepth:Data="{b64}"/>"#);
        let result = parse_gdepth_xmp(&xmp).unwrap();
        assert_eq!(result.metadata.unwrap().format, GDepthFormat::RangeLinear);
    }

    #[test]
    fn gdepth_format_range_inverse() {
        let data = vec![1, 2, 3];
        let b64 = base64_encode(&data);
        let xmp = alloc::format!(
            r#"<rdf:Description GDepth:Format="RangeInverse" GDepth:Data="{b64}"/>"#
        );
        let result = parse_gdepth_xmp(&xmp).unwrap();
        assert_eq!(result.metadata.unwrap().format, GDepthFormat::RangeInverse);
    }

    #[test]
    fn gdepth_units_diopters() {
        let data = vec![1, 2, 3];
        let b64 = base64_encode(&data);
        let xmp = alloc::format!(
            r#"<rdf:Description GDepth:Format="RangeLinear" GDepth:Units="Diopters" GDepth:Data="{b64}"/>"#
        );
        let result = parse_gdepth_xmp(&xmp).unwrap();
        assert_eq!(result.metadata.unwrap().units, GDepthUnits::Diopters);
    }

    #[test]
    fn gdepth_measure_optic_ray() {
        let data = vec![1, 2, 3];
        let b64 = base64_encode(&data);
        let xmp = alloc::format!(
            r#"<rdf:Description GDepth:Format="RangeLinear" GDepth:MeasureType="OpticRay" GDepth:Data="{b64}"/>"#
        );
        let result = parse_gdepth_xmp(&xmp).unwrap();
        assert_eq!(
            result.metadata.unwrap().measure_type,
            GDepthMeasureType::OpticRay
        );
    }

    // === Base64 decode with large data (simulating depth image) ===

    #[test]
    fn base64_roundtrip_large() {
        // Simulate a depth image (64KB+, the size that would need Extended XMP)
        let original: Vec<u8> = (0..70_000).map(|i| (i % 256) as u8).collect();
        let encoded = base64_encode(&original);
        let decoded = base64_decode(&encoded).unwrap();
        assert_eq!(decoded.len(), original.len());
        assert_eq!(decoded, original);
    }

    // === Conformance: Google GDepth spec example ===

    #[test]
    fn gdepth_google_spec_example() {
        // Based on Google's GDepth specification example
        let depth_data = vec![0xFF, 0xD8, 0x00, 0xFF, 0xD9]; // Fake JPEG
        let b64 = base64_encode(&depth_data);

        let xmp = alloc::format!(
            r#"<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/">
  <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <rdf:Description rdf:about=""
      xmlns:GDepth="http://ns.google.com/photos/1.0/depthmap/"
      GDepth:Format="RangeInverse"
      GDepth:Near="0.6117252707481384"
      GDepth:Far="14.117647171020508"
      GDepth:Mime="image/jpeg"
      GDepth:Units="Meters"
      GDepth:MeasureType="OpticalAxis"
      GDepth:Data="{b64}"/>
  </rdf:RDF>
</x:xmpmeta>
<?xpacket end="w"?>"#
        );

        let result = parse_gdepth_xmp(&xmp).unwrap();
        assert_eq!(result.source, DepthSource::GDepthXmp);
        assert_eq!(result.data, depth_data);
        assert_eq!(result.mime, "image/jpeg");

        let meta = result.metadata.unwrap();
        assert_eq!(meta.format, GDepthFormat::RangeInverse);
        assert!((meta.near - 0.6117).abs() < 0.001);
        assert!((meta.far - 14.117).abs() < 0.01);
        assert_eq!(meta.units, GDepthUnits::Meters);
        assert_eq!(meta.measure_type, GDepthMeasureType::OpticalAxis);
    }
}