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
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
501
502
503
504
505
506
//! The decode's tier-2 work bound: a codestream whose packets make tier-2
//! walk far more than its bytes is refused with [`Error::Limit`], in about the
//! time a decode of that many bytes takes, instead of running for seconds or
//! minutes. OpenJPEG 2.5.0 has no such bound and walks them all; see
//! `tests/fixtures/work_bound/README.md` and `docs/resource-bounds.md`.
//!
//! Packet headers that name many code-blocks and include none are not such
//! a walk: the decode steps over the code-blocks a header reads no bit for,
//! a row at a time, so those files decode, as encoder output with many
//! layers does.
//!
//! The committed files are written by `scripts/gen-work-bound-fixtures.py`.
//! The larger shapes are built here, with the same layout.

mod support;
use support::synth::{self, bx, seg};

use std::path::PathBuf;

use rust_j2k::{DecodeOptions, Error, IncrementalDecoder, decode, decode_with, read_header};
use support::jp2::sha256_hex;

fn dir() -> PathBuf {
    synth::fixtures("work_bound")
}

fn assert_limit(name: &str, bytes: &[u8]) {
    match decode(bytes) {
        Err(Error::Limit(m)) => assert!(m.message().contains("work bound"), "{name}: {m}"),
        Err(e) => panic!("{name}: expected the work bound, got {e}"),
        Ok(_) => panic!("{name}: expected the work bound, got an image"),
    }
    // The header does not walk a raw codestream's packets.
    read_header(bytes).unwrap_or_else(|e| panic!("{name}: {e}"));
}

/// Decodes to one flat component of 128, as OpenJPEG 2.5.0 decodes it.
fn assert_flat(name: &str, bytes: &[u8], width: usize, height: usize) {
    let image = decode(bytes).unwrap_or_else(|e| panic!("{name}: {e}"));
    assert_eq!(image.components.len(), 1, "{name}");
    let samples = &image.components[0].samples;
    assert_eq!(samples.len(), width * height, "{name}");
    assert!(samples.iter().all(|&s| s == 128), "{name}: not flat 128");
}

/// A codestream of 8-bit components, one per entry of `comps` (each its
/// `(XRsiz, YRsiz)`), on the image area `[x0, x0 + width) x [0, height)` in
/// `tile` x `tile` tiles, under the COD body `cod` and the QCD body `qcd`.
/// Each tile's data is `body`.
#[allow(clippy::too_many_arguments)]
fn codestream(
    x0: u32,
    width: u32,
    height: u32,
    tile: u32,
    comps: &[(u8, u8)],
    cod: &[u8],
    qcd: &[u8],
    body: &[u8],
) -> Vec<u8> {
    let mut siz = 0u16.to_be_bytes().to_vec();
    for v in [x0 + width, height, x0, 0, tile, tile, x0, 0] {
        siz.extend_from_slice(&v.to_be_bytes());
    }
    siz.extend_from_slice(&(comps.len() as u16).to_be_bytes());
    for &(dx, dy) in comps {
        siz.extend_from_slice(&[7, dx, dy]);
    }
    let mut cs = vec![0xFF, 0x4F];
    cs.extend(seg(0xFF51, &siz));
    cs.extend(seg(0xFF52, cod));
    cs.extend(seg(0xFF5C, qcd));
    let tiles = width.div_ceil(tile) * height.div_ceil(tile);
    for index in 0..tiles {
        let psot = 12 + 2 + body.len() as u32;
        cs.extend_from_slice(&[0xFF, 0x90, 0, 10]);
        cs.extend_from_slice(&(index as u16).to_be_bytes());
        cs.extend_from_slice(&psot.to_be_bytes());
        cs.extend_from_slice(&[0, 1, 0xFF, 0x93]);
        cs.extend_from_slice(body);
    }
    cs.extend_from_slice(&[0xFF, 0xD9]);
    cs
}

