rust-j2k 0.4.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
//! Channel definitions: the Channel Definition box (I.5.3.6, `cdef`).
//!
//! A `cdef` box says, for each output channel, what it is (`Typ`: color,
//! opacity, premultiplied opacity, unspecified) and which color it belongs to
//! (`Asoc`). It describes the channels after the palette, when one applies.
//! OpenJPEG 2.5.0 (`opj_jp2_read_cdef`, `opj_jp2_check_color`,
//! `opj_jp2_apply_cdef`) is the reference, and this module follows it on what
//! is accepted and on the output order:
//!
//! - The box holds `N` (2 bytes), then `N` entries of `Cn`, `Typ`, `Asoc`
//!   (2 bytes each). `N = 0`, or fewer bytes than `N` entries need, is an
//!   error; bytes past the last entry are ignored. A second `cdef` is an
//!   error.
//! - Checked against the channel count after the palette: a `Cn` past the
//!   last channel, an `Asoc` other than 0 and 65535 whose `Asoc - 1` is past
//!   the last channel, or a channel no entry names is an error. An entry may
//!   repeat a channel, as long as every channel is named.
//! - The entries are applied in order. An entry of type color (`Typ 0`)
//!   whose channel is not yet at position `Asoc - 1` swaps it there, and the
//!   later entries follow their channels through the swap. Nothing else
//!   moves a channel. For the usual layouts this puts color `n` at position
//!   `n - 1` and leaves opacity channels where the file put them.
//!
//! Each output channel is labeled with its own entry (the last one that
//! names it), so the label moves with the samples. OpenJPEG keeps only the
//! type, in a field its tools never write out, and can leave it on the wrong
//! channel after a swap; that field is not an output, so it is not followed.
//!
//! [`plan`] reads the box into a [`ChannelPlan`] before any tile is decoded;
//! [`ChannelPlan::apply`] then reorders the channels the palette step built,
//! and [`ChannelPlan::describe`] labels them.

use super::header::Jp2Header;
use super::metadata::{
    Association, ChannelDefinition, ChannelDefinitionEntry, ChannelType, Jp2Metadata,
};
use crate::{Error, Result};

/// The bytes of one `cdef` entry: `Cn`, `Typ`, `Asoc`.
const ENTRY_BYTES: usize = 6;

/// One `cdef` entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Entry {
    /// `Cn`: the channel the entry describes.
    channel: u16,
    /// `Typ`.
    kind: u16,
    /// `Asoc`.
    association: u16,
}

impl Entry {
    /// The color position this entry moves its channel to: `Asoc - 1` for a
    /// color channel associated with a color. `None` when the entry moves
    /// nothing (another type, `Asoc` 0 or 65535).
    fn target(self) -> Option<usize> {
        match (self.kind, self.association) {
            (0, 1..=65534) => Some(usize::from(self.association) - 1),
            _ => None,
        }
    }

    fn definition(self) -> ChannelDefinition {
        ChannelDefinition {
            kind: match self.kind {
                0 => ChannelType::Color,
                1 => ChannelType::Opacity,
                2 => ChannelType::PremultipliedOpacity,
                65535 => ChannelType::Unspecified,
                other => ChannelType::Other(other),
            },
            association: match self.association {
                0 => Association::WholeImage,
                65535 => Association::None,
                n => Association::Color(n),
            },
        }
    }
}

