j2k-native 0.7.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
// SPDX-License-Identifier: MIT OR Apache-2.0

use super::{
    bail, checked_decode_byte_len3, internal_j2k_code_block_style, internal_j2k_sub_band_type, j2c,
    EncodedHtJ2kCodeBlock, EncodedJ2kCodeBlock, HtCleanupEncodeDistribution, J2kCodeBlockSegment,
    J2kCodeBlockStyle, J2kForwardDwt53Level, J2kForwardDwt53Output, J2kForwardDwt97Level,
    J2kForwardDwt97Output, J2kPacketizationEncodeJob, J2kSubBandType, J2kTier1TokenSegment, Result,
    ValidationError, Vec, MAX_DEINTERLEAVE_REFERENCE_BIT_DEPTH,
};
use crate::{DecodingError, EncodeError, EncodeResult};

/// Adapter scalar classic J2K encoder helper for backend experimentation.
#[doc(hidden)]
pub fn encode_j2k_code_block_scalar_with_style(
    coefficients: &[i32],
    width: u32,
    height: u32,
    sub_band_type: J2kSubBandType,
    total_bitplanes: u8,
    style: J2kCodeBlockStyle,
) -> EncodeResult<EncodedJ2kCodeBlock> {
    let encoded = j2c::bitplane_encode::try_encode_code_block_segments_with_style(
        coefficients,
        width,
        height,
        internal_j2k_sub_band_type(sub_band_type),
        total_bitplanes,
        &internal_j2k_code_block_style(style),
    )?;
    try_encoded_j2k_code_block_from_internal(encoded)
}

/// Adapter scalar Classic Tier-1 compact token packer for backend experimentation.
///
/// The token format matches the Metal Classic Tier-1 token-emitter contract:
/// arithmetic segments are 6-bit `(context_label, bit)` MQ tokens, while raw
/// bypass segments are one bit per raw bypass event.
#[doc(hidden)]
pub fn pack_j2k_code_block_scalar_from_tier1_tokens(
    token_bytes: &[u8],
    token_segments: &[J2kTier1TokenSegment],
    number_of_coding_passes: u8,
    missing_bit_planes: u8,
) -> EncodeResult<EncodedJ2kCodeBlock> {
    let encoded = j2c::bitplane_encode::try_pack_classic_selective_bypass_tier1_tokens(
        token_bytes,
        token_segments,
        number_of_coding_passes,
        missing_bit_planes,
    )?;
    try_encoded_j2k_code_block_from_internal(encoded)
}

fn try_encoded_j2k_code_block_from_internal(
    encoded: j2c::bitplane_encode::EncodedCodeBlockWithSegments,
) -> EncodeResult<EncodedJ2kCodeBlock> {
    let mut segments = j2c::encode::allocation::try_untracked_vec(
        encoded.segments.len(),
        "public classic Tier-1 segment metadata",
    )?;
    for segment in encoded.segments {
        segments.push(J2kCodeBlockSegment {
            data_offset: segment.data_offset,
            data_length: segment.data_length,
            start_coding_pass: segment.start_coding_pass,
            end_coding_pass: segment.end_coding_pass,
            use_arithmetic: segment.use_arithmetic,
        });
    }

    Ok(EncodedJ2kCodeBlock {
        data: encoded.data,
        segments,
        number_of_coding_passes: encoded.num_coding_passes,
        missing_bit_planes: encoded.num_zero_bitplanes,
    })
}

/// Adapter scalar HTJ2K cleanup-only encoder helper for backend experimentation.
#[doc(hidden)]
pub fn encode_ht_code_block_scalar(
    coefficients: &[i32],
    width: u32,
    height: u32,
    total_bitplanes: u8,
) -> EncodeResult<EncodedHtJ2kCodeBlock> {
    let encoded =
        j2c::ht_block_encode::try_encode_code_block(coefficients, width, height, total_bitplanes)?;
    Ok(EncodedHtJ2kCodeBlock {
        data: encoded.data,
        cleanup_length: encoded.ht_cleanup_length,
        refinement_length: encoded.ht_refinement_length,
        num_coding_passes: encoded.num_coding_passes,
        num_zero_bitplanes: encoded.num_zero_bitplanes,
    })
}