/// One component in `tile` x `tile` tiles, each with no wavelet levels,
/// 4x4 code-blocks in one precinct and `layers` LRCP layers, whose packets
/// are `first` and then 0x80 (present, a bit of 0 for each tag-tree node
/// read). Each code-block is named in every packet's header, and none is
/// included.
fn layers_of_nothing(width: u32, height: u32, tile: u32, layers: u16, first: u8) -> Vec<u8> {
    let mut cod = vec![0, 0];
    cod.extend_from_slice(&layers.to_be_bytes());
    cod.extend_from_slice(&[0, 0, 0, 0, 0, 1]);
    let mut body = vec![first];
    body.resize(usize::from(layers), 0x80);
    codestream(
        0,
        width,
        height,
        tile,
        &[(1, 1)],
        &cod,
        &[0x40, 0x40],
        &body,
    )
}

/// `t2bomb` from the generator: every packet 0x80, so the inclusion tree's
/// root is past each layer, and the decode steps over all of the tile's
/// code-blocks at once.
fn t2bomb(width: u32, height: u32, tile: u32, layers: u16) -> Vec<u8> {
    layers_of_nothing(width, height, tile, layers, 0x80)
}

/// `t2skip` from the generator: the first packet 0xC0, so the root resolves
/// and each of its children is read past each layer instead. On a tile of 512
/// x 512 (128 x 128 code-blocks) every packet then takes 256 steps, two for
/// each row of code-blocks, for one byte; on 2048 x 1024, 512 steps.
fn t2skip(width: u32, height: u32, tile: u32, layers: u16) -> Vec<u8> {
    layers_of_nothing(width, height, tile, layers, 0xC0)
}

/// One sample column on x in [1, 2), `2 * tiles` rows in tiles of two, in
/// `progression` (0 LRCP, 1 RLCP) with 65535 layers, `levels` wavelet levels
/// and `comps` components. Every resolution but the top one is empty in x, so
/// it holds no packet; each tile is 65535 x `comps` empty packets (0x00). The
/// layer-resolution loops take a step at each of 65535 x (`levels` + 1)
/// points per tile, and one for each component with a precinct there.
fn empty_resolutions(progression: u8, comps: usize, tiles: u32, levels: u8) -> Vec<u8> {
    let mut cod = vec![0, progression];
    cod.extend_from_slice(&u16::MAX.to_be_bytes());
    cod.extend_from_slice(&[0, levels, 0, 0, 0, 1]);
    let mut qcd = vec![0x40];
    qcd.extend(std::iter::repeat_n(0x40, 3 * usize::from(levels) + 1));
    let body = vec![0; usize::from(u16::MAX) * comps];
    codestream(1, 1, 2 * tiles, 2, &vec![(1, 1); comps], &cod, &qcd, &body)
}

/// One sample column on x in [1, 2), 2^17 rows, one tile, in `progression`
/// (2 RPCL, 3 PCRL, 4 CPRL), one layer, 32 wavelet levels and 16
/// components. Every resolution but the top one is empty in x. Component 0's
/// top precinct is 2^15 x 2, so it has 2^16 precincts and sets the sweep's
/// step to 2 rows; the other 15 have 2^15 x 2^15, four each. Every packet is
/// empty (0x00).
fn thin_column(progression: u8) -> Vec<u8> {
    const COMPS: u16 = 16;
    const HEIGHT: u32 = 1 << 17;
    let mut siz = 0u16.to_be_bytes().to_vec();
    for v in [2, HEIGHT, 1, 0, 2, HEIGHT, 0, 0] {
        siz.extend_from_slice(&u32::to_be_bytes(v));
    }
    siz.extend_from_slice(&COMPS.to_be_bytes());
    for _ in 0..COMPS {
        siz.extend_from_slice(&[7, 1, 1]);
    }
    // User precincts; one layer; 32 levels, 64x64 code-blocks, 5/3; then
    // (PPx, PPy) = (15, 15) at every resolution, and for component 0 (15, 1)
    // at the top one.
    let mut cod = vec![1, progression, 0, 1, 0, 32, 4, 4, 0, 1];
    cod.extend([0xFF; 33]);
    let mut coc = vec![0, 1, 32, 4, 4, 0, 1];
    coc.extend([0xFF; 32]);
    coc.push(0x1F);
    let mut qcd = vec![0x40];
    qcd.extend(std::iter::repeat_n(0x40, 3 * 32 + 1));
    let packets = HEIGHT / 2 + u32::from(COMPS - 1) * HEIGHT.div_ceil(1 << 15);
    let mut cs = vec![0xFF, 0x4F];
    for (code, body) in [(0xFF51, siz), (0xFF52, cod), (0xFF53, coc), (0xFF5C, qcd)] {
        cs.extend(seg(code, &body));
    }
    cs.extend_from_slice(&[0xFF, 0x90, 0, 10, 0, 0]);
    cs.extend_from_slice(&(12 + 2 + packets).to_be_bytes());
    cs.extend_from_slice(&[0, 1, 0xFF, 0x93]);
    cs.resize(cs.len() + packets as usize, 0);
    cs.extend_from_slice(&[0xFF, 0xD9]);
    cs
}

