PSD: a memory budget for one decode and a capped smart-object bake (#1138)

The 2^28 pixel limit applied per image; one composite or layer decode kept
the ZIP output, a Vec<f32> per channel and the result alive together, and the
bake size came from the file's quad. A decode now charges every buffer
against a byte budget (4 GiB, 1 GiB on 32-bit), checks its whole expected peak
before allocating, writes channels straight into the result and returns an
error when over budget. Bakes are at most 4x the document across and 2^24
pixels.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76
2026-10-12 23:06:55 +02:00
co-authored by Claude Sonnet 5.5
parent 470ea7a3af
commit 4a08f8fdf4
4 changed files with 745 additions and 73 deletions
+288 -70
View File
@@ -56,6 +56,79 @@ fn decode_pixels(w: usize, h: usize) -> Result<usize> {
}
}
fn too_large(w: usize, h: usize) -> Error {
Error::Unsupported(format!("{w}×{h} pixels is too large to decode"))
}
/// What one decode (the composite, or one layer) may hold in memory at once: 4 GiB, 1 GiB where
/// pointers are 32 bits wide (WebAssembly). [`MAX_DECODE_PIXELS`] bounds one image; this bounds
/// the sum of the buffers a decode keeps alive together.
#[cfg(target_pointer_width = "32")]
const DECODE_BUDGET: u64 = 1 << 30;
#[cfg(not(target_pointer_width = "32"))]
const DECODE_BUDGET: u64 = 4 << 30;
/// The bytes the buffers of one decode hold right now, against a limit. A decode charges a buffer
/// before it allocates it (and releases it when it drops it), so a file that would need more
/// than the limit fails with an error instead of an out-of-memory abort.
struct Budget {
limit: u64,
live: u64,
}
impl Budget {
fn new(limit: u64) -> Budget {
Budget { limit, live: 0 }
}
fn charge(&mut self, bytes: u64) -> Result<()> {
self.check(bytes)?;
self.live += bytes;
Ok(())
}
/// Whether `bytes` more would still fit, without taking them. A decode checks its whole
/// expected peak first, so one that cannot fit fails before it has allocated anything.
fn check(&self, bytes: u64) -> Result<()> {
match self.live.checked_add(bytes) {
Some(total) if total <= self.limit => Ok(()),
_ => {
const MIB: u64 = 1 << 20;
Err(Error::Unsupported(format!(
"decoding needs {} MiB more with {} MiB in use, over the {} MiB memory budget",
bytes / MIB,
self.live / MIB,
self.limit / MIB
)))
}
}
}
fn release(&mut self, bytes: u64) {
self.live = self.live.saturating_sub(bytes);
}
/// An empty vector with room for `n` items, charged.
fn vec<T>(&mut self, n: usize) -> Result<Vec<T>> {
self.charge((n as u64).saturating_mul(std::mem::size_of::<T>() as u64))?;
let mut v = Vec::new();
v.try_reserve_exact(n).map_err(|_| Error::Unsupported(format!("out of memory for {n} items")))?;
Ok(v)
}
/// `n` copies of `value`, charged.
fn filled<T: Clone>(&mut self, n: usize, value: T) -> Result<Vec<T>> {
let mut v = self.vec::<T>(n)?;
v.resize(n, value);
Ok(v)
}
}
/// Inflate a zlib stream to at most `limit` bytes.
fn inflate(src: &[u8], limit: usize) -> Result<Vec<u8>> {
miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(src, limit).map_err(|e| Error::Invalid(format!("zip: {e:?}")))
}
/// Whether `bytes` start like a Photoshop document.
pub fn is_psd(bytes: &[u8]) -> bool {
bytes.len() >= 6 && &bytes[..4] == b"8BPS" && matches!(u16::from_be_bytes([bytes[4], bytes[5]]), 1 | 2)
@@ -788,38 +861,59 @@ impl Psd {
}
}
/// Decode one channel of `w`×`h` samples at `offset` (compression field first). Values 0..1.
fn decode_channel(&self, offset: usize, len: usize, w: usize, h: usize) -> Result<Vec<f32>> {
/// The raw big-endian bytes of one channel of `w`×`h` samples at `offset` (compression field
/// first): `row_bytes(w) × h` of them with the compression and the prediction undone. The
/// buffer is charged to `budget`; the caller releases it once it is dropped.
fn channel_bytes(&self, offset: usize, len: usize, w: usize, h: usize, budget: &mut Budget) -> Result<Vec<u8>> {
let end = offset.checked_add(len).filter(|&e| e <= self.data.len()).ok_or(Error::Truncated)?;
let mut r = Reader::at(&self.data[..end], offset);
let mut r = Reader::at(self.data.get(..end).ok_or(Error::Truncated)?, offset);
let comp = r.u16()?;
decode_pixels(w, h)?;
let rowb = self.row_bytes(w);
let raw = match comp {
0 => r.bytes(rowb * h)?.to_vec(),
let size = rowb.checked_mul(h).ok_or_else(|| too_large(w, h))?;
match comp {
0 => {
let src = r.bytes(size)?;
let mut v = budget.vec::<u8>(size)?;
v.extend_from_slice(src);
Ok(v)
}
1 => {
let mut counts = Vec::with_capacity(h.min(r.remaining() / 2));
for _ in 0..h {
counts.push(if self.psb { r.u32()? as usize } else { r.u16()? as usize });
}
let mut out = Vec::with_capacity(rowb * h);
let mut out = budget.vec::<u8>(size)?;
for c in counts {
unpack_bits(r.bytes(c)?, rowb, &mut out)?;
}
out
Ok(out)
}
2 | 3 => {
let rest = r.bytes(r.remaining())?;
let mut v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, rowb * h).map_err(|e| Error::Invalid(format!("zip: {e:?}")))?;
v.resize(rowb * h, 0);
budget.charge(size as u64)?;
let mut v = inflate(rest, size)?;
v.resize(size, 0);
if comp == 3 {
self.unpredict(&mut v, w, h);
}
v
Ok(v)
}
c => return Err(Error::Unsupported(format!("compression {c}"))),
};
Ok(self.samples(&raw, w, h))
c => Err(Error::Unsupported(format!("compression {c}"))),
}
}
/// One channel as `w * h` floats (0..1), charged to `budget` (the caller releases it).
fn channel_floats(&self, offset: usize, len: usize, w: usize, h: usize, budget: &mut Budget) -> Result<Vec<f32>> {
let raw = self.channel_bytes(offset, len, w, h, budget)?;
let mut out = budget.filled(decode_pixels(w, h)?, 0.0f32)?;
self.for_each_sample(&raw, w, h, |i, v| {
if let Some(o) = out.get_mut(i) {
*o = v;
}
});
budget.release(raw.len() as u64);
Ok(out)
}
fn row_bytes(&self, w: usize) -> usize {
@@ -862,29 +956,30 @@ impl Psd {
}
}
fn samples(&self, raw: &[u8], w: usize, h: usize) -> Vec<f32> {
/// Hand every sample of `raw` (`w`×`h`, row-major, in order) to `f` as a float. Samples a short
/// `raw` does not have are not reported.
fn for_each_sample(&self, raw: &[u8], w: usize, h: usize, mut f: impl FnMut(usize, f32)) {
let n = w.saturating_mul(h);
match self.depth {
1 => {
let rowb = w.div_ceil(8);
let mut out = Vec::with_capacity(w * h);
for y in 0..h {
for x in 0..w {
let b = raw.get(y * rowb + x / 8).copied().unwrap_or(0);
// 1 = black.
out.push(if b & (0x80 >> (x % 8)) != 0 { 0.0 } else { 1.0 });
f(y * w + x, if b & (0x80 >> (x % 8)) != 0 { 0.0 } else { 1.0 });
}
}
out
}
16 => raw.as_chunks::<2>().0.iter().take(w * h).map(|c| u16::from_be_bytes([c[0], c[1]]) as f32 / 65535.0).collect(),
32 => raw.as_chunks::<4>().0.iter().take(w * h).map(|c| f32::from_be_bytes([c[0], c[1], c[2], c[3]])).collect(),
_ => raw.iter().take(w * h).map(|&b| b as f32 / 255.0).collect(),
16 => raw.as_chunks::<2>().0.iter().take(n).enumerate().for_each(|(i, c)| f(i, u16::from_be_bytes([c[0], c[1]]) as f32 / 65535.0)),
32 => raw.as_chunks::<4>().0.iter().take(n).enumerate().for_each(|(i, c)| f(i, f32::from_be_bytes([c[0], c[1], c[2], c[3]]))),
_ => raw.iter().take(n).enumerate().for_each(|(i, &b)| f(i, b as f32 / 255.0)),
}
}
/// Convert colour channels (in the document's mode) to RGB.
fn to_rgb(&self, ch: &[Option<Vec<f32>>], i: usize) -> [f32; 3] {
let g = |k: usize| ch.get(k).and_then(|c| c.as_ref()).map_or(0.0, |c| c[i]);
/// Convert colour values (the document mode's channels, missing ones 0) to RGB.
fn rgb_from(&self, v: [f32; 4]) -> [f32; 3] {
let g = |k: usize| v.get(k).copied().unwrap_or(0.0);
let rgb = match self.mode {
ColorMode::Rgb => [g(0), g(1), g(2)],
ColorMode::Cmyk => {
@@ -902,55 +997,101 @@ impl Psd {
if self.depth == 32 && self.mode != ColorMode::Lab { rgb.map(srgb_encode) } else { rgb }
}
/// Write merged-image channel `c` (raw bytes) into the result: the first three colour channels
/// and the alpha channel into their slot of a pixel, the fourth CMYK channel into `black`.
fn store_merged_channel(&self, c: usize, raw: &[u8], data: &mut [[f32; 4]], black: &mut Option<Vec<f32>>) {
let (w, h, cc) = (self.width as usize, self.height as usize, self.mode.color_channels());
if c < cc.min(3) {
self.for_each_sample(raw, w, h, |i, v| {
if let Some(p) = data.get_mut(i) {
p[c] = v;
}
});
} else if c == 3 && c < cc {
if let Some(k) = black {
self.for_each_sample(raw, w, h, |i, v| {
if let Some(o) = k.get_mut(i) {
*o = v;
}
});
}
} else if c == cc && self.merged_alpha {
self.for_each_sample(raw, w, h, |i, v| {
if let Some(p) = data.get_mut(i) {
p[3] = v;
}
});
}
}
/// The merged (flattened) image.
pub fn composite(&self) -> Result<Pixels> {
self.composite_with_budget(DECODE_BUDGET)
}
/// [`Self::composite`] with at most `limit` bytes live at once. The channels are written
/// straight into the result, so the peak is the result (16 bytes a pixel), the CMYK black
/// channel, and the one buffer a channel (or, for ZIP, all channels) decodes through.
pub(crate) fn composite_with_budget(&self, limit: u64) -> Result<Pixels> {
let mut budget = Budget::new(limit);
let (w, h) = (self.width as usize, self.height as usize);
decode_pixels(w, h)?;
let n = decode_pixels(w, h)?;
let mut r = Reader::at(&self.data, self.merged_offset);
let comp = r.u16()?;
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 size = rowb.checked_mul(h).ok_or_else(|| too_large(w, h))?;
// The peak: the result, the CMYK black channel and what the channels decode through.
let black_bytes = if self.mode == ColorMode::Cmyk && want > 3 { 4 * n as u64 } else { 0 };
let transient = if matches!(comp, 2 | 3) { (size as u64).saturating_mul(want as u64) } else { size as u64 };
budget.check((16 * n as u64).saturating_add(black_bytes).saturating_add(transient))?;
// Opaque black until a channel says otherwise.
let mut data = budget.filled(n, [0.0f32, 0.0, 0.0, 1.0])?;
let mut black = if self.mode == ColorMode::Cmyk && want > 3 { Some(budget.filled(n, 0.0f32)?) } else { None };
match comp {
0 => {
for p in planes.iter_mut().take(want) {
*p = Some(self.samples(r.bytes(rowb * h)?, w, h));
for c in 0..want {
self.store_merged_channel(c, r.bytes(size)?, &mut data, &mut black);
}
}
1 => {
let mut counts = Vec::with_capacity((nch * h).min(r.remaining() / 2));
for _ in 0..nch * h {
let total = nch.saturating_mul(h);
let mut counts = Vec::with_capacity(total.min(r.remaining() / 2));
for _ in 0..total {
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 c in 0..want {
let mut out = budget.vec::<u8>(size)?;
for row in 0..h {
unpack_bits(r.bytes(counts[c * h + row])?, rowb, &mut out)?;
let count = *counts.get(c * h + row).ok_or(Error::Truncated)?;
unpack_bits(r.bytes(count)?, rowb, &mut out)?;
}
*p = Some(self.samples(&out, w, h));
self.store_merged_channel(c, &out, &mut data, &mut black);
budget.release(size as u64);
}
}
2 | 3 => {
let rest = r.bytes(r.remaining())?;
let v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, want * rowb * h).map_err(|e| Error::Invalid(format!("zip: {e:?}")))?;
for (c, p) in planes.iter_mut().enumerate().take(want) {
let mut plane = v.get(c * rowb * h..(c + 1) * rowb * h).ok_or(Error::Truncated)?.to_vec();
// One stream holds every channel: what it may decode to is what they need.
let total = size.checked_mul(want).ok_or_else(|| too_large(w, h))?;
budget.charge(total as u64)?;
let mut v = inflate(rest, total)?;
for c in 0..want {
let plane = v.get_mut(c * size..(c + 1) * size).ok_or(Error::Truncated)?;
if comp == 3 {
self.unpredict(&mut plane, w, h);
self.unpredict(plane, w, h);
}
*p = Some(self.samples(&plane, w, h));
self.store_merged_channel(c, plane, &mut data, &mut black);
}
}
c => return Err(Error::Unsupported(format!("compression {c}"))),
}
let alpha = if self.merged_alpha && nch > cc { planes[cc].take() } else { None };
let mut data = Vec::with_capacity(w * h);
for i in 0..w * h {
let [r, g, b] = self.to_rgb(&planes, i);
let a = alpha.as_ref().map_or(1.0, |a| a[i]);
data.push([r, g, b, a]);
for (i, p) in data.iter_mut().enumerate() {
let k = black.as_ref().and_then(|k| k.get(i)).copied().unwrap_or(0.0);
let [r, g, b] = self.rgb_from([p[0], p[1], p[2], k]);
*p = [r, g, b, p[3]];
}
Ok(Pixels { width: w as u32, height: h as u32, data })
}
@@ -958,6 +1099,14 @@ impl Psd {
/// One layer's pixels with its layer mask (and vector-less fill colour) applied to alpha.
/// `canvas`: document-sized with the layer in place; otherwise the layer's own bounds.
pub fn layer_pixels(&self, index: usize, canvas: bool) -> Result<Pixels> {
self.layer_pixels_with_budget(index, canvas, DECODE_BUDGET)
}
/// [`Self::layer_pixels`] with at most `limit` bytes live at once: the result (16 bytes a
/// pixel) with the channels written straight into it, the CMYK black channel, the one channel
/// being decoded, and the mask.
pub(crate) fn layer_pixels_with_budget(&self, index: usize, canvas: bool, limit: u64) -> Result<Pixels> {
let mut budget = Budget::new(limit);
let l = self.layers.get(index).ok_or_else(|| Error::Invalid(format!("no layer {index}")))?;
let doc = Rect { top: 0, left: 0, bottom: self.height as i32, right: self.width as i32 };
// Fill layers often have no pixel data: they cover the canvas (or the mask).
@@ -971,22 +1120,26 @@ impl Psd {
fill_rect
};
let (ow, oh) = (out_rect.width() as usize, out_rect.height() as usize);
let mut out = vec![[0.0f32; 4]; decode_pixels(ow, oh)?];
let n = decode_pixels(ow, oh)?;
let src = fill_rect;
let (sw, sh) = (src.width() as usize, src.height() as usize);
if sw > 0 && sh > 0 {
let mut planes: Vec<Option<Vec<f32>>> = vec![None; self.mode.color_channels()];
let mut alpha = None;
if l.has_pixels() {
for c in &l.channels {
match c.id {
-1 => alpha = Some(self.decode_channel(c.offset, c.len, sw, sh)?),
id if id >= 0 && (id as usize) < planes.len() => planes[id as usize] = Some(self.decode_channel(c.offset, c.len, sw, sh)?),
_ => {}
}
}
// The peak: the result, then either the CMYK black channel with the channel being decoded,
// or the mask as floats with its bytes. Checked before anything is allocated.
let plane = |w: usize, h: usize| (self.row_bytes(w) as u64).saturating_mul(h as u64);
let mut pixels_peak = 0u64;
if l.has_pixels() && sw > 0 && sh > 0 {
pixels_peak = plane(sw, sh).saturating_add(if self.mode == ColorMode::Cmyk { 4 * n as u64 } else { 0 });
}
let mut mask_peak = 0u64;
if let Some(m) = l.mask.filter(|m| !m.disabled) {
let (mw, mh) = (m.rect.width() as usize, m.rect.height() as usize);
if mw > 0 && mh > 0 && l.channels.iter().any(|c| c.id == -2) {
mask_peak = plane(mw, mh).saturating_add((mw as u64).saturating_mul(mh as u64).saturating_mul(4));
}
let fill = l.fill.map(|c| c.map(|v| v as f32));
}
budget.check((16 * n as u64).saturating_add(pixels_peak.max(mask_peak)))?;
let mut out = budget.filled(n, [0.0f32; 4])?;
if sw > 0 && sh > 0 {
// The source rows / columns that land inside the output (64-bit: file bounds can span
// the whole i32 range).
let span = |s0: i32, n: usize, o0: i32, on: usize| {
@@ -997,16 +1150,77 @@ impl Psd {
};
let (ys, oy) = span(src.top, sh, out_rect.top, oh);
let (xs, ox) = span(src.left, sw, out_rect.left, ow);
for y in ys {
let dy = (y as i64 - oy) as usize;
for x in xs.clone() {
let dx = (x as i64 - ox) as usize;
let i = y * sw + x;
let (rgb, a) = match fill {
Some(f) if !l.has_pixels() => (f, 1.0),
_ => (self.to_rgb(&planes, i), alpha.as_ref().map_or(1.0, |a| a[i])),
};
out[dy * ow + dx] = [rgb[0], rgb[1], rgb[2], a];
let dest = |y: usize, x: usize| (y as i64 - oy) as usize * ow + (x as i64 - ox) as usize;
let fill = l.fill.map(|c| c.map(|v| v as f32));
match fill {
Some(f) if !l.has_pixels() => {
for y in ys {
for x in xs.clone() {
if let Some(p) = out.get_mut(dest(y, x)) {
*p = [f[0], f[1], f[2], 1.0];
}
}
}
}
_ => {
// Opaque unless there is an alpha channel.
for y in ys.clone() {
for x in xs.clone() {
if let Some(p) = out.get_mut(dest(y, x)) {
p[3] = 1.0;
}
}
}
let cc = self.mode.color_channels();
let mut black: Option<Vec<f32>> = None;
if l.has_pixels() {
for c in &l.channels {
let slot = match c.id {
-1 => 3,
id if id >= 0 && (id as usize) < cc => id as usize,
_ => continue,
};
let raw = self.channel_bytes(c.offset, c.len, sw, sh, &mut budget)?;
// The fourth CMYK channel is the black plate, not a slot of the result.
let to_black = c.id == 3;
if to_black && black.is_none() {
black = Some(budget.filled(out.len(), 0.0f32)?);
}
// Samples arrive in row-major order of the source.
let (mut sy, mut sx) = (0usize, 0usize);
self.for_each_sample(&raw, sw, sh, |_, v| {
if ys.contains(&sy) && xs.contains(&sx) {
let d = dest(sy, sx);
if to_black {
if let Some(o) = black.as_mut().and_then(|k| k.get_mut(d)) {
*o = v;
}
} else if let Some(p) = out.get_mut(d) {
p[slot] = v;
}
}
sx += 1;
if sx == sw {
sx = 0;
sy += 1;
}
});
budget.release(raw.len() as u64);
}
}
for y in ys {
for x in xs.clone() {
let d = dest(y, x);
let k = black.as_ref().and_then(|k| k.get(d)).copied().unwrap_or(0.0);
if let Some(p) = out.get_mut(d) {
let [r, g, b] = self.rgb_from([p[0], p[1], p[2], k]);
*p = [r, g, b, p[3]];
}
}
}
if let Some(k) = black {
budget.release(k.len() as u64 * 4);
}
}
}
}
@@ -1015,7 +1229,7 @@ impl Psd {
let mc = l.channels.iter().find(|c| c.id == -2);
let (mw, mh) = (m.rect.width() as usize, m.rect.height() as usize);
let mask = match mc {
Some(c) if mw > 0 && mh > 0 => Some(self.decode_channel(c.offset, c.len, mw, mh)?),
Some(c) if mw > 0 && mh > 0 => Some(self.channel_floats(c.offset, c.len, mw, mh, &mut budget)?),
_ => None,
};
let def = m.default_color as f32 / 255.0;
@@ -1025,13 +1239,15 @@ impl Psd {
let px = out_rect.left + x as i32;
let inside = py >= m.rect.top && py < m.rect.bottom && px >= m.rect.left && px < m.rect.right;
let mut v = match (&mask, inside) {
(Some(mk), true) => mk[(py - m.rect.top) as usize * mw + (px - m.rect.left) as usize],
(Some(mk), true) => mk.get((py - m.rect.top) as usize * mw + (px - m.rect.left) as usize).copied().unwrap_or(def),
_ => def,
};
if m.inverted {
v = 1.0 - v;
}
out[y * ow + x][3] *= v;
if let Some(p) = out.get_mut(y * ow + x) {
p[3] *= v;
}
}
}
}
@@ -1147,5 +1363,7 @@ fn lab_to_rgb(l: f32, a: f32, b: f32) -> [f32; 3] {
[r, g, bb].map(|v| srgb_encode(v.clamp(0.0, 1.0)))
}
#[cfg(test)]
mod memory_tests;
#[cfg(test)]
mod tests;