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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Aggregate direct-plan input, retained scratch, and scalar workspace accounting.

use super::{
    checked_area, DirectComponentBandScratch, DirectComponentPlane, DirectCpuBand,
    J2kDirectCpuScratch,
};
use crate::error::{DecodeError, DecodingError, Result, ValidationError};
use crate::j2c::{bitplane, ht_block_decode};
use crate::{
    try_reserve_decode_elements, J2kDirectColorPlan, J2kDirectGrayscalePlan,
    J2kDirectGrayscaleStep, J2kReferencedClassicPlan, J2kReferencedHtj2kPlan,
    DEFAULT_MAX_DECODE_BYTES,
};
use alloc::vec::Vec;
use core::mem::size_of;

mod referenced;
pub(super) use self::referenced::{
    max_referenced_classic_dimensions, max_referenced_ht_dimensions,
    prepare_referenced_classic_scratch, prepare_referenced_classic_staged_scratch,
    prepare_referenced_direct_scratch, prepare_referenced_htj2k_staged_scratch,
};

pub(super) fn prepare_direct_scratch(
    plan: &J2kDirectColorPlan,
    scratch: &mut J2kDirectCpuScratch,
) -> Result<DirectWorkspaceBudget> {
    prepare_component_scratch(
        &plan.component_plans,
        plan.retained_allocation_bytes()?,
        0,
        None,
        None,
        scratch,
    )
}

