From cc7da71b4792a3b9b2712a47721a9c9ac1ebb4b9 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 25 Oct 2026 14:02:03 +0100 Subject: [PATCH] raw: faster lossless JPEG scan loop (inline diff, 2-component loop, shared tables) The predictor 1 row loop called Bits::diff out of line, indexed the per-component tables with bounds checks, and kept a private copy of the 48 KiB Huffman lookup pair per component even when the components share one DHT (CR2 and DNG two-component frames). - Bits::diff is always inlined. - Rows with one or two components carry Ra in registers and walk the row with chunks of two. - Components that name the same table share one copy. The frame size cap, the one-bit-per-sample truncation guard and the DHT rebuild cap are untouched. Co-Authored-By: Claude Sonnet 5.5 --- crates/raw/src/ljpeg.rs | 48 ++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/crates/raw/src/ljpeg.rs b/crates/raw/src/ljpeg.rs index 4ab32df8b8..28e3a86300 100644 --- a/crates/raw/src/ljpeg.rs +++ b/crates/raw/src/ljpeg.rs @@ -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 { if self.n < 32 { self.refill(); @@ -284,8 +284,11 @@ pub(crate) fn frame(data: &[u8]) -> Result { struct Parsed<'a> { frame: Frame, - /// Per component of the scan (in frame order): its Huffman table. + /// The scan's distinct Huffman tables... tables: Vec, + /// ...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, predictor: u8, point_transform: u8, restart: usize, @@ -378,7 +381,9 @@ fn parse(data: &[u8]) -> Result> { 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 = Vec::with_capacity(ns); + let mut ids: Vec = 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> { } 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> { 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 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 // 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 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 } } }; - let d = bits.diff(&p.tables[c])?; + let d = bits.diff(table)?; cur[i] = (px + d) as u16; } }