pub mod markers;
use crate::{Error, Result};
use markers::{Cod, Progression, Qcd, QuantStyle, Siz, SizComponent, Transform, marker};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MainHeader {
pub siz: Siz,
pub cod: Cod,
pub qcd: Qcd,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TilePart<'a> {
pub tile_index: u16,
pub data: &'a [u8],
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Codestream<'a> {
pub header: MainHeader,
pub tile_parts: Vec<TilePart<'a>>,
}
pub fn parse(bytes: &[u8]) -> Result<Codestream<'_>> {
let (header, sot_offset) = parse_main_header(bytes)?;
let tile_parts = walk_tile_parts(bytes, sot_offset)?;
Ok(Codestream { header, tile_parts })
}
fn walk_tile_parts(bytes: &[u8], sot_offset: usize) -> Result<Vec<TilePart<'_>>> {
let mut cur = Cursor::at(bytes, sot_offset);
if cur.u16()? != marker::SOT {
return Err(Error::Codestream(
"tile-part does not start with SOT".into(),
));
}
let sot = decode_sot(segment(&mut cur)?)?;
if sot.tile_index != 0 {
return Err(Error::Unsupported(format!(
"tile index {}; the subset is a single tile",
sot.tile_index
)));
}
if sot.tile_part_index != 0 || sot.num_tile_parts > 1 {
return Err(Error::Unsupported(
"multiple tile-parts; the subset is a single tile-part".into(),
));
}
loop {
let m = cur.u16()?;
match m {
marker::SOD => break,
marker::COM => {
segment(&mut cur)?;
}
marker::COD
| marker::COC
| marker::QCD
| marker::QCC
| marker::RGN
| marker::POC
| marker::TLM
| marker::PLT
| marker::SOP
| marker::EPH => {
return Err(Error::Unsupported(format!(
"tile-part header marker {m:#06X} is outside the Phase 1 subset"
)));
}
other => {
return Err(Error::Codestream(format!(
"unexpected marker {other:#06X} in tile-part header"
)));
}
}
}
let data_start = cur.pos;
let data_end = if sot.psot == 0 {
bytes
.len()
.checked_sub(2)
.filter(|&end| end >= data_start && read_u16(bytes, end) == Some(marker::EOC))
.ok_or_else(|| Error::Codestream("Psot=0 tile-part is not terminated by EOC".into()))?
} else {
let end = sot_offset
.checked_add(sot.psot as usize)
.filter(|&end| end <= bytes.len())
.ok_or_else(|| Error::Codestream("Psot overruns the codestream".into()))?;
if end < data_start {
return Err(Error::Codestream(
"Psot is shorter than the tile-part header".into(),
));
}
end
};
let data = &bytes[data_start..data_end];
match read_u16(bytes, data_end) {
Some(marker::EOC) => {}
Some(marker::SOT) => {
return Err(Error::Unsupported(
"multiple tile-parts; the subset is a single tile-part".into(),
));
}
Some(other) => {
return Err(Error::Codestream(format!(
"expected EOC after the tile-part, found {other:#06X}"
)));
}
None => return Err(Error::Codestream("missing EOC after the tile-part".into())),
}
Ok(vec![TilePart {
tile_index: sot.tile_index,
data,
}])
}
struct Sot {
tile_index: u16,
psot: u32,
tile_part_index: u8,
num_tile_parts: u8,
}
fn decode_sot(mut b: Cursor<'_>) -> Result<Sot> {
let tile_index = b.u16()?;
let psot = b.u32()?;
let tile_part_index = b.u8()?;
let num_tile_parts = b.u8()?;
b.expect_consumed("SOT")?;
Ok(Sot {
tile_index,
psot,
tile_part_index,
num_tile_parts,
})
}
fn read_u16(bytes: &[u8], pos: usize) -> Option<u16> {
let hi = *bytes.get(pos)?;
let lo = *bytes.get(pos + 1)?;
Some(u16::from_be_bytes([hi, lo]))
}
fn parse_main_header(bytes: &[u8]) -> Result<(MainHeader, usize)> {
let mut cur = Cursor::new(bytes);
if cur.u16()? != marker::SOC {
return Err(Error::Codestream(
"does not start with the SOC marker".into(),
));
}
if cur.u16()? != marker::SIZ {
return Err(Error::Codestream(
"SIZ must be the first marker after SOC".into(),
));
}
let siz = decode_siz(segment(&mut cur)?)?;
let mut cod = None;
let mut qcd = None;
let sot_offset = loop {
let m = cur.u16()?;
match m {
marker::SOT => break cur.pos - 2,
marker::COD => {
if cod.is_some() {
return Err(Error::Codestream("duplicate COD marker".into()));
}
cod = Some(decode_cod(segment(&mut cur)?)?);
}
marker::QCD => {
if qcd.is_some() {
return Err(Error::Codestream("duplicate QCD marker".into()));
}
qcd = Some(decode_qcd(segment(&mut cur)?)?);
}
marker::COM => {
segment(&mut cur)?;
}
marker::COC
| marker::QCC
| marker::RGN
| marker::POC
| marker::TLM
| marker::PLT
| marker::SOP
| marker::EPH => {
return Err(Error::Unsupported(format!(
"marker {m:#06X} is outside the Phase 1 subset"
)));
}
marker::SOD | marker::EOC => {
return Err(Error::Codestream(format!(
"unexpected marker {m:#06X} before any tile-part"
)));
}
other => {
return Err(Error::Codestream(format!(
"unknown marker {other:#06X} in main header"
)));
}
}
};
let cod = cod.ok_or_else(|| Error::Codestream("missing required COD marker".into()))?;
let qcd = qcd.ok_or_else(|| Error::Codestream("missing required QCD marker".into()))?;
Ok((MainHeader { siz, cod, qcd }, sot_offset))
}
const MAX_IMAGE_SAMPLES: u64 = 1 << 26;
fn validate_geometry(siz: &Siz) -> Result<()> {
if siz.x_size == 0 || siz.y_size == 0 {
return Err(Error::Marker("SIZ declares a zero-size image".into()));
}
if siz.x_offset != 0 || siz.y_offset != 0 {
return Err(Error::Unsupported(format!(
"image offset ({}, {}); the Phase 1 subset is canvas-origin only",
siz.x_offset, siz.y_offset
)));
}
if siz.tile_x_offset != 0 || siz.tile_y_offset != 0 {
return Err(Error::Unsupported(format!(
"tile offset ({}, {}); the Phase 1 subset is canvas-origin only",
siz.tile_x_offset, siz.tile_y_offset
)));
}
if siz.tile_width == 0 || siz.tile_height == 0 {
return Err(Error::Marker("SIZ declares a zero-size tile".into()));
}
if (siz.tile_width as u64) < siz.x_size as u64 || (siz.tile_height as u64) < siz.y_size as u64 {
return Err(Error::Unsupported(
"tile smaller than the image (multi-tile grid); the Phase 1 subset is single-tile"
.into(),
));
}
if siz.x_size as u64 * siz.y_size as u64 > MAX_IMAGE_SAMPLES {
return Err(Error::Unsupported(format!(
"image area {}×{} exceeds the Phase 1 decode guard of {MAX_IMAGE_SAMPLES} samples",
siz.x_size, siz.y_size
)));
}
Ok(())
}
fn decode_siz(mut b: Cursor<'_>) -> Result<Siz> {
let _rsiz = b.u16()?; let x_size = b.u32()?;
let y_size = b.u32()?;
let x_offset = b.u32()?;
let y_offset = b.u32()?;
let tile_width = b.u32()?;
let tile_height = b.u32()?;
let tile_x_offset = b.u32()?;
let tile_y_offset = b.u32()?;
let csiz = b.u16()?;
if csiz == 0 {
return Err(Error::Marker("SIZ declares zero components".into()));
}
if csiz != 1 {
return Err(Error::Unsupported(format!(
"{csiz} components; the Phase 1 subset is single-component"
)));
}
let mut components = Vec::with_capacity(csiz as usize);
for _ in 0..csiz {
let ssiz = b.u8()?;
let x_sampling = b.u8()?;
let y_sampling = b.u8()?;
components.push(SizComponent {
bit_depth: (ssiz & 0x7F) + 1,
signed: ssiz & 0x80 != 0,
x_sampling,
y_sampling,
});
}
b.expect_consumed("SIZ")?;
let siz = Siz {
x_size,
y_size,
x_offset,
y_offset,
tile_width,
tile_height,
tile_x_offset,
tile_y_offset,
components,
};
validate_geometry(&siz)?;
Ok(siz)
}
fn decode_cod(mut b: Cursor<'_>) -> Result<Cod> {
let scod = b.u8()?;
if scod & 0x01 != 0 {
return Err(Error::Unsupported(
"explicit precinct partition; the subset uses maximal precincts".into(),
));
}
if scod & 0x06 != 0 {
return Err(Error::Unsupported(
"SOP/EPH error-resilience markers are out of the Phase 1 subset".into(),
));
}
let progression = match b.u8()? {
0 => Progression::Lrcp,
p @ 1..=4 => {
return Err(Error::Unsupported(format!(
"progression order {p}; the subset is LRCP only"
)));
}
other => {
return Err(Error::Marker(format!("reserved progression order {other}")));
}
};
let layers = b.u16()?;
if layers != 1 {
return Err(Error::Unsupported(format!(
"{layers} quality layers; the subset is single-layer"
)));
}
let mct = b.u8()?;
if mct != 0 {
return Err(Error::Unsupported(
"multiple-component transform is out of the single-component subset".into(),
));
}
let decomposition_levels = b.u8()?;
let code_block_width = b.u8()?;
let code_block_height = b.u8()?;
let code_block_style = b.u8()?;
let transform = match b.u8()? {
0 => Transform::Irreversible97,
1 => Transform::Reversible53,
other => return Err(Error::Marker(format!("reserved wavelet transform {other}"))),
};
b.expect_consumed("COD")?;
Ok(Cod {
progression,
layers,
decomposition_levels,
code_block_width,
code_block_height,
code_block_style,
transform,
precinct_sizes: Vec::new(),
})
}
fn decode_qcd(mut b: Cursor<'_>) -> Result<Qcd> {
let sqcd = b.u8()?;
let guard_bits = sqcd >> 5;
let style = match sqcd & 0x1F {
0 => QuantStyle::None,
1 => QuantStyle::ScalarDerived,
2 => QuantStyle::ScalarExpounded,
other => {
return Err(Error::Marker(format!(
"reserved quantization style {other}"
)));
}
};
let mut steps = Vec::new();
match style {
QuantStyle::None => {
if b.remaining() == 0 {
return Err(Error::Codestream("QCD carries no step entries".into()));
}
while b.remaining() > 0 {
let v = b.u8()?;
steps.push((v >> 3, 0));
}
}
QuantStyle::ScalarDerived | QuantStyle::ScalarExpounded => {
if b.remaining() == 0 || !b.remaining().is_multiple_of(2) {
return Err(Error::Codestream("QCD step table is truncated".into()));
}
if style == QuantStyle::ScalarDerived && b.remaining() != 2 {
return Err(Error::Codestream(
"derived QCD must carry exactly one step entry".into(),
));
}
while b.remaining() > 0 {
let v = b.u16()?;
steps.push((((v >> 11) & 0x1F) as u8, v & 0x07FF));
}
}
}
Ok(Qcd {
style,
guard_bits,
steps,
})
}
fn segment<'a>(cur: &mut Cursor<'a>) -> Result<Cursor<'a>> {
let len = cur.u16()? as usize;
if len < 2 {
return Err(Error::Codestream("marker segment length below 2".into()));
}
let body = cur.take(len - 2)?;
Ok(Cursor::new(body))
}
struct Cursor<'a> {
buf: &'a [u8],
pos: usize,
}
impl<'a> Cursor<'a> {
fn new(buf: &'a [u8]) -> Self {
Self { buf, pos: 0 }
}
fn at(buf: &'a [u8], pos: usize) -> Self {
Self { buf, pos }
}
fn remaining(&self) -> usize {
self.buf.len() - self.pos
}
fn take(&mut self, n: usize) -> Result<&'a [u8]> {
if self.remaining() < n {
return Err(Error::Codestream("truncated marker segment".into()));
}
let slice = &self.buf[self.pos..self.pos + n];
self.pos += n;
Ok(slice)
}
fn u8(&mut self) -> Result<u8> {
Ok(self.take(1)?[0])
}
fn u16(&mut self) -> Result<u16> {
let b = self.take(2)?;
Ok(u16::from_be_bytes([b[0], b[1]]))
}
fn u32(&mut self) -> Result<u32> {
let b = self.take(4)?;
Ok(u32::from_be_bytes([b[0], b[1], b[2], b[3]]))
}
fn expect_consumed(&self, marker: &str) -> Result<()> {
if self.remaining() != 0 {
return Err(Error::Codestream(format!(
"{marker} segment has {} unexpected trailing byte(s)",
self.remaining()
)));
}
Ok(())
}
}
#[cfg(test)]
mod tests;