j2k-native 0.8.0

Pure-Rust JPEG 2000 and HTJ2K codec engine for j2k
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
use alloc::vec::Vec;
use core::ops::Range;

use crate::error::{bail, DecodingError, Result};
use crate::j2c::idwt;
use crate::math::{floor_f32, round_f32};
use crate::{
    decode_ht_code_block_scalar_with_workspace, decode_j2k_code_block_scalar_with_workspace,
    try_resize_decode_elements, HtCodeBlockDecodeJob, HtCodeBlockDecodeWorkspace,
    HtOwnedSubBandPlan, J2kCodeBlockDecodeJob, J2kCodeBlockDecodeWorkspace, J2kDirectBandId,
    J2kDirectColorPlan, J2kDirectGrayscalePlan, J2kDirectGrayscaleStep, J2kDirectIdwtStep,
    J2kDirectStoreStep, J2kIdwtBand, J2kOwnedSubBandPlan, J2kRect, J2kSingleDecompositionIdwtJob,
    J2kWaveletTransform,
};

mod allocation;
use allocation::{prepare_direct_scratch, DirectWorkspaceBudget};
mod color;
use color::apply_inverse_mct_region;
pub use color::{execute_direct_color_plan_rgb8_into, execute_direct_color_plan_rgba8_into};
mod component;
use component::{
    checked_area, checked_sub_band_job_output_range, execute_component_plan, execute_idwt_step,
    prepare_sub_band_output, resize_and_zero, store_component, SubBandJobOutputRange,
};
mod referenced;
pub use referenced::{
    execute_referenced_htj2k_plan, execute_referenced_htj2k_plan_from_payloads,
    J2kDirectDecodedComponents, J2kDirectDecodedPlane,
};
mod referenced_staged;
pub use referenced_staged::{
    execute_referenced_classic_entropy_job, execute_referenced_htj2k_entropy_job,
    finish_referenced_classic_staged, finish_referenced_classic_tile_staged,
    finish_referenced_htj2k_staged, finish_referenced_htj2k_tile_staged,
    prepare_referenced_classic_entropy_workspace, prepare_referenced_classic_staged,
    prepare_referenced_classic_tile_staged, prepare_referenced_htj2k_entropy_workspace,
    prepare_referenced_htj2k_staged, prepare_referenced_htj2k_tile_staged, J2kDirectCodeBlockIndex,
    J2kDirectCpuEntropyWorkspace,
};
mod referenced_classic;
pub use referenced_classic::{
    execute_referenced_classic_plan, execute_referenced_classic_plan_from_payloads,
};

/// Adapter reusable scratch for executing direct J2K RGB plans on the CPU.
#[derive(Debug, Default)]
pub struct J2kDirectCpuScratch {
    component_band_sets: Vec<DirectComponentBandScratch>,
    component_planes: Vec<DirectComponentPlane>,
    compressed_payload: Vec<u8>,
    classic_workspace: J2kCodeBlockDecodeWorkspace,
    ht_workspace: HtCodeBlockDecodeWorkspace,
    staged_state: Option<StagedDirectState>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum StagedDirectRoute {
    Classic,
    Htj2k,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct StagedDirectState {
    route: StagedDirectRoute,
    next_tile: usize,
    active_tile: Option<usize>,
    tile_count: usize,
}

impl J2kDirectCpuScratch {
    /// Create empty direct-plan CPU scratch.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            component_band_sets: Vec::new(),
            component_planes: Vec::new(),
            compressed_payload: Vec::new(),
            classic_workspace: J2kCodeBlockDecodeWorkspace::empty(),
            ht_workspace: HtCodeBlockDecodeWorkspace::empty(),
            staged_state: None,
        }
    }

    /// Release retained scratch allocations.
    pub fn clear(&mut self) {
        *self = Self::new();
    }

    /// Bytes retained by the parse-free HT code-block workspace.
    #[doc(hidden)]
    #[must_use]
    pub fn retained_ht_workspace_bytes(&self) -> usize {
        self.ht_workspace.allocated_bytes().unwrap_or(usize::MAX)
    }

    /// Bytes retained by the parse-free classic code-block workspace.
    #[doc(hidden)]
    #[must_use]
    pub fn retained_classic_workspace_bytes(&self) -> usize {
        self.classic_workspace
            .allocated_bytes()
            .unwrap_or(usize::MAX)
    }

    /// Number of coefficient/IDWT buffer owners retained by this scratch.
    ///
    /// Referenced multi-tile execution retains the maximum live tile shape,
    /// not the sum of every tile's bands.
    #[doc(hidden)]
    #[must_use]
    pub fn retained_band_owner_count(&self) -> usize {
        self.component_band_sets
            .iter()
            .map(|component| component.bands.len())
            .sum()
    }

