pub(crate) mod mq;
pub(crate) mod passes;
use crate::tier2::{BandKind, CodeBlock};
use crate::{Error, Result};
use passes::{BlockParams, BlockState, MAX_BIT_PLANES, Orientation, decode_block, top_coded_plane};
use std::borrow::Cow;
#[derive(Debug)]
pub(super) struct Scratch<'a> {
state: BlockState,
segments: Vec<(Cow<'a, [u8]>, u32)>,
}
impl Default for Scratch<'_> {
fn default() -> Self {
Scratch {
state: BlockState::new(0, 0),
segments: Vec::new(),
}
}
}
pub(super) fn decode<'s, 'a>(
block: &CodeBlock<'a>,
kind: BandKind,
numbps: u32,
style: u8,
roi_shift: u8,
scratch: &'s mut Scratch<'a>,
) -> Result<&'s [i32]> {
let orient = match kind {
BandKind::Ll => Orientation::Ll,
BandKind::Hl => Orientation::Hl,
BandKind::Lh => Orientation::Lh,
BandKind::Hh => Orientation::Hh,
};
let top = top_coded_plane(numbps, block.zero_bit_planes, roi_shift);
if top > MAX_BIT_PLANES {
return Err(Error::limit_fixed(
crate::LimitKind::BitPlanes,
format!("code-block needs {top} bit-planes, over the {MAX_BIT_PLANES}-plane limit"),
));
}
let Scratch { state, segments } = scratch;
state.reset(block.width as u32, block.height as u32);
segments.clear();
segments.extend(
block
.segments
.iter()
.map(|segment| (segment.bytes(), segment.passes)),
);
let params = BlockParams {
style,
roi_shift,
last_segment_cut: block.cut,
};
decode_block(
segments,
state,
orient,
numbps,
block.num_passes,
block.zero_bit_planes,
params,
)?;
Ok(&state.coeffs)
}
pub(super) fn passes_run(numbps: u32, block: &CodeBlock<'_>, roi_shift: u8) -> u64 {
if block.num_passes == 0 {
return 0;
}
let top = top_coded_plane(numbps, block.zero_bit_planes, roi_shift);
if !(1..=MAX_BIT_PLANES).contains(&top) {
return 0;
}
u64::from(block.num_passes.min(3 * top - 2))
}