rust-j2k 0.5.0

Decodes JPEG 2000 images in pure Rust, for programs that need to read them without linking a C library.
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
//! Structured fuzz entry points (see `fuzz/`).
//!
//! The `decode` fuzz target must assemble a byte-perfect main header before a
//! mutation reaches tier-2 or tier-1, so those state machines see little of
//! the fuzzer's throughput. The helpers here pin the well-formed scaffolding
//! and hand the fuzzer only the untrusted bytes one stage consumes. The
//! contract under test is the crate-wide one — typed errors, never a panic,
//! bounded work — so results are discarded.
//!
//! `#[doc(hidden)]` and semver-exempt: a test seam for the detached fuzz
//! workspace, not public API. A smoke test below keeps it building in CI.

use crate::tier1::ebcot::mq::MqDecoder;
use crate::tier1::ebcot::passes::{
    BlockParams, BlockState, Orientation, decode_block, init_contexts,
};
use crate::tier2::{CodeBlock, CodedSegment};

/// Drive the raw MQ decoder over `data`: context transitions, BYTEIN
/// stuffing, marker handling, and past-end synthesis, with bounded work.
pub fn mq_stream(data: &[u8]) {
    let mut mq = MqDecoder::new(data);
    // The real per-block context array, standard initial states included.
    let mut contexts = init_contexts();
    // Enough decisions to consume every coded byte and run well past the end
    // into marker synthesis, capped so a large input cannot hang an exec.
    let decisions = (data.len() * 8).clamp(256, 1 << 20);
    for i in 0..decisions {
        let _ = mq.decode(&mut contexts[i % contexts.len()]);
    }
}

/// Decode one code-block whose shape, coding parameters, and coded bytes are
/// all steered by `data`, the way tier-2 would hand them to tier-1. Each
/// parameter reads its own byte so no region of the grid is unreachable.
pub fn tier1_block(data: &[u8]) {
    let [a, b, c, d, e, f, g, coded @ ..] = data else {
        return;
    };
    use crate::codestream::markers::code_block_style::{PTERM, RESET, SEGSYM, TERMALL, VCAUSAL};

    let width = u32::from(a & 0x3F) + 1; // 1..=64, the subset's block ceiling
    let height = u32::from(b & 0x3F) + 1;
    let orient = match a >> 6 {
        0 => Orientation::Ll,
        1 => Orientation::Hl,
        2 => Orientation::Lh,
        _ => Orientation::Hh,
    };
    let params = BlockParams {
        // Every decoded style flag whose contract this hook can honor. `LAZY`
        // stays out: `decode_block` requires the lazy split's homogeneous
        // segments (raw runs never carry a cleanup pass), and this hook feeds
        // one unsplit segment — the `tile_body` target's bypass shape covers
        // it through the real tier-2 split instead. The HTJ2K bits never
        // reach this decoder: an HT block goes to the HT coder, and a Part 1
        // block of a MIXED tile-component gets the style without them.
        style: c & (RESET | TERMALL | VCAUSAL | PTERM | SEGSYM),
        roi_shift: d & 0x3F,
        // A spare bit of the maxshift byte: whether a layer cap cut the
        // (one) segment short, which lifts its `predictable termination` check.
        last_segment_cut: d & 0x40 != 0,
    };
    let num_passes = u32::from(*e) % 110; // tier-2's 109-pass segment cap
    let numbps = u32::from(*f) % 31; // MAX_BIT_PLANES
    // One past the largest agreeing value, so the zero_bit_planes > numbps
    // saturation path is exercised alongside in-range decodes.
    let zero_bit_planes = u32::from(*g) % 32;

    let mut state = BlockState::new(width, height);
    let segments = [(coded.to_vec(), num_passes)];
    let _ = decode_block(
        &segments,
        &mut state,
        orient,
        numbps,
        num_passes,
        zero_bit_planes,
        params,
    );
}

