pdfcer-image-codec 0.160.0

pdfcer terminal image codecs: DCTDecode, CCITTFaxDecode, JBIG2Decode and JPXDecode (ISO 32000-1 7.4.6-7.4.9). Re-exported by pdfcer-core as `image_codec`.
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
//! # Synthetic JPEG 2000 codestreams for the JPXDecode adapter's tests
//!
//! **GENERATED FILE — do not hand-edit.** Regenerate with:
//!
//! ```text
//! python tools/gen-jpx-fixtures.py
//! ```
//!
//! ## Provenance (docs/LEGAL.md §5)
//!
//! Every byte array below was produced on a developer machine by
//! `tools/gen-jpx-fixtures.py` from pixel patterns **authored for this
//! project**. Nothing here was downloaded. The obvious sources — the
//! OpenJPEG conformance suite, pdf.js's JPX regression files, the JPEG
//! committee's own test images — are third-party files of unknown or
//! restricted provenance, which `LEGAL.md` §5 forbids outright.
//! Generating them is not a convenience, it is the only permitted route.
//!
//! ## Why OpenJPEG encodes what `hayro-jpeg2000` decodes
//!
//! The fixtures are written by **OpenJPEG** (through Pillow's JPEG2000
//! plugin) and read back by **`hayro-jpeg2000`**, two implementations
//! that share no code. That is the strongest cross-check available short
//! of running a second decoder: a fixture that one project writes and
//! the other reads to the expected pixels has exercised T.800 itself,
//! not one project's reading of it.
//!
//! Every fixture is encoded with the reversible 5/3 wavelet and no
//! quantization (`irreversible=False, quality_mode='lossless'`), and the
//! generator refuses to emit a fixture whose OpenJPEG round trip is not
//! bit-exact. So the `*_SAMPLES` constants are derived from the **source
//! pixels**, not from whatever the codec happened to produce — the
//! difference between an assertion and a recording.
//!
//! ## Why byte arrays rather than files
//!
//! Hermetic tests, exactly as in the sibling `fixtures` and
//! `fixtures_bilevel` modules: embedding the bytes means the tests run
//! identically from any working directory, under `cargo test`, under
//! `cargo fuzz`, and in a `wasm32` check.

// Shared by TWO test suites — `pdfcer-core`'s codec tests and
// `pdfcer-render`'s rasterizer tests (which pull this file in with
// `#[path]`) — and neither uses the whole set.
#![allow(dead_code)]

/// The expected decoded samples for [`JPX_GRAY_8_JP2`] and
/// [`JPX_GRAY_8_J2K`]: 16 x 4, one 8-bit component, one byte per
/// sample.
///
/// A single-component 8-bit image has an identical layout in every
/// filter pdfcer implements, so this constant is also the check that
/// the JPX adapter did not accidentally introduce padding, a stride,
/// or a row flip that the other codecs do not have.
pub const JPX_GRAY_8_SAMPLES: &[u8] = &[
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
    0x80, 0x00, 0x80, 0xFF, 0x80, 0x00, 0x80, 0xFF, 0x80, 0x00, 0x80, 0xFF, 0x80, 0x00, 0x80, 0xFF,
];

