Skip to main content

j2k_types/
lib.rs

1//! Shared JPEG 2000 encode-stage contract types for j2k.
2//!
3//! This crate is the neutral public contract between the `j2k`
4//! adapter surface and the `j2k-native` codec engine: job, output,
5//! and report types cross the boundary here so neither crate mirrors the
6//! other's definitions. It intentionally contains plain data types only -
7//! codec behavior stays in `j2k-native` and adapter traits stay in
8//! their owning crates.
9
10#![no_std]
11#![forbid(unsafe_code)]
12#![forbid(missing_docs)]
13
14extern crate alloc;
15
16use alloc::vec::Vec;
17use core::ops::Range;
18
19/// Adapter classic J2K sub-band kind for backend experimentation.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum J2kSubBandType {
22    /// Low-low sub-band.
23    LowLow,
24    /// High-low sub-band.
25    HighLow,
26    /// Low-high sub-band.
27    LowHigh,
28    /// High-high sub-band.
29    HighHigh,
30}
31
32/// Adapter classic J2K code-block style for backend experimentation.
33#[derive(Debug, Clone, Copy)]
34#[allow(clippy::struct_excessive_bools)] // models the five independent COD code-block style flags
35pub struct J2kCodeBlockStyle {
36    /// Selective arithmetic coding bypass was enabled.
37    pub selective_arithmetic_coding_bypass: bool,
38    /// Context probabilities reset after each pass.
39    pub reset_context_probabilities: bool,
40    /// Coding terminated after each pass.
41    pub termination_on_each_pass: bool,
42    /// Vertically causal context was enabled.
43    pub vertically_causal_context: bool,
44    /// Segmentation symbols were enabled.
45    pub segmentation_symbols: bool,
46}
47
48/// Adapter classic J2K coded segment for backend experimentation.
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub struct J2kCodeBlockSegment {
51    /// Byte offset of this segment within the combined payload.
52    pub data_offset: u32,
53    /// Segment payload length in bytes.
54    pub data_length: u32,
55    /// First coding pass covered by this segment.
56    pub start_coding_pass: u8,
57    /// One-past-last coding pass covered by this segment.
58    pub end_coding_pass: u8,
59    /// Whether this segment is decoded through the arithmetic path.
60    pub use_arithmetic: bool,
61}
62
63/// Adapter encoded classic J2K code-block payload for backend experimentation.
64#[derive(Debug, Clone)]
65pub struct EncodedJ2kCodeBlock {
66    /// Combined payload bytes for all coded segments in this code block.
67    pub data: Vec<u8>,
68    /// Coded segments for the code block.
69    pub segments: Vec<J2kCodeBlockSegment>,
70    /// Number of coding passes present for this code block.
71    pub number_of_coding_passes: u8,
72    /// Missing most-significant bit planes for this code block.
73    pub missing_bit_planes: u8,
74}
75
76/// Adapter encoded HTJ2K cleanup code-block payload for backend experimentation.
77#[derive(Debug, Clone)]
78pub struct EncodedHtJ2kCodeBlock {
79    /// Combined cleanup/refinement bytes for this code block.
80    pub data: Vec<u8>,
81    /// Cleanup segment length in bytes.
82    pub cleanup_length: u32,
83    /// Refinement segment length in bytes.
84    pub refinement_length: u32,
85    /// Number of coding passes present for this code block.
86    pub num_coding_passes: u8,
87    /// Number of zero most-significant bitplanes before first inclusion.
88    pub num_zero_bitplanes: u8,
89}
90
91/// Adapter pixel deinterleave/level-shift job for backend experimentation.
92#[derive(Debug, Clone, Copy)]
93pub struct J2kDeinterleaveToF32Job<'a> {
94    /// Interleaved source pixel bytes.
95    pub pixels: &'a [u8],
96    /// Number of pixels to convert.
97    pub num_pixels: usize,
98    /// Number of interleaved components per pixel.
99    pub num_components: u8,
100    /// Source sample bit depth.
101    pub bit_depth: u8,
102    /// Whether source samples are signed.
103    pub signed: bool,
104}
105
106/// Adapter forward RCT job for backend experimentation.
107#[derive(Debug)]
108pub struct J2kForwardRctJob<'a> {
109    /// First component plane, updated in place.
110    pub plane0: &'a mut [f32],
111    /// Second component plane, updated in place.
112    pub plane1: &'a mut [f32],
113    /// Third component plane, updated in place.
114    pub plane2: &'a mut [f32],
115}
116
117/// Adapter forward ICT job for backend experimentation.
118#[derive(Debug)]
119pub struct J2kForwardIctJob<'a> {
120    /// First component plane, updated in place.
121    pub plane0: &'a mut [f32],
122    /// Second component plane, updated in place.
123    pub plane1: &'a mut [f32],
124    /// Third component plane, updated in place.
125    pub plane2: &'a mut [f32],
126}
127
128/// Adapter forward 5/3 DWT job for backend experimentation.
129#[derive(Debug, Clone, Copy)]
130pub struct J2kForwardDwt53Job<'a> {
131    /// Source samples in row-major order.
132    pub samples: &'a [f32],
133    /// Source width in samples.
134    pub width: u32,
135    /// Source height in samples.
136    pub height: u32,
137    /// Number of decomposition levels requested.
138    pub num_levels: u8,
139}
140
141/// Adapter forward 5/3 DWT output for backend experimentation.
142#[derive(Debug, Clone)]
143pub struct J2kForwardDwt53Output {
144    /// LL subband coefficients from the lowest decomposition level.
145    pub ll: Vec<f32>,
146    /// LL subband width.
147    pub ll_width: u32,
148    /// LL subband height.
149    pub ll_height: u32,
150    /// Higher resolution detail levels, ordered from lowest to highest.
151    pub levels: Vec<J2kForwardDwt53Level>,
152}
153
154/// Adapter forward 5/3 DWT detail level for backend experimentation.
155#[derive(Debug, Clone)]
156pub struct J2kForwardDwt53Level {
157    /// HL subband coefficients.
158    pub hl: Vec<f32>,
159    /// LH subband coefficients.
160    pub lh: Vec<f32>,
161    /// HH subband coefficients.
162    pub hh: Vec<f32>,
163    /// Full-resolution width represented by this level.
164    pub width: u32,
165    /// Full-resolution height represented by this level.
166    pub height: u32,
167    /// Low-pass width at this level.
168    pub low_width: u32,
169    /// Low-pass height at this level.
170    pub low_height: u32,
171    /// High-pass width at this level.
172    pub high_width: u32,
173    /// High-pass height at this level.
174    pub high_height: u32,
175}
176
177/// Adapter forward irreversible 9/7 DWT job for backend experimentation.
178#[derive(Debug, Clone, Copy)]
179pub struct J2kForwardDwt97Job<'a> {
180    /// Source samples in row-major order.
181    pub samples: &'a [f32],
182    /// Source width in samples.
183    pub width: u32,
184    /// Source height in samples.
185    pub height: u32,
186    /// Number of decomposition levels requested.
187    pub num_levels: u8,
188}
189
190/// Adapter forward 9/7 DWT output for backend experimentation.
191#[derive(Debug, Clone)]
192pub struct J2kForwardDwt97Output {
193    /// LL subband coefficients from the lowest decomposition level.
194    pub ll: Vec<f32>,
195    /// LL subband width.
196    pub ll_width: u32,
197    /// LL subband height.
198    pub ll_height: u32,
199    /// Higher resolution detail levels, ordered from lowest to highest.
200    pub levels: Vec<J2kForwardDwt97Level>,
201}
202
203/// Adapter forward 9/7 DWT detail level for backend experimentation.
204#[derive(Debug, Clone)]
205pub struct J2kForwardDwt97Level {
206    /// HL subband coefficients.
207    pub hl: Vec<f32>,
208    /// LH subband coefficients.
209    pub lh: Vec<f32>,
210    /// HH subband coefficients.
211    pub hh: Vec<f32>,
212    /// Full-resolution width represented by this level.
213    pub width: u32,
214    /// Full-resolution height represented by this level.
215    pub height: u32,
216    /// Low-pass width at this level.
217    pub low_width: u32,
218    /// Low-pass height at this level.
219    pub low_height: u32,
220    /// High-pass width at this level.
221    pub high_width: u32,
222    /// High-pass height at this level.
223    pub high_height: u32,
224}
225
226/// Adapter sub-band quantization job for backend experimentation.
227#[derive(Debug, Clone, Copy)]
228pub struct J2kQuantizeSubbandJob<'a> {
229    /// Source sub-band coefficients in row-major order.
230    pub coefficients: &'a [f32],
231    /// Quantization step-size exponent.
232    pub step_exponent: u16,
233    /// Quantization step-size mantissa.
234    pub step_mantissa: u16,
235    /// Nominal range bits for this sub-band.
236    pub range_bits: u8,
237    /// Whether to use reversible integer quantization.
238    pub reversible: bool,
239}
240
241/// Adapter Tier-1 classic J2K code-block encode job for backend experimentation.
242#[derive(Debug, Clone, Copy)]
243pub struct J2kTier1CodeBlockEncodeJob<'a> {
244    /// Quantized coefficients in row-major order.
245    pub coefficients: &'a [i32],
246    /// Code-block width in samples.
247    pub width: u32,
248    /// Code-block height in samples.
249    pub height: u32,
250    /// Subband kind containing this code-block.
251    pub sub_band_type: J2kSubBandType,
252    /// Total bitplanes for this subband/code-block.
253    pub total_bitplanes: u8,
254    /// Classic J2K code-block style flags.
255    pub style: J2kCodeBlockStyle,
256}
257
258/// Adapter HTJ2K code-block encode job for backend experimentation.
259#[derive(Debug, Clone, Copy)]
260pub struct J2kHtCodeBlockEncodeJob<'a> {
261    /// Quantized coefficients in row-major order.
262    pub coefficients: &'a [i32],
263    /// Code-block width in samples.
264    pub width: u32,
265    /// Code-block height in samples.
266    pub height: u32,
267    /// Total bitplanes for this subband/code-block.
268    pub total_bitplanes: u8,
269    /// Requested HT coding passes for this contribution.
270    ///
271    /// `1` is cleanup-only. Higher values require an accelerator that can
272    /// encode those passes and must not be silently reduced by CPU fallback.
273    pub target_coding_passes: u8,
274}
275
276/// Adapter HTJ2K cleanup encode job for one unquantized sub-band.
277#[derive(Debug, Clone, Copy)]
278pub struct J2kHtSubbandEncodeJob<'a> {
279    /// Source sub-band coefficients in row-major order.
280    pub coefficients: &'a [f32],
281    /// Sub-band width in samples.
282    pub width: u32,
283    /// Sub-band height in samples.
284    pub height: u32,
285    /// Quantization step-size exponent.
286    pub step_exponent: u16,
287    /// Quantization step-size mantissa.
288    pub step_mantissa: u16,
289    /// Nominal range bits for this sub-band.
290    pub range_bits: u8,
291    /// Whether to use reversible integer quantization.
292    pub reversible: bool,
293    /// Code-block width in samples.
294    pub code_block_width: u32,
295    /// Code-block height in samples.
296    pub code_block_height: u32,
297    /// Total coded bitplanes for this sub-band.
298    pub total_bitplanes: u8,
299}
300
301/// Adapter HTJ2K tile-body encode job for backend-resident full-tile paths.
302#[derive(Debug, Clone, Copy)]
303pub struct J2kHtj2kTileEncodeJob<'a> {
304    /// Interleaved source pixel bytes.
305    pub pixels: &'a [u8],
306    /// Tile/image width in samples.
307    pub width: u32,
308    /// Tile/image height in samples.
309    pub height: u32,
310    /// Number of interleaved image components.
311    pub num_components: u8,
312    /// Source component bit depth.
313    pub bit_depth: u8,
314    /// Whether source samples are signed.
315    pub signed: bool,
316    /// Number of DWT decomposition levels.
317    pub num_decomposition_levels: u8,
318    /// Whether the codestream uses reversible coding.
319    pub reversible: bool,
320    /// Whether a multi-component transform should be applied.
321    pub use_mct: bool,
322    /// JPEG 2000 guard bits used to derive total coded bitplanes.
323    pub guard_bits: u8,
324    /// Code-block width in samples.
325    pub code_block_width: u32,
326    /// Code-block height in samples.
327    pub code_block_height: u32,
328    /// Packet progression order to emit.
329    pub progression_order: J2kPacketizationProgressionOrder,
330    /// Per-component sampling factors, as `(x_rsiz, y_rsiz)`.
331    pub component_sampling: &'a [(u8, u8)],
332    /// Quantization step sizes, as `(exponent, mantissa)`, in codestream order.
333    pub quantization_steps: &'a [(u16, u16)],
334}
335
336/// Adapter LRCP packetization code-block contribution for backend experimentation.
337#[derive(Debug, Clone, Copy, PartialEq, Eq)]
338pub struct J2kPacketizationCodeBlock<'a> {
339    /// Encoded Tier-1 bitstream bytes for this packet contribution.
340    pub data: &'a [u8],
341    /// HTJ2K cleanup segment length in bytes when using high-throughput coding.
342    pub ht_cleanup_length: u32,
343    /// HTJ2K refinement segment length in bytes when using high-throughput coding.
344    pub ht_refinement_length: u32,
345    /// Number of coding passes in this contribution.
346    pub num_coding_passes: u8,
347    /// Number of zero most-significant bitplanes before first inclusion.
348    pub num_zero_bitplanes: u8,
349    /// Whether this code-block was included in a previous packet.
350    pub previously_included: bool,
351    /// L-block value used for segment length coding.
352    pub l_block: u32,
353    /// Block coder used for this contribution.
354    pub block_coding_mode: J2kPacketizationBlockCodingMode,
355}
356
357/// Adapter packetization block coding mode for backend experimentation.
358#[derive(Debug, Clone, Copy, PartialEq, Eq)]
359pub enum J2kPacketizationBlockCodingMode {
360    /// Classic JPEG 2000 Part 1 EBCOT block coding.
361    Classic,
362    /// High-throughput JPEG 2000 Part 15 block coding.
363    HighThroughput,
364}
365
366/// Adapter packet progression order for backend packetization experimentation.
367#[derive(Debug, Clone, Copy, PartialEq, Eq)]
368pub enum J2kPacketizationProgressionOrder {
369    /// Layer-resolution-component-position progression.
370    Lrcp,
371    /// Resolution-layer-component-position progression.
372    Rlcp,
373    /// Resolution-position-component-layer progression.
374    Rpcl,
375    /// Position-component-resolution-layer progression.
376    Pcrl,
377    /// Component-position-resolution-layer progression.
378    Cprl,
379}
380
381/// Adapter LRCP packetization subband precinct for backend experimentation.
382#[derive(Debug, Clone, PartialEq, Eq)]
383pub struct J2kPacketizationSubband<'a> {
384    /// Code-block contributions in row-major order.
385    pub code_blocks: Vec<J2kPacketizationCodeBlock<'a>>,
386    /// Number of code-blocks in the x direction.
387    pub num_cbs_x: u32,
388    /// Number of code-blocks in the y direction.
389    pub num_cbs_y: u32,
390}
391
392/// Adapter LRCP packetization resolution packet for backend experimentation.
393#[derive(Debug, Clone, PartialEq, Eq)]
394pub struct J2kPacketizationResolution<'a> {
395    /// Subbands in packet order: LL for resolution 0, then HL/LH/HH.
396    pub subbands: Vec<J2kPacketizationSubband<'a>>,
397}
398
399/// Adapter explicit packet descriptor for backend packetization experimentation.
400#[derive(Debug, Clone, Copy, PartialEq, Eq)]
401pub struct J2kPacketizationPacketDescriptor {
402    /// Index into the packet contribution array.
403    pub packet_index: u32,
404    /// Persistent packet-state index for repeated layer/precinct packets.
405    pub state_index: u32,
406    /// Quality layer for inclusion tag-tree thresholds.
407    pub layer: u8,
408    /// Resolution index in the output progression.
409    pub resolution: u32,
410    /// Component index in the output progression.
411    pub component: u8,
412    /// Precinct index in the output progression.
413    pub precinct: u64,
414}
415
416/// Adapter LRCP packetization job for backend experimentation.
417#[derive(Debug, Clone, Copy, PartialEq, Eq)]
418pub struct J2kPacketizationEncodeJob<'a> {
419    /// Number of resolution packets prepared for packetization.
420    pub resolution_count: u32,
421    /// Number of layers to write.
422    pub num_layers: u8,
423    /// Number of image components.
424    pub num_components: u8,
425    /// Total number of code-block contributions.
426    pub code_block_count: u32,
427    /// Packet progression order to emit.
428    pub progression_order: J2kPacketizationProgressionOrder,
429    /// Explicit packet descriptors in output progression order.
430    pub packet_descriptors: &'a [J2kPacketizationPacketDescriptor],
431    /// Packet payload prepared by Tier-1, in LRCP packet order.
432    pub resolutions: &'a [J2kPacketizationResolution<'a>],
433}
434
435/// Adapter encode-stage dispatch counters for backend experimentation.
436#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
437pub struct J2kEncodeDispatchReport {
438    /// Pixel deinterleave/level-shift dispatch count.
439    pub deinterleave: usize,
440    /// Forward RCT kernel dispatch count.
441    pub forward_rct: usize,
442    /// Forward ICT kernel dispatch count.
443    pub forward_ict: usize,
444    /// Forward reversible 5/3 DWT kernel dispatch count.
445    pub forward_dwt53: usize,
446    /// Forward irreversible 9/7 DWT kernel dispatch count.
447    pub forward_dwt97: usize,
448    /// Sub-band quantization dispatch count.
449    pub quantize_subband: usize,
450    /// Tier-1 code-block encode dispatch count.
451    pub tier1_code_block: usize,
452    /// HTJ2K code-block encode dispatch count.
453    pub ht_code_block: usize,
454    /// Packetization dispatch count.
455    pub packetization: usize,
456}
457
458impl J2kEncodeDispatchReport {
459    /// Return the saturating per-stage delta from `before` to `self`.
460    #[must_use]
461    pub fn saturating_delta(self, before: Self) -> Self {
462        Self {
463            deinterleave: self.deinterleave.saturating_sub(before.deinterleave),
464            forward_rct: self.forward_rct.saturating_sub(before.forward_rct),
465            forward_ict: self.forward_ict.saturating_sub(before.forward_ict),
466            forward_dwt53: self.forward_dwt53.saturating_sub(before.forward_dwt53),
467            forward_dwt97: self.forward_dwt97.saturating_sub(before.forward_dwt97),
468            quantize_subband: self
469                .quantize_subband
470                .saturating_sub(before.quantize_subband),
471            tier1_code_block: self
472                .tier1_code_block
473                .saturating_sub(before.tier1_code_block),
474            ht_code_block: self.ht_code_block.saturating_sub(before.ht_code_block),
475            packetization: self.packetization.saturating_sub(before.packetization),
476        }
477    }
478
479    /// Return total dispatches across all encode stages.
480    #[must_use]
481    pub fn total(self) -> usize {
482        self.forward_rct
483            .saturating_add(self.deinterleave)
484            .saturating_add(self.forward_ict)
485            .saturating_add(self.forward_dwt53)
486            .saturating_add(self.forward_dwt97)
487            .saturating_add(self.quantize_subband)
488            .saturating_add(self.tier1_code_block)
489            .saturating_add(self.ht_code_block)
490            .saturating_add(self.packetization)
491    }
492
493    /// Return whether at least one encode stage dispatched.
494    #[must_use]
495    pub fn any(self) -> bool {
496        self.total() > 0
497    }
498}
499
500/// Adapter CPU-only encode accelerator that always falls back to native stages.
501#[derive(Debug, Default, Clone, Copy)]
502pub struct CpuOnlyJ2kEncodeStageAccelerator;
503
504/// Multipliers applied to irreversible 9/7 quantization step sizes by subband.
505#[derive(Debug, Clone, Copy, PartialEq)]
506pub struct IrreversibleQuantizationSubbandScales {
507    /// Multiplier for the LL subband.
508    pub low_low: f32,
509    /// Multiplier for HL subbands.
510    pub high_low: f32,
511    /// Multiplier for LH subbands.
512    pub low_high: f32,
513    /// Multiplier for HH subbands.
514    pub high_high: f32,
515}
516
517/// Public JPEG 2000 irreversible quantization step-size tuple.
518#[derive(Debug, Clone, Copy, PartialEq, Eq)]
519pub struct IrreversibleQuantizationStep {
520    /// Quantization step-size exponent.
521    pub exponent: u8,
522    /// Quantization step-size mantissa.
523    pub mantissa: u16,
524}
525
526impl Default for IrreversibleQuantizationSubbandScales {
527    fn default() -> Self {
528        Self {
529            low_low: 1.0,
530            high_low: 1.0,
531            low_high: 1.0,
532            high_high: 1.0,
533        }
534    }
535}
536
537/// Precomputed reversible 5/3 wavelet coefficients for one component.
538#[derive(Debug, Clone)]
539pub struct PrecomputedHtj2k53Component {
540    /// Horizontal SIZ sampling factor (`XRsiz`).
541    pub x_rsiz: u8,
542    /// Vertical SIZ sampling factor (`YRsiz`).
543    pub y_rsiz: u8,
544    /// Forward 5/3 DWT output, ordered as the encoder expects.
545    pub dwt: J2kForwardDwt53Output,
546}
547
548/// Precomputed reversible 5/3 wavelet image.
549#[derive(Debug, Clone)]
550pub struct PrecomputedHtj2k53Image {
551    /// Reference-grid image width.
552    pub width: u32,
553    /// Reference-grid image height.
554    pub height: u32,
555    /// Component precision in bits.
556    pub bit_depth: u8,
557    /// Whether component samples are signed.
558    pub signed: bool,
559    /// Components at their native resolution.
560    pub components: Vec<PrecomputedHtj2k53Component>,
561}
562
563/// Precomputed irreversible 9/7 wavelet coefficients for one component.
564#[derive(Debug, Clone)]
565pub struct PrecomputedHtj2k97Component {
566    /// Horizontal SIZ sampling factor (`XRsiz`).
567    pub x_rsiz: u8,
568    /// Vertical SIZ sampling factor (`YRsiz`).
569    pub y_rsiz: u8,
570    /// Forward 9/7 DWT output, ordered as the encoder expects.
571    pub dwt: J2kForwardDwt97Output,
572}
573
574/// Precomputed irreversible 9/7 wavelet image.
575#[derive(Debug, Clone)]
576pub struct PrecomputedHtj2k97Image {
577    /// Reference-grid image width.
578    pub width: u32,
579    /// Reference-grid image height.
580    pub height: u32,
581    /// Component precision in bits.
582    pub bit_depth: u8,
583    /// Whether component samples are signed.
584    pub signed: bool,
585    /// Components at their native resolution.
586    pub components: Vec<PrecomputedHtj2k97Component>,
587}
588
589/// Prequantized irreversible 9/7 HTJ2K code-block image.
590#[derive(Debug, Clone)]
591pub struct PrequantizedHtj2k97Image {
592    /// Reference-grid image width.
593    pub width: u32,
594    /// Reference-grid image height.
595    pub height: u32,
596    /// Component precision in bits.
597    pub bit_depth: u8,
598    /// Whether component samples are signed.
599    pub signed: bool,
600    /// Components at their native resolution.
601    pub components: Vec<PrequantizedHtj2k97Component>,
602}
603
604/// Prequantized irreversible 9/7 HTJ2K component.
605#[derive(Debug, Clone)]
606pub struct PrequantizedHtj2k97Component {
607    /// Horizontal SIZ sampling factor (`XRsiz`).
608    pub x_rsiz: u8,
609    /// Vertical SIZ sampling factor (`YRsiz`).
610    pub y_rsiz: u8,
611    /// Resolution packets for this component, ordered from lowest to highest.
612    pub resolutions: Vec<PrequantizedHtj2k97Resolution>,
613}
614
615/// One component resolution's prequantized HTJ2K subbands.
616#[derive(Debug, Clone)]
617pub struct PrequantizedHtj2k97Resolution {
618    /// Subbands in packet order: LL for resolution 0, then HL/LH/HH.
619    pub subbands: Vec<PrequantizedHtj2k97Subband>,
620}
621
622/// One prequantized HTJ2K subband split into code-blocks.
623#[derive(Debug, Clone)]
624pub struct PrequantizedHtj2k97Subband {
625    /// Subband kind.
626    pub sub_band_type: J2kSubBandType,
627    /// Number of code-blocks in the x direction.
628    pub num_cbs_x: u32,
629    /// Number of code-blocks in the y direction.
630    pub num_cbs_y: u32,
631    /// Total bitplanes declared for every code-block in this subband.
632    pub total_bitplanes: u8,
633    /// Code-block coefficients in row-major code-block order.
634    pub code_blocks: Vec<PrequantizedHtj2k97CodeBlock>,
635}
636
637/// One prequantized HTJ2K code-block.
638#[derive(Debug, Clone)]
639pub struct PrequantizedHtj2k97CodeBlock {
640    /// Quantized coefficients in row-major order.
641    pub coefficients: Vec<i32>,
642    /// Code-block width in coefficients.
643    pub width: u32,
644    /// Code-block height in coefficients.
645    pub height: u32,
646}
647
648/// Preencoded irreversible 9/7 HTJ2K code-block image.
649#[derive(Debug, Clone)]
650pub struct PreencodedHtj2k97Image {
651    /// Reference-grid image width.
652    pub width: u32,
653    /// Reference-grid image height.
654    pub height: u32,
655    /// Component precision in bits.
656    pub bit_depth: u8,
657    /// Whether component samples are signed.
658    pub signed: bool,
659    /// Components at their native resolution.
660    pub components: Vec<PreencodedHtj2k97Component>,
661}
662
663/// Preencoded irreversible 9/7 HTJ2K component.
664#[derive(Debug, Clone)]
665pub struct PreencodedHtj2k97Component {
666    /// Horizontal SIZ sampling factor (`XRsiz`).
667    pub x_rsiz: u8,
668    /// Vertical SIZ sampling factor (`YRsiz`).
669    pub y_rsiz: u8,
670    /// Resolution packets for this component, ordered from lowest to highest.
671    pub resolutions: Vec<PreencodedHtj2k97Resolution>,
672}
673
674/// One component resolution's preencoded HTJ2K subbands.
675#[derive(Debug, Clone)]
676pub struct PreencodedHtj2k97Resolution {
677    /// Subbands in packet order: LL for resolution 0, then HL/LH/HH.
678    pub subbands: Vec<PreencodedHtj2k97Subband>,
679}
680
681/// One preencoded HTJ2K subband split into code-blocks.
682#[derive(Debug, Clone)]
683pub struct PreencodedHtj2k97Subband {
684    /// Subband kind.
685    pub sub_band_type: J2kSubBandType,
686    /// Number of code-blocks in the x direction.
687    pub num_cbs_x: u32,
688    /// Number of code-blocks in the y direction.
689    pub num_cbs_y: u32,
690    /// Total bitplanes declared for every code-block in this subband.
691    pub total_bitplanes: u8,
692    /// Encoded code-block payloads in row-major code-block order.
693    pub code_blocks: Vec<PreencodedHtj2k97CodeBlock>,
694}
695
696/// One preencoded HTJ2K code-block.
697#[derive(Debug, Clone)]
698pub struct PreencodedHtj2k97CodeBlock {
699    /// Code-block width in coefficients.
700    pub width: u32,
701    /// Code-block height in coefficients.
702    pub height: u32,
703    /// Encoded cleanup/refinement payload and packet metadata.
704    pub encoded: EncodedHtJ2kCodeBlock,
705}
706
707/// Preencoded irreversible 9/7 HTJ2K code-block image backed by one compact
708/// payload buffer.
709#[derive(Debug, Clone)]
710pub struct PreencodedHtj2k97CompactImage {
711    /// Reference-grid image width.
712    pub width: u32,
713    /// Reference-grid image height.
714    pub height: u32,
715    /// Component precision in bits.
716    pub bit_depth: u8,
717    /// Whether component samples are signed.
718    pub signed: bool,
719    /// Contiguous encoded code-block payload bytes.
720    pub payload: Vec<u8>,
721    /// Components at their native resolution.
722    pub components: Vec<PreencodedHtj2k97CompactComponent>,
723}
724
725/// Preencoded compact irreversible 9/7 HTJ2K component.
726#[derive(Debug, Clone)]
727pub struct PreencodedHtj2k97CompactComponent {
728    /// Horizontal SIZ sampling factor (`XRsiz`).
729    pub x_rsiz: u8,
730    /// Vertical SIZ sampling factor (`YRsiz`).
731    pub y_rsiz: u8,
732    /// Resolution packets for this component, ordered from lowest to highest.
733    pub resolutions: Vec<PreencodedHtj2k97CompactResolution>,
734}
735
736/// One component resolution's compact preencoded HTJ2K subbands.
737#[derive(Debug, Clone)]
738pub struct PreencodedHtj2k97CompactResolution {
739    /// Subbands in packet order: LL for resolution 0, then HL/LH/HH.
740    pub subbands: Vec<PreencodedHtj2k97CompactSubband>,
741}
742
743/// One compact preencoded HTJ2K subband split into code-blocks.
744#[derive(Debug, Clone)]
745pub struct PreencodedHtj2k97CompactSubband {
746    /// Subband kind.
747    pub sub_band_type: J2kSubBandType,
748    /// Number of code-blocks in the x direction.
749    pub num_cbs_x: u32,
750    /// Number of code-blocks in the y direction.
751    pub num_cbs_y: u32,
752    /// Total bitplanes declared for every code-block in this subband.
753    pub total_bitplanes: u8,
754    /// Code-block metadata in row-major code-block order.
755    pub code_blocks: Vec<PreencodedHtj2k97CompactCodeBlock>,
756}
757
758/// One compact preencoded HTJ2K code-block.
759#[derive(Debug, Clone)]
760pub struct PreencodedHtj2k97CompactCodeBlock {
761    /// Code-block width in coefficients.
762    pub width: u32,
763    /// Code-block height in coefficients.
764    pub height: u32,
765    /// Byte range into the image-level compact payload.
766    pub payload_range: Range<usize>,
767    /// HTJ2K cleanup segment length in bytes.
768    pub cleanup_length: u32,
769    /// HTJ2K refinement segment length in bytes.
770    pub refinement_length: u32,
771    /// Number of coding passes in the encoded payload.
772    pub num_coding_passes: u8,
773    /// Number of missing most-significant bitplanes.
774    pub num_zero_bitplanes: u8,
775}