/// Decode one HT code-block whose shape, coding parameters and segments all
/// come from `data`, straight into the HT coder, the way tier-1 hands it a
/// block. The first 13 bytes steer; the rest are coded bytes.
///
/// - Bytes 0 to 2 and 12: the shape. Its width is 1 to 1024 and its height 1 to
///   `min(1024, 4096 / width)`, so 1x1, 1x1024, 1024x4, 4x1024 and odd sizes
///   all occur, up to the 4096 samples a block holds. With flag bit 3 both
///   sides are 1 to 64 instead.
/// - Byte 3: flags. Bit 0 is the causal flag, bit 1 makes the input dense in
///   0xFF, bit 2 stretches the segments by repeating the coded bytes, bit 3
///   is the small shape above, bit 4 gives a `CAP` bound, bit 5 a maxshift,
///   bit 6 splits each segment into chunks as layers do, and bit 7 rewrites
///   the cleanup segment's last two bytes into a valid `Scup`.
/// - Bytes 4 and 5: `Mb` and `P`, each 0 to 39, past the 30-plane limit.
/// - Byte 6: the coding passes (0 to 4, one more than a set holds) and, from
///   bit 3, the maxshift: 0 to 30, or with 31 the whole of byte 12, 0 to 255.
///   With the maxshift flag, byte 5 is the zero bit-planes up to 255.
/// - Bytes 7 to 10: the cleanup length `Lcup` and the refinement length, each
///   0 to 5000 when the segments are stretched, else cut to the bytes there
///   are.
/// - Byte 11: the `CAP` bound, the chunk split and the `Scup` to write.
/// - Byte 12: the high byte of the height (byte 2 is the low one).
pub fn ht_block(data: &[u8]) {
    let _ = run_ht_block(data);
}

/// [`ht_block`], giving the block's shape and the samples it decoded, or `None` when `data` is too
/// short to steer a block.
fn run_ht_block(data: &[u8]) -> Option<((usize, usize), crate::Result<usize>)> {
    let [
        w0,
        w1,
        h,
        flags,
        mb,
        zbp,
        pr,
        l0,
        l1,
        r0,
        r1,
        extra,
        h1,
        coded @ ..,
    ] = data
    else {
        return None;
    };
    let width = 1 + usize::from(u16::from_le_bytes([*w0, *w1]) % 1024);
    let height = 1 + usize::from(u16::from_le_bytes([*h, *h1])) % (4096 / width).min(1024);
    let (width, height) = if flags & 0x08 != 0 {
        (1 + usize::from(*w0 & 0x3F), 1 + usize::from(*h & 0x3F))
    } else {
        (width, height)
    };
    let numbps = u32::from(*mb) % 40;
    // Under a maxshift the zero bit-planes reach the 255 a byte holds, past
    // `Mb + s`'s usual range, as the packet header allows up to 293.
    let zero_bit_planes = if flags & 0x20 != 0 {
        u32::from(*zbp)
    } else {
        u32::from(*zbp) % 40
    };
    let num_passes = u32::from(pr & 7) % 5;
    let roi_shift = if flags & 0x20 != 0 {
        match (pr >> 3) & 0x1F {
            31 => *h1,
            s => s,
        }
    } else {
        0
    };
    let bound_bits = (flags & 0x10 != 0).then(|| u32::from(*extra) % 80);

    // The two segments' bytes: `coded` in order, or `coded` repeated to the
    // lengths asked for (to 5000 bytes each).
    let stretch = flags & 0x04 != 0;
    let dense = |b: u8| {
        if flags & 0x02 != 0 && b >= 0x40 {
            0xFF
        } else {
            b
        }
    };
    let cleanup_len = usize::from(u16::from_le_bytes([*l0, *l1]) % 5001);
    let refine_len = usize::from(u16::from_le_bytes([*r0, *r1]) % 5001);
    let (mut cleanup, refinement): (Vec<u8>, Vec<u8>) = if stretch && !coded.is_empty() {
        let fill = |from: usize, len: usize| -> Vec<u8> {
            (0..len)
                .map(|i| dense(coded[(from + i) % coded.len()]))
                .collect()
        };
        (fill(0, cleanup_len), fill(cleanup_len, refine_len))
    } else {
        let (a, b) = coded.split_at(cleanup_len.min(coded.len()));
        (
            a.iter().map(|&x| dense(x)).collect(),
            b.iter().take(refine_len).map(|&x| dense(x)).collect(),
        )
    };
    if flags & 0x80 != 0 && cleanup.len() >= 2 {
        let n = cleanup.len();
        let scup = 2 + usize::from(*extra) * 16 % (n.min(4079) - 1);
        cleanup[n - 1] = (scup >> 4) as u8;
        cleanup[n - 2] = (cleanup[n - 2] & 0xF0) | (scup & 0xF) as u8;
    }

    // A segment's bytes in one chunk, or two, as two layers deliver them.
    fn chunks(bytes: &[u8], split: Option<u8>) -> Vec<&[u8]> {
        match split {
            Some(at) if bytes.len() > 1 => {
                let at = 1 + usize::from(at) % (bytes.len() - 1);
                vec![&bytes[..at], &bytes[at..]]
            }
            _ => vec![bytes],
        }
    }
    let split = (flags & 0x40 != 0).then_some(*extra);
    let mut segments = vec![CodedSegment {
        passes: 1,
        chunks: chunks(&cleanup, split),
    }];
    if num_passes > 1 || !refinement.is_empty() {
        segments.push(CodedSegment {
            passes: num_passes.saturating_sub(1),
            chunks: chunks(&refinement, split),
        });
    }
    let block = CodeBlock {
        x: 0,
        y: 0,
        width,
        height,
        num_passes,
        zero_bit_planes,
        segments,
        cut: false,
        placeholders: 0,
        coder: crate::tier2::Coder::Ht,
        missing_from: None,
    };
    let decoded =
        crate::tier1::decode_ht_block(&block, numbps, roi_shift, flags & 1 != 0, bound_bits);
    if let Ok(coefficients) = &decoded {
        assert_eq!(coefficients.len(), width * height);
    }
    Some(((width, height), decoded.map(|c| c.len())))
}

