zenjpeg 0.8.2

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
//! Lightweight JPEG marker scanner for header-only probing.
//!
//! Parses marker headers without entropy decoding. Extracts DQT tables,
//! SOF parameters, DHT symbol counts, APP markers, and SOS count.

use alloc::vec::Vec;

use crate::foundation::consts::{
    JPEG_NATURAL_ORDER, MARKER_APP0, MARKER_APP2, MARKER_DQT, MARKER_EOI, MARKER_SOF0, MARKER_SOF1,
    MARKER_SOF2, MARKER_SOF9, MARKER_SOF10, MARKER_SOI, MARKER_SOS,
};

/// Raw data extracted from JPEG headers.
#[derive(Debug)]
pub(crate) struct ScanResult {
    /// DQT tables in natural (row-major) order, indexed by table ID (0-3).
    pub dqt_tables: [Option<DqtEntry>; 4],
    /// SOF parameters (None if no SOF found).
    pub sof: Option<SofInfo>,
    /// Total AC Huffman symbol count (sum across all AC tables).
    pub total_ac_symbols: u16,
    /// Number of DHT tables found.
    pub dht_count: u8,
    /// Whether a JFIF APP0 marker was found.
    pub has_jfif: bool,
    /// Whether an ICC_PROFILE APP2 marker was found.
    pub has_icc_profile: bool,
    /// Whether an Adobe APP14 marker was found.
    pub has_adobe: bool,
    /// Whether a Photoshop 3.0 APP13 (IPTC) marker was found.
    pub has_photoshop_iptc: bool,
    /// Number of SOS markers found.
    pub sos_count: u16,
}

/// A single DQT table entry.
#[derive(Debug, Clone)]
pub(crate) struct DqtEntry {
    /// Quantization values in natural (row-major) order.
    pub values: [u16; 64],
    /// Precision: 0 = 8-bit, 1 = 16-bit.
    pub precision: u8,
}

/// SOF (Start of Frame) parameters.
#[derive(Debug, Clone)]
pub(crate) struct SofInfo {
    /// SOF marker type (0xC0, 0xC1, 0xC2, 0xC9, 0xCA).
    pub marker: u8,
    /// Image width in pixels.
    pub width: u16,
    /// Image height in pixels.
    pub height: u16,
    /// Number of components.
    pub num_components: u8,
    /// Per-component info: (id, h_samp, v_samp, quant_table_idx).
    pub components: Vec<(u8, u8, u8, u8)>,
}

/// Errors from scanning.
#[derive(Debug)]
pub(crate) enum ScanError {
    TooShort,
    NotJpeg,
    Truncated,
}

/// Scan JPEG headers and extract structural information.
///
/// Reads only marker headers — no entropy decoding. Stops fully parsing
/// after finding SOS but continues scanning for additional SOS markers
/// to count scans in progressive files.
pub(crate) fn scan_headers(data: &[u8]) -> Result<ScanResult, ScanError> {
    if data.len() < 2 {
        return Err(ScanError::TooShort);
    }

    // Check SOI marker
    if data[0] != 0xFF || data[1] != MARKER_SOI {
        return Err(ScanError::NotJpeg);
    }

    let mut result = ScanResult {
        dqt_tables: [const { None }; 4],
        sof: None,
        total_ac_symbols: 0,
        dht_count: 0,
        has_jfif: false,
        has_icc_profile: false,
        has_adobe: false,
        has_photoshop_iptc: false,
        sos_count: 0,
    };

    let mut pos = 2;

    loop {
        // Find next marker
        pos = match find_marker(data, pos) {
            Some(p) => p,
            None => break,
        };

        if pos + 1 >= data.len() {
            break;
        }

        let marker = data[pos + 1];
        pos += 2; // Skip 0xFF and marker byte

        match marker {
            MARKER_EOI => break,

            MARKER_DQT => {
                pos = parse_dqt(data, pos, &mut result)?;
            }

            m if is_sof_marker(m) => {
                pos = parse_sof(data, pos, m, &mut result)?;
            }

            // DHT (0xC4)
            0xC4 => {
                pos = parse_dht(data, pos, &mut result)?;
            }

            MARKER_SOS => {
                result.sos_count += 1;
                // Skip the SOS header
                if pos + 1 >= data.len() {
                    break;
                }
                let len = read_u16(data, pos) as usize;
                pos += len;
                // Skip entropy data until next marker
                pos = skip_entropy_data(data, pos);
            }

            // APP0 (JFIF check)
            MARKER_APP0 => {
                pos = parse_app0(data, pos, &mut result)?;
            }

            // APP2 (ICC profile check)
            MARKER_APP2 => {
                pos = parse_app2(data, pos, &mut result)?;
            }

            // APP13 (IPTC / Photoshop 3.0 check)
            0xED => {
                pos = parse_app13(data, pos, &mut result)?;
            }

            // APP14 (Adobe check, 0xEE)
            0xEE => {
                pos = parse_app14(data, pos, &mut result)?;
            }

            // Restart markers (0xD0-0xD7) — no length field
            0xD0..=0xD7 => {
                // No payload, continue
            }

            // All other markers have a length field
            _ => {
                if pos + 1 >= data.len() {
                    break;
                }
                let len = read_u16(data, pos) as usize;
                if len < 2 || pos + len > data.len() {
                    break;
                }
                pos += len;
            }
        }
    }

    Ok(result)
}