/// Parse a `cdef` payload into its entries. The length is checked before
/// anything is allocated.
fn parse(payload: &[u8]) -> Result<Vec<Entry>> {
    let &[n0, n1, ref body @ ..] = payload else {
        return Err(Error::FileFormat(format!(
            "the `cdef` box holds {} bytes, fewer than its 2-byte count",
            payload.len()
        )));
    };
    let count = u16::from_be_bytes([n0, n1]);
    if count == 0 {
        return Err(Error::FileFormat(
            "the `cdef` box declares 0 channel definitions".into(),
        ));
    }
    let needed = usize::from(count)
        .checked_mul(ENTRY_BYTES)
        .ok_or_else(|| Error::FileFormat("the `cdef` box's size overflows".into()))?;
    let Some(entries) = body.get(..needed) else {
        return Err(Error::FileFormat(format!(
            "the `cdef` box declares {count} channel definitions ({needed} bytes) but holds {}",
            body.len()
        )));
    };
    Ok(entries
        .chunks_exact(ENTRY_BYTES)
        .map(|e| Entry {
            channel: u16::from_be_bytes([e[0], e[1]]),
            kind: u16::from_be_bytes([e[2], e[3]]),
            association: u16::from_be_bytes([e[4], e[5]]),
        })
        .collect())
}

/// The file's `cdef` entries as written, or `None` without a `cdef` box (or
/// with one that does not parse, which planning has already refused).
pub(super) fn definitions(header: &Jp2Header<'_>) -> Option<Vec<ChannelDefinitionEntry>> {
    let payload = header.cdef().first().copied()?;
    let entries = parse(payload).ok()?;
    Some(
        entries
            .into_iter()
            .map(|e| ChannelDefinitionEntry {
                channel: e.channel,
                definition: e.definition(),
            })
            .collect(),
    )
}

/// Check a `cdef` payload's layout only, as OpenJPEG reads the box.
pub(super) fn check_layout(payload: &[u8]) -> Result<()> {
    parse(payload).map(|_| ())
}

/// What the `cdef` box does to the file's channels. Computed before any tile
/// is decoded, so a header-only read can report the output channels too.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ChannelPlan {
    /// No `cdef`, or the caller asked for the raw channels: the channels keep
    /// their order and their default labels.
    None,
    /// The `cdef` applies.
    Apply {
        /// `order[p]` is the input channel that ends up at position `p`.
        order: Vec<usize>,
        /// The label of the channel at each output position.
        definitions: Vec<ChannelDefinition>,
    },
}

/// Read the file's `cdef` box into a [`ChannelPlan`] for `channels` input
/// channels: the codestream's components, or the palette's channels when the
/// palette applies. The box layout is always checked; the entries are checked
/// against `channels` only when they are applied, so a caller asking for the
/// raw channels gets them whatever the entries say, as OpenJPEG does with its
/// "ignore palette and channel definitions" flag.
pub(crate) fn plan(
    header: &Jp2Header<'_>,
    channels: usize,
    raw_channels: bool,
) -> Result<ChannelPlan> {
    let boxes = header.cdef();
    if boxes.len() > 1 {
        return Err(Error::FileFormat(
            "the file has more than one `cdef` box".into(),
        ));
    }
    let Some(payload) = boxes.first() else {
        return Ok(ChannelPlan::None);
    };
    let entries = parse(payload)?;
    if raw_channels {
        return Ok(ChannelPlan::None);
    }
    check(&entries, channels)?;
    Ok(place(&entries, channels))
}

/// Check the entries against the channel count as `opj_jp2_check_color` does:
/// each entry's `Cn` and `Asoc - 1` in range, then every channel named.
fn check(entries: &[Entry], channels: usize) -> Result<()> {
    for (i, entry) in entries.iter().enumerate() {
        if usize::from(entry.channel) >= channels {
            return Err(Error::FileFormat(format!(
                "`cdef` entry {i} describes channel {}, but the file has {channels}",
                entry.channel
            )));
        }
        if !matches!(entry.association, 0 | 65535) && usize::from(entry.association) > channels {
            return Err(Error::FileFormat(format!(
                "`cdef` entry {i} associates channel {} with color {}, but the file has \
                 {channels} channels",
                entry.channel, entry.association
            )));
        }
    }
    let mut named = vec![false; channels];
    for entry in entries {
        // In range: checked above.
        if let Some(slot) = named.get_mut(usize::from(entry.channel)) {
            *slot = true;
        }
    }
    if let Some(missing) = named.iter().position(|&n| !n) {
        return Err(Error::FileFormat(format!(
            "the `cdef` box does not define channel {missing}; it must define every channel"
        )));
    }
    Ok(())
}