/// Open `data` as an ICC profile and read it as a monochrome and as an RGB
/// input profile, without building the transform to sRGB. The transform is
/// far costlier than the reading, so the `icc_srgb` target builds it for
/// only some inputs.
pub fn icc_profile(data: &[u8]) {
    crate::jp2::SrgbPlan::read_profile(data);
}

/// An [`IncrementalDecoder`](crate::IncrementalDecoder) as if its earlier
/// refines had spent `blocks` of the code-block work, so the
/// `incremental_decode` target reaches the cut that bound makes without
/// inputs large enough to spend it. Otherwise it is
/// [`IncrementalDecoder::new`](crate::IncrementalDecoder::new).
pub fn incremental_with_code_block_work_spent(
    options: crate::DecodeOptions,
    blocks: u64,
) -> crate::Result<crate::IncrementalDecoder> {
    crate::IncrementalDecoder::with_code_block_work_spent(options, blocks)
}

/// The code-block work `decoder`'s refines have spent, and the code-block
/// bound of the last decode a refine tried: `None` when that decode did not
/// get as far as the bound, or the options set none.
pub fn incremental_code_block_work(decoder: &crate::IncrementalDecoder) -> (u64, Option<u64>) {
    decoder.code_block_work()
}

#[cfg(test)]
mod tests {
    /// A decoder started with work spent is cut by what is left of the
    /// bound: a refine whose work just fits decodes, and one more unit of
    /// work spent refuses it. The hooks report the work and the bound.
    #[test]
    fn incremental_hooks_cut_the_code_block_bound() {
        use crate::{DecodeOptions, decode_with};
        let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("tests/fixtures/layers/lrcp.j2k");
        let bytes = std::fs::read(path).unwrap();
        let start = |spent: u64| {
            let mut decoder =
                super::incremental_with_code_block_work_spent(DecodeOptions::default(), spent)
                    .unwrap();
            decoder.feed(&bytes);
            let refined = decoder.refine().unwrap();
            (refined, super::incremental_code_block_work(&decoder))
        };
        let (image, (work, bound)) = start(0);
        let bound = bound.unwrap();
        assert!(work > 0 && work < bound, "{work} of {bound}");
        let lenient = DecodeOptions::default().with_strict(false);
        assert_eq!(image.unwrap(), decode_with(&bytes, &lenient).unwrap());

        let (fits, (spent, _)) = start(bound - work);
        assert!(fits.is_some());
        assert_eq!(spent, bound);
        let (cut, (spent, seen)) = start(bound - work + 1);
        assert_eq!(cut, None);
        assert_eq!(seen, Some(bound));
        assert_eq!(spent, bound - work + 1, "a refused tile spends nothing");
    }