/// Find the next 0xFF marker byte, skipping any padding 0xFF bytes.
fn find_marker(data: &[u8], mut pos: usize) -> Option<usize> {
    while pos < data.len() {
        if data[pos] == 0xFF {
            // Skip padding 0xFF bytes
            while pos + 1 < data.len() && data[pos + 1] == 0xFF {
                pos += 1;
            }
            if pos + 1 < data.len() && data[pos + 1] != 0x00 {
                return Some(pos);
            }
        }
        pos += 1;
    }
    None
}

/// Check if a marker byte is a SOF marker we care about.
fn is_sof_marker(m: u8) -> bool {
    matches!(
        m,
        MARKER_SOF0 | MARKER_SOF1 | MARKER_SOF2 | MARKER_SOF9 | MARKER_SOF10
    )
}

/// Parse DQT marker segment. Can contain multiple tables.
fn parse_dqt(data: &[u8], pos: usize, result: &mut ScanResult) -> Result<usize, ScanError> {
    if pos + 1 >= data.len() {
        return Err(ScanError::Truncated);
    }
    let seg_len = read_u16(data, pos) as usize;
    if seg_len < 2 || pos + seg_len > data.len() {
        return Err(ScanError::Truncated);
    }

    let seg_end = pos + seg_len;
    let mut p = pos + 2; // Skip length field

    while p < seg_end {
        if p >= data.len() {
            return Err(ScanError::Truncated);
        }

        let pq_tq = data[p];
        let precision = (pq_tq >> 4) & 0x0F; // 0 = 8-bit, 1 = 16-bit
        let table_id = (pq_tq & 0x0F) as usize;
        p += 1;

        if table_id > 3 {
            // Invalid table ID, skip
            let value_bytes = if precision == 0 { 64 } else { 128 };
            p += value_bytes;
            continue;
        }

        let mut values_zigzag = [0u16; 64];

        if precision == 0 {
            // 8-bit values
            if p + 64 > data.len() {
                return Err(ScanError::Truncated);
            }
            for i in 0..64 {
                values_zigzag[i] = data[p + i] as u16;
            }
            p += 64;
        } else {
            // 16-bit values
            if p + 128 > data.len() {
                return Err(ScanError::Truncated);
            }
            for i in 0..64 {
                values_zigzag[i] = read_u16(data, p + i * 2);
            }
            p += 128;
        }

        // Convert from zigzag to natural (row-major) order
        let mut values_natural = [0u16; 64];
        for zigzag_idx in 0..64 {
            let natural_idx = JPEG_NATURAL_ORDER[zigzag_idx] as usize;
            values_natural[natural_idx] = values_zigzag[zigzag_idx];
        }

        result.dqt_tables[table_id] = Some(DqtEntry {
            values: values_natural,
            precision,
        });
    }

    Ok(seg_end)
}

/// Parse SOF marker segment.
fn parse_sof(
    data: &[u8],
    pos: usize,
    marker: u8,
    result: &mut ScanResult,
) -> Result<usize, ScanError> {
    if pos + 1 >= data.len() {
        return Err(ScanError::Truncated);
    }
    let seg_len = read_u16(data, pos) as usize;
    if seg_len < 8 || pos + seg_len > data.len() {
        return Err(ScanError::Truncated);
    }

    let precision = data[pos + 2];
    let _ = precision; // We don't need sample precision for detection
    let height = read_u16(data, pos + 3);
    let width = read_u16(data, pos + 5);
    let num_components = data[pos + 7];

    let expected_len = 8 + num_components as usize * 3;
    if seg_len < expected_len || pos + expected_len > data.len() {
        return Err(ScanError::Truncated);
    }

    let mut components = Vec::new();
    for c in 0..num_components as usize {
        let offset = pos + 8 + c * 3;
        let id = data[offset];
        let sampling = data[offset + 1];
        let h_samp = (sampling >> 4) & 0x0F;
        let v_samp = sampling & 0x0F;
        let quant_table_idx = data[offset + 2];
        components.push((id, h_samp, v_samp, quant_table_idx));
    }

    result.sof = Some(SofInfo {
        marker,
        width,
        height,
        num_components,
        components,
    });

    Ok(pos + seg_len)
}