/// Run OpenJPEG's placement over checked entries: each color entry swaps its
/// channel to `Asoc - 1`, and later entries follow their channels.
fn place(entries: &[Entry], channels: usize) -> ChannelPlan {
    // `order[p]` is the input channel at position `p`; `position[c]` is where
    // input channel `c` is now.
    let mut order: Vec<usize> = (0..channels).collect();
    let mut position: Vec<usize> = (0..channels).collect();
    // Each input channel's label: the last entry naming it. Every channel is
    // named (checked), so the placeholder never survives.
    let mut labels = vec![
        ChannelDefinition {
            kind: ChannelType::Unspecified,
            association: Association::None,
        };
        channels
    ];
    for entry in entries {
        let channel = usize::from(entry.channel);
        labels[channel] = entry.definition();
        let Some(target) = entry.target() else {
            continue;
        };
        let from = position[channel];
        if from != target {
            let displaced = order[target];
            order.swap(from, target);
            position[channel] = target;
            position[displaced] = from;
        }
    }
    let definitions = order.iter().map(|&c| labels[c]).collect();
    ChannelPlan::Apply { order, definitions }
}

impl ChannelPlan {
    /// Put the channels in their output order: the decoded components, or
    /// their formats for a header-only read.
    pub(crate) fn apply<T>(&self, components: Vec<T>) -> Result<Vec<T>> {
        let ChannelPlan::Apply { order, .. } = self else {
            return Ok(components);
        };
        if order.len() != components.len() {
            return Err(Error::Inconsistent(format!(
                "the `cdef` plan covers {} channels but {} were decoded",
                order.len(),
                components.len()
            )));
        }
        let mut slots: Vec<Option<T>> = components.into_iter().map(Some).collect();
        order
            .iter()
            .map(|&from| {
                slots.get_mut(from).and_then(Option::take).ok_or_else(|| {
                    Error::Inconsistent(format!("`cdef` places channel {from} twice"))
                })
            })
            .collect()
    }

