use crate::Result;
use crate::codestream::MainHeader;
use crate::codestream::markers::{Qcd, QuantStyle};
use crate::error::Error;
const MANTISSA_DENOM: f64 = 2048.0;
pub fn half_step(header: &MainHeader, comp: usize, band: usize) -> Result<f32> {
let prec = i32::from(
header
.siz
.components
.get(comp)
.ok_or_else(|| Error::inconsistent(format!("`SIZ` declares no component {comp}")))?
.bit_depth,
);
let qcd = &header
.components
.get(comp)
.ok_or_else(|| Error::inconsistent(format!("no coding parameters for component {comp}")))?
.quant;
Ok(half_step_of(step_params(qcd, band)?, prec))
}
fn step_params(qcd: &Qcd, band: usize) -> Result<(u8, u16)> {
if qcd.style == QuantStyle::None {
return Err(Error::inconsistent(
"irreversible transform needs scalar quantization, found QuantStyle::None",
));
}
qcd.subband_step(band).ok_or_else(|| {
Error::inconsistent(format!(
"`QCD` carries {} step sizes but subband {band} needs one",
qcd.steps.len()
))
})
}
fn half_step_of((exp, mant): (u8, u16), prec: i32) -> f32 {
let step = (1.0 + f64::from(mant) / MANTISSA_DENOM) * 2f64.powi(prec - i32::from(exp));
0.5f32 * step as f32
}
#[cfg(test)]
mod tests {
use super::*;
use crate::codestream::markers::Transform;
fn header(prec: u8, qcd: Qcd) -> MainHeader {
MainHeader::single_component((4, 4), (prec, false), (1, Transform::Irreversible97), qcd)
}
fn step(prec: i32, exp: i32, mant: i32) -> f32 {
(1.0 + mant as f32 / 2048.0) * 2f32.powi(prec - exp)
}
fn want(step: f32) -> f32 {
0.5f32 * step
}
fn assert_close(got: f32, want: f32) {
assert!(
(got - want).abs() <= 1e-3 * want.abs().max(1.0),
"got {got}, want {want}"
);
}
#[test]
fn expounded_matches_hand_computed() {
let qcd = Qcd {
style: QuantStyle::ScalarExpounded,
guard_bits: 2,
steps: vec![(8, 0), (7, 512), (7, 512), (6, 1024)],
};
let h = header(8, qcd);
assert_close(half_step(&h, 0, 0).unwrap(), want(step(8, 8, 0)));
assert_close(half_step(&h, 0, 1).unwrap(), want(step(8, 7, 512)));
assert_close(half_step(&h, 0, 2).unwrap(), want(step(8, 7, 512)));
assert_close(half_step(&h, 0, 3).unwrap(), want(step(8, 6, 1024)));
}
#[test]
fn half_step_is_the_narrowed_step_halved() {
let qcd = Qcd {
style: QuantStyle::ScalarExpounded,
guard_bits: 2,
steps: vec![(9, 1234)],
};
let exact = (1.0 + 1234.0 / 2048.0) * 2f64.powi(12 - 9);
assert_eq!(
half_step(&header(12, qcd), 0, 0).unwrap().to_bits(),
(0.5f32 * exact as f32).to_bits()
);
}
#[test]
fn derived_drops_exponent_one_per_level() {
let qcd = Qcd {
style: QuantStyle::ScalarDerived,
guard_bits: 1,
steps: vec![(10, 100)],
};
let h = header(8, qcd);
assert_close(half_step(&h, 0, 0).unwrap(), want(step(8, 10, 100)));
assert_close(half_step(&h, 0, 1).unwrap(), want(step(8, 10, 100)));
assert_close(half_step(&h, 0, 4).unwrap(), want(step(8, 9, 100)));
}
#[test]
fn derived_exponent_saturates_at_zero() {
let qcd = Qcd {
style: QuantStyle::ScalarDerived,
guard_bits: 1,
steps: vec![(1, 0)],
};
assert_close(
half_step(&header(8, qcd), 0, 7).unwrap(),
want(step(8, 0, 0)),
);
}
#[test]
fn expounded_walks_two_levels_in_order() {
let steps = [
(9, 0),
(8, 16),
(8, 32),
(7, 64),
(6, 128),
(6, 256),
(5, 512),
];
let qcd = Qcd {
style: QuantStyle::ScalarExpounded,
guard_bits: 2,
steps: steps.to_vec(),
};
let h = header(8, qcd);
for (band, (exp, mant)) in steps.into_iter().enumerate() {
assert_close(
half_step(&h, 0, band).unwrap(),
want(step(8, i32::from(exp), i32::from(mant))),
);
}
}
#[test]
fn a_band_without_a_step_is_inconsistent() {
let qcd = Qcd {
style: QuantStyle::ScalarExpounded,
guard_bits: 2,
steps: vec![(8, 0)],
};
let err = half_step(&header(8, qcd), 0, 1).unwrap_err();
assert!(matches!(err, Error::Inconsistent(_)));
}
#[test]
fn excess_steps_are_ignored() {
let qcd = Qcd {
style: QuantStyle::ScalarExpounded,
guard_bits: 2,
steps: vec![(8, 0); 7],
};
half_step(&header(8, qcd), 0, 3)
.expect("excess entries are padding, as OpenJPEG treats them");
}
#[test]
fn irreversible_without_scalar_quant_is_inconsistent() {
let qcd = Qcd {
style: QuantStyle::None,
guard_bits: 2,
steps: vec![(8, 0)],
};
let err = half_step(&header(8, qcd), 0, 0).unwrap_err();
assert!(matches!(err, Error::Inconsistent(_)));
}
#[test]
fn an_unknown_component_is_inconsistent() {
let qcd = Qcd {
style: QuantStyle::ScalarExpounded,
guard_bits: 2,
steps: vec![(8, 0)],
};
let err = half_step(&header(8, qcd), 1, 0).unwrap_err();
assert!(matches!(err, Error::Inconsistent(_)));
}
}