    /// Prepare retained scratch for `plan` and report the actual-capacity peak
    /// allocation for one execution, including the retained plan and temporary
    /// scalar code-block workspace.
    ///
    /// This is an adapter accounting hook used to admit concurrent direct CPU
    /// executions without treating every small ROI as a worst-case full-frame
    /// native decode.
    #[doc(hidden)]
    pub fn prepare_execution_allocation_bytes(
        &mut self,
        plan: &J2kDirectColorPlan,
    ) -> Result<usize> {
        prepare_direct_scratch(plan, self).map(DirectWorkspaceBudget::peak_bytes)
    }

    #[cfg(test)]
    fn allocation_profile_for_tests(&self) -> DirectScratchAllocationProfile {
        let band_buffers = self
            .component_band_sets
            .iter()
            .map(|component| component.bands.len())
            .sum();
        let band_sample_len = self
            .component_band_sets
            .iter()
            .flat_map(|component| component.bands.iter())
            .map(|band| band.coefficients.len())
            .sum();
        let band_sample_capacity = self
            .component_band_sets
            .iter()
            .flat_map(|component| component.bands.iter())
            .map(|band| band.coefficients.capacity())
            .sum();
        let component_sample_len = self
            .component_planes
            .iter()
            .map(|plane| plane.samples.len())
            .sum();
        let component_sample_capacity = self
            .component_planes
            .iter()
            .map(|plane| plane.samples.capacity())
            .sum();
        DirectScratchAllocationProfile {
            component_band_sets: self.component_band_sets.len(),
            component_planes: self.component_planes.len(),
            band_buffers,
            band_sample_len,
            band_sample_capacity,
            component_sample_len,
            component_sample_capacity,
            compressed_payload_capacity: self.compressed_payload.capacity(),
            classic_workspace_bytes: self.retained_classic_workspace_bytes(),
            ht_workspace_bytes: self.retained_ht_workspace_bytes(),
        }
    }
}

#[cfg(test)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct DirectScratchAllocationProfile {
    component_band_sets: usize,
    component_planes: usize,
    band_buffers: usize,
    band_sample_len: usize,
    band_sample_capacity: usize,
    component_sample_len: usize,
    component_sample_capacity: usize,
    compressed_payload_capacity: usize,
    classic_workspace_bytes: usize,
    ht_workspace_bytes: usize,
}

#[derive(Debug, Default)]
struct DirectComponentBandScratch {
    bands: Vec<DirectCpuBand>,
    active_len: usize,
}

impl DirectComponentBandScratch {
    fn reset(&mut self) {
        self.active_len = 0;
    }

    fn active(&self) -> &[DirectCpuBand] {
        &self.bands[..self.active_len]
    }

    fn prepare_band(
        &mut self,
        band_id: J2kDirectBandId,
        rect: J2kRect,
        len: usize,
    ) -> Result<usize> {
        let index = self.active_len;
        if index == self.bands.len() {
            return Err(DecodingError::HostAllocationFailed.into());
        }
        let band = &mut self.bands[index];
        band.band_id = band_id;
        band.rect = rect;
        resize_and_zero(&mut band.coefficients, len)?;
        self.active_len += 1;
        Ok(index)
    }
}

#[derive(Debug)]
struct DirectCpuBand {
    band_id: J2kDirectBandId,
    rect: J2kRect,
    coefficients: Vec<f32>,
}

impl DirectCpuBand {
    const fn empty() -> Self {
        Self {
            band_id: 0,
            rect: J2kRect {
                x0: 0,
                y0: 0,
                x1: 0,
                y1: 0,
            },
            coefficients: Vec::new(),
        }
    }
}

#[derive(Debug, Default)]
struct DirectComponentPlane {
    width: u32,
    height: u32,
    samples: Vec<f32>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{encode_htj2k, DecodeSettings, DecoderContext, EncodeOptions, Image};
    use alloc::vec;

    const EMPTY_DIRECT_CPU_SCRATCH: J2kDirectCpuScratch = J2kDirectCpuScratch::new();

    #[test]
    fn direct_cpu_scratch_constructor_remains_const() {
        let scratch = EMPTY_DIRECT_CPU_SCRATCH;
        assert_eq!(
            scratch.allocation_profile_for_tests(),
            DirectScratchAllocationProfile {
                component_band_sets: 0,
                component_planes: 0,
                band_buffers: 0,
                band_sample_len: 0,
                band_sample_capacity: 0,
                component_sample_len: 0,
                component_sample_capacity: 0,
                compressed_payload_capacity: 0,
                classic_workspace_bytes: 0,
                ht_workspace_bytes: 0,
            }
        );
    }

