diff --git a/crates/psd/src/file.rs b/crates/psd/src/file.rs index 1e2c60f827..babcef4f58 100644 --- a/crates/psd/src/file.rs +++ b/crates/psd/src/file.rs @@ -382,10 +382,9 @@ impl PsdFile { o.put_u16(self.image_data.compression.as_u16()); Ok(()) })?; - let merged = self.image_data.encoded()?; match self.merged_widened_rows(widen)? { - 0 => out.put(&merged), - rows => out.put_widened_rle(&merged, rows), + 0 => out.put_encoded(&self.image_data.data, self.image_data.stored.as_ref()), + rows => out.put_widened_rle(&self.image_data.encoded()?, rows), } } @@ -719,6 +718,35 @@ mod write_tests { } } + /// Channel data and a merged image still in their source (an imported file's, or what an + /// exporter spilled to scratch) are copied through in short reads, never read whole. + #[test] + fn source_backed_data_is_copied_in_short_reads() { + struct Reads(Vec, std::sync::Mutex); + impl crate::source::ReadAt for Reads { + fn len(&self) -> u64 { + self.0.len() as u64 + } + fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> { + let mut most = self.1.lock().unwrap(); + *most = (*most).max(buf.len()); + self.0.as_slice().read_at(offset, buf) + } + } + let mut file = testgen::small(Version::Psd, Compression::Raw); + file.image_data = ImageData::new(Compression::Raw, vec![5; 300]); + let info = file.layer_info.as_mut().unwrap(); + info.layers[0].channels[0] = crate::layer::ChannelData::new(0, Some(Compression::Raw), vec![6; 301]); + let bytes = file.to_bytes().unwrap(); + let source = std::sync::Arc::new(Reads(bytes.clone(), std::sync::Mutex::new(0))); + let lazy = PsdFile::from_source(crate::Source::from_arc(source.clone())).unwrap(); + *source.1.lock().unwrap() = 0; + let mut w = std::io::Cursor::new(Vec::new()); + lazy.write_to(&mut w).unwrap(); + assert!(w.into_inner() == bytes); + assert!(*source.1.lock().unwrap() <= 7, "read {} bytes at once", source.1.lock().unwrap()); + } + /// #6607: pixel data goes to the stream as it is, never through a staging copy, so a save /// holds the model and a short buffer, not a second copy of the file. #[test] diff --git a/crates/psd/src/io.rs b/crates/psd/src/io.rs index eb7185bde2..bf4468711a 100644 --- a/crates/psd/src/io.rs +++ b/crates/psd/src/io.rs @@ -213,6 +213,8 @@ impl WriteExt for Vec { /// Staged bytes [`Sink`] collects before writing them out in one call. const STAGE: usize = 64 << 10; +/// Bytes of stored channel data read per piece by [`Sink::put_encoded`]. +const COPY_CHUNK: u64 = if cfg!(test) { 7 } else { 8 << 20 }; #[cfg(test)] pub(crate) const STAGE_FOR_TESTS: usize = STAGE; @@ -283,6 +285,21 @@ impl<'w, W: Write + Seek + ?Sized> Sink<'w, W> { self.put(packed) } + /// Appends encoded bytes held in memory (`data`) or, when `stored`, still in their source: + /// those are copied through in [`COPY_CHUNK`] pieces, so a channel or merged image kept on + /// disk (an imported file's, or one an exporter spilled to scratch) is never read whole. + pub(crate) fn put_encoded(&mut self, data: &[u8], stored: Option<&crate::source::Stored>) -> std::io::Result<()> { + let Some(s) = stored else { return self.put(data) }; + let mut at = 0u64; + while at < s.len { + let n = (s.len - at).min(COPY_CHUNK); + let part = s.read_part(at, n)?; + self.put(&part)?; + at += n; + } + Ok(()) + } + /// `n` zero bytes (padding, at most a few). pub(crate) fn zeros(&mut self, n: usize) -> std::io::Result<()> { self.small(|b| { diff --git a/crates/psd/src/layer.rs b/crates/psd/src/layer.rs index fc1a3ff571..3b492e6a04 100644 --- a/crates/psd/src/layer.rs +++ b/crates/psd/src/layer.rs @@ -858,10 +858,9 @@ impl LayerInfo { o.put_u16(comp.as_u16()); Ok(()) })?; - let data = c.encoded()?; match l.widened_rows(c, widen)? { - 0 => out.put(&data)?, - rows => out.put_widened_rle(&data, rows)?, + 0 => out.put_encoded(&c.data, c.stored.as_ref())?, + rows => out.put_widened_rle(&c.encoded()?, rows)?, } } }