    /// Label the output channels with their definitions.
    pub(crate) fn describe(&self, metadata: &mut Jp2Metadata) {
        if let ChannelPlan::Apply { definitions, .. } = self {
            metadata.channels.clone_from(definitions);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Component;

    fn entries(triples: &[(u16, u16, u16)]) -> Vec<Entry> {
        triples
            .iter()
            .map(|&(channel, kind, association)| Entry {
                channel,
                kind,
                association,
            })
            .collect()
    }

    fn order(triples: &[(u16, u16, u16)], channels: usize) -> Vec<usize> {
        let entries = entries(triples);
        check(&entries, channels).expect("valid entries");
        match place(&entries, channels) {
            ChannelPlan::Apply { order, .. } => order,
            ChannelPlan::None => unreachable!(),
        }
    }

    /// Each order measured with `opj_decompress` 2.5.0 on a three-component
    /// file (see `tests/fixtures/README.md` for the harness).
    #[test]
    fn placement_follows_openjpeg() {
        type Case = (&'static [(u16, u16, u16)], [usize; 3]);
        let cases: &[Case] = &[
            (&[(0, 0, 1), (1, 0, 2), (2, 0, 3)], [0, 1, 2]),
            (&[(0, 0, 3), (1, 0, 2), (2, 0, 1)], [2, 1, 0]),
            (&[(2, 0, 1), (1, 0, 2), (0, 0, 3)], [2, 1, 0]),
            (&[(0, 0, 2), (1, 0, 3), (2, 0, 1)], [2, 0, 1]),
            // Two entries claim color 2: the second swaps the first back.
            (&[(0, 0, 2), (1, 0, 2), (2, 0, 3)], [0, 1, 2]),
            // An opacity channel moves only when a color channel takes its
            // place.
            (&[(1, 1, 0), (0, 0, 2), (2, 0, 3)], [1, 0, 2]),
            (&[(0, 1, 3), (1, 0, 2), (2, 0, 1)], [2, 1, 0]),
            // Asoc 0 and 65535 move nothing, whatever the type.
            (&[(0, 0, 0), (1, 0, 65535), (2, 0, 3)], [0, 1, 2]),
            (&[(0, 7, 1), (1, 0, 2), (2, 0, 3)], [0, 1, 2]),
            (
                &[(0, 65535, 65535), (1, 65535, 65535), (2, 65535, 65535)],
                [0, 1, 2],
            ),
            // A repeated channel is fine once every channel is named.
            (&[(0, 0, 1), (1, 0, 2), (2, 0, 3), (2, 1, 0)], [0, 1, 2]),
        ];
        for (triples, want) in cases {
            assert_eq!(order(triples, 3), want, "{triples:?}");
        }
    }

    /// Opacity first (an ARGB layout): every color entry swaps, and the
    /// opacity channel is pushed to the end. Its label goes with it.
    #[test]
    fn an_opacity_channel_pushed_along_keeps_its_label() {
        let entries = entries(&[(0, 1, 0), (1, 0, 1), (2, 0, 2), (3, 0, 3)]);
        check(&entries, 4).unwrap();
        let ChannelPlan::Apply { order, definitions } = place(&entries, 4) else {
            unreachable!()
        };
        assert_eq!(order, [1, 2, 3, 0]);
        let color = |n| ChannelDefinition {
            kind: ChannelType::Color,
            association: Association::Color(n),
        };
        assert_eq!(
            definitions,
            [
                color(1),
                color(2),
                color(3),
                ChannelDefinition {
                    kind: ChannelType::Opacity,
                    association: Association::WholeImage,
                },
            ]
        );
    }

    #[test]
    fn the_last_entry_naming_a_channel_labels_it() {
        let entries = entries(&[(0, 0, 1), (0, 2, 0)]);
        let ChannelPlan::Apply { definitions, .. } = place(&entries, 1) else {
            unreachable!()
        };
        assert_eq!(definitions[0].kind, ChannelType::PremultipliedOpacity);
        assert_eq!(definitions[0].association, Association::WholeImage);
    }

    #[test]
    fn types_and_associations_map_to_their_variants() {
        let label = |kind, association| {
            Entry {
                channel: 0,
                kind,
                association,
            }
            .definition()
        };
        assert_eq!(label(0, 2).kind, ChannelType::Color);
        assert_eq!(label(1, 0).kind, ChannelType::Opacity);
        assert_eq!(label(2, 0).kind, ChannelType::PremultipliedOpacity);
        assert_eq!(label(65535, 0).kind, ChannelType::Unspecified);
        assert_eq!(label(3, 0).kind, ChannelType::Other(3));
        assert_eq!(label(65534, 0).kind, ChannelType::Other(65534));
        assert_eq!(label(0, 0).association, Association::WholeImage);
        assert_eq!(label(0, 65535).association, Association::None);
        assert_eq!(label(0, 7).association, Association::Color(7));
    }

    #[test]
    fn apply_moves_each_channel_once() {
        let plan = ChannelPlan::Apply {
            order: vec![2, 0, 1],
            definitions: vec![],
        };
        let plane = |v| Component::new(1, 1, 8, false, 1, 1, vec![v]);
        let out = plan.apply(vec![plane(10), plane(11), plane(12)]).unwrap();
        let samples: Vec<i32> = out.iter().map(|c| c.samples[0]).collect();
        assert_eq!(samples, [12, 10, 11]);
        assert!(matches!(
            plan.apply(vec![plane(10), plane(11)]),
            Err(Error::Inconsistent(_))
        ));
    }

    #[test]
    fn parse_reads_entries_and_ignores_trailing_bytes() {
        let payload = [0, 1, 0, 2, 0, 1, 0, 0, 0xFF, 0xFF];
        assert_eq!(parse(&payload).unwrap(), entries(&[(2, 1, 0)]));
    }
}