Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

Merge pull request #1137 from vangel76/psd-optimise

Brandon Thomasac9d4eb72fc9
diff --cc crates/psd/src/lib.rs
index 2fcd76e3c,eff3e1678..a2bf5893a
--- a/crates/psd/src/lib.rs
+++ b/crates/psd/src/lib.rs
@@@ -843,20 -799,22 +840,36 @@@ impl Psd
Ok(self.samples(&raw, w, h))
}
- /// The raw bytes (`row_bytes(w)` × `h`) of one channel whose pixel data follows `r`'s position
- /// (after the compression field). Uncompressed data is borrowed, not copied.
++ /// The raw bytes (`row_bytes(w)` × `h`, ZIP prediction undone) of one channel whose pixel data
++ /// follows `r`'s position (after the compression field). Uncompressed data is borrowed, not
++ /// copied.
+ fn plane_bytes<'a>(&self, comp: u16, r: &mut Reader<'a>, w: usize, h: usize) -> Result<Cow<'a, [u8]>> {
++ let rowb = self.row_bytes(w);
++ Ok(match self.open_planes(comp, r, 1, 1, w, h)? {
++ PlaneStream::Raw => Cow::Borrowed(r.bytes(rowb * h)?),
++ PlaneStream::Rle(counts) => Cow::Owned(Self::unpack_plane(&counts, 0, r, rowb, h)?),
++ PlaneStream::Zip(mut v) => {
++ v.resize(rowb * h, 0);
++ self.zip_plane(comp, &mut v, 0, w, h)?;
++ Cow::Owned(v)
++ }
++ })
++ }
++
+ /// Prepare the pixel data at `r` (just after the compression field) of `total` planes of
+ /// `w`×`h` samples, of which the first `want` are decoded: the RLE row sizes of every plane,
+ /// or the inflated ZIP data, which holds all planes one after another.
+ fn open_planes(&self, comp: u16, r: &mut Reader, total: usize, want: usize, w: usize, h: usize) -> Result<PlaneStream> {
let rowb = self.row_bytes(w);
Ok(match comp {
- 0 => Cow::Borrowed(r.bytes(rowb * h)?),
+ 0 => PlaneStream::Raw,
1 => {
- let mut counts = Vec::with_capacity(h.min(r.remaining() / 2));
- for _ in 0..h {
+ let n = total.saturating_mul(h);
+ let mut counts = Vec::with_capacity(n.min(r.remaining() / 2));
+ for _ in 0..n {
counts.push(if self.psb { r.u32()? as usize } else { r.u16()? as usize });
}
- let mut out = Vec::with_capacity(rowb * h);
- for c in counts {
- unpack_bits(r.bytes(c)?, rowb, &mut out)?;
- }
- Cow::Owned(out)
+ PlaneStream::Rle(counts)
}
2 | 3 => {
let rest = r.bytes(r.remaining())?;
@@@ -867,30 -829,16 +880,38 @@@
})
}
- /// The raw bytes (`row_bytes(w)` × `h`, ZIP prediction undone) of plane `c`; raw and RLE
- /// planes are read from `r` in order.
- fn decode_plane(&self, comp: u16, planes: &PlaneStream, r: &mut Reader, c: usize, w: usize, h: usize) -> Result<Vec<u8>> {
++ /// The raw bytes (`row_bytes(w)` × `h`, ZIP prediction undone) of plane `c`: raw and RLE
++ /// planes are read from `r` in order, raw ones borrowed from the file data, ZIP ones from the
++ /// inflated buffer, un-predicted in place.
++ fn decode_plane<'x, 'a: 'x>(&self, comp: u16, planes: &'x mut PlaneStream, r: &mut Reader<'a>, c: usize, w: usize, h: usize) -> Result<Cow<'x, [u8]>> {
+ let rowb = self.row_bytes(w);
+ Ok(match planes {
- PlaneStream::Raw => r.bytes(rowb * h)?.to_vec(),
- PlaneStream::Rle(counts) => {
- let mut out = Vec::with_capacity(rowb * h);
- for row in 0..h {
- let n = *counts.get(c * h + row).ok_or(Error::Truncated)?;
- unpack_bits(r.bytes(n)?, rowb, &mut out)?;
- }
- out
- }
- PlaneStream::Zip(v) => {
- let mut plane = v.get(c * rowb * h..(c + 1) * rowb * h).ok_or(Error::Truncated)?.to_vec();
- if comp == 3 {
- self.unpredict(&mut plane, w, h);
- }
- plane
- }
++ PlaneStream::Raw => Cow::Borrowed(r.bytes(rowb * h)?),
++ PlaneStream::Rle(counts) => Cow::Owned(Self::unpack_plane(counts, c, r, rowb, h)?),
++ PlaneStream::Zip(v) => Cow::Borrowed(self.zip_plane(comp, v, c, w, h)?),
+ })
+ }
+
++ /// Unpack the `h` RLE rows of plane `c` (row byte counts `counts`, plane after plane).
++ fn unpack_plane(counts: &[usize], c: usize, r: &mut Reader, rowb: usize, h: usize) -> Result<Vec<u8>> {
++ let mut out = Vec::with_capacity(rowb * h);
++ for row in 0..h {
++ let n = *counts.get(c * h + row).ok_or(Error::Truncated)?;
++ unpack_bits(r.bytes(n)?, rowb, &mut out)?;
++ }
++ Ok(out)
++ }
++
+ /// Plane `c` of the inflated ZIP data of consecutive planes, un-predicted in place (`comp` 3).
+ fn zip_plane<'v>(&self, comp: u16, v: &'v mut [u8], c: usize, w: usize, h: usize) -> Result<&'v [u8]> {
+ let rowb = self.row_bytes(w);
+ let plane = v.get_mut(c * rowb * h..(c + 1) * rowb * h).ok_or(Error::Truncated)?;
+ if comp == 3 {
+ self.unpredict(plane, w, h);
+ }
+ Ok(plane)
+ }
+
fn row_bytes(&self, w: usize) -> usize {
if self.depth == 1 { w.div_ceil(8) } else { w * self.bytes_per_sample() }
}
@@@ -998,10 -932,37 +1023,10 @@@
let nch = self.channels as usize;
let cc = self.mode.color_channels();
let want = (cc + usize::from(self.merged_alpha)).min(nch);
- let rowb = self.row_bytes(w);
let mut planes: Vec<Option<Vec<f32>>> = vec![None; nch];
- let stream = self.open_planes(comp, &mut r, nch, want, w, h)?;
- match comp {
- 0 => {
- for p in planes.iter_mut().take(want) {
- *p = Some(self.samples(r.bytes(rowb * h)?, w, h));
- }
- }
- 1 => {
- let mut counts = Vec::with_capacity((nch * h).min(r.remaining() / 2));
- for _ in 0..nch * h {
- counts.push(if self.psb { r.u32()? as usize } else { r.u16()? as usize });
- }
- for (c, p) in planes.iter_mut().enumerate().take(want) {
- let mut out = Vec::with_capacity(rowb * h);
- for row in 0..h {
- unpack_bits(r.bytes(counts[c * h + row])?, rowb, &mut out)?;
- }
- *p = Some(self.samples(&out, w, h));
- }
- }
- 2 | 3 => {
- let rest = r.bytes(r.remaining())?;
- let mut v =
- miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, want * rowb * h).map_err(|e| Error::Invalid(format!("zip: {e:?}")))?;
- // Each plane is un-predicted in place in the inflated buffer.
- for (c, p) in planes.iter_mut().enumerate().take(want) {
- *p = Some(self.samples(self.zip_plane(comp, &mut v, c, w, h)?, w, h));
- }
- }
- c => return Err(Error::Unsupported(format!("compression {c}"))),
++ let mut stream = self.open_planes(comp, &mut r, nch, want, w, h)?;
+ for (c, p) in planes.iter_mut().enumerate().take(want) {
- *p = Some(self.samples(&self.decode_plane(comp, &stream, &mut r, c, w, h)?, w, h));
++ *p = Some(self.samples(&self.decode_plane(comp, &mut stream, &mut r, c, w, h)?, w, h));
}
let alpha = if self.merged_alpha && nch > cc { planes[cc].take() } else { None };
let mut data = Vec::with_capacity(w * h);