/// 16 x 4 grayscale, 8 bits per component, in a **JP2 box
/// container** — the shape §7.4.9 describes when it says the filter
/// "shall expect to read a full JPX file structure".
///
/// Encoded losslessly by OpenJPEG through Pillow; see
/// `tools/gen-jpx-fixtures.py`. Must decode to
/// [`JPX_GRAY_8_SAMPLES`].
pub const JPX_GRAY_8_JP2: &[u8] = &[
    0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A, 0x00, 0x00, 0x00, 0x14,
    0x66, 0x74, 0x79, 0x70, 0x6A, 0x70, 0x32, 0x20, 0x00, 0x00, 0x00, 0x00, 0x6A, 0x70, 0x32, 0x20,
    0x00, 0x00, 0x00, 0x2D, 0x6A, 0x70, 0x32, 0x68, 0x00, 0x00, 0x00, 0x16, 0x69, 0x68, 0x64, 0x72,
    0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x0F, 0x63, 0x6F, 0x6C, 0x72, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
    0xDB, 0x6A, 0x70, 0x32, 0x63, 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07,
    0x01, 0x01, 0xFF, 0x52, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x04, 0x04, 0x00, 0x01,
    0xFF, 0x5C, 0x00, 0x0A, 0x40, 0x40, 0x48, 0x48, 0x50, 0x48, 0x48, 0x50, 0xFF, 0x64, 0x00, 0x25,
    0x00, 0x01, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x4F, 0x70, 0x65,
    0x6E, 0x4A, 0x50, 0x45, 0x47, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x32, 0x2E,
    0x35, 0x2E, 0x34, 0xFF, 0x90, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x00, 0x01, 0xFF,
    0x93, 0xC3, 0xE7, 0x06, 0x06, 0xCE, 0x4E, 0xC0, 0xF9, 0x02, 0x43, 0xEA, 0x05, 0x87, 0xDA, 0x0A,
    0x0B, 0x13, 0xAA, 0xDF, 0x09, 0xD6, 0xD6, 0x6C, 0xCF, 0x0B, 0x5F, 0x09, 0x66, 0x89, 0xC7, 0xDA,
    0x17, 0x7E, 0x62, 0x71, 0xF8, 0x08, 0x80, 0x18, 0x34, 0x44, 0x34, 0xE5, 0x58, 0xFA, 0x30, 0xD1,
    0x3D, 0xE7, 0x05, 0x39, 0xE2, 0xFB, 0x16, 0x96, 0xF7, 0xFB, 0x7C, 0x82, 0x8B, 0x09, 0x0C, 0x7F,
    0x6D, 0x51, 0x0B, 0x8A, 0x7F, 0x0C, 0x1E, 0x6E, 0xA2, 0x8E, 0x0C, 0x6A, 0x3A, 0xFC, 0xE8, 0x52,
    0x71, 0xEB, 0x8C, 0x71, 0x54, 0xE7, 0xFF, 0xD9,
];

/// The **same picture** as [`JPX_GRAY_8_JP2`], as a **raw
/// codestream** (SOC + SIZ, `FF 4F FF 51`) with no JP2 boxes at all.
///
/// §7.4.9 describes the box-container shape, but real producers embed
/// bare codestreams and `hayro-jpeg2000` accepts both by sniffing the
/// first bytes. The two fixtures must decode to the identical
/// [`JPX_GRAY_8_SAMPLES`], which is what proves pdfcer reads geometry
/// and colour from the codestream rather than from the container.
///
/// Note that a raw codestream carries NO colour specification box, so
/// the colour model here comes from §7.4.9's terminal fallback —
/// 1 channel means `DeviceGray`.
pub const JPX_GRAY_8_J2K: &[u8] = &[
    0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x04,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x04,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x01, 0xFF, 0x52, 0x00,
    0x0C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x04, 0x04, 0x00, 0x01, 0xFF, 0x5C, 0x00, 0x0A, 0x40,
    0x40, 0x48, 0x48, 0x50, 0x48, 0x48, 0x50, 0xFF, 0x64, 0x00, 0x25, 0x00, 0x01, 0x43, 0x72, 0x65,
    0x61, 0x74, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x4F, 0x70, 0x65, 0x6E, 0x4A, 0x50, 0x45, 0x47,
    0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x32, 0x2E, 0x35, 0x2E, 0x34, 0xFF, 0x90,
    0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x00, 0x01, 0xFF, 0x93, 0xC3, 0xE7, 0x06, 0x06,
    0xCE, 0x4E, 0xC0, 0xF9, 0x02, 0x43, 0xEA, 0x05, 0x87, 0xDA, 0x0A, 0x0B, 0x13, 0xAA, 0xDF, 0x09,
    0xD6, 0xD6, 0x6C, 0xCF, 0x0B, 0x5F, 0x09, 0x66, 0x89, 0xC7, 0xDA, 0x17, 0x7E, 0x62, 0x71, 0xF8,
    0x08, 0x80, 0x18, 0x34, 0x44, 0x34, 0xE5, 0x58, 0xFA, 0x30, 0xD1, 0x3D, 0xE7, 0x05, 0x39, 0xE2,
    0xFB, 0x16, 0x96, 0xF7, 0xFB, 0x7C, 0x82, 0x8B, 0x09, 0x0C, 0x7F, 0x6D, 0x51, 0x0B, 0x8A, 0x7F,
    0x0C, 0x1E, 0x6E, 0xA2, 0x8E, 0x0C, 0x6A, 0x3A, 0xFC, 0xE8, 0x52, 0x71, 0xEB, 0x8C, 0x71, 0x54,
    0xE7, 0xFF, 0xD9,
];

