pub mod marker {
pub const SOC: u16 = 0xFF4F; pub const SOT: u16 = 0xFF90; pub const SOD: u16 = 0xFF93; pub const EOC: u16 = 0xFFD9; pub const CAP: u16 = 0xFF50; pub const SIZ: u16 = 0xFF51; pub const COD: u16 = 0xFF52; pub const COC: u16 = 0xFF53; pub const RGN: u16 = 0xFF5E; pub const QCD: u16 = 0xFF5C; pub const QCC: u16 = 0xFF5D; pub const POC: u16 = 0xFF5F; pub const TLM: u16 = 0xFF55; pub const PLM: u16 = 0xFF57; pub const PLT: u16 = 0xFF58; pub const PPM: u16 = 0xFF60; pub const PPT: u16 = 0xFF61; pub const CRG: u16 = 0xFF63; pub const SOP: u16 = 0xFF91; pub const EPH: u16 = 0xFF92; pub const COM: u16 = 0xFF64;
pub const MCT: u16 = 0xFF74; pub const MCC: u16 = 0xFF75; pub const NLT: u16 = 0xFF76; pub const MCO: u16 = 0xFF77; pub const CBD: u16 = 0xFF78;
pub const PART2: std::ops::RangeInclusive<u16> = MCT..=CBD;
pub const RESERVED_NO_SEGMENT: std::ops::RangeInclusive<u16> = 0xFF30..=0xFF3F;
pub fn name(m: u16) -> Option<&'static str> {
Some(match m {
SOC => "SOC",
SOT => "SOT",
SOD => "SOD",
EOC => "EOC",
CAP => "CAP",
SIZ => "SIZ",
COD => "COD",
COC => "COC",
RGN => "RGN",
QCD => "QCD",
QCC => "QCC",
POC => "POC",
TLM => "TLM",
PLM => "PLM",
PLT => "PLT",
PPM => "PPM",
PPT => "PPT",
CRG => "CRG",
SOP => "SOP",
EPH => "EPH",
COM => "COM",
MCT => "MCT",
MCC => "MCC",
NLT => "NLT",
MCO => "MCO",
CBD => "CBD",
_ => return None,
})
}
pub fn describe(m: u16) -> String {
match name(m) {
Some(name) => format!("{name} ({m:#06X})"),
None => format!("{m:#06X}"),
}
}
pub fn is_marker(m: u16) -> bool {
m >> 8 == 0xFF && !matches!(m & 0xFF, 0x00 | 0xFF)
}
pub fn has_segment(m: u16) -> bool {
!matches!(m, SOC | SOD | EOC | EPH) && !RESERVED_NO_SEGMENT.contains(&m)
}
}
pub mod code_block_style {
pub const LAZY: u8 = 0x01;
pub const VCAUSAL: u8 = 0x08;
pub const RESET: u8 = 0x02;
pub const TERMALL: u8 = 0x04;
pub const PTERM: u8 = 0x10;
pub const SEGSYM: u8 = 0x20;
pub const FLAGS: [(u8, &str); 8] = [
(0x01, "selective arithmetic coding bypass"),
(0x02, "reset context probabilities"),
(0x04, "termination on each coding pass"),
(0x08, "vertically causal context"),
(0x10, "predictable termination"),
(0x20, "segmentation symbols"),
(0x40, "HTJ2K high-throughput block coding"),
(0x80, "HTJ2K mixed-mode block coding"),
];
pub fn describe(style: u8) -> String {
FLAGS
.iter()
.filter(|(bit, _)| style & bit != 0)
.map(|(_, name)| *name)
.collect::<Vec<_>>()
.join(", ")
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Transform {
Irreversible97,
Reversible53,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Progression {
Lrcp,
Rlcp,
Rpcl,
Pcrl,
Cprl,
}
impl Progression {
pub fn from_u8(code: u8) -> Option<Progression> {
Some(match code {
0 => Progression::Lrcp,
1 => Progression::Rlcp,
2 => Progression::Rpcl,
3 => Progression::Pcrl,
4 => Progression::Cprl,
_ => return None,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PocVolume {
pub res_start: u8,
pub res_end: u8,
pub comp_start: u16,
pub comp_end: u16,
pub layer_end: u16,
pub progression: Progression,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QuantStyle {
None,
ScalarDerived,
ScalarExpounded,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SizComponent {
pub bit_depth: u8,
pub signed: bool,
pub x_sampling: u8,
pub y_sampling: u8,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Siz {
pub x_size: u32,
pub y_size: u32,
pub x_offset: u32,
pub y_offset: u32,
pub tile_width: u32,
pub tile_height: u32,
pub tile_x_offset: u32,
pub tile_y_offset: u32,
pub components: Vec<SizComponent>,
}
pub const MAX_COMPONENTS: u16 = 16384;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TlmEntry {
pub tile_index: u32,
pub length: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Rect {
pub x0: u32,
pub y0: u32,
pub x1: u32,
pub y1: u32,
}
impl Rect {
pub fn width(&self) -> u32 {
self.x1.saturating_sub(self.x0)
}
pub fn height(&self) -> u32 {
self.y1.saturating_sub(self.y0)
}
pub fn area(&self) -> u64 {
u64::from(self.width()) * u64::from(self.height())
}
pub fn is_empty(&self) -> bool {
self.x1 <= self.x0 || self.y1 <= self.y0
}
pub fn reduced(&self, reduction: u8) -> Rect {
let scale = 1u64 << reduction.min(32);
let reduce = |v: u32| u64::from(v).div_ceil(scale) as u32;
Rect {
x0: reduce(self.x0),
y0: reduce(self.y0),
x1: reduce(self.x1),
y1: reduce(self.y1),
}
}
}
impl Siz {
pub fn tile_grid(&self) -> (u32, u32) {
if self.tile_width == 0 || self.tile_height == 0 {
return (0, 0);
}
let across = u64::from(self.x_size).saturating_sub(u64::from(self.tile_x_offset));
let down = u64::from(self.y_size).saturating_sub(u64::from(self.tile_y_offset));
(
across.div_ceil(u64::from(self.tile_width)) as u32,
down.div_ceil(u64::from(self.tile_height)) as u32,
)
}
pub fn num_tiles(&self) -> u32 {
let (across, down) = self.tile_grid();
across.saturating_mul(down)
}
pub fn tile_rect(&self, index: u32) -> Option<Rect> {
let (across, _) = self.tile_grid();
if across == 0 || index >= self.num_tiles() {
return None;
}
let (p, q) = (u64::from(index % across), u64::from(index / across));
let (xt, yt) = (u64::from(self.tile_width), u64::from(self.tile_height));
let (xto, yto) = (u64::from(self.tile_x_offset), u64::from(self.tile_y_offset));
Some(Rect {
x0: (xto + p * xt).max(u64::from(self.x_offset)) as u32,
y0: (yto + q * yt).max(u64::from(self.y_offset)) as u32,
x1: (xto + (p + 1) * xt).min(u64::from(self.x_size)) as u32,
y1: (yto + (q + 1) * yt).min(u64::from(self.y_size)) as u32,
})
}
pub fn tile_component_rect(&self, index: u32, comp: usize) -> Option<Rect> {
let tile = self.tile_rect(index)?;
let c = self.components.get(comp)?;
let (xr, yr) = (u64::from(c.x_sampling), u64::from(c.y_sampling));
if xr == 0 || yr == 0 {
return None;
}
let div = |v: u32, r: u64| u64::from(v).div_ceil(r) as u32;
Some(Rect {
x0: div(tile.x0, xr),
y0: div(tile.y0, yr),
x1: div(tile.x1, xr),
y1: div(tile.y1, yr),
})
}
pub fn component_rect(&self, comp: usize) -> Option<Rect> {
let c = self.components.get(comp)?;
let (xr, yr) = (u64::from(c.x_sampling), u64::from(c.y_sampling));
if xr == 0 || yr == 0 {
return None;
}
let div = |v: u32, r: u64| u64::from(v).div_ceil(r) as u32;
Some(Rect {
x0: div(self.x_offset, xr),
y0: div(self.y_offset, yr),
x1: div(self.x_size, xr),
y1: div(self.y_size, yr),
})
}
pub fn image_extent_at(&self, reduction: u8) -> (u32, u32) {
let scale = 1u64 << reduction.min(32);
let reduce = |size: u32, offset: u32| {
(u64::from(size).div_ceil(scale)).saturating_sub(u64::from(offset).div_ceil(scale))
as u32
};
(
reduce(self.x_size, self.x_offset),
reduce(self.y_size, self.y_offset),
)
}
pub fn component_extent(&self, index: usize) -> Option<(u32, u32)> {
self.component_extent_at(index, 0)
}
pub fn component_extent_at(&self, index: usize, reduction: u8) -> Option<(u32, u32)> {
let comp = self.components.get(index)?;
let (xr, yr) = (comp.x_sampling as u64, comp.y_sampling as u64);
if xr == 0 || yr == 0 {
return None;
}
let scale = 1u64 << reduction.min(32);
let reduce = |size: u32, offset: u32, sampling: u64| {
let hi = (u64::from(size)).div_ceil(sampling).div_ceil(scale);
let lo = (u64::from(offset)).div_ceil(sampling).div_ceil(scale);
hi.saturating_sub(lo) as u32
};
Some((
reduce(self.x_size, self.x_offset, xr),
reduce(self.y_size, self.y_offset, yr),
))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Coding {
pub decomposition_levels: u8,
pub code_block_width: u8, pub code_block_height: u8, pub code_block_style: u8, pub transform: Transform,
pub precinct_sizes: Vec<(u8, u8)>,
}
impl Coding {
pub fn precinct(&self, r: usize) -> (u32, u32) {
self.precinct_sizes
.get(r)
.map_or((15, 15), |&(ppx, ppy)| (u32::from(ppx), u32::from(ppy)))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ComponentParams {
pub coding: Coding,
pub quant: Qcd,
pub roi_shift: u8,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cod {
pub progression: Progression,
pub layers: u16,
pub decomposition_levels: u8,
pub code_block_width: u8, pub code_block_height: u8, pub code_block_style: u8, pub use_sop: bool,
pub use_eph: bool,
pub multiple_component_transform: bool,
pub transform: Transform,
pub precinct_sizes: Vec<(u8, u8)>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Qcd {
pub style: QuantStyle,
pub guard_bits: u8,
pub steps: Vec<(u8, u16)>,
}
impl Cod {
pub fn coding(&self) -> Coding {
Coding {
decomposition_levels: self.decomposition_levels,
code_block_width: self.code_block_width,
code_block_height: self.code_block_height,
code_block_style: self.code_block_style,
transform: self.transform,
precinct_sizes: self.precinct_sizes.clone(),
}
}
}
impl Qcd {
pub fn subband_step(&self, band: usize) -> Option<(u8, u16)> {
match self.style {
QuantStyle::None | QuantStyle::ScalarExpounded => self.steps.get(band).copied(),
QuantStyle::ScalarDerived => {
let (exp0, mant0) = *self.steps.first()?;
let drop = u8::try_from(band.saturating_sub(1) / 3).unwrap_or(u8::MAX);
Some((exp0.saturating_sub(drop), mant0))
}
}
}
}