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    /// Whether this component stores signed samples.
84    pub signed: bool,
85    /// Ordered execution steps.
86    pub steps: Vec<J2kDirectGrayscaleStep>,
87}
88
89/// Direct-device RGB decode plan.
90#[derive(Debug)]
91pub struct J2kDirectColorPlan {
92    /// Final output dimensions.
93    pub dimensions: (u32, u32),
94    /// Final output bit depths for the first three components.
95    pub bit_depths: [u8; 3],
96    /// Whether inverse MCT is required.
97    pub mct: bool,
98    /// Wavelet transform used by the color transform.
99    pub transform: J2kWaveletTransform,
100    /// Per-component plans in R, G, B order.
101    pub component_plans: Vec<J2kDirectGrayscalePlan>,
102}
103
104/// Direct-device RGBA decode plan with RGB-only inverse MCT semantics.
105#[derive(Debug)]
106pub struct J2kDirectRgbaPlan {
107    /// Final output dimensions.
108    pub dimensions: (u32, u32),
109    /// Final output bit depths in R, G, B, A order.
110    pub bit_depths: [u8; 4],
111    /// Whether inverse MCT is required for the first three components.
112    pub mct: bool,
113    /// Wavelet transform used by the RGB color transform.
114    pub transform: J2kWaveletTransform,
115    /// Per-component plans in R, G, B, A order.
116    pub component_plans: Vec<J2kDirectGrayscalePlan>,
117}
118
119/// Owned classic JPEG 2000 subband decode plan.
120#[derive(Debug)]
121pub struct J2kOwnedSubBandPlan {
122    /// Stable output-band identifier.
123    pub band_id: J2kDirectBandId,
124    /// Absolute subband rectangle.
125    pub rect: J2kRect,
126    /// Subband width in samples.
127    pub width: u32,
128    /// Subband height in samples.
129    pub height: u32,
130    /// Whether irreversible midpoint reconstruction is required.
131    pub irreversible_midpoint: bool,
132    /// Owned code-block jobs.
133    pub jobs: Vec<J2kOwnedCodeBlockBatchJob>,
134}
135
136/// Owned HTJ2K subband decode plan.
137#[derive(Debug)]
138pub struct HtOwnedSubBandPlan {
139    /// Stable output-band identifier.
140    pub band_id: J2kDirectBandId,
141    /// Absolute subband rectangle.
142    pub rect: J2kRect,
143    /// Subband width in samples.
144    pub width: u32,
145    /// Subband height in samples.
146    pub height: u32,
147    /// Whether irreversible midpoint reconstruction is required.
148    pub irreversible_midpoint: bool,
149    /// Owned code-block jobs.
150    pub jobs: Vec<HtOwnedCodeBlockBatchJob>,
151}
152
153/// Owned classic JPEG 2000 code-block decode job.
154#[derive(Debug)]
155pub struct J2kOwnedCodeBlockBatchJob {
156    /// X offset in the target subband.
157    pub output_x: u32,
158    /// Y offset in the target subband.
159    pub output_y: u32,
160    /// Combined bytes for every coded segment.
161    pub data: Vec<u8>,
162    /// Coded segments.
163    pub segments: Vec<J2kCodeBlockSegment>,
164    /// Code-block width.
165    pub width: u32,
166    /// Code-block height.
167    pub height: u32,
168    /// Output row stride in samples.
169    pub output_stride: usize,
170    /// Missing most-significant bit planes.
171    pub missing_bit_planes: u8,
172    /// Number of coding passes.
173    pub number_of_coding_passes: u8,
174    /// Total coded bitplanes for the parent subband.
175    pub total_bitplanes: u8,
176    /// ROI maxshift value.
177    pub roi_shift: u8,
178    /// Parent subband type.
179    pub sub_band_type: J2kSubBandType,
180    /// Code-block style flags.
181    pub style: J2kCodeBlockStyle,
182    /// Whether strict validation is enabled.
183    pub strict: bool,
184    /// Dequantization step.
185    pub dequantization_step: f32,
186}
187
188/// Owned HTJ2K code-block decode job.
189#[derive(Debug)]
190pub struct HtOwnedCodeBlockBatchJob {
191    /// X offset in the target subband.
192    pub output_x: u32,
193    /// Y offset in the target subband.
194    pub output_y: u32,
195    /// Combined cleanup and refinement bytes.
196    pub data: Vec<u8>,
197    /// Cleanup segment length in bytes.
198    pub cleanup_length: u32,
199    /// Refinement segment length in bytes.
200    pub refinement_length: u32,
201    /// Code-block width.
202    pub width: u32,
203    /// Code-block height.
204    pub height: u32,
205    /// Output row stride in samples.
206    pub output_stride: usize,
207    /// Missing most-significant bit planes.
208    pub missing_bit_planes: u8,
209    /// Number of coding passes.
210    pub number_of_coding_passes: u8,
211    /// Total coded bitplanes for the parent subband.
212    pub num_bitplanes: u8,
213    /// ROI maxshift value.
214    pub roi_shift: u8,
215    /// Whether vertically causal contexts are enabled.
216    pub stripe_causal: bool,
217    /// Whether strict validation is enabled.
218    pub strict: bool,
219    /// Dequantization step.
220    pub dequantization_step: f32,
221}
222
223/// One inverse-transform step in a direct-device plan.
224#[derive(Debug, Clone, Copy)]
225pub struct J2kDirectIdwtStep {
226    /// Output coefficient-band identifier.
227    pub output_band_id: J2kDirectBandId,
228    /// Output rectangle.
229    pub rect: J2kRect,
230    /// Transform to apply.
231    pub transform: J2kWaveletTransform,
232    /// LL input-band identifier.
233    pub ll_band_id: J2kDirectBandId,
234    /// LL input rectangle.
235    pub ll: J2kRect,
236    /// HL input-band identifier.
237    pub hl_band_id: J2kDirectBandId,
238    /// HL input rectangle.
239    pub hl: J2kRect,
240    /// LH input-band identifier.
241    pub lh_band_id: J2kDirectBandId,
242    /// LH input rectangle.
243    pub lh: J2kRect,
244    /// HH input-band identifier.
245    pub hh_band_id: J2kDirectBandId,
246    /// HH input rectangle.
247    pub hh: J2kRect,
248}
249
250/// One final component-store step in a direct-device plan.
251#[derive(Debug, Clone, Copy)]
252pub struct J2kDirectStoreStep {
253    /// Input coefficient-band identifier.
254    pub input_band_id: J2kDirectBandId,
255    /// Input plane rectangle.
256    pub input_rect: J2kRect,
257    /// Source x offset.
258    pub source_x: u32,
259    /// Source y offset.
260    pub source_y: u32,
261    /// Samples copied per row.
262    pub copy_width: u32,
263    /// Rows copied.
264    pub copy_height: u32,
265    /// Destination row width.
266    pub output_width: u32,
267    /// Destination height.
268    pub output_height: u32,
269    /// Destination x offset.
270    pub output_x: u32,
271    /// Destination y offset.
272    pub output_y: u32,
273    /// Constant added to every copied sample.
274    pub addend: f32,
275}