j2k 0.6.2

JPEG 2000 and HTJ2K decoder/encoder APIs with optional GPU acceleration
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
// SPDX-License-Identifier: MIT OR Apache-2.0

use super::{codestream::parse_codestream, ParsedImageInfo};
use crate::{
    J2kChannelAssociation, J2kChannelDefinition, J2kChannelType, J2kColorSpec, J2kComponentInfo,
    J2kComponentMapping, J2kComponentMappingType, J2kError, J2kFileMetadata, J2kPaletteColumn,
    J2kPaletteMetadata,
};
use j2k_core::{Colorspace, CompressedPayloadKind, InputError};

const JP2_SIGNATURE: [u8; 12] = [0, 0, 0, 12, b'j', b'P', b' ', b' ', 0x0D, 0x0A, 0x87, 0x0A];
const JP2_SIGNATURE_PREFIX: [u8; 8] = [0, 0, 0, 12, b'j', b'P', b' ', b' '];

pub(crate) fn looks_like_jp2(input: &[u8]) -> bool {
    input.starts_with(&JP2_SIGNATURE_PREFIX)
}

pub(crate) fn parse_jp2(input: &[u8]) -> Result<ParsedImageInfo, J2kError> {
    if input.len() < JP2_SIGNATURE.len() {
        return Err(InputError::TooShort {
            need: JP2_SIGNATURE.len(),
            have: input.len(),
        }
        .into());
    }

    let mut offset = 0usize;
    let mut saw_signature = false;
    let mut saw_ftyp = false;
    let mut saw_jp2h = false;
    let mut saw_ihdr = false;
    let mut image_header = None;
    let mut file_metadata = None;
    let mut codestream = None;
    let mut payload_kind = CompressedPayloadKind::Jp2File;

    while offset < input.len() {
        let header = read_box_header(input, offset)?;
        if header.end > input.len() {
            return Err(InputError::TruncatedAt {
                offset,
                segment: "box payload",
            }
            .into());
        }
        let payload = &input[header.payload_start..header.end];
        match &header.box_type {
            b"jP  " => {
                if saw_signature || offset != 0 {
                    return Err(J2kError::InvalidBox {
                        offset,
                        what: "signature box must appear exactly once at the start of the file",
                    });
                }
                if payload != &JP2_SIGNATURE[8..] {
                    return Err(J2kError::InvalidBox {
                        offset,
                        what: "invalid JP2 signature payload",
                    });
                }
                saw_signature = true;
            }
            b"ftyp" => {
                if !saw_signature || saw_ftyp || saw_jp2h || codestream.is_some() {
                    return Err(J2kError::InvalidBox {
                        offset,
                        what: "file type box must appear exactly once before jp2h and jp2c",
                    });
                }
                if payload.len() < 8 {
                    return Err(J2kError::InvalidBox {
                        offset,
                        what: "ftyp payload shorter than 8 bytes",
                    });
                }
                payload_kind = classify_file_type(payload);
                saw_ftyp = true;
            }
            b"jp2h" => {
                if !saw_ftyp || saw_jp2h || codestream.is_some() {
                    return Err(J2kError::InvalidBox {
                        offset,
                        what: "jp2h must appear exactly once after ftyp and before jp2c",
                    });
                }
                let (ihdr, metadata) = parse_jp2h(payload, header.payload_start)?;
                saw_jp2h = true;
                saw_ihdr = ihdr.is_some();
                image_header = ihdr;
                file_metadata = Some(metadata);
            }
            b"jp2c" => {
                if !saw_jp2h || codestream.is_some() {
                    return Err(J2kError::InvalidBox {
                        offset,
                        what: "jp2c must appear exactly once after jp2h",
                    });
                }
                codestream = Some(payload);
            }
            _ => {}
        }
        offset = header.end;
    }

    if !saw_signature {
        return Err(J2kError::MissingRequiredBox { box_type: "jP  " });
    }
    if !saw_ftyp {
        return Err(J2kError::MissingRequiredBox { box_type: "ftyp" });
    }
    if !saw_jp2h {
        return Err(J2kError::MissingRequiredBox { box_type: "jp2h" });
    }
    if !saw_ihdr {
        return Err(J2kError::MissingRequiredBox { box_type: "ihdr" });
    }
    let codestream = codestream.ok_or(J2kError::MissingRequiredBox { box_type: "jp2c" })?;
    let parsed = parse_codestream(codestream)?;
    if let Some(ihdr) = image_header {
        validate_ihdr_matches_codestream(ihdr, &parsed.siz)?;
        validate_component_metadata(ihdr, file_metadata.as_ref(), &parsed.siz)?;
    }
    if payload_kind == CompressedPayloadKind::JphFile && !parsed.cod.high_throughput {
        return Err(J2kError::InvalidBox {
            offset: 0,
            what: "JPH file type requires an HTJ2K codestream",
        });
    }
    if payload_kind == CompressedPayloadKind::Jp2File && parsed.cod.high_throughput {
        return Err(J2kError::InvalidBox {
            offset: 0,
            what: "JP2 file type must not wrap an HTJ2K codestream; use JPH",
        });
    }
    let colorspace = file_metadata
        .as_ref()
        .and_then(primary_colorspace_from_file_metadata);
    let info = parsed.clone().into_info(colorspace);
    let components = parsed.siz.component_info.clone();
    Ok(ParsedImageInfo {
        info,
        transfer_syntax: parsed.transfer_syntax(),
        payload_kind,
        components,
        file_metadata,
    })
}

