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
//! A packet cut short under `strict` off, at the code-block level: each
//! code-block holds whole layers, those the data gives it in full.

use std::borrow::Cow;

use super::{CodeBlock, CodedData, decode_packets_partial};
use crate::codestream::{MainHeader, Tile};
use crate::synth::{self, seg};

/// Every code-block of `coded`, in walk order, with the index of the
/// (component, resolution) it is in.
fn blocks<'a>(coded: &'a CodedData<'a>) -> Vec<(usize, &'a CodeBlock<'a>)> {
    coded
        .components
        .iter()
        .flat_map(|c| &c.resolutions)
        .enumerate()
        .flat_map(|(at, r)| {
            r.subbands
                .iter()
                .flat_map(move |b| b.blocks.iter().map(move |k| (at, k)))
        })
        .collect()
}

fn walk<'a>(header: &MainHeader, tile: &'a Tile<'a>) -> CodedData<'a> {
    let meter = super::Meter::new(u64::MAX);
    decode_packets_partial(header, tile, &meter, u64::MAX, 0, None)
        .unwrap_or_else(|_| panic!("the walk fails"))
        .0
}

/// `multilayer_lossless.j2k` (three layers, LRCP) cut at every byte of its
/// tile data. Each code-block is what a decode of the first `k` layers of
/// the whole file gives it, for some `k`: a cut packet adds the code-blocks
/// whose data is all there and leaves the rest at the layers before (B.11),
/// where OpenJPEG drops those layers too. Some cut leaves code-blocks of one
/// resolution, which is one precinct and so one packet a layer, at two layer
/// counts, the lower at least 1.
#[test]
fn a_cut_packet_keeps_each_code_blocks_whole_layers() {
    assert!(
        whole_layers("multilayer_lossless.j2k", 3),
        "no cut kept a block at an earlier layer beside a whole one"
    );
}

/// The same for HT code-blocks: `ds0_ht_02_b11` (six layers, LRCP), whose
/// blocks' first contributions are placeholder passes, with the cleanup pass
/// and refinement passes in later layers. A cut HT segment is undone as a
/// Part 1 one is.
#[test]
fn a_cut_packet_keeps_whole_layers_of_ht_code_blocks() {
    assert!(
        whole_layers("conformance/ht/codestreams/ds0_ht_02_b11.j2k", 6),
        "no cut kept a block at an earlier layer beside a whole one"
    );
}

/// The same under the code-block styles that end segments within a block's
/// passes (bypass, restart) or not (the others), two layers each: a block
/// whose layer 2 continues its layer 1 segment and opens another keeps
/// neither when the second runs out.
#[test]
fn a_cut_packet_keeps_whole_layers_under_each_code_block_style() {
    for name in [
        "cblksty_bypass_lossless.j2k",
        "cblksty_restart_lossless.j2k",
        "cblksty_restart_pterm_segsym_lossless.j2k",
        "cblksty_pterm_lossless.j2k",
        "cblksty_reset_lossless.j2k",
    ] {
        whole_layers(name, 2);
    }
}

/// The same under `predictable termination`, on the layer-cap fixtures:
/// `pterm_cut` and `bypass_pterm`, whose cut prefixes OpenJPEG decodes to
/// other images (see `tests/max_quality_layers.rs`), and `precincts_pterm`,
/// LRCP with several precincts in each resolution, so a block's cut flag
/// comes from its own precinct's packets.
#[test]
fn a_cut_packet_keeps_whole_layers_under_predictable_termination() {
    for (name, layers) in [
        ("layers/pterm_cut.j2k", 2),
        ("layers/bypass_pterm.j2k", 4),
        ("layers/precincts_pterm.j2k", 3),
    ] {
        whole_layers(name, layers);
    }
}