/// The expected decoded samples for [`JPX_RGB_8_JP2`]: 4 x 2, three
/// 8-bit components, **interleaved** R,G,B per pixel (§8.9.3).
///
/// `hayro-jpeg2000` returns one planar buffer per component; pdfcer
/// interleaves them. Primaries, secondaries, white and black are all
/// present and no two pixels share a component triple, so a channel
/// swap or an interleave off-by-one cannot produce these bytes by
/// accident.
pub const JPX_RGB_8_SAMPLES: &[u8] = &[
    0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
    0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
];

/// 4 x 2 sRGB, 8 bits per component, JP2 box container with an
/// enumerated `colr` box (value 16, sRGB).
///
/// Must decode to [`JPX_RGB_8_SAMPLES`].
pub const JPX_RGB_8_JP2: &[u8] = &[
    0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A, 0x00, 0x00, 0x00, 0x14,
    0x66, 0x74, 0x79, 0x70, 0x6A, 0x70, 0x32, 0x20, 0x00, 0x00, 0x00, 0x00, 0x6A, 0x70, 0x32, 0x20,
    0x00, 0x00, 0x00, 0x2D, 0x6A, 0x70, 0x32, 0x68, 0x00, 0x00, 0x00, 0x16, 0x69, 0x68, 0x64, 0x72,
    0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x03, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x0F, 0x63, 0x6F, 0x6C, 0x72, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
    0xA7, 0x6A, 0x70, 0x32, 0x63, 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07,
    0x01, 0x01, 0x07, 0x01, 0x01, 0x07, 0x01, 0x01, 0xFF, 0x52, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x01,
    0x00, 0x01, 0x04, 0x04, 0x00, 0x01, 0xFF, 0x5C, 0x00, 0x07, 0x40, 0x40, 0x48, 0x48, 0x50, 0xFF,
    0x64, 0x00, 0x25, 0x00, 0x01, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20,
    0x4F, 0x70, 0x65, 0x6E, 0x4A, 0x50, 0x45, 0x47, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E,
    0x20, 0x32, 0x2E, 0x35, 0x2E, 0x34, 0xFF, 0x90, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2C,
    0x00, 0x01, 0xFF, 0x93, 0x80, 0x80, 0x80, 0xA7, 0xE0, 0x05, 0x3F, 0x30, 0x30, 0x0A, 0x71, 0x0B,
    0xA4, 0x7F, 0x93, 0xF3, 0x03, 0x08, 0x63, 0x7F, 0xAF, 0xCC, 0x0A, 0x3F, 0x00, 0x20, 0x02, 0xEF,
    0x04, 0x27, 0xFF, 0xD9,
];

/// The expected **colour** samples for [`JPX_RGBA_8_JP2`] — three
/// components per pixel, with the opacity channel already lifted out.
///
/// This is the constant that catches an adapter which left the
/// opacity channel interleaved: the colours would all still be
/// present, merely shifted one component to the right, which is
/// exactly the kind of failure that looks plausible in a thumbnail.
pub const JPX_RGBA_8_SAMPLES: &[u8] = &[
    0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
    0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
];

/// The expected **opacity** samples for [`JPX_RGBA_8_JP2`] — one
/// 8-bit component per pixel, the shape
/// `CodedImage::embedded_alpha` carries.
///
/// Only reachable with `/SMaskInData 1`. Table 89's default is **0**,
/// which means "encoded soft-mask image information shall be
/// ignored" — so a decoder that always hands back the alpha it found
/// is wrong, and the pair of tests over this fixture pins both
/// directions.
pub const JPX_RGBA_8_ALPHA: &[u8] = &[0xFF, 0xAA, 0x55, 0x00, 0x00, 0x55, 0xAA, 0xFF];