/// The committed shapes past the bound: RPCL and PCRL sweeps that check 64
/// components at each of 512^2 positions for about 2000 empty packets, a
/// one-column CPRL file whose sweep visits 2^23 positions for 512 packets,
/// once and repeated by 31 POC volumes, and `t2skip` on 512 x 256 code-blocks
/// with 40000 layers, about 2 x 10^7 steps for 40 KB of packets. Each
/// decodes to a flat image with an unbounded walk, as OpenJPEG 2.5.0 decodes
/// it.
#[test]
fn the_committed_shapes_are_refused_at_the_work_bound() {
    for name in [
        "sweep_rpcl_1024_64.j2k",
        "sweep_pcrl_1024_64.j2k",
        "cprl_tall.j2k",
        "cprl_tall_poc31.j2k",
        "t2skip_2048x1024_40000.j2k",
    ] {
        let bytes = std::fs::read(dir().join(name)).unwrap();
        assert_limit(name, &bytes);
    }
}

/// Packets that name every code-block and include none, under a root the
/// header puts past each layer: the committed 1 KB file of 2^17 code-blocks
/// and 1000 layers, and 65535 layers over 2^14 and 2^19 code-blocks. Each
/// cost a visit per code-block per layer, up to 3.4 x 10^10 visits, before
/// the decode stepped over them; now each layer is one step. A row at a time
/// would be 4.7 x 10^7 steps for the last, past its bound. OpenJPEG 2.5.0
/// decodes all three to a flat 128.
#[test]
fn packets_that_include_nothing_decode() {
    let name = "t2bomb_2048x1024_1000.j2k";
    let bytes = std::fs::read(dir().join(name)).unwrap();
    assert_flat(name, &bytes, 2048, 1024);
    assert_flat(
        "t2bomb 512 x 65535",
        &t2bomb(512, 512, 512, 65535),
        512,
        512,
    );
    assert_flat(
        "t2bomb 2896 x 65535",
        &t2bomb(2896, 2896, 2896, 65535),
        2896,
        2896,
    );
}

/// 31 identical POC volumes over 4 precincts and 1024 layers of empty
/// packets: each packet is read once and met 30 times more, skipped without
/// a visit each time. OpenJPEG 2.5.0 decodes it to a flat 128.
#[test]
fn repeated_poc_volumes_decode() {
    let name = "pocdup_4x4_1024_31.j2k";
    let bytes = std::fs::read(dir().join(name)).unwrap();
    assert_flat(name, &bytes, 4, 4);
}

/// `opj_compress` output with 4x4 code-blocks and 64 or 100 quality layers on
/// a nearly flat 2048^2 image. OpenJPEG's encoder sets every packet's
/// "present" bit, so each layer's header names every one of its 262144
/// code-blocks; both files were refused before the decode stepped over the
/// ones read past the layer. Each decodes to the samples OpenJPEG 2.5.0
/// decodes (the SHA-256 of its 8-bit output).
#[test]
fn encoder_output_with_many_layers_decodes() {
    for (name, sha256) in [
        (
            "opj_patch2048_b4_q64.j2k",
            "80c5ce50a9d9924b4429898da04cf9d3e6ba7b8d8775521a84bfd030bf76d529",
        ),
        (
            "opj_patch2048_b4_q100.j2k",
            "3f2603a0a9cdd97c064f5f865d96adeea056f434a135756395a9763731a20251",
        ),
    ] {
        let bytes = std::fs::read(dir().join(name)).unwrap();
        let image = decode(&bytes).unwrap_or_else(|e| panic!("{name}: {e}"));
        let component = &image.components[0];
        assert_eq!((component.width, component.height), (2048, 2048), "{name}");
        let samples: Vec<u8> = component.samples.iter().map(|&s| s as u8).collect();
        assert_eq!(sha256_hex(&samples), sha256, "{name}");
    }
}