/// Parse DHT marker segment. Counts total AC symbols across all tables.
fn parse_dht(data: &[u8], pos: usize, result: &mut ScanResult) -> Result<usize, ScanError> {
    if pos + 1 >= data.len() {
        return Err(ScanError::Truncated);
    }
    let seg_len = read_u16(data, pos) as usize;
    if seg_len < 2 || pos + seg_len > data.len() {
        return Err(ScanError::Truncated);
    }

    let seg_end = pos + seg_len;
    let mut p = pos + 2;

    while p < seg_end {
        if p >= data.len() {
            return Err(ScanError::Truncated);
        }

        let tc_th = data[p];
        let table_class = (tc_th >> 4) & 0x0F; // 0 = DC, 1 = AC
        p += 1;

        if p + 16 > data.len() {
            return Err(ScanError::Truncated);
        }

        // Count symbols from the 16-byte `bits` array
        let mut total_symbols: u16 = 0;
        for i in 0..16 {
            total_symbols += data[p + i] as u16;
        }
        p += 16;

        // Track AC symbol counts for Huffman discrimination
        if table_class == 1 {
            result.total_ac_symbols += total_symbols;
        }

        result.dht_count += 1;

        // Skip symbol values
        let sym_count = total_symbols as usize;
        if p + sym_count > data.len() {
            return Err(ScanError::Truncated);
        }
        p += sym_count;
    }

    Ok(seg_end)
}

/// Parse APP0 marker — check for JFIF identifier.
fn parse_app0(data: &[u8], pos: usize, result: &mut ScanResult) -> Result<usize, ScanError> {
    if pos + 1 >= data.len() {
        return Err(ScanError::Truncated);
    }
    let seg_len = read_u16(data, pos) as usize;
    if seg_len < 2 || pos + seg_len > data.len() {
        return Err(ScanError::Truncated);
    }

    // Check for "JFIF\0" identifier at offset 2
    if seg_len >= 7 && pos + 6 < data.len() {
        let id = &data[pos + 2..pos + 7];
        if id == b"JFIF\0" {
            result.has_jfif = true;
        }
    }

    Ok(pos + seg_len)
}

/// Parse APP2 marker — check for ICC_PROFILE identifier.
fn parse_app2(data: &[u8], pos: usize, result: &mut ScanResult) -> Result<usize, ScanError> {
    if pos + 1 >= data.len() {
        return Err(ScanError::Truncated);
    }
    let seg_len = read_u16(data, pos) as usize;
    if seg_len < 2 || pos + seg_len > data.len() {
        return Err(ScanError::Truncated);
    }

    // Check for "ICC_PROFILE\0" identifier at offset 2
    if seg_len >= 14 && pos + 14 <= data.len() {
        let id = &data[pos + 2..pos + 14];
        if id == b"ICC_PROFILE\0" {
            result.has_icc_profile = true;
        }
    }

    Ok(pos + seg_len)
}

/// Parse APP13 marker — check for Photoshop 3.0 (IPTC) identifier.
fn parse_app13(data: &[u8], pos: usize, result: &mut ScanResult) -> Result<usize, ScanError> {
    if pos + 1 >= data.len() {
        return Err(ScanError::Truncated);
    }
    let seg_len = read_u16(data, pos) as usize;
    if seg_len < 2 || pos + seg_len > data.len() {
        return Err(ScanError::Truncated);
    }

    // Check for "Photoshop 3.0\0" identifier at offset 2
    if seg_len >= 16 && pos + 16 <= data.len() {
        let id = &data[pos + 2..pos + 16];
        if id == b"Photoshop 3.0\0" {
            result.has_photoshop_iptc = true;
        }
    }

    Ok(pos + seg_len)
}

/// Parse APP14 marker — check for Adobe identifier.
fn parse_app14(data: &[u8], pos: usize, result: &mut ScanResult) -> Result<usize, ScanError> {
    if pos + 1 >= data.len() {
        return Err(ScanError::Truncated);
    }
    let seg_len = read_u16(data, pos) as usize;
    if seg_len < 2 || pos + seg_len > data.len() {
        return Err(ScanError::Truncated);
    }

    // Check for "Adobe" identifier at offset 2
    if seg_len >= 7 && pos + 7 <= data.len() {
        let id = &data[pos + 2..pos + 7];
        if id == b"Adobe" {
            result.has_adobe = true;
        }
    }

    Ok(pos + seg_len)
}

/// Skip entropy-coded data after an SOS marker.
/// Looks for the next 0xFF byte that's not followed by 0x00 or a restart marker.
fn skip_entropy_data(data: &[u8], mut pos: usize) -> usize {
    while pos < data.len() {
        if data[pos] == 0xFF {
            if pos + 1 >= data.len() {
                return pos;
            }
            let next = data[pos + 1];
            if next == 0x00 {
                // Byte-stuffed 0xFF in data stream — skip both bytes
                pos += 2;
                continue;
            }
            if (0xD0..=0xD7).contains(&next) {
                // Restart marker — skip and continue entropy data
                pos += 2;
                continue;
            }
            // Found a real marker — back up so find_marker can see it
            return pos;
        }
        pos += 1;
    }
    pos
}

/// Read a big-endian u16 from data at the given position.
#[inline]
fn read_u16(data: &[u8], pos: usize) -> u16 {
    (data[pos] as u16) << 8 | data[pos + 1] as u16
}