/// 4 x 2 sRGB **plus an opacity channel**, JP2 box container.
///
/// The `cdef` box (JP2 I.5.3.6) declares channel 3 as type 1,
/// "opacity" — the generator asserts that, because without the box
/// the decoder would see four ordinary colour channels and report no
/// alpha, making the fixture silently inert. §7.4.9's channel model
/// calls this an *ordinary* opacity channel, which is the one
/// `/SMaskInData 1` selects; the *premultiplied* type that
/// `/SMaskInData 2` describes is a different channel type that
/// `hayro-jpeg2000` does not parse (see `jpx.rs`).
///
/// Alpha runs 255, 170, 85, 0, 0, 85, 170, 255 — both extremes and
/// two intermediate values, so a wrong scale or an inverted channel
/// cannot pass.
pub const JPX_RGBA_8_JP2: &[u8] = &[
    0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A, 0x00, 0x00, 0x00, 0x14,
    0x66, 0x74, 0x79, 0x70, 0x6A, 0x70, 0x32, 0x20, 0x00, 0x00, 0x00, 0x00, 0x6A, 0x70, 0x32, 0x20,
    0x00, 0x00, 0x00, 0x4F, 0x6A, 0x70, 0x32, 0x68, 0x00, 0x00, 0x00, 0x16, 0x69, 0x68, 0x64, 0x72,
    0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x0F, 0x63, 0x6F, 0x6C, 0x72, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
    0x22, 0x63, 0x64, 0x65, 0x66, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
    0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
    0x00, 0x00, 0xB8, 0x6A, 0x70, 0x32, 0x63, 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x32, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x04, 0x07, 0x01, 0x01, 0x07, 0x01, 0x01, 0x07, 0x01, 0x01, 0x07, 0x01, 0x01, 0xFF, 0x52, 0x00,
    0x0C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x04, 0x04, 0x00, 0x01, 0xFF, 0x5C, 0x00, 0x07, 0x40,
    0x40, 0x48, 0x48, 0x50, 0xFF, 0x64, 0x00, 0x25, 0x00, 0x01, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65,
    0x64, 0x20, 0x62, 0x79, 0x20, 0x4F, 0x70, 0x65, 0x6E, 0x4A, 0x50, 0x45, 0x47, 0x20, 0x76, 0x65,
    0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x32, 0x2E, 0x35, 0x2E, 0x34, 0xFF, 0x90, 0x00, 0x0A, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x3A, 0x00, 0x01, 0xFF, 0x93, 0x80, 0x80, 0x80, 0x80, 0xA7, 0xE0, 0x05,
    0x3F, 0x30, 0x30, 0x0A, 0x71, 0x0B, 0xA4, 0x7F, 0x93, 0xF3, 0x03, 0x08, 0x63, 0x7F, 0xAF, 0xCC,
    0x0A, 0x3F, 0x00, 0x20, 0x02, 0xEF, 0x04, 0x27, 0xA7, 0xE0, 0x09, 0x1F, 0x80, 0x18, 0x09, 0x69,
    0x74, 0x7F, 0x0B, 0x65, 0x7F, 0xFF, 0xD9,
];

/// The expected decoded samples for [`JPX_GRAY_16_JP2`] **after
/// pdfcer's 16 -> 8 bit normalization**.
///
/// pdfcer delivers JPX samples at 8 bits per component, range-scaled
/// `round(v / (2^d - 1) * 255)`. The fixture's 0x00FF pixel is the
/// discriminator: full-range scaling gives **1**, a high-byte
/// truncation would give **0**. Table 89 makes the bit depth
/// "determined by the conforming reader in the process of
/// decoding", so 8 is a conforming choice — but only the full-range
/// scale maps the codestream's white point onto 255 for every depth.
pub const JPX_GRAY_16_SAMPLES: &[u8] = &[0x00, 0xFF, 0x80, 0x01, 0x01, 0xFE, 0x00, 0xAA];

