mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge PR #9451: raw: faster lossless JPEG scan loop (inline diff, 2-component loop, shared tables)
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
commit
fd533fbaf9
1 file changed
+36
-12
+36
-12
@@ -249,7 +249,7 @@ impl<'a> Bits<'a> {
|
||||
}
|
||||
|
||||
/// A difference value: Huffman-coded category SSSS then SSSS extra bits (T.81 H.1.2.2).
|
||||
#[inline]
|
||||
#[inline(always)]
|
||||
fn diff(&mut self, h: &Huffman) -> Result<i32> {
|
||||
if self.n < 32 {
|
||||
self.refill();
|
||||
@@ -284,8 +284,11 @@ pub(crate) fn frame(data: &[u8]) -> Result<Frame> {
|
||||
|
||||
struct Parsed<'a> {
|
||||
frame: Frame,
|
||||
/// Per component of the scan (in frame order): its Huffman table.
|
||||
/// The scan's distinct Huffman tables...
|
||||
tables: Vec<Huffman>,
|
||||
/// ...and per component of the scan (in frame order) the index of its table: components
|
||||
/// sharing a table share one copy (one 48 KiB lookup pair in cache instead of several).
|
||||
comp_table: Vec<usize>,
|
||||
predictor: u8,
|
||||
point_transform: u8,
|
||||
restart: usize,
|
||||
@@ -378,7 +381,9 @@ fn parse(data: &[u8]) -> Result<Parsed<'_>> {
|
||||
if ns != f.components || seg.len() < 1 + 2 * ns + 3 {
|
||||
return Err(RawError::unsupported("lossless JPEG with non-interleaved scans"));
|
||||
}
|
||||
let mut tables = Vec::with_capacity(ns);
|
||||
let mut tables: Vec<Huffman> = Vec::with_capacity(ns);
|
||||
let mut ids: Vec<usize> = Vec::with_capacity(ns);
|
||||
let mut comp_table = Vec::with_capacity(ns);
|
||||
for c in 0..ns {
|
||||
let id = seg[1 + 2 * c];
|
||||
if comp_ids.get(c) != Some(&id) {
|
||||
@@ -386,7 +391,14 @@ fn parse(data: &[u8]) -> Result<Parsed<'_>> {
|
||||
}
|
||||
let td = usize::from(seg[2 + 2 * c] >> 4);
|
||||
let h = huff.get(td).and_then(Option::as_ref).ok_or_else(|| RawError::malformed("missing Huffman table"))?;
|
||||
tables.push(h.clone());
|
||||
match ids.iter().position(|&i| i == td) {
|
||||
Some(k) => comp_table.push(k),
|
||||
None => {
|
||||
ids.push(td);
|
||||
comp_table.push(tables.len());
|
||||
tables.push(h.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
let predictor = seg[1 + 2 * ns];
|
||||
let point_transform = seg[3 + 2 * ns] & 0x0F;
|
||||
@@ -397,7 +409,7 @@ fn parse(data: &[u8]) -> Result<Parsed<'_>> {
|
||||
return Err(RawError::malformed("lossless JPEG point transform"));
|
||||
}
|
||||
let entropy = data.get(pos + len..).unwrap_or(&[]);
|
||||
return Ok(Parsed { frame: f, tables, predictor, point_transform, restart, entropy });
|
||||
return Ok(Parsed { frame: f, tables, comp_table, predictor, point_transform, restart, entropy });
|
||||
}
|
||||
_ => {} // APPn, COM, DQT, DNL… ignored
|
||||
}
|
||||
@@ -427,6 +439,7 @@ pub(crate) fn decode(data: &[u8], max_samples: usize) -> Result<(Frame, Vec<u16>
|
||||
let mut out = vec![0u16; total];
|
||||
let mut bits = Bits::new(p.entropy);
|
||||
let nc = f.components;
|
||||
let tables: Vec<&Huffman> = p.comp_table.iter().map(|&k| &p.tables[k]).collect();
|
||||
let initial = 1i32 << (f.precision - p.point_transform - 1);
|
||||
let pred = p.predictor;
|
||||
let mut interval_row = 0usize;
|
||||
@@ -447,17 +460,28 @@ pub(crate) fn decode(data: &[u8], max_samples: usize) -> Result<(Frame, Vec<u16>
|
||||
// Predictor 1 (or the first line of an interval): Ra after the first column.
|
||||
for c in 0..nc {
|
||||
let px = if first_line { initial } else { i32::from(prev[c]) };
|
||||
cur[c] = (px + bits.diff(&p.tables[c])?) as u16;
|
||||
cur[c] = (px + bits.diff(tables[c])?) as u16;
|
||||
}
|
||||
// Ra: the previous pixel's sample of the same component, carried in registers.
|
||||
if nc == 1 {
|
||||
let t = &p.tables[0];
|
||||
for i in 1..row_len {
|
||||
cur[i] = (i32::from(cur[i - 1]) + bits.diff(t)?) as u16;
|
||||
let t = tables[0];
|
||||
let mut ra = cur[0];
|
||||
for v in &mut cur[1..] {
|
||||
ra = (i32::from(ra) + bits.diff(t)?) as u16;
|
||||
*v = ra;
|
||||
}
|
||||
} else if nc == 2 {
|
||||
let (t0, t1) = (tables[0], tables[1]);
|
||||
let mut ra = [cur[0], cur[1]];
|
||||
for px in cur[2..].as_chunks_mut::<2>().0 {
|
||||
ra[0] = (i32::from(ra[0]) + bits.diff(t0)?) as u16;
|
||||
ra[1] = (i32::from(ra[1]) + bits.diff(t1)?) as u16;
|
||||
*px = ra;
|
||||
}
|
||||
} else {
|
||||
for i in (nc..row_len).step_by(nc) {
|
||||
for c in 0..nc {
|
||||
cur[i + c] = (i32::from(cur[i + c - nc]) + bits.diff(&p.tables[c])?) as u16;
|
||||
cur[i + c] = (i32::from(cur[i + c - nc]) + bits.diff(tables[c])?) as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -468,7 +492,7 @@ pub(crate) fn decode(data: &[u8], max_samples: usize) -> Result<(Frame, Vec<u16>
|
||||
continue;
|
||||
}
|
||||
for x in 0..f.width {
|
||||
for c in 0..nc {
|
||||
for (c, &table) in tables.iter().enumerate() {
|
||||
let i = x * nc + c;
|
||||
let px = if x == 0 {
|
||||
if first_line { initial } else { i32::from(prev[i]) }
|
||||
@@ -490,7 +514,7 @@ pub(crate) fn decode(data: &[u8], max_samples: usize) -> Result<(Frame, Vec<u16>
|
||||
}
|
||||
}
|
||||
};
|
||||
let d = bits.diff(&p.tables[c])?;
|
||||
let d = bits.diff(table)?;
|
||||
cur[i] = (px + d) as u16;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user