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
/*
 * // Copyright (c) Radzivon Bartoshyk 7/2026. All rights reserved.
 * //
 * // Redistribution and use in source and binary forms, with or without modification,
 * // are permitted provided that the following conditions are met:
 * //
 * // 1.  Redistributions of source code must retain the above copyright notice, this
 * // list of conditions and the following disclaimer.
 * //
 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
 * // this list of conditions and the following disclaimer in the documentation
 * // and/or other materials provided with the distribution.
 * //
 * // 3.  Neither the name of the copyright holder nor the names of its
 * // contributors may be used to endorse or promote products derived from
 * // this software without specific prior written permission.
 * //
 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

mod arith;
mod brotli;
mod coeff_order;
mod encode;
mod jbrd;
mod parse;
mod uhdr;

#[allow(unused_imports)]
pub(crate) use parse::{JpegError, parse_jpeg};
use std::num::NonZeroUsize;

use crate::util::EncodeError;
use crate::{Orientation, Speed};
pub use brotli::BrotliCompression;

/// Appends an ISOBMFF box with the given type and payload.
fn push_box(out: &mut Vec<u8>, kind: &[u8; 4], payload: &[u8]) {
    let size = 8 + payload.len();
    if let Ok(small) = u32::try_from(size) {
        out.extend_from_slice(&small.to_be_bytes());
        out.extend_from_slice(kind);
    } else {
        // 64-bit "largesize" escape.
        out.extend_from_slice(&1u32.to_be_bytes());
        out.extend_from_slice(kind);
        out.extend_from_slice(&((size + 8) as u64).to_be_bytes());
    }
    out.extend_from_slice(payload);
}

/// An ICC profile reassembled from the APP2 `ICC_PROFILE` chunks.
#[derive(Debug, PartialEq)]
struct JpegIcc {
    profile: Vec<u8>,
    /// The chunks' `app_data` indices when they appear in file order as chunk
    /// 1, 2, .., N. Only then can the decoder regenerate them from the
    /// codestream's profile, so `jbrd` need not store them a second time.
    in_order_apps: Option<Vec<usize>>,
}

/// Reassembles an ICC profile from the APP2 `ICC_PROFILE` chunks.
fn extract_icc(jpg: &JpegData) -> Option<JpegIcc> {
    const TAG: &[u8] = b"ICC_PROFILE\0";
    // Each entry is [marker, len_hi, len_lo, payload..].
    let mut chunks: Vec<(u8, usize, &[u8])> = Vec::new();
    let mut expected = 0u8;
    for (index, app) in jpg.app_data.iter().enumerate() {
        if app.first() != Some(&0xE2) || app.len() < 3 + TAG.len() + 2 {
            continue;
        }
        let payload = &app[3..];
        if !payload.starts_with(TAG) {
            continue;
        }
        let seq = payload[TAG.len()];
        let count = payload[TAG.len() + 1];
        if seq == 0 || count == 0 || (expected != 0 && count != expected) {
            return None;
        }
        expected = count;
        chunks.push((seq, index, &payload[TAG.len() + 2..]));
    }
    if chunks.is_empty() || chunks.len() != usize::from(expected) {
        return None;
    }
    let in_order = chunks
        .iter()
        .enumerate()
        .all(|(i, &(seq, _, _))| usize::from(seq) == i + 1);
    chunks.sort_by_key(|&(seq, _, _)| seq);
    if chunks
        .iter()
        .enumerate()
        .any(|(i, &(seq, _, _))| usize::from(seq) != i + 1)
    {
        return None;
    }
    let profile: Vec<u8> = chunks
        .iter()
        .flat_map(|&(_, _, data)| data)
        .copied()
        .collect();
    (!profile.is_empty()).then(|| JpegIcc {
        profile,
        in_order_apps: in_order.then(|| chunks.iter().map(|&(_, index, _)| index).collect()),
    })
}

/// Structural checks a profile must pass before it is embedded, the ones PNG
/// writers and color managers also apply: header size field, `acsp`
/// signature, and every tag inside the profile. Anything else (a corrupted or
/// foreign APP2 payload) would be handed on to every decoder of the image.
fn icc_is_well_formed(icc: &[u8]) -> bool {
    const HEADER: usize = 128;
    let be32 = |at: usize| -> Option<usize> {
        let b = icc.get(at..at + 4)?;
        Some(u32::from_be_bytes([b[0], b[1], b[2], b[3]]) as usize)
    };
    if icc.len() < HEADER + 4 || be32(0) != Some(icc.len()) || &icc[36..40] != b"acsp" {
        return false;
    }
    let Some(num_tags) = be32(HEADER) else {
        return false;
    };
    let table_end = (num_tags as u64) * 12 + HEADER as u64 + 4;
    if table_end > icc.len() as u64 {
        return false;
    }
    (0..num_tags).all(|i| {
        let entry = HEADER + 4 + i * 12;
        match (be32(entry + 4), be32(entry + 8)) {
            (Some(offset), Some(size)) => (offset as u64 + size as u64) <= icc.len() as u64,
            _ => false,
        }
    })
}

/// Finds the first Exif APP1 segment: its index in `app_data` and the TIFF
/// data that follows the `Exif\0\0` tag.
fn extract_exif(jpg: &JpegData) -> Option<(usize, &[u8])> {
    const TAG: &[u8] = b"Exif\0\0";
    jpg.app_data.iter().enumerate().find_map(|(index, app)| {
        if app.first() != Some(&0xE1) || app.len() <= 3 + TAG.len() {
            return None;
        }
        let payload = &app[3..];
        payload
            .starts_with(TAG)
            .then(|| (index, &payload[TAG.len()..]))
    })
}

/// Extracts the first standard XMP APP1 segment.
///
/// Returns both its index in `app_data` (for JPEG reconstruction tagging) and
/// the XML packet without the JPEG XMP namespace prefix.
fn extract_xmp(jpg: &JpegData) -> Option<(usize, Vec<u8>)> {
    const TAG: &[u8] = b"http://ns.adobe.com/xap/1.0/\0";
    jpg.app_data.iter().enumerate().find_map(|(index, app)| {
        if app.first() != Some(&0xE1) || app.len() < 3 + TAG.len() {
            return None;
        }
        let payload = &app[3..];
        payload
            .starts_with(TAG)
            .then(|| (index, payload[TAG.len()..].to_vec()))
    })
}

/// Options for [`encode_jpeg_lossless_with_config`].
pub struct JpegTranscodeConfig {
    /// Whether to embed the data needed to rebuild the original JPEG bytes.
    /// For arithmetic-coded input this rebuilds the Huffman-coded equivalent.
    ///
    /// Defaults to `true`. See [`Self::with_jpeg_reconstruction`].
    /// Binary Ultra HDR gain maps are written to `jhgm` even when this is false.
    /// JPEG reconstruction is omitted for an arithmetic-coded Ultra HDR base
    /// when its binary gain map can be stored in `jhgm`.
    pub jpeg_reconstruction: bool,
    /// Worker threads used while building the codestream. Defaults to 1.
    ///
    /// See [`Self::with_num_threads`].
    pub num_threads: usize,
    /// Optional Brotli implementation for JPEG reconstruction data and the
    /// Exif and XMP boxes.
    ///
    /// When absent, Jixel uses its own small Brotli encoder, so no Brotli
    /// dependency is needed; a stronger one can be supplied here.
    pub brotli_compression: Option<Box<dyn BrotliCompression>>,
    /// Entropy-coding effort. The coefficients, and so the decoded pixels and
    /// the reconstructed JPEG, are the same at every speed. Defaults to
    /// [`Speed::Fast`].
    ///
    /// [`Speed::Slow`] and [`Speed::ExtraSlow`] also try a learned DC context
    /// tree, retaining it only when the complete codestream is smaller.
    /// [`Speed::ExtraSlow`] also fits an AC context map to the DC values of
    /// all three channels on images of about a megapixel or more, kept when
    /// its AC coding is cheaper.
    pub speed: Speed,
}

impl std::fmt::Debug for JpegTranscodeConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JpegTranscodeConfig")
            .field("jpeg_reconstruction", &self.jpeg_reconstruction)
            .field("num_threads", &self.num_threads)
            .field("speed", &self.speed)
            .field(
                "brotli_compression",
                &self.brotli_compression.as_ref().map(|_| "custom"),
            )
            .finish()
    }
}

impl Default for JpegTranscodeConfig {
    fn default() -> Self {
        Self {
            jpeg_reconstruction: true,
            num_threads: std::thread::available_parallelism()
                .unwrap_or(NonZeroUsize::new(1).unwrap())
                .get(),
            brotli_compression: None,
            speed: Speed::default(),
        }
    }
}

impl JpegTranscodeConfig {
    /// Controls whether JPEG reconstruction data is embedded when supported.
    /// Ultra HDR gain maps with binary metadata are preserved either way.
    pub fn with_jpeg_reconstruction(mut self, enabled: bool) -> Self {
        self.jpeg_reconstruction = enabled;
        self
    }

    /// Sets the number of worker threads, which does not affect the output.
    pub fn with_num_threads(mut self, threads: usize) -> Self {
        self.num_threads = threads.max(1);
        self
    }

    /// Sets the entropy-coding effort; see [`Self::speed`].
    pub fn with_speed(mut self, speed: Speed) -> Self {
        self.speed = speed;
        self
    }

    /// Uses a caller-provided Brotli encoder for JPEG reconstruction data and
    /// metadata boxes.
    pub fn with_brotli_compression(mut self, compressor: Box<dyn BrotliCompression>) -> Self {
        self.brotli_compression = Some(compressor);
        self
    }
}

/// Losslessly transcodes a JPEG file into JPEG XL, keeping the ability to
/// reconstruct the original bytes.
///
/// For arithmetic-coded JPEGs (SOF9/SOF10), reconstruction produces a
/// Huffman-coded JPEG with the same image coefficients and quantization.
/// Reconstruction may consolidate table definitions and restart intervals.
///
/// Ultra HDR JPEGs with binary ISO 21496-1 metadata are carried over as a
/// `jhgm` gain map: both JPEG images are transcoded without requantization.
/// If the base uses arithmetic coding, the transcoder omits reconstruction
/// because rewriting the base would invalidate MPF offsets. XMP-only gain maps
/// rely on JPEG reconstruction.
pub fn encode_jpeg_lossless(jpeg: &[u8]) -> Result<Vec<u8>, EncodeError> {
    encode_jpeg_lossless_with_config(jpeg, &JpegTranscodeConfig::default())
}

pub fn encode_jpeg_lossless_with_config(
    jpeg: &[u8],
    config: &JpegTranscodeConfig,
) -> Result<Vec<u8>, EncodeError> {
    let mut parsed = parse_jpeg(jpeg).map_err(|e| EncodeError::Jpeg(e.to_string()))?;
    let mut exif = extract_exif(&parsed);
    // Viewers follow the codestream's orientation, not the Exif tag.
    let orientation = exif
        .and_then(|(_, tiff)| Orientation::from_exif_tiff(tiff))
        .unwrap_or_default();
    let gain_map = uhdr::encode_gain_map(&parsed, orientation, config)?;

    let reconstruct = config.jpeg_reconstruction && !(parsed.is_arithmetic && gain_map.is_some());
    if parsed.is_arithmetic && reconstruct {
        let has_mpf = parsed
            .app_data
            .iter()
            .any(|app| app.first() == Some(&0xE2) && app.get(3..7) == Some(b"MPF\0"));
        if has_mpf && parsed.tail_data.windows(2).any(|w| w == [0xFF, 0xD8]) {
            return Err(EncodeError::Jpeg(
                "arithmetic-coded MPF requires disabling JPEG reconstruction".into(),
            ));
        }
        arith::synthesize_huffman(&mut parsed).map_err(|e| EncodeError::Jpeg(e.to_string()))?;
        exif = extract_exif(&parsed);
    }

    // Embedded in the codestream either way: without it, dropping the `jbrd`
    // box would silently discard the profile and leave the image mislabeled.
    // A malformed one is left out, as libjxl does for broken chunking: the
    // image reads as sRGB and its APP2 bytes still travel in `jbrd`.
    let icc = extract_icc(&parsed).filter(|icc| icc_is_well_formed(&icc.profile));
    let xmp = extract_xmp(&parsed);
    let codestream = encode::encode_jpeg_codestream(
        &parsed,
        icc.as_ref().map(|icc| icc.profile.as_slice()),
        orientation,
        config.speed,
        config.num_threads,
    )?;

    let reconstruction = if reconstruct {
        Some(jbrd::encode_jbrd(
            &parsed,
            xmp.as_ref().map(|(index, _)| *index),
            exif.map(|(index, _)| index),
            icc.as_ref()
                .and_then(|icc| icc.in_order_apps.as_deref())
                .unwrap_or_default(),
            config.brotli_compression.as_deref(),
        )?)
    } else {
        None
    };
    if reconstruction.is_none() && exif.is_none() && xmp.is_none() && gain_map.is_none() {
        // Nothing needs a container.
        return Ok(codestream);
    }

    let mut out = Vec::with_capacity(codestream.len() + 4096);
    out.extend_from_slice(&[
        0, 0, 0, 0x0C, b'J', b'X', b'L', b' ', 0x0D, 0x0A, 0x87, 0x0A,
    ]);
    push_box(
        &mut out,
        b"ftyp",
        &[b'j', b'x', b'l', b' ', 0, 0, 0, 0, b'j', b'x', b'l', b' '],
    );
    // The reconstruction data has to precede the codestream box.
    if let Some(reconstruction) = &reconstruction {
        push_box(&mut out, b"jbrd", reconstruction);
    }
    let compressor = config.brotli_compression.as_deref();
    if let Some((_, tiff)) = exif {
        // A zero offset to the TIFF header, then the TIFF data itself.
        let mut payload = Vec::with_capacity(4 + tiff.len());
        payload.extend_from_slice(&[0; 4]);
        payload.extend_from_slice(tiff);
        push_metadata_box(&mut out, b"Exif", &payload, compressor)?;
    }
    push_box(&mut out, b"jxlc", &codestream);
    if let Some(gain_map) = gain_map {
        push_box(&mut out, b"jhgm", &gain_map);
    }
    if let Some((_, xmp)) = xmp {
        push_metadata_box(&mut out, b"xml ", &xmp, compressor)?;
    }
    Ok(out)
}

/// Appends a metadata box, as a Brotli-compressed `brob` box when that is
/// smaller.
fn push_metadata_box(
    out: &mut Vec<u8>,
    kind: &[u8; 4],
    payload: &[u8],
    compressor: Option<&dyn BrotliCompression>,
) -> Result<(), EncodeError> {
    let compressed = brotli::compress(payload, compressor)?;
    if compressed.len() + 4 < payload.len() {
        let mut brob = Vec::with_capacity(4 + compressed.len());
        brob.extend_from_slice(kind);
        brob.extend_from_slice(&compressed);
        push_box(out, b"brob", &brob);
    } else {
        push_box(out, kind, payload);
    }
    Ok(())
}

/// Number of coefficients in one DCT block.
pub(crate) const DCT_BLOCK_SIZE: usize = 64;
/// Longest Huffman code permitted by the JPEG spec.
pub(crate) const HUFF_MAX_BIT_LENGTH: usize = 16;
/// Size of the Huffman alphabet (values 0..=255).
pub(crate) const HUFF_ALPHABET_SIZE: usize = 256;
/// Maximum number of components we accept (the JPEG spec allows 4).
pub(crate) const MAX_COMPONENTS: usize = 4;

pub(crate) static NATURAL_ORDER: [usize; DCT_BLOCK_SIZE] = [
    0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20,
    13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59,
    52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
];

/// A quantization table as it appeared in a DQT segment. `values` are held in
/// natural (raster) order, de-zig-zagged at parse time.
#[derive(Debug, Clone)]
pub(crate) struct JpegQuantTable {
    pub(crate) values: [i32; DCT_BLOCK_SIZE],
    /// 0 for 8-bit tables, 1 for 16-bit tables.
    pub(crate) precision: u32,
    /// Destination slot (`Tq`), 0..=3.
    pub(crate) index: u32,
    /// Whether this table was the last one in its DQT segment.
    pub(crate) is_last: bool,
}

/// A Huffman table as it appeared in a DHT segment.
#[derive(Debug, Clone)]
pub(crate) struct JpegHuffmanCode {
    /// `counts[i]` = number of codes of length `i`, for `i` in 1..=16.
    pub(crate) counts: [u32; HUFF_MAX_BIT_LENGTH + 1],
    /// Symbol values in canonical order.
    pub(crate) values: [u32; HUFF_ALPHABET_SIZE + 1],
    /// `Tc << 4 | Th` — class in the high nibble, destination in the low one.
    pub(crate) slot_id: u32,
    /// Whether this table was the last one in its DHT segment.
    pub(crate) is_last: bool,
}

impl Default for JpegHuffmanCode {
    fn default() -> Self {
        Self {
            counts: [0; HUFF_MAX_BIT_LENGTH + 1],
            values: [0; HUFF_ALPHABET_SIZE + 1],
            slot_id: 0,
            is_last: true,
        }
    }
}

/// Per-component entry inside a scan header.
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct JpegComponentScanInfo {
    pub(crate) comp_idx: u32,
    pub(crate) dc_tbl_idx: u32,
    pub(crate) ac_tbl_idx: u32,
}

/// An AC scan position where the encoder emitted a longer zero run than needed.
#[derive(Debug, Clone, Copy)]
pub(crate) struct ExtraZeroRunInfo {
    pub(crate) block_idx: u32,
    pub(crate) num_extra_zero_runs: u32,
}

/// One scan (SOS segment) plus the extra bookkeeping needed to rebuild it.
#[derive(Debug, Clone, Default)]
pub(crate) struct JpegScanInfo {
    /// Spectral selection start.
    pub(crate) ss: u32,
    /// Spectral selection end.
    pub(crate) se: u32,
    /// Successive approximation high bit.
    pub(crate) ah: u32,
    /// Successive approximation low bit.
    pub(crate) al: u32,
    pub(crate) num_components: u32,
    pub(crate) components: [JpegComponentScanInfo; MAX_COMPONENTS],
    /// Block indices at which a non-minimal zero run was emitted.
    pub(crate) extra_zero_runs: Vec<ExtraZeroRunInfo>,
    /// Block indices where one end-of-block run immediately followed another,
    /// which the re-encoder must reproduce. Nothing to do with restart markers.
    pub(crate) reset_points: Vec<u32>,
    /// Always zero: libjxl declares the field but never sets or reads it.
    pub(crate) last_needed_pass: u32,
}

/// One image component, with its fully-reconstructed coefficient plane.
#[derive(Debug, Clone, Default)]
pub(crate) struct JpegComponent {
    /// Component identifier (`Ci`) from the frame header.
    pub(crate) id: u32,
    pub(crate) h_samp_factor: usize,
    pub(crate) v_samp_factor: usize,
    /// Index into `JpegData::quant`.
    pub(crate) quant_idx: u32,
    pub(crate) width_in_blocks: usize,
    pub(crate) height_in_blocks: usize,
    /// Quantized coefficients
    pub(crate) coeffs: Vec<i16>,
}

/// The coefficients and metadata recovered from a JPEG file.
#[derive(Debug, Clone, Default)]
pub(crate) struct JpegData {
    pub(crate) width: usize,
    pub(crate) height: usize,
    pub(crate) restart_interval: u32,
    /// Raw APPn segments, each as `[marker_byte, len_hi, len_lo, payload..]`.
    pub(crate) app_data: Vec<Vec<u8>>,
    /// Raw COM segments, same layout as `app_data`.
    pub(crate) com_data: Vec<Vec<u8>>,
    pub(crate) quant: Vec<JpegQuantTable>,
    pub(crate) huffman_code: Vec<JpegHuffmanCode>,
    pub(crate) components: Vec<JpegComponent>,
    pub(crate) scan_info: Vec<JpegScanInfo>,
    /// The marker byte of every segment encountered, in file order.
    pub(crate) marker_order: Vec<u8>,
    /// Bytes found between segments that belong to no marker.
    pub(crate) inter_marker_data: Vec<Vec<u8>>,
    /// Bytes following EOI.
    pub(crate) tail_data: Vec<u8>,
    /// Set when some entropy-coded segment padded with something other than
    /// the all-ones fill the JPEG standard prescribes. Only then does
    /// `padding_bits` have to be transmitted; otherwise the reconstruction
    /// regenerates the padding itself.
    pub(crate) has_zero_padding_bit: bool,
    /// Every padding bit observed, MSB-first within each segment and
    /// concatenated in scan order. Always collected, conditionally serialized.
    pub(crate) padding_bits: Vec<u8>,
    /// True if the frame used SOF2 (progressive) rather than SOF0/SOF1.
    pub(crate) is_progressive: bool,
    /// True if the source was arithmetic-coded (SOF9/SOF10). Reconstruction
    /// requires preparing the equivalent Huffman markers and tables first.
    pub(crate) is_arithmetic: bool,
}

impl JpegData {
    /// Maximum horizontal sampling factor across all components.
    pub(crate) fn max_h_samp(&self) -> usize {
        self.components
            .iter()
            .map(|c| c.h_samp_factor)
            .max()
            .unwrap_or(1)
    }

    /// Maximum vertical sampling factor across all components.
    pub(crate) fn max_v_samp(&self) -> usize {
        self.components
            .iter()
            .map(|c| c.v_samp_factor)
            .max()
            .unwrap_or(1)
    }
}

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

    /// Builds an APP2 segment carrying one chunk of an ICC profile.
    fn icc_app2(seq: u8, count: u8, body: &[u8]) -> Vec<u8> {
        let mut payload = b"ICC_PROFILE\0".to_vec();
        payload.push(seq);
        payload.push(count);
        payload.extend_from_slice(body);
        let mut seg = vec![0xE2u8];
        seg.extend_from_slice(&((payload.len() + 2) as u16).to_be_bytes());
        seg.extend_from_slice(&payload);
        seg
    }

    fn xmp_app1(body: &[u8]) -> Vec<u8> {
        let mut payload = b"http://ns.adobe.com/xap/1.0/\0".to_vec();
        payload.extend_from_slice(body);
        let mut seg = vec![0xE1u8];
        seg.extend_from_slice(&((payload.len() + 2) as u16).to_be_bytes());
        seg.extend_from_slice(&payload);
        seg
    }

    fn with_app(segments: Vec<Vec<u8>>) -> JpegData {
        JpegData {
            app_data: segments,
            ..Default::default()
        }
    }

    #[test]
    fn reassembles_a_split_icc_profile() {
        // Deliberately out of order: chunks carry their own sequence numbers.
        let jpg = with_app(vec![
            icc_app2(2, 3, b"world"),
            icc_app2(1, 3, b"hello "),
            icc_app2(3, 3, b"!"),
        ]);
        let icc = extract_icc(&jpg).unwrap();
        assert_eq!(icc.profile, b"hello world!");
        // Out of file order, so `jbrd` has to keep the segments verbatim.
        assert_eq!(icc.in_order_apps, None);
    }

    #[test]
    fn reads_a_single_chunk_profile() {
        let jpg = with_app(vec![icc_app2(1, 1, b"profile")]);
        let icc = extract_icc(&jpg).unwrap();
        assert_eq!(icc.profile, b"profile");
        assert_eq!(icc.in_order_apps.as_deref(), Some(&[0][..]));
    }

    #[test]
    fn rejects_incomplete_or_inconsistent_profiles() {
        // A chunk is missing.
        let missing = with_app(vec![icc_app2(1, 2, b"half")]);
        assert_eq!(extract_icc(&missing), None);

        // Chunk counts disagree.
        let inconsistent = with_app(vec![icc_app2(1, 2, b"a"), icc_app2(2, 3, b"b")]);
        assert_eq!(extract_icc(&inconsistent), None);

        // Sequence numbers are 1-based and contiguous.
        let bad_seq = with_app(vec![icc_app2(0, 1, b"a")]);
        assert_eq!(extract_icc(&bad_seq), None);
    }

    #[test]
    fn in_order_chunks_are_reported_for_regeneration() {
        let mut exif = vec![0xE1u8, 0x00, 0x05];
        exif.extend_from_slice(b"Exi");
        let jpg = with_app(vec![
            icc_app2(1, 2, b"hello "),
            exif,
            icc_app2(2, 2, b"world"),
        ]);
        let icc = extract_icc(&jpg).unwrap();
        assert_eq!(icc.profile, b"hello world");
        assert_eq!(icc.in_order_apps.as_deref(), Some(&[0, 2][..]));
    }

    /// A minimal structurally valid profile: header plus one tag.
    fn tiny_profile() -> Vec<u8> {
        let mut icc = vec![0u8; 128];
        icc[36..40].copy_from_slice(b"acsp");
        icc.extend_from_slice(&1u32.to_be_bytes());
        icc.extend_from_slice(b"desc");
        icc.extend_from_slice(&144u32.to_be_bytes());
        icc.extend_from_slice(&4u32.to_be_bytes());
        icc.extend_from_slice(b"data");
        let len = icc.len() as u32;
        icc[..4].copy_from_slice(&len.to_be_bytes());
        icc
    }

    #[test]
    fn well_formed_profiles_pass_and_damaged_ones_do_not() {
        let good = tiny_profile();
        assert!(icc_is_well_formed(&good));

        assert!(!icc_is_well_formed(b"hello world"));
        let mut bad_magic = good.clone();
        bad_magic[36] = b'x';
        assert!(!icc_is_well_formed(&bad_magic));
        // Size field disagrees with the reassembled length.
        let mut truncated = good.clone();
        truncated.pop();
        assert!(!icc_is_well_formed(&truncated));
        // A tag pointing past the end.
        let mut bad_tag = good.clone();
        bad_tag[136..140].copy_from_slice(&200u32.to_be_bytes());
        assert!(!icc_is_well_formed(&bad_tag));
        // More tags than the table can hold.
        let mut bad_count = good;
        bad_count[128..132].copy_from_slice(&1000u32.to_be_bytes());
        assert!(!icc_is_well_formed(&bad_count));
    }

    /// A TIFF block whose first directory holds `entries` as (tag, type,
    /// count, value) with SHORT values left-aligned in the value field.
    fn tiff(big_endian: bool, entries: &[(u16, u16, u32, u16)]) -> Vec<u8> {
        let u16b = |v: u16| {
            if big_endian {
                v.to_be_bytes()
            } else {
                v.to_le_bytes()
            }
        };
        let u32b = |v: u32| {
            if big_endian {
                v.to_be_bytes()
            } else {
                v.to_le_bytes()
            }
        };
        let mut t = if big_endian {
            b"MM\0*".to_vec()
        } else {
            b"II*\0".to_vec()
        };
        t.extend_from_slice(&u32b(8));
        t.extend_from_slice(&u16b(entries.len() as u16));
        for &(tag, kind, count, value) in entries {
            t.extend_from_slice(&u16b(tag));
            t.extend_from_slice(&u16b(kind));
            t.extend_from_slice(&u32b(count));
            t.extend_from_slice(&u16b(value));
            t.extend_from_slice(&[0, 0]);
        }
        t.extend_from_slice(&u32b(0));
        t
    }

    #[test]
    fn reads_the_exif_orientation_in_either_byte_order() {
        for big_endian in [true, false] {
            let t = tiff(big_endian, &[(0x010F, 2, 4, 0), (0x0112, 3, 1, 8)]);
            assert_eq!(Orientation::from_exif_tiff(&t), Orientation::from_exif(8));
        }
        // No tag, a wrong type, an out-of-range value or junk: no orientation.
        assert_eq!(
            Orientation::from_exif_tiff(&tiff(true, &[(0x010F, 2, 4, 0)])),
            None
        );
        assert_eq!(
            Orientation::from_exif_tiff(&tiff(true, &[(0x0112, 4, 1, 6)])),
            None
        );
        assert_eq!(
            Orientation::from_exif_tiff(&tiff(false, &[(0x0112, 3, 1, 9)])),
            None
        );
        assert_eq!(Orientation::from_exif_tiff(b"MM\0*\0\0\0\x40"), None);
        assert_eq!(Orientation::from_exif_tiff(b"nonsense"), None);
    }

    #[test]
    fn finds_the_first_exif_segment() {
        let mut exif = vec![0xE1u8, 0, 0];
        exif.extend_from_slice(b"Exif\0\0");
        exif.extend_from_slice(&tiff(true, &[(0x0112, 3, 1, 6)]));
        let jpg = with_app(vec![xmp_app1(b"<x/>"), exif.clone(), exif.clone()]);
        let (index, data) = extract_exif(&jpg).unwrap();
        assert_eq!(index, 1);
        assert_eq!(Orientation::from_exif_tiff(data), Orientation::from_exif(6));
        // A bare tag with no TIFF data is not Exif.
        assert_eq!(
            extract_exif(&with_app(vec![vec![
                0xE1, 0, 8, b'E', b'x', b'i', b'f', 0, 0
            ]])),
            None
        );
    }

    #[test]
    fn ignores_unrelated_app_segments() {
        let mut jfif = vec![0xE0u8, 0x00, 0x10];
        jfif.extend_from_slice(b"JFIF\0\x01\x02\x00\x00\x01\x00\x01\x00\x00");
        assert_eq!(extract_icc(&with_app(vec![jfif])), None);
    }

    #[test]
    fn extracts_the_first_standard_xmp_packet() {
        let jpg = with_app(vec![
            vec![0xE1, 0, 5, b'n', b'o', b'p'],
            xmp_app1(b"<x:xmpmeta>first</x:xmpmeta>"),
            xmp_app1(b"<x:xmpmeta>second</x:xmpmeta>"),
        ]);
        let (index, xmp) = extract_xmp(&jpg).expect("XMP packet");
        assert_eq!(index, 1);
        assert_eq!(xmp, b"<x:xmpmeta>first</x:xmpmeta>");
    }

    #[test]
    fn reconstruction_is_on_by_default() {
        let default = JpegTranscodeConfig::default();
        assert!(default.jpeg_reconstruction);
        assert!(default.brotli_compression.is_none());
        assert!(
            !JpegTranscodeConfig::default()
                .with_jpeg_reconstruction(false)
                .jpeg_reconstruction
        );
    }
}