/// 4 x 2 grayscale at **16 bits per component** — the fixture that
/// exercises §7.4.9's "any value from 1 to 38 shall be allowed".
///
/// Must decode to [`JPX_GRAY_16_SAMPLES`], i.e. to 8-bit samples, and
/// `CodedImage::bits_per_component` must report **8** rather than 16:
/// the field describes the samples pdfcer delivers, not the depth the
/// codestream stored them at.
pub const JPX_GRAY_16_JP2: &[u8] = &[
    0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A, 0x00, 0x00, 0x00, 0x14,
    0x66, 0x74, 0x79, 0x70, 0x6A, 0x70, 0x32, 0x20, 0x00, 0x00, 0x00, 0x00, 0x6A, 0x70, 0x32, 0x20,
    0x00, 0x00, 0x00, 0x2D, 0x6A, 0x70, 0x32, 0x68, 0x00, 0x00, 0x00, 0x16, 0x69, 0x68, 0x64, 0x72,
    0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x0F, 0x63, 0x6F, 0x6C, 0x72, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
    0xA8, 0x6A, 0x70, 0x32, 0x63, 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0F,
    0x01, 0x01, 0xFF, 0x52, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x04, 0x04, 0x00, 0x01,
    0xFF, 0x5C, 0x00, 0x07, 0x40, 0x80, 0x88, 0x88, 0x90, 0xFF, 0x64, 0x00, 0x25, 0x00, 0x01, 0x43,
    0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x4F, 0x70, 0x65, 0x6E, 0x4A, 0x50,
    0x45, 0x47, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x20, 0x32, 0x2E, 0x35, 0x2E, 0x34,
    0xFF, 0x90, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x00, 0x01, 0xFF, 0x93, 0xC1, 0xFE,
    0x01, 0x40, 0x06, 0x3E, 0xA9, 0x20, 0xBF, 0xCF, 0xFC, 0x48, 0x16, 0x1F, 0xF8, 0x30, 0x2C, 0xFF,
    0x63, 0x00, 0xC0, 0x03, 0x9E, 0x0D, 0xAD, 0x63, 0x01, 0x1A, 0x31, 0xCD, 0xAF, 0x0B, 0x76, 0x54,
    0xE8, 0xEF, 0xBF, 0xFF, 0xD9,
];

/// The expected decoded samples for [`JPX_CMYK_8_JP2`]: 4 x 2, four
/// 8-bit components interleaved C,M,Y,K.
///
/// Raw ink values, with no `/Decode` applied and no inversion of any
/// kind (rules R26/R29) — the same contract the DCT adapter honours
/// for its CMYK output.
pub const JPX_CMYK_8_SAMPLES: &[u8] = &[
    0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF,
    0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x40, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00,
];