/// Adapter scalar HTJ2K encoder helper with an explicit coding-pass request.
#[doc(hidden)]
pub fn encode_ht_code_block_scalar_with_passes(
    coefficients: &[i32],
    width: u32,
    height: u32,
    total_bitplanes: u8,
    target_coding_passes: u8,
) -> EncodeResult<EncodedHtJ2kCodeBlock> {
    let encoded = j2c::ht_block_encode::try_encode_code_block_with_passes(
        coefficients,
        width,
        height,
        total_bitplanes,
        target_coding_passes,
    )?;
    Ok(EncodedHtJ2kCodeBlock {
        data: encoded.data,
        cleanup_length: encoded.ht_cleanup_length,
        refinement_length: encoded.ht_refinement_length,
        num_coding_passes: encoded.num_coding_passes,
        num_zero_bitplanes: encoded.num_zero_bitplanes,
    })
}

/// Adapter HTJ2K cleanup-encode distribution helper for benchmark tuning.
#[doc(hidden)]
pub fn collect_ht_cleanup_encode_distribution(
    coefficients: &[i32],
    width: u32,
    height: u32,
    total_bitplanes: u8,
) -> EncodeResult<HtCleanupEncodeDistribution> {
    j2c::ht_block_encode::collect_encode_distribution(coefficients, width, height, total_bitplanes)
}

/// Adapter scalar forward 5/3 DWT reference for CUDA stage parity.
///
/// Runs the native CPU reversible 5/3 forward DWT on `samples` and returns
/// the decomposed subbands packed into the public `J2kForwardDwt53Output`
/// type.  The returned layout matches what the encoder feeds to Tier-1.
///
/// # Errors
///
/// Returns a typed error for invalid caller geometry, arithmetic overflow, or
/// a host allocation failure while materializing the decomposed reference.
#[doc(hidden)]
pub fn forward_dwt53_reference(
    samples: &[f32],
    width: u32,
    height: u32,
    num_levels: u8,
) -> EncodeResult<J2kForwardDwt53Output> {
    let decomposition = j2c::fdwt::try_forward_dwt(samples, width, height, num_levels, true)?;
    try_public_dwt_output::<Dwt53OutputAdapter>(decomposition)
}

/// Adapter scalar forward 9/7 DWT reference for Metal/CUDA stage parity.
///
/// Runs the native CPU irreversible 9/7 forward DWT on `samples` and returns
/// the decomposed subbands packed into the public `J2kForwardDwt97Output`
/// type. The returned layout matches what the encoder feeds to Tier-1.
///
/// # Errors
///
/// Returns a typed error for invalid caller geometry, arithmetic overflow, or
/// a host allocation failure while materializing the decomposed reference.
#[doc(hidden)]
pub fn forward_dwt97_reference(
    samples: &[f32],
    width: u32,
    height: u32,
    num_levels: u8,
) -> EncodeResult<J2kForwardDwt97Output> {
    let decomposition = j2c::fdwt::try_forward_dwt(samples, width, height, num_levels, false)?;
    try_public_dwt_output::<Dwt97OutputAdapter>(decomposition)
}

trait PublicDwtOutputAdapter {
    type Level;
    type Output;

    fn level(level: j2c::fdwt::DwtLevel) -> Self::Level;
    fn output(
        ll: Vec<f32>,
        ll_width: u32,
        ll_height: u32,
        levels: Vec<Self::Level>,
    ) -> Self::Output;
}

struct Dwt53OutputAdapter;

impl PublicDwtOutputAdapter for Dwt53OutputAdapter {
    type Level = J2kForwardDwt53Level;
    type Output = J2kForwardDwt53Output;