fn prepare_component_scratch(
    components: &[J2kDirectGrayscalePlan],
    retained_plan_bytes: usize,
    compressed_payload_bytes: usize,
    retained_classic_workspace_dimensions: Option<(u32, u32)>,
    retained_ht_workspace_dimensions: Option<(u32, u32)>,
    scratch: &mut J2kDirectCpuScratch,
) -> Result<DirectWorkspaceBudget> {
    normalize_retained_scratch(
        components,
        compressed_payload_bytes,
        retained_classic_workspace_dimensions.is_some(),
        retained_ht_workspace_dimensions.is_some(),
        scratch,
    )?;
    if let Err(error) = validate_aggregate_plan(
        components,
        retained_plan_bytes,
        compressed_payload_bytes,
        retained_classic_workspace_dimensions,
        retained_ht_workspace_dimensions,
        scratch,
    ) {
        if !matches!(
            error,
            DecodeError::Validation(ValidationError::ImageTooLarge)
        ) {
            return Err(error);
        }
        // Retention is optional. Retry from an empty owner so prior larger
        // plans cannot make the current logical request fail.
        scratch.clear();
        validate_aggregate_plan(
            components,
            retained_plan_bytes,
            compressed_payload_bytes,
            retained_classic_workspace_dimensions,
            retained_ht_workspace_dimensions,
            scratch,
        )?;
    }

    if let Err(error) = reserve_scratch(
        components,
        compressed_payload_bytes,
        retained_classic_workspace_dimensions,
        retained_ht_workspace_dimensions,
        scratch,
    ) {
        scratch.clear();
        return Err(error);
    }
    match validate_aggregate_plan(
        components,
        retained_plan_bytes,
        compressed_payload_bytes,
        retained_classic_workspace_dimensions,
        retained_ht_workspace_dimensions,
        scratch,
    ) {
        Ok(workspace_budget) => Ok(workspace_budget),
        Err(error) => {
            scratch.clear();
            Err(error)
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct DirectWorkspaceBudget {
    base_bytes: usize,
    peak_bytes: usize,
}

impl DirectWorkspaceBudget {
    pub(super) fn validate_workspace(self, workspace_bytes: usize) -> Result<()> {
        let total = self
            .base_bytes
            .checked_add(workspace_bytes)
            .ok_or(ValidationError::ImageTooLarge)?;
        if total > DEFAULT_MAX_DECODE_BYTES {
            return Err(ValidationError::ImageTooLarge.into());
        }
        Ok(())
    }

    pub(super) const fn peak_bytes(self) -> usize {
        self.peak_bytes
    }
}

fn normalize_retained_scratch(
    components: &[J2kDirectGrayscalePlan],
    compressed_payload_bytes: usize,
    retain_classic_workspace: bool,
    retain_ht_workspace: bool,
    scratch: &mut J2kDirectCpuScratch,
) -> Result<()> {
    let component_count = components.len();
    normalize_outer_owner(&mut scratch.component_band_sets, component_count);
    normalize_outer_owner(&mut scratch.component_planes, component_count);

    fill_without_allocation(
        &mut scratch.component_band_sets,
        component_count,
        DirectComponentBandScratch::default,
    );
    fill_without_allocation(
        &mut scratch.component_planes,
        component_count,
        DirectComponentPlane::default,
    );

    for (component_idx, component_plan) in components.iter().enumerate() {
        let band_count = component_band_count(component_plan)?;
        if let Some(component) = scratch.component_band_sets.get_mut(component_idx) {
            normalize_outer_owner(&mut component.bands, band_count);
            fill_without_allocation(&mut component.bands, band_count, DirectCpuBand::empty);
            for_each_band_target(component_plan, |band_idx, target_len| {
                if let Some(band) = component.bands.get_mut(band_idx) {
                    band.coefficients.clear();
                    if band.coefficients.capacity() < target_len {
                        band.coefficients = Vec::new();
                    }
                }
                Ok(())
            })?;
            component.active_len = 0;
        }

        let plane_len = component_plane_len(component_plan)?;
        if let Some(plane) = scratch.component_planes.get_mut(component_idx) {
            plane.samples.clear();
            if plane.samples.capacity() < plane_len {
                plane.samples = Vec::new();
            }
        }
    }
    scratch.compressed_payload.clear();
    if scratch.compressed_payload.capacity() < compressed_payload_bytes {
        scratch.compressed_payload = Vec::new();
    }
    if !retain_classic_workspace {
        scratch.classic_workspace = crate::J2kCodeBlockDecodeWorkspace::default();
    }
    if !retain_ht_workspace {
        scratch.ht_workspace = crate::HtCodeBlockDecodeWorkspace::default();
    }
    Ok(())
}

fn normalize_outer_owner<T>(values: &mut Vec<T>, target_len: usize) {
    if values.capacity() < target_len {
        *values = Vec::new();
    } else {
        values.truncate(target_len);
    }
}

fn fill_without_allocation<T>(
    values: &mut Vec<T>,
    target_len: usize,
    mut new_value: impl FnMut() -> T,
) {
    while values.len() < target_len && values.len() < values.capacity() {
        values.push(new_value());
    }
}

fn validate_aggregate_plan(
    components: &[J2kDirectGrayscalePlan],
    retained_plan_bytes: usize,
    compressed_payload_bytes: usize,
    retained_classic_workspace_dimensions: Option<(u32, u32)>,
    retained_ht_workspace_dimensions: Option<(u32, u32)>,
    scratch: &J2kDirectCpuScratch,
) -> Result<DirectWorkspaceBudget> {
    let mut budget = DirectAllocationBudget::default();
    let temporary_workspace_bytes =
        include_plan_allocations(&mut budget, components, retained_plan_bytes)?;
    include_scratch_allocations(
        &mut budget,
        components,
        compressed_payload_bytes,
        retained_classic_workspace_dimensions,
        retained_ht_workspace_dimensions,
        scratch,
    )?;
    let base_bytes = budget.bytes;
    budget.include_bytes(temporary_workspace_bytes)?;
    Ok(DirectWorkspaceBudget {
        base_bytes,
        peak_bytes: budget.bytes,
    })
}

#[derive(Default)]
struct DirectAllocationBudget {
    bytes: usize,
}

impl DirectAllocationBudget {
    fn include_capacity<T>(&mut self, capacity: usize) -> Result<()> {
        let bytes = capacity
            .checked_mul(size_of::<T>())
            .ok_or(ValidationError::ImageTooLarge)?;
        self.include_bytes(bytes)
    }

    fn include_bytes(&mut self, bytes: usize) -> Result<()> {
        self.bytes = self
            .bytes
            .checked_add(bytes)
            .ok_or(ValidationError::ImageTooLarge)?;
        if self.bytes > DEFAULT_MAX_DECODE_BYTES {
            return Err(ValidationError::ImageTooLarge.into());
        }
        Ok(())
    }
}

fn include_plan_allocations(
    budget: &mut DirectAllocationBudget,
    components: &[J2kDirectGrayscalePlan],
    retained_plan_bytes: usize,
) -> Result<usize> {
    budget.include_bytes(retained_plan_bytes)?;
    let mut classic_dimensions = None;
    let mut ht_dimensions = None;
    for component in components {
        for step in &component.steps {
            match step {
                J2kDirectGrayscaleStep::ClassicSubBand(sub_band) => {
                    for job in &sub_band.jobs {
                        observe_max_dimensions(&mut classic_dimensions, job.width, job.height);
                    }
                }
                J2kDirectGrayscaleStep::HtSubBand(sub_band) => {
                    for job in &sub_band.jobs {
                        observe_max_dimensions(&mut ht_dimensions, job.width, job.height);
                    }
                }
                J2kDirectGrayscaleStep::Idwt(_) | J2kDirectGrayscaleStep::Store(_) => {}
            }
        }
    }
    let classic_bytes = classic_dimensions.map_or(Ok(0), |(width, height)| {
        bitplane::classic_decode_workspace_bytes(width, height)
    })?;
    let ht_bytes = ht_dimensions.map_or(Ok(0), |(width, height)| {
        ht_block_decode::ht_decode_workspace_bytes(width, height)
    })?;
    Ok(classic_bytes.max(ht_bytes))
}

fn observe_max_dimensions(target: &mut Option<(u32, u32)>, width: u32, height: u32) {
    *target = Some(
        target.map_or((width, height), |(current_width, current_height)| {
            (current_width.max(width), current_height.max(height))
        }),
    );
}

fn include_scratch_allocations(
    budget: &mut DirectAllocationBudget,
    components: &[J2kDirectGrayscalePlan],
    compressed_payload_bytes: usize,
    retained_classic_workspace_dimensions: Option<(u32, u32)>,
    retained_ht_workspace_dimensions: Option<(u32, u32)>,
    scratch: &J2kDirectCpuScratch,
) -> Result<()> {
    let component_count = components.len();
    budget.include_capacity::<DirectComponentBandScratch>(
        scratch.component_band_sets.capacity().max(component_count),
    )?;
    budget.include_capacity::<DirectComponentPlane>(
        scratch.component_planes.capacity().max(component_count),
    )?;

    for (component_idx, component_plan) in components.iter().enumerate() {
        let band_count = component_band_count(component_plan)?;
        let component = scratch.component_band_sets.get(component_idx);
        budget.include_capacity::<DirectCpuBand>(
            component
                .map_or(0, |component| component.bands.capacity())
                .max(band_count),
        )?;
        for_each_band_target(component_plan, |band_idx, target_len| {
            let retained_capacity = component
                .and_then(|component| component.bands.get(band_idx))
                .map_or(0, |band| band.coefficients.capacity());
            budget.include_capacity::<f32>(retained_capacity.max(target_len))
        })?;

        let plane_len = component_plane_len(component_plan)?;
        let retained_capacity = scratch
            .component_planes
            .get(component_idx)
            .map_or(0, |plane| plane.samples.capacity());
        budget.include_capacity::<f32>(retained_capacity.max(plane_len))?;
    }
    budget.include_capacity::<u8>(
        scratch
            .compressed_payload
            .capacity()
            .max(compressed_payload_bytes),
    )?;
    let retained_classic_workspace_bytes = scratch.classic_workspace.allocated_bytes()?;
    let target_classic_workspace_bytes = retained_classic_workspace_dimensions
        .map_or(Ok(0), |(width, height)| {
            bitplane::classic_decode_workspace_bytes(width, height)
        })?;
    budget.include_bytes(retained_classic_workspace_bytes.max(target_classic_workspace_bytes))?;
    let retained_ht_workspace_bytes = scratch.ht_workspace.allocated_bytes()?;
    let target_ht_workspace_bytes = retained_ht_workspace_dimensions
        .map_or(Ok(0), |(width, height)| {
            ht_block_decode::ht_decode_workspace_bytes(width, height)
        })?;
    budget.include_bytes(retained_ht_workspace_bytes.max(target_ht_workspace_bytes))?;
    Ok(())
}

fn reserve_scratch(
    components: &[J2kDirectGrayscalePlan],
    compressed_payload_bytes: usize,
    retained_classic_workspace_dimensions: Option<(u32, u32)>,
    retained_ht_workspace_dimensions: Option<(u32, u32)>,
    scratch: &mut J2kDirectCpuScratch,
) -> Result<()> {
    let component_count = components.len();
    try_reserve_decode_elements(&mut scratch.component_band_sets, component_count)?;
    try_reserve_decode_elements(&mut scratch.component_planes, component_count)?;
    while scratch.component_band_sets.len() < component_count {
        scratch
            .component_band_sets
            .push(DirectComponentBandScratch::default());
    }
    while scratch.component_planes.len() < component_count {
        scratch
            .component_planes
            .push(DirectComponentPlane::default());
    }

    for (component_idx, component_plan) in components.iter().enumerate() {
        let component = &mut scratch.component_band_sets[component_idx];
        let band_count = component_band_count(component_plan)?;
        try_reserve_decode_elements(&mut component.bands, band_count)?;
        while component.bands.len() < band_count {
            component.bands.push(DirectCpuBand::empty());
        }
        for_each_band_target(component_plan, |band_idx, target_len| {
            try_reserve_decode_elements(&mut component.bands[band_idx].coefficients, target_len)
        })?;

        let plane_len = component_plane_len(component_plan)?;
        try_reserve_decode_elements(
            &mut scratch.component_planes[component_idx].samples,
            plane_len,
        )?;
    }
    try_reserve_decode_elements(&mut scratch.compressed_payload, compressed_payload_bytes)?;
    if let Some((width, height)) = retained_classic_workspace_dimensions {
        scratch.classic_workspace.prepare(width, height)?;
    }
    if let Some((width, height)) = retained_ht_workspace_dimensions {
        scratch.ht_workspace.prepare(width, height)?;
    }
    Ok(())
}

fn component_band_count(plan: &J2kDirectGrayscalePlan) -> Result<usize> {
    plan.steps.iter().try_fold(0usize, |count, step| {
        if matches!(
            step,
            J2kDirectGrayscaleStep::ClassicSubBand(_)
                | J2kDirectGrayscaleStep::HtSubBand(_)
                | J2kDirectGrayscaleStep::Idwt(_)
        ) {
            count
                .checked_add(1)
                .ok_or(ValidationError::ImageTooLarge.into())
        } else {
            Ok(count)
        }
    })
}

fn component_plane_len(plan: &J2kDirectGrayscalePlan) -> Result<usize> {
    let mut dimensions = None;
    for step in &plan.steps {
        let J2kDirectGrayscaleStep::Store(store) = step else {
            continue;
        };
        let current = (store.output_width, store.output_height);
        if dimensions.is_some_and(|dimensions| dimensions != current) {
            return Err(crate::DecodingError::CodeBlockDecodeFailure.into());
        }
        dimensions = Some(current);
    }
    dimensions.map_or(Ok(0), |(width, height)| checked_area(width, height))
}

fn for_each_band_target(
    plan: &J2kDirectGrayscalePlan,
    mut visit: impl FnMut(usize, usize) -> Result<()>,
) -> Result<()> {
    let mut band_idx = 0usize;
    for step in &plan.steps {
        let target_len = match step {
            J2kDirectGrayscaleStep::ClassicSubBand(sub_band) => {
                Some(checked_area(sub_band.width, sub_band.height)?)
            }
            J2kDirectGrayscaleStep::HtSubBand(sub_band) => {
                Some(checked_area(sub_band.width, sub_band.height)?)
            }
            J2kDirectGrayscaleStep::Idwt(step) => {
                Some(checked_area(step.rect.width(), step.rect.height())?)
            }
            J2kDirectGrayscaleStep::Store(_) => None,
        };
        if let Some(target_len) = target_len {
            visit(band_idx, target_len)?;
            band_idx = band_idx
                .checked_add(1)
                .ok_or(ValidationError::ImageTooLarge)?;
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{DirectAllocationBudget, DirectWorkspaceBudget};
    use crate::error::{DecodeError, ValidationError};
    use crate::DEFAULT_MAX_DECODE_BYTES;

    #[test]
    fn aggregate_budget_has_an_exact_shared_cap_boundary() {
        let mut budget = DirectAllocationBudget {
            bytes: DEFAULT_MAX_DECODE_BYTES - 1,
        };
        budget.include_bytes(1).expect("exact boundary fits");
        assert_eq!(
            budget.include_bytes(1),
            Err(DecodeError::Validation(ValidationError::ImageTooLarge))
        );
    }

    #[test]
    fn actual_scalar_workspace_uses_the_remaining_direct_budget() {
        let budget = DirectWorkspaceBudget {
            base_bytes: DEFAULT_MAX_DECODE_BYTES - 1,
            peak_bytes: DEFAULT_MAX_DECODE_BYTES - 1,
        };
        budget.validate_workspace(1).expect("exact boundary fits");
        assert_eq!(
            budget.validate_workspace(2),
            Err(DecodeError::Validation(ValidationError::ImageTooLarge))
        );
    }
}