/// 4 x 2 CMYK, JP2 box container with **enumerated colour space 12**.
///
/// §7.4.9 singles this value out: enumerated colour space 12 (CMYK)
/// "is part of JPX but not JPX baseline" and "shall be supported
/// in a PDF" regardless. The generator asserts the `colr` box really
/// says 12, so the fixture cannot degrade into a four-channel
/// `Unknown` image without the test noticing.
pub const JPX_CMYK_8_JP2: &[u8] = &[
    0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A, 0x00, 0x00, 0x00, 0x14,
    0x66, 0x74, 0x79, 0x70, 0x6A, 0x70, 0x32, 0x20, 0x00, 0x00, 0x00, 0x00, 0x6A, 0x70, 0x32, 0x20,
    0x00, 0x00, 0x00, 0x2D, 0x6A, 0x70, 0x32, 0x68, 0x00, 0x00, 0x00, 0x16, 0x69, 0x68, 0x64, 0x72,
    0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x0F, 0x63, 0x6F, 0x6C, 0x72, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00,
    0xE5, 0x6A, 0x70, 0x32, 0x63, 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x07,
    0x01, 0x01, 0x07, 0x01, 0x01, 0x07, 0x01, 0x01, 0x07, 0x01, 0x01, 0xFF, 0x52, 0x00, 0x0C, 0x00,
    0x00, 0x00, 0x01, 0x00, 0x01, 0x04, 0x04, 0x00, 0x01, 0xFF, 0x5C, 0x00, 0x07, 0x40, 0x40, 0x48,
    0x48, 0x50, 0xFF, 0x64, 0x00, 0x25, 0x00, 0x01, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20,
    0x62, 0x79, 0x20, 0x4F, 0x70, 0x65, 0x6E, 0x4A, 0x50, 0x45, 0x47, 0x20, 0x76, 0x65, 0x72, 0x73,
    0x69, 0x6F, 0x6E, 0x20, 0x32, 0x2E, 0x35, 0x2E, 0x34, 0xFF, 0x90, 0x00, 0x0A, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x67, 0x00, 0x01, 0xFF, 0x93, 0xCF, 0xB4, 0x0C, 0x0B, 0xA1, 0x6F, 0xCF, 0xB4, 0x0C,
    0x02, 0xE3, 0x5F, 0xCF, 0xB4, 0x0C, 0x06, 0x30, 0x67, 0xDF, 0x80, 0x10, 0x07, 0x03, 0xCF, 0xC0,
    0x0E, 0x3E, 0xD0, 0x38, 0xFC, 0x00, 0x80, 0x05, 0xF9, 0x43, 0x0A, 0x7B, 0x4F, 0x0B, 0xEA, 0xCF,
    0xC0, 0x0E, 0x7E, 0x00, 0x71, 0xF8, 0x01, 0x80, 0x02, 0xF8, 0x61, 0x04, 0x42, 0x37, 0x08, 0xF5,
    0x7B, 0xCF, 0xC0, 0x0A, 0x7E, 0x00, 0x73, 0xF3, 0x03, 0x0B, 0xF4, 0x05, 0x14, 0x7F, 0x04, 0x4C,
    0xF7, 0xC7, 0xDA, 0x05, 0x0F, 0xA8, 0x0A, 0x7E, 0x60, 0x40, 0x0B, 0x8B, 0x0B, 0x87, 0x0C, 0x47,
    0xFF, 0xD9,
];