    fn level(level: j2c::fdwt::DwtLevel) -> Self::Level {
        J2kForwardDwt53Level {
            hl: level.hl,
            lh: level.lh,
            hh: level.hh,
            width: level.low_width + level.high_width,
            height: level.low_height + level.high_height,
            low_width: level.low_width,
            low_height: level.low_height,
            high_width: level.high_width,
            high_height: level.high_height,
        }
    }

    fn output(
        ll: Vec<f32>,
        ll_width: u32,
        ll_height: u32,
        levels: Vec<Self::Level>,
    ) -> Self::Output {
        J2kForwardDwt53Output {
            ll,
            ll_width,
            ll_height,
            levels,
        }
    }
}

struct Dwt97OutputAdapter;

impl PublicDwtOutputAdapter for Dwt97OutputAdapter {
    type Level = J2kForwardDwt97Level;
    type Output = J2kForwardDwt97Output;

    fn level(level: j2c::fdwt::DwtLevel) -> Self::Level {
        J2kForwardDwt97Level {
            hl: level.hl,
            lh: level.lh,
            hh: level.hh,
            width: level.low_width + level.high_width,
            height: level.low_height + level.high_height,
            low_width: level.low_width,
            low_height: level.low_height,
            high_width: level.high_width,
            high_height: level.high_height,
        }
    }

    fn output(
        ll: Vec<f32>,
        ll_width: u32,
        ll_height: u32,
        levels: Vec<Self::Level>,
    ) -> Self::Output {
        J2kForwardDwt97Output {
            ll,
            ll_width,
            ll_height,
            levels,
        }
    }
}

fn try_public_dwt_output<A: PublicDwtOutputAdapter>(
    decomposition: j2c::fdwt::DwtDecomposition,
) -> EncodeResult<A::Output> {
    let level_count = decomposition.levels.len();
    let requested_bytes = level_count
        .checked_mul(core::mem::size_of::<A::Level>())
        .ok_or(EncodeError::ArithmeticOverflow {
            what: "scalar DWT public level owner bytes",
        })?;
    let mut levels = Vec::new();
    levels
        .try_reserve_exact(level_count)
        .map_err(|_| EncodeError::HostAllocationFailed {
            what: "scalar DWT public level owners",
            bytes: requested_bytes,
        })?;
    levels.extend(decomposition.levels.into_iter().map(A::level));
    Ok(A::output(
        decomposition.ll,
        decomposition.ll_width,
        decomposition.ll_height,
        levels,
    ))
}

/// Adapter scalar forward RCT reference for CUDA stage parity.
///
/// Applies the native CPU forward Reversible Color Transform to three
/// component planes supplied as owned `Vec<f32>` arrays.  The transform is
/// applied in place and the mutated planes are returned, so callers do not
/// need to pass a mutable slice.
#[doc(hidden)]
#[must_use]
pub fn forward_rct_reference(mut planes: Vec<Vec<f32>>) -> Vec<Vec<f32>> {
    j2c::forward_mct::forward_rct(&mut planes);
    planes
}

/// Adapter scalar forward ICT reference for Metal/CUDA stage parity.
///
/// Applies the native CPU forward Irreversible Color Transform to three
/// component planes supplied as owned `Vec<f32>` arrays. The transform is
/// applied in place and the mutated planes are returned.
#[doc(hidden)]
#[must_use]
pub fn forward_ict_reference(mut planes: Vec<Vec<f32>>) -> Vec<Vec<f32>> {
    j2c::forward_mct::forward_ict(&mut planes);
    planes
}

/// Adapter scalar sub-band quantization reference for backend stage parity.
#[doc(hidden)]
#[must_use]
pub fn quantize_subband_reference(
    coefficients: &[f32],
    step_exponent: u16,
    step_mantissa: u16,
    range_bits: u8,
    reversible: bool,
) -> Vec<i32> {
    let step = j2c::quantize::QuantStepSize {
        exponent: step_exponent,
        mantissa: step_mantissa,
    };
    j2c::quantize::quantize_subband(coefficients, &step, range_bits, reversible)
}

