Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Brushes: keep the 512 MiB .abr cap on 32-bit targets

Derek Gliwad53101c9a148
diff --git a/crates/engine/src/preset_import_cmds.rs b/crates/engine/src/preset_import_cmds.rs
index dfff9857d..5e3e2cda9 100644
--- a/crates/engine/src/preset_import_cmds.rs
+++ b/crates/engine/src/preset_import_cmds.rs
@@ -28,7 +28,9 @@ fn always(_: &Session) -> std::result::Result<(), String> {
/// Largest accepted file (Photoshop's biggest bundled sets are well under this).
pub const MAX_FILE_BYTES: u64 = 512 << 20;
/// Largest accepted `.abr` file: brush libraries of large sampled tips outgrow [`MAX_FILE_BYTES`].
-pub const MAX_ABR_FILE_BYTES: u64 = 1 << 30;
+/// Tied to the decoder's sample budget (1 GiB, 512 MiB on 32-bit targets) so it never refuses
+/// what this accepts.
+pub const MAX_ABR_FILE_BYTES: u64 = photocraft_psd::abr::MAX_TOTAL_BYTES as u64;
/// File bytes from `data` (base64) or `path`, plus a default group name.
pub(crate) fn file_bytes(p: &Value, cmd: &str, what: &str) -> Result<(Vec<u8>, String)> {
@@ -597,8 +599,9 @@ mod tests {
#[test]
fn abr_files_get_a_larger_size_cap() {
- // The decoder must not refuse what the reader accepts.
- const _: () = assert!(MAX_ABR_FILE_BYTES == 1 << 30 && photocraft_psd::abr::MAX_TOTAL_BYTES as u64 == MAX_ABR_FILE_BYTES);
+ // 1 GiB on 64-bit; 32-bit (wasm) keeps 512 MiB so a big file fails cleanly instead of aborting.
+ const WANT: u64 = if cfg!(target_pointer_width = "64") { 1 << 30 } else { 512 << 20 };
+ const _: () = assert!(MAX_ABR_FILE_BYTES == WANT && MAX_ABR_FILE_BYTES >= MAX_FILE_BYTES);
let dir = std::env::temp_dir().join(format!("pc-abr-cap-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("cap.abr");
diff --git a/crates/io/examples/gen_abr_library.rs b/crates/io/examples/gen_abr_library.rs
index a72b5ed99..cbb2ad303 100644
--- a/crates/io/examples/gen_abr_library.rs
+++ b/crates/io/examples/gen_abr_library.rs
@@ -10,7 +10,7 @@
//!
//! Options (defaults in brackets): `--brushes N` [600], `--edge PX` largest tip edge [2000],
//! `--min-edge PX` smallest tip edge [800], `--depth 8|16` [16], `--per-file N` presets per file [100],
-//! `--raw` store tips uncompressed (bigger files). Each file stays under the reader's 1 GiB decoded cap.
+//! `--raw` store tips uncompressed (bigger files). Each file stays under the reader's decoded cap (1 GiB; 512 MiB on 32-bit).
#![allow(clippy::unwrap_used, clippy::expect_used)]
use photocraft_paint::procedural::*;
diff --git a/crates/psd/src/abr.rs b/crates/psd/src/abr.rs
index 025aad294..0a0a41ffb 100644
--- a/crates/psd/src/abr.rs
+++ b/crates/psd/src/abr.rs
@@ -43,8 +43,11 @@ use crate::patterns::{PatternBudget, PsdPattern, parse_pattern_block_with, write
pub const MAX_EDGE: u32 = 8192;
/// Most brushes read from one file.
pub const MAX_BRUSHES: usize = 20_000;
-/// Cap on decoded sample bytes over the whole file.
-pub const MAX_TOTAL_BYTES: usize = 1 << 30;
+/// Cap on decoded sample bytes over the whole file. 32-bit targets (wasm) keep 512 MiB: the file,
+/// its base64 copies and the decoded tips must all fit a 4 GiB address space, and running out
+/// there aborts instead of returning an error.
+pub const MAX_TOTAL_BYTES: usize = if cfg!(target_pointer_width = "64") { 1 << 30 } else { 512 << 20 };
+const _: () = assert!(cfg!(target_pointer_width = "64") || MAX_TOTAL_BYTES == 512 << 20);
/// A sampled tip: one grayscale plane where the maximum value is full paint.
#[derive(Debug, Clone, PartialEq)]