fn classify_file_type(payload: &[u8]) -> CompressedPayloadKind {
    if payload.len() >= 4 && &payload[..4] == b"jph " {
        return CompressedPayloadKind::JphFile;
    }
    if payload[8..]
        .chunks_exact(4)
        .any(|compatible_brand| compatible_brand == b"jph ")
    {
        return CompressedPayloadKind::JphFile;
    }
    CompressedPayloadKind::Jp2File
}

#[derive(Debug, Clone, Copy)]
struct Jp2ImageHeader {
    offset: usize,
    width: u32,
    height: u32,
    components: u16,
    bits_per_component: Option<J2kComponentInfo>,
}

fn parse_jp2h(
    payload: &[u8],
    base_offset: usize,
) -> Result<(Option<Jp2ImageHeader>, J2kFileMetadata), J2kError> {
    let mut offset = 0usize;
    let mut image_header = None;
    let mut metadata = J2kFileMetadata {
        bits_per_component: Vec::new(),
        color_specs: Vec::new(),
        palette: None,
        component_mappings: Vec::new(),
        channel_definitions: Vec::new(),
        has_palette: false,
        has_component_mapping: false,
        has_channel_definition: false,
    };

    while offset < payload.len() {
        let header = read_box_header(payload, offset)?;
        if header.end > payload.len() {
            return Err(InputError::TruncatedAt {
                offset: base_offset + offset,
                segment: "box payload",
            }
            .into());
        }
        let inner = &payload[header.payload_start..header.end];
        match &header.box_type {
            b"ihdr" => {
                if image_header.is_some() {
                    return Err(J2kError::InvalidBox {
                        offset: base_offset + offset,
                        what: "ihdr must appear exactly once",
                    });
                }
                image_header = Some(parse_ihdr(inner, base_offset + offset)?);
            }
            b"colr" => {
                metadata.color_specs.push(parse_colr(inner));
            }
            b"bpcc" => {
                metadata.bits_per_component = parse_bpcc(inner, base_offset + offset)?;
            }
            b"pclr" => {
                metadata.has_palette = true;
                metadata.palette = Some(parse_pclr(inner, base_offset + offset)?);
            }
            b"cmap" => {
                metadata.has_component_mapping = true;
                metadata.component_mappings = parse_cmap(inner, base_offset + offset)?;
            }
            b"cdef" => {
                metadata.has_channel_definition = true;
                metadata.channel_definitions = parse_cdef(inner, base_offset + offset)?;
            }
            _ => {}
        }
        offset = header.end;
    }

    if metadata.color_specs.is_empty() {
        return Err(J2kError::MissingRequiredBox { box_type: "colr" });
    }

    Ok((image_header, metadata))
}

fn parse_ihdr(payload: &[u8], offset: usize) -> Result<Jp2ImageHeader, J2kError> {
    if payload.len() < 14 {
        return Err(J2kError::InvalidBox {
            offset,
            what: "ihdr payload shorter than 14 bytes",
        });
    }
    if payload[11] != 7 {
        return Err(J2kError::InvalidBox {
            offset,
            what: "ihdr compression type must be JPEG 2000",
        });
    }
    Ok(Jp2ImageHeader {
        offset,
        height: u32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]]),
        width: u32::from_be_bytes([payload[4], payload[5], payload[6], payload[7]]),
        components: u16::from_be_bytes([payload[8], payload[9]]),
        bits_per_component: if payload[10] == 0xff {
            None
        } else {
            Some(parse_component_descriptor(payload[10], offset, "ihdr bpc")?)
        },
    })
}