/// `setskip` from the generator: one 38608 x 1 tile in RPCL, no wavelet
/// levels, 1 x 1 precincts, `layers` layers and `volumes` identical POC
/// volumes. One component at `XRsiz` 254 sets the sweep's step; 64 more at
/// 255 have 152 precincts each, but the sweep meets only their first, so
/// the volumes reach 9880 packets a layer and read 216. Every packet read is
/// empty (0x00).
fn setskip(layers: u16, volumes: usize) -> Vec<u8> {
    let mut comps = vec![(254, 1)];
    comps.extend([(255, 1); 64]);
    let mut cod = vec![1, 2];
    cod.extend_from_slice(&layers.to_be_bytes());
    cod.extend_from_slice(&[0, 0, 0, 0, 0, 1, 0x00]);
    let body = vec![0; 216 * usize::from(layers)];
    let mut cs = codestream(
        0,
        152 * 254,
        1,
        152 * 254,
        &comps,
        &cod,
        &[0x40, 0x40],
        &body,
    );
    // RSpoc, CSpoc, LYEpoc, REpoc, CEpoc, Ppoc, before the first SOT.
    let mut volume = vec![0, 0];
    volume.extend_from_slice(&layers.to_be_bytes());
    volume.extend_from_slice(&[1, 65, 2]);
    let sot = cs.windows(2).position(|w| w == [0xFF, 0x90]).unwrap();
    cs.splice(sot..sot, seg(0xFF5F, &volume.repeat(volumes)));
    cs
}

/// More packets reached than the bitmap may hold, in a positional order, so
/// the packets read are held as per-precinct counts, and each one they skip is
/// charged 12 steps: 2048 layers in 31 volumes skip 1.3 x 10^7 packets,
/// past the bound for their 442 KB. With one volume, nothing is skipped and
/// it decodes. OpenJPEG 2.5.0 decodes both to a flat 128.
#[test]
fn skips_from_the_poc_counts_are_bounded() {
    assert_limit("setskip 31 volumes", &setskip(2048, 31));
    let image = decode(&setskip(2048, 1)).unwrap();
    assert_eq!(image.components.len(), 65);
    assert!(image.components[0].samples.iter().all(|&s| s == 128));
}

/// A refine after earlier refines holds a POC sequence's packets as a
/// decode of its bytes on its own does: in the bitmap that decode's bound
/// sizes, not as counts, whose skips cost 12 visits each. 1650 layers in
/// 31 volumes reach 1.63 x 10^7 packets, just within the bitmap of the
/// default bound's 2^24 visits, and the decode spends about 10^6 visits. A
/// bitmap sized from what the earlier refines left of the bound would not
/// hold them, and as counts the skips would take 2 x 10^8.
#[test]
fn a_refine_holds_poc_packets_as_one_decode_does() {
    let bytes = setskip(1650, 31);
    let lenient = DecodeOptions::default().with_strict(false);
    let mut decoder = IncrementalDecoder::new(DecodeOptions::default()).unwrap();
    let mut fed = 0;
    for end in [bytes.len() / 4, bytes.len() / 2, bytes.len()] {
        decoder.feed(&bytes[fed..end]);
        fed = end;
        let image = decoder.refine().unwrap();
        assert_eq!(
            image,
            Some(decode_with(&bytes[..end], &lenient).unwrap()),
            "{end}"
        );
    }
    assert_eq!(decoder.finish().unwrap(), decode(&bytes).unwrap());
}