    fn direct_htj2k_rgb_plan() -> (J2kDirectColorPlan, J2kRect) {
        let pixels = (0..16 * 16 * 3)
            .map(|idx| {
                u8::try_from((idx * 13 + idx / 3) & 0xff)
                    .expect("test pattern is masked to one byte")
            })
            .collect::<Vec<_>>();
        let options = EncodeOptions {
            reversible: true,
            num_decomposition_levels: 2,
            ..EncodeOptions::default()
        };
        let bytes = encode_htj2k(&pixels, 16, 16, 3, 8, false, &options).expect("encode HTJ2K RGB");
        let image = Image::new(
            &bytes,
            &DecodeSettings {
                target_resolution: Some((4, 4)),
                ..DecodeSettings::default()
            },
        )
        .expect("scaled image");
        let output_region = J2kRect {
            x0: 1,
            y0: 1,
            x1: 3,
            y1: 3,
        };
        let mut context = DecoderContext::default();
        let plan = image
            .build_direct_color_plan_region_with_context(&mut context, (1, 1, 2, 2))
            .expect("direct color plan");
        (plan, output_region)
    }

    #[test]
    fn direct_cpu_scratch_retains_component_buffers_between_executions() {
        let (plan, output_region) = direct_htj2k_rgb_plan();
        let stride = output_region.width() as usize * 3;
        let mut out = vec![0_u8; stride * output_region.height() as usize];
        let mut scratch = J2kDirectCpuScratch::new();

        execute_direct_color_plan_rgb8_into(&plan, output_region, &mut scratch, &mut out, stride)
            .expect("first direct execute");
        let first = scratch.allocation_profile_for_tests();

        execute_direct_color_plan_rgb8_into(&plan, output_region, &mut scratch, &mut out, stride)
            .expect("second direct execute");
        let second = scratch.allocation_profile_for_tests();

        assert_eq!(first.component_band_sets, 3);
        assert_eq!(first.component_planes, 3);
        assert_eq!(second.component_band_sets, first.component_band_sets);
        assert_eq!(second.component_planes, first.component_planes);
        assert_eq!(second.band_buffers, first.band_buffers);
        assert_eq!(
            second.component_sample_capacity,
            first.component_sample_capacity
        );
        assert_eq!(second.band_sample_capacity, first.band_sample_capacity);
        assert!(second.band_sample_capacity >= second.band_sample_len);
        assert!(second.component_sample_capacity >= second.component_sample_len);
    }

    #[test]
    fn direct_cpu_scratch_clear_releases_every_retained_owner() {
        let (plan, output_region) = direct_htj2k_rgb_plan();
        let stride = output_region.width() as usize * 3;
        let mut out = vec![0_u8; stride * output_region.height() as usize];
        let mut scratch = J2kDirectCpuScratch::new();
        execute_direct_color_plan_rgb8_into(&plan, output_region, &mut scratch, &mut out, stride)
            .expect("populate direct scratch");

        scratch.clear();

        assert_eq!(
            scratch.allocation_profile_for_tests(),
            DirectScratchAllocationProfile {
                component_band_sets: 0,
                component_planes: 0,
                band_buffers: 0,
                band_sample_len: 0,
                band_sample_capacity: 0,
                component_sample_len: 0,
                component_sample_capacity: 0,
                compressed_payload_capacity: 0,
                classic_workspace_bytes: 0,
                ht_workspace_bytes: 0,
            }
        );
        assert_eq!(scratch.component_band_sets.capacity(), 0);
        assert_eq!(scratch.component_planes.capacity(), 0);
    }

    #[test]
    fn prepared_direct_execution_reports_its_actual_peak_allocation() {
        let (plan, _) = direct_htj2k_rgb_plan();
        let plan_bytes = plan.retained_allocation_bytes().expect("plan bytes");
        let mut scratch = J2kDirectCpuScratch::new();

        let peak = scratch
            .prepare_execution_allocation_bytes(&plan)
            .expect("prepare direct execution");

        assert!(peak >= plan_bytes);
        assert!(peak <= crate::DEFAULT_MAX_DECODE_BYTES);
        assert_eq!(
            scratch
                .prepare_execution_allocation_bytes(&plan)
                .expect("re-prepare retained direct execution"),
            peak
        );
    }

    #[test]
    fn direct_cpu_rejects_aggregate_planes_before_allocating_scratch() {
        let (mut plan, _) = direct_htj2k_rgb_plan();
        for component in &mut plan.component_plans {
            for step in &mut component.steps {
                if let J2kDirectGrayscaleStep::Store(store) = step {
                    store.output_width = 60_000;
                    store.output_height = 60_000;
                }
            }
        }
        let mut scratch = J2kDirectCpuScratch::new();

        assert_eq!(
            prepare_direct_scratch(&plan, &mut scratch),
            Err(crate::DecodeError::Validation(
                crate::ValidationError::ImageTooLarge
            ))
        );
        assert_eq!(scratch.component_band_sets.capacity(), 0);
        assert_eq!(scratch.component_planes.capacity(), 0);
    }
}