Skip to content

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

Merge remote-tracking branch 'upstream/main' into abr-1gib-limit

Derek Gliwa4992a4305ee6
diff --cc crates/engine/src/preset_import_cmds.rs
index 2dcac20df,51ce9bf07..dfff9857d
--- a/crates/engine/src/preset_import_cmds.rs
+++ b/crates/engine/src/preset_import_cmds.rs
@@@ -533,32 -543,55 +550,77 @@@ mod tests
assert_eq!(s.tools.presets.len(), n, "a failed import adds nothing");
}
+ /// A version 1 brush with a computed tip and no name is named after its tip ("9 px Round"). The
+ /// import used to number each repeated name by searching the preset names once for every number
+ /// it tried, so importing 10,000 such brushes took minutes. The test fails when the import takes
+ /// more than 20 seconds.
+ #[test]
+ fn repeated_brush_names_are_numbered_in_linear_time() {
+ let (tx, rx) = std::sync::mpsc::channel();
+ std::thread::spawn(move || {
+ let mut s = Session::new();
+ s.execute("brush.presets.save", json!({"name": "9 PX ROUND 3"})).unwrap();
+ let round = LegacyBrush {
+ name: String::new(),
+ spacing: 25,
+ anti_alias: true,
+ tip: LegacyTip::Computed { diameter: 9, hardness: 100, angle: 0, roundness: 100 },
+ };
+ let data = photocraft_paint::tile::b64_encode(&write_v12(1, &vec![round; 10_000], false).unwrap());
+ let r = s.execute("brush.presets.importAbr", json!({"data": data})).unwrap();
+ let names: Vec<String> = r["imported"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_string()).collect();
+ let _ = tx.send(names);
+ });
+ let names = match rx.recv_timeout(std::time::Duration::from_secs(20)) {
+ Ok(names) => names,
+ Err(std::sync::mpsc::RecvTimeoutError::Timeout) => panic!("importing the brushes did not finish"),
+ Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => panic!("the import thread panicked"),
+ };
+ // Numbering starts at 2 and skips the number a preset already uses, in any letter case.
+ assert_eq!(names[..4], ["9 px Round", "9 px Round 2", "9 px Round 4", "9 px Round 5"]);
+ assert_eq!(names.last(), Some(&format!("9 px Round {}", names.len() + 1)));
+ assert_eq!(names.iter().collect::<std::collections::HashSet<_>>().len(), names.len());
+ }
+
+ #[test]
+ fn repeated_brush_names_take_the_first_free_number() {
+ let mut s = Session::new();
+ s.execute("brush.presets.save", json!({"name": "speckle 3"})).unwrap();
+ let tip = AbrSample { id: String::new(), width: 6, height: 4, depth: 8, data: vec![255; 24] };
+ let brush = |name: &str| LegacyBrush { name: name.into(), spacing: 25, anti_alias: true, tip: LegacyTip::Sampled(tip.clone()) };
+ let file = write_v12(2, &["Speckle", "SPECKLE", "speckle", "Speckle 2", "Speckle", "hard round"].map(brush), true).unwrap();
+ let r = s.execute("brush.presets.importAbr", json!({"data": photocraft_paint::tile::b64_encode(&file)})).unwrap();
+ // Names that differ only in letter case count as the same name. The file's "Speckle 2" comes
+ // after "SPECKLE" took that name, and "hard round" clashes with the built-in Hard Round.
+ assert_eq!(r["imported"], json!(["Speckle", "SPECKLE 2", "speckle 4", "Speckle 2 2", "Speckle 5", "hard round 2"]));
+ }
+
+ #[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);
+ 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");
+ std::fs::write(&path, [0u8; 10]).unwrap();
+ let p = json!({"path": path.to_string_lossy()});
+ assert!(file_bytes_within(&p, "t", "f", 10).is_ok());
+ assert!(file_bytes_within(&p, "t", "f", 9).is_err(), "a file over the cap is refused before reading");
+ // A sparse file between the two caps: refused as a .grd, which is checked before reading.
+ let big = dir.join("big.grd");
+ std::fs::File::create(&big).unwrap().set_len(MAX_FILE_BYTES + 1).unwrap();
+ let err = Session::new().execute("gradient.presets.importGrd", json!({"path": big.to_string_lossy()})).unwrap_err();
+ assert!(err.to_string().contains("too large"), "{err}");
+ let data = json!({"data": "A".repeat(16)});
+ assert!(file_bytes_within(&data, "t", "f", 12).is_ok());
+ assert!(file_bytes_within(&data, "t", "f", 8).is_err());
+ std::fs::remove_dir_all(&dir).ok();
+ }
+
#[test]
fn unique_names() {
- let taken = vec!["A".to_string(), "a 2".to_string()];
- assert_eq!(unique("a", &taken), "a 3");
- assert_eq!(unique("B", &taken), "B");
+ let mut taken = UniqueNames::new(["A", "a 2"]);
+ assert_eq!(taken.unique("a"), "a 3");
+ assert_eq!(taken.unique("B"), "B");
}
}