fn validate_ihdr_matches_codestream(
    ihdr: Jp2ImageHeader,
    siz: &super::ParsedSiz,
) -> Result<(), J2kError> {
    if (ihdr.width, ihdr.height) != siz.dimensions {
        return Err(J2kError::InvalidBox {
            offset: ihdr.offset,
            what: "ihdr dimensions must match codestream image dimensions",
        });
    }
    Ok(())
}

fn validate_component_metadata(
    ihdr: Jp2ImageHeader,
    metadata: Option<&J2kFileMetadata>,
    siz: &super::ParsedSiz,
) -> Result<(), J2kError> {
    let resolved = metadata.and_then(|metadata| resolved_image_component_metadata(metadata, siz));
    let resolved = resolved.as_deref().unwrap_or(&siz.component_info);
    if resolved.len() != ihdr.components as usize {
        return Err(J2kError::InvalidBox {
            offset: ihdr.offset,
            what: "ihdr component count must match resolved JP2 image components",
        });
    }

    if let Some(descriptor) = ihdr.bits_per_component {
        if metadata.is_some_and(|metadata| !metadata.bits_per_component.is_empty()) {
            return Err(J2kError::InvalidBox {
                offset: ihdr.offset,
                what: "bpcc must not be present when ihdr bpc is explicit",
            });
        }
        if !resolved
            .iter()
            .all(|component| same_component_precision(*component, descriptor))
        {
            return Err(J2kError::InvalidBox {
                offset: ihdr.offset,
                what: "ihdr bpc must match resolved JP2 image component precision",
            });
        }
    } else {
        let Some(metadata) = metadata else {
            return Err(J2kError::InvalidBox {
                offset: ihdr.offset,
                what: "ihdr bpc=255 requires bpcc",
            });
        };
        if metadata.bits_per_component.len() != ihdr.components as usize {
            return Err(J2kError::InvalidBox {
                offset: ihdr.offset,
                what: "bpcc component count must match ihdr component count",
            });
        }
        for (component, descriptor) in resolved.iter().zip(metadata.bits_per_component.iter()) {
            if !same_component_precision(*component, *descriptor) {
                return Err(J2kError::InvalidBox {
                    offset: ihdr.offset,
                    what: "bpcc entries must match resolved JP2 image component precision",
                });
            }
        }
    }

    Ok(())
}

fn resolved_image_component_metadata(
    metadata: &J2kFileMetadata,
    siz: &super::ParsedSiz,
) -> Option<Vec<J2kComponentInfo>> {
    if metadata.component_mappings.is_empty() {
        if let Some(palette) = metadata.palette.as_ref() {
            return Some(
                palette
                    .columns
                    .iter()
                    .copied()
                    .map(component_from_palette_column)
                    .collect(),
            );
        }
        return Some(siz.component_info.clone());
    }

    let mut resolved = Vec::with_capacity(metadata.component_mappings.len());
    for mapping in &metadata.component_mappings {
        match mapping.mapping_type {
            J2kComponentMappingType::Direct => {
                let component = siz.component_info.get(mapping.component_index as usize)?;
                resolved.push(*component);
            }
            J2kComponentMappingType::Palette { column } => {
                let palette = metadata.palette.as_ref()?;
                let column = palette.columns.get(column as usize)?;
                resolved.push(component_from_palette_column(*column));
            }
            J2kComponentMappingType::Unknown { .. } => return None,
        }
    }

    Some(resolved)
}

fn component_from_palette_column(column: J2kPaletteColumn) -> J2kComponentInfo {
    J2kComponentInfo {
        bit_depth: column.bit_depth,
        signed: column.signed,
        x_rsiz: 1,
        y_rsiz: 1,
    }
}

fn same_component_precision(left: J2kComponentInfo, right: J2kComponentInfo) -> bool {
    left.bit_depth == right.bit_depth && left.signed == right.signed
}

