Skip to main content

j2k_types/decode_plan/
mod.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Backend-neutral retained JPEG 2000 decode-plan contracts.
4
5mod allocation;
6mod referenced;
7
8use alloc::vec::Vec;
9
10use crate::{J2kCodeBlockSegment, J2kCodeBlockStyle, J2kSubBandType};
11
12pub use allocation::DecodePlanAllocationError;
13pub use referenced::{
14    J2kReferencedClassicPlan, J2kReferencedHtj2kPlan, J2kReferencedImageGeometry,
15    J2kReferencedPayloadRecordSpan, J2kReferencedTileGeometry, J2kReferencedTilePlan,
16};
17
18/// Default maximum simultaneously retained codec allocation in bytes.
19pub const DEFAULT_MAX_CODEC_BYTES: usize = 512 * 1024 * 1024;
20
21/// Default maximum retained decode allocation in bytes.
22pub const DEFAULT_MAX_DECODE_BYTES: usize = DEFAULT_MAX_CODEC_BYTES;
23
24/// Stable identifier for one device-owned grayscale coefficient band.
25pub type J2kDirectBandId = u32;
26
27/// Integer rectangle in component coordinates.
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub struct J2kRect {
30    /// Inclusive minimum x coordinate.
31    pub x0: u32,
32    /// Inclusive minimum y coordinate.
33    pub y0: u32,
34    /// Exclusive maximum x coordinate.
35    pub x1: u32,
36    /// Exclusive maximum y coordinate.
37    pub y1: u32,
38}
39
40impl J2kRect {
41    /// Rectangle width in samples.
42    #[must_use]
43    pub const fn width(self) -> u32 {
44        self.x1.saturating_sub(self.x0)
45    }
46
47    /// Rectangle height in samples.
48    #[must_use]
49    pub const fn height(self) -> u32 {
50        self.y1.saturating_sub(self.y0)
51    }
52}
53
54/// Wavelet transform used by retained decode geometry.
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub enum J2kWaveletTransform {
57    /// Reversible 5/3 transform.
58    Reversible53,
59    /// Irreversible 9/7 transform.
60    Irreversible97,
61}
62
63/// Ordered direct-device decode step for one component.
64#[derive(Debug)]
65pub enum J2kDirectGrayscaleStep {
66    /// Decode one classic JPEG 2000 subband.
67    ClassicSubBand(J2kOwnedSubBandPlan),
68    /// Decode one HTJ2K subband.
69    HtSubBand(HtOwnedSubBandPlan),
70    /// Apply one single-decomposition inverse transform.
71    Idwt(J2kDirectIdwtStep),
72    /// Store the final component plane.
73    Store(J2kDirectStoreStep),
74}
75
76/// Direct-device decode plan for one component.
77#[derive(Debug)]
78pub struct J2kDirectGrayscalePlan {
79    /// Final output dimensions.
80    pub dimensions: (u32, u32),
81    /// Final output bit depth.
82    pub bit_depth: u8,
83    /// Ordered execution steps.
84    pub steps: Vec<J2kDirectGrayscaleStep>,
85}
86
87/// Direct-device RGB decode plan.
88#[derive(Debug)]
89pub struct J2kDirectColorPlan {
90    /// Final output dimensions.
91    pub dimensions: (u32, u32),
92    /// Final output bit depths for the first three components.
93    pub bit_depths: [u8; 3],
94    /// Whether inverse MCT is required.
95    pub mct: bool,
96    /// Wavelet transform used by the color transform.
97    pub transform: J2kWaveletTransform,
98    /// Per-component plans in R, G, B order.
99    pub component_plans: Vec<J2kDirectGrayscalePlan>,
100}
101
102/// Direct-device RGBA decode plan with RGB-only inverse MCT semantics.
103#[derive(Debug)]
104pub struct J2kDirectRgbaPlan {
105    /// Final output dimensions.
106    pub dimensions: (u32, u32),
107    /// Final output bit depths in R, G, B, A order.
108    pub bit_depths: [u8; 4],
109    /// Whether inverse MCT is required for the first three components.
110    pub mct: bool,
111    /// Wavelet transform used by the RGB color transform.
112    pub transform: J2kWaveletTransform,
113    /// Per-component plans in R, G, B, A order.
114    pub component_plans: Vec<J2kDirectGrayscalePlan>,
115}
116
117/// Owned classic JPEG 2000 subband decode plan.
118#[derive(Debug)]
119pub struct J2kOwnedSubBandPlan {
120    /// Stable output-band identifier.
121    pub band_id: J2kDirectBandId,
122    /// Absolute subband rectangle.
123    pub rect: J2kRect,
124    /// Subband width in samples.
125    pub width: u32,
126    /// Subband height in samples.
127    pub height: u32,
128    /// Whether irreversible midpoint reconstruction is required.
129    pub irreversible_midpoint: bool,
130    /// Owned code-block jobs.
131    pub jobs: Vec<J2kOwnedCodeBlockBatchJob>,
132}
133
134/// Owned HTJ2K subband decode plan.
135#[derive(Debug)]
136pub struct HtOwnedSubBandPlan {
137    /// Stable output-band identifier.
138    pub band_id: J2kDirectBandId,
139    /// Absolute subband rectangle.
140    pub rect: J2kRect,
141    /// Subband width in samples.
142    pub width: u32,
143    /// Subband height in samples.
144    pub height: u32,
145    /// Whether irreversible midpoint reconstruction is required.
146    pub irreversible_midpoint: bool,
147    /// Owned code-block jobs.
148    pub jobs: Vec<HtOwnedCodeBlockBatchJob>,
149}
150
151/// Owned classic JPEG 2000 code-block decode job.
152#[derive(Debug)]
153pub struct J2kOwnedCodeBlockBatchJob {
154    /// X offset in the target subband.
155    pub output_x: u32,
156    /// Y offset in the target subband.
157    pub output_y: u32,
158    /// Combined bytes for every coded segment.
159    pub data: Vec<u8>,
160    /// Coded segments.
161    pub segments: Vec<J2kCodeBlockSegment>,
162    /// Code-block width.
163    pub width: u32,
164    /// Code-block height.
165    pub height: u32,
166    /// Output row stride in samples.
167    pub output_stride: usize,
168    /// Missing most-significant bit planes.
169    pub missing_bit_planes: u8,
170    /// Number of coding passes.
171    pub number_of_coding_passes: u8,
172    /// Total coded bitplanes for the parent subband.
173    pub total_bitplanes: u8,
174    /// ROI maxshift value.
175    pub roi_shift: u8,
176    /// Parent subband type.
177    pub sub_band_type: J2kSubBandType,
178    /// Code-block style flags.
179    pub style: J2kCodeBlockStyle,
180    /// Whether strict validation is enabled.
181    pub strict: bool,
182    /// Dequantization step.
183    pub dequantization_step: f32,
184}
185
186/// Owned HTJ2K code-block decode job.
187#[derive(Debug)]
188pub struct HtOwnedCodeBlockBatchJob {
189    /// X offset in the target subband.
190    pub output_x: u32,
191    /// Y offset in the target subband.
192    pub output_y: u32,
193    /// Combined cleanup and refinement bytes.
194    pub data: Vec<u8>,
195    /// Cleanup segment length in bytes.
196    pub cleanup_length: u32,
197    /// Refinement segment length in bytes.
198    pub refinement_length: u32,
199    /// Code-block width.
200    pub width: u32,
201    /// Code-block height.
202    pub height: u32,
203    /// Output row stride in samples.
204    pub output_stride: usize,
205    /// Missing most-significant bit planes.
206    pub missing_bit_planes: u8,
207    /// Number of coding passes.
208    pub number_of_coding_passes: u8,
209    /// Total coded bitplanes for the parent subband.
210    pub num_bitplanes: u8,
211    /// ROI maxshift value.
212    pub roi_shift: u8,
213    /// Whether vertically causal contexts are enabled.
214    pub stripe_causal: bool,
215    /// Whether strict validation is enabled.
216    pub strict: bool,
217    /// Dequantization step.
218    pub dequantization_step: f32,
219}
220
221/// One inverse-transform step in a direct-device plan.
222#[derive(Debug, Clone, Copy)]
223pub struct J2kDirectIdwtStep {
224    /// Output coefficient-band identifier.
225    pub output_band_id: J2kDirectBandId,
226    /// Output rectangle.
227    pub rect: J2kRect,
228    /// Transform to apply.
229    pub transform: J2kWaveletTransform,
230    /// LL input-band identifier.
231    pub ll_band_id: J2kDirectBandId,
232    /// LL input rectangle.
233    pub ll: J2kRect,
234    /// HL input-band identifier.
235    pub hl_band_id: J2kDirectBandId,
236    /// HL input rectangle.
237    pub hl: J2kRect,
238    /// LH input-band identifier.
239    pub lh_band_id: J2kDirectBandId,
240    /// LH input rectangle.
241    pub lh: J2kRect,
242    /// HH input-band identifier.
243    pub hh_band_id: J2kDirectBandId,
244    /// HH input rectangle.
245    pub hh: J2kRect,
246}
247
248/// One final component-store step in a direct-device plan.
249#[derive(Debug, Clone, Copy)]
250pub struct J2kDirectStoreStep {
251    /// Input coefficient-band identifier.
252    pub input_band_id: J2kDirectBandId,
253    /// Input plane rectangle.
254    pub input_rect: J2kRect,
255    /// Source x offset.
256    pub source_x: u32,
257    /// Source y offset.
258    pub source_y: u32,
259    /// Samples copied per row.
260    pub copy_width: u32,
261    /// Rows copied.
262    pub copy_height: u32,
263    /// Destination row width.
264    pub output_width: u32,
265    /// Destination height.
266    pub output_height: u32,
267    /// Destination x offset.
268    pub output_x: u32,
269    /// Destination y offset.
270    pub output_y: u32,
271    /// Constant added to every copied sample.
272    pub addend: f32,
273}