/// Adapter scalar reversible sub-band quantization reference for CUDA stage parity.
///
/// Quantizes `coefficients` using the reversible (lossless) integer path of
/// the native CPU quantizer.  `step_exponent` and `step_mantissa` encode the
/// JPEG 2000 `QuantStepSize` for the sub-band; `range_bits` is the nominal
/// bit depth for the sub-band.  When `reversible` is `true` the step-size
/// parameters are ignored and each coefficient is rounded to the nearest
/// integer.
#[doc(hidden)]
#[must_use]
pub fn quantize_reversible_reference(
    coefficients: &[f32],
    step_exponent: u16,
    step_mantissa: u16,
    range_bits: u8,
    reversible: bool,
) -> Vec<i32> {
    quantize_subband_reference(
        coefficients,
        step_exponent,
        step_mantissa,
        range_bits,
        reversible,
    )
}

fn checked_deinterleave_reference_bytes_per_sample(bit_depth: u8) -> Result<usize> {
    if bit_depth == 0 || bit_depth > MAX_DEINTERLEAVE_REFERENCE_BIT_DEPTH {
        bail!(ValidationError::InvalidComponentMetadata);
    }
    Ok(usize::from(bit_depth).div_ceil(8).max(1))
}

/// Checked adapter scalar pixel deinterleave/level-shift reference for backend
/// stage parity.
///
/// Converts interleaved pixel bytes to per-component f32 planes with the
/// same level-shift logic as the native CPU encode path.  The result is one
/// `Vec<f32>` per component, each of length `num_pixels`.
///
/// The input byte slice must exactly contain
/// `num_pixels * num_components * bytes_per_sample(bit_depth)` bytes.
#[doc(hidden)]
pub fn try_deinterleave_reference(
    pixels: &[u8],
    num_pixels: usize,
    num_components: u16,
    bit_depth: u8,
    signed: bool,
) -> Result<Vec<Vec<f32>>> {
    if num_components == 0 {
        bail!(ValidationError::InvalidComponentMetadata);
    }
    let bytes_per_sample = checked_deinterleave_reference_bytes_per_sample(bit_depth)?;
    let expected_len =
        checked_decode_byte_len3(num_pixels, usize::from(num_components), bytes_per_sample)?;
    if pixels.len() != expected_len {
        bail!(ValidationError::InvalidComponentMetadata);
    }
    j2c::encode::try_deinterleave_to_f32(pixels, num_pixels, num_components, bit_depth, signed)
        .map_err(|error| map_deinterleave_encode_error(&error))
}

fn map_deinterleave_encode_error(error: &EncodeError) -> crate::DecodeError {
    match error {
        EncodeError::InvalidInput { .. } | EncodeError::Unsupported { .. } => {
            ValidationError::InvalidComponentMetadata.into()
        }
        EncodeError::ArithmeticOverflow { .. } | EncodeError::AllocationTooLarge { .. } => {
            ValidationError::ImageTooLarge.into()
        }
        EncodeError::HostAllocationFailed { .. } => DecodingError::HostAllocationFailed.into(),
        EncodeError::Accelerator { .. }
        | EncodeError::CodestreamValidation { .. }
        | EncodeError::InternalInvariant { .. } => {
            DecodingError::CodeBlockDecodeFailureWithContext(
                "scalar deinterleave returned an unexpected encode-stage error",
            )
            .into()
        }
    }
}

/// Adapter scalar Tier-2 packetization helper for backend experimentation.
#[doc(hidden)]
pub fn encode_j2k_packetization_scalar(
    job: J2kPacketizationEncodeJob<'_>,
) -> crate::EncodeResult<Vec<u8>> {
    j2c::packet_encode::form_borrowed_packetization_scalar(job, 0)
}