fn parse_colr(payload: &[u8]) -> J2kColorSpec {
    if payload.len() < 3 {
        return J2kColorSpec::Unknown { method: 0 };
    }
    match payload[0] {
        1 if payload.len() >= 7 => J2kColorSpec::Enumerated {
            value: u32::from_be_bytes([payload[3], payload[4], payload[5], payload[6]]),
        },
        2 => J2kColorSpec::IccProfile {
            profile: payload[3..].to_vec(),
        },
        method => J2kColorSpec::Unknown { method },
    }
}

fn primary_colorspace_from_file_metadata(metadata: &J2kFileMetadata) -> Option<Colorspace> {
    metadata
        .color_specs
        .first()
        .map(|color_spec| match color_spec {
            J2kColorSpec::Enumerated { value: 16 } => Colorspace::SRgb,
            J2kColorSpec::Enumerated { value: 17 } => Colorspace::SGray,
            J2kColorSpec::Enumerated { value: 18 } => Colorspace::YCbCr,
            J2kColorSpec::Enumerated { .. } | J2kColorSpec::IccProfile { .. } => {
                Colorspace::IccTagged
            }
            J2kColorSpec::Unknown { .. } => Colorspace::IccTagged,
        })
}

fn parse_bpcc(payload: &[u8], offset: usize) -> Result<Vec<J2kComponentInfo>, J2kError> {
    if payload.is_empty() {
        return Err(J2kError::InvalidBox {
            offset,
            what: "bpcc payload must not be empty",
        });
    }
    payload
        .iter()
        .map(|descriptor| parse_component_descriptor(*descriptor, offset, "bpcc component bpc"))
        .collect()
}

fn parse_component_descriptor(
    descriptor: u8,
    offset: usize,
    what: &'static str,
) -> Result<J2kComponentInfo, J2kError> {
    let bit_depth = (descriptor & 0x7f) + 1;
    if bit_depth > 38 {
        return Err(J2kError::InvalidBox { offset, what });
    }
    Ok(J2kComponentInfo {
        bit_depth,
        signed: (descriptor & 0x80) != 0,
        x_rsiz: 1,
        y_rsiz: 1,
    })
}

fn parse_pclr(payload: &[u8], offset: usize) -> Result<J2kPaletteMetadata, J2kError> {
    if payload.len() < 3 {
        return Err(J2kError::InvalidBox {
            offset,
            what: "pclr payload shorter than header",
        });
    }
    let entry_count = u16::from_be_bytes([payload[0], payload[1]]) as usize;
    let column_count = usize::from(payload[2]);
    if entry_count == 0 || column_count == 0 {
        return Err(J2kError::InvalidBox {
            offset,
            what: "pclr entry and column counts must be non-zero",
        });
    }
    let descriptors_end = 3usize
        .checked_add(column_count)
        .ok_or(J2kError::InvalidBox {
            offset,
            what: "pclr column descriptor length overflow",
        })?;
    if descriptors_end > payload.len() {
        return Err(J2kError::InvalidBox {
            offset,
            what: "pclr column descriptors are truncated",
        });
    }

    let columns = payload[3..descriptors_end]
        .iter()
        .map(|descriptor| J2kPaletteColumn {
            bit_depth: (descriptor & 0x7f) + 1,
            signed: (descriptor & 0x80) != 0,
        })
        .collect::<Vec<_>>();

    let mut cursor = descriptors_end;
    let mut entries = Vec::with_capacity(entry_count);
    for _ in 0..entry_count {
        let mut row = Vec::with_capacity(column_count);
        for column in &columns {
            let byte_count = usize::from(column.bit_depth).div_ceil(8).max(1);
            let end = cursor.checked_add(byte_count).ok_or(J2kError::InvalidBox {
                offset,
                what: "pclr entry length overflow",
            })?;
            let bytes = payload.get(cursor..end).ok_or(J2kError::InvalidBox {
                offset,
                what: "pclr entries are truncated",
            })?;
            let mut raw = 0u64;
            for &byte in bytes {
                raw = (raw << 8) | u64::from(byte);
            }
            row.push(raw);
            cursor = end;
        }
        entries.push(row);
    }

    Ok(J2kPaletteMetadata { columns, entries })
}