/// `setskip(1650, 31)` in two tiles side by side, the first cut to one
/// byte of packets. The decode counts the first tile's packets past its
/// data, so the second, whose volumes reach 1.63 x 10^7 packets, finds
/// fewer visits left than that: it holds them as counts, and its skips
/// pass the bound, with any share of the second tile's data. A refine holds
/// them as that decode does, and waits; one that sized the bitmap from the
/// whole bound would decode.
#[test]
fn a_refine_sizes_a_later_tiles_poc_bitmap_as_one_decode_does() {
    let layers = 1650u16;
    let mut comps = vec![(254, 1)];
    comps.extend([(255, 1); 64]);
    let mut cod = vec![1, 2];
    cod.extend_from_slice(&layers.to_be_bytes());
    cod.extend_from_slice(&[0, 0, 0, 0, 0, 1, 0x00]);
    let body = vec![0; 216 * usize::from(layers)];
    let width = 152 * 254;
    let mut bytes = codestream(0, 2 * width, 1, width, &comps, &cod, &[0x40, 0x40], &body);
    let sot = bytes.windows(2).position(|w| w == [0xFF, 0x90]).unwrap();
    bytes[sot + 6..sot + 10].copy_from_slice(&15u32.to_be_bytes());
    bytes.drain(sot + 15..sot + 14 + body.len());
    let mut volume = vec![0, 0];
    volume.extend_from_slice(&layers.to_be_bytes());
    volume.extend_from_slice(&[1, 65, 2]);
    bytes.splice(sot..sot, seg(0xFF5F, &volume.repeat(31)));

    let lenient = DecodeOptions::default().with_strict(false);
    match decode_with(&bytes, &lenient) {
        Err(Error::Limit(m)) => assert_eq!(m.kind(), rust_j2k::LimitKind::PacketWork, "{m}"),
        other => panic!("{other:?}"),
    }
    let mut decoder = IncrementalDecoder::new(DecodeOptions::default()).unwrap();
    decoder.feed(&bytes);
    assert_eq!(decoder.refine().unwrap(), None);
    assert_eq!(decoder.bytes_decoded(), 0);
}

/// `cs` in a JP2 file as `jp2_psot0` in the generator writes it: its one
/// tile-part's `Psot` set to 0 and its EOC dropped, in a `jp2c` box that ends
/// where the packets end, followed here by an `xml ` box of `xml`.
fn jp2_psot0(cs: &[u8], xml: &[u8]) -> Vec<u8> {
    let word = |at: usize| u32::from_be_bytes(cs[at..at + 4].try_into().unwrap());
    let ihdr = synth::ihdr_payload(
        word(12) - word(20),
        word(8) - word(16),
        u16::from_be_bytes([cs[40], cs[41]]),
        [7, 7, 0, 0],
    );
    let sot = cs.windows(2).position(|w| w == [0xFF, 0x90]).unwrap();
    let mut body = cs[..sot + 6].to_vec();
    body.extend_from_slice(&[0; 4]);
    body.extend_from_slice(&cs[sot + 10..cs.len() - 2]);
    synth::jp2_file(
        &[bx(b"ihdr", &ihdr), bx(b"colr", &synth::colr_enum(17))],
        &[],
        &body,
        &[bx(b"xml ", xml)],
    )
}

/// `read_header` walks a `Psot = 0` tile's packets to see whether its
/// codestream ends at its box's end, holding a POC sequence's packets in a
/// bitmap wherever the decode does. `setskip(1024, 31)` reaches 1.0e7
/// packets, under the decode's 2^24 visits: the codestream ends at its box,
/// and the `xml ` box after it is reported by both calls. OpenJPEG 2.5.0
/// decodes it to a flat 128. `setskip(2048, 2)` reaches 2.0e7, so the decode
/// would keep counts, and the walk gives up: the codestream runs to the end
/// of the file, the `xml ` box is not reported, and the decode refuses its
/// bytes as left after the last packet, as for the files of
/// `a_psot_zero_tile_too_costly_to_walk_reads_to_the_end` in
/// `tests/psot_zero.rs`.
#[test]
fn a_psot_zero_poc_sequence_ends_at_its_box_within_the_decodes_bitmap() {
    let file = jp2_psot0(&setskip(1024, 31), b"<a/>");
    let image = decode(&file).unwrap();
    assert_eq!(image.components.len(), 65);
    assert!(
        image
            .components
            .iter()
            .all(|c| c.samples.iter().all(|&s| s == 128))
    );
    let xml = |metadata: &rust_j2k::Jp2Metadata| {
        let payloads: Vec<Vec<u8>> = metadata.xml.iter().map(|x| x.payload.clone()).collect();
        payloads
    };
    assert_eq!(xml(image.jp2.as_ref().unwrap()), [b"<a/>".to_vec()]);
    let header = read_header(&file).unwrap();
    assert_eq!(xml(header.jp2.as_ref().unwrap()), [b"<a/>".to_vec()]);

    let file = jp2_psot0(&setskip(2048, 2), b"<a/>");
    assert!(xml(read_header(&file).unwrap().jp2.as_ref().unwrap()).is_empty());
    match decode(&file) {
        Err(Error::Codestream(m)) => {
            assert!(m.message().contains("left after the last packet"), "{m}")
        }
        other => panic!("expected a codestream error, got {other:?}"),
    }
}

