Skip to main content

rust_j2k/codestream/
markers.rs

1//! JPEG 2000 codestream marker codes and the segment structs we parse.
2//!
3//! ISO/IEC 15444-1 Annex A. Only the markers the GRIB2 subset needs are parsed
4//! in full; the rest are recognised so they can be skipped or rejected cleanly.
5
6/// Delimiting and fixed-information markers (ISO Table A-1).
7pub mod marker {
8    pub const SOC: u16 = 0xFF4F; // start of codestream
9    pub const SOT: u16 = 0xFF90; // start of tile-part
10    pub const SOD: u16 = 0xFF93; // start of data
11    pub const EOC: u16 = 0xFFD9; // end of codestream
12    pub const SIZ: u16 = 0xFF51; // image and tile size
13    pub const COD: u16 = 0xFF52; // coding style default
14    pub const COC: u16 = 0xFF53; // coding style component
15    pub const RGN: u16 = 0xFF5E; // region of interest
16    pub const QCD: u16 = 0xFF5C; // quantization default
17    pub const QCC: u16 = 0xFF5D; // quantization component
18    pub const POC: u16 = 0xFF5F; // progression order change
19    pub const TLM: u16 = 0xFF55; // tile-part lengths
20    pub const PLT: u16 = 0xFF58; // packet lengths, tile-part
21    pub const SOP: u16 = 0xFF91; // start of packet
22    pub const EPH: u16 = 0xFF92; // end of packet header
23    pub const COM: u16 = 0xFF64; // comment
24}
25
26/// Wavelet transform (COD/COC, byte "SPcod transformation").
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum Transform {
29    /// 9/7 irreversible — lossy.
30    Irreversible97,
31    /// 5/3 reversible — lossless.
32    Reversible53,
33}
34
35/// Progression order (COD, "SGcod progression order"). ISO Table A-16.
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub enum Progression {
38    Lrcp,
39    Rlcp,
40    Rpcl,
41    Pcrl,
42    Cprl,
43}
44
45/// Quantization style (QCD/QCC, low 5 bits of Sqcd). ISO Table A-28.
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub enum QuantStyle {
48    /// No quantization — reversible path (5/3).
49    None,
50    /// Scalar, single step derived for all subbands.
51    ScalarDerived,
52    /// Scalar, explicit step per subband.
53    ScalarExpounded,
54}
55
56/// One component's geometry from SIZ.
57#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58pub struct SizComponent {
59    pub bit_depth: u8,
60    pub signed: bool,
61    pub x_sampling: u8,
62    pub y_sampling: u8,
63}
64
65/// SIZ — image and tile size (ISO A.5.1). The GRIB2 subset expects exactly one
66/// component.
67#[derive(Debug, Clone, PartialEq, Eq)]
68pub struct Siz {
69    pub x_size: u32,
70    pub y_size: u32,
71    pub x_offset: u32,
72    pub y_offset: u32,
73    pub tile_width: u32,
74    pub tile_height: u32,
75    pub tile_x_offset: u32,
76    pub tile_y_offset: u32,
77    pub components: Vec<SizComponent>,
78}
79
80/// COD — coding style default (ISO A.6.1): the parameters Tier-2 and the DWT
81/// need (decomposition levels, code-block size + style, transform, precincts).
82#[derive(Debug, Clone, PartialEq, Eq)]
83pub struct Cod {
84    pub progression: Progression,
85    pub layers: u16,
86    pub decomposition_levels: u8,
87    pub code_block_width: u8,  // exponent: width  = 2^(value + 2)
88    pub code_block_height: u8, // exponent: height = 2^(value + 2)
89    pub code_block_style: u8,  // bit flags: bypass, reset, restart, vcausal, segsym, …
90    pub transform: Transform,
91    /// Per-level precinct sizes when explicit; empty = maximal (PPx=PPy=15).
92    pub precinct_sizes: Vec<(u8, u8)>,
93}
94
95/// QCD — quantization default (ISO A.6.4): the step sizes / exponents the
96/// dequant stage applies, with the guard-bit count.
97#[derive(Debug, Clone, PartialEq, Eq)]
98pub struct Qcd {
99    pub style: QuantStyle,
100    pub guard_bits: u8,
101    /// (exponent, mantissa) per subband; mantissa is 0 for the reversible style.
102    pub steps: Vec<(u8, u16)>,
103}
104
105impl Qcd {
106    /// The `(exponent, mantissa)` quantization pair for subband index `band`
107    /// (0 = LL, then `HL, LH, HH` per resolution level coarsest-first), or
108    /// `None` if the QCD does not carry it. Reversible (`None` style) and
109    /// expounded QCDs store one pair per subband; the derived style stores only
110    /// subband 0 and drops the exponent by one per resolution level finer
111    /// (E-5: `ε_b = max(ε_0 − ⌊(b−1)/3⌋, 0)`), keeping the single mantissa.
112    ///
113    /// Both the Tier-1 bit-plane count (`Mb`) and the dequant step read this, so
114    /// the per-band mapping lives here once rather than in two places that must
115    /// stay numerically identical.
116    pub fn subband_step(&self, band: usize) -> Option<(u8, u16)> {
117        match self.style {
118            QuantStyle::None | QuantStyle::ScalarExpounded => self.steps.get(band).copied(),
119            QuantStyle::ScalarDerived => {
120                let (exp0, mant0) = *self.steps.first()?;
121                let drop = u8::try_from(band.saturating_sub(1) / 3).unwrap_or(u8::MAX);
122                Some((exp0.saturating_sub(drop), mant0))
123            }
124        }
125    }
126}