fn parse_cmap(payload: &[u8], offset: usize) -> Result<Vec<J2kComponentMapping>, J2kError> {
    if payload.is_empty() || !payload.len().is_multiple_of(4) {
        return Err(J2kError::InvalidBox {
            offset,
            what: "cmap payload length must be a non-zero multiple of 4",
        });
    }
    Ok(payload
        .chunks_exact(4)
        .map(|entry| {
            let component_index = u16::from_be_bytes([entry[0], entry[1]]);
            let mapping_type = match entry[2] {
                0 => J2kComponentMappingType::Direct,
                1 => J2kComponentMappingType::Palette { column: entry[3] },
                value => J2kComponentMappingType::Unknown {
                    value,
                    column: entry[3],
                },
            };
            J2kComponentMapping {
                component_index,
                mapping_type,
            }
        })
        .collect())
}

fn parse_cdef(payload: &[u8], offset: usize) -> Result<Vec<J2kChannelDefinition>, J2kError> {
    if payload.len() < 2 {
        return Err(J2kError::InvalidBox {
            offset,
            what: "cdef payload shorter than entry count",
        });
    }
    let entry_count = u16::from_be_bytes([payload[0], payload[1]]) as usize;
    if entry_count == 0 {
        return Err(J2kError::InvalidBox {
            offset,
            what: "cdef entry count must be non-zero",
        });
    }
    let required_len = 2usize
        .checked_add(entry_count.checked_mul(6).ok_or(J2kError::InvalidBox {
            offset,
            what: "cdef entry length overflow",
        })?)
        .ok_or(J2kError::InvalidBox {
            offset,
            what: "cdef payload length overflow",
        })?;
    if required_len > payload.len() {
        return Err(J2kError::InvalidBox {
            offset,
            what: "cdef entries are truncated",
        });
    }

    Ok(payload[2..required_len]
        .chunks_exact(6)
        .map(|entry| {
            let channel_index = u16::from_be_bytes([entry[0], entry[1]]);
            let raw_type = u16::from_be_bytes([entry[2], entry[3]]);
            let raw_association = u16::from_be_bytes([entry[4], entry[5]]);
            J2kChannelDefinition {
                channel_index,
                channel_type: match raw_type {
                    0 => J2kChannelType::Color,
                    1 => J2kChannelType::Opacity,
                    2 => J2kChannelType::PremultipliedOpacity,
                    u16::MAX => J2kChannelType::Unspecified,
                    value => J2kChannelType::Unknown { value },
                },
                association: match raw_association {
                    0 => J2kChannelAssociation::WholeImage,
                    u16::MAX => J2kChannelAssociation::Unspecified,
                    index => J2kChannelAssociation::Color { index },
                },
            }
        })
        .collect())
}

#[derive(Debug, Clone, Copy)]
struct BoxHeader {
    box_type: [u8; 4],
    payload_start: usize,
    end: usize,
}

fn read_box_header(input: &[u8], offset: usize) -> Result<BoxHeader, J2kError> {
    if offset + 8 > input.len() {
        return Err(InputError::TruncatedAt {
            offset,
            segment: "box header",
        }
        .into());
    }
    let lbox = u32::from_be_bytes([
        input[offset],
        input[offset + 1],
        input[offset + 2],
        input[offset + 3],
    ]);
    let box_type = [
        input[offset + 4],
        input[offset + 5],
        input[offset + 6],
        input[offset + 7],
    ];

    let (payload_start, end) = match lbox {
        0 => (offset + 8, input.len()),
        1 => {
            if offset + 16 > input.len() {
                return Err(InputError::TruncatedAt {
                    offset,
                    segment: "extended box header",
                }
                .into());
            }
            let xlbox = u64::from_be_bytes([
                input[offset + 8],
                input[offset + 9],
                input[offset + 10],
                input[offset + 11],
                input[offset + 12],
                input[offset + 13],
                input[offset + 14],
                input[offset + 15],
            ]);
            if xlbox < 16 {
                return Err(J2kError::InvalidBox {
                    offset,
                    what: "extended box length smaller than header",
                });
            }
            let end = offset
                .checked_add(xlbox as usize)
                .ok_or(J2kError::InvalidBox {
                    offset,
                    what: "extended box length overflow",
                })?;
            (offset + 16, end)
        }
        length if length < 8 => {
            return Err(J2kError::InvalidBox {
                offset,
                what: "box length smaller than header",
            })
        }
        length => {
            let end = offset
                .checked_add(length as usize)
                .ok_or(J2kError::InvalidBox {
                    offset,
                    what: "box length overflow",
                })?;
            (offset + 8, end)
        }
    };

    Ok(BoxHeader {
        box_type,
        payload_start,
        end,
    })
}