/// [`JPX_GRAY_8_JP2`] with a **palette**: a `pclr` box of one RGB entry and a
/// `cmap` box routing three output channels through it.
///
/// # What this fixture is for
///
/// It is the only shape in which a JPEG 2000 image and its PDF colour space
/// can each carry a lookup table for the same samples — and applying **both**
/// is the one way to render it wrong.
///
/// A file built to catch that ships a JP2 whose `pclr` entry and whose PDF
/// `/Indexed` lookup hold **identical bytes**. Resolve the palette in the
/// codec and the samples become colours; the renderer then reads a colour as
/// an index, asks a one-entry table for entry 114, and paints black. Apply
/// either lookup alone and the answer is correct.
///
/// ISO 32000-1 §8.9 Table 89 decides which: with `JPXDecode`, if
/// `/ColorSpace` is present then "colour space specifications in the JPEG2000
/// data shall be ignored". A `pclr`/`cmap` pair is such a specification.
///
/// # Construction
///
/// Built from [`JPX_GRAY_8_JP2`] rather than hand-authored, so the codestream
/// is byte-identical to the one the other tests use and **only the container
/// differs** — if a decode difference appears, it cannot be the image data.
///
/// The two boxes are spliced into `jp2h` and its length is repaired:
/// 45 + 17 (`pclr`) + 20 (`cmap`) = **82**, which is exactly the arithmetic a
/// real palette-bearing file shows.
/// # Why 256 entries and not one
///
/// A real file built to trap the double-resolution bug can ship a **single**
/// palette entry, because its codestream is a flat field of zeroes and index
/// 0 is the only one ever used. This fixture reuses [`JPX_GRAY_8_JP2`]'s
/// codestream, whose samples deliberately span `0x00..=0xFF` — so a one-entry
/// table makes almost every index out of range and the decoder refuses the
/// file outright (`failed to resolve palette indices`).
///
/// That refusal is CORRECT and was hit while writing this fixture. It is
/// recorded because it looks like the bug under test: "palette not applied"
/// is both the fix's intended behaviour and this failure's symptom, and the
/// two are told apart only by the error. A full table keeps the fixture
/// exercising the branch rather than the guard.
///
/// Entry `i` is `[i, 255 - i, 13]`. The constant third channel is a
/// fingerprint: if a resolved sample's blue is not 13, the palette was not
/// what produced it.
#[must_use]
pub fn jpx_gray_8_jp2_with_palette() -> Vec<u8> {
    const NE: usize = 256;
    // `pclr` (ISO/IEC 15444-1 I.5.3.4): NE entries, NPC=3 components, each
    // 8-bit (the depth byte is `bits - 1`, high bit clear for unsigned).
    let body_len = 2 + 1 + 3 + NE * 3;
    let mut pclr = Vec::with_capacity(8 + body_len);
    pclr.extend_from_slice(
        &u32::try_from(8 + body_len)
            .unwrap_or(u32::MAX)
            .to_be_bytes(),
    );
    pclr.extend_from_slice(b"pclr");
    pclr.extend_from_slice(&u16::try_from(NE).unwrap_or(u16::MAX).to_be_bytes()); // NE
    pclr.push(3); // NPC
    pclr.extend_from_slice(&[7, 7, 7]); // Bi: 8-bit unsigned x3
    for i in 0..NE {
        let i = u8::try_from(i).unwrap_or(u8::MAX);
        pclr.extend_from_slice(&[i, 255 - i, 13]);
    }

    // `cmap` (I.5.3.5): three output channels, all from component 0, each
    // taking a different palette column.
    let mut cmap = vec![0u8, 0u8, 0u8, 20u8, b'c', b'm', b'a', b'p'];
    for col in 0..3u8 {
        cmap.extend_from_slice(&0u16.to_be_bytes()); // CMP: component 0
        cmap.push(1); // MTYP 1 = palette mapping
        cmap.push(col); // PCOL
    }

    let src = JPX_GRAY_8_JP2;

    // TOTAL, NOT PANICKING, and that is this crate's lint policy rather
    // than defensiveness for its own sake: `fixtures_jpx` is a `pub` module
    // compiled into the library, so `expect` and raw slicing are denied here
    // exactly as they are in the parser. A fixture builder that can panic is
    // a panic reachable from a dependent.
    //
    // If the grey fixture ever stops containing a `jp2h` box, this returns it
    // UNCHANGED — and the palette tests then fail on the flag they assert
    // rather than crashing, which points at the fixture instead of at the
    // decoder.
    let Some((start, len)) = find_box(src, b"jp2h") else {
        return src.to_vec();
    };
    let (Some(head), Some(inner), Some(tail)) = (
        src.get(..start),
        src.get(start + 8..start + len),
        src.get(start + len..),
    ) else {
        return src.to_vec();
    };

    let mut out = Vec::with_capacity(src.len() + pclr.len() + cmap.len());
    out.extend_from_slice(head);
    // The repaired `jp2h` header, then its original contents, then the two
    // new boxes appended INSIDE it.
    let new_len = u32::try_from(len + pclr.len() + cmap.len()).unwrap_or(u32::MAX);
    out.extend_from_slice(&new_len.to_be_bytes());
    out.extend_from_slice(b"jp2h");
    out.extend_from_slice(inner);
    out.extend_from_slice(&pclr);
    out.extend_from_slice(&cmap);
    out.extend_from_slice(tail);
    out
}

/// Byte offset and length of the first top-level box with `tag`.
fn find_box(buf: &[u8], tag: &[u8; 4]) -> Option<(usize, usize)> {
    let mut i = 0usize;
    while i + 8 <= buf.len() {
        let len = u32::from_be_bytes(buf.get(i..i + 4)?.try_into().ok()?) as usize;
        if buf.get(i + 4..i + 8)? == tag {
            return Some((i, len));
        }
        if len == 0 {
            return None;
        }
        i = i.checked_add(len)?;
    }
    None
}