/// Cut the tile data of fixture `name`, of one tile and `layers` layers, at
/// every byte, and check each code-block is what a decode of the first `k`
/// layers of the whole file gives it, for some `k`. Whether some cut left
/// code-blocks of one resolution at two layer counts, the lower at least 1.
///
/// The blocks are compared but for [`CodeBlock::cut`], which is checked on
/// its own: a block that lacks a layer the whole file gives it has a later
/// packet of its precinct missing, so its one segment is cut, under a style
/// that does not end segments within a block's passes. Under `restart` each
/// segment is one pass and whole, so none is cut. `bypass` and HT end some
/// segments (an HT cleanup segment holds every pass it can), so neither is
/// checked here.
fn whole_layers(name: &str, layers: u16) -> bool {
    let bytes = std::fs::read(format!(
        "{}/tests/fixtures/{name}",
        env!("CARGO_MANIFEST_DIR")
    ))
    .unwrap();
    let parsed = crate::codestream::parse_header(&bytes).unwrap();
    let cs = crate::codestream::parse_tiles(&bytes, parsed).unwrap();
    assert_eq!(cs.tiles.len(), 1, "{name}");
    let header = cs.tile_header(0).unwrap().into_owned();
    let tile = &cs.tiles[0];
    assert_eq!(header.cod.layers, layers, "{name}");
    let style = header.components[0].coding.code_block_style;
    let restart = style & crate::codestream::markers::code_block_style::TERMALL != 0;
    let one_segment = style
        & (crate::codestream::markers::code_block_style::TERMALL
            | crate::codestream::markers::code_block_style::LAZY
            | crate::codestream::markers::code_block_style::HT)
        == 0;
    let mut cuts = 0;

    // The first k layers of the whole file, for k from 0 to 3: the same data
    // under a COD that declares k layers, its packets past them left over.
    let limited: Vec<CodedData<'_>> = (0..=layers)
        .map(|k| {
            let mut header = header.clone();
            header.cod.layers = k.max(1);
            if k == 0 {
                let empty = Tile {
                    data: Cow::Borrowed(&[]),
                    part_ends: vec![0],
                    ..tile.clone()
                };
                // A tile with no data: every block empty, borrowing nothing.
                let coded = walk(&header, &empty);
                return strip(coded);
            }
            walk(&header, tile)
        })
        .collect();
    let limited: Vec<Vec<(usize, &CodeBlock<'_>)>> = limited.iter().map(blocks).collect();

    let mut split = false;
    for cut in 0..tile.data.len() {
        let short = Tile {
            data: Cow::Borrowed(&tile.data[..cut]),
            part_ends: vec![cut],
            ..tile.clone()
        };
        let coded = walk(&header, &short);
        let got = blocks(&coded);
        // For each block, its resolution and the layer counts it matches.
        let mut matched: Vec<(usize, Vec<u16>)> = Vec::new();
        for (b, (at, block)) in got.iter().enumerate() {
            let uncut = CodeBlock {
                cut: false,
                missing_from: None,
                ..(*block).clone()
            };
            let ks: Vec<u16> = (0..=layers)
                .filter(|&k| {
                    // Where the data ends, not which layers a block holds.
                    let layer = CodeBlock {
                        missing_from: None,
                        ..limited[usize::from(k)][b].1.clone()
                    };
                    layer == uncut
                })
                .collect();
            assert!(
                !ks.is_empty(),
                "{name} cut {cut}: block {b} holds part of a layer"
            );
            if restart {
                assert!(!block.cut, "{name} cut {cut}: block {b} is cut");
            } else if one_segment && !block.segments.is_empty() && !ks.contains(&layers) {
                assert!(block.cut, "{name} cut {cut}: block {b} is not cut");
                cuts += 1;
            }
            matched.push((*at, ks));
        }
        // A block that holds layers 1 to k but not layer k + 1, beside one of
        // the same packet that holds layer k + 1.
        for (at, ks) in &matched {
            for &k in ks {
                let earlier = k >= 1 && !ks.contains(&0) && !ks.contains(&(k + 1));
                split |= earlier
                    && matched.iter().any(|(other, os)| {
                        other == at && os.contains(&(k + 1)) && !os.contains(&k)
                    });
            }
        }
    }
    assert!(
        restart || !one_segment || cuts > 0,
        "{name}: no block was cut"
    );
    split
}

/// `coded` with its blocks' segments made `'static`: the empty decode holds
/// no bytes.
fn strip(coded: CodedData<'_>) -> CodedData<'static> {
    let mut out = CodedData::default();
    for component in coded.components {
        let mut c = super::ComponentCoded::default();
        for resolution in component.resolutions {
            let subbands = resolution
                .subbands
                .into_iter()
                .map(|band| super::Subband {
                    blocks: band
                        .blocks
                        .into_iter()
                        .map(|block| {
                            assert!(block.segments.is_empty());
                            CodeBlock {
                                segments: Vec::new(),
                                ..block
                            }
                        })
                        .collect(),
                    ..band
                })
                .collect();
            c.resolutions.push(super::Resolution { subbands });
        }
        out.components.push(c);
    }
    out
}

/// A packet whose inline header runs past the end of the data adds nothing,
/// even to a code-block whose entry was read whole before the cut. One 8 x 4
/// tile of two 4 x 4 code-blocks, no decomposition, one layer; the header
/// gives the first block one pass and no bytes, the second one pass and one
/// byte: nonempty 1; included 1 1, zero bit-planes 1 1, one pass 0, Lblock
/// 0, length 000; included 1, zero bit-planes 1, one pass 0, Lblock 0,
/// length 001. That is 17 bits, `F8 30 80`; the data is cut after `F8 30`.
#[test]
fn a_cut_inline_header_adds_nothing() {
    let data = [0xF8, 0x30, 0x80, 0x00];
    let mut bytes = vec![0xFF, 0x4F];
    bytes.extend(seg(
        0xFF51,
        &synth::siz_body([8, 4, 0, 0, 8, 4, 0, 0], &[[7, 1, 1]]),
    ));
    bytes.extend(seg(0xFF52, &synth::cod_body(1, 0, 0, 0)));
    bytes.extend(seg(0xFF5C, &synth::qcd_none_body(1, &[9])));
    bytes.extend(synth::tile_part(0, 0, 1, &[], &data));
    bytes.extend_from_slice(&[0xFF, 0xD9]);

    let parsed = crate::codestream::parse_header(&bytes).unwrap();
    let cs = crate::codestream::parse_tiles(&bytes, parsed).unwrap();
    let header = cs.tile_header(0).unwrap().into_owned();
    let tile = &cs.tiles[0];
    let at = |cut: usize| Tile {
        data: Cow::Borrowed(&tile.data[..cut]),
        part_ends: vec![cut],
        ..tile.clone()
    };
    let empty = strip(walk(&header, &at(0)));
    let empty = blocks(&empty);
    // The whole header with its body cut: the first block's entry and data
    // are whole, so it keeps its pass.
    let body_cut = at(3);
    let body_cut = walk(&header, &body_cut);
    let body_cut = blocks(&body_cut);
    assert_eq!(body_cut.len(), 2);
    assert_eq!(body_cut[0].1.num_passes, 1);
    assert_eq!(body_cut[1], empty[1]);
    // The header cut: nothing.
    let header_cut = at(2);
    let header_cut = walk(&header, &header_cut);
    assert_eq!(blocks(&header_cut), empty);
}