mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-17 08:54:42 +00:00
psd: delegate ZIP prediction reversal with budgeted scratch
This commit is contained in:
Generated
+1
-1
@@ -1359,7 +1359,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-psd"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e836b6fd893eca3fbe5880f000dbf446c3a32fcc#e836b6fd893eca3fbe5880f000dbf446c3a32fcc"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c864e1e8c9e0f7e2c9f24fca1faa6f298a068bca#c864e1e8c9e0f7e2c9f24fca1faa6f298a068bca"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"flate2",
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs"
|
||||
craft-ps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", default-features = false, features = ["read"] }
|
||||
craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
|
||||
craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
|
||||
craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e836b6fd893eca3fbe5880f000dbf446c3a32fcc" }
|
||||
craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c864e1e8c9e0f7e2c9f24fca1faa6f298a068bca" }
|
||||
craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
|
||||
craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
|
||||
craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
|
||||
|
||||
+29
-39
@@ -134,46 +134,36 @@ impl Psd {
|
||||
|
||||
/// Undo ZIP prediction in place; the 32-bit scratch row is charged to `budget`.
|
||||
pub(crate) fn unpredict(&self, v: &mut [u8], w: usize, h: usize, budget: &mut Budget) -> Result<()> {
|
||||
match self.depth {
|
||||
16 => {
|
||||
for row in v.chunks_exact_mut(w * 2).take(h) {
|
||||
let mut prev = 0u16;
|
||||
for c in row.as_chunks_mut::<2>().0 {
|
||||
let x = u16::from_be_bytes([c[0], c[1]]).wrapping_add(prev);
|
||||
c.copy_from_slice(&x.to_be_bytes());
|
||||
prev = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
32 => {
|
||||
// One scratch row for the whole plane, charged: a wide, short layer passes the
|
||||
// decode's peak check and would otherwise allocate it on top.
|
||||
let mut planar = budget.filled(w.checked_mul(4).ok_or_else(|| too_large(w, h))?, 0u8)?;
|
||||
#[cfg(test)]
|
||||
counters::bump(&counters::SCRATCH);
|
||||
for row in v.chunks_exact_mut(w * 4).take(h) {
|
||||
for i in 1..row.len() {
|
||||
row[i] = row[i].wrapping_add(row[i - 1]);
|
||||
}
|
||||
// Bytes are stored plane by plane (all first bytes, then all second bytes…).
|
||||
planar.copy_from_slice(row);
|
||||
for x in 0..w {
|
||||
for b in 0..4 {
|
||||
row[x * 4 + b] = planar[b * w + x];
|
||||
}
|
||||
}
|
||||
}
|
||||
budget.release(planar.len() as u64);
|
||||
}
|
||||
_ => {
|
||||
for row in v.chunks_exact_mut(w).take(h) {
|
||||
for i in 1..row.len() {
|
||||
row[i] = row[i].wrapping_add(row[i - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Preserve the reader's recovery behavior: decode only complete rows, at most h.
|
||||
// The fallback depths historically used byte-wise prediction rather than packed bits.
|
||||
let depth = match self.depth {
|
||||
16 => 16,
|
||||
32 => 32,
|
||||
_ => 8,
|
||||
};
|
||||
let rowb = w.checked_mul(usize::from(depth / 8)).ok_or_else(|| too_large(w, h))?;
|
||||
if rowb == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
Ok(())
|
||||
let rows = (v.len() / rowb).min(h);
|
||||
let layout = craft_psd::compression::PlaneLayout {
|
||||
planes: 1,
|
||||
width: w,
|
||||
height: rows,
|
||||
depth,
|
||||
version: if self.psb { craft_psd::Version::Psb } else { craft_psd::Version::Psd },
|
||||
};
|
||||
let mut scratch = if depth == 32 {
|
||||
let scratch = budget.filled(rowb, 0u8)?;
|
||||
#[cfg(test)]
|
||||
counters::bump(&counters::SCRATCH);
|
||||
scratch
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let result = craft_psd::compression::unpredict_with_scratch(&mut v[..rows * rowb], &layout, &mut scratch);
|
||||
budget.release(scratch.len() as u64);
|
||||
result.map_err(|e| Error::Invalid(e.to_string()))
|
||||
}
|
||||
|
||||
/// Hand every sample of `raw` (`w`×`h`, row-major, in order) to `f` as a float. Samples a short
|
||||
|
||||
@@ -650,3 +650,21 @@ fn placed_bake_solves_the_quad_once() {
|
||||
so.map([1.0, 1.0], 20.0, 10.0);
|
||||
assert_eq!(counters::take(&QUAD), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_unpredict_keeps_tail_bytes_and_refuses_unbudgeted_scratch() {
|
||||
for depth in [8u16, 16, 32] {
|
||||
let p = Psd { depth, ..base() };
|
||||
let (w, h) = (3, 2);
|
||||
let row = w * usize::from(depth / 8);
|
||||
let mut actual: Vec<u8> = (0..row * 3 + 1).map(|i| (i * 31 % 251) as u8).collect();
|
||||
let mut expected = actual.clone();
|
||||
ref_unpredict(depth, &mut expected, w, h);
|
||||
p.unpredict(&mut actual, w, h, &mut Budget::new(u64::MAX)).unwrap();
|
||||
assert_eq!(actual, expected, "complete extra rows and partial tails stay untouched");
|
||||
}
|
||||
let p = Psd { depth: 32, ..base() };
|
||||
let mut bytes = vec![0x31; 24];
|
||||
assert!(p.unpredict(&mut bytes, 3, 2, &mut Budget::new(11)).is_err());
|
||||
assert_eq!(bytes, vec![0x31; 24], "budget refusal precedes mutation");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user