/// The bound is one budget for the whole decode, not one per tile: 16 tiles
/// of 128 x 128 code-blocks and 5000 layers spend about 2 x 10^7 steps in
/// all, past the bound for their 80 KB of packets, while each tile alone
/// spends 1.3 million.
#[test]
fn the_tiles_share_one_work_bound() {
    assert_limit("16 tiles", &t2skip(2048, 2048, 512, 5000));
}

/// The layer-resolution orders check only the components that hold a
/// precinct at a resolution, so 32 empty resolutions of 8 components cost
/// one step per (layer, resolution), about 3 million in all for 512 KB of
/// empty packets: it decodes, to the flat 128 OpenJPEG 2.5.0 decodes, in
/// LRCP and RLCP alike. Those points are still steps: one component in 8
/// such tiles spends 1.8 x 10^7 for 512 KB, past the bound.
#[test]
fn the_layer_orders_are_bounded_over_empty_resolutions() {
    for (name, progression) in [("LRCP", 0), ("RLCP", 1)] {
        let image = decode(&empty_resolutions(progression, 8, 1, 32))
            .unwrap_or_else(|e| panic!("{name}: {e}"));
        assert_eq!(image.components.len(), 8, "{name}");
        for component in &image.components {
            assert_eq!(component.samples, [128, 128], "{name}");
        }
        assert_limit(name, &empty_resolutions(progression, 1, 8, 32));
    }
}

/// The layer orders charge a step at each (layer, resolution) and one more
/// for each component that holds a precinct there, on top of the step its
/// packet read takes. In 10 tiles of one component and 30 levels that is 33
/// steps for each byte of empty packets, past the bound of 32, and the decode
/// is `Limit`; with 29 levels it is 32 and decodes, to the flat 128 OpenJPEG
/// 2.5.0 decodes for both. Without the step for the component, 30 levels
/// would decode too.
#[test]
fn the_layer_orders_charge_each_component_they_check() {
    for (name, progression) in [("LRCP", 0), ("RLCP", 1)] {
        assert_limit(name, &empty_resolutions(progression, 1, 10, 30));
        let image = decode(&empty_resolutions(progression, 1, 10, 29))
            .unwrap_or_else(|e| panic!("{name}: {e}"));
        assert_eq!(image.components.len(), 1, "{name}");
        assert_eq!(image.components[0].samples, [128; 20], "{name}");
    }
}

/// The positional orders check only the (component, resolution) pairs that
/// hold a precinct, and RPCL sweeps no resolution where none does. On
/// `thin_column` that is 17 steps at each of 2^16 positions, 1.1 million for
/// 64 KB of empty packets, where checking all 33 resolutions of each
/// component took about 3.5 x 10^7 in RPCL and PCRL, past the bound. Each
/// order decodes to the
/// flat 128 OpenJPEG 2.5.0 decodes, 16 components of 1 x 2^17.
#[test]
fn the_positional_orders_pass_over_resolutions_with_no_precinct() {
    for (name, progression) in [("RPCL", 2), ("PCRL", 3), ("CPRL", 4)] {
        let image = decode(&thin_column(progression)).unwrap_or_else(|e| panic!("{name}: {e}"));
        assert_eq!(image.components.len(), 16, "{name}");
        for component in &image.components {
            assert_eq!((component.width, component.height), (1, 1 << 17), "{name}");
            assert!(component.samples.iter().all(|&s| s == 128), "{name}");
        }
    }
}

/// Within the bound the same shape decodes: `t2skip` on 512 x 256
/// code-blocks with 30000 layers spends about 1.5 x 10^7 steps, and the image
/// is the flat 128 OpenJPEG 2.5.0 decodes.
#[test]
fn a_walk_within_the_work_bound_decodes() {
    assert_flat("t2skip 30000", &t2skip(2048, 1024, 2048, 30000), 2048, 1024);
}