    /// Smoke-run every hook so the fuzz surface builds and stays panic-free in
    /// CI, where `--cfg fuzzing` never is set.
    #[test]
    fn fuzz_hooks_run_on_sample_inputs() {
        let samples: [&[u8]; 5] = [
            &[],
            &[0xFF; 40],
            &[0x00; 40],
            &[0x55, 0x1E, 0x22, 0x0A, 0x03, 0x80, 0xFF, 0x91],
            &[0xC0, 0x24, 0x6D, 0x1E, 0x21, 0xFF, 0xD9, 0x00],
        ];
        for sample in samples {
            super::mq_stream(sample);
            super::tier1_block(sample);
            super::ht_block(sample);
            super::icc_profile(sample);
        }
    }

    /// The hook reaches the tall shapes the review of #360 and #367 asked for:
    /// a height past 256 reads its second byte.
    #[test]
    fn ht_block_reaches_tall_shapes() {
        let shape = |w: u16, h: u16| {
            let mut data = vec![0u8; 13];
            data[..2].copy_from_slice(&(w - 1).to_le_bytes());
            data[2] = (h - 1) as u8;
            data[12] = ((h - 1) >> 8) as u8;
            super::run_ht_block(&data).unwrap().0
        };
        assert_eq!(shape(1, 1024), (1, 1024));
        assert_eq!(shape(4, 1024), (4, 1024));
        assert_eq!(shape(2, 1024), (2, 1024));
        assert_eq!(shape(8, 512), (8, 512));
        assert_eq!(shape(1024, 4), (1024, 4));
        assert_eq!(shape(1, 1), (1, 1));
    }

    /// The HT conformance codestreams whose blocks seed the `ht_block` target.
    const HT_SEED_FILES: [&str; 6] = [
        "ds0_ht_02_b11",
        "ds0_ht_04_b11",
        "ds0_ht_05_b11",
        "ds1_ht_01_b11",
        "ds1_ht_04_b9",
        "ds0_ht_12_b11",
    ];

    /// Slice the code-blocks of an HT codestream into `ht_block` inputs, each
    /// with whether it must decode (the tall copies of a block carry bytes made
    /// for another shape and need not): the
    /// block's shape, passes and bytes, in the layout the hook reads. A seed's
    /// `Mb` is `P + 6`, which leaves planes to code.
    fn ht_seeds(name: &str) -> Vec<(Vec<u8>, bool)> {
        let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("tests/fixtures/conformance/ht/codestreams")
            .join(format!("{name}.j2k"));
        // The conformance directory is not in the published package.
        let Ok(bytes) = std::fs::read(path) else {
            return Vec::new();
        };
        let parsed = crate::codestream::parse_header(&bytes).unwrap();
        let cs = crate::codestream::parse_tiles(&bytes, parsed).unwrap();
        let mut seeds = Vec::new();
        for (index, tile) in cs.tiles.iter().enumerate() {
            let header = cs.tile_header(index).unwrap();
            let mut visits = u64::MAX;
            let coded =
                crate::tier2::decode_packets(&header, tile, &mut visits, u64::MAX, 0).unwrap();
            let blocks = coded
                .components
                .iter()
                .flat_map(|c| &c.resolutions)
                .flat_map(|r| &r.subbands)
                .flat_map(|s| &s.blocks);
            for block in blocks {
                let [cleanup, rest @ ..] = &block.segments[..] else {
                    continue;
                };
                let cleanup = cleanup.bytes();
                let refinement = rest.first().map(|s| s.bytes()).unwrap_or_default();
                let mut seed = ((block.width - 1) as u16).to_le_bytes().to_vec();
                seed.push((block.height - 1) as u8);
                seed.push(0);
                seed.push((block.zero_bit_planes + 6) as u8);
                seed.push(block.zero_bit_planes as u8);
                seed.push(block.num_passes as u8);
                seed.extend((cleanup.len() as u16).to_le_bytes());
                seed.extend((refinement.len() as u16).to_le_bytes());
                seed.push(0);
                seed.push(((block.height - 1) >> 8) as u8);
                seed.extend(cleanup.iter());
                seed.extend(refinement.iter());
                // The same bytes in the tall shapes the corpus lacks.
                if seeds.len() % 64 == 0 {
                    for (w, h) in [(1u16, 1024u16), (4, 1024), (2, 1024), (8, 512)] {
                        let mut tall = seed.clone();
                        tall[..2].copy_from_slice(&(w - 1).to_le_bytes());
                        tall[2] = (h - 1) as u8;
                        tall[12] = ((h - 1) >> 8) as u8;
                        seeds.push((tall, false));
                    }
                }
                seeds.push((seed, true));
            }
        }
        seeds
    }

    /// A one-pass block of `width` x `height` over the cleanup segment
    /// `F2 00` (`Scup = 2`), which decodes at any shape.
    fn tall_seed(width: u16, height: u16) -> Vec<u8> {
        let mut seed = (width - 1).to_le_bytes().to_vec();
        seed.extend([(height - 1) as u8, 0, 8, 0, 1, 2, 0, 0, 0, 0]);
        seed.push(((height - 1) >> 8) as u8);
        seed.extend([0xF2, 0x00]);
        seed
    }

    /// The tall shapes decode: one seed for each.
    #[test]
    fn ht_block_tall_seeds_decode() {
        for (w, h) in [(1u16, 1024u16), (2, 1024), (3, 1024), (4, 1024), (8, 512)] {
            let (shape, decoded) = super::run_ht_block(&tall_seed(w, h)).unwrap();
            assert_eq!(shape, (usize::from(w), usize::from(h)));
            assert_eq!(decoded.unwrap(), usize::from(w) * usize::from(h), "{w}x{h}");
        }
    }

    /// The seeds sliced from the HT corpus decode through the hook, so the
    /// target starts from blocks it accepts. Set `RUST_J2K_HT_SEED_DIR` to
    /// write them out as the target's seed corpus (see `fuzz/README.md`).
    #[test]
    fn ht_block_seeds_from_the_corpus_decode() {
        let dir = std::env::var_os("RUST_J2K_HT_SEED_DIR").map(std::path::PathBuf::from);
        let (mut total, mut refined) = (0usize, 0usize);
        for name in HT_SEED_FILES {
            for (i, (seed, must_decode)) in ht_seeds(name).iter().enumerate() {
                let width = 1 + usize::from(u16::from_le_bytes([seed[0], seed[1]]));
                let height = 1 + usize::from(u16::from_le_bytes([seed[2], seed[12]]));
                let (shape, decoded) = super::run_ht_block(seed).expect("a full header");
                assert_eq!(shape, (width, height));
                if *must_decode {
                    assert_eq!(decoded.expect("a corpus block decodes"), width * height);
                }
                total += 1;
                refined += usize::from(seed[6] > 1);
                if let Some(dir) = &dir {
                    std::fs::create_dir_all(dir).unwrap();
                    std::fs::write(dir.join(format!("{name}_{i}")), seed).unwrap();
                }
            }
        }
        if let Some(dir) = &dir {
            for (w, h) in [(1, 1024), (2, 1024), (3, 1024), (4, 1024), (8, 512)] {
                std::fs::write(dir.join(format!("tall_{w}x{h}")), tall_seed(w, h)).unwrap();
            }
        }
        if total > 0 {
            assert!(refined > 0, "no seed runs a refinement pass");
        }
    }
}