mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-18 02:32:57 +00:00
Merge origin/main into feat/jpeg-options-size-readout
Conflicts: the i18n catalogs keep both main's new rows and the JPEG size row; the parity docs take main's status lines plus this PR's JPEG clause; the JPEG Options module doc keeps both the size readout and main's remembered settings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -105,6 +105,7 @@ The other built-in ICC profiles and the generated LUT looks are produced by code
|
||||
| `docs/brand/` (all files) | ArtCraft name, wordmark and mark | ArtCraft Team | getartcraft.com | Not open source; trademarks of the ArtCraft Team, [`docs/brand/LICENSE-brand.txt`](docs/brand/LICENSE-brand.txt) |
|
||||
| `docs/images/photocraft-*.jpg` | PhotoCraft screenshots | PhotoCraft contributors (UI) | Rendered offscreen with the `snapshot` example | MIT OR Apache-2.0 (UI); the artwork in each is public domain, listed below |
|
||||
| `docs/images/preferences-apply-*.png` | Preferences before and after adding Apply (no artwork) | PhotoCraft contributors | PhotoCraft control-channel capture and offscreen `snapshot` example | MIT OR Apache-2.0 |
|
||||
| `docs/images/guide-position-*-tab.png` | New Guide before and after focusing Position with Tab (no artwork) | PhotoCraft contributors | Offscreen `snapshot` example with `ui.key Tab` | MIT OR Apache-2.0 |
|
||||
| `docs/images/embedded-vector-editor-en.png` | Embedded AI/PDF/EPS conversion warning and editor chooser (no artwork) | Don Omondi | Offscreen `smart_svg_ui::tests::capture_embedded_vector_conversion_prompt` with authored bytes and mock services | MIT OR Apache-2.0 |
|
||||
| `docs/images/embedded-vector-tabs-linux.png` | Shared VectorCraft window after a portal editor save and ordinary document launch | Don Omondi | Local Linux GUI acceptance with authored orange rectangle/blue circle SVGs; no private artwork | MIT OR Apache-2.0 |
|
||||
| `docs/images/gradient-overlay-method-*.png` | Gradient Overlay Method selector, before and after (synthetic gradient) | @oRE-o | Rendered offscreen with the `snapshot` example; no external artwork | MIT OR Apache-2.0 |
|
||||
|
||||
Generated
+1
@@ -5247,6 +5247,7 @@ dependencies = [
|
||||
"photocraft-engine",
|
||||
"photocraft-format",
|
||||
"photocraft-io",
|
||||
"photocraft-psd",
|
||||
"photocraft-raster",
|
||||
"photocraft-raw",
|
||||
"serde",
|
||||
|
||||
+4
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
**Stage: alpha** · next: beta, ~30 points (ready for real work ~45% → ~75%, with reliable PSD) and ~1,100–1,800 h away
|
||||
|
||||
> **Last reviewed:** 2026-10-17 · **Last updated:** 2026-10-17 · **Change:** minor (Object Finder retains large enclosed foregrounds (#6548); Russian catalog covers every dynamic label; previously minor: oracle floor 145 of 258 and 953 open issues, as in gaps.md and on GitHub; toolbox count 56 of 68, 12 missing) · **Target:** Adobe Photoshop 2026
|
||||
> **Last reviewed:** 2026-10-17 · **Last updated:** 2026-10-22 · **Change:** minor (Print: Japanese paper sizes — はがき, 往復はがき, JIS B5/B4, A6, L判, 2L判, KG判, 六切, 四切; previously: Object Finder retains large enclosed foregrounds (#6548); Russian catalog covers every dynamic label; previously minor: oracle floor 145 of 258 and 953 open issues, as in gaps.md and on GitHub; toolbox count 56 of 68, 12 missing) · **Target:** Adobe Photoshop 2026
|
||||
|
||||
PhotoCraft's one-page summary of how close it is to Adobe Photoshop 2026 (27.11.0, measured from
|
||||
the installed app on 2026-10-10) at version 0.6.0. Detail: [target-app parity](docs/target-app-parity.md)
|
||||
@@ -122,8 +122,10 @@ Detail and the next releases: [docs/roadmap.md](docs/roadmap.md).
|
||||
|
||||
| Date | Entry |
|
||||
|---|---|
|
||||
| 2026-11-02 | Layer Style: with Gradient Overlay selected, a drag on the image moves the gradient (its centre as a fraction of the layer's bounds, `params.offset`), as a drag moves a shadow (#10763) |
|
||||
| 2026-11-02 | Load Actions: Mac Roman step names with é ä ö ü Ä or ° read as Mac Roman instead of windows-1252 or Cyrillic (#10774): windows-1252's rare 0x80–0x9F letters and a lone Cyrillic letter in a Latin word no longer count as common text, the degree sign does, and the file's own Unicode text settles a word-initial rare letter and breaks exact ties toward windows-1252 or Mac Roman |
|
||||
| 2026-10-21 | Linux can use font-free Adwaita controls when System Title Bar is selected, while the integrated CSD title bar remains the default (as in #4439). Saved preferences are respected; startup and preference-dialog regressions cover both choices (#6266). |
|
||||
| 2026-10-22 | Print paper sizes: はがき (100 × 148 mm), 往復はがき (148 × 200), JIS B5 (182 × 257), JIS B4 (257 × 364), A6, L判 (89 × 127), 2L判 (127 × 178), KG判 (102 × 152), 六切 (203 × 254) and 四切 (254 × 305) are selectable in the Print dialog and `file.print`, matched by their customary names (`はがき`, `L判`, `JIS B5`…), and the PDF page takes their exact size. Existing papers unchanged. |
|
||||
| 2026-10-17 | Object Finder classifies background by boundary coverage, retaining large enclosed foregrounds and rejecting surrounding backdrop (#6548); learned segmentation remains open (#2788) |
|
||||
| 2026-10-10 | Full re-measure against Photoshop 2026 27.11.0 (installed bundle inspected): breadth ~76%, ready ~45%, alpha. Progress docs reorganized (`parity.md` → `parity-checklist.md`, `parity-estimate.md` merged into `target-app-parity.md`). 0.6.0 released. |
|
||||
| 2026-10-09 | Remove tool, Rotate View, crop rotation; OpenRaster and Paint.NET; 39 Affinity documents in the corpus |
|
||||
@@ -138,6 +140,7 @@ Detail and the next releases: [docs/roadmap.md](docs/roadmap.md).
|
||||
|
||||
| Date | Change | Summary |
|
||||
|---|---|---|
|
||||
| 2026-10-22 | minor | Print: Japanese paper sizes (はがき, 往復はがき, JIS B5/B4, A6, L判, 2L判, KG判, 六切, 四切) in the Print dialog, `file.print` and the PDF output |
|
||||
| 2026-10-17 | minor | Object Finder retains large enclosed foregrounds (#6548) |
|
||||
| 2026-10-13 | minor | Focus item 1 quoted the oracle floor as 133 of 256; it is 145 of 258 (`crates/io/tests/it/corpus.rs`, gaps.md G1). Open issues 579 → 953 (2026-10-13) |
|
||||
| 2026-10-15 | minor | Russian catalog covers every dynamic UI label the other catalogs translate |
|
||||
|
||||
@@ -45,6 +45,8 @@ tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
|
||||
[dev-dependencies]
|
||||
serde = { workspace = true }
|
||||
tempfile = "3"
|
||||
# Reading back a saved PSD's merged-image flag (#8064).
|
||||
photocraft-psd = { workspace = true }
|
||||
# Synthetic camera raws (#7789).
|
||||
photocraft-raw = { workspace = true, features = ["testgen"] }
|
||||
# Shell completions and man pages (examples/man-and-completions.rs, for the packages); not in
|
||||
|
||||
@@ -87,6 +87,12 @@ pub(crate) struct ExportArgs {
|
||||
/// composite/full.png; it may be omitted. Check the manifest's full_composite member.
|
||||
#[arg(long)]
|
||||
pub(crate) pcraft_full_composite: bool,
|
||||
/// A layered PSD/PSB stores Photoshop's white placeholder instead of the merged image
|
||||
/// (Maximize Compatibility off): a smaller file and a faster save, but readers that show only
|
||||
/// the merged image see white. Bitmap, Indexed Color and Duotone documents still write the
|
||||
/// real image
|
||||
#[arg(long)]
|
||||
pub(crate) no_maximize_compatibility: bool,
|
||||
}
|
||||
|
||||
/// Convert between formats (.pcraft, .psd, .png, .jpg, .tif, .webp, .exr, …)
|
||||
|
||||
@@ -33,6 +33,7 @@ impl From<&ExportArgs> for ExportOptions {
|
||||
o.encode.dds.compression = c;
|
||||
}
|
||||
o.encode.dds.mipmaps = a.dds_mipmaps;
|
||||
o.psd_maximize_compatibility = !a.no_maximize_compatibility;
|
||||
o
|
||||
}
|
||||
}
|
||||
|
||||
@@ -867,6 +867,24 @@ fn dds_converts_both_ways_with_the_compression_asked_for() {
|
||||
}
|
||||
}
|
||||
|
||||
/// #8064: `--no-maximize-compatibility` writes a layered PSD/PSB with Photoshop's placeholder
|
||||
/// instead of the merged image, flagged in resource 1057; without it, the real image.
|
||||
#[test]
|
||||
fn no_maximize_compatibility_writes_the_psd_placeholder() {
|
||||
let d = tmp("maximize-compat");
|
||||
let a = d.join("a.png");
|
||||
write_png(&a, 32, 16, 3);
|
||||
for (args, ext, real) in
|
||||
[(&[][..], "psd", true), (&["--no-maximize-compatibility"][..], "psd", false), (&["--no-maximize-compatibility"][..], "psb", false)]
|
||||
{
|
||||
let out = d.join(format!("a.{ext}"));
|
||||
ok(bin().arg("run").arg(&a).args(["--cmd", "layer.new.layer"]).arg("--out").arg(&out).args(args));
|
||||
let psd = photocraft_psd::PsdFile::from_bytes(&std::fs::read(&out).unwrap()).unwrap();
|
||||
assert_eq!(psd.has_real_merged_data(), Some(real), "{args:?} {ext}");
|
||||
}
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
/// `--tga-bits` picks a TGA's bits per pixel (byte 16 of its header); anything but 24 or 32 is a
|
||||
/// usage error and nothing is written.
|
||||
#[test]
|
||||
|
||||
@@ -681,6 +681,7 @@ fn main() -> eframe::Result {
|
||||
#[cfg(target_os = "linux")]
|
||||
tablet::spawn_x11(&app.stylus.feed, display);
|
||||
app.services.right_button_down = tablet::right_button_service();
|
||||
app.services.pen_end = tablet::pen_end_service();
|
||||
// Paths on the command line (Linux/Windows file associations, `photocraft a.psd`).
|
||||
app.open_paths(&files);
|
||||
if !unreadable_paths.is_empty() {
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
//! Pen tablet pressure, tilt, rotation and eraser end on macOS and Linux X11 (issue #79), and
|
||||
//! barrel button on Windows (#5905).
|
||||
//! barrel button (#5905) and eraser end (#2285) on Windows.
|
||||
//!
|
||||
//! winit 0.30 drops tablet data on both macOS and X11, so `photocraft-tablet` reads it beside winit
|
||||
//! (an AppKit local event monitor; XInput2 raw events on a second X connection) and this module writes
|
||||
//! each sample into the UI's [`StylusFeed`], where the canvas reads it exactly like the web runner's
|
||||
//! Pointer Events and automation's simulated pen. On Windows, winit forwards `WM_POINTER` pressure
|
||||
//! as touch force, but drops `PEN_FLAG_BARREL` (`0x0001`); `photocraft-tablet` recovers the barrel
|
||||
//! button through a thread-local message hook beside winit. With Windows Ink disabled for the
|
||||
//! tablet, the optional WinTab mode reads pressure, tilt, rotation and the eraser end from a
|
||||
//! WinTab 1.4 system context instead ([`install_wintab`]).
|
||||
//! as touch force, but drops `PEN_FLAG_BARREL` (`0x0001`) and the eraser end (`PEN_FLAG_INVERTED`);
|
||||
//! `photocraft-tablet` recovers them through thread-local message hooks beside winit. With Windows
|
||||
//! Ink disabled for the tablet, the optional WinTab mode reads pressure, tilt, rotation and the
|
||||
//! eraser end from a WinTab 1.4 system context instead ([`install_wintab`]).
|
||||
//!
|
||||
//! Wayland compositors give a pen only to clients that bind the tablet protocol
|
||||
//! (`zwp_tablet_v2`), which winit 0.30 doesn't (and binding it on winit's connection needs
|
||||
@@ -34,11 +34,15 @@ pub fn sink(feed: StylusFeed) -> impl Fn(Option<Sample>) + Send + 'static {
|
||||
move |s| feed.set(s.map(pen_sample))
|
||||
}
|
||||
|
||||
/// Install the Windows pointer message hook (main thread; keep the result until the event loop
|
||||
/// returns). On failure this logs why: the pen barrel button is then missed.
|
||||
/// Install the Windows pointer message hooks (main thread; keep the result until the event loop
|
||||
/// returns). On failure this logs why: the pen barrel button and eraser end are then missed.
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn install_windows() -> Option<photocraft_tablet::windows::Monitor> {
|
||||
photocraft_tablet::windows::Monitor::install().map_err(|e| log::warn!("{e}")).ok()
|
||||
let monitor = photocraft_tablet::windows::Monitor::install().map_err(|e| log::warn!("{e}")).ok()?;
|
||||
if !monitor.tracks_leave() {
|
||||
log::warn!("tablet input: the WH_CALLWNDPROC hook did not install, so the pen leaving the window may be missed");
|
||||
}
|
||||
Some(monitor)
|
||||
}
|
||||
|
||||
/// Installs the WinTab backend on the real PhotoCraft window and returns its per-frame poll.
|
||||
@@ -88,6 +92,18 @@ pub fn right_button_service() -> Option<photocraft_ui_egui::RightButtonDownFn> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Which end of the pen is in use (Windows): flipping the pen to its eraser end selects the
|
||||
/// Eraser (#2285). Elsewhere the end arrives with the samples in the stylus feed.
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn pen_end_service() -> Option<photocraft_ui_egui::PenEndFn> {
|
||||
Some(Box::new(photocraft_tablet::windows::take_pen_end))
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
pub fn pen_end_service() -> Option<photocraft_ui_egui::PenEndFn> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Install the AppKit tablet monitor (main thread, once winit created the shared application:
|
||||
/// from eframe's app creation, not before the event loop). Keep the result until the event loop
|
||||
/// returns. On failure this logs why: pens then paint like a mouse.
|
||||
@@ -419,4 +435,40 @@ mod tests {
|
||||
assert!(photocraft_ui_egui::stylus::Stylus::sync_eraser_tool(&mut app));
|
||||
assert_eq!(app.ui.tool, Tool::Brush);
|
||||
}
|
||||
|
||||
/// Windows: what the pointer hooks record reaches the Eraser switch through the service the
|
||||
/// app installs (#2285).
|
||||
#[cfg(target_os = "windows")]
|
||||
#[test]
|
||||
fn windows_eraser_end_switches_to_the_eraser_and_back() {
|
||||
use photocraft_tablet::windows::{
|
||||
PEN_FLAG_ERASER, PEN_FLAG_INVERTED, WM_POINTERDOWN, WM_POINTERLEAVE, WM_POINTERUP, WM_POINTERUPDATE, handle_pointer_message,
|
||||
};
|
||||
use photocraft_ui_egui::stylus::Stylus;
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
||||
app.services.pen_end = pen_end_service();
|
||||
app.ui.tool = Tool::Brush;
|
||||
let hover = |pen_flags: u32| handle_pointer_message(WM_POINTERUPDATE, 1, move |_| Some(pen_flags));
|
||||
hover(0);
|
||||
assert!(!Stylus::sync_eraser_tool(&mut app), "the tip changes nothing");
|
||||
hover(PEN_FLAG_ERASER);
|
||||
assert!(!Stylus::sync_eraser_tool(&mut app), "an eraser button on the barrel is not the eraser end");
|
||||
handle_pointer_message(WM_POINTERLEAVE, 1, |_| None);
|
||||
hover(PEN_FLAG_INVERTED);
|
||||
assert!(Stylus::sync_eraser_tool(&mut app));
|
||||
assert_eq!(app.ui.tool, Tool::Eraser);
|
||||
handle_pointer_message(WM_POINTERLEAVE, 1, |_| None);
|
||||
assert!(!Stylus::sync_eraser_tool(&mut app), "out of range");
|
||||
hover(0);
|
||||
assert!(Stylus::sync_eraser_tool(&mut app));
|
||||
assert_eq!(app.ui.tool, Tool::Brush, "the tip restores the Brush");
|
||||
// An eraser-end tap and the pen leaving, all before the next frame: the frame that
|
||||
// handles the queued click still selects the Eraser.
|
||||
handle_pointer_message(WM_POINTERDOWN, 1, |_| Some(PEN_FLAG_INVERTED | PEN_FLAG_ERASER));
|
||||
handle_pointer_message(WM_POINTERUP, 1, |_| None);
|
||||
handle_pointer_message(WM_POINTERLEAVE, 1, |_| None);
|
||||
assert!(Stylus::sync_eraser_tool(&mut app));
|
||||
assert_eq!(app.ui.tool, Tool::Eraser);
|
||||
assert!(!Stylus::sync_eraser_tool(&mut app), "out of range");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,7 +296,31 @@ impl<'a> Archive<'a> {
|
||||
|
||||
/// Extract an entry by name: decompress, undo the byte predictor and verify the CRC.
|
||||
pub fn read(&mut self, name: &str) -> Result<Vec<u8>, Error> {
|
||||
let e = self.entries.get(name).ok_or(Error::Malformed("missing archive entry"))?.clone();
|
||||
self.claim(name)?.decode()
|
||||
}
|
||||
|
||||
/// Extract several entries, decoding them on worker threads. The entries are charged one
|
||||
/// after the other exactly as the same number of [`read`](Self::read) calls would charge
|
||||
/// them, and the first error in that order is the one returned.
|
||||
pub(crate) fn read_many(&mut self, names: &[&str]) -> Result<Vec<Vec<u8>>, Error> {
|
||||
let mut claimed = Vec::with_capacity(names.len());
|
||||
let mut refused = None;
|
||||
for name in names {
|
||||
match self.claim(name) {
|
||||
Ok(c) => claimed.push(c),
|
||||
Err(e) => {
|
||||
refused = Some(e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
let decoded = decode_all(&claimed).into_iter().collect::<Result<Vec<_>, _>>()?;
|
||||
refused.map_or(Ok(decoded), Err)
|
||||
}
|
||||
|
||||
/// Check an entry against the limits, count it as extracted and locate its stored bytes.
|
||||
fn claim(&mut self, name: &str) -> Result<Claimed<'a>, Error> {
|
||||
let e = self.entries.get(name).ok_or(Error::Malformed("missing archive entry"))?;
|
||||
let size = usize::try_from(e.size).map_err(|_| Error::Limit("archive entry too large"))?;
|
||||
if size > self.limits.max_entry {
|
||||
return Err(Error::Limit("archive entry too large"));
|
||||
@@ -315,22 +339,79 @@ impl<'a> Archive<'a> {
|
||||
let method = e.compression & 3;
|
||||
let stored_len = if matches!(method, 1 | 2) { e.compressed } else { e.size };
|
||||
let data = c.take(offset(stored_len)?)?;
|
||||
let mut out = match method {
|
||||
1 => inflate(data, size)?,
|
||||
2 => unzstd(data, size)?,
|
||||
_ => data.to_vec(),
|
||||
Ok(Claimed { data, size, compression: e.compression, crc: e.crc })
|
||||
}
|
||||
}
|
||||
|
||||
/// An entry that is charged and located but not yet decoded.
|
||||
struct Claimed<'a> {
|
||||
data: &'a [u8],
|
||||
size: usize,
|
||||
compression: u8,
|
||||
crc: u32,
|
||||
}
|
||||
|
||||
impl Claimed<'_> {
|
||||
fn decode(&self) -> Result<Vec<u8>, Error> {
|
||||
let mut out = match self.compression & 3 {
|
||||
1 => inflate(self.data, self.size)?,
|
||||
2 => unzstd(self.data, self.size)?,
|
||||
_ => self.data.to_vec(),
|
||||
};
|
||||
if out.len() != size {
|
||||
if out.len() != self.size {
|
||||
return Err(Error::Malformed("archive entry size mismatch"));
|
||||
}
|
||||
unpredict(&mut out, e.compression)?;
|
||||
if crc32fast::hash(&out) != e.crc {
|
||||
unpredict(&mut out, self.compression)?;
|
||||
if crc32fast::hash(&out) != self.crc {
|
||||
return Err(Error::Malformed("archive entry checksum mismatch"));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
|
||||
/// Most threads decoding entries at once; each zstd frame may reserve its declared window.
|
||||
const MAX_DECODE_THREADS: usize = 8;
|
||||
/// Fewer entries than this are decoded in place: starting a thread costs more than they do.
|
||||
const MIN_PARALLEL_ENTRIES: usize = 4;
|
||||
|
||||
/// Decode every entry, in parallel when there are enough of them. The results are in order.
|
||||
fn decode_all(jobs: &[Claimed<'_>]) -> Vec<Result<Vec<u8>, Error>> {
|
||||
static THREADS: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
|
||||
// The browser build has no threads to start.
|
||||
let threads = if jobs.len() < MIN_PARALLEL_ENTRIES || cfg!(target_arch = "wasm32") {
|
||||
1
|
||||
} else {
|
||||
(*THREADS.get_or_init(|| std::thread::available_parallelism().map_or(1, usize::from).min(MAX_DECODE_THREADS))).min(jobs.len())
|
||||
};
|
||||
if threads <= 1 {
|
||||
return jobs.iter().map(Claimed::decode).collect();
|
||||
}
|
||||
let next = std::sync::atomic::AtomicUsize::new(0);
|
||||
// The workers and this thread take the next undecoded entry until none is left.
|
||||
let work = || {
|
||||
let mut done = Vec::new();
|
||||
loop {
|
||||
let i = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
let Some(job) = jobs.get(i) else { break };
|
||||
done.push((i, job.decode()));
|
||||
}
|
||||
done
|
||||
};
|
||||
let mut slots: Vec<Option<Result<Vec<u8>, Error>>> = jobs.iter().map(|_| None).collect();
|
||||
std::thread::scope(|sc| {
|
||||
// A thread that cannot be started just leaves its share to the others.
|
||||
let workers: Vec<_> = (1..threads).filter_map(|_| std::thread::Builder::new().spawn_scoped(sc, work).ok()).collect();
|
||||
let mut finished = vec![work()];
|
||||
finished.extend(workers.into_iter().map(|w| w.join().unwrap_or_default()));
|
||||
for (i, result) in finished.into_iter().flatten() {
|
||||
if let Some(slot) = slots.get_mut(i) {
|
||||
*slot = Some(result);
|
||||
}
|
||||
}
|
||||
});
|
||||
slots.into_iter().map(|r| r.unwrap_or(Err(Error::Malformed("tile decoder stopped")))).collect()
|
||||
}
|
||||
|
||||
/// Read at most `size + 1` bytes so a stream that decodes past its declared size is caught
|
||||
/// without unbounded allocation.
|
||||
fn read_bounded(mut r: impl Read, size: usize) -> Result<Vec<u8>, Error> {
|
||||
|
||||
+137
-49
@@ -2,7 +2,7 @@
|
||||
//! (`DyBm.Bckg` → archive entry `c/N`), which is imported as is; other pixels are planar tiles of
|
||||
//! 256 bytes × 256 rows per channel, each its own archive entry.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use crate::Error;
|
||||
use crate::model::{Affine, Image, Pixels, Reader};
|
||||
@@ -123,35 +123,78 @@ fn decode(r: &mut Reader, bitmap: ObjId, crop: (u32, u32, u32, u32), world: Affi
|
||||
let scratch = pixels.checked_mul(channels * bps).ok_or(Error::Limit("pixel layer size"))?;
|
||||
r.admit_pixels(pixels.checked_mul(4).ok_or(Error::Limit("pixel layer size"))?, scratch)?;
|
||||
let mut planes = Vec::with_capacity(channels);
|
||||
let mut cache = HashMap::new();
|
||||
let mut cache = Cache::new();
|
||||
for c in 1..=channels {
|
||||
planes.push(plane(r, bitmap, c, bps, (x0, y0, w, h), &mut cache)?);
|
||||
}
|
||||
let n = (w as usize) * (h as usize);
|
||||
let mut rgba = vec![0u8; n * 4];
|
||||
let sample = |p: &Vec<u8>, i: usize| -> f64 {
|
||||
if bps == 2 {
|
||||
f64::from(u16::from_le_bytes([p.get(2 * i).copied().unwrap_or(0), p.get(2 * i + 1).copied().unwrap_or(0)])) / 65535.0
|
||||
} else {
|
||||
f64::from(p.get(i).copied().unwrap_or(0)) / 255.0
|
||||
}
|
||||
};
|
||||
for (i, px) in rgba.as_chunks_mut::<4>().0.iter_mut().enumerate() {
|
||||
let v: Vec<f64> = planes.iter().map(|p| sample(p, i)).collect();
|
||||
let (r2, g, b, a) = if channels == 1 {
|
||||
(v[0], v[0], v[0], 1.0)
|
||||
} else if channels == 5 {
|
||||
let k = 1.0 - v[3];
|
||||
((1.0 - v[0]) * k, (1.0 - v[1]) * k, (1.0 - v[2]) * k, v[4])
|
||||
} else {
|
||||
(v[0], v[1], v[2], v[3])
|
||||
};
|
||||
px.copy_from_slice(&[r2, g, b, a].map(|c| (c.clamp(0.0, 1.0) * 255.0).round() as u8));
|
||||
}
|
||||
let rgba = interleave(&planes, channels, bps, pixels);
|
||||
let transform = Affine([1.0, 0.0, 0.0, 1.0, f64::from(x0), f64::from(y0)]).then(world);
|
||||
Ok(Some(Image { width: w, height: h, pixels: Pixels::Rgba8(rgba), transform }))
|
||||
}
|
||||
|
||||
/// Quantize a unit-range sample to a byte: the one conversion every format goes through.
|
||||
fn quant(v: f64) -> u8 {
|
||||
(v.clamp(0.0, 1.0) * 255.0).round() as u8
|
||||
}
|
||||
|
||||
/// Interleave the decoded planes (`n` samples of `bps` bytes each) into RGBA8.
|
||||
///
|
||||
/// The samples used to be widened to `f64` one pixel at a time. The tables below hold the same
|
||||
/// expressions evaluated once per input value, so the bytes are identical: an 8-bit sample
|
||||
/// `x / 255 * 255` rounds back to `x`, a 16-bit one goes through a 64 Ki table, and CMYK's
|
||||
/// `(1 - c)(1 - k)` through a table over the 256 x 256 `(c, k)` pairs.
|
||||
pub(crate) fn interleave(planes: &[Vec<u8>], channels: usize, bps: usize, n: usize) -> Vec<u8> {
|
||||
let mut rgba = vec![0u8; n * 4];
|
||||
let out = rgba.as_chunks_mut::<4>().0.iter_mut();
|
||||
let p = |c: usize| planes.get(c).map_or(&[][..], Vec::as_slice);
|
||||
match (channels, bps) {
|
||||
(1, 1) => {
|
||||
for (o, &v) in out.zip(p(0)) {
|
||||
*o = [v, v, v, 255];
|
||||
}
|
||||
}
|
||||
(4, 1) => {
|
||||
let (r, g, b, a) = (p(0), p(1), p(2), p(3));
|
||||
for ((((o, &r), &g), &b), &a) in out.zip(r).zip(g).zip(b).zip(a) {
|
||||
*o = [r, g, b, a];
|
||||
}
|
||||
}
|
||||
(5, 1) => {
|
||||
let lut = cmyk_table();
|
||||
let (c, m, y, k, a) = (p(0), p(1), p(2), p(3), p(4));
|
||||
for (((((o, &c), &m), &y), &k), &a) in out.zip(c).zip(m).zip(y).zip(k).zip(a) {
|
||||
let k = usize::from(k) << 8;
|
||||
*o = [lut[k | usize::from(c)], lut[k | usize::from(m)], lut[k | usize::from(y)], a];
|
||||
}
|
||||
}
|
||||
(_, _) => {
|
||||
let lut: Vec<u8> = (0..=u16::MAX).map(|v| quant(f64::from(v) / 65535.0)).collect();
|
||||
let s16 = |c: usize| p(c).as_chunks::<2>().0.iter().map(|b| lut[usize::from(u16::from_le_bytes(*b))]);
|
||||
if channels == 1 {
|
||||
for (o, g) in out.zip(s16(0)) {
|
||||
*o = [g, g, g, 255];
|
||||
}
|
||||
} else {
|
||||
for ((((o, r), g), b), a) in out.zip(s16(0)).zip(s16(1)).zip(s16(2)).zip(s16(3)) {
|
||||
*o = [r, g, b, a];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
rgba
|
||||
}
|
||||
|
||||
/// `(1 - c)(1 - k)` quantized, indexed by `k << 8 | c`.
|
||||
fn cmyk_table() -> Vec<u8> {
|
||||
let mut lut = Vec::with_capacity(256 * 256);
|
||||
for k in 0..=255u8 {
|
||||
for c in 0..=255u8 {
|
||||
lut.push(quant((1.0 - f64::from(c) / 255.0) * (1.0 - f64::from(k) / 255.0)));
|
||||
}
|
||||
}
|
||||
lut
|
||||
}
|
||||
|
||||
/// The original image file: entry `c/N` is a stream whose root (`Blck`) holds it as `Data`.
|
||||
fn original(r: &mut Reader, name: &str) -> Result<Vec<u8>, Error> {
|
||||
let bytes = r.archive.read(name)?;
|
||||
@@ -167,14 +210,24 @@ fn tag(prefix: &[u8; 3], c: usize) -> [u8; 4] {
|
||||
[prefix[0], prefix[1], prefix[2], b'0' + c as u8]
|
||||
}
|
||||
|
||||
/// Decoded tiles by archive entry name: a tile shared by several grid cells or channels is
|
||||
/// extracted (and charged to the archive's budget) once.
|
||||
type Cache<'s> = HashMap<&'s str, Vec<u8>>;
|
||||
|
||||
/// A tile to copy into a plane once its entry is decoded: pixel column and row of its corner.
|
||||
struct Place<'s> {
|
||||
name: &'s str,
|
||||
at: (usize, usize),
|
||||
}
|
||||
|
||||
/// One channel of level 0, cropped to (x0, y0, w, h) pixels; `bps` bytes per sample.
|
||||
fn plane(
|
||||
r: &mut Reader,
|
||||
fn plane<'s>(
|
||||
r: &mut Reader<'s, '_, '_>,
|
||||
bitmap: ObjId,
|
||||
c: usize,
|
||||
bps: usize,
|
||||
(x0, y0, w, h): (u32, u32, u32, u32),
|
||||
cache: &mut HashMap<String, Vec<u8>>,
|
||||
cache: &mut Cache<'s>,
|
||||
) -> Result<Vec<u8>, Error> {
|
||||
let s = r.s;
|
||||
let width = s.int(bitmap, b"BmpW").and_then(|v| usize::try_from(v).ok()).unwrap_or(0);
|
||||
@@ -199,6 +252,15 @@ fn plane(
|
||||
let mut out = vec![0u8; row * h as usize];
|
||||
let (bx0, bx1) = (x0 as usize * bps, (x0 + w) as usize * bps);
|
||||
let (py0, py1) = (y0 as usize, (y0 + h) as usize);
|
||||
let crop = (bx0, py0, bx1, py1);
|
||||
// First walk the grid: flat cells are filled right away (cells never overlap, so the order
|
||||
// of the copies does not matter) and the tiles that show are collected. They are extracted
|
||||
// together below, on several threads, then copied. A walk that fails still extracts the
|
||||
// tiles before the failing cell, as a tile-by-tile walk would have.
|
||||
let mut places: Vec<Place<'s>> = Vec::new();
|
||||
let mut wanted: Vec<&'s str> = Vec::new();
|
||||
let mut queued: HashSet<&'s str> = HashSet::new();
|
||||
let mut failure = None;
|
||||
for (t, state) in states.iter().enumerate().take(tw.saturating_mul(th)) {
|
||||
let state = &if let Value::UInt(u) = state { u8::try_from(*u).unwrap_or(255) } else { 255 };
|
||||
let (ty, tx) = (t / tw.max(1), t % tw.max(1));
|
||||
@@ -208,30 +270,28 @@ fn plane(
|
||||
4 => {
|
||||
let Some(at) = tiles.iter().position(|v| matches!(v, Value::Obj(Some(_)))) else {
|
||||
r.charge_slots(tiles.len())?;
|
||||
return Err(Error::Malformed("missing pixel tile"));
|
||||
failure = Some(Error::Malformed("missing pixel tile"));
|
||||
break;
|
||||
};
|
||||
r.charge_slots(at + 1)?;
|
||||
let Some(Value::Obj(Some(blck))) = tiles.get(at) else { return Err(Error::Malformed("missing pixel tile")) };
|
||||
let Some(Value::Obj(Some(blck))) = tiles.get(at) else {
|
||||
failure = Some(Error::Malformed("missing pixel tile"));
|
||||
break;
|
||||
};
|
||||
let blck = *blck;
|
||||
tiles = tiles.get(at + 1..).unwrap_or_default();
|
||||
let (ox, oy) = (tx * TILE, ty * TILE);
|
||||
if ox >= bx1 || ox + TILE <= bx0 || oy >= py1 || oy + TILE <= py0 {
|
||||
continue;
|
||||
}
|
||||
let name = s.entry(blck, b"Data").ok_or(Error::Malformed("pixel tile without data"))?.to_string();
|
||||
if !cache.contains_key(&name) {
|
||||
let mut data = r.archive.read(&name)?;
|
||||
// Older files wrap a tile in a one-field stream: header, then a 64 KiB blob.
|
||||
if data.len() == TILE_BYTES + 22 && data.starts_with(b"\x00\xffKS") {
|
||||
data = data.get(21..21 + TILE_BYTES).ok_or(Error::Malformed("pixel tile"))?.to_vec();
|
||||
}
|
||||
if data.len() != TILE_BYTES {
|
||||
return Err(Error::Malformed("pixel tile size"));
|
||||
}
|
||||
cache.insert(name.clone(), data);
|
||||
let Some(name) = s.entry(blck, b"Data") else {
|
||||
failure = Some(Error::Malformed("pixel tile without data"));
|
||||
break;
|
||||
};
|
||||
if !cache.contains_key(name) && queued.insert(name) {
|
||||
wanted.push(name);
|
||||
}
|
||||
let tile = cache.get(&name).ok_or(Error::Malformed("pixel tile"))?;
|
||||
copy(&mut out, row, (bx0, py0, bx1, py1), (ox, oy), |x, y| tile.get(y * TILE + x).copied().unwrap_or(0));
|
||||
places.push(Place { name, at: (ox, oy) });
|
||||
continue;
|
||||
}
|
||||
3 => {
|
||||
@@ -242,22 +302,50 @@ fn plane(
|
||||
r.warn("pixel layers drawn from an embedded image (left empty)");
|
||||
None
|
||||
}
|
||||
_ => return Err(Error::Malformed("unknown pixel tile state")),
|
||||
_ => {
|
||||
failure = Some(Error::Malformed("unknown pixel tile state"));
|
||||
break;
|
||||
}
|
||||
};
|
||||
if let Some(v) = fill {
|
||||
copy(&mut out, row, (bx0, py0, bx1, py1), (tx * TILE, ty * TILE), |_, _| v);
|
||||
copy(&mut out, row, crop, (tx * TILE, ty * TILE), None, v);
|
||||
}
|
||||
}
|
||||
let decoded = r.archive.read_many(&wanted);
|
||||
// A tile that fails to extract is reported before the failure of the walk after it.
|
||||
for (name, mut data) in wanted.into_iter().zip(decoded?) {
|
||||
// Older files wrap a tile in a one-field stream: header, then a 64 KiB blob.
|
||||
if data.len() == TILE_BYTES + 22 && data.starts_with(b"\x00\xffKS") {
|
||||
data = data.get(21..21 + TILE_BYTES).ok_or(Error::Malformed("pixel tile"))?.to_vec();
|
||||
}
|
||||
if data.len() != TILE_BYTES {
|
||||
return Err(Error::Malformed("pixel tile size"));
|
||||
}
|
||||
cache.insert(name, data);
|
||||
}
|
||||
if let Some(e) = failure {
|
||||
return Err(e);
|
||||
}
|
||||
for place in &places {
|
||||
let tile = cache.get(place.name).ok_or(Error::Malformed("pixel tile"))?;
|
||||
copy(&mut out, row, crop, place.at, Some(tile), 0);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Copy the overlap of a tile at byte column `ox`, row `oy` into the cropped plane.
|
||||
fn copy(out: &mut [u8], row: usize, (bx0, py0, bx1, py1): (usize, usize, usize, usize), (ox, oy): (usize, usize), at: impl Fn(usize, usize) -> u8) {
|
||||
/// Copy the overlap of a tile at byte column `ox`, row `oy` into the cropped plane. `tile` is
|
||||
/// the tile's bytes, or `None` to fill the overlap with `fill`.
|
||||
pub(crate) fn copy(out: &mut [u8], row: usize, (bx0, py0, bx1, py1): (usize, usize, usize, usize), (ox, oy): (usize, usize), tile: Option<&[u8]>, fill: u8) {
|
||||
let (xa, xb) = (ox.max(bx0), (ox + TILE).min(bx1));
|
||||
if xa >= xb {
|
||||
return;
|
||||
}
|
||||
for y in oy.max(py0)..(oy + TILE).min(py1) {
|
||||
for x in ox.max(bx0)..(ox + TILE).min(bx1) {
|
||||
if let Some(b) = out.get_mut((y - py0) * row + (x - bx0)) {
|
||||
*b = at(x - ox, y - oy);
|
||||
}
|
||||
let start = (y - py0) * row + (xa - bx0);
|
||||
let Some(dst) = out.get_mut(start..start + (xb - xa)) else { continue };
|
||||
match tile.and_then(|t| t.get((y - oy) * TILE + (xa - ox)..(y - oy) * TILE + (xb - ox))) {
|
||||
Some(src) => dst.copy_from_slice(src),
|
||||
None => dst.fill(if tile.is_some() { 0 } else { fill }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -808,6 +808,192 @@ mod spot_colours {
|
||||
}
|
||||
}
|
||||
|
||||
/// The raster fast paths against the code they replaced.
|
||||
mod raster_identity {
|
||||
use crate::raster::{copy, interleave};
|
||||
|
||||
/// Deterministic filler (xorshift).
|
||||
fn bytes(n: usize, mut x: u64) -> Vec<u8> {
|
||||
(0..n)
|
||||
.map(|_| {
|
||||
x ^= x << 13;
|
||||
x ^= x >> 7;
|
||||
x ^= x << 17;
|
||||
(x >> 24) as u8
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The per-pixel `f64` conversion `decode` used before the tables.
|
||||
fn reference(planes: &[Vec<u8>], channels: usize, bps: usize, n: usize) -> Vec<u8> {
|
||||
let sample = |p: &Vec<u8>, i: usize| -> f64 {
|
||||
if bps == 2 {
|
||||
f64::from(u16::from_le_bytes([p.get(2 * i).copied().unwrap_or(0), p.get(2 * i + 1).copied().unwrap_or(0)])) / 65535.0
|
||||
} else {
|
||||
f64::from(p.get(i).copied().unwrap_or(0)) / 255.0
|
||||
}
|
||||
};
|
||||
let mut rgba = vec![0u8; n * 4];
|
||||
for (i, px) in rgba.as_chunks_mut::<4>().0.iter_mut().enumerate() {
|
||||
let v: Vec<f64> = planes.iter().map(|p| sample(p, i)).collect();
|
||||
let (r, g, b, a) = if channels == 1 {
|
||||
(v[0], v[0], v[0], 1.0)
|
||||
} else if channels == 5 {
|
||||
let k = 1.0 - v[3];
|
||||
((1.0 - v[0]) * k, (1.0 - v[1]) * k, (1.0 - v[2]) * k, v[4])
|
||||
} else {
|
||||
(v[0], v[1], v[2], v[3])
|
||||
};
|
||||
px.copy_from_slice(&[r, g, b, a].map(|c| (c.clamp(0.0, 1.0) * 255.0).round() as u8));
|
||||
}
|
||||
rgba
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eight_bit_samples_round_trip() {
|
||||
// Every byte through every position of the 4-channel and the grey path.
|
||||
let ramp: Vec<u8> = (0..=255).collect();
|
||||
let planes = vec![ramp.clone(), ramp.iter().rev().copied().collect(), ramp.clone(), ramp.iter().rev().copied().collect()];
|
||||
assert_eq!(interleave(&planes, 4, 1, 256), reference(&planes, 4, 1, 256));
|
||||
assert_eq!(interleave(&planes[..1], 1, 1, 256), reference(&planes[..1], 1, 1, 256));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sixteen_bit_samples_are_exhaustive() {
|
||||
// All 65536 values in each channel position (rotated so the channels differ).
|
||||
let all: Vec<u8> = (0..=u16::MAX).flat_map(u16::to_le_bytes).collect();
|
||||
let rot = |k: usize| -> Vec<u8> { (0..=u16::MAX).flat_map(|v| v.wrapping_add((k * 4099) as u16).to_le_bytes()).collect() };
|
||||
let planes = vec![all, rot(1), rot(2), rot(3)];
|
||||
assert_eq!(interleave(&planes, 4, 2, 65536), reference(&planes, 4, 2, 65536));
|
||||
assert_eq!(interleave(&planes[..1], 1, 2, 65536), reference(&planes[..1], 1, 2, 65536));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cmyk_is_exhaustive_over_colour_and_black() {
|
||||
// Every (c, k) pair in each of the c, m and y positions; alpha is arbitrary.
|
||||
let n = 256 * 256;
|
||||
let k: Vec<u8> = (0..n).map(|i| (i >> 8) as u8).collect();
|
||||
let c: Vec<u8> = (0..n).map(|i| i as u8).collect();
|
||||
let m: Vec<u8> = (0..n).map(|i| (i as u8).wrapping_mul(7).wrapping_add(3)).collect();
|
||||
let y: Vec<u8> = (0..n).map(|i| (i as u8).wrapping_mul(13).wrapping_add(250)).collect();
|
||||
let planes = vec![c, m, y, k, bytes(n, 9)];
|
||||
assert_eq!(interleave(&planes, 5, 1, n), reference(&planes, 5, 1, n));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_planes_match_for_every_format() {
|
||||
for (channels, bps) in [(1, 1), (1, 2), (4, 1), (4, 2), (5, 1)] {
|
||||
let n = 5000;
|
||||
let planes: Vec<Vec<u8>> = (0..channels).map(|c| bytes(n * bps, 1 + c as u64)).collect();
|
||||
assert_eq!(interleave(&planes, channels, bps, n), reference(&planes, channels, bps, n), "{channels}x{bps}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The per-byte closure copy `plane` used before the row copies.
|
||||
fn copy_reference(
|
||||
out: &mut [u8],
|
||||
row: usize,
|
||||
(bx0, py0, bx1, py1): (usize, usize, usize, usize),
|
||||
(ox, oy): (usize, usize),
|
||||
at: impl Fn(usize, usize) -> u8,
|
||||
) {
|
||||
for y in oy.max(py0)..(oy + 256).min(py1) {
|
||||
for x in ox.max(bx0)..(ox + 256).min(bx1) {
|
||||
if let Some(b) = out.get_mut((y - py0) * row + (x - bx0)) {
|
||||
*b = at(x - ox, y - oy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tile_copy_matches_the_byte_loop_for_every_crop() {
|
||||
let tile = bytes(256 * 256, 77);
|
||||
// Crops that cut the tile on each side, sit inside it, and miss it.
|
||||
for &(bx0, bx1, py0, py1) in
|
||||
&[(0, 600, 0, 600), (10, 300, 20, 200), (100, 150, 100, 130), (256, 512, 0, 256), (0, 256, 300, 400), (255, 257, 255, 257), (300, 301, 0, 1)]
|
||||
{
|
||||
for &(ox, oy) in &[(0, 0), (256, 0), (0, 256), (256, 256), (512, 256)] {
|
||||
let row = bx1 - bx0;
|
||||
let mut old = vec![0xAAu8; row * (py1 - py0)];
|
||||
let mut new = old.clone();
|
||||
copy_reference(&mut old, row, (bx0, py0, bx1, py1), (ox, oy), |x, y| tile[y * 256 + x]);
|
||||
copy(&mut new, row, (bx0, py0, bx1, py1), (ox, oy), Some(&tile), 0);
|
||||
assert_eq!(old, new, "tile {ox},{oy} crop {bx0}..{bx1} x {py0}..{py1}");
|
||||
copy_reference(&mut old, row, (bx0, py0, bx1, py1), (ox, oy), |_, _| 0xFF);
|
||||
copy(&mut new, row, (bx0, py0, bx1, py1), (ox, oy), None, 0xFF);
|
||||
assert_eq!(old, new, "fill {ox},{oy} crop {bx0}..{bx1} x {py0}..{py1}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `Archive::read_many` against the same reads made one at a time.
|
||||
mod read_many {
|
||||
use crate::synth::{self, Method};
|
||||
use crate::{Archive, Error, Limits};
|
||||
|
||||
fn entry(i: usize) -> Vec<u8> {
|
||||
(0..4096 + i * 37).map(|b| (b * 31 + i * 7) as u8).collect()
|
||||
}
|
||||
|
||||
fn archive(count: usize, method: Method) -> Vec<u8> {
|
||||
let datas: Vec<Vec<u8>> = (0..count).map(entry).collect();
|
||||
let names: Vec<String> = (0..count).map(|i| format!("d/{i}")).collect();
|
||||
let entries: Vec<(&str, &[u8], Method)> = names.iter().zip(&datas).map(|(n, d)| (n.as_str(), d.as_slice(), method)).collect();
|
||||
synth::container(&entries, None)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matches_serial_reads_and_charges_the_same() {
|
||||
for method in [Method::Stored, Method::Zlib, Method::Zstd] {
|
||||
for count in [1, 3, 4, 5, 40] {
|
||||
let file = archive(count, method);
|
||||
let names: Vec<String> = (0..count).map(|i| format!("d/{i}")).collect();
|
||||
let names: Vec<&str> = names.iter().map(String::as_str).collect();
|
||||
let mut one = Archive::open(&file, Limits::default()).unwrap();
|
||||
let serial: Vec<Vec<u8>> = names.iter().map(|n| one.read(n).unwrap()).collect();
|
||||
let mut many = Archive::open(&file, Limits::default()).unwrap();
|
||||
assert_eq!(many.read_many(&names).unwrap(), serial);
|
||||
assert_eq!(many.extracted(), one.extracted());
|
||||
assert_eq!(serial[count - 1], entry(count - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_limit_stops_at_the_same_entry_as_serial_reads() {
|
||||
let file = archive(12, Method::Zstd);
|
||||
let names: Vec<String> = (0..12).map(|i| format!("d/{i}")).collect();
|
||||
let names: Vec<&str> = names.iter().map(String::as_str).collect();
|
||||
let limits = Limits { max_total: (0..7).map(|i| entry(i).len()).sum(), ..Limits::default() };
|
||||
let mut one = Archive::open(&file, limits).unwrap();
|
||||
let failed = names.iter().position(|n| one.read(n).is_err()).unwrap();
|
||||
assert_eq!(failed, 7);
|
||||
let mut many = Archive::open(&file, limits).unwrap();
|
||||
assert!(matches!(many.read_many(&names), Err(Error::Limit(_))));
|
||||
assert_eq!(many.extracted(), one.extracted());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_first_failing_entry_in_order_is_reported() {
|
||||
let mut file = archive(12, Method::Stored);
|
||||
// Flip a data byte of entry 2 (checksum failure); entry 9 is missing from the list.
|
||||
let at = file.windows(4).position(|w| w == b"#Fil").unwrap();
|
||||
let second = at + 4 + entry(0).len();
|
||||
let third = second + 4 + entry(1).len();
|
||||
file[third + 4 + 10] ^= 0xFF;
|
||||
let names: Vec<String> = (0..12).map(|i| if i == 9 { "gone".to_string() } else { format!("d/{i}") }).collect();
|
||||
let names: Vec<&str> = names.iter().map(String::as_str).collect();
|
||||
let mut a = Archive::open(&file, Limits::default()).unwrap();
|
||||
assert_eq!(a.read_many(&names).unwrap_err(), Error::Malformed("archive entry checksum mismatch"));
|
||||
// Without the corrupt entry the missing one is what fails.
|
||||
let intact = archive(12, Method::Stored);
|
||||
let mut a = Archive::open(&intact, Limits::default()).unwrap();
|
||||
assert_eq!(a.read_many(&names).unwrap_err(), Error::Malformed("missing archive entry"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Object lookup and record arrays against the structures they replaced.
|
||||
mod stream_storage {
|
||||
use crate::stream::{self, Kind, Object, Tag, Value};
|
||||
|
||||
@@ -976,7 +976,7 @@ impl ColorBvh {
|
||||
|
||||
fn reserve_nodes(capacity: usize) -> Result<Vec<ColorNode>, &'static str> {
|
||||
let mut nodes = Vec::new();
|
||||
nodes.try_reserve_exact(capacity).map_err(|_| "not enough memory to index selection colours")?;
|
||||
nodes.try_reserve_exact(capacity).map_err(|_| "not enough memory to index selection colors")?;
|
||||
Ok(nodes)
|
||||
}
|
||||
|
||||
@@ -1072,10 +1072,10 @@ impl ColorMatcher {
|
||||
let key = [norm(c[0]), norm(c[1]), norm(c[2]), norm(c[3])];
|
||||
if !seen.contains(&key) {
|
||||
if seen.len() >= limit {
|
||||
return Err("the selection contains too many distinct colours to match; select a smaller area");
|
||||
return Err("the selection contains too many distinct colors to match; select a smaller area");
|
||||
}
|
||||
seen.try_reserve(1).map_err(|_| "not enough memory to index selection colours")?;
|
||||
opaque_or_semi.try_reserve(1).map_err(|_| "not enough memory to index selection colours")?;
|
||||
seen.try_reserve(1).map_err(|_| "not enough memory to index selection colors")?;
|
||||
opaque_or_semi.try_reserve(1).map_err(|_| "not enough memory to index selection colors")?;
|
||||
seen.insert(key);
|
||||
opaque_or_semi.push(c);
|
||||
}
|
||||
|
||||
@@ -184,6 +184,9 @@ impl Headless {
|
||||
let tiff_bool = |key| photocraft_engine::file_cmds::tiff_bool_param(&p, key, "doc.save").map_err(|e| bad(e.to_string()));
|
||||
opts.tiff_transparency = tiff_bool("tiffTransparency")?;
|
||||
opts.encode.tiff_pyramid = tiff_bool("tiffPyramid")?.unwrap_or(false);
|
||||
if let Some(maximize) = photocraft_engine::file_cmds::maximize_compatibility_param(&p, "doc.save").map_err(|e| bad(e.to_string()))? {
|
||||
opts.psd_maximize_compatibility = maximize;
|
||||
}
|
||||
match p.get("quality") {
|
||||
None | Some(Value::Null) => {}
|
||||
Some(v) => {
|
||||
|
||||
@@ -303,6 +303,28 @@ fn doc_save_rejects_a_bad_quality_instead_of_ignoring_or_clamping_it() {
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// #8064: `doc.save` takes `maximizeCompatibility` for a layered PSD's merged image (false: the
|
||||
/// placeholder, flagged in resource 1057); anything but a boolean writes nothing.
|
||||
#[test]
|
||||
fn doc_save_takes_maximize_compatibility() {
|
||||
let dir = std::env::temp_dir().join(format!("pc-rpc-save-maximize-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let mut h = Headless::trusted_local();
|
||||
h.handle("doc.new", json!({"width": 64, "height": 32})).unwrap();
|
||||
h.handle("engine.execute", json!({"command": "layer.new.layer"})).unwrap();
|
||||
let real_merged = |p: &std::path::Path| photocraft_psd::PsdFile::from_bytes(&std::fs::read(p).unwrap()).unwrap().has_real_merged_data();
|
||||
for (name, maximize, real) in [("default.psd", json!(null), true), ("off.psd", json!(false), false), ("on.psd", json!(true), true)] {
|
||||
let path = dir.join(name);
|
||||
h.handle("doc.save", json!({"path": path.to_string_lossy(), "maximizeCompatibility": maximize})).unwrap();
|
||||
assert_eq!(real_merged(&path), Some(real), "{name}");
|
||||
}
|
||||
let bad = dir.join("bad.psd");
|
||||
let err = h.handle("doc.save", json!({"path": bad.to_string_lossy(), "maximizeCompatibility": 0})).unwrap_err().to_string();
|
||||
assert!(err.contains("maximizeCompatibility"), "{err}");
|
||||
assert!(!bad.exists(), "a bad value writes nothing");
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn automation_preview_rejects_oversized_full_size_and_numeric_wraparound() {
|
||||
let mut h = Headless::new();
|
||||
|
||||
@@ -113,6 +113,11 @@ pub struct ExportLayersParams {
|
||||
/// Encoder quality, 0..12, for the formats that take one.
|
||||
#[serde(default)]
|
||||
pub quality: Option<f64>,
|
||||
/// PSD/PSB: write the real merged image (Photoshop's Maximize Compatibility); false writes
|
||||
/// Photoshop's white placeholder (Bitmap, Indexed Color and Duotone documents still write the
|
||||
/// real image). Unset: the engine's Preferences › File Handling decides.
|
||||
#[serde(default, rename = "maximizeCompatibility")]
|
||||
pub maximize_compatibility: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize, JsonSchema)]
|
||||
@@ -214,6 +219,13 @@ pub struct SaveParams {
|
||||
/// The save may omit it; check the manifest's `full_composite` member. Off by default.
|
||||
#[serde(default, rename = "pcraftFullComposite")]
|
||||
pub pcraft_full_composite: Option<bool>,
|
||||
/// PSD/PSB: write the real merged image (Photoshop's Maximize Compatibility). false writes
|
||||
/// Photoshop's white placeholder instead: a smaller file and a faster save, but readers that
|
||||
/// show only the merged image see white. Bitmap, Indexed Color and Duotone documents still
|
||||
/// write the real image. Unset: the real image (in bridge mode, the app's
|
||||
/// Preferences › File Handling decides).
|
||||
#[serde(default, rename = "maximizeCompatibility")]
|
||||
pub maximize_compatibility: Option<bool>,
|
||||
#[serde(default)]
|
||||
pub index: Option<usize>,
|
||||
}
|
||||
@@ -798,6 +810,9 @@ impl PhotocraftMcp {
|
||||
if let Some(v) = p.quality {
|
||||
opts.insert("quality".into(), json!(v));
|
||||
}
|
||||
if let Some(v) = p.maximize_compatibility {
|
||||
opts.insert("maximizeCompatibility".into(), json!(v));
|
||||
}
|
||||
let opts = Value::Object(opts);
|
||||
// Bridge mode runs the app on this machine, which writes its own paths like any command.
|
||||
let mut bridge_params = opts.clone();
|
||||
@@ -1186,9 +1201,10 @@ impl PhotocraftMcp {
|
||||
if let Some(b) = self.bridge_client() {
|
||||
// The bridge forwards to the running app's `app.save`, which takes only an optional
|
||||
// path (without one it writes back to the document's own layered file, as headless
|
||||
// does), the Targa bits, the DDS options and the TIFF compression, transparency and
|
||||
// pyramid, so the other options are headless-only. Saying so beats saving with
|
||||
// defaults while the caller believes their quality or format was applied.
|
||||
// does), the Targa bits, the DDS options, the TIFF compression, transparency and
|
||||
// pyramid, and Maximize Compatibility, so the other options are headless-only. Saying
|
||||
// so beats saving with defaults while the caller believes their quality or format was
|
||||
// applied.
|
||||
let unsupported: Vec<&str> = [
|
||||
p.format.is_some().then_some("format"),
|
||||
p.quality.is_some().then_some("quality"),
|
||||
@@ -1226,6 +1242,9 @@ impl PhotocraftMcp {
|
||||
if p.tiff_pyramid {
|
||||
params["tiffPyramid"] = json!(true);
|
||||
}
|
||||
if let Some(maximize) = p.maximize_compatibility {
|
||||
params["maximizeCompatibility"] = json!(maximize);
|
||||
}
|
||||
return to_result(b.call("app.save", params).await);
|
||||
}
|
||||
let Some(r) = self
|
||||
@@ -1243,6 +1262,9 @@ impl PhotocraftMcp {
|
||||
}
|
||||
opts.encode.tiff_pyramid = p.tiff_pyramid;
|
||||
opts.encode.dds = photocraft_io::DdsOptions { compression: dds_compression.unwrap_or_default(), mipmaps: p.dds_mipmaps };
|
||||
if let Some(maximize) = p.maximize_compatibility {
|
||||
opts.psd_maximize_compatibility = maximize;
|
||||
}
|
||||
if let Some(q) = p.quality {
|
||||
opts.encode.jpeg_quality = q;
|
||||
opts.encode.webp_quality = q;
|
||||
|
||||
@@ -513,6 +513,70 @@ async fn headless_doc_save_writes_tiff_options() {
|
||||
cleanup(&dir);
|
||||
}
|
||||
|
||||
/// #8064: headless `doc_save`/`doc_export` take `maximizeCompatibility` for a layered PSD/PSB's
|
||||
/// merged image: false writes Photoshop's placeholder and says so in resource 1057; unset, the
|
||||
/// real image. A value that isn't a boolean is refused before anything is written.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn headless_doc_save_takes_maximize_compatibility() {
|
||||
let dir = tmp("maximize-compat");
|
||||
let client = connect(headless_in(&dir)).await;
|
||||
call(&client, "doc_new", json!({"width": 64, "height": 32, "background": "white"})).await;
|
||||
let r = call(&client, "command_run", json!({"id": "layer.new.layer", "params": {}})).await;
|
||||
assert_ne!(r.is_error, Some(true), "{}", text(&r));
|
||||
for (tool, params, real) in [
|
||||
("doc_export", json!({"path": "default.psd"}), true),
|
||||
("doc_export", json!({"path": "off.psd", "maximizeCompatibility": false}), false),
|
||||
("doc_save", json!({"path": "off.psb", "maximizeCompatibility": false}), false),
|
||||
("doc_save", json!({"path": "on.psd", "maximizeCompatibility": true}), true),
|
||||
] {
|
||||
let r = call(&client, tool, params.clone()).await;
|
||||
assert_ne!(r.is_error, Some(true), "{params}: {}", text(&r));
|
||||
let bytes = std::fs::read(dir.join(params["path"].as_str().unwrap())).unwrap();
|
||||
assert_eq!(photocraft_psd::PsdFile::from_bytes(&bytes).unwrap().has_real_merged_data(), Some(real), "{params}");
|
||||
}
|
||||
let r = call(&client, "doc_export", json!({"path": "bad.psd", "maximizeCompatibility": "no"})).await;
|
||||
assert_eq!(r.is_error, Some(true), "{}", text(&r));
|
||||
assert!(!dir.join("bad.psd").exists());
|
||||
client.cancel().await.unwrap();
|
||||
cleanup(&dir);
|
||||
}
|
||||
|
||||
/// #8064: `doc_export_layers` takes `maximizeCompatibility` too, for PSD/PSB layer files.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn headless_doc_export_layers_takes_maximize_compatibility() {
|
||||
let dir = tmp("export-layers-maximize-compat");
|
||||
let client = connect(headless_in(&dir)).await;
|
||||
call(&client, "doc_new", json!({"width": 16, "height": 16, "background": "white", "name": "card"})).await;
|
||||
for (sub, params, real) in
|
||||
[("on", json!({"dir": "on", "format": "psd"}), true), ("off", json!({"dir": "off", "format": "psd", "maximizeCompatibility": false}), false)]
|
||||
{
|
||||
let result = json_of(&call(&client, "doc_export_layers", params.clone()).await);
|
||||
assert_eq!(result["count"], 1, "{params}: {result}");
|
||||
let file = result["files"][0].as_str().unwrap();
|
||||
assert!(file.starts_with(sub), "{file}");
|
||||
let bytes = std::fs::read(dir.join(file)).unwrap();
|
||||
assert_eq!(photocraft_psd::PsdFile::from_bytes(&bytes).unwrap().has_real_merged_data(), Some(real), "{params}");
|
||||
}
|
||||
let r = call(&client, "doc_export_layers", json!({"dir": "bad", "format": "psd", "maximizeCompatibility": "no"})).await;
|
||||
assert_eq!(r.is_error, Some(true), "{}", text(&r));
|
||||
client.cancel().await.unwrap();
|
||||
cleanup(&dir);
|
||||
}
|
||||
|
||||
/// #8064: `maximizeCompatibility` reaches the app's `app.save` through the bridge; unset, nothing
|
||||
/// is sent and the app's preference decides.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn bridge_doc_save_forwards_maximize_compatibility() {
|
||||
let (addr, app) = fake_app().await;
|
||||
let client = connect(PhotocraftMcp::bridge(&addr, CONTROL_TOKEN).unwrap()).await;
|
||||
let saved = json_of(&call(&client, "doc_save", json!({"path": "out.psd", "maximizeCompatibility": false})).await);
|
||||
assert_eq!(saved, json!({"saved": {"path": "out.psd", "maximizeCompatibility": false}}));
|
||||
let saved = json_of(&call(&client, "doc_save", json!({"path": "out.psd"})).await);
|
||||
assert_eq!(saved, json!({"saved": {"path": "out.psd"}}));
|
||||
client.cancel().await.unwrap();
|
||||
app.abort();
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn bridge_response_budget_drops_connection_without_retrying_the_operation() {
|
||||
use photocraft_automation::{BridgeClient, budgets::MAX_RESPONSE_BYTES};
|
||||
|
||||
@@ -183,6 +183,17 @@ impl Pipeline {
|
||||
out[..self.outputs].copy_from_slice(&v[..self.outputs]);
|
||||
}
|
||||
|
||||
/// Evaluates the stages after the first one on values already passed through it.
|
||||
#[inline]
|
||||
pub(crate) fn eval_after_first(&self, v: &mut [f32; MAX], out: &mut [f32]) {
|
||||
let mut n = self.inputs;
|
||||
for s in self.stages.iter().skip(1) {
|
||||
n = s.run(v, n);
|
||||
}
|
||||
let _ = n;
|
||||
out[..self.outputs].copy_from_slice(&v[..self.outputs]);
|
||||
}
|
||||
|
||||
/// Removes no-op stages and cancels adjacent inverse pairs (Lab↔XYZ, encode/decode).
|
||||
pub fn optimize(&mut self) {
|
||||
let mut out: Vec<Stage> = Vec::with_capacity(self.stages.len());
|
||||
|
||||
+105
-1
@@ -42,6 +42,8 @@ pub const GRID_4D: usize = 17;
|
||||
const TABLE_1D: usize = GpuMatrixShaper::INPUT_INTERVALS;
|
||||
/// Resolution of output shaper tables (indexed by `x^(1/4)`).
|
||||
const OUT_TABLE: usize = GpuMatrixShaper::OUTPUT_ENTRIES;
|
||||
// The blocked matrix/TRC path wraps table indices with a mask instead of a bounds check.
|
||||
const _: () = assert!(OUT_TABLE.is_power_of_two());
|
||||
|
||||
/// ICC v4 perceptual reference medium black (ICC.1:2010 §6.3.4.4), XYZ.
|
||||
const PERCEPTUAL_BLACK: [f64; 3] = [0.00336, 0.0034731, 0.00287];
|
||||
@@ -91,6 +93,8 @@ pub struct Transform {
|
||||
post: Option<Post>,
|
||||
/// Pre-located grid coordinates per 8-bit code: (offset contribution, fraction) per input.
|
||||
grid8: Vec<[(u32, f32); 256]>,
|
||||
/// For an exact core: the first stage's curves at every 8-bit code.
|
||||
exact8: Vec<[f32; 256]>,
|
||||
}
|
||||
|
||||
impl Transform {
|
||||
@@ -178,7 +182,13 @@ impl Transform {
|
||||
.collect(),
|
||||
_ => Vec::new(),
|
||||
};
|
||||
Transform { inputs, outputs, opts, pipeline, core, post, grid8 }
|
||||
let exact8 = match (&core, pipeline.stages.first()) {
|
||||
(Core::Exact, Some(Stage::Curves(cs))) if cs.len() == inputs && inputs <= 4 => {
|
||||
cs.iter().map(|c| std::array::from_fn(|i| c.eval(i as f32 / 255.0))).collect()
|
||||
}
|
||||
_ => Vec::new(),
|
||||
};
|
||||
Transform { inputs, outputs, opts, pipeline, core, post, grid8, exact8 }
|
||||
}
|
||||
|
||||
/// White preservation: the device-link node at the source's device white gets exactly the
|
||||
@@ -386,6 +396,10 @@ impl Transform {
|
||||
}
|
||||
}
|
||||
}
|
||||
Core::Shaper { curves8, matrix: Some((3, 3, m, off)), .. } if self.shaper3_ok(curves8.len(), m.len(), off.len()) => {
|
||||
let load = |sp: &[u8]| [curves8[0][sp[0] as usize], curves8[1][sp[1] as usize], curves8[2][sp[2] as usize]];
|
||||
self.shaper3_blocks(s, ss, d, ds, extra, m, off, load, q);
|
||||
}
|
||||
Core::Shaper { curves8, matrix, .. } => {
|
||||
let mut v = [0.0f32; 16];
|
||||
for (sp, dp) in s.chunks_exact(ss).zip(d.chunks_exact_mut(ds)) {
|
||||
@@ -402,6 +416,21 @@ impl Transform {
|
||||
}
|
||||
}
|
||||
}
|
||||
Core::Exact if !self.exact8.is_empty() => {
|
||||
for (sp, dp) in s.chunks_exact(ss).zip(d.chunks_exact_mut(ds)) {
|
||||
let mut v = [0.0f32; 16];
|
||||
for k in 0..self.inputs {
|
||||
v[k] = self.exact8[k][sp[k] as usize];
|
||||
}
|
||||
self.pipeline.eval_after_first(&mut v, &mut out);
|
||||
for k in 0..self.outputs {
|
||||
dp[k] = q(out[k]);
|
||||
}
|
||||
for e in 0..extra {
|
||||
dp[self.outputs + e] = sp[self.inputs + e];
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let mut inp = [0.0f32; 16];
|
||||
for (sp, dp) in s.chunks_exact(ss).zip(d.chunks_exact_mut(ds)) {
|
||||
@@ -420,6 +449,74 @@ impl Transform {
|
||||
}
|
||||
}
|
||||
|
||||
fn shaper3_ok(&self, curves: usize, m: usize, off: usize) -> bool {
|
||||
self.inputs == 3
|
||||
&& self.outputs == 3
|
||||
&& curves == 3
|
||||
&& m == 9
|
||||
&& off == 3
|
||||
&& self.post.as_ref().is_some_and(|p| p.tables.len() == 3 && p.tables.iter().all(|t| t.len() == OUT_TABLE))
|
||||
}
|
||||
|
||||
/// Matrix/TRC transform of whole pixels in blocks: the matrix and the output-table index
|
||||
/// maths run over a block of pixels at a time (same operations, same order, per pixel).
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn shaper3_blocks<S: Copy>(
|
||||
&self,
|
||||
s: &[S],
|
||||
ss: usize,
|
||||
d: &mut [S],
|
||||
ds: usize,
|
||||
extra: usize,
|
||||
m: &[f32],
|
||||
off: &[f32],
|
||||
load: impl Fn(&[S]) -> [f32; 3],
|
||||
store: impl Fn(f32) -> S,
|
||||
) {
|
||||
const B: usize = 64;
|
||||
let Some(post) = self.post.as_ref() else { return };
|
||||
let tabs = [&post.tables[0][..OUT_TABLE], &post.tables[1][..OUT_TABLE], &post.tables[2][..OUT_TABLE]];
|
||||
let m: [f32; 9] = std::array::from_fn(|i| m[i]);
|
||||
let off: [f32; 3] = std::array::from_fn(|i| off[i]);
|
||||
let n_px = (s.len() / ss).min(d.len() / ds);
|
||||
let mut x = [[0.0f32; B]; 3];
|
||||
let mut start = 0;
|
||||
while start < n_px {
|
||||
let n = B.min(n_px - start);
|
||||
let sb = &s[start * ss..(start + n) * ss];
|
||||
let db = &mut d[start * ds..(start + n) * ds];
|
||||
for (j, sp) in sb.chunks_exact(ss).enumerate() {
|
||||
let [a, b, c] = load(sp);
|
||||
for r in 0..3 {
|
||||
let mut acc = off[r];
|
||||
acc += m[r * 3] * a;
|
||||
acc += m[r * 3 + 1] * b;
|
||||
acc += m[r * 3 + 2] * c;
|
||||
x[r][j] = acc;
|
||||
}
|
||||
}
|
||||
for (ch, t) in tabs.iter().enumerate() {
|
||||
let mut u = [0.0f32; B];
|
||||
let mut ix = [0i32; B];
|
||||
for j in 0..B {
|
||||
let v = x[ch][j].clamp(0.0, 1.0).sqrt().sqrt() * (OUT_TABLE - 1) as f32;
|
||||
let i = (v as i32).min(OUT_TABLE as i32 - 2);
|
||||
ix[j] = i;
|
||||
u[j] = v - i as f32;
|
||||
}
|
||||
for (j, dp) in db.chunks_exact_mut(ds).enumerate() {
|
||||
let i = ix[j] as usize & (OUT_TABLE - 1);
|
||||
let (v0, v1) = (t[i], t[(i + 1) & (OUT_TABLE - 1)]);
|
||||
dp[ch] = store(v0 + (v1 - v0) * u[j]);
|
||||
}
|
||||
}
|
||||
for (sp, dp) in sb.chunks_exact(ss).zip(db.chunks_exact_mut(ds)) {
|
||||
dp[3..3 + extra].copy_from_slice(&sp[3..3 + extra]);
|
||||
}
|
||||
start += n;
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts 16-bit pixels (see [`Transform::convert_f32`] for the layout).
|
||||
pub fn convert_u16(&self, src: &[u16], src_stride: usize, dst: &mut [u16], dst_stride: usize, copy_extra: bool) {
|
||||
self.check(src_stride, dst_stride);
|
||||
@@ -428,6 +525,13 @@ impl Transform {
|
||||
}
|
||||
|
||||
fn run_u16(&self, s: &[u16], ss: usize, d: &mut [u16], ds: usize, extra: usize) {
|
||||
if let Core::Shaper { curves, matrix: Some((3, 3, m, off)), .. } = &self.core
|
||||
&& self.shaper3_ok(curves.len(), m.len(), off.len())
|
||||
{
|
||||
let load =
|
||||
|sp: &[u16]| [table(&curves[0], sp[0] as f32 / 65535.0), table(&curves[1], sp[1] as f32 / 65535.0), table(&curves[2], sp[2] as f32 / 65535.0)];
|
||||
return self.shaper3_blocks(s, ss, d, ds, extra, m, off, load, |v: f32| (v.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16);
|
||||
}
|
||||
let mut inp = [0.0f32; 16];
|
||||
let mut out = [0.0f32; 16];
|
||||
for (sp, dp) in s.chunks_exact(ss).zip(d.chunks_exact_mut(ds)) {
|
||||
|
||||
@@ -819,3 +819,93 @@ fn device_links_parse_and_are_reported_unsupported() {
|
||||
// The output space still bounds the tag: a CMYK→CMYK link with a 4→3 table is malformed.
|
||||
assert!(matches!(Profile::parse(&device_link(b"CMYK", b"CMYK", 4, 3)), Err(photocraft_cms::CmsError::Invalid(_))));
|
||||
}
|
||||
|
||||
/// The blocked matrix/TRC path (64 pixels at a time) must match the per-pixel evaluation for
|
||||
/// every pixel count around the block size, with and without extra channels.
|
||||
#[test]
|
||||
fn matrix_trc_blocks_match_per_pixel_eval() {
|
||||
let pairs = [(Builtin::Srgb, Builtin::DisplayP3), (Builtin::ProPhotoCompat, Builtin::Srgb), (Builtin::AdobeRgbCompat, Builtin::Rec2020)];
|
||||
for (a, b) in pairs {
|
||||
let t = Transform::new(a.profile(), b.profile(), Intent::RelativeColorimetric, true).unwrap();
|
||||
for &(ss, ds, copy) in &[(3usize, 3usize, false), (4, 4, true), (5, 4, true)] {
|
||||
let max = 20011;
|
||||
let seed = |i: usize| (i.wrapping_mul(2654435761) >> 7) as u32;
|
||||
let src16: Vec<u16> = (0..max * ss).map(|i| (seed(i) >> 3) as u16).collect();
|
||||
let src8: Vec<u8> = (0..max * ss).map(|i| (seed(i) >> 11) as u8).collect();
|
||||
let mut full8 = vec![0u8; max * ds];
|
||||
t.convert_u8(&src8, ss, &mut full8, ds, copy);
|
||||
for px in [1usize, 2, 63, 64, 65, 127, 128, 129, 1000] {
|
||||
let mut o16 = vec![0u16; px * ds];
|
||||
t.convert_u16(&src16[..px * ss], ss, &mut o16, ds, copy);
|
||||
let mut o8 = vec![0u8; px * ds];
|
||||
t.convert_u8(&src8[..px * ss], ss, &mut o8, ds, copy);
|
||||
assert_eq!(o8[..], full8[..px * ds], "u8 prefix of a longer buffer, {px} px");
|
||||
for p in 0..px {
|
||||
let inp: Vec<f32> = (0..3).map(|k| src16[p * ss + k] as f32 / 65535.0).collect();
|
||||
let mut want = [0.0f32; 3];
|
||||
t.eval_fast(&inp, &mut want);
|
||||
for k in 0..3 {
|
||||
assert_eq!(o16[p * ds + k], (want[k].clamp(0.0, 1.0) * 65535.0 + 0.5) as u16, "u16 px {p} ch {k} of {px}");
|
||||
}
|
||||
if copy && ds > 3 {
|
||||
assert_eq!(o16[p * ds + 3], src16[p * ss + 3]);
|
||||
assert_eq!(o8[p * ds + 3], src8[p * ss + 3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 8-bit and 16-bit conversion through an analytic pipeline (no lookup table, here RGB to Lab)
|
||||
/// must equal the exact per-pixel evaluation. The 8-bit path tabulates the first stage's
|
||||
/// curves per code; `curves_are_tables` selects whether those curves are parametric (their
|
||||
/// `eval` is analytic) or `curv` tables (their `eval` interpolates inside the same loop).
|
||||
fn check_exact_core(src_profile: &Profile, curves_are_tables: bool) {
|
||||
let trc = src_profile.trc.as_ref().expect("matrix/TRC source");
|
||||
assert!(trc.iter().all(|c| matches!(c, photocraft_cms::Curve::Table(_)) == curves_are_tables), "TRC kind");
|
||||
let t = Transform::new(src_profile, lab(), Intent::RelativeColorimetric, true).unwrap();
|
||||
let px = 256usize * 256;
|
||||
let src: Vec<u8> = (0..px as u32).flat_map(|i| [(i & 255) as u8, (i >> 8) as u8, (i.wrapping_mul(2654435761) >> 13) as u8, 200]).collect();
|
||||
let mut out = vec![0u8; px * 4];
|
||||
t.convert_u8(&src, 4, &mut out, 4, true);
|
||||
for (sp, dp) in src.as_chunks::<4>().0.iter().zip(out.as_chunks::<4>().0) {
|
||||
let inp = [sp[0] as f32 / 255.0, sp[1] as f32 / 255.0, sp[2] as f32 / 255.0];
|
||||
let mut want = [0.0f32; 3];
|
||||
t.eval(&inp, &mut want);
|
||||
for k in 0..3 {
|
||||
assert_eq!(dp[k], (want[k].clamp(0.0, 1.0) * 255.0 + 0.5) as u8, "u8 {sp:?} ch {k}");
|
||||
}
|
||||
assert_eq!(dp[3], 200);
|
||||
}
|
||||
// 16-bit input stays off the 8-bit table and must agree with the untabulated evaluation.
|
||||
let src16: Vec<u16> = (0..px * 4).map(|i| if i % 4 == 3 { 51400 } else { (i.wrapping_mul(2654435761) >> 9) as u16 }).collect();
|
||||
let mut out16 = vec![0u16; px * 4];
|
||||
t.convert_u16(&src16, 4, &mut out16, 4, true);
|
||||
for (sp, dp) in src16.as_chunks::<4>().0.iter().zip(out16.as_chunks::<4>().0) {
|
||||
let inp = [sp[0] as f32 / 65535.0, sp[1] as f32 / 65535.0, sp[2] as f32 / 65535.0];
|
||||
let mut want = [0.0f32; 3];
|
||||
t.eval(&inp, &mut want);
|
||||
for k in 0..3 {
|
||||
assert_eq!(dp[k], (want[k].clamp(0.0, 1.0) * 65535.0 + 0.5) as u16, "u16 {sp:?} ch {k}");
|
||||
}
|
||||
assert_eq!(dp[3], 51400);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_core_u8_matches_exact_eval() {
|
||||
check_exact_core(srgb(), false);
|
||||
}
|
||||
|
||||
/// Same, with `curv` table TRCs: the first-stage table is built from `Curve::eval`'s table
|
||||
/// branch and must still be applied to its own channel.
|
||||
#[test]
|
||||
fn exact_core_u8_matches_exact_eval_with_table_trc() {
|
||||
// Distinct tables per channel, so a first stage applied to the wrong channel shows up.
|
||||
let mut p = srgb_as_v2_tables();
|
||||
let table = |g: f64| photocraft_cms::Curve::Table((0..1024).map(|i| ((i as f64 / 1023.0).powf(g) * 65535.0).round() as f32 / 65535.0).collect());
|
||||
p.trc = Some([table(1.8), table(2.2), table(2.6)]);
|
||||
let table_trc = Profile::parse(&p.with_encoded_bytes().to_bytes()).unwrap();
|
||||
check_exact_core(&table_trc, true);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
//! - any other `layer` id becomes the layer's name when the name is unique in the document, and
|
||||
//! `layer.select` always does (selecting is choosing a layer by name, as Photoshop records
|
||||
//! it); an ambiguous name keeps the id, which still replays on the recording document;
|
||||
//! - a mask drop's destination (`layer.layerMask.moveTo`'s `to`) becomes the layer's name, with
|
||||
//! `toOccurrence` when several layers share it;
|
||||
//! - the Type tool's keystrokes fold into the `type.create` that made the layer. On an existing
|
||||
//! layer, each replacement stays separate so replay keeps style runs between edited spans.
|
||||
//!
|
||||
@@ -69,6 +71,9 @@ pub(crate) fn portable(s: &Session, id: &str, mut params: Value, before: &Before
|
||||
object.insert("yPercent".into(), serde_json::json!(y_percent));
|
||||
}
|
||||
}
|
||||
if id == "layer.layerMask.moveTo" {
|
||||
portable_mask_destination(doc, &mut params);
|
||||
}
|
||||
let Some(target) = params.get("layer").and_then(Value::as_u64).map(LayerId) else { return params };
|
||||
let Some(obj) = params.as_object_mut() else { return params };
|
||||
let only_selected = before.selected.len() <= 1;
|
||||
@@ -85,6 +90,22 @@ pub(crate) fn portable(s: &Session, id: &str, mut params: Value, before: &Before
|
||||
params
|
||||
}
|
||||
|
||||
/// A mask drop's `to` id becomes the layer's name, and `toOccurrence` which of several layers of
|
||||
/// that name it is, in document order. Moving a mask leaves the stack as it is, so the count means
|
||||
/// the same layer when the step replays. It is taken from the document before the step: dropping
|
||||
/// on the Background renames it "Layer 0".
|
||||
fn portable_mask_destination(doc: &Document, params: &mut Value) {
|
||||
let Some(to) = params.get("to").and_then(Value::as_u64).map(LayerId) else { return };
|
||||
let Some(layer) = doc.layer(to) else { return };
|
||||
let matches: Vec<_> = doc.walk().into_iter().filter(|(_, _, l)| l.name == layer.name).map(|(_, _, l)| l.id).collect();
|
||||
let Some(occurrence) = matches.iter().position(|id| *id == to) else { return };
|
||||
let Some(fields) = params.as_object_mut() else { return };
|
||||
fields.insert("to".into(), Value::String(layer.name.clone()));
|
||||
if matches.len() > 1 {
|
||||
fields.insert("toOccurrence".into(), occurrence.into());
|
||||
}
|
||||
}
|
||||
|
||||
/// The text layer a journaled `type.create` made or `type.edit` changed, so the keystrokes that
|
||||
/// follow fold into that entry only when they type into the same layer (the Type tool selects
|
||||
/// a clicked layer without a command, so the journal alone cannot tell).
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
//! Pressure for Size starts off (#11809). The reference application leaves both pressure toggles
|
||||
//! in the options bar unhighlighted after Reset Tool, so a fresh brush has neither on — and the
|
||||
//! brush Reset Tool hands back is `BrushSettings::default()` plus smoothing
|
||||
//! (`crates/ui-egui/src/canvas/paint_mouse.rs::fresh_brush`), so the default is what the tool
|
||||
//! reset reports through `brush.get`. The options bar's two `circle-dot` buttons are drawn from
|
||||
//! these same two fields (`crates/ui-egui/src/panels/options_bar.rs`), so off here means drawn
|
||||
//! off there.
|
||||
|
||||
use crate::Session;
|
||||
use photocraft_paint::brush::BrushSettings;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
/// The brush a fresh session starts with: neither pressure toggle on.
|
||||
#[test]
|
||||
fn the_default_brush_has_neither_pressure_toggle_on() {
|
||||
let b = BrushSettings::default();
|
||||
assert!(!b.pressure_size, "Pressure for Size is off by default, as the reference leaves it after Reset Tool (#11809)");
|
||||
assert!(!b.pressure_opacity, "Pressure for Opacity stays off");
|
||||
}
|
||||
|
||||
/// What `brush.get` answers on a fresh session, which is what the options bar reads to draw the
|
||||
/// two toggles.
|
||||
#[test]
|
||||
fn brush_get_reports_both_pressure_toggles_off() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 32, "height": 32})).unwrap();
|
||||
let got = s.execute("brush.get", json!({})).unwrap();
|
||||
assert_eq!(got["pressureSize"], json!(false), "brush.get reports Pressure for Size off (#11809)");
|
||||
assert_eq!(got["pressureOpacity"], json!(false), "brush.get reports Pressure for Opacity off");
|
||||
// The session's own starting brush is the default brush at Photoshop's 10 % smoothing — the
|
||||
// same brush Reset Tool hands back — so only the smoothing differs.
|
||||
assert_eq!(
|
||||
s.tools.brush,
|
||||
BrushSettings { smoothing: photocraft_paint::brush::Smoothing { amount: 0.1, ..Default::default() }, ..BrushSettings::default() }
|
||||
);
|
||||
}
|
||||
|
||||
/// A brush that does not name the field — a preset saved before #11809, an older recorded
|
||||
/// command — takes the off default rather than the old on one.
|
||||
#[test]
|
||||
fn a_brush_that_omits_the_field_takes_the_off_default() {
|
||||
let b: BrushSettings = serde_json::from_value(json!({"size": 30.0, "hardness": 1.0})).unwrap();
|
||||
assert!(!b.pressure_size, "an omitted pressureSize reads as off (#11809)");
|
||||
assert!(!b.pressure_opacity, "an omitted pressureOpacity reads as off");
|
||||
assert_eq!(b.size, 30.0, "the fields that are there still apply");
|
||||
}
|
||||
|
||||
/// Switching it on still works: `tools.setBrush` writes the field `brush.get` reads back, and the
|
||||
/// same field the options-bar button is drawn from.
|
||||
#[test]
|
||||
fn pressure_for_size_still_switches_on_and_off() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 32, "height": 32})).unwrap();
|
||||
s.execute("tools.setBrush", json!({"brush": {"pressureSize": true}})).unwrap();
|
||||
assert!(s.tools.brush.pressure_size, "the toggle still turns on");
|
||||
assert_eq!(s.execute("brush.get", json!({})).unwrap()["pressureSize"], json!(true));
|
||||
s.execute("tools.setBrush", json!({"brush": {"pressureSize": false}})).unwrap();
|
||||
assert!(!s.tools.brush.pressure_size, "and off again");
|
||||
assert!(!s.tools.brush.pressure_opacity, "it never touches Pressure for Opacity");
|
||||
}
|
||||
|
||||
/// One stroke of a pressure ramp (0.3 → 0.65 → 1.0) across a new empty layer with `brush`: the
|
||||
/// layer's pixels and what the stroke covers.
|
||||
fn ramp(brush: Value) -> (Vec<f32>, photocraft_geom::Rect) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 80, "height": 40})).unwrap();
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
s.execute("paint.stroke", json!({"points": [[20.0, 20.0, 0.3], [40.0, 20.0, 0.65], [60.0, 20.0, 1.0]], "size": 24.0, "color": "#ff0000", "brush": brush}))
|
||||
.unwrap();
|
||||
let st = s.active().unwrap();
|
||||
let surf = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap();
|
||||
(surf.read_region(st.doc.bounds()), surf.content_bounds())
|
||||
}
|
||||
|
||||
/// Done when 2: a pen stroke with varying pressure and the default brush keeps a constant size,
|
||||
/// so it paints exactly what the same stroke with Pressure for Size turned off does — and
|
||||
/// something else with it turned on, which is what makes this say anything.
|
||||
#[test]
|
||||
fn a_default_brush_stroke_keeps_its_size_under_a_pressure_ramp() {
|
||||
let (off, off_box) = ramp(json!({"spacing": 0.25, "pressureSize": false}));
|
||||
let (on, on_box) = ramp(json!({"spacing": 0.25, "pressureSize": true}));
|
||||
let (dflt, dflt_box) = ramp(json!({"spacing": 0.25}));
|
||||
assert_ne!(on, off, "the ramp alone does not change the stroke: Pressure for Size has to");
|
||||
assert_eq!(dflt, off, "the default brush paints like Pressure for Size off (#11809)");
|
||||
assert_eq!(dflt_box, off_box, "and reaches as far under the whole ramp");
|
||||
assert_ne!(dflt_box, on_box, "the turned-on brush still tapers the stroke's ends");
|
||||
}
|
||||
@@ -67,6 +67,8 @@ pub mod color_cmds;
|
||||
pub mod color_to_alpha_cmds;
|
||||
pub mod comps_cmds;
|
||||
pub mod contour_lib_cmds;
|
||||
#[cfg(test)]
|
||||
mod count_delete_group_tests;
|
||||
pub mod cutout_cmds;
|
||||
pub mod distort_cmds;
|
||||
pub mod document_preset_cmds;
|
||||
@@ -169,7 +171,7 @@ use enabled::{can_redo, can_undo, has_doc, has_layer, has_pixel_layer, has_pixel
|
||||
pub use layer_params::blend_from_str;
|
||||
pub(crate) use layer_params::{NEW_LAYER_PATH, explicit_layer, layer_listing, layer_panel_option, layer_param, mask_at_depth, mask_new_layer, selection_mask};
|
||||
pub(crate) use layer_references::{recorded_drop, recorded_layer};
|
||||
use ops::{arrange, merge_group, set_mask, set_mask_and_deselect};
|
||||
use ops::{arrange, merge_group, move_mask, set_mask, set_mask_and_deselect};
|
||||
pub(crate) use ops::{layer_copy, translate_layer};
|
||||
use params::{bad, set_clipped};
|
||||
pub(crate) use params::{check_f32_values, cmyk_param, color_param, int, int_i32, parse_hex, u32_id, u32_id_param};
|
||||
@@ -807,6 +809,15 @@ fn build() -> Vec<CommandSpec> {
|
||||
"Delete Layer Mask",
|
||||
None
|
||||
)),
|
||||
cmd!(
|
||||
"layer.layerMask.moveTo",
|
||||
"Move Layer Mask",
|
||||
[],
|
||||
None,
|
||||
r##"{"layer":id? (the mask's layer, default active),"to":id|name (the layer or group it goes to; a recorded step names it),"toOccurrence":n? (which of several layers named `to`, in document order),"copy":bool=false (⌥-drag: a copy goes and the original stays),"replace":bool=false (replace a mask already there; otherwise refused)} → {layer,to,copy}"##,
|
||||
has_layer,
|
||||
move_mask
|
||||
),
|
||||
cmd!("layer.mergeDown", "Merge Down", ["Layer"], None, r##"{"layer":id?}"##, has_layer, |s, p| {
|
||||
let id = layer_param(s, p)?;
|
||||
s.edit("Merge Down", |doc, active| {
|
||||
|
||||
@@ -600,6 +600,84 @@ fn recorded_layer_selection_replays_by_name_on_another_document() {
|
||||
/// #7845: dragging a layer in the Layers panel while recording keeps the drop target by name,
|
||||
/// so the action plays on another document (whose layer ids differ) instead of failing with
|
||||
/// "no such layer". The reporter's steps, replayed on a new document.
|
||||
/// #10485: a mask thumbnail dropped on another layer while recording keeps its destination by
|
||||
/// name, so the action moves the mask to the same layer in another document (whose ids differ)
|
||||
/// rather than to whichever layer has the recorded id there.
|
||||
#[test]
|
||||
fn recorded_mask_drop_replays_on_another_document() {
|
||||
let masked = |s: &mut Session, name: &str| {
|
||||
let id = s.execute("layer.new.layer", json!({"name": name})).unwrap()["layer"].as_u64().unwrap();
|
||||
s.execute("select.rect", json!({"x": 1, "y": 1, "width": 4, "height": 4})).unwrap();
|
||||
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
|
||||
id
|
||||
};
|
||||
let has_mask = |s: &Session, name: &str| s.active().unwrap().doc.walk().iter().any(|(_, _, l)| l.name == name && l.mask.is_some());
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
|
||||
let photo = masked(&mut s, "Photo");
|
||||
let shadow = s.execute("layer.new.layer", json!({"name": "Shadow"})).unwrap()["layer"].as_u64().unwrap();
|
||||
s.execute("actions.record", json!({"name": "Mask to shadow"})).unwrap();
|
||||
s.execute("layer.layerMask.moveTo", json!({"layer": photo, "to": shadow, "copy": false})).unwrap();
|
||||
s.execute("actions.stop", json!({})).unwrap();
|
||||
assert_eq!(s.actions.list[0].steps[0].1, json!({"layer": "Photo", "to": "Shadow", "copy": false}));
|
||||
// Another document: an extra layer between them, so the recorded id is a different layer.
|
||||
let saved = serde_json::to_string(&s.actions.list).unwrap();
|
||||
let mut replay = Session::new();
|
||||
replay.actions.list = serde_json::from_str(&saved).unwrap();
|
||||
replay.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
|
||||
masked(&mut replay, "Photo");
|
||||
replay.execute("layer.new.layer", json!({"name": "Extra"})).unwrap();
|
||||
replay.execute("layer.new.layer", json!({"name": "Shadow"})).unwrap();
|
||||
let result = replay.execute("actions.play", json!({"action": 0})).unwrap();
|
||||
assert!(result.get("failed").is_none(), "{result}");
|
||||
assert!(has_mask(&replay, "Shadow"), "the mask went to Shadow");
|
||||
assert!(!has_mask(&replay, "Photo") && !has_mask(&replay, "Extra"), "and nowhere else");
|
||||
}
|
||||
|
||||
/// A mask dropped on one of several same-named layers records which of them it is, so replay
|
||||
/// picks the same one (not the topmost of that name), and an explicit name needs the occurrence.
|
||||
#[test]
|
||||
fn recorded_mask_drop_onto_a_duplicate_name_replays_on_the_same_layer() {
|
||||
let build = |s: &mut Session, extra: bool| {
|
||||
s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
|
||||
if extra {
|
||||
s.execute("layer.new.layer", json!({"name": "Extra"})).unwrap();
|
||||
}
|
||||
let photo = s.execute("layer.new.layer", json!({"name": "Photo"})).unwrap()["layer"].as_u64().unwrap();
|
||||
s.execute("select.rect", json!({"x": 1, "y": 1, "width": 4, "height": 4})).unwrap();
|
||||
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
|
||||
let lower = s.execute("layer.new.layer", json!({"name": "Twin"})).unwrap()["layer"].as_u64().unwrap();
|
||||
let upper = s.execute("layer.new.layer", json!({"name": "Twin"})).unwrap()["layer"].as_u64().unwrap();
|
||||
(photo, lower, upper)
|
||||
};
|
||||
let mask_on = |s: &Session, id: u64| s.active().unwrap().doc.layer(photocraft_doc::LayerId(id)).unwrap().mask.is_some();
|
||||
let mut s = Session::new();
|
||||
let (photo, lower, upper) = build(&mut s, false);
|
||||
s.execute("actions.record", json!({"name": "Copy mask"})).unwrap();
|
||||
s.execute("layer.layerMask.moveTo", json!({"layer": photo, "to": lower, "copy": true})).unwrap();
|
||||
s.execute("actions.stop", json!({})).unwrap();
|
||||
assert_eq!(s.actions.list[0].steps[0].1, json!({"layer": "Photo", "to": "Twin", "toOccurrence": 0, "copy": true}));
|
||||
assert!(mask_on(&s, lower) && !mask_on(&s, upper));
|
||||
let saved = serde_json::to_string(&s.actions.list).unwrap();
|
||||
let mut replay = Session::new();
|
||||
replay.actions.list = serde_json::from_str(&saved).unwrap();
|
||||
let (photo2, lower2, upper2) = build(&mut replay, true);
|
||||
assert_ne!((photo, lower, upper), (photo2, lower2, upper2), "the ids differ");
|
||||
let result = replay.execute("actions.play", json!({"action": 0})).unwrap();
|
||||
assert!(result.get("failed").is_none(), "{result}");
|
||||
assert!(mask_on(&replay, photo2), "a copy: the original stays");
|
||||
assert!(mask_on(&replay, lower2), "the copy went to the lower Twin");
|
||||
assert!(!mask_on(&replay, upper2), "not to the upper one");
|
||||
// Explicit calls: a name shared by several layers needs `toOccurrence`; ids still work.
|
||||
let err = replay.execute("layer.layerMask.moveTo", json!({"layer": photo2, "to": "Twin", "copy": true})).unwrap_err().to_string();
|
||||
assert!(err.contains("toOccurrence"), "{err}");
|
||||
replay.execute("layer.layerMask.moveTo", json!({"layer": photo2, "to": "Twin", "toOccurrence": 1, "copy": true})).unwrap();
|
||||
assert!(mask_on(&replay, upper2), "occurrence 1 is the upper Twin");
|
||||
replay.execute("layer.layerMask.delete", json!({"layer": upper2})).unwrap();
|
||||
replay.execute("layer.layerMask.moveTo", json!({"layer": photo2, "to": upper2})).unwrap();
|
||||
assert!(mask_on(&replay, upper2) && !mask_on(&replay, photo2), "a numeric id still names the layer");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recorded_layer_drag_replays_on_another_document() {
|
||||
for depth in [8, 16, 32] {
|
||||
|
||||
@@ -434,6 +434,33 @@ fn replace_color(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// Localized Color Clusters, as in Select › Color Range: the colour only matches within Range
|
||||
// (a percentage of the canvas's longer side) of where it was picked.
|
||||
let points = sample_points(p).map_err(|m| bad(CMD, m))?;
|
||||
// Several samples (Add to Sample / Subtract from Sample, #6280): the colours match as
|
||||
// Select › Color Range's do for the same clicks, through its own matcher.
|
||||
// A malformed `subtractPoints` is refused here, as malformed `points` are, rather than read as
|
||||
// no subtraction by the single-colour path.
|
||||
let subtracted = match p.get("subtractPoints") {
|
||||
None => false,
|
||||
Some(v) => !v.as_array().ok_or_else(|| bad(CMD, "`subtractPoints` must be [[x, y], …]"))?.is_empty(),
|
||||
};
|
||||
if subtracted || points.len() > 1 {
|
||||
let mut q = json!({"points": p.get("points").cloned().unwrap_or(Value::Array(Vec::new())), "fuzziness": fuzz});
|
||||
// With no Add samples, the subtraction starts from the dialog's From colour, not the
|
||||
// foreground; with them, the samples alone span the range, as in Color Range.
|
||||
// Forwarded as resolved (a `null` or missing colour is the foreground), as RGB 0..1.
|
||||
if points.is_empty() {
|
||||
q["color"] = json!([c[0], c[1], c[2]]);
|
||||
}
|
||||
for key in ["subtractPoints", "order", "localized", "range"] {
|
||||
if let Some(v) = p.get(key) {
|
||||
q[key] = v.clone();
|
||||
}
|
||||
}
|
||||
let samples = crate::selection_cmds::ColorRangeSamples::new(s, &q).map_err(|e| bad(CMD, e.to_string()))?;
|
||||
return rgba_edit(s, "Replace Color", p, |px, r| {
|
||||
let mask = samples.coverage(px, r.width() as usize, 1.0, [r.x0 as f32, r.y0 as f32]);
|
||||
tone::replace_color_masked(px, &mask, h, sat, l);
|
||||
});
|
||||
}
|
||||
let localized = if p.get("localized").and_then(Value::as_bool).unwrap_or(false) {
|
||||
if points.is_empty() {
|
||||
return Err(bad(CMD, "localized clusters need eyedropper `points` (where the colour was picked)"));
|
||||
@@ -587,7 +614,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"image.adjustments.replaceColor",
|
||||
"Replace Color…",
|
||||
["Image", "Adjustments"],
|
||||
r##"{"localized":bool=false,"color":color,"fuzziness":0..200=40,"range":0..100=100,"hue":-180..180=0,"saturation":-100..100=0,"lightness":-100..100=0,"points":[[x,y],…]?,"labMinimum":[L,a,b]?,"labMaximum":[L,a,b]?} (Lab bounds: a finite ordered D50 box, mutually exclusive with color/points/localized/range; composite pixel-layer target only. Otherwise color defaults to the foreground; localized points use range % of the longer side.)"##,
|
||||
r##"{"localized":bool=false,"color":color,"fuzziness":0..200=40,"range":0..100=100,"hue":-180..180=0,"saturation":-100..100=0,"lightness":-100..100=0,"points":[[x,y],…]?,"subtractPoints":[[x,y],…]?,"order":["+"|"-",…]?,"labMinimum":[L,a,b]?,"labMaximum":[L,a,b]?} (Lab bounds: a finite ordered D50 box, mutually exclusive with color/points/subtractPoints/order/localized/range; composite pixel-layer target only. Otherwise color ("#rrggbb") defaults to the foreground; localized points use range % of the longer side; several "points" (Add to Sample) or any "subtractPoints" (Subtract from Sample) match as select.colorRange does for the same samples, "order" the click order.)"##,
|
||||
has_pixels,
|
||||
replace_color
|
||||
),
|
||||
|
||||
@@ -242,4 +242,20 @@ mod tests {
|
||||
assert_eq!(s.active().unwrap().doc, before);
|
||||
assert_eq!(s.active().unwrap().history.past_len(), history);
|
||||
}
|
||||
|
||||
/// Add to Sample / Subtract from Sample (#6280) are RGB samples: with Lab bounds they are
|
||||
/// refused, as `points` are, and the document is left as it was.
|
||||
#[test]
|
||||
fn lab_bounds_refuse_subtracted_samples_and_click_order() {
|
||||
for extra in [serde_json::json!({"subtractPoints": [[1, 1]]}), serde_json::json!({"order": ["+", "-"]}), serde_json::json!({"subtractPoints": []})] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", serde_json::json!({"width": 4, "height": 2})).unwrap();
|
||||
let before = s.active().unwrap().doc.clone();
|
||||
let mut p = serde_json::json!({"labMinimum": [0, -10, -10], "labMaximum": [100, 10, 10], "lightness": 50});
|
||||
p.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
|
||||
let err = s.execute(CMD, p).unwrap_err().to_string();
|
||||
assert!(err.contains("Lab bounds cannot be combined"), "{extra}: {err}");
|
||||
assert_eq!(s.active().unwrap().doc, before, "{extra}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,12 @@ fn changed(a: [f32; 4], b: [f32; 4]) -> f32 {
|
||||
(0..3).map(|c| (a[c] - b[c]).abs()).fold(0.0, f32::max)
|
||||
}
|
||||
|
||||
/// Every pixel of the active layer, row by row.
|
||||
fn pixels(s: &Session) -> Vec<[f32; 4]> {
|
||||
let b = doc(s).bounds();
|
||||
(b.y0..b.y1).flat_map(|y| (b.x0..b.x1).map(move |x| (x, y))).map(|(x, y)| rgba(s, x, y)).collect()
|
||||
}
|
||||
|
||||
fn doc(s: &Session) -> &Document {
|
||||
&s.active().unwrap().doc
|
||||
}
|
||||
@@ -754,6 +760,239 @@ fn replace_color_localized_clusters_only_change_the_colour_near_the_sample() {
|
||||
assert!(err.to_string().contains("bad point"), "{err}");
|
||||
}
|
||||
|
||||
/// #6280: Add to Sample and Subtract from Sample, as in Photoshop: several picked colours are
|
||||
/// all replaced, and a subtracted one is left alone, matching as Select › Color Range does for
|
||||
/// the same samples.
|
||||
#[test]
|
||||
fn replace_color_adds_and_subtracts_samples_as_color_range_does() {
|
||||
// Red on the left (x < 16), blue in the middle, green on the right (32 ≤ x < 44), and a
|
||||
// lighter red at the edge that the samples only partly cover.
|
||||
let (red, blue, green, pink) = ([0.85, 0.1, 0.1, 1.0], [0.1, 0.15, 0.85, 1.0], [0.1, 0.8, 0.15, 1.0], [0.95, 0.25, 0.25, 1.0]);
|
||||
let mut s = session(8, "rgb");
|
||||
paint(&mut s, |x, _| {
|
||||
if x < 16 {
|
||||
red
|
||||
} else if x < 32 {
|
||||
blue
|
||||
} else if x < 44 {
|
||||
green
|
||||
} else {
|
||||
pink
|
||||
}
|
||||
});
|
||||
let before = (rgba(&s, 4, 16), rgba(&s, 24, 16), rgba(&s, 40, 16), rgba(&s, 46, 16));
|
||||
let original = pixels(&s);
|
||||
let shift = |extra: Value| {
|
||||
let mut p = json!({"fuzziness": 40, "hue": 120, "saturation": 20});
|
||||
p.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
|
||||
p
|
||||
};
|
||||
// Red and blue sampled (one added): both change, green doesn't.
|
||||
s.execute("image.adjustments.replaceColor", shift(json!({"color": "#d91a1a", "points": [[4, 16], [24, 16]]}))).unwrap();
|
||||
assert!(changed(rgba(&s, 4, 16), before.0) > 0.1, "red is replaced");
|
||||
assert!(changed(rgba(&s, 24, 16), before.1) > 0.1, "the added blue is replaced");
|
||||
assert_eq!(rgba(&s, 40, 16), before.2, "green wasn't sampled");
|
||||
let partial = rgba(&s, 46, 16);
|
||||
// One undo step brings every pixel back exactly.
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
assert!(pixels(&s) == original, "undo restores the pixels");
|
||||
// The same coverage as Select › Color Range for the same clicks.
|
||||
s.execute("select.colorRange", json!({"points": [[4, 16], [24, 16]], "fuzziness": 40})).unwrap();
|
||||
let sel = doc(&s).selection.clone().unwrap();
|
||||
for x in [4, 24, 40] {
|
||||
let want = sel.read_region(Rect::from_xywh(x, 16, 1, 1))[0];
|
||||
assert!((want > 0.5) == (x < 32), "Color Range at {x}: {want}");
|
||||
}
|
||||
// The lighter red is partly covered, and replaced in proportion: the blend of its colour and
|
||||
// its fully replaced colour at that coverage, not either one (as a 0.5 threshold would give).
|
||||
let k = sel.read_region(Rect::from_xywh(46, 16, 1, 1))[0];
|
||||
assert!(k > 0.1 && k < 0.9, "partial coverage: {k}");
|
||||
let mut full = [before.3];
|
||||
tone::replace_color_masked(&mut full, &[1.0], 120.0, 20.0, 0.0);
|
||||
let blend: [f32; 4] = std::array::from_fn(|c| before.3[c] + (full[0][c] - before.3[c]) * k);
|
||||
assert!(changed(partial, blend) < 1.5 / 255.0, "{partial:?} vs the blend {blend:?} at coverage {k}");
|
||||
assert!(changed(blend, before.3).min(changed(blend, full[0])) > 0.1, "the blend is far from both ends a threshold gives");
|
||||
s.execute("select.deselect", json!({})).unwrap();
|
||||
// A subtraction undoes exactly too. (Blue is far from the red sample here, so subtracting
|
||||
// it changes nothing; `replace_color_subtracting_inside_the_added_range_takes_a_colour_out`
|
||||
// has a subtraction that does.)
|
||||
let selected = pixels(&s);
|
||||
s.execute("image.adjustments.replaceColor", shift(json!({"points": [[4, 16]], "subtractPoints": [[24, 16]], "order": ["+", "-"]}))).unwrap();
|
||||
assert!(changed(rgba(&s, 4, 16), before.0) > 0.1);
|
||||
assert_eq!(rgba(&s, 40, 16), before.2);
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
assert!(pixels(&s) == selected, "undo restores the pixels");
|
||||
// Bad samples are refused, naming this command.
|
||||
let err = s.execute("image.adjustments.replaceColor", shift(json!({"points": [[4, 16]], "subtractPoints": [[999, 16]]}))).unwrap_err();
|
||||
assert!(err.to_string().contains("replaceColor") && err.to_string().contains("outside"), "{err}");
|
||||
}
|
||||
|
||||
/// FredApps review on #10443: a subtracted sample inside the added range. Photoshop's Subtract
|
||||
/// from Sample reshapes the samples' Lab box (`LabRange::subtract`): a subtracted colour inside
|
||||
/// it pulls the nearer edge onto itself, so it stays matched and the added colour beyond that
|
||||
/// edge drops out. Two added greys span dark to light; subtracting a grey near the light end
|
||||
/// leaves the light one unreplaced.
|
||||
#[test]
|
||||
fn replace_color_subtracting_inside_the_added_range_takes_a_colour_out() {
|
||||
let grey = |v: f32| [v, v, v, 1.0];
|
||||
// Dark grey (x < 12), mid grey (subtracted), light grey, black (never sampled).
|
||||
let mut s = session(8, "rgb");
|
||||
paint(&mut s, |x, _| match x {
|
||||
..12 => grey(0.25),
|
||||
12..24 => grey(0.65),
|
||||
24..36 => grey(0.75),
|
||||
_ => grey(0.0),
|
||||
});
|
||||
let at = [(4, 16), (16, 16), (28, 16), (40, 16)];
|
||||
let before: Vec<_> = at.iter().map(|&(x, y)| rgba(&s, x, y)).collect();
|
||||
let added = json!({"fuzziness": 20, "lightness": -50, "points": [[4, 16], [28, 16]]});
|
||||
// Added only: the box spans dark to light, so all three greys are replaced.
|
||||
s.execute("image.adjustments.replaceColor", added.clone()).unwrap();
|
||||
for (i, &(x, y)) in at[..3].iter().enumerate() {
|
||||
assert!(changed(rgba(&s, x, y), before[i]) > 0.1, "grey {i} is in the added range");
|
||||
}
|
||||
assert_eq!(rgba(&s, 40, 16), before[3]);
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
// Mid grey subtracted: the light end moves onto it, so the light grey is left alone.
|
||||
let mut subtracted = added.clone();
|
||||
subtracted["subtractPoints"] = json!([[16, 16]]);
|
||||
subtracted["order"] = json!(["+", "+", "-"]);
|
||||
s.execute("image.adjustments.replaceColor", subtracted.clone()).unwrap();
|
||||
assert!(changed(rgba(&s, 4, 16), before[0]) > 0.1, "the dark grey is still replaced");
|
||||
assert!(changed(rgba(&s, 16, 16), before[1]) > 0.1, "the subtracted grey stays matched, at the new edge");
|
||||
assert_eq!(rgba(&s, 28, 16), before[2], "the light grey beyond it is taken out");
|
||||
assert_eq!(rgba(&s, 40, 16), before[3]);
|
||||
// Select › Color Range agrees for the same clicks.
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
let mut range = subtracted;
|
||||
range.as_object_mut().unwrap().remove("lightness");
|
||||
s.execute("select.colorRange", range).unwrap();
|
||||
let sel = doc(&s).selection.clone().unwrap();
|
||||
let covered: Vec<bool> = at.iter().map(|&(x, y)| sel.read_region(Rect::from_xywh(x, y, 1, 1))[0] > 0.5).collect();
|
||||
assert_eq!(covered, [true, true, false, false]);
|
||||
}
|
||||
|
||||
/// CodeRabbit review on #10443: the click order (`"order"`) reaches the matcher, as in Select ›
|
||||
/// Color Range. Added dark grey, subtracted mid grey, added light grey (`+ - +`): the subtraction
|
||||
/// comes while the box is the dark grey alone, far from the mid grey, so it changes nothing and the
|
||||
/// light grey is replaced; all additions first (`+ + -`) would leave it out.
|
||||
#[test]
|
||||
fn replace_color_follows_the_click_order_as_color_range_does() {
|
||||
let grey = |v: f32| [v, v, v, 1.0];
|
||||
// Dark grey (x < 12), mid grey (subtracted), light grey, black (never sampled).
|
||||
let mut s = session(8, "rgb");
|
||||
paint(&mut s, |x, _| match x {
|
||||
..12 => grey(0.25),
|
||||
12..24 => grey(0.65),
|
||||
24..36 => grey(0.75),
|
||||
_ => grey(0.0),
|
||||
});
|
||||
let at = [(4, 16), (16, 16), (28, 16), (40, 16)];
|
||||
let before: Vec<_> = at.iter().map(|&(x, y)| rgba(&s, x, y)).collect();
|
||||
let clicks = json!({"fuzziness": 20, "points": [[4, 16], [28, 16]], "subtractPoints": [[16, 16]], "order": ["+", "-", "+"]});
|
||||
let mut p = clicks.clone();
|
||||
p["lightness"] = json!(-50);
|
||||
s.execute("image.adjustments.replaceColor", p).unwrap();
|
||||
let replaced: Vec<bool> = at.iter().enumerate().map(|(i, &(x, y))| changed(rgba(&s, x, y), before[i]) > 0.1).collect();
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
// Darkening leaves black black whether or not it's matched: lighten to see whether it is.
|
||||
p = clicks.clone();
|
||||
p["lightness"] = json!(50);
|
||||
s.execute("image.adjustments.replaceColor", p).unwrap();
|
||||
let lifted = changed(rgba(&s, 40, 16), before[3]) > 0.1;
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
s.execute("select.colorRange", clicks).unwrap();
|
||||
let sel = doc(&s).selection.clone().unwrap();
|
||||
let covered: Vec<bool> = at.iter().map(|&(x, y)| sel.read_region(Rect::from_xywh(x, y, 1, 1))[0] > 0.5).collect();
|
||||
assert_eq!(covered, [true, true, true, false], "Color Range: the light grey is added back after the subtraction");
|
||||
assert_eq!(replaced, covered, "Replace Color matches what Color Range covers for the same clicks");
|
||||
assert_eq!(lifted, covered[3], "Replace Color matches black as Color Range does");
|
||||
}
|
||||
|
||||
/// FredApps review on #10443: Localized Color Clusters with several samples. A matching colour
|
||||
/// is replaced only near one of the samples, as in Select › Color Range.
|
||||
#[test]
|
||||
fn replace_color_localized_clusters_with_several_samples() {
|
||||
// Two creams, sampled at x = 2 and x = 24; the first cream again far right (x ≥ 40).
|
||||
let (cream, cream2, grey) = ([0.9, 0.88, 0.79, 1.0], [0.88, 0.86, 0.76, 1.0], [0.5, 0.5, 0.5, 1.0]);
|
||||
let mut s = session(8, "rgb");
|
||||
paint(&mut s, |x, _| match x {
|
||||
..8 => cream,
|
||||
20..28 => cream2,
|
||||
40.. => cream,
|
||||
_ => grey,
|
||||
});
|
||||
let at = [(3, 16), (24, 16), (44, 16), (14, 16)];
|
||||
let before: Vec<_> = at.iter().map(|&(x, y)| rgba(&s, x, y)).collect();
|
||||
let mut p = json!({"fuzziness": 20, "hue": 180, "saturation": 60, "points": [[2, 16], [24, 16]]});
|
||||
// Without clusters both creams change everywhere, the far one too.
|
||||
s.execute("image.adjustments.replaceColor", p.clone()).unwrap();
|
||||
assert!(changed(rgba(&s, 44, 16), before[2]) > 0.05, "far cream without clusters");
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
// Range 20 % of 48 px: within about 10 px of either sample.
|
||||
p["localized"] = json!(true);
|
||||
p["range"] = json!(20);
|
||||
s.execute("image.adjustments.replaceColor", p.clone()).unwrap();
|
||||
assert!(changed(rgba(&s, 3, 16), before[0]) > 0.05, "near the first sample");
|
||||
assert!(changed(rgba(&s, 24, 16), before[1]) > 0.05, "near the second sample");
|
||||
assert_eq!(rgba(&s, 44, 16), before[2], "the same cream far from both samples");
|
||||
assert_eq!(rgba(&s, 14, 16), before[3], "grey isn't sampled");
|
||||
// Select › Color Range agrees for the same clicks.
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
for k in ["hue", "saturation"] {
|
||||
p.as_object_mut().unwrap().remove(k);
|
||||
}
|
||||
s.execute("select.colorRange", p).unwrap();
|
||||
let sel = doc(&s).selection.clone().unwrap();
|
||||
let covered: Vec<bool> = at.iter().map(|&(x, y)| sel.read_region(Rect::from_xywh(x, y, 1, 1))[0] > 0.5).collect();
|
||||
assert_eq!(covered, [true, true, false, false]);
|
||||
}
|
||||
|
||||
/// #10443 review: with only Subtract from Sample clicks, the dialog's From colour is what the
|
||||
/// subtraction starts from, not the foreground (black here), so the red is still replaced.
|
||||
#[test]
|
||||
fn replace_color_subtract_only_starts_from_the_from_colour() {
|
||||
let (red, blue) = ([0.85, 0.1, 0.1, 1.0], [0.1, 0.15, 0.85, 1.0]);
|
||||
let mut s = session(8, "rgb");
|
||||
paint(&mut s, |x, _| if x < 16 { red } else { blue });
|
||||
let (r0, b0) = (rgba(&s, 4, 16), rgba(&s, 24, 16));
|
||||
let p = json!({"color": "#d91a1a", "hue": 120, "fuzziness": 40, "points": [], "subtractPoints": [[24, 16]]});
|
||||
s.execute("image.adjustments.replaceColor", p).unwrap();
|
||||
assert_ne!(rgba(&s, 4, 16), r0, "the From colour (red) is replaced");
|
||||
// Blue is outside the From colour's range anyway: subtracting from a single colour doesn't
|
||||
// reshape its range, as in Photoshop (see
|
||||
// `replace_color_subtracting_inside_the_added_range_takes_a_colour_out`).
|
||||
assert_eq!(rgba(&s, 24, 16), b0, "blue isn't matched");
|
||||
}
|
||||
|
||||
/// #10443 review: a `null` From colour means the foreground, for the subtract-only matcher too.
|
||||
#[test]
|
||||
fn replace_color_subtract_only_reads_a_null_colour_as_the_foreground() {
|
||||
let (red, blue) = ([0.85, 0.1, 0.1, 1.0], [0.1, 0.15, 0.85, 1.0]);
|
||||
let mut s = session(8, "rgb");
|
||||
paint(&mut s, |x, _| if x < 16 { red } else { blue });
|
||||
s.tools.foreground = red;
|
||||
let r0 = rgba(&s, 4, 16);
|
||||
let p = json!({"color": null, "hue": 120, "fuzziness": 40, "points": [], "subtractPoints": [[24, 16]]});
|
||||
s.execute("image.adjustments.replaceColor", p).unwrap();
|
||||
assert_ne!(rgba(&s, 4, 16), r0, "the foreground (red) is replaced");
|
||||
}
|
||||
|
||||
/// A malformed `subtractPoints` is refused even with one sample, where the single-colour path
|
||||
/// would otherwise ignore it (CodeRabbit review on #10443).
|
||||
#[test]
|
||||
fn replace_color_refuses_malformed_subtract_points() {
|
||||
let mut s = session(8, "rgb");
|
||||
paint(&mut s, |_, _| [0.85, 0.1, 0.1, 1.0]);
|
||||
for points in [json!([]), json!([[4, 16]])] {
|
||||
let p = json!({"color": "#d91a1a", "hue": 120, "points": points, "subtractPoints": "invalid"});
|
||||
let err = s.execute("image.adjustments.replaceColor", p).unwrap_err();
|
||||
assert!(err.to_string().contains("replaceColor") && err.to_string().contains("subtractPoints"), "{err}");
|
||||
}
|
||||
// An empty list is no subtraction: the single-colour path.
|
||||
s.execute("image.adjustments.replaceColor", json!({"color": "#d91a1a", "hue": 120, "points": [[4, 16]], "subtractPoints": []})).unwrap();
|
||||
}
|
||||
|
||||
/// #6095: ⌘I in Quick Mask inverts the whole mask, not only where it was white, so leaving Quick
|
||||
/// Mask selects everything but the original rectangle, whichever way the colour reads. With
|
||||
/// Color Indicates Selected Areas and nothing selected, it selects everything. A black alpha
|
||||
|
||||
@@ -896,9 +896,15 @@ fn count_delete_group(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
s.edit("Delete Count Group", |d, _| {
|
||||
d.measurement.count_groups.remove(g);
|
||||
let n = d.measurement.count_groups.len();
|
||||
// Removing a group before the active one shifts it down a slot.
|
||||
// Deleting the current group makes the first one current (#11771); removing a group
|
||||
// before the current one shifts it down a slot (#5654).
|
||||
let a = d.measurement.active_count_group;
|
||||
d.measurement.active_count_group = (if g < a { a - 1 } else { a }).min(n.saturating_sub(1));
|
||||
let next = match g.cmp(&a) {
|
||||
std::cmp::Ordering::Equal => 0,
|
||||
std::cmp::Ordering::Less => a - 1,
|
||||
std::cmp::Ordering::Greater => a,
|
||||
};
|
||||
d.measurement.active_count_group = next.min(n.saturating_sub(1));
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(count_json(doc(s)?.as_ref()))
|
||||
|
||||
@@ -19,6 +19,7 @@ use crate::{EngineError, Result, Session};
|
||||
|
||||
/// Artboard size presets (name, width, height) in pixels, Photoshop's common devices and pages.
|
||||
pub const PRESETS: &[(&str, u32, u32)] = &[
|
||||
("iPhone 8/7/6", 750, 1334),
|
||||
("iPhone 14", 390, 844),
|
||||
("iPhone 14 Pro Max", 430, 932),
|
||||
("Android 1080p", 360, 640),
|
||||
@@ -414,6 +415,9 @@ fn artboard_from_layers(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
/// Changing X/Y moves the board with its contents; changing W/H resizes it from the top left.
|
||||
fn set_props(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "layer.artboard.set";
|
||||
if p.get("layers").is_some() {
|
||||
return set_backgrounds(s, p);
|
||||
}
|
||||
let d = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let id = crate::commands::explicit_layer(s, p)?.or_else(|| active_artboard(s)).ok_or_else(|| bad(cmd, "no artboard selected"))?;
|
||||
let old = d.doc.layer(id).and_then(Layer::artboard).cloned().ok_or_else(|| bad(cmd, "the layer is not an artboard"))?;
|
||||
@@ -469,6 +473,34 @@ fn set_props(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({"layer": id.0, "rect": [rect.x0, rect.y0, rect.width(), rect.height()]}))
|
||||
}
|
||||
|
||||
/// `layer.artboard.set` with a `layers` array: the background of every listed board in one undo
|
||||
/// step, "Edit Artboard" for one board and "Edit Artboards" for several (the options bar's
|
||||
/// background control with boards selected). Only `background`/`color` apply to several boards.
|
||||
fn set_backgrounds(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "layer.artboard.set";
|
||||
let d = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let mut ids: Vec<LayerId> = Vec::new();
|
||||
for v in p.get("layers").and_then(Value::as_array).ok_or_else(|| bad(cmd, "`layers` must be an array of layer ids"))? {
|
||||
let id = LayerId(v.as_u64().ok_or_else(|| bad(cmd, "`layers` must be an array of layer ids"))?);
|
||||
d.doc.layer(id).and_then(Layer::artboard).ok_or_else(|| bad(cmd, format!("layer {} is not an artboard", id.0)))?;
|
||||
if !ids.contains(&id) {
|
||||
ids.push(id);
|
||||
}
|
||||
}
|
||||
if ids.is_empty() {
|
||||
return Err(bad(cmd, "`layers` is empty"));
|
||||
}
|
||||
let background = background_param(p, cmd)?.ok_or_else(|| bad(cmd, "`layers` needs a `background`"))?;
|
||||
let label = if ids.len() == 1 { "Edit Artboard" } else { "Edit Artboards" };
|
||||
s.edit(label, |doc, _| {
|
||||
for id in &ids {
|
||||
doc.layer_mut(*id).and_then(Layer::artboard_mut).ok_or(EngineError::NoLayer(*id))?.background = background;
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(json!({"layers": ids.iter().map(|i| i.0).collect::<Vec<_>>()}))
|
||||
}
|
||||
|
||||
/// The Artboard tool's "+" buttons: a board the size of board `layer` on one of its sides, a
|
||||
/// [`GAP`] away (further out past any board in the way), empty or (`duplicate`, the ⌥-click) a
|
||||
/// copy with its contents. A board left of or above the canvas origin shifts the whole document
|
||||
|
||||
@@ -1080,3 +1080,28 @@ fn a_layer_centred_just_left_of_a_board_at_negative_coordinates_stays_off_it() {
|
||||
s.execute("layer.translate", json!({"layer": id.0, "dx": 1, "dy": 0, "nest": true})).unwrap();
|
||||
assert_eq!(doc(&s).artboard_of(id), Some(b));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_layers_array_sets_every_listed_boards_background_in_one_step() {
|
||||
let mut s = session(8);
|
||||
let a = LayerId(s.execute("layer.new.artboard", json!({"rect": [0, 0, 50, 50]})).unwrap()["layer"].as_u64().unwrap());
|
||||
let b = LayerId(s.execute("layer.new.artboard", json!({"rect": [200, 0, 50, 50]})).unwrap()["layer"].as_u64().unwrap());
|
||||
let steps = s.active().unwrap().history.entries().len();
|
||||
s.execute("layer.artboard.set", json!({"layers": [a.0, b.0], "background": "black"})).unwrap();
|
||||
let entries = s.active().unwrap().history.entries();
|
||||
assert_eq!(entries.len(), steps + 1);
|
||||
assert_eq!(entries.last().map(String::as_str), Some("Edit Artboards"));
|
||||
for id in [a, b] {
|
||||
assert_eq!(doc(&s).layer(id).and_then(Layer::artboard).unwrap().background, ArtboardBackground::Black);
|
||||
}
|
||||
s.execute("layer.artboard.set", json!({"layers": [a.0], "background": "custom", "color": "#ff0000"})).unwrap();
|
||||
assert_eq!(s.active().unwrap().history.entries().last().map(String::as_str), Some("Edit Artboard"));
|
||||
// Undo is one step; errors change nothing.
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(doc(&s).layer(a).and_then(Layer::artboard).unwrap().background, ArtboardBackground::Black);
|
||||
let n = s.active().unwrap().history.entries().len();
|
||||
assert!(s.execute("layer.artboard.set", json!({"layers": [a.0, 9999], "background": "white"})).is_err());
|
||||
assert!(s.execute("layer.artboard.set", json!({"layers": [], "background": "white"})).is_err());
|
||||
assert!(s.execute("layer.artboard.set", json!({"layers": [a.0]})).is_err());
|
||||
assert_eq!(s.active().unwrap().history.entries().len(), n);
|
||||
}
|
||||
|
||||
@@ -26,8 +26,9 @@ fn stroke_backwards_compatible() {
|
||||
assert_eq!(r["damage"].as_array().unwrap().len(), 4);
|
||||
assert_eq!(rgba(&s, 30, 20), [0.0, 0.0, 1.0, 1.0]);
|
||||
assert_eq!(rgba(&s, 30, 30)[3], 0.0);
|
||||
// [x, y, pressure] with the default pressure-size brush: light pressure → thinner line.
|
||||
s.execute("paint.stroke", json!({"points": [[4, 5, 0.2], [60, 5, 0.2]], "size": 10})).unwrap();
|
||||
// [x, y, pressure] with Pressure for Size on: light pressure → a thinner line. The default
|
||||
// brush has it off since #11809, so this stroke asks for it.
|
||||
s.execute("paint.stroke", json!({"points": [[4, 5, 0.2], [60, 5, 0.2]], "size": 10, "brush": {"pressureSize": true}})).unwrap();
|
||||
assert!(rgba(&s, 30, 5)[3] > 0.9 && rgba(&s, 30, 8)[3] < 0.01);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
//! #11771: deleting the current count group makes the first remaining group current. Deleting
|
||||
//! another group by index keeps the current one (#5654), and the last group goes without error.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::Session;
|
||||
|
||||
/// Three groups, each with one marker, and the second one current.
|
||||
fn three_groups() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 100, "height": 100})).unwrap();
|
||||
for (i, name) in ["One", "Two", "Three"].iter().enumerate() {
|
||||
s.execute("count.newGroup", json!({"name": name})).unwrap();
|
||||
s.execute("count.add", json!({"x": 10 + 10 * i, "y": 10})).unwrap();
|
||||
}
|
||||
s.execute("count.setGroup", json!({"group": 1})).unwrap();
|
||||
assert_eq!(current(&mut s), (1, "Two".to_string()));
|
||||
s
|
||||
}
|
||||
|
||||
/// The current group's index and name.
|
||||
fn current(s: &mut Session) -> (u64, String) {
|
||||
let c = s.execute("image.analysis.countTool", json!({})).unwrap();
|
||||
let i = c["activeGroup"].as_u64().unwrap();
|
||||
let name = c["groups"].get(i as usize).and_then(|g| g["name"].as_str()).unwrap_or_default().to_string();
|
||||
(i, name)
|
||||
}
|
||||
|
||||
fn groups(s: &mut Session) -> usize {
|
||||
s.execute("image.analysis.countTool", json!({})).unwrap()["groups"].as_array().map_or(0, Vec::len)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_the_current_group_makes_the_first_one_current() {
|
||||
for params in [json!({}), json!({"group": 1})] {
|
||||
let mut s = three_groups();
|
||||
s.execute("count.deleteGroup", params.clone()).unwrap();
|
||||
assert_eq!(groups(&mut s), 2, "{params}");
|
||||
assert_eq!(current(&mut s), (0, "One".to_string()), "{params}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_another_group_keeps_the_current_one() {
|
||||
let mut s = three_groups();
|
||||
s.execute("count.deleteGroup", json!({"group": 2})).unwrap();
|
||||
assert_eq!(current(&mut s), (1, "Two".to_string()));
|
||||
let mut s = three_groups();
|
||||
s.execute("count.deleteGroup", json!({"group": 0})).unwrap();
|
||||
assert_eq!(current(&mut s), (0, "Two".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_the_last_group_leaves_none() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 100, "height": 100})).unwrap();
|
||||
s.execute("count.newGroup", json!({"name": "Only"})).unwrap();
|
||||
s.execute("count.deleteGroup", json!({})).unwrap();
|
||||
let c: Value = s.execute("image.analysis.countTool", json!({})).unwrap();
|
||||
assert_eq!(c["groups"].as_array().map(Vec::len), Some(0));
|
||||
}
|
||||
@@ -63,8 +63,8 @@ use processing::{conditional_mode_change, fit_image, flatten_all_effects, flatte
|
||||
use save::save_a_copy;
|
||||
pub(crate) use save::{SaveOpts, encode, save_doc};
|
||||
pub use save::{
|
||||
dds_param, dpx_bits_param, jpeg_format_param, jpeg_quality, pcraft_full_composite_param, tga_bits_param, tiff_bool_param, tiff_compression_param,
|
||||
tiff_would_write_layers,
|
||||
dds_param, dpx_bits_param, jpeg_format_param, jpeg_quality, maximize_compatibility_param, pcraft_full_composite_param, tga_bits_param, tiff_bool_param,
|
||||
tiff_compression_param, tiff_would_write_layers,
|
||||
};
|
||||
|
||||
mod batch_naming;
|
||||
@@ -272,7 +272,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Save a Copy…",
|
||||
&["File"],
|
||||
Some("Cmd+Alt+S"),
|
||||
r##"{"path":str (format from the extension),"quality":0..12? (JPEG),"jpegFormat":"baseline|optimized|progressive"? (JPEG; default baseline),"layers":bool=true,"overwrite":bool=false (replace the document's own file),"tiffLayers":bool=false (TIFF: keep the layers; flat by default),"tiffCompression":"none|lzw|zip"? (TIFF: image compression; unset, none),"tiffTransparency":bool? (TIFF: Save Transparency; off composites over white; unset, kept unless the document has alpha or spot channels),"tiffPyramid":bool=false (TIFF: Save Image Pyramid, half-size copies after the image),"tgaBits":24|32? (TGA: 32 writes the alpha channel, or else the transparency, as the alpha; unset, 32 with an alpha channel or transparency, else 24),"dpxBits":8|10|12|16? (DPX: bits per sample; unset, 8 for an 8-bit document, else 16),"ddsCompression":"none|bc1|bc2|bc3"="none" (DDS: uncompressed, or block compression; BC1 keeps alpha on or off),"ddsMipmaps":bool=false (DDS: also write the full mip chain),"pcraftFullComposite":bool? (.pcraft: request the flattened document at its canvas size as composite/full.png; it may be omitted; check the manifest's full_composite member; unset, Preferences › File Handling)}"##,
|
||||
r##"{"path":str (format from the extension),"quality":0..12? (JPEG),"jpegFormat":"baseline|optimized|progressive"? (JPEG; default baseline),"layers":bool=true,"overwrite":bool=false (replace the document's own file),"tiffLayers":bool=false (TIFF: keep the layers; flat by default),"tiffCompression":"none|lzw|zip"? (TIFF: image compression; unset, none),"tiffTransparency":bool? (TIFF: Save Transparency; off composites over white; unset, kept unless the document has alpha or spot channels),"tiffPyramid":bool=false (TIFF: Save Image Pyramid, half-size copies after the image),"tgaBits":24|32? (TGA: 32 writes the alpha channel, or else the transparency, as the alpha; unset, 32 with an alpha channel or transparency, else 24),"dpxBits":8|10|12|16? (DPX: bits per sample; unset, 8 for an 8-bit document, else 16),"ddsCompression":"none|bc1|bc2|bc3"="none" (DDS: uncompressed, or block compression; BC1 keeps alpha on or off),"ddsMipmaps":bool=false (DDS: also write the full mip chain),"maximizeCompatibility":bool? (PSD/PSB: write the real merged image; unset, Preferences > File Handling > Maximize PSD and PSB File Compatibility),"pcraftFullComposite":bool? (.pcraft: request the flattened document at its canvas size as composite/full.png; it may be omitted; check the manifest's full_composite member; unset, Preferences › File Handling)}"##,
|
||||
native_doc,
|
||||
save_a_copy
|
||||
),
|
||||
@@ -349,7 +349,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Batch…",
|
||||
&["File", "Automate"],
|
||||
None,
|
||||
r##"{"action":name|index|[steps]? (used only when steps is absent; named actions exclude disabled steps),"set":str? (for a named action),"steps":[[commandId,params]|{"command":id,"params":{}}…] (a recorded action; a Save, Save As or Save a Copy step marks the document written to output, in this command's format and options: as the save found it if it was closed or another document is active at the end, else as the action leaves it; with no such step, the active document at the end),"source":"folder|open"="folder" (open runs on live documents),"input":folder|[paths] (folder source),"errorLog":path? (blank disables it),"suppressOpenOptions":bool=false (use caller RAW defaults and fully supported prior XMP without a dialog; unsupported prior settings or unanswerable profile questions fail the input),"suppressWarnings":bool=false (apply caller opening policy without profile questions; import diagnostics remain in notices),"overrideOpen":bool=false (execute action Open steps with the current input path, retaining other Open options; without an executed Open the input fails),"includeNonImages":bool=false (folder inputs report unsupported files as per-file errors instead of skipping them; arrays are unchanged),"includeSubfolders":bool=false (a folder input also takes the images in its subfolders, up to 32 levels and 100000 files; ordinary outputs are flat; namingComponents can retain subfolders when flatOutput is false),"destination":"folder|saveAndClose"="folder" (Save and Close writes the same source path/format; rejects already-open sources and changed result document identity),"output":folder? (required only for folder; omit for Save and Close),"format":"same|png|jpg|psd|tiff|…"="same" (Save and Close requires same),"quality":0..12?,"jpegFormat":"baseline|optimized|progressive"?,"namingComponents":[{"component":choice}|{"text":str}]? (up to six; the exact filename, with no automatic extension),"compatibility":{"macintosh":bool,"windows":bool,"unix":bool}?,"flatOutput":bool=false (structured naming only; put every result directly under output, dropping its subfolder path; duplicate output names make later results error entries),"overrideSaveAs":bool? (recorded routing; requires a directly represented Save configuration),"stopOnError":bool=false (report the first failed input and stop; earlier outputs remain saved),"naming":string? (blank keeps source names; {name}/{NAME}/{Name}, {serial}/{serial:1..4}, {letter}/{LETTER}, {date} and date formats, {ext}/{EXT}; template determines the whole filename),"startingSerial":1..4294967295=1 (input position, including failed inputs),"tiffLayers":bool? (Folder default false; Save and Close default true, with false refused when TIFF layer data would be lost),"tiffCompression":"none|lzw|zip"?,"tiffTransparency":bool?,"tiffPyramid":bool=false,"tgaBits":24|32?,"dpxBits":8|10|12|16? (DPX: bits per sample; unset, 8 for an 8-bit document, else 16),"ddsCompression":"none|bc1|bc2|bc3"="none" (DDS: uncompressed, or block compression; BC1 keeps alpha on or off),"ddsMipmaps":bool=false (DDS: also write the full mip chain),"calledActions":[{"name","steps"}…]? (internal action references; default: the session's; a named source is selected from the caller)} → {files, errors, stopped?, skipped?, errorLog?, documents?} (structured naming, includeNonImages and Save and Close refuse duplicate results; ordinary native templates keep the later result)"##,
|
||||
r##"{"action":name|index|[steps]? (used only when steps is absent; named actions exclude disabled steps),"set":str? (for a named action),"steps":[[commandId,params]|{"command":id,"params":{}}…] (a recorded action; a Save, Save As or Save a Copy step marks the document written to output, in this command's format and options: as the save found it if it was closed or another document is active at the end, else as the action leaves it; with no such step, the active document at the end),"source":"folder|open"="folder" (open runs on live documents),"input":folder|[paths] (folder source),"errorLog":path? (blank disables it),"suppressOpenOptions":bool=false (use caller RAW defaults and fully supported prior XMP without a dialog; unsupported prior settings or unanswerable profile questions fail the input),"suppressWarnings":bool=false (apply caller opening policy without profile questions; import diagnostics remain in notices),"overrideOpen":bool=false (execute action Open steps with the current input path, retaining other Open options; without an executed Open the input fails),"includeNonImages":bool=false (folder inputs report unsupported files as per-file errors instead of skipping them; arrays are unchanged),"includeSubfolders":bool=false (a folder input also takes the images in its subfolders, up to 32 levels and 100000 files; ordinary outputs are flat; namingComponents can retain subfolders when flatOutput is false),"destination":"folder|saveAndClose"="folder" (Save and Close writes the same source path/format; rejects already-open sources and changed result document identity),"output":folder? (required only for folder; omit for Save and Close),"format":"same|png|jpg|psd|tiff|…"="same" (Save and Close requires same),"quality":0..12?,"jpegFormat":"baseline|optimized|progressive"?,"namingComponents":[{"component":choice}|{"text":str}]? (up to six; the exact filename, with no automatic extension),"compatibility":{"macintosh":bool,"windows":bool,"unix":bool}?,"flatOutput":bool=false (structured naming only; put every result directly under output, dropping its subfolder path; duplicate output names make later results error entries),"overrideSaveAs":bool? (recorded routing; requires a directly represented Save configuration),"stopOnError":bool=false (report the first failed input and stop; earlier outputs remain saved),"naming":string? (blank keeps source names; {name}/{NAME}/{Name}, {serial}/{serial:1..4}, {letter}/{LETTER}, {date} and date formats, {ext}/{EXT}; template determines the whole filename),"startingSerial":1..4294967295=1 (input position, including failed inputs),"tiffLayers":bool? (Folder default false; Save and Close default true, with false refused when TIFF layer data would be lost),"tiffCompression":"none|lzw|zip"?,"tiffTransparency":bool?,"tiffPyramid":bool=false,"tgaBits":24|32?,"dpxBits":8|10|12|16? (DPX: bits per sample; unset, 8 for an 8-bit document, else 16),"ddsCompression":"none|bc1|bc2|bc3"="none" (DDS: uncompressed, or block compression; BC1 keeps alpha on or off),"ddsMipmaps":bool=false (DDS: also write the full mip chain),"maximizeCompatibility":bool? (PSD/PSB: write the real merged image; unset, Preferences > File Handling > Maximize PSD and PSB File Compatibility),"calledActions":[{"name","steps"}…]? (internal action references; default: the session's; a named source is selected from the caller)} → {files, errors, stopped?, skipped?, errorLog?, documents?} (structured naming, includeNonImages and Save and Close refuse duplicate results; ordinary native templates keep the later result)"##,
|
||||
native,
|
||||
batch
|
||||
),
|
||||
@@ -358,7 +358,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Image Processor…",
|
||||
&["File", "Scripts"],
|
||||
None,
|
||||
r##"{"input":folder|[paths],"includeSubfolders":bool=false (a folder input also takes the images in its subfolders, up to 32 levels and 100000 files; each result keeps its subfolder under output),"output":folder,"format":"jpg|png|psd|tiff|…"="jpg","quality":0..12=8,"jpegFormat":"baseline|optimized|progressive"?,"tiffLayers":bool=false,"tiffCompression":"none|lzw|zip"?,"tiffTransparency":bool?,"tiffPyramid":bool=false,"tgaBits":24|32?,"dpxBits":8|10|12|16? (DPX: bits per sample; unset, 8 for an 8-bit document, else 16),"ddsCompression":"none|bc1|bc2|bc3"="none" (DDS: uncompressed, or block compression; BC1 keeps alpha on or off),"ddsMipmaps":bool=false (DDS: also write the full mip chain),"width":px?,"height":px? (fit, never enlarge),"convertToSrgb":bool=false} → {files, errors} (an input whose output name was already written in the run goes to errors)"##,
|
||||
r##"{"input":folder|[paths],"includeSubfolders":bool=false (a folder input also takes the images in its subfolders, up to 32 levels and 100000 files; each result keeps its subfolder under output),"output":folder,"format":"jpg|png|psd|tiff|…"="jpg","quality":0..12=8,"jpegFormat":"baseline|optimized|progressive"?,"tiffLayers":bool=false,"tiffCompression":"none|lzw|zip"?,"tiffTransparency":bool?,"tiffPyramid":bool=false,"tgaBits":24|32?,"dpxBits":8|10|12|16? (DPX: bits per sample; unset, 8 for an 8-bit document, else 16),"ddsCompression":"none|bc1|bc2|bc3"="none" (DDS: uncompressed, or block compression; BC1 keeps alpha on or off),"ddsMipmaps":bool=false (DDS: also write the full mip chain),"maximizeCompatibility":bool? (PSD/PSB: write the real merged image; unset, Preferences > File Handling > Maximize PSD and PSB File Compatibility),"width":px?,"height":px? (fit, never enlarge),"convertToSrgb":bool=false} → {files, errors} (an input whose output name was already written in the run goes to errors)"##,
|
||||
native,
|
||||
image_processor
|
||||
),
|
||||
@@ -386,7 +386,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Layers to Files…",
|
||||
&["File", "Export"],
|
||||
None,
|
||||
r##"{"dir":folder,"format":"png|jpg|psd|tiff|…"="png","prefix":str=document name,"visibleOnly":bool=true,"trim":bool=false,"quality":0..12?,"jpegFormat":"baseline|optimized|progressive"?,"tiffLayers":bool=false,"tiffCompression":"none|lzw|zip"?,"tiffTransparency":bool?,"tiffPyramid":bool=false,"tgaBits":24|32?,"dpxBits":8|10|12|16? (DPX: bits per sample; unset, 8 for an 8-bit document, else 16),"ddsCompression":"none|bc1|bc2|bc3"="none" (DDS: uncompressed, or block compression; BC1 keeps alpha on or off),"ddsMipmaps":bool=false (DDS: also write the full mip chain)} → {files}"##,
|
||||
r##"{"dir":folder,"format":"png|jpg|psd|tiff|…"="png","prefix":str=document name,"visibleOnly":bool=true,"trim":bool=false,"quality":0..12?,"jpegFormat":"baseline|optimized|progressive"?,"tiffLayers":bool=false,"tiffCompression":"none|lzw|zip"?,"tiffTransparency":bool?,"tiffPyramid":bool=false,"tgaBits":24|32?,"dpxBits":8|10|12|16? (DPX: bits per sample; unset, 8 for an 8-bit document, else 16),"ddsCompression":"none|bc1|bc2|bc3"="none" (DDS: uncompressed, or block compression; BC1 keeps alpha on or off),"ddsMipmaps":bool=false (DDS: also write the full mip chain),"maximizeCompatibility":bool? (PSD/PSB: write the real merged image; unset, Preferences > File Handling > Maximize PSD and PSB File Compatibility)} → {files}"##,
|
||||
native_doc,
|
||||
layers_to_files
|
||||
),
|
||||
|
||||
@@ -72,7 +72,8 @@ impl SaveOpts {
|
||||
/// `jpegFormat` it doesn't name or a `tgaBits` other than 24 or 32 is an error); the image
|
||||
/// previews and the PSD merged image from the session's File Handling preferences (Ask When
|
||||
/// Saving has no dialog here, so it saves them, as Photoshop's Save As checkbox and its
|
||||
/// Maximize Compatibility checkbox both start on). The `.pcraft` full-size composite comes from
|
||||
/// Maximize Compatibility checkbox both start on); `maximizeCompatibility` decides the merged
|
||||
/// image for this save instead. The `.pcraft` full-size composite comes from
|
||||
/// `pcraftFullComposite`, or without it from the File Handling preference.
|
||||
pub(crate) fn from_params(s: &Session, p: &Value, cmd: &str) -> Result<Self> {
|
||||
let file_handling = &s.prefs().file_handling;
|
||||
@@ -88,7 +89,7 @@ impl SaveOpts {
|
||||
tiff_pyramid: tiff_bool_param(p, "tiffPyramid", cmd)?.unwrap_or(false),
|
||||
dds: dds_param(p, cmd)?.unwrap_or_default(),
|
||||
image_previews: file_handling.image_previews != crate::prefs::Ask::Never,
|
||||
psd_maximize_compatibility: file_handling.maximize_psd_compatibility != crate::prefs::Ask::Never,
|
||||
psd_maximize_compatibility: maximize_compatibility_param(p, cmd)?.unwrap_or(file_handling.maximize_psd_compatibility != crate::prefs::Ask::Never),
|
||||
pcraft_full_composite: pcraft_full_composite_param(p, cmd)?.unwrap_or(file_handling.pcraft_full_composite),
|
||||
})
|
||||
}
|
||||
@@ -131,6 +132,19 @@ pub fn pcraft_full_composite_param(p: &Value, cmd: &str) -> Result<Option<bool>>
|
||||
}
|
||||
}
|
||||
|
||||
/// One save's Maximize Compatibility in `p` (`"maximizeCompatibility": bool`, Photoshop's checkbox
|
||||
/// when saving a PSD/PSB; absent or null: the File Handling preference decides). Anything else is
|
||||
/// a bad parameter of command `cmd` (#8064).
|
||||
pub fn maximize_compatibility_param(p: &Value, cmd: &str) -> Result<Option<bool>> {
|
||||
match p.get("maximizeCompatibility") {
|
||||
None | Some(Value::Null) => Ok(None),
|
||||
Some(v) => v
|
||||
.as_bool()
|
||||
.map(Some)
|
||||
.ok_or_else(|| EngineError::BadParams { cmd: cmd.into(), msg: format!("`maximizeCompatibility` must be true or false, got {v}") }),
|
||||
}
|
||||
}
|
||||
|
||||
/// The TIFF compression in `p` (`"tiffCompression": "none" | "lzw" | "zip"`, Photoshop's TIFF
|
||||
/// Options; absent or null: none). Anything else is a bad parameter of command `cmd` (#5301).
|
||||
pub fn tiff_compression_param(p: &Value, cmd: &str) -> Result<Option<photocraft_codecs::TiffCompression>> {
|
||||
|
||||
@@ -1875,6 +1875,41 @@ fn full_composite_preference_controls_the_pcraft_full_size_composite() {
|
||||
}
|
||||
}
|
||||
|
||||
/// #8064: `maximizeCompatibility` decides one save's PSD/PSB merged image over the preference
|
||||
/// (Photoshop's Maximize Compatibility checkbox); unset, the preference decides. Anything but a
|
||||
/// boolean is refused before anything is written.
|
||||
#[test]
|
||||
fn maximize_compatibility_param_overrides_the_preference_for_one_save() {
|
||||
use crate::prefs::Ask;
|
||||
let dir = tmp("maximize-param");
|
||||
let mut s = session(300, 200, 16);
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
let real_merged = |path: &str| photocraft_psd::PsdFile::from_bytes(&std::fs::read(path).unwrap()).unwrap().has_real_merged_data();
|
||||
for (ask, param, expect) in [
|
||||
(Ask::Always, json!(false), false),
|
||||
(Ask::Never, json!(true), true),
|
||||
(Ask::Ask, json!(false), false),
|
||||
(Ask::Never, json!(null), false),
|
||||
(Ask::Always, json!(null), true),
|
||||
] {
|
||||
s.edit_prefs(|p| p.file_handling.maximize_psd_compatibility = ask);
|
||||
for ext in ["psd", "psb"] {
|
||||
let out = join(&dir, &format!("{ask:?}-{param}.{ext}"));
|
||||
s.execute("file.saveACopy", json!({"path": out, "maximizeCompatibility": param})).unwrap();
|
||||
assert_eq!(real_merged(&out), Some(expect), "{ask:?} {param} {ext}");
|
||||
}
|
||||
}
|
||||
let state = |s: &Session| s.active().map(|st| (st.revision, st.path.clone(), st.is_dirty()));
|
||||
let before = state(&s);
|
||||
for bad in [json!("no"), json!(0), json!(1), json!([true])] {
|
||||
let out = join(&dir, "bad.psd");
|
||||
let r = s.execute("file.saveACopy", json!({"path": out, "maximizeCompatibility": bad}));
|
||||
assert!(matches!(r, Err(EngineError::BadParams { .. })), "{bad}: {r:?}");
|
||||
assert!(!std::path::Path::new(&out).exists(), "{bad}: nothing written");
|
||||
assert_eq!(state(&s), before, "{bad}: the document is as it was");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_as_forces_the_decoder() {
|
||||
let dir = tmp("openas");
|
||||
|
||||
@@ -49,6 +49,7 @@ pub(crate) fn top_level(doc: &Document, ids: &[LayerId]) -> Vec<LayerId> {
|
||||
|
||||
/// Replace the layer selection without a history step (like [`Session::select_layer`]).
|
||||
pub fn set_selection(s: &mut Session, ids: Vec<LayerId>, active: Option<LayerId>, anchor: Option<LayerId>) -> Result<()> {
|
||||
let before = s.active_layer_stamp();
|
||||
let st = s.active_mut().ok_or(EngineError::NoDocument)?;
|
||||
if let Some(bad) = ids.iter().chain(active.iter()).find(|id| st.doc.layer(**id).is_none()) {
|
||||
return Err(EngineError::NoLayer(*bad));
|
||||
@@ -70,6 +71,7 @@ pub fn set_selection(s: &mut Session, ids: Vec<LayerId>, active: Option<LayerId>
|
||||
st.saved_revision = st.revision;
|
||||
}
|
||||
st.last_damage = Some(Rect::EMPTY);
|
||||
s.note_layer_change(before);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ use std::sync::Arc;
|
||||
|
||||
use photocraft_algo::magnetic::{self, Settings, Tracer};
|
||||
use photocraft_algo::selection as sel;
|
||||
use photocraft_doc::{DocId, Document, LayerId};
|
||||
use photocraft_doc::{ColorMode, DocId, Document, LayerId};
|
||||
use photocraft_geom::Rect;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -23,9 +23,12 @@ const MAX_POINTS: usize = 100_000;
|
||||
/// Longest border one command traces (px, measured along the fastening points).
|
||||
const MAX_LENGTH: f64 = 200_000.0;
|
||||
|
||||
/// The pixels the Magnetic Lasso follows: the active layer's when it is a pixel layer (Photoshop's
|
||||
/// tool sees only the current layer), else the composite. A snapshot of one document revision,
|
||||
/// read lazily and cached while a border is drawn.
|
||||
/// The pixels the Magnetic Lasso follows: the visible composite of an RGB document, whichever layer
|
||||
/// is active (hidden layers add no edges). Documents in other modes (CMYK, Grayscale, Indexed, ...)
|
||||
/// keep the older rule: the active layer's own pixels when it is a pixel layer, else the composite.
|
||||
/// A snapshot of one document revision, read lazily (only the tiles near the border) and cached by
|
||||
/// the tracer while a border is drawn; [`EdgeSource::is_current`] drops it when the document changes,
|
||||
/// so the composite is rendered once per revision and tile, not once per pointer event.
|
||||
pub struct EdgeSource {
|
||||
doc: Arc<Document>,
|
||||
layer: Option<LayerId>,
|
||||
@@ -33,8 +36,12 @@ pub struct EdgeSource {
|
||||
tracer: Tracer,
|
||||
}
|
||||
|
||||
/// The active layer when it has pixels of its own.
|
||||
/// The one layer whose pixels the border follows, or `None` for the composite: always the composite
|
||||
/// in an RGB document; in other modes the active layer when it has pixels of its own.
|
||||
fn pixel_layer(d: &DocState) -> Option<LayerId> {
|
||||
if d.doc.mode == ColorMode::Rgb {
|
||||
return None;
|
||||
}
|
||||
d.active_layer.filter(|id| d.doc.layer(*id).is_some_and(|l| l.surface().is_some()))
|
||||
}
|
||||
|
||||
@@ -45,7 +52,13 @@ impl EdgeSource {
|
||||
Some(EdgeSource { doc: d.doc.clone(), layer: pixel_layer(d), revision: d.revision, tracer: Tracer::new(d.doc.bounds()) })
|
||||
}
|
||||
|
||||
/// Whether this still shows the session's active document, layer and revision.
|
||||
/// Whether this still shows the session's active document and revision (and, outside RGB
|
||||
/// documents, active layer).
|
||||
///
|
||||
/// When this returns `false` the caller must drop the `EdgeSource` and build a new one with
|
||||
/// [`EdgeSource::new`]: [`EdgeSource::snap`] and [`EdgeSource::trace`] keep reading the old
|
||||
/// snapshot and its cached pixels otherwise. The Magnetic Lasso tool does so in `source()`
|
||||
/// (`crates/ui-egui/src/tools/magnetic_lasso_ui.rs`) before every snap and trace.
|
||||
pub fn is_current(&self, s: &Session) -> bool {
|
||||
s.active().is_some_and(|d| d.doc.id == self.doc.id && d.revision == self.revision && pixel_layer(d) == self.layer)
|
||||
}
|
||||
@@ -70,7 +83,7 @@ impl EdgeSource {
|
||||
}
|
||||
}
|
||||
|
||||
/// Straight RGBA8 pixels of `r` from the layer, or the composite without one.
|
||||
/// Straight RGBA8 pixels of `r` from the layer, or the visible composite without one.
|
||||
fn read(doc: &Document, layer: Option<LayerId>, r: Rect) -> Vec<[u8; 4]> {
|
||||
match layer.and_then(|id| doc.layer(id)).and_then(|l| l.surface()) {
|
||||
Some(surface) => sel::rgba8_image(surface, r),
|
||||
|
||||
@@ -65,16 +65,16 @@ fn modes_and_a_given_outline() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edges_come_from_the_active_pixel_layer() {
|
||||
fn edges_come_from_the_visible_composite() {
|
||||
let mut s = session();
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
// An empty layer has no edges: the point stays where it is.
|
||||
// An empty layer on top adds no pixels: the edge of the Background's square still counts.
|
||||
let settings = Settings::default();
|
||||
let mut src = EdgeSource::new(&s).unwrap();
|
||||
assert_eq!(src.snap([44.0, 50.0], settings), Some([44.0, 50.0]));
|
||||
let p = src.snap([44.0, 50.0], settings).unwrap();
|
||||
assert!((p[0] - 40.0).abs() < 0.01, "{p:?}");
|
||||
let bg = s.active().unwrap().doc.layers[0].id;
|
||||
s.execute("layer.select", json!({"layer": bg.0})).unwrap();
|
||||
assert!(!src.is_current(&s), "another layer");
|
||||
let mut src = EdgeSource::new(&s).unwrap();
|
||||
assert!(src.is_current(&s));
|
||||
let p = src.snap([44.0, 50.0], settings).unwrap();
|
||||
|
||||
@@ -97,6 +97,63 @@ fn set_mask_with(s: &mut Session, p: &Value, label: &str, mask: Option<LayerMask
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
/// Drag a layer mask's thumbnail onto another layer or group in the Layers panel: the mask moves
|
||||
/// there (⌥: a copy goes there and the original stays), as in Photoshop (#10485). A layer that
|
||||
/// already has a mask keeps it unless `replace` (Photoshop asks first). One history step.
|
||||
pub(super) fn move_mask(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let bad = |m: &str| EngineError::BadParams { cmd: "layer.layerMask.moveTo".into(), msg: m.into() };
|
||||
let from = layer_param(s, p)?;
|
||||
let to = mask_destination(s, p)?;
|
||||
let copy = p.get("copy").and_then(Value::as_bool).unwrap_or(false);
|
||||
let replace = p.get("replace").and_then(Value::as_bool).unwrap_or(false);
|
||||
if from == to {
|
||||
return Err(bad("the mask is already on that layer"));
|
||||
}
|
||||
let label = if copy { "Copy Layer Mask" } else { "Move Layer Mask" };
|
||||
s.edit(label, |doc, _| {
|
||||
let mask = doc.layer(from).ok_or(EngineError::NoLayer(from))?.mask.clone().ok_or_else(|| bad("the layer has no layer mask"))?;
|
||||
if doc.layer(to).ok_or(EngineError::NoLayer(to))?.mask.is_some() && !replace {
|
||||
return Err(bad("the layer already has a layer mask (pass `replace`)"));
|
||||
}
|
||||
check_all_unlocked(doc, to)?;
|
||||
if !copy {
|
||||
check_all_unlocked(doc, from)?;
|
||||
}
|
||||
// The Background can't hold a mask, so it becomes a normal layer first, as for Add Mask.
|
||||
crate::extra_cmds::background_to_layer_for_mask(doc, to);
|
||||
doc.layer_mut(to).ok_or(EngineError::NoLayer(to))?.mask = Some(mask);
|
||||
if !copy {
|
||||
doc.layer_mut(from).ok_or(EngineError::NoLayer(from))?.mask = None;
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(json!({"layer": from.0, "to": to.0, "copy": copy}))
|
||||
}
|
||||
|
||||
/// `layer.layerMask.moveTo`'s `to`: an id, or a name (as a recorded step keeps it) with
|
||||
/// `toOccurrence` choosing among the layers that share it, in document order (required when
|
||||
/// several do).
|
||||
fn mask_destination(s: &Session, p: &Value) -> Result<LayerId> {
|
||||
let bad = |m: String| EngineError::BadParams { cmd: "layer.layerMask.moveTo".into(), msg: m };
|
||||
match p.get("to") {
|
||||
Some(Value::String(name)) => {
|
||||
let occurrence = p
|
||||
.get("toOccurrence")
|
||||
.map(|v| v.as_u64().and_then(|n| usize::try_from(n).ok()).ok_or_else(|| bad("`toOccurrence` must be a non-negative integer".into())))
|
||||
.transpose()?;
|
||||
let doc = &s.active().ok_or(EngineError::NoDocument)?.doc;
|
||||
let matches: Vec<_> = doc.walk().into_iter().filter(|(_, _, l)| &l.name == name).map(|(_, _, l)| l.id).collect();
|
||||
if matches.len() > 1 && occurrence.is_none() {
|
||||
return Err(bad(format!("multiple layers named `{name}`; specify `toOccurrence`")));
|
||||
}
|
||||
let index = occurrence.unwrap_or(0);
|
||||
matches.get(index).copied().ok_or_else(|| bad(format!("no layer named `{name}` at occurrence {index}")))
|
||||
}
|
||||
Some(v) => v.as_u64().map(LayerId).ok_or_else(|| bad("`to` must be a layer id or name".into())),
|
||||
None => Err(bad("missing `to`".into())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Move a layer's pixels, linked mask and type by whole pixels (vectors move via
|
||||
/// `vector_cmds::translate_vectors`).
|
||||
pub(crate) fn translate_layer(doc: &Document, l: &mut Layer, dx: i32, dy: i32) {
|
||||
@@ -188,3 +245,110 @@ pub(super) fn merge_group(s: &mut Session, id: LayerId) -> Result<Value> {
|
||||
})?;
|
||||
Ok(json!({ "layer": id.0 }))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
/// #10485: dragging a layer's mask onto a group moves it there, ⌥ copies it (Photoshop). A
|
||||
/// layer that has a mask already keeps it unless asked to replace it; one undo puts it back.
|
||||
#[test]
|
||||
fn a_layer_mask_moves_or_copies_onto_another_layer_or_group() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 32, "height": 32})).unwrap();
|
||||
let a = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap();
|
||||
s.execute("select.rect", json!({"x": 4, "y": 4, "width": 10, "height": 10})).unwrap();
|
||||
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
|
||||
let b = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap();
|
||||
let group = s.execute("layer.groupLayers", json!({"layer": b})).unwrap()["layer"].as_u64().unwrap();
|
||||
let mask_of = |s: &Session, id: u64| s.active().unwrap().doc.layer(photocraft_doc::LayerId(id)).unwrap().mask.clone();
|
||||
let original = mask_of(&s, a).expect("a has a mask");
|
||||
// Move onto the group.
|
||||
s.execute("layer.layerMask.moveTo", json!({"layer": a, "to": group})).unwrap();
|
||||
assert!(mask_of(&s, a).is_none(), "moved away");
|
||||
assert_eq!(mask_of(&s, group).unwrap().surface, original.surface, "the same mask on the group");
|
||||
// One undo puts it back.
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
assert!(mask_of(&s, a).is_some() && mask_of(&s, group).is_none());
|
||||
// ⌥: a copy.
|
||||
s.execute("layer.layerMask.moveTo", json!({"layer": a, "to": group, "copy": true})).unwrap();
|
||||
assert!(mask_of(&s, a).is_some() && mask_of(&s, group).is_some());
|
||||
assert_eq!(mask_of(&s, group).unwrap().surface, original.surface, "the copy on the group");
|
||||
// The group gets a mask of its own: refused unless replacing, which leaves a's on it.
|
||||
s.execute("layer.layerMask.delete", json!({"layer": group})).unwrap();
|
||||
s.execute("layer.layerMask.hideAll", json!({"layer": group})).unwrap();
|
||||
assert_ne!(mask_of(&s, group).unwrap().surface, original.surface, "the group's own mask differs");
|
||||
assert!(s.execute("layer.layerMask.moveTo", json!({"layer": a, "to": group})).is_err());
|
||||
s.execute("layer.layerMask.moveTo", json!({"layer": a, "to": group, "replace": true})).unwrap();
|
||||
assert!(mask_of(&s, a).is_none());
|
||||
assert_eq!(mask_of(&s, group).unwrap().surface, original.surface, "a's mask replaced the group's");
|
||||
// Bad input.
|
||||
assert!(s.execute("layer.layerMask.moveTo", json!({"layer": a, "to": group})).is_err(), "no mask to move");
|
||||
assert!(s.execute("layer.layerMask.moveTo", json!({"layer": group, "to": group})).is_err(), "onto itself");
|
||||
assert!(s.execute("layer.layerMask.moveTo", json!({"layer": group})).is_err(), "no target");
|
||||
}
|
||||
|
||||
/// #10485: dropping a mask back on its own layer's row is a no-op: the mask keeps its pixels,
|
||||
/// enabled state and density, nothing else in the document changes, and no history step is added.
|
||||
#[test]
|
||||
fn dropping_a_layer_mask_on_its_own_layer_changes_nothing() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 32, "height": 32})).unwrap();
|
||||
let a = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap();
|
||||
s.execute("select.rect", json!({"x": 4, "y": 4, "width": 10, "height": 10})).unwrap();
|
||||
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
|
||||
s.execute("layer.layerMask.edit", json!({"layer": a, "density": 40})).unwrap();
|
||||
s.execute("layer.layerMask.enabled", json!({"layer": a, "enabled": false})).unwrap();
|
||||
let doc = s.active().unwrap().doc.clone();
|
||||
let mask = doc.layer(photocraft_doc::LayerId(a)).unwrap().mask.clone().expect("a has a mask");
|
||||
assert!(!mask.enabled && (mask.density - 0.4).abs() < 1e-6, "set up: disabled, 40% density");
|
||||
let history = s.active().unwrap().history.entries();
|
||||
for (copy, replace) in [(false, false), (true, false), (false, true), (true, true)] {
|
||||
let r = s.execute("layer.layerMask.moveTo", json!({"layer": a, "to": a, "copy": copy, "replace": replace}));
|
||||
assert!(r.is_err(), "copy: {copy}, replace: {replace}: refused");
|
||||
let st = s.active().unwrap();
|
||||
assert_eq!(st.doc.layer(photocraft_doc::LayerId(a)).unwrap().mask.as_ref(), Some(&mask), "copy: {copy}, replace: {replace}: the mask is untouched");
|
||||
assert!(st.doc == doc, "copy: {copy}, replace: {replace}: the document is unchanged");
|
||||
assert_eq!(st.history.entries(), history, "copy: {copy}, replace: {replace}: no history step");
|
||||
}
|
||||
}
|
||||
|
||||
/// #10485: one undo after an ⌥-copy, or after a move or copy that replaces the target's own
|
||||
/// mask, puts both layers' masks back exactly (pixels, enabled, density, feather).
|
||||
#[test]
|
||||
fn undo_restores_both_masks_after_a_copy_or_a_replace() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 32, "height": 32})).unwrap();
|
||||
let a = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap();
|
||||
s.execute("select.rect", json!({"x": 4, "y": 4, "width": 10, "height": 10})).unwrap();
|
||||
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
|
||||
s.execute("layer.layerMask.edit", json!({"layer": a, "density": 60, "feather": 3})).unwrap();
|
||||
let b = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap();
|
||||
s.execute("select.rect", json!({"x": 18, "y": 2, "width": 6, "height": 20})).unwrap();
|
||||
s.execute("layer.layerMask.hideSelection", json!({"layer": b})).unwrap();
|
||||
s.execute("layer.layerMask.enabled", json!({"layer": b, "enabled": false})).unwrap();
|
||||
let c = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap();
|
||||
let mask_of = |s: &Session, id: u64| s.active().unwrap().doc.layer(photocraft_doc::LayerId(id)).unwrap().mask.clone();
|
||||
let (mask_a, mask_b) = (mask_of(&s, a).expect("a has a mask"), mask_of(&s, b).expect("b has a mask"));
|
||||
assert_ne!(mask_a, mask_b, "set up: two different masks");
|
||||
let doc = s.active().unwrap().doc.clone();
|
||||
let history = s.active().unwrap().history.entries().len();
|
||||
let cases = [
|
||||
("⌥-copy onto a layer without a mask", json!({"layer": a, "to": c, "copy": true}), c),
|
||||
("⌥-copy replacing a mask", json!({"layer": a, "to": b, "copy": true, "replace": true}), b),
|
||||
("move replacing a mask", json!({"layer": a, "to": b, "replace": true}), b),
|
||||
];
|
||||
for (case, params, to) in cases {
|
||||
s.execute("layer.layerMask.moveTo", params).unwrap();
|
||||
assert_eq!(mask_of(&s, to).as_ref(), Some(&mask_a), "{case}: the target has a's mask");
|
||||
assert_eq!(s.active().unwrap().history.entries().len(), history + 1, "{case}: one history step");
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(mask_of(&s, a).as_ref(), Some(&mask_a), "{case}: a's mask is back");
|
||||
assert_eq!(mask_of(&s, b).as_ref(), Some(&mask_b), "{case}: b's mask is back");
|
||||
assert!(mask_of(&s, c).is_none(), "{case}: c has no mask again");
|
||||
assert!(s.active().unwrap().doc == doc, "{case}: the document is as before");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -476,8 +476,9 @@ fn system_printers(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
// ---------- PDF ----------
|
||||
|
||||
/// Paper sizes in points (portrait).
|
||||
pub const PAPERS: [(&str, f64, f64); 8] = [
|
||||
/// Paper sizes in points (portrait). The original list keeps its order and sizes; the Japanese
|
||||
/// photo, postcard and JIS sizes follow it (millimetres × 72/25.4).
|
||||
pub const PAPERS: &[(&str, f64, f64)] = &[
|
||||
("letter", 612.0, 792.0),
|
||||
("legal", 612.0, 1008.0),
|
||||
("tabloid", 792.0, 1224.0),
|
||||
@@ -486,8 +487,76 @@ pub const PAPERS: [(&str, f64, f64); 8] = [
|
||||
("a5", 419.53, 595.28),
|
||||
("4x6", 288.0, 432.0),
|
||||
("5x7", 360.0, 504.0),
|
||||
// Japanese postcards (はがき and 往復はがき), the JIS B series that Japanese printing and
|
||||
// Excel use (B5 182×257, B4 257×364 mm), A6 and the photo sizes L判, 2L判, KG判, 六切, 四切.
|
||||
("postcard", 283.46, 419.53),
|
||||
("double-postcard", 419.53, 566.93),
|
||||
("b5", 515.91, 728.50),
|
||||
("b4", 728.50, 1031.81),
|
||||
("a6", 297.64, 419.53),
|
||||
("l", 252.28, 360.00),
|
||||
("2l", 360.00, 504.57),
|
||||
("kg", 289.13, 430.87),
|
||||
("6cut", 575.43, 720.00),
|
||||
("4cut", 720.00, 864.57),
|
||||
];
|
||||
|
||||
/// Japanese customary names and other spellings for the papers in [`PAPERS`], matched after
|
||||
/// [`normalize_paper`]: `はがき` is `postcard`, `L判` is `l`, `JIS B5` is `b5`, and so on.
|
||||
const PAPER_ALIASES: &[(&str, &str)] = &[
|
||||
("はがき", "postcard"),
|
||||
("ハガキ", "postcard"),
|
||||
("葉書", "postcard"),
|
||||
("ポストカード", "postcard"),
|
||||
("往復はがき", "double-postcard"),
|
||||
("往復ハガキ", "double-postcard"),
|
||||
("JIS B5", "b5"),
|
||||
("B5 (JIS)", "b5"),
|
||||
("JIS B4", "b4"),
|
||||
("B4 (JIS)", "b4"),
|
||||
("L判", "l"),
|
||||
("Lサイズ", "l"),
|
||||
("L-size", "l"),
|
||||
("2L判", "2l"),
|
||||
("2Lサイズ", "2l"),
|
||||
("KG判", "kg"),
|
||||
("KGサイズ", "kg"),
|
||||
("六切", "6cut"),
|
||||
("6切", "6cut"),
|
||||
("四切", "4cut"),
|
||||
("4切", "4cut"),
|
||||
];
|
||||
|
||||
/// The form [`paper_size`] matches names in: lower case, with whitespace and punctuation ignored
|
||||
/// and `×`/`*` read as `x`, so `L判`, `l-size` and `l` are one name, `4 × 6` is `4x6`, and
|
||||
/// `B5(JIS)` and `JIS B5.` are `b5`.
|
||||
fn normalize_paper(name: &str) -> String {
|
||||
name.chars()
|
||||
.map(|c| match c {
|
||||
'×' | '*' => 'x',
|
||||
c => c,
|
||||
})
|
||||
.filter(|c| !c.is_whitespace() && !c.is_ascii_punctuation() && !matches!(c, '(' | ')' | '、' | '。' | ',' | '.'))
|
||||
.collect::<String>()
|
||||
.to_lowercase()
|
||||
}
|
||||
|
||||
/// The portrait size in points of a named paper, `None` for unknown names. Case, spaces and
|
||||
/// punctuation don't matter, and the Japanese customary names (`はがき`, `L判`, `六切`, …) and the
|
||||
/// JIS spelling (`JIS B5`) are aliases of the papers above.
|
||||
pub fn paper_size(name: &str) -> Option<(f64, f64)> {
|
||||
let n = normalize_paper(name);
|
||||
if n.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let canonical = PAPERS
|
||||
.iter()
|
||||
.find(|p| normalize_paper(p.0) == n)
|
||||
.map(|p| p.0)
|
||||
.or_else(|| PAPER_ALIASES.iter().find(|(alias, _)| normalize_paper(alias) == n).map(|(_, name)| *name))?;
|
||||
PAPERS.iter().find(|p| p.0 == canonical).map(|p| (p.1, p.2))
|
||||
}
|
||||
|
||||
/// The image placed on a print page.
|
||||
pub struct PrintImage {
|
||||
pub width: u32,
|
||||
@@ -770,11 +839,9 @@ fn print_image(doc: &Document, p: &Value, cmd: &str) -> Result<(PrintImage, Valu
|
||||
fn paper(p: &Value, cmd: &str) -> Result<(f64, f64)> {
|
||||
let (w, h) = match p.get("paper") {
|
||||
None => (612.0, 792.0),
|
||||
Some(Value::String(name)) => PAPERS
|
||||
.iter()
|
||||
.find(|(n, _, _)| n.eq_ignore_ascii_case(name))
|
||||
.map(|(_, w, h)| (*w, *h))
|
||||
.ok_or_else(|| bad(cmd, format!("unknown paper `{name}` ({})", PAPERS.map(|p| p.0).join("|"))))?,
|
||||
Some(Value::String(name)) => {
|
||||
paper_size(name).ok_or_else(|| bad(cmd, format!("unknown paper `{name}` ({})", PAPERS.iter().map(|p| p.0).collect::<Vec<_>>().join("|"))))?
|
||||
}
|
||||
Some(Value::Array(a)) if a.len() == 2 => (a[0].as_f64().unwrap_or(0.0), a[1].as_f64().unwrap_or(0.0)),
|
||||
_ => return Err(bad(cmd, "paper is a name or [width, height] in points")),
|
||||
};
|
||||
|
||||
@@ -144,6 +144,80 @@ fn print_dry_run_renders_a_pdf_and_reports_the_platform_backend() {
|
||||
assert!(s.execute("file.print", json!({"colorHandling": "photocraftManages", "dryRun": true})).is_err(), "needs a printer profile");
|
||||
}
|
||||
|
||||
/// The Japanese papers are in the table at their metric sizes, their customary names resolve (case,
|
||||
/// spaces and punctuation ignored), and the sizes that were there before are unchanged.
|
||||
#[test]
|
||||
fn japanese_paper_sizes_and_aliases() {
|
||||
// mm -> pt, within 0.1 pt.
|
||||
for (name, w_mm, h_mm) in [
|
||||
("postcard", 100.0, 148.0),
|
||||
("double-postcard", 148.0, 200.0),
|
||||
("b5", 182.0, 257.0),
|
||||
("b4", 257.0, 364.0),
|
||||
("a6", 105.0, 148.0),
|
||||
("l", 89.0, 127.0),
|
||||
("2l", 127.0, 178.0),
|
||||
("kg", 102.0, 152.0),
|
||||
("6cut", 203.0, 254.0),
|
||||
("4cut", 254.0, 305.0),
|
||||
] {
|
||||
let (w, h) = paper_size(name).unwrap_or_else(|| panic!("{name}"));
|
||||
let want = (w_mm * 72.0 / 25.4, h_mm * 72.0 / 25.4);
|
||||
assert!((w - want.0).abs() < 0.1 && (h - want.1).abs() < 0.1, "{name}: {w} x {h}, wanted {want:?}");
|
||||
}
|
||||
// The customary and English names resolve to the same paper.
|
||||
for (a, b) in [
|
||||
("はがき", "postcard"),
|
||||
("ハガキ", "postcard"),
|
||||
("葉書", "postcard"),
|
||||
("往復はがき", "double-postcard"),
|
||||
("JIS B5", "b5"),
|
||||
("B5 (JIS)", "b5"),
|
||||
("JIS B4", "b4"),
|
||||
("L判", "l"),
|
||||
("Lサイズ", "l"),
|
||||
("l-size", "l"),
|
||||
("2L判", "2l"),
|
||||
("KG判", "kg"),
|
||||
("六切", "6cut"),
|
||||
("6切", "6cut"),
|
||||
("四切", "4cut"),
|
||||
("4切", "4cut"),
|
||||
("A4", "a 4"),
|
||||
("4 × 6", "4x6"),
|
||||
("B5(JIS)", "b5"),
|
||||
("JIS B5.", "b5"),
|
||||
("JIS B5。", "b5"),
|
||||
] {
|
||||
assert_eq!(paper_size(a), paper_size(b), "{a} vs {b}");
|
||||
}
|
||||
assert!(paper_size("napkin").is_none());
|
||||
// The papers that were already there keep their sizes.
|
||||
assert_eq!(paper_size("letter"), Some((612.0, 792.0)));
|
||||
assert_eq!(paper_size("a4"), Some((595.28, 841.89)));
|
||||
assert_eq!(paper_size("4x6"), Some((288.0, 432.0)));
|
||||
assert_eq!(paper_size("5x7"), Some((360.0, 504.0)));
|
||||
}
|
||||
|
||||
/// A print on はがき or L判 writes a PDF page of that paper's size: 100 × 148 mm is
|
||||
/// 283.46 × 419.53 pt and 89 × 127 mm is 252.28 × 360.00 pt. The customary names and the JIS
|
||||
/// spelling work too, and an unknown paper is still refused.
|
||||
#[test]
|
||||
fn japanese_paper_sizes_reach_the_pdf_page() {
|
||||
let dir = tmp("jp-paper");
|
||||
for (paper, w, h) in [("postcard", 283.46, 419.53), ("l", 252.28, 360.00), ("はがき", 283.46, 419.53), ("JIS B5", 515.91, 728.50)] {
|
||||
let mut s = session("rgb", 8);
|
||||
let out = format!("{dir}/{}.pdf", paper.replace(' ', "_"));
|
||||
let r = s.execute("file.print", json!({"output": out, "paper": paper, "scaleToFit": true})).unwrap();
|
||||
assert_eq!(r["pdf"], out);
|
||||
let pdf = std::fs::read(&out).unwrap();
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
let want = format!("/MediaBox [0 0 {w:.2} {h:.2}]");
|
||||
assert!(text.contains(&want), "{paper}: wanted {want}");
|
||||
}
|
||||
assert!(session("rgb", 8).execute("file.print", json!({"paper": "napkin", "dryRun": true})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn photocraft_manages_colors_converts_to_the_printer_profile() {
|
||||
let mut s = session("rgb", 8);
|
||||
|
||||
@@ -193,7 +193,7 @@ pub(crate) use filters::{add_smart_filter, add_smart_filter_in, filter_mask_pain
|
||||
use filters::{blending_options, delete_filter, edit_filters, move_filter, set_filter_params, set_filter_visible, smart, transfer_filters};
|
||||
pub(crate) use placing::detach_psd;
|
||||
pub(crate) use placing::{affine_homography, set_placement, shift_smart, transform_placement};
|
||||
use placing::{convert, shift_layer, via_copy};
|
||||
use placing::{convert, reset_transform, shift_layer, via_copy};
|
||||
pub use placing::{layer_to_smart, layers_to_smart, placement};
|
||||
#[cfg(test)]
|
||||
pub(crate) use rendering::DISPLAY_NATIVE_HASH_BYTES;
|
||||
@@ -239,6 +239,14 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
has_smart,
|
||||
edit_contents
|
||||
),
|
||||
spec!(
|
||||
"layer.smartObjects.resetTransform",
|
||||
"Reset Transform",
|
||||
SO,
|
||||
r##"{"layer":id?} (the placement returns to 100 % of the current contents' own size with rotation, skew, Distort, Perspective and the Warp cleared, centred on the average of the placement's four corners; the smart filters stay; refused with nothing changed when the object can't be re-rendered (a visible filter PhotoCraft doesn't have, or contents that can't be read); one undo step)"##,
|
||||
has_smart,
|
||||
reset_transform
|
||||
),
|
||||
spec!("layer.smartObjects.saveContents", "Save Contents", &[], "{} (in an Edit Contents document)", is_smart_child, |s, _| {
|
||||
let i = s.active_index().ok_or(EngineError::NoDocument)?;
|
||||
Ok(json!({"updated": commit_child(s, i)?}))
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
use super::rendering::{refresh_or_fail, unapplied_filters};
|
||||
use super::source::{SourceImage, buffer_to_surface, cache_key, cache_put, source_bytes};
|
||||
use super::{
|
||||
Affine, Arc, BlendMode, Document, EngineError, Homography, Layer, LayerContent, LayerId, PLACED_KEYS, Rect, Result, Session, Size, SmartContentsId,
|
||||
SmartObject, SmartSource, Surface, Value, encode_source, json, layer_param, other, translate_surface,
|
||||
SmartObject, SmartSource, Surface, Value, encode_source, json, layer_param, other, source_box, translate_surface,
|
||||
};
|
||||
use photocraft_algo::transform::Interp;
|
||||
|
||||
/// Rounds float noise off a composed transform (scale 0.1 then 10 should be exactly 1), so
|
||||
/// repeated transforms land back on exact, lossless placements.
|
||||
@@ -57,6 +59,73 @@ pub(crate) fn shift_smart(sm: &mut SmartObject, dx: i32, dy: i32) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Layer ▸ Smart Objects ▸ Reset Transform, and the same item of the Layers panel menu (#11662):
|
||||
/// the placement goes back to 100 % of the current contents' own size with rotation, skew, Distort,
|
||||
/// Perspective **and Warp** cleared, centred where the object sits. Measured in the reference
|
||||
/// application 2026-11-06 (see the #11675 discussion): a smart object warped with Arc 50, rotated
|
||||
/// and scaled came back with no warp at all, at its contents' own size, unrotated, and centred on
|
||||
/// the average of its four corners. The smart filters stay.
|
||||
///
|
||||
/// The step needs the contents re-rendered from their source: a stack whose filters can't all be
|
||||
/// applied, or a source that can't be read, refuses it whole rather than recording the old (still
|
||||
/// warped) pixels under a placement that no longer describes them.
|
||||
pub(super) fn reset_transform(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = layer_param(s, p)?;
|
||||
let st = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let LayerContent::Smart(sm) = &st.doc.layer(id).ok_or(EngineError::NoLayer(id))?.content else {
|
||||
return Err(other("the layer is not a smart object"));
|
||||
};
|
||||
// Checked before anything changes, so a filter PhotoCraft doesn't have (kept verbatim from a
|
||||
// PSD) leaves the document as it was, as `layer.smartObjects.updateModifiedContent` insists on
|
||||
// a re-render too.
|
||||
let stuck = unapplied_filters(sm);
|
||||
if let Some(name) = stuck.first() {
|
||||
return Err(other(format!("the {name} smart filter is not supported, so the smart object's transform can't be reset")));
|
||||
}
|
||||
// The contents' own box in source pixels: 100 % maps it one-to-one onto the document.
|
||||
let [x0, y0, x1, y1] = source_box(&st.doc, sm).ok_or_else(|| other("the smart object's contents are unavailable"))?;
|
||||
let (cx, cy) = ((x0 + x1) / 2.0, (y0 + y1) / 2.0);
|
||||
let old = placement(sm);
|
||||
// The object stays where it is: the average of the placement's four corners, which is its
|
||||
// centre for an affine placement and the quad's centre once Distort or Perspective bent it.
|
||||
let (mut sx, mut sy) = (0.0, 0.0);
|
||||
for (u, v) in [(x0, y0), (x1, y0), (x1, y1), (x0, y1)] {
|
||||
let (px, py) = old.apply(u, v);
|
||||
sx += px;
|
||||
sy += py;
|
||||
}
|
||||
let (dx, dy) = (sx / 4.0 - cx, sy / 4.0 - cy);
|
||||
let to = Affine { m: [1.0, 0.0, 0.0, 1.0, dx, dy] };
|
||||
let new = affine_homography(&to);
|
||||
// Everything the accumulated transform moved the pixels by, so the linked masks and the Smart
|
||||
// Filter mask follow the placement back as they do under Free Transform.
|
||||
let rel = new.mul(&old.inverse().ok_or_else(|| other("the smart object has a degenerate transform"))?);
|
||||
if ![dx, dy, cx, cy].iter().all(|v| v.is_finite()) || !rel.0.iter().all(|v| v.is_finite()) {
|
||||
return Err(other("the smart object's placement is not a usable transform"));
|
||||
}
|
||||
s.edit("Reset Transform", |doc, _| {
|
||||
// The Warp is part of what the reference takes back, so the re-render below starts from the
|
||||
// unwarped contents. How far a painted Smart Filter mask should follow a cleared warp is
|
||||
// unmeasured: here it follows the placement, as it does under Free Transform.
|
||||
if let Some(LayerContent::Smart(sm)) = doc.layer_mut(id).map(|l| &mut l.content) {
|
||||
sm.warp = None;
|
||||
}
|
||||
crate::transform_cmds::transform_by_map(doc, id, &rel, None, Interp::Bicubic)?;
|
||||
// Required, not best-effort: `transform_by_map` re-renders too, but falls back to the old
|
||||
// pixels when that isn't possible. `Session::edit` runs this on a copy, so an `Err` here
|
||||
// records no step and leaves the document exactly as it was. The state is unchanged since
|
||||
// that render, so the guarded refresh answers from the stack cache rather than placing the
|
||||
// contents a second time.
|
||||
refresh_or_fail(doc, id)?;
|
||||
// `new ∘ old⁻¹ ∘ old` only lands on the reset map to within float noise, and could leave a
|
||||
// Distort placement behind as a nearly affine one: store the exact map.
|
||||
if let Some(LayerContent::Smart(sm)) = doc.layer_mut(id).map(|l| &mut l.content) {
|
||||
set_placement(sm, new);
|
||||
}
|
||||
Ok(json!({"layer": id.0, "transform": to.m}))
|
||||
})
|
||||
}
|
||||
|
||||
/// Forget PSD placed-layer data that no longer describes the smart object (its source changed):
|
||||
/// PSD export then writes fresh placed-layer blocks and embeds the new source.
|
||||
pub(crate) fn detach_psd(l: &mut Layer) {
|
||||
|
||||
@@ -503,6 +503,193 @@ fn transforms_re_render_from_source_losslessly() {
|
||||
assert!(active_smart(&smart).perspective.is_some());
|
||||
}
|
||||
|
||||
/// The contents' box centre in document pixels under `sm`'s placement: what an affine placement is
|
||||
/// centred on, and where the box centre of a projective one maps to.
|
||||
fn placed_centre(s: &Session, sm: &SmartObject) -> (f64, f64) {
|
||||
let b = crate::smart_cmds::source_box(&s.active().unwrap().doc, sm).unwrap();
|
||||
placement(sm).apply((b[0] + b[2]) / 2.0, (b[1] + b[3]) / 2.0)
|
||||
}
|
||||
|
||||
/// The average of the four corners of `sm`'s placement in document pixels: the point Reset
|
||||
/// Transform keeps (measured in the reference application 2026-11-06, see the #11675 discussion).
|
||||
/// It is the box centre under an affine placement, and the quad's centre under a projective one.
|
||||
fn quad_centre(s: &Session, sm: &SmartObject) -> (f64, f64) {
|
||||
let b = crate::smart_cmds::source_box(&s.active().unwrap().doc, sm).unwrap();
|
||||
let h = placement(sm);
|
||||
let (mut x, mut y) = (0.0, 0.0);
|
||||
for (u, v) in [(b[0], b[1]), (b[2], b[1]), (b[2], b[3]), (b[0], b[3])] {
|
||||
let (px, py) = h.apply(u, v);
|
||||
x += px;
|
||||
y += py;
|
||||
}
|
||||
(x / 4.0, y / 4.0)
|
||||
}
|
||||
|
||||
/// Layer ▸ Smart Objects ▸ Reset Transform (#11662): 100 % of the contents' own size, no rotation,
|
||||
/// skew, Distort or Perspective, and the object stays centred where it is.
|
||||
#[test]
|
||||
fn reset_transform_restores_100_percent_in_place() {
|
||||
let mut s = session(8);
|
||||
paint(&mut s);
|
||||
let id = convert(&mut s);
|
||||
let fresh = active_smart(&s).cache.unwrap().content_bounds();
|
||||
s.execute("edit.transform", json!({"layer": id, "scaleX": 200, "scaleY": 200, "rotate": 30})).unwrap();
|
||||
s.execute("layer.translate", json!({"dx": 7, "dy": 4})).unwrap();
|
||||
let sm = active_smart(&s);
|
||||
assert!(sm.transform.m[0].hypot(sm.transform.m[1]) > 1.5, "the object really is scaled up");
|
||||
assert_ne!(sm.transform.m[1], 0.0, "…and rotated");
|
||||
let centre = placed_centre(&s, &sm);
|
||||
s.execute("layer.smartObjects.resetTransform", json!({})).unwrap();
|
||||
let after = active_smart(&s);
|
||||
assert!(after.perspective.is_none(), "no projective placement left");
|
||||
assert_eq!(&after.transform.m[..4], &[1.0, 0.0, 0.0, 1.0], "100 %, square, neither rotated nor skewed");
|
||||
let now = placed_centre(&s, &after);
|
||||
assert!((now.0 - centre.0).abs() < 1e-6 && (now.1 - centre.1).abs() < 1e-6, "centred where it was: {now:?} vs {centre:?}");
|
||||
let r = after.cache.unwrap().content_bounds();
|
||||
assert_eq!((r.width(), r.height()), (fresh.width(), fresh.height()), "the contents at their own size");
|
||||
// Re-rendered from the source rather than resampled from the rotated pixels: the reset object
|
||||
// is exactly the converted layer moved by the same whole-pixel distance.
|
||||
let mut control = session(8);
|
||||
paint(&mut control);
|
||||
convert(&mut control);
|
||||
control.execute("layer.translate", json!({"dx": 7, "dy": 4})).unwrap();
|
||||
let (got, want) = (flat(&s), flat(&control));
|
||||
assert!(max_diff(&got, &want) < 1e-6, "lossless reset (mean {})", mean_diff(&got, &want));
|
||||
// Already reset: the second run changes nothing.
|
||||
s.execute("layer.smartObjects.resetTransform", json!({})).unwrap();
|
||||
assert_eq!(active_smart(&s).transform, after.transform, "idempotent");
|
||||
}
|
||||
|
||||
/// Reset Transform takes the Warp back with the transform and keeps the smart filter stack:
|
||||
/// measured in the reference application 2026-11-06 (an Arc 50 warp came back as no warp at all),
|
||||
/// see the #11675 discussion.
|
||||
#[test]
|
||||
fn reset_transform_clears_the_warp_and_keeps_the_smart_filters() {
|
||||
let mut s = session(8);
|
||||
paint(&mut s);
|
||||
let id = convert(&mut s);
|
||||
s.execute("edit.transform.warp", json!({"style": "arc", "bend": 30})).unwrap();
|
||||
s.execute("filter.blur.gaussianBlur", json!({"radius": 2})).unwrap();
|
||||
let before = active_smart(&s);
|
||||
assert!(before.warp.is_some(), "the fixture is warped");
|
||||
let r = before.cache.unwrap().content_bounds();
|
||||
let (x0, y0, x1, y1) = (f64::from(r.x0), f64::from(r.y0), f64::from(r.x1), f64::from(r.y1));
|
||||
s.execute("edit.transform", json!({"layer": id, "rect": [x0, y0, x1, y1], "quad": [[x0, y0], [x1, y0], [x1 - 12.0, y1 - 8.0], [x0, y1]]})).unwrap();
|
||||
assert!(active_smart(&s).perspective.is_some(), "Distort keeps a projective placement");
|
||||
let centre = quad_centre(&s, &active_smart(&s));
|
||||
s.execute("layer.smartObjects.resetTransform", json!({})).unwrap();
|
||||
let after = active_smart(&s);
|
||||
assert!(after.warp.is_none(), "the Warp goes with the transform, as the reference does");
|
||||
assert_eq!(after.smart_filters.len(), 1, "the smart filter stack stays");
|
||||
assert!(after.perspective.is_none(), "Distort is cleared");
|
||||
assert_eq!(&after.transform.m[..4], &[1.0, 0.0, 0.0, 1.0]);
|
||||
let now = placed_centre(&s, &after);
|
||||
assert!((now.0 - centre.0).abs() < 1e-6 && (now.1 - centre.1).abs() < 1e-6, "centred where it was: {now:?} vs {centre:?}");
|
||||
s.execute("layer.smartObjects.resetTransform", json!({})).unwrap();
|
||||
let again = active_smart(&s);
|
||||
assert_eq!(again.transform, after.transform, "idempotent");
|
||||
assert!(again.warp.is_none(), "and still unwarpd");
|
||||
}
|
||||
|
||||
/// The centre Reset Transform keeps is the average of the placement's four corners, not the point
|
||||
/// its box centre maps to: they differ once Distort or Perspective bends the quad, and the
|
||||
/// reference application keeps the corners' average (measured 2026-11-06, #11675 discussion).
|
||||
#[test]
|
||||
fn reset_transform_centres_a_distorted_object_on_its_corner_average() {
|
||||
let mut s = session(8);
|
||||
paint(&mut s);
|
||||
let id = convert(&mut s);
|
||||
let r = active_smart(&s).cache.unwrap().content_bounds();
|
||||
let (x0, y0, x1, y1) = (f64::from(r.x0), f64::from(r.y0), f64::from(r.x1), f64::from(r.y1));
|
||||
let quad = [[x0 - 10.0, y0 - 6.0], [x1 + 26.0, y0 - 2.0], [x1 + 4.0, y1 + 18.0], [x0 - 16.0, y1 + 8.0]];
|
||||
s.execute("edit.transform", json!({"layer": id, "rect": [x0, y0, x1, y1], "quad": quad})).unwrap();
|
||||
let sm = active_smart(&s);
|
||||
assert!(sm.perspective.is_some(), "a projective placement");
|
||||
let corners = quad_centre(&s, &sm);
|
||||
let middle = placed_centre(&s, &sm);
|
||||
assert!((corners.0 - middle.0).abs() + (corners.1 - middle.1).abs() > 1.0, "the fixture splits the two centres: {corners:?} vs {middle:?}");
|
||||
s.execute("layer.smartObjects.resetTransform", json!({})).unwrap();
|
||||
let after = active_smart(&s);
|
||||
assert_eq!(&after.transform.m[..4], &[1.0, 0.0, 0.0, 1.0], "100 %, square");
|
||||
assert!(after.perspective.is_none() && after.warp.is_none(), "no projective placement or warp left");
|
||||
let now = placed_centre(&s, &after);
|
||||
assert!((now.0 - corners.0).abs() < 1e-6 && (now.1 - corners.1).abs() < 1e-6, "centred on the corners: {now:?} vs {corners:?}");
|
||||
assert!((now.0 - middle.0).abs() + (now.1 - middle.1).abs() > 1.0, "not on the point the box centre mapped to");
|
||||
}
|
||||
|
||||
/// A stack holding a filter this build can't apply can't be re-rendered without the warp, so Reset
|
||||
/// Transform refuses the whole step instead of recording the old warped pixels at the new
|
||||
/// placement: no history state, and placement, warp and stack left exactly as they were.
|
||||
#[test]
|
||||
fn reset_transform_refuses_a_stack_it_cannot_re_render() {
|
||||
let mut s = session(8);
|
||||
paint(&mut s);
|
||||
convert(&mut s);
|
||||
s.execute("edit.transform.warp", json!({"style": "arc", "bend": 30})).unwrap();
|
||||
s.execute("edit.transform", json!({"scaleX": 160, "scaleY": 160})).unwrap();
|
||||
// A smart filter whose command this build doesn't have, as an imported PSD leaves it.
|
||||
s.edit("unsupported filter", |doc, active| {
|
||||
let LayerContent::Smart(sm) = &mut doc.layer_mut(active.unwrap()).unwrap().content else { panic!("a smart object") };
|
||||
sm.smart_filters.push(SmartFilter {
|
||||
command: UNSUPPORTED_FILTER.into(),
|
||||
params: json!({"name": "Camera Raw Filter"}),
|
||||
blend: BlendMode::Normal,
|
||||
opacity: 1.0,
|
||||
visible: true,
|
||||
});
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let before = active_smart(&s);
|
||||
assert!(!super::rendering::unapplied_filters(&before).is_empty(), "the fixture's filter can't be applied");
|
||||
let states = s.active().unwrap().history.entries().len();
|
||||
let err = s.execute("layer.smartObjects.resetTransform", json!({})).unwrap_err().to_string();
|
||||
assert!(err.contains("Camera Raw Filter"), "the error names the filter that blocks it: {err}");
|
||||
let after = active_smart(&s);
|
||||
assert_eq!(after.transform, before.transform, "the placement is left alone");
|
||||
assert_eq!(after.warp, before.warp, "the warp is still there");
|
||||
assert_eq!(after.smart_filters, before.smart_filters, "so is the stack");
|
||||
assert_eq!(s.active().unwrap().history.entries().len(), states, "a refused step records no history state");
|
||||
// Only this step is refused: with the stack gone the same object resets normally.
|
||||
s.execute("layer.smartFilter.clearSmartFilters", json!({})).unwrap();
|
||||
s.execute("layer.smartObjects.resetTransform", json!({})).unwrap();
|
||||
let cleared = active_smart(&s);
|
||||
assert_eq!(&cleared.transform.m[..4], &[1.0, 0.0, 0.0, 1.0], "then the reset goes through");
|
||||
assert!(cleared.warp.is_none(), "with the warp");
|
||||
}
|
||||
|
||||
/// Enabled for a smart object only, and one undo step back (#11662).
|
||||
#[test]
|
||||
fn reset_transform_needs_a_smart_object_and_undoes_in_one_step() {
|
||||
let mut empty = Session::new();
|
||||
assert!(!empty.is_enabled("layer.smartObjects.resetTransform"), "no document");
|
||||
assert!(empty.execute("layer.smartObjects.resetTransform", json!({})).is_err());
|
||||
let mut s = session(8);
|
||||
paint(&mut s);
|
||||
let plain = s.active().unwrap().active_layer.unwrap().0;
|
||||
assert!(!s.is_enabled("layer.smartObjects.resetTransform"), "a pixel layer has no placement to reset");
|
||||
assert!(s.execute("layer.smartObjects.resetTransform", json!({"layer": plain})).is_err());
|
||||
let id = convert(&mut s);
|
||||
assert!(s.is_enabled("layer.smartObjects.resetTransform"));
|
||||
s.execute("edit.transform", json!({"layer": id, "matrix": [2, 0, 0, 2, 0, 0]})).unwrap();
|
||||
let scaled = active_smart(&s);
|
||||
s.execute("layer.smartObjects.resetTransform", json!({})).unwrap();
|
||||
assert_eq!(&active_smart(&s).transform.m[..4], &[1.0, 0.0, 0.0, 1.0]);
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
let back = active_smart(&s);
|
||||
assert_eq!(back.transform, scaled.transform, "one history step back to the scaled placement");
|
||||
s.execute("edit.redo", json!({})).unwrap();
|
||||
assert_eq!(&active_smart(&s).transform.m[..4], &[1.0, 0.0, 0.0, 1.0], "…and forward again");
|
||||
// Hostile targets are refused, never acted on.
|
||||
assert!(s.execute("layer.smartObjects.resetTransform", json!({"layer": u64::MAX})).is_err());
|
||||
assert!(s.execute("layer.smartObjects.resetTransform", json!({"layer": "nope"})).is_err());
|
||||
// A locked layer can't be moved.
|
||||
s.execute("layer.lockLayers", json!({"layer": id, "position": true})).unwrap();
|
||||
let locked = active_smart(&s).transform;
|
||||
assert!(s.execute("layer.smartObjects.resetTransform", json!({})).is_err());
|
||||
assert_eq!(active_smart(&s).transform, locked, "the placement is left alone");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filters_follow_transforms() {
|
||||
let mut s = session(16);
|
||||
@@ -712,6 +899,7 @@ fn commands_are_registered_with_catalog_ids() {
|
||||
"layer.smartObjects.newSmartObjectViaCopy",
|
||||
"layer.smartObjects.rasterize",
|
||||
"layer.smartObjects.editContents",
|
||||
"layer.smartObjects.resetTransform",
|
||||
"layer.smartObjects.replaceContents",
|
||||
"layer.smartObjects.exportContents",
|
||||
"layer.smartObjects.updateModifiedContent",
|
||||
|
||||
@@ -634,18 +634,23 @@ pub fn split_selected(surf: &Surface, sel: &Surface) -> Result<(Surface, Surface
|
||||
/// masks and Smart Filter mask follow, and type, shapes and smart objects re-render. The document
|
||||
/// selection is left alone.
|
||||
pub(crate) fn transform_by(doc: &mut Document, id: LayerId, affine: Affine, interp: Interp) -> Result<()> {
|
||||
let h = crate::smart_cmds::affine_homography(&affine);
|
||||
transform_by_map(doc, id, &crate::smart_cmds::affine_homography(&affine), Some(affine), interp)
|
||||
}
|
||||
|
||||
/// [`transform_by`] with the map already built, and `affine` only when it is affine (Distort and
|
||||
/// Perspective results aren't: their smart object keeps the full projective placement).
|
||||
pub(crate) fn transform_by_map(doc: &mut Document, id: LayerId, h: &Homography, affine: Option<Affine>, interp: Interp) -> Result<()> {
|
||||
let group = group_locks(doc, id);
|
||||
let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
|
||||
// Price the smart object caches before they are taken: the fallback warp below isn't budgeted.
|
||||
check_layer_budget(None, l, &h)?;
|
||||
check_layer_budget(None, l, h)?;
|
||||
let taken = crate::smart_cmds::take_caches(std::slice::from_mut(l));
|
||||
apply_transform(None, group, l, &h, Some(affine), interp, &Interrupt::NONE)?;
|
||||
apply_transform(None, group, l, h, affine, interp, &Interrupt::NONE)?;
|
||||
let snapshot = doc.clone();
|
||||
if let Some(l) = doc.layer_mut(id) {
|
||||
refresh_text(&snapshot, l);
|
||||
}
|
||||
crate::smart_cmds::restore_unrendered(doc, taken, |c| warp_surface(c, c.content_bounds(), &h, interp))?;
|
||||
crate::smart_cmds::restore_unrendered(doc, taken, |c| warp_surface(c, c.content_bounds(), h, interp))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -653,6 +653,9 @@ fn stroke_path_simulate_pressure_tapers_both_ends() {
|
||||
let coverage = |simulate: bool, x: i32| -> usize {
|
||||
let mut s = session(200, 60, 8);
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
// Simulate Pressure tapers a brush that follows pen pressure; the default brush stopped
|
||||
// doing that on its own (#11809).
|
||||
s.execute("tools.setBrush", json!({"brush": {"pressureSize": true}})).unwrap();
|
||||
s.execute("path.set", json!({"path": {"subpaths": [{"closed": false, "knots": [[20, 30], [180, 30]]}]}})).unwrap();
|
||||
let p = json!({"tool": "brush", "size": 20, "hardness": 1.0, "color": "#000000", "opacity": 100, "simulatePressure": simulate});
|
||||
s.execute("path.stroke", p).unwrap();
|
||||
|
||||
@@ -14,6 +14,36 @@ use crate::{EngineError, Result, Session};
|
||||
/// Maximum dense RGBA staging pixels in one animated GIF frame. A GIF also keeps the
|
||||
/// quantised frame and palette while rendering, so the document canvas limit is not enough.
|
||||
const MAX_GIF_FRAME_PIXELS: u64 = 64 << 20;
|
||||
/// Largest total an animated GIF render keeps staged before encoding: one index byte per pixel plus
|
||||
/// a 768-byte palette for every frame, the same 2 GiB bound as imported footage ([`MAX_FOOTAGE_BYTES`]).
|
||||
/// The per-frame cap alone lets `frames × pixels` reach terabytes (a 4000 × 4000 canvas over a
|
||||
/// 1,000,000-frame timeline). 1920 × 1080 holds about 2,145 frames and 4096 × 2160 about 242.
|
||||
/// This bounds the staged frames only (indices and palettes). It does not bound the encoder's output
|
||||
/// buffer (LZW data, roughly 1.3-1.5 times the raw size, growing by doubling) or the per-frame
|
||||
/// transients (RGBA f32, 16 bytes per pixel), so it is not a bound on peak memory.
|
||||
const MAX_GIF_STAGED_BYTES: u64 = MAX_FOOTAGE_BYTES;
|
||||
/// The GIF format stores each side as 16 bits.
|
||||
const MAX_GIF_SIDE: u64 = 65_535;
|
||||
|
||||
/// Reject a GIF render whose canvas or frames-times-canvas staging would be unreasonable, before any
|
||||
/// frame is rendered. All sums are checked in `u64`.
|
||||
fn check_gif_budget(w: u64, h: u64, frames: u64) -> std::result::Result<(), String> {
|
||||
if w > MAX_GIF_SIDE || h > MAX_GIF_SIDE {
|
||||
return Err(format!("{w} × {h} is too large to render as an animated GIF (the GIF format allows {MAX_GIF_SIDE} pixels per side)"));
|
||||
}
|
||||
let pixels = w.checked_mul(h).filter(|p| *p <= MAX_GIF_FRAME_PIXELS);
|
||||
let Some(pixels) = pixels else {
|
||||
return Err(format!("{w} × {h} is too large to render as an animated GIF (limit {MAX_GIF_FRAME_PIXELS} pixels per frame)"));
|
||||
};
|
||||
let per_frame = pixels + 768;
|
||||
if frames.checked_mul(per_frame).is_none_or(|total| total > MAX_GIF_STAGED_BYTES) {
|
||||
return Err(format!(
|
||||
"{frames} frames of {w} × {h} are too large to render as an animated GIF (limit {} MiB staged in total); shorten the timeline or reduce the canvas",
|
||||
MAX_GIF_STAGED_BYTES >> 20
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set every video layer's displayed content to its frame at the playhead.
|
||||
pub fn sync(doc: &mut Document) {
|
||||
@@ -638,11 +668,9 @@ pub fn render_video_with(
|
||||
|
||||
// Animated GIF: one file, every frame quantised to its own palette.
|
||||
if format.eq_ignore_ascii_case("gif") {
|
||||
// The whole canvas is staged as f32 RGBA for each frame; reject over-budget
|
||||
// dimensions before an infallible allocation can abort the process.
|
||||
if (w as u64) * (h as u64) > MAX_GIF_FRAME_PIXELS {
|
||||
return Err(EngineError::Other(format!("{w} × {h} is too large to render as an animated GIF (limit {MAX_GIF_FRAME_PIXELS} pixels per frame)")));
|
||||
}
|
||||
// Each frame is staged as f32 RGBA and every encoded frame is kept until the file is written;
|
||||
// reject over-budget sizes before an infallible allocation can abort the process.
|
||||
check_gif_budget(w as u64, h as u64, frames as u64).map_err(EngineError::Other)?;
|
||||
let delay = (100.0 / fps).round().clamp(1.0, 65535.0) as u16;
|
||||
let mut gframes = Vec::with_capacity(frames);
|
||||
for f in 0..frames {
|
||||
|
||||
@@ -916,6 +916,33 @@ fn render_video_rejects_gif_frames_too_large_to_stage() {
|
||||
assert!(err.to_string().contains("too large"), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_video_rejects_a_gif_whose_frames_together_are_too_large() {
|
||||
// Each 4000 x 4000 frame is under the per-frame cap, but 100,000 of them keep ~1.6 TB staged.
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 4000, "height": 4000})).unwrap();
|
||||
s.execute("timeline.create", json!({"duration": 100_000, "fps": 30})).unwrap();
|
||||
let err = s.execute("file.export.renderVideo", json!({"dir": tmpdir("many-gif"), "format": "gif"})).unwrap_err();
|
||||
assert!(err.to_string().contains("staged in total"), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gif_budget_boundaries() {
|
||||
// Sane work is untouched: a minute of 1080p at 30 fps, and the existing small cases.
|
||||
assert!(check_gif_budget(1920, 1080, 1000).is_ok());
|
||||
assert!(check_gif_budget(16, 12, 1).is_ok());
|
||||
// Exactly at the limit passes, one frame more fails.
|
||||
let per_frame = 1000 * 1000 + 768;
|
||||
let n = MAX_GIF_STAGED_BYTES / per_frame;
|
||||
assert!(check_gif_budget(1000, 1000, n).is_ok());
|
||||
assert!(check_gif_budget(1000, 1000, n + 1).is_err());
|
||||
// The per-frame cap and the 16-bit GIF side still apply, and nothing overflows.
|
||||
assert!(check_gif_budget(9000, 8000, 1).is_err());
|
||||
assert!(check_gif_budget(65_536, 1, 1).is_err());
|
||||
assert!(check_gif_budget(65_535, 65_535, u64::MAX).is_err());
|
||||
assert!(check_gif_budget(1, 1, u64::MAX).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fps_from_delays_picks_most_common_and_breaks_ties_by_first_seen() {
|
||||
// Most common delay wins: 100 ms appears twice, 40 ms once -> 10 fps.
|
||||
|
||||
@@ -80,7 +80,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Save for Web (Legacy)…",
|
||||
&["File", "Export"],
|
||||
Some("Cmd+Alt+Shift+S"),
|
||||
r##"{"preset":"GIF 128 Dithered|JPEG High|PNG-24|…"?,"format":"gif|png8|png24|jpeg|wbmp|webp"="png24","palette":"perceptual|selective|adaptive|restrictive|exact|systemMac|systemWindows|uniform"="selective","colors":2..256=256,"colorTable":["#rrggbb",…]? (fixed opaque source palette, 1–256 entries; 255 with transparency),"colorTableEdits":{"0":"#rrggbb",…}? (replace entries after indexing; requires colorTable),"dither":"none|diffusion|pattern|noise"="diffusion","ditherAmount":0..100=88,"transparency":bool=true,"matte":"#rrggbb|none"="none" (a preset's own: white; formats without transparency go over white),"interlaced":bool=false,"webSnap":0..100=0,"quality":0..100=60 (JPEG, lossy WebP),"lossless":bool=false (WebP),"progressive":bool=false,"optimized":bool=true,"embedIcc":bool=false,"metadata":"none|copyright|copyrightAndContact|allExceptCameraInfo|all"="copyright" (EXIF only with all),"convertToSrgb":bool=true,"width"|"height"|"percent"? (image size),"resample":"bicubic|bilinear|nearest"?,"path":file? (whole image),"folder":folder? (whole image, named after the document; ignored with "path" or "dir"),"dir":folder? (one file per slice in images/),"imagesFolder":relative-folder?="images","html":bool=false,"trustedSliceHtml":bool=false (only for trusted documents; emit raw no-image cell HTML),"slices":"all|user"="all","numbers":[n]?} → no path/dir: {bytes,width,height,colors,colorTable,transparentIndex} estimate; else {files, html, bytes, warnings}"##,
|
||||
r##"{"preset":"GIF 128 Dithered|JPEG High|PNG-24|…"?,"format":"gif|png8|png24|jpeg|wbmp|webp"="png24","palette":"perceptual|selective|adaptive|restrictive|exact|systemMac|systemWindows|uniform"="selective","colors":2..256=256,"colorTable":["#rrggbb",…]? (fixed opaque source palette, 1–256 entries; 255 with transparency),"colorTableEdits":{"0":"#rrggbb",…}? (replace entries after indexing; requires colorTable),"dither":"none|diffusion|pattern|noise"="diffusion","ditherAmount":0..100=88,"transparency":bool=true,"matte":"#rrggbb|none"="none" (a preset's own: white; formats without transparency go over white),"interlaced":bool=false,"webSnap":0..100=0,"quality":0..100=60 (JPEG, lossy WebP),"lossless":bool=false (WebP),"progressive":bool=false,"optimized":bool=true,"embedIcc":bool=false,"metadata":"none|copyright|copyrightAndContact|allExceptCameraInfo|all"="copyright" (EXIF only with all),"convertToSrgb":bool=true,"width"|"height"|"percent"? (image size),"resample":"bicubic|bilinear|nearest"?,"path":file? (whole image),"folder":folder? (whole image, named after the document; ignored with "path" or "dir"),"dir":folder? (one file per slice in images/),"imagesFolder":relative-folder?="images","html":bool=false,"trustedSliceHtml":bool=false (only for trusted documents; emit raw no-image cell HTML),"slices":"all|user|selected"="all" (selected requires numbers when the document has slices),"numbers":[n]? (slice numbers, each an existing slice; chooses the slices whatever the mode; with `path` or `folder` alone on a document with slices, refused: use `dir`)} → no path/dir: {bytes,width,height,colors,colorTable,transparentIndex} estimate; else {files, html, bytes, warnings}"##,
|
||||
has_doc,
|
||||
save_for_web
|
||||
),
|
||||
|
||||
@@ -167,6 +167,57 @@ fn remember_web_settings(s: &mut Session, cmd: &str, settings: &Value) {
|
||||
});
|
||||
}
|
||||
|
||||
/// Which slices an export writes.
|
||||
enum SlicePick {
|
||||
All,
|
||||
User,
|
||||
Numbers(Vec<u64>),
|
||||
}
|
||||
|
||||
/// The slices chosen by `slices` (`all|user|selected`) and `numbers`. Photoshop's dialog can
|
||||
/// only select slices it shows, so a script naming one that doesn't exist, an unknown mode, or
|
||||
/// Selected Slices with nothing selected is refused rather than exporting too few or too many
|
||||
/// files. A document without slices is one image whatever the mode, as in the dialog.
|
||||
fn slice_pick(p: &Value, cmd: &str, doc: &photocraft_doc::Document) -> Result<SlicePick> {
|
||||
let mode = match p.get("slices") {
|
||||
None | Some(Value::Null) => "all",
|
||||
Some(Value::String(m)) if matches!(m.as_str(), "all" | "user" | "selected") => m.as_str(),
|
||||
Some(v) => return Err(bad(cmd, format!("unknown slices `{}` (all|user|selected)", v.as_str().map_or_else(|| v.to_string(), str::to_string)))),
|
||||
};
|
||||
let Some(numbers) = p.get("numbers").filter(|v| !v.is_null()) else {
|
||||
return match mode {
|
||||
"selected" if !doc.slices.is_empty() => Err(bad(cmd, "no slices are selected: Selected Slices needs `numbers`")),
|
||||
"user" => Ok(SlicePick::User),
|
||||
_ => Ok(SlicePick::All),
|
||||
};
|
||||
};
|
||||
let numbers: Vec<u64> = numbers
|
||||
.as_array()
|
||||
.filter(|a| !a.is_empty())
|
||||
.and_then(|a| a.iter().map(Value::as_u64).collect())
|
||||
.ok_or_else(|| bad(cmd, "numbers must be a list of slice numbers"))?;
|
||||
let count = slices::resolve(doc).len();
|
||||
let mut missing: Vec<u64> = numbers.iter().copied().filter(|n| !(1..=count as u64).contains(n)).collect();
|
||||
missing.sort_unstable();
|
||||
missing.dedup();
|
||||
if !missing.is_empty() {
|
||||
let list = missing.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
|
||||
let what = if missing.len() == 1 { "slice" } else { "slices" };
|
||||
return Err(bad(cmd, format!("no {what} numbered {list} (the document's slices are 1–{count})")));
|
||||
}
|
||||
// `path` alone is the whole image in one file (with `slices` too, the slices go next to it):
|
||||
// the chosen slices would be ignored, so a document with slices refuses it.
|
||||
let given = |k: &str| p.get(k).and_then(Value::as_str).is_some_and(|v| !v.is_empty());
|
||||
if given("path") && !given("dir") && matches!(p.get("slices"), None | Some(Value::Null)) && count > 1 {
|
||||
return Err(bad(cmd, "`path` writes the whole image as one file: to export the slices in `numbers`, use `dir`"));
|
||||
}
|
||||
// `folder` alone (#5305) is the whole image too, under the document's name.
|
||||
if given("folder") && !given("path") && !given("dir") && count > 1 {
|
||||
return Err(bad(cmd, "`folder` writes the whole image as one file: to export the slices in `numbers`, use `dir`"));
|
||||
}
|
||||
Ok(SlicePick::Numbers(numbers))
|
||||
}
|
||||
|
||||
fn save_for_web_impl(s: &mut Session, p: &Value, write: &mut dyn FnMut(&str, &[u8]) -> Result<()>, native_paths: bool) -> Result<Value> {
|
||||
let cmd = "file.export.saveForWebLegacy";
|
||||
if let Some(authorize) = s.authorize {
|
||||
@@ -174,6 +225,8 @@ fn save_for_web_impl(s: &mut Session, p: &Value, write: &mut dyn FnMut(&str, &[u
|
||||
}
|
||||
// Settings are checked first, so a refused export leaves a floating selection where it is.
|
||||
let st = WebSettings::from_params(p, cmd)?;
|
||||
// So are the slices: committing a floating move doesn't change them.
|
||||
let pick = slice_pick(p, cmd, &s.active().ok_or(EngineError::NoDocument)?.doc)?;
|
||||
// Direct platform calls must export the moved selection as it appears on the canvas,
|
||||
// just as Session::execute does before dispatching the native command.
|
||||
crate::float_cmds::before_command(s, cmd)?;
|
||||
@@ -191,16 +244,12 @@ fn save_for_web_impl(s: &mut Session, p: &Value, write: &mut dyn FnMut(&str, &[u
|
||||
let dpi = wdoc.resolution_dpi;
|
||||
let warnings = web_warnings(&st, exif, dpi);
|
||||
let all = slices::resolve(&doc);
|
||||
let pick = p.get("slices").and_then(Value::as_str).unwrap_or("all");
|
||||
let numbers: Option<Vec<u64>> = p.get("numbers").and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_u64).collect());
|
||||
let chosen: Vec<&ResolvedSlice> = all
|
||||
.iter()
|
||||
.filter(|r| match (&numbers, pick) {
|
||||
(Some(n), _) => n.contains(&(r.number as u64)),
|
||||
(None, "user") => r.origin != photocraft_doc::SliceOrigin::Auto,
|
||||
_ => true,
|
||||
})
|
||||
.collect();
|
||||
let picked = |r: &ResolvedSlice| match &pick {
|
||||
SlicePick::Numbers(n) => n.contains(&(r.number as u64)),
|
||||
SlicePick::User => r.origin != photocraft_doc::SliceOrigin::Auto,
|
||||
SlicePick::All => true,
|
||||
};
|
||||
let chosen: Vec<&ResolvedSlice> = all.iter().filter(|r| picked(r)).collect();
|
||||
let single = p.get("path").and_then(Value::as_str).filter(|v| !v.is_empty());
|
||||
let dir = p.get("dir").and_then(Value::as_str).filter(|v| !v.is_empty());
|
||||
// `folder` (with no `path` or `dir`): the whole image into that folder under the document's
|
||||
@@ -265,13 +314,20 @@ fn save_for_web_impl(s: &mut Session, p: &Value, write: &mut dyn FnMut(&str, &[u
|
||||
};
|
||||
// Slices never overlap here: one covered by later slices comes as its visible pieces (see
|
||||
// `slices::resolve`), each its own image and cell, as Photoshop's subslices. A no-image cell's
|
||||
// text goes into its first piece only.
|
||||
// text goes into its first piece only. In the HTML, a slice that isn't exported stays as an
|
||||
// empty cell, so the exported ones keep their place in the table.
|
||||
let mut texted = std::collections::HashSet::new();
|
||||
for r in &chosen {
|
||||
for r in &all {
|
||||
let piece = scale_rect(r.rect, sx, sy).intersect(&wdoc.bounds());
|
||||
if piece.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if !picked(r) {
|
||||
if html {
|
||||
cells.push((piece, String::new(), String::new()));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let stored = r.id.and_then(|id| doc.slices.get(id));
|
||||
if r.kind == SliceKind::NoImage {
|
||||
let text = stored
|
||||
|
||||
@@ -1417,3 +1417,169 @@ fn folder_saves_under_the_documents_own_name() {
|
||||
assert_eq!(r["files"], json!([format!("{dir}/named.png")]));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_slices_keep_their_place_in_the_html_table() {
|
||||
// CodeRabbit review on PR #10557: exporting only the right of two slices put its image in
|
||||
// the table's first column, on the left. Unexported slices stay as empty cells.
|
||||
let dir = tmp("selected-html");
|
||||
let mut s = session(8);
|
||||
s.execute("slice.new", json!({"rect": [0, 0, 32, 48], "name": "left"})).unwrap();
|
||||
s.execute("slice.new", json!({"rect": [32, 0, 32, 48], "name": "right"})).unwrap();
|
||||
let right = photocraft_doc::slices::resolve(&s.active().unwrap().doc).iter().find(|r| r.name == "right").unwrap().number;
|
||||
let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "dir": dir, "html": true, "numbers": [right]})).unwrap();
|
||||
let files: Vec<&str> = r["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().rsplit('/').next().unwrap()).collect();
|
||||
assert_eq!(files, ["right.png"]);
|
||||
let page = std::fs::read_to_string(r["html"].as_str().unwrap()).unwrap();
|
||||
let row = &page[page.find("<tr>").unwrap()..page.find("</tr>").unwrap()];
|
||||
let cells: Vec<&str> = row.split("<td").skip(1).collect();
|
||||
assert!(cells.len() >= 2, "{row}");
|
||||
assert!(!cells[0].contains("images/right.png"), "the right slice is in column 0:\n{row}");
|
||||
assert!(cells[1].contains("images/right.png"), "column 1:\n{row}");
|
||||
}
|
||||
|
||||
/// Two side-by-side user slices, `left` and `right`, and their numbers.
|
||||
fn two_slices() -> (Session, u64, u64) {
|
||||
let mut s = session(8);
|
||||
s.execute("slice.new", json!({"rect": [0, 0, 32, 48], "name": "left"})).unwrap();
|
||||
s.execute("slice.new", json!({"rect": [32, 0, 32, 48], "name": "right"})).unwrap();
|
||||
let all = photocraft_doc::slices::resolve(&s.active().unwrap().doc);
|
||||
let number = |name: &str| all.iter().find(|r| r.name == name).unwrap().number as u64;
|
||||
(s, number("left"), number("right"))
|
||||
}
|
||||
|
||||
/// A refused slice export writes and remembers nothing, and (called directly, as the platform
|
||||
/// writer is) leaves a floating move in place.
|
||||
fn assert_refused(s: &mut Session, dir: &str, p: Value, expect: &str) {
|
||||
s.execute("select.rect", json!({"x": 8, "y": 8, "width": 4, "height": 4})).unwrap();
|
||||
s.execute("select.float", json!({"dx": 32})).unwrap();
|
||||
let mut written = Vec::new();
|
||||
let err = save_for_web_with_writer(s, &p, &mut |path, _| {
|
||||
written.push(path.to_string());
|
||||
Ok(())
|
||||
})
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
assert!(err.contains(expect), "{p}: {err}");
|
||||
assert_eq!(written, Vec::<String>::new(), "{p}: nothing written");
|
||||
assert!(s.file_menu.last_web.is_none(), "{p}: nothing remembered");
|
||||
assert!(crate::float_cmds::floating(s.active().unwrap()).is_some(), "{p}: the floating move is not committed");
|
||||
let err = s.execute("file.export.saveForWebLegacy", p.clone()).unwrap_err().to_string();
|
||||
assert!(err.contains(expect), "{p}: {err}");
|
||||
assert_eq!(std::fs::read_dir(dir).unwrap().count(), 0, "{p}: nothing written");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice_numbers_that_do_not_exist_are_refused() {
|
||||
// FredApps review on PR #10557: a number naming no slice was dropped, so an export of only
|
||||
// missing numbers "succeeded" with no files and a page of empty cells. Photoshop's dialog
|
||||
// can only select existing slices; a script naming others is refused.
|
||||
let dir = tmp("missing-numbers");
|
||||
let (mut s, left, right) = two_slices();
|
||||
let count = photocraft_doc::slices::resolve(&s.active().unwrap().doc).len() as u64;
|
||||
let (gone, also_gone) = (count + 1, count + 7);
|
||||
assert_refused(&mut s, &dir, json!({"format": "png24", "dir": dir, "html": true, "numbers": [gone]}), &format!("no slice numbered {gone}"));
|
||||
// Partly missing: the dialog builds `numbers` from slices it shows, so every number it sends
|
||||
// exists; one that doesn't is a stale or mistyped script and the export is refused whole.
|
||||
let err = s
|
||||
.execute("file.export.saveForWebLegacy", json!({"format": "png24", "dir": dir, "html": true, "numbers": [left, also_gone, right, gone]}))
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
assert!(err.contains(&format!("no slices numbered {gone}, {also_gone}")), "{err}");
|
||||
assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 0, "nothing written");
|
||||
// Slice numbers start at 1.
|
||||
let err = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "dir": dir, "numbers": [0]})).unwrap_err().to_string();
|
||||
assert!(err.contains("no slice numbered 0"), "{err}");
|
||||
// Existing numbers still export exactly those slices.
|
||||
let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "dir": dir, "numbers": [right, left]})).unwrap();
|
||||
let mut files: Vec<&str> = r["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().rsplit('/').next().unwrap()).collect();
|
||||
files.sort_unstable();
|
||||
assert_eq!(files, ["left.png", "right.png"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice_modes_are_validated() {
|
||||
// FredApps review on PR #10557: any `slices` value but "user" exported every slice,
|
||||
// including the dialog's "selected" without `numbers`.
|
||||
let dir = tmp("slice-modes");
|
||||
let (mut s, left, right) = two_slices();
|
||||
for (p, expect) in [
|
||||
(json!({"slices": "selcted"}), "unknown slices `selcted` (all|user|selected)"),
|
||||
(json!({"slices": 2}), "unknown slices `2` (all|user|selected)"),
|
||||
(json!({"slices": "selected"}), "no slices are selected"),
|
||||
(json!({"slices": "selected", "numbers": []}), "numbers must be a list of slice numbers"),
|
||||
(json!({"numbers": ["1"]}), "numbers must be a list of slice numbers"),
|
||||
(json!({"numbers": 1}), "numbers must be a list of slice numbers"),
|
||||
] {
|
||||
let mut p = p;
|
||||
p["format"] = json!("png24");
|
||||
p["dir"] = json!(dir);
|
||||
assert_refused(&mut s, &dir, p, expect);
|
||||
}
|
||||
// Every valid call still works.
|
||||
let count = photocraft_doc::slices::resolve(&s.active().unwrap().doc).len();
|
||||
let names = |r: &Value| r["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().rsplit('/').next().unwrap().to_string()).collect::<Vec<_>>();
|
||||
for (mode, expect) in [(json!(null), count), (json!("all"), count), (json!("user"), 2)] {
|
||||
let out = tmp("slice-modes-ok");
|
||||
let mut p = json!({"format": "png24", "dir": out});
|
||||
if !mode.is_null() {
|
||||
p["slices"] = mode.clone();
|
||||
}
|
||||
assert_eq!(names(&s.execute("file.export.saveForWebLegacy", p).unwrap()).len(), expect, "{mode}");
|
||||
}
|
||||
let out = tmp("slice-modes-selected");
|
||||
let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "dir": out, "slices": "selected", "numbers": [right]})).unwrap();
|
||||
assert_eq!(names(&r), ["right.png"]);
|
||||
let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "dir": out, "slices": "user", "numbers": [left]})).unwrap();
|
||||
assert_eq!(names(&r), ["left.png"], "numbers choose the slices whatever the mode");
|
||||
// A document without slices exports the whole image as one, whatever the dialog's Slices
|
||||
// choice: the dialog sends "selected" without numbers then (`web_params`).
|
||||
let mut s = session(8);
|
||||
for mode in ["all", "user", "selected"] {
|
||||
let out = tmp("slice-modes-none");
|
||||
if mode != "user" {
|
||||
let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "dir": out, "slices": mode})).unwrap();
|
||||
assert_eq!(names(&r), ["web.png"], "{mode}");
|
||||
assert_eq!(decode(r["files"][0].as_str().unwrap()).dimensions(), (64, 48), "{mode}");
|
||||
}
|
||||
let path = format!("{out}/whole.png");
|
||||
s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": path, "slices": mode})).unwrap();
|
||||
assert_eq!(decode(&path).dimensions(), (64, 48), "{mode}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_whole_image_path_with_slice_numbers_is_refused() {
|
||||
// CodeRabbit review on PR #10557: `path` alone writes the whole image as one file, so slice
|
||||
// `numbers` with it were ignored and every slice came out in that file. It is refused; `dir`
|
||||
// exports the chosen slices.
|
||||
let dir = tmp("path-numbers");
|
||||
let (mut s, left, right) = two_slices();
|
||||
let path = format!("{dir}/web.png");
|
||||
assert_refused(&mut s, &dir, json!({"format": "png24", "path": path, "numbers": [right]}), "use `dir`");
|
||||
assert_refused(&mut s, &dir, json!({"format": "png24", "path": path, "dir": "", "numbers": [left, right]}), "use `dir`");
|
||||
// `folder` alone is the whole image under the document's name (#5305): refused the same way.
|
||||
assert_refused(&mut s, &dir, json!({"format": "png24", "folder": dir, "numbers": [right]}), "use `dir`");
|
||||
// An explicit null `slices` is the default mode, as if absent.
|
||||
assert_refused(&mut s, &dir, json!({"format": "png24", "path": path, "slices": null, "numbers": [right]}), "use `dir`");
|
||||
// Valid calls still work: `path` alone is the whole image; with `slices`, the chosen slices
|
||||
// go next to it; `dir` with `numbers` writes those slices.
|
||||
let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": path})).unwrap();
|
||||
assert_eq!(r["files"], json!([path]));
|
||||
assert_eq!(decode(&path).dimensions(), (64, 48));
|
||||
let names = |r: &Value| r["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().rsplit('/').next().unwrap().to_string()).collect::<Vec<_>>();
|
||||
let out = tmp("path-numbers-selected");
|
||||
let r = s
|
||||
.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": format!("{out}/web.png"), "slices": "selected", "numbers": [right]}))
|
||||
.unwrap();
|
||||
assert_eq!(names(&r), ["right.png"]);
|
||||
let out = tmp("path-numbers-dir");
|
||||
let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": format!("{out}/web.png"), "dir": out, "numbers": [left]})).unwrap();
|
||||
assert_eq!(names(&r), ["left.png"]);
|
||||
// A document without slices is one image: its only slice number is the whole image.
|
||||
let mut s = session(8);
|
||||
let out = tmp("path-numbers-none");
|
||||
let whole = format!("{out}/whole.png");
|
||||
s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": whole, "numbers": [1]})).unwrap();
|
||||
assert_eq!(decode(&whole).dimensions(), (64, 48));
|
||||
}
|
||||
|
||||
@@ -1042,7 +1042,10 @@ impl Session {
|
||||
/// Run a command, in the background when it supports it (see [`jobs`](crate::jobs)). Errors
|
||||
/// like [`Session::execute`] for unknown, disabled or failing commands.
|
||||
pub fn start(&mut self, id: &str, params: Value) -> Result<Started> {
|
||||
self.dispatch(id, params, true)
|
||||
let before = self.active_layer_stamp();
|
||||
let started = self.dispatch(id, params, true);
|
||||
self.note_layer_change(before);
|
||||
started
|
||||
}
|
||||
|
||||
pub const MAX_PENDING_PREFLIGHTS: usize = 32;
|
||||
@@ -1924,7 +1927,13 @@ impl Session {
|
||||
}
|
||||
|
||||
/// Apply (or report the failure of) a job whose worker has delivered `r`.
|
||||
fn finish_job(&mut self, mut job: Running, r: Result<Payload>) {
|
||||
fn finish_job(&mut self, job: Running, r: Result<Payload>) {
|
||||
let before = self.active_layer_stamp();
|
||||
self.finish_job_step(job, r);
|
||||
self.note_layer_change(before);
|
||||
}
|
||||
|
||||
fn finish_job_step(&mut self, mut job: Running, r: Result<Payload>) {
|
||||
let apply = job.apply.take();
|
||||
// Keep the typed error beside its display text, so `wait_job` and `JobEvent` can still
|
||||
// report a hard budget's required/limit or an approval request's limits.
|
||||
|
||||
@@ -676,6 +676,15 @@ fn overlay_gradient(g: &mut Gradient, p: &Value) {
|
||||
if let Some(x) = b_of(p, "reverse") {
|
||||
g.reverse = x;
|
||||
}
|
||||
// The gradient's centre as a fraction of the layer's bounds, `[x, y]` (0: centred); a drag on
|
||||
// the image with the Layer Style dialog on the page moves it (#10763).
|
||||
if let Some(a) = p.get("offset").and_then(Value::as_array)
|
||||
&& let (Some(x), Some(y)) = (a.first().and_then(Value::as_f64), a.get(1).and_then(Value::as_f64))
|
||||
&& x.is_finite()
|
||||
&& y.is_finite()
|
||||
{
|
||||
g.offset = (x.clamp(-4.0, 4.0) as f32, y.clamp(-4.0, 4.0) as f32);
|
||||
}
|
||||
}
|
||||
|
||||
/// Applies the params present in `p` onto an existing effect, in the command's
|
||||
|
||||
@@ -200,6 +200,16 @@ mod tests {
|
||||
assert_eq!(gradient.angle, 90.0);
|
||||
assert!(gradient.reverse);
|
||||
assert_eq!(gradient.scale, 0.75);
|
||||
// The offset (#10763) is geometry too: a fraction of the layer's bounds, left alone by
|
||||
// junk and clamped.
|
||||
overlay(&mut gradient, &mut source, &json!({"offset": [0.25, -0.1]}));
|
||||
assert_eq!(gradient.offset, (0.25, -0.1));
|
||||
assert_eq!(gradient.stops, expected);
|
||||
overlay(&mut gradient, &mut source, &json!({"offset": "far"}));
|
||||
overlay(&mut gradient, &mut source, &json!({"offset": [f64::NAN, 0.0]}));
|
||||
assert_eq!(gradient.offset, (0.25, -0.1));
|
||||
overlay(&mut gradient, &mut source, &json!({"offset": [100.0, -100.0]}));
|
||||
assert_eq!(gradient.offset, (4.0, -4.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -216,6 +216,13 @@ pub struct Session {
|
||||
active_generation: u64,
|
||||
/// The document id [`Self::active_generation`] was last bumped for.
|
||||
active_doc_for_generation: Option<photocraft_doc::DocId>,
|
||||
/// Change marker: advances at least once whenever the active document or layer changes through
|
||||
/// the `Session` API (commands via `execute`, `start` and finished jobs, `select_layer`,
|
||||
/// `restore_layer_selection`, `layer_multi_cmds::set_selection`, `set_active_layer`, undo / redo /
|
||||
/// rollback, document switches). Compare two readings for equality to know whether anything
|
||||
/// changed in between; it is not an exact count of operations (one operation may advance it
|
||||
/// more than once). Writing `DocState::active_layer` directly bypasses it.
|
||||
active_layer_changes: u64,
|
||||
pub tools: ToolState,
|
||||
/// Log of executed commands (for action recording and debugging).
|
||||
pub journal: Vec<(String, Value)>,
|
||||
@@ -332,6 +339,8 @@ pub struct Session {
|
||||
pub damage_misses: usize,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod brush_pressure_size_default_tests;
|
||||
#[cfg(test)]
|
||||
mod embed_all_linked_tests;
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -227,6 +227,10 @@ pub struct General {
|
||||
pub export_clipboard: bool,
|
||||
pub resize_image_during_place: bool,
|
||||
pub always_create_smart_objects_when_placing: bool,
|
||||
/// Place files without opening Free Transform on them (#8492). Only the interactive place
|
||||
/// (File ▸ Place, canvas drops) opens Free Transform; `file.placeEmbedded`/`file.placeLinked`
|
||||
/// run with a `path` (actions, scripts, automation) never do, so this doesn't affect them.
|
||||
pub skip_transform_when_placing: bool,
|
||||
/// Scroll wheel zooms instead of panning.
|
||||
pub zoom_with_scroll_wheel: bool,
|
||||
/// Wheel zoom eases over the frames of a notch; off, a notch is one instant ×1.1 step.
|
||||
@@ -247,6 +251,7 @@ impl Default for General {
|
||||
export_clipboard: true,
|
||||
resize_image_during_place: true,
|
||||
always_create_smart_objects_when_placing: true,
|
||||
skip_transform_when_placing: false,
|
||||
zoom_with_scroll_wheel: false,
|
||||
animated_zoom: true,
|
||||
zoom_resizes_windows: false,
|
||||
@@ -1296,6 +1301,10 @@ pub struct Preferences {
|
||||
/// Last selected tool, restored at launch. A tool-name string owned and validated by the
|
||||
/// shell; null in older files. Kept separate from tool options so neither overwrites the other.
|
||||
pub selected_tool: Value,
|
||||
/// The tools used last, most recent first, restored at launch so each toolbar slot shows the
|
||||
/// tool it was left on and each tool key brings it back, as in Photoshop (#3569). Tool-name
|
||||
/// strings owned and validated by the shell; null in older files.
|
||||
pub group_tools: Value,
|
||||
/// View › Extras, the View › Show items (Pixel Grid, Grid, Guides, …) and the View › Snap To
|
||||
/// targets that differ from their defaults, saved as the user toggles them and restored at
|
||||
/// launch, as Photoshop keeps them between sessions (#3762). JSON owned by the shell.
|
||||
|
||||
@@ -307,8 +307,8 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
kind_spec!("view.proofSetup.workingBlackPlate", "Working Black Plate"),
|
||||
kind_spec!("view.proofSetup.workingCmyPlate", "Working CMY Plates"),
|
||||
kind_spec!("view.proofSetup.legacyMacintoshRgb", "Legacy Macintosh RGB (Gamma 1.8)"),
|
||||
kind_spec!("view.proofSetup.colorBlindnessProtanopia", "Color Blindness — Protanopia-type"),
|
||||
kind_spec!("view.proofSetup.colorBlindnessDeuteranopia", "Color Blindness — Deuteranopia-type"),
|
||||
kind_spec!("view.proofSetup.colorBlindnessProtanopia", "Color Blindness - Protanopia-type"),
|
||||
kind_spec!("view.proofSetup.colorBlindnessDeuteranopia", "Color Blindness - Deuteranopia-type"),
|
||||
CommandSpec {
|
||||
id: "view.thirtyTwoBitPreviewOptions",
|
||||
label: "32-bit Preview Options…",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::{EngineError, Result};
|
||||
|
||||
use super::history::fix_selection;
|
||||
use super::{DocState, Document, Session, channel_cmds, smart_cmds};
|
||||
use super::{DocState, Document, LayerId, Session, channel_cmds, smart_cmds};
|
||||
|
||||
/// Move item `i` of `v` to position `to`, clamped to the end. Returns where it went; `None` when
|
||||
/// `i` is out of range.
|
||||
@@ -43,16 +43,48 @@ impl Session {
|
||||
self.active_generation
|
||||
}
|
||||
|
||||
/// Change marker: advances at least once whenever the active document or layer changes through
|
||||
/// the `Session` API (see the field). Compare two readings for equality to know whether anything
|
||||
/// changed in between; it is not an exact count of operations. A direct write to
|
||||
/// `DocState::active_layer` through `active_mut()` bypasses it.
|
||||
pub fn active_layer_changes(&self) -> u64 {
|
||||
self.active_layer_changes
|
||||
}
|
||||
|
||||
/// Bump [`Self::active_generation`] when the active document id changed. Call after every
|
||||
/// assignment to `self.active`.
|
||||
pub(crate) fn note_active_change(&mut self) {
|
||||
let id = self.active().map(|st| st.doc.id);
|
||||
if id != self.active_doc_for_generation {
|
||||
self.active_layer_changes = self.active_layer_changes.wrapping_add(1);
|
||||
self.active_doc_for_generation = id;
|
||||
self.active_generation = self.active_generation.wrapping_add(1);
|
||||
}
|
||||
}
|
||||
|
||||
/// The active document and its active layer, for [`Self::note_layer_change`].
|
||||
pub(crate) fn active_layer_stamp(&self) -> Option<(photocraft_doc::DocId, Option<LayerId>)> {
|
||||
self.active().map(|st| (st.doc.id, st.active_layer))
|
||||
}
|
||||
|
||||
/// Count a change of the active document or layer since `before` (from
|
||||
/// [`Self::active_layer_stamp`]). Every path that can change either calls this after it.
|
||||
pub(crate) fn note_layer_change(&mut self, before: Option<(photocraft_doc::DocId, Option<LayerId>)>) {
|
||||
if self.active_layer_stamp() != before {
|
||||
self.active_layer_changes = self.active_layer_changes.wrapping_add(1);
|
||||
}
|
||||
}
|
||||
|
||||
/// Make `layer` the active document's active layer and nothing else (the selection list and
|
||||
/// anchor are left as they are), counted in [`Self::active_layer_changes`].
|
||||
pub fn set_active_layer(&mut self, layer: Option<LayerId>) {
|
||||
let before = self.active_layer_stamp();
|
||||
if let Some(st) = self.active_mut() {
|
||||
st.active_layer = layer;
|
||||
}
|
||||
self.note_layer_change(before);
|
||||
}
|
||||
|
||||
pub fn set_active(&mut self, index: usize) -> bool {
|
||||
if index < self.docs.len() {
|
||||
self.active = Some(index);
|
||||
|
||||
@@ -15,7 +15,10 @@ impl Session {
|
||||
/// Runs synchronously, including job-capable commands (see [`jobs`]; [`Session::start`] runs
|
||||
/// those in the background).
|
||||
pub fn execute(&mut self, id: &str, params: Value) -> Result<Value> {
|
||||
match self.dispatch(id, params, false)? {
|
||||
let before = self.active_layer_stamp();
|
||||
let result = self.dispatch(id, params, false);
|
||||
self.note_layer_change(before);
|
||||
match result? {
|
||||
jobs::Started::Done(v) => Ok(v),
|
||||
// Not reached: inline dispatch never starts a job. Waiting is still correct.
|
||||
jobs::Started::Job(j) => self.wait_job(j),
|
||||
|
||||
@@ -4,6 +4,13 @@ use super::{Arc, DocState, Document, LayerId, LayerTarget, Session, channel_cmds
|
||||
impl Session {
|
||||
/// Undo a document step or restore Clear History's rows and source without editing pixels.
|
||||
pub fn undo(&mut self) -> bool {
|
||||
let before = self.active_layer_stamp();
|
||||
let taken = self.undo_step();
|
||||
self.note_layer_change(before);
|
||||
taken
|
||||
}
|
||||
|
||||
fn undo_step(&mut self) -> bool {
|
||||
// A background job is computing from the current state: it must not move under it.
|
||||
if self.active_job().is_some() {
|
||||
return false;
|
||||
@@ -55,6 +62,13 @@ impl Session {
|
||||
|
||||
/// Restore the next document state and its selection/visibility bookkeeping through the ordinary history path.
|
||||
pub fn redo(&mut self) -> bool {
|
||||
let before = self.active_layer_stamp();
|
||||
let taken = self.redo_step();
|
||||
self.note_layer_change(before);
|
||||
taken
|
||||
}
|
||||
|
||||
fn redo_step(&mut self) -> bool {
|
||||
if self.active_job().is_some() {
|
||||
return false;
|
||||
}
|
||||
@@ -92,6 +106,13 @@ impl Session {
|
||||
/// last of them). Falls back to Undo plus forgetting the redo states when the history
|
||||
/// changed since that step. Returns whether a step was taken back.
|
||||
pub fn rollback(&mut self) -> bool {
|
||||
let before = self.active_layer_stamp();
|
||||
let taken = self.rollback_step();
|
||||
self.note_layer_change(before);
|
||||
taken
|
||||
}
|
||||
|
||||
fn rollback_step(&mut self) -> bool {
|
||||
if self.active_job().is_some() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ use crate::TypeLayerRef;
|
||||
impl Session {
|
||||
/// Replace the active document's active layer without creating a history step.
|
||||
pub fn select_layer(&mut self, id: LayerId) -> Result<()> {
|
||||
let before = self.active_layer_stamp();
|
||||
let st = self.active_mut().ok_or(EngineError::NoDocument)?;
|
||||
if st.doc.layer(id).is_none() {
|
||||
return Err(EngineError::NoLayer(id));
|
||||
@@ -20,6 +21,7 @@ impl Session {
|
||||
st.saved_revision = st.revision;
|
||||
}
|
||||
st.last_damage = Some(photocraft_geom::Rect::EMPTY);
|
||||
self.note_layer_change(before);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -28,6 +30,7 @@ impl Session {
|
||||
/// is no layer selected. Selected layers that are gone are dropped; an active layer that is
|
||||
/// gone is an error and changes nothing. Like [`Self::select_layer`], not an edit.
|
||||
pub fn restore_layer_selection(&mut self, active: Option<LayerId>, selected: &[LayerId], anchor: Option<LayerId>) -> Result<()> {
|
||||
let before = self.active_layer_stamp();
|
||||
let st = self.active_mut().ok_or(EngineError::NoDocument)?;
|
||||
if let Some(id) = active
|
||||
&& st.doc.layer(id).is_none()
|
||||
@@ -45,6 +48,7 @@ impl Session {
|
||||
st.saved_revision = st.revision;
|
||||
}
|
||||
st.last_damage = Some(photocraft_geom::Rect::EMPTY);
|
||||
self.note_layer_change(before);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1986,3 +1986,59 @@ fn the_document_activation_generation_bumps_on_switch_and_back() {
|
||||
assert_eq!(s.active_generation(), g3);
|
||||
assert_eq!(s.active().unwrap().doc.id, first);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_active_layer_change_count_moves_only_when_a_command_changes_the_active_layer() {
|
||||
let mut s = session_with_doc();
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
let a = s.active().unwrap().active_layer.unwrap();
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
let b = s.active().unwrap().active_layer.unwrap();
|
||||
let n = s.active_layer_changes();
|
||||
s.execute("layer.select", json!({"layer": b.0})).unwrap();
|
||||
assert_eq!(s.active_layer_changes(), n, "selecting the active layer again changes nothing");
|
||||
s.execute("layer.select", json!({"layer": a.0})).unwrap();
|
||||
assert!(s.active_layer_changes() > n);
|
||||
let m = s.active_layer_changes();
|
||||
s.execute("layer.select", json!({"layer": b.0})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": a.0})).unwrap();
|
||||
assert!(s.active_layer_changes() >= m + 2, "a round trip back to the same layer still counts");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn direct_selection_calls_count_active_layer_changes_only_when_the_layer_changes() {
|
||||
let mut s = session_with_doc();
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
let a = s.active().unwrap().active_layer.unwrap();
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
let b = s.active().unwrap().active_layer.unwrap();
|
||||
let count = |s: &Session| s.active_layer_changes();
|
||||
let n = count(&s);
|
||||
s.select_layer(b).unwrap();
|
||||
assert_eq!(count(&s), n, "select_layer of the active layer");
|
||||
s.select_layer(a).unwrap();
|
||||
assert_eq!(count(&s), n + 1, "select_layer of another layer");
|
||||
s.restore_layer_selection(Some(a), &[a], Some(a)).unwrap();
|
||||
assert_eq!(count(&s), n + 1, "restore_layer_selection of the same active layer");
|
||||
s.restore_layer_selection(Some(b), &[b], Some(b)).unwrap();
|
||||
assert_eq!(count(&s), n + 2, "restore_layer_selection of another layer");
|
||||
layer_multi_cmds::set_selection(&mut s, vec![b], Some(b), Some(b)).unwrap();
|
||||
assert_eq!(count(&s), n + 2, "set_selection of the same active layer");
|
||||
layer_multi_cmds::set_selection(&mut s, vec![a], Some(a), Some(a)).unwrap();
|
||||
assert_eq!(count(&s), n + 3, "set_selection of another layer");
|
||||
s.set_active_layer(Some(a));
|
||||
assert_eq!(count(&s), n + 3, "set_active_layer of the same layer");
|
||||
s.set_active_layer(Some(b));
|
||||
assert_eq!(count(&s), n + 4, "set_active_layer of another layer");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_active_layer_marker_advances_for_a_new_document_and_not_for_a_same_layer_select() {
|
||||
let mut s = session_with_doc();
|
||||
let before = s.active_layer_changes();
|
||||
let id = s.active().unwrap().active_layer.unwrap();
|
||||
s.select_layer(id).unwrap();
|
||||
assert_eq!(s.active_layer_changes(), before, "re-selecting the active layer leaves the marker alone");
|
||||
s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
|
||||
assert!(s.active_layer_changes() > before, "a new active document advances the marker");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
//! The Magnetic Lasso follows the edges of the visible composite, whichever layer is active
|
||||
//! (#11686), in the live `EdgeSource` and in `select.magneticLasso` with `trace: true`.
|
||||
|
||||
use photocraft_algo::magnetic::Settings;
|
||||
use photocraft_doc::LayerId;
|
||||
use photocraft_engine::Session;
|
||||
use photocraft_engine::magnetic_cmds::EdgeSource;
|
||||
use photocraft_geom::Rect;
|
||||
use serde_json::json;
|
||||
|
||||
/// The red rectangle's pixels: x 50..125, y 40..100, 75 x 60 = 4500 px.
|
||||
const RED: Rect = Rect { x0: 50, y0: 40, x1: 125, y1: 100 };
|
||||
const RED_AREA: f32 = 4500.0;
|
||||
/// Fastening points 4 px outside the rectangle's corners.
|
||||
const OUTSIDE: [[f64; 2]; 4] = [[46.0, 36.0], [129.0, 36.0], [129.0, 104.0], [46.0, 104.0]];
|
||||
|
||||
struct Doc {
|
||||
s: Session,
|
||||
background: LayerId,
|
||||
red: LayerId,
|
||||
}
|
||||
|
||||
/// A 200 x 150 RGB document: a white Background and a layer above it filled red over `RED`. The
|
||||
/// red layer is active.
|
||||
fn doc() -> Doc {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 200, "height": 150})).unwrap();
|
||||
let background = s.active().unwrap().doc.layers[0].id;
|
||||
s.edit("paint", |doc, _| {
|
||||
doc.layers[0].surface_mut().unwrap().fill_rect(Rect::new(0, 0, 200, 150), &[1.0, 1.0, 1.0, 1.0]);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
let red = s.active().unwrap().active_layer.unwrap();
|
||||
assert_ne!(red, background);
|
||||
s.edit("paint", |doc, _| {
|
||||
doc.layer_mut(red).unwrap().surface_mut().unwrap().fill_rect(RED, &[0.86, 0.12, 0.12, 1.0]);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
Doc { s, background, red }
|
||||
}
|
||||
|
||||
fn select(s: &mut Session, id: LayerId) {
|
||||
s.execute("layer.select", json!({"layer": id.0})).unwrap();
|
||||
}
|
||||
|
||||
fn coverage(s: &Session, x: i32, y: i32) -> f32 {
|
||||
s.active().unwrap().doc.selection.as_ref().map_or(0.0, |m| m.sample_channel(x, y, 0))
|
||||
}
|
||||
|
||||
fn selected_area(s: &Session) -> f32 {
|
||||
(0..150).flat_map(|y| (0..200).map(move |x| (x, y))).map(|(x, y)| coverage(s, x, y)).sum()
|
||||
}
|
||||
|
||||
/// Traces a border through `OUTSIDE` and asserts it selected the rectangle, not the points' own
|
||||
/// quadrilateral (83 x 68 = 5644 px).
|
||||
fn assert_snaps(s: &mut Session, what: &str) {
|
||||
s.execute("select.magneticLasso", json!({"points": OUTSIDE, "trace": true})).unwrap();
|
||||
let area = selected_area(s);
|
||||
assert!((area - RED_AREA).abs() < 150.0, "{what}: the border should select the rectangle ({RED_AREA} px), selected {area} px");
|
||||
assert!(coverage(s, 52, 42) > 0.99 && coverage(s, 122, 97) > 0.99, "{what}: the rectangle's corners are inside");
|
||||
assert!(coverage(s, 48, 70) < 0.01 && coverage(s, 127, 70) < 0.01, "{what}: just outside the rectangle is not selected");
|
||||
s.execute("select.deselect", json!({})).unwrap();
|
||||
}
|
||||
|
||||
/// Whether a fastening point 4 px outside the rectangle's left edge snaps onto it.
|
||||
fn edge_snaps(s: &Session) -> bool {
|
||||
let mut src = EdgeSource::new(s).unwrap();
|
||||
let p = src.snap([46.0, 70.0], Settings::default()).unwrap();
|
||||
(p[0] - 50.0).abs() < 1.5
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_border_snaps_whichever_layer_is_active() {
|
||||
let mut d = doc();
|
||||
assert_snaps(&mut d.s, "the red layer active");
|
||||
select(&mut d.s, d.background);
|
||||
assert_snaps(&mut d.s, "the Background active");
|
||||
assert!(edge_snaps(&d.s), "Background active: the live source sees the red layer's edge");
|
||||
d.s.execute("layer.new.layer", json!({})).unwrap();
|
||||
assert!(d.s.active().unwrap().active_layer.is_some_and(|id| id != d.background && id != d.red));
|
||||
assert!(edge_snaps(&d.s), "a new empty layer active");
|
||||
assert_snaps(&mut d.s, "a new empty layer active");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_adjustment_or_group_layer_active_snaps_too() {
|
||||
let mut d = doc();
|
||||
d.s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap();
|
||||
let st = d.s.active().unwrap();
|
||||
assert!(st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| l.surface().is_none()));
|
||||
assert!(edge_snaps(&d.s), "an adjustment layer active");
|
||||
assert_snaps(&mut d.s, "an adjustment layer active");
|
||||
select(&mut d.s, d.background);
|
||||
d.s.execute("layer.new.group", json!({})).unwrap();
|
||||
assert!(edge_snaps(&d.s), "an empty group active");
|
||||
assert_snaps(&mut d.s, "an empty group active");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hidden_layer_contributes_no_edges() {
|
||||
let mut d = doc();
|
||||
select(&mut d.s, d.background);
|
||||
d.s.execute("layer.hideLayers", json!({"layer": d.red.0})).unwrap();
|
||||
assert!(!edge_snaps(&d.s), "the hidden red layer has no edge to snap to");
|
||||
d.s.execute("select.magneticLasso", json!({"points": OUTSIDE, "trace": true})).unwrap();
|
||||
let area = selected_area(&d.s);
|
||||
// The border follows the pointer: the points' own 83 x 68 quadrilateral.
|
||||
assert!((area - 5644.0).abs() < 150.0, "selected {area} px");
|
||||
// Showing it again brings the edge back, for a source made after the change.
|
||||
d.s.execute("layer.showLayers", json!({"layer": d.red.0})).unwrap();
|
||||
assert!(edge_snaps(&d.s));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_live_source_goes_stale_when_the_composite_changes() {
|
||||
let mut d = doc();
|
||||
select(&mut d.s, d.background);
|
||||
let src = EdgeSource::new(&d.s).unwrap();
|
||||
assert!(src.is_current(&d.s));
|
||||
d.s.execute("layer.hideLayers", json!({"layer": d.red.0})).unwrap();
|
||||
assert!(!src.is_current(&d.s), "hiding a layer changes what the border follows");
|
||||
}
|
||||
@@ -89,6 +89,11 @@ fn no_command_panics_or_hangs_on_adversarial_params() {
|
||||
json!({"x": -9, "y": -9, "radius": 0, "amount": 0, "angle": 0, "opacity": 0, "scale": 0, "levels": 0, "tolerance": 0, "gamma": 0, "layer": 999999, "channel": 999, "index": 999999, "count": 0}),
|
||||
json!({"x": 1e5, "y": -1e5, "radius": 300, "amount": 300, "angle": 1e5, "opacity": -100, "scale": -5, "gamma": -1}),
|
||||
json!({"points": [], "from": [0, 0], "to": [0, 0], "stops": [], "matrix": [0, 0, 0, 0, 0, 0], "colors": [], "name": "", "mode": "", "style": ""}),
|
||||
// Color Range and Replace Color samples (#6280): far off the canvas, malformed, and a
|
||||
// click order that doesn't match them.
|
||||
json!({"points": [[1e9, -1e9], [0.5, 0.5]], "subtractPoints": [[-1e9, 1e9], [2, 2]], "order": ["+", "-", "x", 5], "localized": true, "range": 0}),
|
||||
json!({"points": [[3, 3]], "subtractPoints": [[3]], "order": {}, "fuzziness": -1}),
|
||||
json!({"subtractPoints": "none", "order": ["-"], "labMinimum": [0, 0, 0], "labMaximum": [100, 0, 0]}),
|
||||
];
|
||||
let mut bad: Vec<String> = Vec::new();
|
||||
for spec in command_specs() {
|
||||
|
||||
@@ -21,6 +21,9 @@
|
||||
//! - **Network shares (macOS):** `sync_all` asks for a full flush to the disk's platters
|
||||
//! (`F_FULLFSYNC`), which SMB shares and some virtual file systems refuse while a plain `fsync`
|
||||
//! works. [`sync_file`] falls back to the plain one there (#1336).
|
||||
//! - **Hidden flags (network shares):** the temporary file's name never starts with a dot. An SMB
|
||||
//! server that hides dot files marks one hidden when it is created, the mark survives the
|
||||
//! rename, and the saved document then doesn't show in Finder (#11624). See [`temp_name`].
|
||||
//!
|
||||
//! On the web there is no file system: writes go through the platform services (downloads), so
|
||||
//! this module is never reached there; on `wasm32` every call returns the `std` "unsupported"
|
||||
@@ -479,13 +482,53 @@ fn resolve_symlink(path: &Path) -> io::Result<PathBuf> {
|
||||
Err(io::Error::new(io::ErrorKind::InvalidInput, format!("{}: too many levels of symbolic links", path.display())))
|
||||
}
|
||||
|
||||
/// A unique hidden temporary file name to write beside `file` before renaming over it.
|
||||
/// What every [`temp_name`] ends with.
|
||||
const TEMP_SUFFIX: &str = ".pcsave.tmp";
|
||||
|
||||
/// How many characters of the file name a [`temp_name`] keeps.
|
||||
const TEMP_STEM_CHARS: usize = 64;
|
||||
|
||||
/// A unique temporary file name to write beside `file` before renaming over it:
|
||||
/// `{file}.{pid}-{n}.pcsave.tmp`.
|
||||
///
|
||||
/// It does not start with a dot of its own, so it is not hidden: an SMB server that hides dot
|
||||
/// files sets the hidden flag on such a file when it is created, the flag survives the rename,
|
||||
/// and the saved document would be invisible in Finder (#11624). The cost is that the file shows
|
||||
/// in a folder listing while a save writes it, and one an interrupted save leaves beside the
|
||||
/// document stays visible until it is removed (#6604). The
|
||||
/// process id, counter and suffix keep it apart from the files people name, and it is created
|
||||
/// with `create_new`, so it can never replace one. Older versions wrote
|
||||
/// `.{file}.{pid}-{n}.pcsave.tmp`; [`temp_owner`] recognizes both.
|
||||
pub fn temp_name(file: &str) -> String {
|
||||
static COUNTER: AtomicU64 = AtomicU64::new(0);
|
||||
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
|
||||
// Keep the name short: a long file name plus the suffix must stay under NAME_MAX.
|
||||
let stem: String = file.chars().take(64).collect();
|
||||
format!(".{stem}.{}-{n}.pcsave.tmp", std::process::id())
|
||||
let stem: String = file.chars().take(TEMP_STEM_CHARS).collect();
|
||||
format!("{stem}.{}-{n}{TEMP_SUFFIX}", std::process::id())
|
||||
}
|
||||
|
||||
/// The id of the process that wrote `name`, if it is a [`temp_name`]: today's
|
||||
/// `{file}.{pid}-{n}.pcsave.tmp` or the `.{file}.{pid}-{n}.pcsave.tmp` older versions wrote, so
|
||||
/// their leftovers are still cleaned up.
|
||||
pub fn temp_owner(name: &str) -> Option<u32> {
|
||||
let rest = name.strip_suffix(TEMP_SUFFIX)?;
|
||||
let (stem, ids) = rest.rsplit_once('.')?;
|
||||
let (pid, n) = ids.split_once('-')?;
|
||||
// Only a file part `temp_name` writes: at most `TEMP_STEM_CHARS` characters, or one more when
|
||||
// the first is the dot older versions put in front of them.
|
||||
let chars = stem.chars().count();
|
||||
if chars > TEMP_STEM_CHARS && !(chars == TEMP_STEM_CHARS + 1 && stem.starts_with('.')) {
|
||||
return None;
|
||||
}
|
||||
let stem = stem.strip_prefix('.').unwrap_or(stem);
|
||||
// Only numbers `temp_name` writes: plain decimal digits with no leading zero (a lone "0" is
|
||||
// fine), a `u32` pid and the `u64` counter it counts with. Any other name is someone else's
|
||||
// file, and the sweep must not delete it.
|
||||
let decimal = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit()) && (s == "0" || !s.starts_with('0'));
|
||||
if stem.is_empty() || !decimal(pid) || !decimal(n) || n.parse::<u64>().is_err() {
|
||||
return None;
|
||||
}
|
||||
pid.parse().ok()
|
||||
}
|
||||
|
||||
fn context(e: io::Error, msg: String) -> io::Error {
|
||||
|
||||
@@ -274,7 +274,7 @@ pub(crate) fn recover_after_1177(
|
||||
/// **A backup name is not optional.** `ReplaceFile` without one deletes the replaced file before
|
||||
/// the final rename, and documents `ERROR_UNABLE_TO_MOVE_REPLACEMENT`: "the replaced file no
|
||||
/// longer exists and the replacement file exists under its original name" — the user's document
|
||||
/// gone, its replacement left under a hidden temporary name. With a backup name the same failure
|
||||
/// gone, its replacement left under a temporary name. With a backup name the same failure
|
||||
/// leaves both files where they were. So this always passes one, and deletes it on the way out.
|
||||
///
|
||||
/// The failures and what each leaves behind (from the `ReplaceFileW` reference):
|
||||
|
||||
@@ -663,3 +663,105 @@ fn keeps_the_creation_time_under_a_deny_write_acl() {
|
||||
let _ = exacl::setfacl(&[&p], &[], None);
|
||||
assert_no_temp(&d.0);
|
||||
}
|
||||
|
||||
/// A real file system that records the name of every temporary file a save writes.
|
||||
#[derive(Default)]
|
||||
struct TempNames(std::cell::RefCell<Vec<String>>);
|
||||
|
||||
impl AtomicFs for TempNames {
|
||||
fn write_new(&self, tmp: &Path, bytes: &[u8]) -> io::Result<()> {
|
||||
self.0.borrow_mut().push(tmp.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default());
|
||||
RealFs.write_new(tmp, bytes)
|
||||
}
|
||||
fn rename(&self, from: &Path, to: &Path) -> io::Result<()> {
|
||||
RealFs.rename(from, to)
|
||||
}
|
||||
fn remove(&self, path: &Path) -> io::Result<()> {
|
||||
RealFs.remove(path)
|
||||
}
|
||||
fn sync_dir(&self, dir: &Path) -> io::Result<()> {
|
||||
RealFs.sync_dir(dir)
|
||||
}
|
||||
fn copy_metadata(&self, from: &Path, to: &Path) -> io::Result<()> {
|
||||
RealFs.copy_metadata(from, to)
|
||||
}
|
||||
}
|
||||
|
||||
/// #11624: the temporary file a save renames over the document has no leading dot. An SMB share
|
||||
/// that hides dot files marks such a file hidden when it is created, and the mark stayed on the
|
||||
/// saved document, so it didn't show in Finder.
|
||||
#[test]
|
||||
fn the_temporary_file_is_not_a_dot_file() {
|
||||
let d = TempDir::new("visible");
|
||||
let p = d.0.join("report.psd");
|
||||
let fs = TempNames::default();
|
||||
atomic_write_with(&fs, NO_RETRY, &p, b"first").unwrap();
|
||||
atomic_write_with(&fs, NO_RETRY, &p, b"second").unwrap();
|
||||
let names = fs.0.take();
|
||||
assert_eq!(names.len(), 2, "{names:?}");
|
||||
for name in &names {
|
||||
assert!(!name.starts_with('.'), "a dot file is hidden on some shares: {name}");
|
||||
assert!(name.starts_with("report.psd.") && name.ends_with(".pcsave.tmp"), "{name}");
|
||||
assert_eq!(temp_owner(name), Some(std::process::id()), "{name}");
|
||||
}
|
||||
assert_ne!(names[0], names[1]);
|
||||
assert_eq!(std::fs::read(&p).unwrap(), b"second");
|
||||
assert_no_temp(&d.0);
|
||||
}
|
||||
|
||||
/// #11624: both temp name forms are recognized, so the leftovers of versions that wrote
|
||||
/// `.{file}.{pid}-{n}.pcsave.tmp` are still cleaned up; nothing else is.
|
||||
#[test]
|
||||
fn temp_owner_reads_both_name_forms_and_nothing_else() {
|
||||
assert_eq!(temp_owner(&temp_name("doc.pcraft")), Some(std::process::id()));
|
||||
assert_eq!(temp_owner("doc.pcraft.42-7.pcsave.tmp"), Some(42));
|
||||
assert_eq!(temp_owner(".doc.pcraft.42-7.pcsave.tmp"), Some(42), "the dot-prefixed form older versions wrote");
|
||||
assert_eq!(temp_owner(".hidden.psd.42-0.pcsave.tmp"), Some(42), "a dot file's own temp file");
|
||||
assert_eq!(temp_owner("doc.42-18446744073709551615.pcsave.tmp"), Some(42), "the largest counter");
|
||||
assert_eq!(temp_owner("doc.0-0.pcsave.tmp"), Some(0), "a lone zero is a number temp_name writes");
|
||||
for other in [
|
||||
"doc.pcraft",
|
||||
"doc.pcraft.42-7.tmp",
|
||||
"doc.42-7.pcsave",
|
||||
".42-7.pcsave.tmp",
|
||||
"doc.x-7.pcsave.tmp",
|
||||
"doc.+42-7.pcsave.tmp",
|
||||
"doc.42-.pcsave.tmp",
|
||||
"doc.42-x.pcsave.tmp",
|
||||
"doc.42-18446744073709551616.pcsave.tmp",
|
||||
"doc.4294967296-7.pcsave.tmp",
|
||||
"doc.42-07.pcsave.tmp",
|
||||
"doc.42-00.pcsave.tmp",
|
||||
"doc.042-7.pcsave.tmp",
|
||||
"doc.00-7.pcsave.tmp",
|
||||
"doc.42.pcsave.tmp",
|
||||
"42-7.pcsave.tmp",
|
||||
] {
|
||||
assert_eq!(temp_owner(other), None, "{other}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A recovery folder can hold someone else's file that only looks like a temp name. `temp_name`
|
||||
/// keeps at most 64 characters (not bytes) of the file name, and older versions put one dot in
|
||||
/// front of them, so a longer file part is not a leftover of a save and the sweep must keep it.
|
||||
#[test]
|
||||
fn temp_owner_takes_only_a_file_part_temp_name_writes() {
|
||||
let pid = Some(std::process::id());
|
||||
for file in ["a".repeat(200), "🚀".repeat(70), format!(".{}", "a".repeat(100))] {
|
||||
assert_eq!(temp_owner(&temp_name(&file)), pid, "{file}");
|
||||
}
|
||||
let today = |stem: &str| format!("{stem}.42-7.pcsave.tmp");
|
||||
let older = |stem: &str| format!(".{stem}.42-7.pcsave.tmp");
|
||||
for c in ["a", "é", "🚀"] {
|
||||
let (at, over) = (c.repeat(64), c.repeat(65));
|
||||
assert_eq!(temp_owner(&today(&at)), Some(42), "64 characters: {c}");
|
||||
assert_eq!(temp_owner(&older(&at)), Some(42), "64 characters after the old dot: {c}");
|
||||
assert_eq!(temp_owner(&today(&over)), None, "65 characters: {c}");
|
||||
assert_eq!(temp_owner(&older(&over)), None, "65 characters after the old dot: {c}");
|
||||
// A dot file's own dot counts in its 64 characters; the old form adds one more dot.
|
||||
let dotted = |k: usize| format!(".{}", c.repeat(k));
|
||||
assert_eq!(temp_owner(&today(&dotted(63))), Some(42), "a dot file today: {c}");
|
||||
assert_eq!(temp_owner(&older(&dotted(63))), Some(42), "a dot file in the old form: {c}");
|
||||
assert_eq!(temp_owner(&older(&dotted(64))), None, "a dot file one character over: {c}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -473,10 +473,8 @@ fn sweep_orphans(recovery_dir: &Path, min_age: std::time::Duration) {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn sweep_temp_file(path: &Path, min_age: std::time::Duration) {
|
||||
let Some(name) = path.file_name().and_then(|n| n.to_str()) else { return };
|
||||
// `.{stem}.{pid}-{n}.pcsave.tmp`
|
||||
let Some(rest) = name.strip_prefix('.').and_then(|n| n.strip_suffix(".pcsave.tmp")) else { return };
|
||||
let Some((_, owner)) = rest.rsplit_once('.') else { return };
|
||||
let Some(pid) = owner.split_once('-').and_then(|(pid, _)| pid.parse::<u32>().ok()) else { return };
|
||||
// `{stem}.{pid}-{n}.pcsave.tmp`, or `.{stem}…` from versions before #11624.
|
||||
let Some(pid) = crate::atomic::temp_owner(name) else { return };
|
||||
if pid != std::process::id() && untouched_for(path, min_age, false) {
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
@@ -573,6 +571,30 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// #11624: temp files are no longer dot files; the sweep removes another process's in both
|
||||
/// forms, so what older versions left is still cleaned up.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn sweep_removes_temp_files_of_both_name_forms() {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"pcraft-sweep-forms-{}-{}",
|
||||
std::process::id(),
|
||||
SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0)
|
||||
));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let foreign = [dir.join("kept.json.4294967294-3.pcsave.tmp"), dir.join(".kept.json.4294967294-4.pcsave.tmp")];
|
||||
let own = [dir.join(crate::atomic::temp_name("kept.json")), dir.join(format!(".kept.json.{}-9.pcsave.tmp", std::process::id()))];
|
||||
let others = [dir.join("kept.json"), dir.join("kept.json.4294967294-3.tmp"), dir.join("kept.json.pcsave.tmp")];
|
||||
for p in foreign.iter().chain(&own).chain(&others) {
|
||||
std::fs::write(p, b"tmp").unwrap();
|
||||
}
|
||||
sweep_orphans(&dir, std::time::Duration::ZERO);
|
||||
assert!(foreign.iter().all(|p| !p.exists()), "another process's temp files are swept, either form");
|
||||
assert!(own.iter().all(|p| p.is_file()), "this process's temp files are kept");
|
||||
assert!(others.iter().all(|p| p.is_file()), "anything else is left alone");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn a_committed_bundle_without_a_sidecar_is_listed_and_survives_the_sweep() {
|
||||
|
||||
@@ -630,7 +630,8 @@ impl Default for BrushSettings {
|
||||
spacing_enabled: true,
|
||||
opacity: 1.0,
|
||||
flow: 1.0,
|
||||
pressure_size: true,
|
||||
// Off, as the reference application leaves it after Reset Tool (#11809).
|
||||
pressure_size: false,
|
||||
pressure_opacity: false,
|
||||
color: [0.0, 0.0, 0.0, 1.0],
|
||||
color_cmyk: None,
|
||||
@@ -702,6 +703,15 @@ impl BrushSettings {
|
||||
brush.dual_brush.size = safe_size(brush.dual_brush.size);
|
||||
brush.scattering.scatter.jitter = safe_scatter(brush.scattering.scatter.jitter);
|
||||
brush.dual_brush.scatter = safe_scatter(brush.dual_brush.scatter);
|
||||
// A NaN here would paint fully opaque (`f32::min` returns the other operand) or not at all;
|
||||
// it falls back to the default (the values of `BrushSettings::default` and `DualBrush::default`). Every other value, infinities included, is left to the clamps downstream.
|
||||
let nan_or = |v: f32, d: f32| if v.is_nan() { d } else { v };
|
||||
brush.opacity = nan_or(brush.opacity, 1.0);
|
||||
brush.flow = nan_or(brush.flow, 1.0);
|
||||
brush.hardness = nan_or(brush.hardness, 1.0);
|
||||
brush.spacing = nan_or(brush.spacing, 0.1);
|
||||
brush.dual_brush.hardness = nan_or(brush.dual_brush.hardness, 1.0);
|
||||
brush.dual_brush.spacing = nan_or(brush.dual_brush.spacing, 0.25);
|
||||
brush
|
||||
}
|
||||
|
||||
|
||||
+185
-26
@@ -114,6 +114,10 @@ impl Smoother {
|
||||
|
||||
/// Feed one input point; returns the brush positions to paint through.
|
||||
pub fn push(&mut self, p: StrokePoint, out: &mut Vec<StrokePoint>) {
|
||||
// A point without a position is dropped: kept, it would make every later smoothed position NaN.
|
||||
if !(p.x.is_finite() && p.y.is_finite()) {
|
||||
return;
|
||||
}
|
||||
let prev_input = self.last_input.replace(p);
|
||||
if !self.active() {
|
||||
out.push(p);
|
||||
@@ -550,6 +554,13 @@ pub fn base_diameter(b: &BrushSettings, s: &StepInput) -> f32 {
|
||||
b.size * f
|
||||
}
|
||||
|
||||
/// Distance to the next step at `p`: the diameter there times the spacing.
|
||||
fn step_len_for(brush: &BrushSettings, p: &StrokePoint) -> f64 {
|
||||
let s = StepInput { point: *p, direction: 0.0, initial_direction: 0.0, step: 0 };
|
||||
let d = base_diameter(brush, &s).max(1.0);
|
||||
f64::from(d * brush.spacing.max(0.01)).max(0.5)
|
||||
}
|
||||
|
||||
/// Builds the primary dabs of a step (Shape Dynamics, Scattering, Transfer, Color, Texture depth).
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct DabBuilder {
|
||||
@@ -566,9 +577,7 @@ impl DabBuilder {
|
||||
}
|
||||
|
||||
pub fn step_len(&self, p: &StrokePoint) -> f64 {
|
||||
let s = StepInput { point: *p, direction: 0.0, initial_direction: 0.0, step: 0 };
|
||||
let d = base_diameter(&self.brush, &s).max(1.0);
|
||||
f64::from(d * self.brush.spacing.max(0.01)).max(0.5)
|
||||
step_len_for(&self.brush, p)
|
||||
}
|
||||
|
||||
pub fn build(&mut self, s: &StepInput, out: &mut Vec<Dab>) {
|
||||
@@ -768,6 +777,9 @@ pub struct DabGenerator {
|
||||
builder: DabBuilder,
|
||||
dual: Option<DualBuilder>,
|
||||
scratch: Vec<StrokePoint>,
|
||||
/// The brush the step length is read from, apart from the builder so the walker can read it
|
||||
/// while its callback has the builder.
|
||||
step_brush: std::sync::Arc<BrushSettings>,
|
||||
}
|
||||
|
||||
impl DabGenerator {
|
||||
@@ -798,26 +810,21 @@ impl DabGenerator {
|
||||
builder: DabBuilder::new(&brush),
|
||||
dual: dual_on.then(|| DualBuilder::new(&brush)),
|
||||
scratch: Vec::new(),
|
||||
step_brush: std::sync::Arc::new(brush),
|
||||
}
|
||||
}
|
||||
|
||||
fn walk(&mut self, pts: &[StrokePoint], dabs: &mut Vec<Dab>, dual: &mut Vec<Dab>) {
|
||||
for p in pts {
|
||||
let p = apply_pose(&self.builder.brush, *p);
|
||||
let b = &self.builder;
|
||||
let len = |q: &StrokePoint| b.step_len(q);
|
||||
let mut steps = Vec::new();
|
||||
self.walker.push(p, &len, &mut |s| steps.push(s));
|
||||
for s in &steps {
|
||||
self.builder.build(s, dabs);
|
||||
}
|
||||
// Each step becomes its dabs as the walker emits it: a long segment never holds its
|
||||
// step list (the pixel-grid mode can emit 1e7 steps for one).
|
||||
let brush = &*self.step_brush;
|
||||
let builder = &mut self.builder;
|
||||
self.walker.push(p, &|q| step_len_for(brush, q), &mut |s| builder.build(&s, dabs));
|
||||
if let (Some(w), Some(db)) = (self.dual_walker.as_mut(), self.dual.as_mut()) {
|
||||
let mut steps = Vec::new();
|
||||
let len = |q: &StrokePoint| db.step_len(q);
|
||||
w.push(p, &len, &mut |s| steps.push(s));
|
||||
for s in &steps {
|
||||
db.build(s, dual);
|
||||
}
|
||||
let step = db.step_len(&p);
|
||||
w.push(p, &|_| step, &mut |s| db.build(&s, dual));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -838,17 +845,9 @@ impl DabGenerator {
|
||||
let mut sm = Vec::new();
|
||||
self.smoother.finish(&mut sm);
|
||||
self.walk(&sm, dabs, dual);
|
||||
let mut steps = Vec::new();
|
||||
self.walker.finish(&mut |s| steps.push(s));
|
||||
for s in &steps {
|
||||
self.builder.build(s, dabs);
|
||||
}
|
||||
self.walker.finish(&mut |s| self.builder.build(&s, dabs));
|
||||
if let (Some(w), Some(db)) = (self.dual_walker.as_mut(), self.dual.as_mut()) {
|
||||
let mut steps = Vec::new();
|
||||
w.finish(&mut |s| steps.push(s));
|
||||
for s in &steps {
|
||||
db.build(s, dual);
|
||||
}
|
||||
w.finish(&mut |s| db.build(&s, dual));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -887,3 +886,163 @@ mod walker_budget_tests {
|
||||
assert_eq!(steps.get(), before + 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod streaming_tests {
|
||||
use super::*;
|
||||
|
||||
/// The step-by-step walk before the steps were streamed: every step of a point is collected
|
||||
/// first, then built. The streamed walk must produce the same dabs.
|
||||
fn buffered(brush: &BrushSettings, pts: &[StrokePoint]) -> (Vec<Dab>, Vec<Dab>) {
|
||||
let brush = brush.bounded_for_render();
|
||||
let mut smoother = Smoother::new(&brush.smoothing, 1.0);
|
||||
let mut walker = DabGenerator::new(&brush, 1.0).walker;
|
||||
let mut dual_walker = brush.dual_brush.enabled.then(|| PathWalker::new(None));
|
||||
let (mut builder, mut dual_builder) = (DabBuilder::new(&brush), brush.dual_brush.enabled.then(|| DualBuilder::new(&brush)));
|
||||
let (mut dabs, mut dual) = (Vec::new(), Vec::new());
|
||||
let mut sm = Vec::new();
|
||||
for p in pts {
|
||||
smoother.push(*p, &mut sm);
|
||||
}
|
||||
smoother.finish(&mut sm);
|
||||
for p in sm {
|
||||
let p = apply_pose(&builder.brush, p);
|
||||
let mut steps = Vec::new();
|
||||
walker.push(p, &|q| builder.step_len(q), &mut |s| steps.push(s));
|
||||
for s in &steps {
|
||||
builder.build(s, &mut dabs);
|
||||
}
|
||||
if let (Some(w), Some(db)) = (dual_walker.as_mut(), dual_builder.as_mut()) {
|
||||
let mut steps = Vec::new();
|
||||
w.push(p, &|q| db.step_len(q), &mut |s| steps.push(s));
|
||||
for s in &steps {
|
||||
db.build(s, &mut dual);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut steps = Vec::new();
|
||||
walker.finish(&mut |s| steps.push(s));
|
||||
for s in &steps {
|
||||
builder.build(s, &mut dabs);
|
||||
}
|
||||
if let (Some(w), Some(db)) = (dual_walker.as_mut(), dual_builder.as_mut()) {
|
||||
let mut steps = Vec::new();
|
||||
w.finish(&mut |s| steps.push(s));
|
||||
for s in &steps {
|
||||
db.build(s, &mut dual);
|
||||
}
|
||||
}
|
||||
(dabs, dual)
|
||||
}
|
||||
|
||||
fn streamed(brush: &BrushSettings, pts: &[StrokePoint], chunk: usize) -> (Vec<Dab>, Vec<Dab>) {
|
||||
let mut g = DabGenerator::new(brush, 1.0);
|
||||
let (mut dabs, mut dual) = (Vec::new(), Vec::new());
|
||||
for c in pts.chunks(chunk) {
|
||||
g.push(c, &mut dabs, &mut dual);
|
||||
}
|
||||
g.finish(&mut dabs, &mut dual);
|
||||
(dabs, dual)
|
||||
}
|
||||
|
||||
fn path() -> Vec<StrokePoint> {
|
||||
let mut pts = Vec::new();
|
||||
for i in 0..40 {
|
||||
let t = f64::from(i);
|
||||
let mut p = StrokePoint::new(10.0 + t * 9.3, 50.0 + (t * 0.7).sin() * 30.0, 0.3 + 0.7 * ((t * 0.31).sin().abs() as f32));
|
||||
p.time = t * 7.0;
|
||||
pts.push(p);
|
||||
}
|
||||
pts
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn streamed_dabs_equal_the_buffered_walk() {
|
||||
let mut variants: Vec<BrushSettings> = vec![BrushSettings::default()];
|
||||
variants.push(BrushSettings { size: 14.0, spacing: 0.05, ..BrushSettings::default() });
|
||||
variants.push(BrushSettings { size: 1.0, aliased: true, ..BrushSettings::default() });
|
||||
variants.push(BrushSettings { size: 9.0, spacing_enabled: false, ..BrushSettings::default() });
|
||||
variants.push(BrushSettings { size: 12.0, build_up: true, build_up_rate: 40.0, ..BrushSettings::default() });
|
||||
let mut rich = BrushSettings { size: 18.0, spacing: 0.2, ..BrushSettings::default() };
|
||||
rich.scattering.enabled = true;
|
||||
rich.scattering.scatter.jitter = 0.5;
|
||||
rich.scattering.count = 3;
|
||||
rich.dual_brush.enabled = true;
|
||||
rich.dual_brush.size = 8.0;
|
||||
rich.dual_brush.count = 2;
|
||||
rich.shape_dynamics.enabled = true;
|
||||
rich.shape_dynamics.size.jitter = 0.6;
|
||||
rich.smoothing.amount = 0.5;
|
||||
variants.push(rich);
|
||||
for (n, b) in variants.iter().enumerate() {
|
||||
let (want, want_dual) = buffered(b, &path());
|
||||
assert!(want.len() > 10, "variant {n}: {} dabs", want.len());
|
||||
for chunk in [1, 7, 1000] {
|
||||
let (got, got_dual) = streamed(b, &path(), chunk);
|
||||
assert_eq!(got, want, "variant {n}, chunk {chunk}");
|
||||
assert_eq!(got_dual, want_dual, "variant {n}, chunk {chunk} (dual)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_nan_opacity_flow_hardness_or_spacing_paints_as_the_default() {
|
||||
let pts = path();
|
||||
let base = BrushSettings { size: 10.0, ..BrushSettings::default() };
|
||||
let (want, _) = streamed(&base, &pts, 1000);
|
||||
for broken in [
|
||||
BrushSettings { opacity: f32::NAN, ..base.clone() },
|
||||
BrushSettings { flow: f32::NAN, ..base.clone() },
|
||||
BrushSettings { hardness: f32::NAN, ..base.clone() },
|
||||
BrushSettings { spacing: f32::NAN, ..base.clone() },
|
||||
] {
|
||||
// The default brush has opacity, flow and hardness 1 and spacing 0.1, as `base` does.
|
||||
let (got, _) = streamed(&broken, &pts, 1000);
|
||||
assert_eq!(got, want);
|
||||
let b = broken.bounded_for_render();
|
||||
assert!(b.opacity.is_finite() && b.flow.is_finite() && b.hardness.is_finite() && b.spacing.is_finite());
|
||||
}
|
||||
// Finite values are not touched, even out of range.
|
||||
let odd = BrushSettings { opacity: 3.0, flow: f32::NEG_INFINITY, spacing: f32::INFINITY, ..base };
|
||||
let b = odd.bounded_for_render();
|
||||
assert_eq!((b.opacity, b.flow, b.spacing), (3.0, f32::NEG_INFINITY, f32::INFINITY));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_nan_dual_hardness_or_spacing_paints_as_the_default() {
|
||||
let pts = path();
|
||||
let mut explicit = BrushSettings { size: 10.0, ..BrushSettings::default() };
|
||||
explicit.dual_brush.enabled = true;
|
||||
explicit.dual_brush.size = 6.0;
|
||||
explicit.dual_brush.hardness = 1.0;
|
||||
explicit.dual_brush.spacing = 0.25;
|
||||
let (_, want_dual) = streamed(&explicit, &pts, 1000);
|
||||
assert!(!want_dual.is_empty());
|
||||
let mut broken = explicit.clone();
|
||||
broken.dual_brush.spacing = f32::NAN;
|
||||
assert_eq!(streamed(&broken, &pts, 1000).1, want_dual);
|
||||
broken.dual_brush.hardness = f32::NAN;
|
||||
let b = broken.bounded_for_render();
|
||||
assert_eq!((b.dual_brush.hardness, b.dual_brush.spacing), (1.0, 0.25));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_point_without_a_position_does_not_poison_a_smoothed_stroke() {
|
||||
let mut brush = BrushSettings { size: 8.0, ..BrushSettings::default() };
|
||||
brush.smoothing.amount = 0.6;
|
||||
let clean = path();
|
||||
let mut dirty = clean.clone();
|
||||
dirty.insert(10, StrokePoint::new(f64::NAN, 40.0, 1.0));
|
||||
dirty.insert(20, StrokePoint::new(30.0, f64::INFINITY, 1.0));
|
||||
assert_eq!(streamed(&brush, &dirty, 1000).0, streamed(&brush, &clean, 1000).0);
|
||||
brush.smoothing.amount = 0.0;
|
||||
assert_eq!(streamed(&brush, &dirty, 1000).0, streamed(&brush, &clean, 1000).0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_long_pixel_grid_segment_streams() {
|
||||
let brush = BrushSettings { size: 1.0, aliased: true, spacing: 1.0, ..BrushSettings::default() };
|
||||
let (dabs, _) = streamed(&brush, &[StrokePoint::new(0.0, 0.0, 1.0), StrokePoint::new(2.0e6, 3.0, 1.0)], 1000);
|
||||
assert_eq!(dabs.len(), 2_000_001);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,12 +190,14 @@ pub fn apply_mixer_dab(
|
||||
state: &mut MixerState,
|
||||
lock_transparency: bool,
|
||||
) {
|
||||
// Only the dab's own rectangle is ever read from the sample, not the whole stroke rectangle.
|
||||
let sample = sample.filter(|_| m.samples_canvas(&fp.dab)).map(|surface| {
|
||||
let source = crate::retouch::Region::read(surface, work.rect);
|
||||
let rect = fp.rect.intersect(&work.rect);
|
||||
let source = crate::retouch::Region::read(surface, rect);
|
||||
if surface.format().mode == fmt.mode && surface.format().alpha == fmt.alpha {
|
||||
source
|
||||
} else {
|
||||
let mut native = crate::retouch::Region::new(work.rect, fmt.channels());
|
||||
let mut native = crate::retouch::Region::new(rect, fmt.channels());
|
||||
for (src, dst) in source.data.chunks_exact(source.ch).zip(native.data.chunks_exact_mut(native.ch)) {
|
||||
from_rgba_into(&fmt, to_rgba(&surface.format(), src), dst);
|
||||
}
|
||||
@@ -205,9 +207,10 @@ pub fn apply_mixer_dab(
|
||||
apply_mixer_dab_region(work, fp, fmt, sample.as_ref(), m, state, lock_transparency);
|
||||
}
|
||||
|
||||
/// Contiguous native-channel sample variant. `sample` must have the same rectangle and
|
||||
/// channel layout as `work`. The live compositor supplies it directly, avoiding a tiled
|
||||
/// temporary surface and a heap allocation for every sampled/deposited pixel.
|
||||
/// Contiguous native-channel sample variant. `sample` must have the channel layout of `work` and
|
||||
/// cover at least `fp.rect` clipped to `work.rect` (the only pixels read). The live compositor
|
||||
/// supplies it directly, avoiding a tiled temporary surface and a heap allocation for every
|
||||
/// sampled/deposited pixel.
|
||||
pub fn apply_mixer_dab_region(
|
||||
work: &mut crate::retouch::Region,
|
||||
fp: &crate::retouch::Footprint,
|
||||
|
||||
@@ -205,3 +205,41 @@ fn dry_and_wet_transitions_match_frozen_kernel() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A sample surface is read only under the dab: dabs that overhang the working rectangle or sit in
|
||||
/// a corner of a much larger one still match the frozen kernel.
|
||||
#[test]
|
||||
fn sampled_dabs_clipped_by_the_working_rectangle_match_frozen_kernel() {
|
||||
use crate::retouch::Region;
|
||||
use crate::{BrushContext, BrushSettings, Dab};
|
||||
use photocraft_color::{PixelFormat, SampleType};
|
||||
use photocraft_geom::Point;
|
||||
|
||||
let work_rect = Rect::new(10, 10, 60, 50);
|
||||
let fmt = PixelFormat::new(photocraft_color::ColorMode::Cmyk, SampleType::U16, true);
|
||||
let mut surface = Surface::new(fmt);
|
||||
surface.fill_rect(Rect::new(0, 0, 80, 80), &from_rgba(&fmt, [0.25, 0.5, 0.75, 0.9]));
|
||||
let mut source = Surface::new(PixelFormat::RGBA32F);
|
||||
for y in 0..80 {
|
||||
for x in 0..80 {
|
||||
source.fill_rect(Rect::new(x, y, x + 1, y + 1), &[x as f32 / 80.0, y as f32 / 80.0, 0.3, 0.8]);
|
||||
}
|
||||
}
|
||||
let ctx = BrushContext::new(&BrushSettings { size: 30.0, ..Default::default() });
|
||||
for sample in [&source, &surface] {
|
||||
let mut actual = Region::read(&surface, work_rect);
|
||||
let mut expected = actual.clone();
|
||||
let mut state = MixerState::default();
|
||||
state.load([0.8, 0.2, 0.1, 1.0]);
|
||||
let mut expected_state = state.clone();
|
||||
// Overhanging the left edge, a corner, and the middle.
|
||||
for (i, (x, y)) in [(8.0, 30.0), (62.0, 52.0), (30.0, 12.0), (35.0, 30.0)].into_iter().enumerate() {
|
||||
let fp = ctx.footprint(&Dab::round(Point::new(x, y), 12.0, 1.0), i);
|
||||
let settings = MixerSettings { wet: 0.8, mix: 0.7, ..Default::default() };
|
||||
reference(&mut expected, &fp, fmt, Some(sample), &settings, &mut expected_state, false);
|
||||
apply_mixer_dab(&mut actual, &fp, fmt, Some(sample), &settings, &mut state, false);
|
||||
assert_eq!(actual, expected, "dab {i}");
|
||||
assert_eq!(state, expected_state, "state after dab {i}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -263,9 +263,7 @@ impl BrushContext {
|
||||
let (fx, fy) = ((x as f32 + 0.5) / s - 0.5, (y as f32 + 0.5) / s - 0.5);
|
||||
let (x0, y0) = (fx.floor(), fy.floor());
|
||||
let (tx, ty) = (fx - x0, fy - y0);
|
||||
let a = p.sample_wrap(x0, y0) + (p.sample_wrap(x0 + 1.0, y0) - p.sample_wrap(x0, y0)) * tx;
|
||||
let b = p.sample_wrap(x0, y0 + 1.0) + (p.sample_wrap(x0 + 1.0, y0 + 1.0) - p.sample_wrap(x0, y0 + 1.0)) * tx;
|
||||
a + (b - a) * ty
|
||||
p.sample_bilinear_wrap(x0, y0, tx, ty)
|
||||
}
|
||||
|
||||
/// Bounding rectangle of a dab (rotated sampled tips need the corner reach).
|
||||
|
||||
@@ -188,3 +188,26 @@ fn sparse_large_footprint_only_allocates_and_dirties_nonzero_tiles() {
|
||||
map.accumulate(Rect::new(0, 0, 0, 0), &[], 0.8, None, None);
|
||||
assert_eq!(map.bounds(), bounds);
|
||||
}
|
||||
|
||||
/// `texture_at` at a scale other than 100 % is the bilinear value of the four wrapped texels around
|
||||
/// the sample: x and y are not swapped, and a different fraction on each axis is honoured.
|
||||
#[test]
|
||||
fn texture_at_scaled_is_the_bilinear_of_the_wrapped_texels() {
|
||||
let (w, h) = (4usize, 3usize);
|
||||
let data: Vec<f32> = (0..w * h).map(|i| (i * i) as f32 / 100.0).collect();
|
||||
let mut brush = BrushSettings::default();
|
||||
brush.texture.scale = 4.0;
|
||||
let mut ctx = BrushContext::new(&brush);
|
||||
ctx.texture = Some(Arc::new(PatternImage { width: w, height: h, data: data.clone() }));
|
||||
let texel = |x: i64, y: i64| data[y.rem_euclid(h as i64) as usize * w + x.rem_euclid(w as i64) as usize];
|
||||
for (x, y) in [(5, 2), (2, 5), (-3, 7), (0, 0), (-9, -4)] {
|
||||
let (fx, fy) = ((x as f32 + 0.5) / 4.0 - 0.5, (y as f32 + 0.5) / 4.0 - 0.5);
|
||||
let (x0, y0) = (fx.floor() as i64, fy.floor() as i64);
|
||||
let (tx, ty) = (fx - fx.floor(), fy - fy.floor());
|
||||
let top = texel(x0, y0) + (texel(x0 + 1, y0) - texel(x0, y0)) * tx;
|
||||
let bottom = texel(x0, y0 + 1) + (texel(x0 + 1, y0 + 1) - texel(x0, y0 + 1)) * tx;
|
||||
let want = top + (bottom - top) * ty;
|
||||
let got = ctx.texture_at(x, y);
|
||||
assert!((got - want).abs() < 1e-5, "({x}, {y}): {got} vs {want}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,12 +303,92 @@ impl PatternImage {
|
||||
let yi = (y.floor() as i64).rem_euclid(self.height as i64) as usize;
|
||||
self.data[yi * self.width + xi]
|
||||
}
|
||||
|
||||
/// Bilinear sample of the four texels at (`x0`, `y0`) and their right/lower neighbours, all
|
||||
/// wrapped; `x0`, `y0` are already floored. Same values as four [`sample_wrap`](Self::sample_wrap)
|
||||
/// calls, with the wrap done once per axis instead of once per texel.
|
||||
#[inline]
|
||||
pub fn sample_bilinear_wrap(&self, x0: f32, y0: f32, tx: f32, ty: f32) -> f32 {
|
||||
// Past 2^24 `x0 + 1.0` rounds back to `x0`, so the neighbour is not `x0 + 1` any more.
|
||||
const EXACT: f32 = 16_777_216.0;
|
||||
// `rem_euclid` below is on `i32`, so a side past `i32::MAX` takes the `i64` path, like the coordinates past 2^24.
|
||||
if !(x0.abs() < EXACT && y0.abs() < EXACT) || self.width.max(self.height) > i32::MAX as usize {
|
||||
let a = self.sample_wrap(x0, y0) + (self.sample_wrap(x0 + 1.0, y0) - self.sample_wrap(x0, y0)) * tx;
|
||||
let b = self.sample_wrap(x0, y0 + 1.0) + (self.sample_wrap(x0 + 1.0, y0 + 1.0) - self.sample_wrap(x0, y0 + 1.0)) * tx;
|
||||
return a + (b - a) * ty;
|
||||
}
|
||||
let (w, h) = (self.width, self.height);
|
||||
let xa = (x0 as i32).rem_euclid(w as i32) as usize;
|
||||
let ya = (y0 as i32).rem_euclid(h as i32) as usize;
|
||||
let xb = if xa + 1 == w { 0 } else { xa + 1 };
|
||||
let yb = if ya + 1 == h { 0 } else { ya + 1 };
|
||||
let (ra, rb) = (ya * w, yb * w);
|
||||
let (p00, p10, p01, p11) = (self.data[ra + xa], self.data[ra + xb], self.data[rb + xa], self.data[rb + xb]);
|
||||
let a = p00 + (p10 - p00) * tx;
|
||||
let b = p01 + (p11 - p01) * tx;
|
||||
a + (b - a) * ty
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn old_bilinear(p: &PatternImage, x0: f32, y0: f32, tx: f32, ty: f32) -> f32 {
|
||||
let a = p.sample_wrap(x0, y0) + (p.sample_wrap(x0 + 1.0, y0) - p.sample_wrap(x0, y0)) * tx;
|
||||
let b = p.sample_wrap(x0, y0 + 1.0) + (p.sample_wrap(x0 + 1.0, y0 + 1.0) - p.sample_wrap(x0, y0 + 1.0)) * tx;
|
||||
a + (b - a) * ty
|
||||
}
|
||||
|
||||
/// The wrapped bilinear sampler returns the same bits as four `sample_wrap` calls: odd and
|
||||
/// non-square sizes, negative and huge coordinates, the wrap seam, NaN.
|
||||
#[test]
|
||||
fn bilinear_wrap_is_bit_identical_to_four_sample_wraps() {
|
||||
for (w, h) in [(1usize, 1usize), (1, 7), (5, 1), (7, 5), (128, 128), (1024, 3)] {
|
||||
let data = (0..w * h).map(|i| ((i as u32).wrapping_mul(2_654_435_761) >> 8) as f32 / 16_777_216.0).collect();
|
||||
let p = PatternImage { width: w, height: h, data };
|
||||
let mut coords =
|
||||
vec![0.0f32, -1.0, -(w as f32), w as f32 - 1.0, w as f32, 16_777_215.0, 16_777_216.0, -16_777_216.0, 3.0e9, -3.0e9, f32::NAN, f32::INFINITY];
|
||||
coords.extend((-300..300).map(|i| i as f32 * 1.0));
|
||||
for &x0 in &coords {
|
||||
for &y0 in coords.iter().filter(|y| !y.is_finite() || y.abs() > 20.0 || **y % 7.0 == 0.0) {
|
||||
for (tx, ty) in [(0.0, 0.0), (0.25, 0.75), (0.999, 0.5)] {
|
||||
let (new, old) = (p.sample_bilinear_wrap(x0, y0, tx, ty), old_bilinear(&p, x0, y0, tx, ty));
|
||||
assert_eq!(new.to_bits(), old.to_bits(), "{w}x{h} at ({x0}, {y0})");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Manual timing: `cargo test -p photocraft-paint --release bilinear_wrap_timing -- --ignored --nocapture`.
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn bilinear_wrap_timing() {
|
||||
let p = PatternImage { width: 128, height: 128, data: (0..128 * 128).map(|i| (i % 251) as f32 / 251.0).collect() };
|
||||
let n = 4_000_000;
|
||||
for round in 0..3 {
|
||||
let t = std::time::Instant::now();
|
||||
let mut s = 0.0f32;
|
||||
for i in 0..n {
|
||||
let (fx, fy) = ((i % 2000) as f32 * 1.25 - 40.5, (i / 2000) as f32 * 1.25 - 40.5);
|
||||
s += old_bilinear(&p, fx.floor(), fy.floor(), fx.fract(), fy.fract());
|
||||
}
|
||||
let old = t.elapsed();
|
||||
let t = std::time::Instant::now();
|
||||
let mut s2 = 0.0f32;
|
||||
for i in 0..n {
|
||||
let (fx, fy) = ((i % 2000) as f32 * 1.25 - 40.5, (i / 2000) as f32 * 1.25 - 40.5);
|
||||
s2 += p.sample_bilinear_wrap(fx.floor(), fy.floor(), fx.fract(), fy.fract());
|
||||
}
|
||||
println!(
|
||||
"round {round}: old {:.2} ns/px, new {:.2} ns/px ({s} {s2})",
|
||||
old.as_secs_f64() * 1e9 / n as f64,
|
||||
t.elapsed().as_secs_f64() * 1e9 / n as f64
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Cloning a tile (and the brush settings holding it, on every pointer event of a stroke)
|
||||
/// shares its samples instead of copying them (#4534).
|
||||
#[test]
|
||||
|
||||
+28
-1
@@ -78,7 +78,14 @@ LayerInfo::unpadded_len(version), LayerInfo::pad_to(version, 4) // Photoshop p
|
||||
// Image resources
|
||||
ImageResource { signature, id, name, data }; ImageResource::parsed() -> Option<Result<ResourceData>>
|
||||
ResourceData::{ResolutionInfo, LayerState, LayerGroupInfo, Thumbnail, GlobalAngle, IccProfile,
|
||||
GlobalAltitude, VersionInfo, Exif, Xmp}
|
||||
GlobalAltitude, VersionInfo, Exif, Xmp, TimelineInfo, Animation}
|
||||
|
||||
// Legacy animation syntax (no playback/document-model dependency)
|
||||
animation::AnimationResource::from_bytes / to_bytes // maniIRFR; ordered raw records
|
||||
animation::AnimationRecord::descriptor() // AnDs; version-16 descriptor
|
||||
animation::{frame_info, frame_sets, layer_frame_states} // borrowed sparse views, opaque ids
|
||||
animation::parse_descriptor // descriptor + up to 3 zero alignment bytes
|
||||
metadata::MetadataItem::animation_descriptor() // mlst / tmln; leaves other metadata alone
|
||||
|
||||
// Compression
|
||||
Compression::{Raw, Rle, Zip, ZipPrediction, Unknown(u16)}
|
||||
@@ -117,6 +124,7 @@ testgen::{all_cases, merged_only, layered, small, sample_descriptor, pattern_pla
|
||||
| luni lsct lsdk lyid lnsr clbl infx knko lspf lclr iOpa shmd | ✓ | ✓ | ✓ | `TaggedBlock::parsed()` |
|
||||
| Layer XMP (`shmd` › `cust` › `layerXMP`) | ✓ | ✓ | ✓ | `metadata::layer_xmp` / `take_layer_xmp` / `put_layer_xmp`, laid out as Photoshop 2026 writes it |
|
||||
| ActionDescriptor (all OSTypes) | ✓ | ✓ | ✓ | including references, `ObAr` (best effort), `UnFl`, `Pth `, version-16 wrapper |
|
||||
| Legacy frame animation syntax | ✓ | ✓ | partial | `mani` / `AnDs`, `mlst`, `tmln`, resource 1075; raw-preserving descriptor views, not playback or keyframe evaluation |
|
||||
| Layer tree (groups) | ✓ | ✓ | ✓ | open/closed folders plus bounding dividers; tolerant of malformed nesting |
|
||||
| RGBA8 extraction | ✓ | – | – | RGB / gray / CMYK layers; merged image also supports indexed / bitmap / duotone |
|
||||
| Builder | – | ✓ | – | RGB / gray / CMYK at 8/16 bit, masks, groups, blend modes, opacity, fill, visibility, clipping |
|
||||
@@ -140,6 +148,25 @@ allocations for padded channels return an error on failure.
|
||||
* Adjustment and fill layers: `curv`, `levl`, `hue2`, `SoCo`, `GdFl`, `PtFl`, …, plus `Patt`, `Txt2`, `FMsk`, 3D and video blocks.
|
||||
* Global layer mask info (raw, with accessors), thumbnails (raw), slices, guides, paths, and every other resource id.
|
||||
|
||||
## Legacy animation inspection
|
||||
|
||||
```sh
|
||||
cargo run -p photocraft-psd --example animation_inspect -- /path/to/input.psd
|
||||
cargo test -p photocraft-psd --test it animation::
|
||||
```
|
||||
|
||||
The inspection example accepts an explicit local PSD/PSB (up to 128 MiB), reports frame counts,
|
||||
stored delays and descriptor counts, and checks byte-exact raw round-trips without modifying the
|
||||
input. It does not print layer names, text or descriptor values. No personal file is bundled;
|
||||
committed animation tests construct synthetic payloads and PSD/PSB containers.
|
||||
|
||||
`mani` is detected by its payload signature within the plug-in resource range 4000–4999, not by
|
||||
assuming that resource 4003 always belongs to animation. Unknown records and unsupported descriptor
|
||||
versions stay raw. `FrIn` definitions and `FSts` playback order are separate; ids are opaque stored
|
||||
`long` values. Missing frame delays remain absent, and sparse `mlst` properties are not filled with
|
||||
guessed defaults. Timeline tracks, loops, inheritance and property evaluation are left to higher
|
||||
layers. This parser alone does **not** make PSD animations play in PhotoCraft.
|
||||
|
||||
## Testing
|
||||
|
||||
`cargo test -p photocraft-psd --all-features` runs the unit tests, integration tests and proptests:
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
//! Inspect a local PSD/PSB's animation structure without printing layer names, text, or metadata
|
||||
//! values. No asset is bundled: pass a path explicitly. Does not decode pixels or modify the file.
|
||||
//! `cargo run -p photocraft-psd --example animation_inspect -- /path/to/input.psd`
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn inspect() -> Result<(), Box<dyn std::error::Error>> {
|
||||
use std::collections::BTreeMap;
|
||||
use std::io::Read;
|
||||
|
||||
use photocraft_psd::animation::{frame_info, frame_sets, layer_frame_states};
|
||||
use photocraft_psd::metadata::parse_shmd;
|
||||
use photocraft_psd::{PsdFile, ResourceData};
|
||||
|
||||
let mut args = std::env::args_os().skip(1);
|
||||
let path = args.next().ok_or("usage: animation_inspect <input.psd|input.psb>")?;
|
||||
if args.next().is_some() {
|
||||
return Err("expected exactly one input path".into());
|
||||
}
|
||||
// Diagnostic tool, not the production large-file importer. Bound even a file growing while read.
|
||||
const MAX_BYTES: u64 = 128 * 1024 * 1024;
|
||||
let file = std::fs::File::open(path)?;
|
||||
if !file.metadata()?.is_file() || file.metadata()?.len() > MAX_BYTES {
|
||||
return Err("expected a regular PSD/PSB no larger than 128 MiB".into());
|
||||
}
|
||||
let mut bytes = Vec::new();
|
||||
file.take(MAX_BYTES + 1).read_to_end(&mut bytes)?;
|
||||
if bytes.len() as u64 > MAX_BYTES {
|
||||
return Err("input exceeds the diagnostic tool's 128 MiB limit".into());
|
||||
}
|
||||
let psd = PsdFile::from_bytes(&bytes)?;
|
||||
println!("canvas={}x{} depth={} layers={}", psd.header.width, psd.header.height, psd.header.depth, psd.layers().len());
|
||||
let mut animations = 0;
|
||||
for resource in &psd.resources {
|
||||
match resource.parsed() {
|
||||
Some(Ok(ResourceData::Animation(animation))) => {
|
||||
animations += 1;
|
||||
let back = animation.to_bytes()?;
|
||||
if back != resource.data {
|
||||
return Err("animation resource failed its byte-exact round trip".into());
|
||||
}
|
||||
println!("animation resource: {} records; byte-exact round trip", animation.records.len());
|
||||
for record in &animation.records {
|
||||
if let Some(d) = record.descriptor() {
|
||||
let d = d?;
|
||||
let frames = frame_info(&d.descriptor)?;
|
||||
let sets = frame_sets(&d.descriptor)?;
|
||||
let mut delays = BTreeMap::new();
|
||||
for frame in &frames {
|
||||
*delays.entry(frame.delay_centiseconds).or_insert(0usize) += 1;
|
||||
}
|
||||
println!(" AnDs: {} definitions, {} frame sets", frames.len(), sets.len());
|
||||
println!(" stored delays (centiseconds -> frame count): {delays:?}");
|
||||
for set in sets {
|
||||
let unresolved = set.frame_ids.iter().filter(|id| !frames.iter().any(|f| f.id == **id)).count();
|
||||
println!(" frame set: {} ordered references, {unresolved} unresolved", set.frame_ids.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(Ok(ResourceData::TimelineInfo(d))) => println!("document timeline: {} descriptor fields", d.descriptor.items.len()),
|
||||
Some(Err(e)) if resource.id == 1075 || (4000..=4999).contains(&resource.id) && resource.data.starts_with(b"mani") => return Err(e.into()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let (mut states, mut tracks) = (0usize, 0usize);
|
||||
for layer in psd.layers() {
|
||||
for block in &layer.blocks {
|
||||
if block.key != *b"shmd" {
|
||||
continue;
|
||||
}
|
||||
let items = parse_shmd(&block.data)?;
|
||||
for item in &items {
|
||||
if let Some(d) = item.animation_descriptor() {
|
||||
let d = d?;
|
||||
if item.key == *b"mlst" {
|
||||
layer_frame_states(&d.descriptor)?;
|
||||
states += 1;
|
||||
} else {
|
||||
tracks += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if photocraft_psd::metadata::write_shmd(&items) != block.data {
|
||||
return Err("layer metadata failed its byte-exact round trip".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
if psd.to_bytes()? != bytes {
|
||||
return Err("PSD/PSB failed its byte-exact round trip".into());
|
||||
}
|
||||
println!("animation resources={animations}; layer frame-state descriptors={states}; layer timeline descriptors={tracks}");
|
||||
println!("whole PSD/PSB: byte-exact round trip; input unchanged");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> std::process::ExitCode {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if let Err(e) = inspect() {
|
||||
eprintln!("animation inspection failed: {e}");
|
||||
return std::process::ExitCode::FAILURE;
|
||||
}
|
||||
std::process::ExitCode::SUCCESS
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
//! Legacy Photoshop frame-animation syntax, independent of playback or document semantics.
|
||||
//!
|
||||
//! Observed in a CS5-authored PSD: a plug-in image resource (id in 4000..=4999) starts with
|
||||
//! `maniIRFR`, a big-endian u32 byte length, then `8BIM` / key / u32 length / data records.
|
||||
//! `AnDs` holds a version-16 ActionDescriptor; other records (including `Roll`) stay opaque.
|
||||
//! Layer `shmd` items `mlst` and `tmln`, and image resource 1075, also hold versioned descriptors.
|
||||
//! These undocumented layouts were established by inspecting file structure, not proprietary code.
|
||||
//!
|
||||
//! Raw records remain the source of truth: typed accessors do not rewrite them, discard unknown
|
||||
//! fields, interpret sparse layer-state inheritance, or assume a frame id is its sequence index.
|
||||
//! This module does **not** import an animation into PhotoCraft or evaluate timeline tracks.
|
||||
|
||||
use crate::descriptor::{Descriptor, Value, VersionedDescriptor};
|
||||
use crate::error::{PsdError, Result};
|
||||
use crate::io::{Reader, WriteExt};
|
||||
|
||||
/// A record in a `mani` resource. Data includes any padding counted in its stored length.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct AnimationRecord<'a> {
|
||||
/// Record key (`AnDs`, `Roll`, or an unknown key).
|
||||
pub key: [u8; 4],
|
||||
/// Borrowed, unmodified payload.
|
||||
pub data: &'a [u8],
|
||||
}
|
||||
|
||||
impl AnimationRecord<'_> {
|
||||
/// Typed view of `AnDs`; `None` for other records, including `Roll`.
|
||||
pub fn descriptor(&self) -> Option<Result<VersionedDescriptor>> {
|
||||
(self.key == *b"AnDs").then(|| parse_descriptor(self.data))
|
||||
}
|
||||
}
|
||||
|
||||
/// The records of a legacy frame-animation plug-in resource, in stored order.
|
||||
///
|
||||
/// Unknown keys and duplicate records are preserved, not silently collapsed. The observed
|
||||
/// envelope has no alignment bytes outside record lengths. Unsupported envelopes return an error;
|
||||
/// callers should keep their original image resource unchanged in that case.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct AnimationResource<'a> {
|
||||
/// Ordered records, borrowing their payloads from the input.
|
||||
pub records: Vec<AnimationRecord<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> AnimationResource<'a> {
|
||||
/// Parses a complete `maniIRFR` payload, checking the outer and every inner length.
|
||||
/// Descriptor decoding is lazy, so unknown/future `AnDs` versions can still round-trip raw.
|
||||
pub fn from_bytes(data: &'a [u8]) -> Result<Self> {
|
||||
let mut r = Reader::new(data);
|
||||
signature(&mut r, *b"mani", "mani")?;
|
||||
signature(&mut r, *b"IRFR", "IRFR animation envelope")?;
|
||||
let len = r.u32()?;
|
||||
let mut body = r.sub(u64::from(len))?;
|
||||
if !r.is_empty() {
|
||||
return Err(PsdError::invalid("trailing bytes after animation envelope"));
|
||||
}
|
||||
let mut records = Vec::new();
|
||||
while !body.is_empty() {
|
||||
signature(&mut body, *b"8BIM", "8BIM animation record")?;
|
||||
let key = body.array()?;
|
||||
let len = body.u32()?;
|
||||
let data = body.bytes_u64(u64::from(len))?;
|
||||
records.push(AnimationRecord { key, data });
|
||||
}
|
||||
Ok(Self { records })
|
||||
}
|
||||
|
||||
/// Writes the raw records, preserving their payloads exactly, including unknown fields,
|
||||
/// unknown descriptor versions and noncanonical descriptor encodings.
|
||||
pub fn to_bytes(&self) -> Result<Vec<u8>> {
|
||||
// Check all lengths before constructing the output; no unchecked 32-bit length casts.
|
||||
let mut len = 0u32;
|
||||
for record in &self.records {
|
||||
let n = u32::try_from(record.data.len()).map_err(|_| PsdError::LimitExceeded("animation record exceeds u32 length"))?;
|
||||
len = len.checked_add(12).and_then(|v| v.checked_add(n)).ok_or(PsdError::LimitExceeded("animation envelope exceeds u32 length"))?;
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
out.put(b"maniIRFR");
|
||||
out.put_u32(len);
|
||||
for record in &self.records {
|
||||
out.put(b"8BIM");
|
||||
out.put(&record.key);
|
||||
out.put_len(record.data.len() as u64, false)?;
|
||||
out.put(record.data);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
|
||||
fn signature(r: &mut Reader<'_>, expected: [u8; 4], name: &'static str) -> Result<()> {
|
||||
let found = r.array()?;
|
||||
if found != expected {
|
||||
return Err(PsdError::InvalidSignature { expected: name, found });
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Parses a version-16 animation descriptor, allowing up to three zero alignment bytes.
|
||||
///
|
||||
/// Raw payloads should be retained separately for byte-exact writing. This accessor is strict
|
||||
/// about non-padding trailing bytes and does not guess how to read a future descriptor version.
|
||||
/// It is shared by `AnDs`, layer `mlst` / `tmln`, and document timeline resource 1075.
|
||||
pub fn parse_descriptor(data: &[u8]) -> Result<VersionedDescriptor> {
|
||||
let (value, consumed) = VersionedDescriptor::parse_prefix(data)?;
|
||||
let tail = data.get(consumed..).ok_or_else(|| PsdError::invalid("animation descriptor length"))?;
|
||||
if tail.len() > 3 || tail.iter().any(|&b| b != 0) {
|
||||
return Err(PsdError::invalid("trailing bytes after animation descriptor"));
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
/// A frame-definition view. Its id is an opaque signed `long`, not a frame index.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct FrameInfo<'a> {
|
||||
/// `FrID`, preserved without reinterpreting its bit pattern.
|
||||
pub id: i32,
|
||||
/// `FrDl` in centiseconds, or absent. No playback default or browser clamping is applied.
|
||||
pub delay_centiseconds: Option<u32>,
|
||||
/// All fields, including unknown ones, in their original order.
|
||||
pub descriptor: &'a Descriptor,
|
||||
}
|
||||
|
||||
/// Reads the `FrIn` list from an `AnDs` descriptor. A missing list is not an empty animation.
|
||||
/// Wrong types, missing frame ids and negative delays return errors. Duplicate ids are retained
|
||||
/// for the document importer to diagnose rather than being silently overwritten here.
|
||||
pub fn frame_info(descriptor: &Descriptor) -> Result<Vec<FrameInfo<'_>>> {
|
||||
let Some(Value::List(list)) = descriptor.get("FrIn") else {
|
||||
return Err(PsdError::invalid("animation FrIn must be a list"));
|
||||
};
|
||||
list.iter()
|
||||
.map(|v| {
|
||||
let Value::Descriptor(d) = v else { return Err(PsdError::invalid("animation frame must be a descriptor")) };
|
||||
let Some(Value::Integer(id)) = d.get("FrID") else { return Err(PsdError::invalid("animation frame FrID must be a long")) };
|
||||
let delay_centiseconds = match d.get("FrDl") {
|
||||
None => None,
|
||||
Some(Value::Integer(n)) => Some(u32::try_from(*n).map_err(|_| PsdError::invalid("negative animation frame delay"))?),
|
||||
_ => return Err(PsdError::invalid("animation frame FrDl must be a long")),
|
||||
};
|
||||
Ok(FrameInfo { id: *id, delay_centiseconds, descriptor: d })
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A stored frame set: explicit playback order is separate from the `FrIn` definitions.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct FrameSet<'a> {
|
||||
/// `FsID`, an opaque identifier.
|
||||
pub id: i32,
|
||||
/// `AFrm`, when present. Kept uninterpreted for the importer to validate.
|
||||
pub active_frame: Option<i32>,
|
||||
/// `FsFr` frame ids in stored order (including any duplicate or unresolved ids).
|
||||
pub frame_ids: Vec<i32>,
|
||||
/// Complete original descriptor, including fields not yet understood.
|
||||
pub descriptor: &'a Descriptor,
|
||||
}
|
||||
|
||||
/// One sparse layer-state entry from `mlst` / `LaSt`.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct LayerFrameState<'a> {
|
||||
/// `FrLs`: frame ids to which this entry refers. They are not frame indices.
|
||||
pub frame_ids: Vec<i32>,
|
||||
/// Complete sparse state, including visibility, blend options, offsets, masks, effects,
|
||||
/// text settings and unknown fields. Omitted properties are deliberately not defaulted.
|
||||
pub descriptor: &'a Descriptor,
|
||||
}
|
||||
|
||||
/// A layer's sparse animation states, without attempting to resolve their inheritance.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct LayerFrameStates<'a> {
|
||||
/// `LaID`: PSD layer identifier, not the layer's record index.
|
||||
pub layer_id: i32,
|
||||
/// `LaSt` entries in stored order.
|
||||
pub states: Vec<LayerFrameState<'a>>,
|
||||
}
|
||||
|
||||
/// Reads the identifiers and sparse entries of an `mlst` descriptor. This is a syntax view:
|
||||
/// missing properties stay missing, and duplicate/unresolved references remain available for
|
||||
/// the importer to validate. The original descriptor remains available to the caller.
|
||||
pub fn layer_frame_states(descriptor: &Descriptor) -> Result<LayerFrameStates<'_>> {
|
||||
let Some(Value::Integer(layer_id)) = descriptor.get("LaID") else {
|
||||
return Err(PsdError::invalid("animation layer LaID must be a long"));
|
||||
};
|
||||
let Some(Value::List(list)) = descriptor.get("LaSt") else {
|
||||
return Err(PsdError::invalid("animation layer LaSt must be a list"));
|
||||
};
|
||||
let states = list
|
||||
.iter()
|
||||
.map(|v| {
|
||||
let Value::Descriptor(d) = v else { return Err(PsdError::invalid("animation layer state must be a descriptor")) };
|
||||
let Some(Value::List(ids)) = d.get("FrLs") else { return Err(PsdError::invalid("animation layer FrLs must be a list")) };
|
||||
let frame_ids = ids
|
||||
.iter()
|
||||
.map(|v| match v {
|
||||
Value::Integer(n) => Ok(*n),
|
||||
_ => Err(PsdError::invalid("animation layer frame reference must be a long")),
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
Ok(LayerFrameState { frame_ids, descriptor: d })
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
Ok(LayerFrameStates { layer_id: *layer_id, states })
|
||||
}
|
||||
|
||||
/// Reads `FSts` without inferring order from frame ids or interpreting loop-related fields.
|
||||
pub fn frame_sets(descriptor: &Descriptor) -> Result<Vec<FrameSet<'_>>> {
|
||||
let Some(Value::List(list)) = descriptor.get("FSts") else {
|
||||
return Err(PsdError::invalid("animation FSts must be a list"));
|
||||
};
|
||||
list.iter()
|
||||
.map(|v| {
|
||||
let Value::Descriptor(d) = v else { return Err(PsdError::invalid("animation frame set must be a descriptor")) };
|
||||
let Some(Value::Integer(id)) = d.get("FsID") else { return Err(PsdError::invalid("animation frame set FsID must be a long")) };
|
||||
let active_frame = match d.get("AFrm") {
|
||||
None => None,
|
||||
Some(Value::Integer(n)) => Some(*n),
|
||||
_ => return Err(PsdError::invalid("animation frame set AFrm must be a long")),
|
||||
};
|
||||
let Some(Value::List(ids)) = d.get("FsFr") else { return Err(PsdError::invalid("animation frame set FsFr must be a list")) };
|
||||
let frame_ids = ids
|
||||
.iter()
|
||||
.map(|v| match v {
|
||||
Value::Integer(n) => Ok(*n),
|
||||
_ => Err(PsdError::invalid("animation frame reference must be a long")),
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
Ok(FrameSet { id: *id, active_frame, frame_ids, descriptor: d })
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -497,22 +497,35 @@ pub fn predict(buf: &mut [u8], layout: &PlaneLayout) -> Result<()> {
|
||||
}
|
||||
}
|
||||
16 => {
|
||||
for i in (1..w).rev() {
|
||||
let a = u16::from_be_bytes([row[2 * i], row[2 * i + 1]]);
|
||||
let b = u16::from_be_bytes([row[2 * i - 2], row[2 * i - 1]]);
|
||||
row[2 * i..2 * i + 2].copy_from_slice(&a.wrapping_sub(b).to_be_bytes());
|
||||
// One forward pass: the previous *original* sample stays in a
|
||||
// register, so nothing is re-read after being overwritten.
|
||||
let mut prev = 0u16;
|
||||
for s in row.as_chunks_mut::<2>().0 {
|
||||
let cur = u16::from_be_bytes(*s);
|
||||
*s = cur.wrapping_sub(prev).to_be_bytes();
|
||||
prev = cur;
|
||||
}
|
||||
}
|
||||
32 => {
|
||||
scratch.clear();
|
||||
scratch.resize(rb, 0);
|
||||
for i in 0..w {
|
||||
for k in 0..4 {
|
||||
scratch[k * w + i] = row[4 * i + k];
|
||||
}
|
||||
// Split into four byte planes (MSB first) and delta the whole
|
||||
// plane-major row. Plane k's first delta uses the last byte of
|
||||
// plane k - 1, so seed each plane's "previous" with it.
|
||||
let mut prev = [0u8; 4];
|
||||
if let Some(l) = row.as_chunks::<4>().0.last() {
|
||||
prev = [0, l[0], l[1], l[2]];
|
||||
}
|
||||
for i in (1..rb).rev() {
|
||||
scratch[i] = scratch[i].wrapping_sub(scratch[i - 1]);
|
||||
let (p0, rest) = scratch.split_at_mut(w);
|
||||
let (p1, rest) = rest.split_at_mut(w);
|
||||
let (p2, p3) = rest.split_at_mut(w);
|
||||
let planes = p0.iter_mut().zip(p1.iter_mut()).zip(p2.iter_mut().zip(p3.iter_mut()));
|
||||
for (s, ((o0, o1), (o2, o3))) in row.as_chunks::<4>().0.iter().zip(planes) {
|
||||
*o0 = s[0].wrapping_sub(prev[0]);
|
||||
*o1 = s[1].wrapping_sub(prev[1]);
|
||||
*o2 = s[2].wrapping_sub(prev[2]);
|
||||
*o3 = s[3].wrapping_sub(prev[3]);
|
||||
prev = [s[0], s[1], s[2], s[3]];
|
||||
}
|
||||
row.copy_from_slice(&scratch);
|
||||
}
|
||||
@@ -538,22 +551,33 @@ pub fn unpredict(buf: &mut [u8], layout: &PlaneLayout) -> Result<()> {
|
||||
}
|
||||
}
|
||||
16 => {
|
||||
for i in 1..w {
|
||||
let a = u16::from_be_bytes([row[2 * i], row[2 * i + 1]]);
|
||||
let b = u16::from_be_bytes([row[2 * i - 2], row[2 * i - 1]]);
|
||||
row[2 * i..2 * i + 2].copy_from_slice(&a.wrapping_add(b).to_be_bytes());
|
||||
// Running sum carried in a register; one pass over the row.
|
||||
let mut acc = 0u16;
|
||||
for s in row.as_chunks_mut::<2>().0 {
|
||||
acc = acc.wrapping_add(u16::from_be_bytes(*s));
|
||||
*s = acc.to_be_bytes();
|
||||
}
|
||||
}
|
||||
32 => {
|
||||
for i in 1..rb {
|
||||
row[i] = row[i].wrapping_add(row[i - 1]);
|
||||
}
|
||||
scratch.clear();
|
||||
scratch.extend_from_slice(row);
|
||||
for i in 0..w {
|
||||
for k in 0..4 {
|
||||
row[4 * i + k] = scratch[k * w + i];
|
||||
}
|
||||
// The byte-wise running sum runs across the four planes, so
|
||||
// plane k starts at the wrapping total of planes 0..k.
|
||||
let (p0, rest) = scratch.split_at(w);
|
||||
let (p1, rest) = rest.split_at(w);
|
||||
let (p2, p3) = rest.split_at(w);
|
||||
let sum = |p: &[u8]| p.iter().fold(0u8, |a, &b| a.wrapping_add(b));
|
||||
let b1 = sum(p0);
|
||||
let b2 = b1.wrapping_add(sum(p1));
|
||||
let b3 = b2.wrapping_add(sum(p2));
|
||||
let mut acc = [0u8, b1, b2, b3];
|
||||
let planes = p0.iter().zip(p1).zip(p2.iter().zip(p3));
|
||||
for (o, ((&s0, &s1), (&s2, &s3))) in row.as_chunks_mut::<4>().0.iter_mut().zip(planes) {
|
||||
acc[0] = acc[0].wrapping_add(s0);
|
||||
acc[1] = acc[1].wrapping_add(s1);
|
||||
acc[2] = acc[2].wrapping_add(s2);
|
||||
acc[3] = acc[3].wrapping_add(s3);
|
||||
*o = acc;
|
||||
}
|
||||
}
|
||||
d => return Err(PsdError::Unsupported(format!("prediction for depth {d}"))),
|
||||
|
||||
@@ -1,5 +1,117 @@
|
||||
use super::*;
|
||||
|
||||
/// The straightforward index-based versions, kept as the oracle for the
|
||||
/// register-carried rewrites in [`predict`] and [`unpredict`].
|
||||
mod reference {
|
||||
use super::super::*;
|
||||
|
||||
pub fn predict(buf: &mut [u8], layout: &PlaneLayout) {
|
||||
let rb = layout.row_bytes();
|
||||
let w = layout.width;
|
||||
let mut scratch = Vec::new();
|
||||
for row in buf.chunks_exact_mut(rb) {
|
||||
match layout.depth {
|
||||
8 => {
|
||||
for i in (1..row.len()).rev() {
|
||||
row[i] = row[i].wrapping_sub(row[i - 1]);
|
||||
}
|
||||
}
|
||||
16 => {
|
||||
for i in (1..w).rev() {
|
||||
let a = u16::from_be_bytes([row[2 * i], row[2 * i + 1]]);
|
||||
let b = u16::from_be_bytes([row[2 * i - 2], row[2 * i - 1]]);
|
||||
row[2 * i..2 * i + 2].copy_from_slice(&a.wrapping_sub(b).to_be_bytes());
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
scratch.clear();
|
||||
scratch.resize(rb, 0);
|
||||
for i in 0..w {
|
||||
for k in 0..4 {
|
||||
scratch[k * w + i] = row[4 * i + k];
|
||||
}
|
||||
}
|
||||
for i in (1..rb).rev() {
|
||||
scratch[i] = scratch[i].wrapping_sub(scratch[i - 1]);
|
||||
}
|
||||
row.copy_from_slice(&scratch);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unpredict(buf: &mut [u8], layout: &PlaneLayout) {
|
||||
let rb = layout.row_bytes();
|
||||
let w = layout.width;
|
||||
let mut scratch = Vec::new();
|
||||
for row in buf.chunks_exact_mut(rb) {
|
||||
match layout.depth {
|
||||
8 => {
|
||||
for i in 1..row.len() {
|
||||
row[i] = row[i].wrapping_add(row[i - 1]);
|
||||
}
|
||||
}
|
||||
16 => {
|
||||
for i in 1..w {
|
||||
let a = u16::from_be_bytes([row[2 * i], row[2 * i + 1]]);
|
||||
let b = u16::from_be_bytes([row[2 * i - 2], row[2 * i - 1]]);
|
||||
row[2 * i..2 * i + 2].copy_from_slice(&a.wrapping_add(b).to_be_bytes());
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
for i in 1..rb {
|
||||
row[i] = row[i].wrapping_add(row[i - 1]);
|
||||
}
|
||||
scratch.clear();
|
||||
scratch.extend_from_slice(row);
|
||||
for i in 0..w {
|
||||
for k in 0..4 {
|
||||
row[4 * i + k] = scratch[k * w + i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prediction_matches_reference_on_random_planes() {
|
||||
let mut state = 0x9E37_79B9_7F4A_7C15u64;
|
||||
let mut next = move || {
|
||||
state ^= state << 13;
|
||||
state ^= state >> 7;
|
||||
state ^= state << 17;
|
||||
state
|
||||
};
|
||||
for depth in [8u16, 16, 32] {
|
||||
for width in (1..=70).chain([255, 256, 257, 1000, 1023]) {
|
||||
for height in [1usize, 2, 3] {
|
||||
for fill in 0..4 {
|
||||
let layout = PlaneLayout { planes: 1, width, height, depth, version: Version::Psd };
|
||||
let len = layout.row_bytes() * height;
|
||||
let src: Vec<u8> = (0..len)
|
||||
.map(|_| match fill {
|
||||
0 => next() as u8,
|
||||
1 => 0xFF,
|
||||
2 => [0x00, 0xFF, 0x80, 0x7F][(next() & 3) as usize],
|
||||
_ => (next() as u8) | 0xF0,
|
||||
})
|
||||
.collect();
|
||||
let (mut want, mut got) = (src.clone(), src.clone());
|
||||
reference::predict(&mut want, &layout);
|
||||
predict(&mut got, &layout).unwrap();
|
||||
assert_eq!(got, want, "predict depth {depth} width {width} height {height} fill {fill}");
|
||||
let (mut want, mut got) = (src.clone(), src);
|
||||
reference::unpredict(&mut want, &layout);
|
||||
unpredict(&mut got, &layout).unwrap();
|
||||
assert_eq!(got, want, "unpredict depth {depth} width {width} height {height} fill {fill}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checked_zip_validates_trailer_size_and_padding_budget() {
|
||||
let layout = PlaneLayout { planes: 1, width: 3, height: 1, depth: 8, version: Version::Psd };
|
||||
|
||||
@@ -43,6 +43,7 @@
|
||||
pub mod abr;
|
||||
pub mod aco;
|
||||
pub mod act;
|
||||
pub mod animation;
|
||||
pub mod ase;
|
||||
pub mod asl;
|
||||
pub mod atn;
|
||||
|
||||
@@ -27,6 +27,18 @@ pub struct MetadataItem {
|
||||
}
|
||||
|
||||
impl MetadataItem {
|
||||
/// Typed animation descriptor for `mlst` (sparse layer frame states) or `tmln`
|
||||
/// (layer timeline tracks). Other metadata, including `cust` / XMP, is not interpreted.
|
||||
/// The original item stays unchanged; inheritance and track evaluation belong to the importer.
|
||||
pub fn animation_descriptor(&self) -> Option<Result<crate::VersionedDescriptor>> {
|
||||
matches!(&self.key, b"mlst" | b"tmln").then(|| {
|
||||
if self.signature != *b"8BIM" {
|
||||
return Err(PsdError::InvalidSignature { expected: "8BIM animation metadata", found: self.signature });
|
||||
}
|
||||
crate::animation::parse_descriptor(&self.data)
|
||||
})
|
||||
}
|
||||
|
||||
/// A new item; `data` is zero-padded to an even length as Photoshop writes it.
|
||||
pub fn new(key: [u8; 4], mut data: Vec<u8>) -> Self {
|
||||
if data.len() % 2 == 1 {
|
||||
|
||||
@@ -28,6 +28,8 @@ pub mod ids {
|
||||
pub const EXIF: u16 = 1058;
|
||||
/// XMP metadata.
|
||||
pub const XMP: u16 = 1060;
|
||||
/// Document timeline settings (versioned descriptor).
|
||||
pub const TIMELINE_INFO: u16 = 1075;
|
||||
}
|
||||
|
||||
/// One image resource block.
|
||||
@@ -169,6 +171,10 @@ pub enum ResourceData<'a> {
|
||||
Exif(&'a [u8]),
|
||||
/// 1060: XMP packet (UTF-8).
|
||||
Xmp(&'a [u8]),
|
||||
/// 1075: document timeline settings; tracks and rational times remain descriptor data.
|
||||
TimelineInfo(crate::VersionedDescriptor),
|
||||
/// A `mani` legacy animation resource in the plug-in resource id range (4000..=4999).
|
||||
Animation(crate::animation::AnimationResource<'a>),
|
||||
}
|
||||
|
||||
impl ImageResource {
|
||||
@@ -199,6 +205,8 @@ impl ImageResource {
|
||||
ids::VERSION_INFO => VersionInfo::from_bytes(d).map(ResourceData::VersionInfo),
|
||||
ids::EXIF => Ok(ResourceData::Exif(d)),
|
||||
ids::XMP => Ok(ResourceData::Xmp(d)),
|
||||
ids::TIMELINE_INFO => crate::animation::parse_descriptor(d).map(ResourceData::TimelineInfo),
|
||||
4000..=4999 if d.starts_with(b"mani") => crate::animation::AnimationResource::from_bytes(d).map(ResourceData::Animation),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
//! Synthetic only: no personal PSDs, paths, or copied descriptor payloads.
|
||||
|
||||
use photocraft_psd::animation::{AnimationRecord, AnimationResource, frame_info, frame_sets, layer_frame_states, parse_descriptor};
|
||||
use photocraft_psd::descriptor::{Descriptor, Value, VersionedDescriptor};
|
||||
use photocraft_psd::metadata::{MetadataItem, parse_shmd, write_shmd};
|
||||
use photocraft_psd::{ImageResource, PsdBuilder, PsdFile, ResourceData, TaggedBlock, Version};
|
||||
use proptest::prelude::*;
|
||||
|
||||
fn descriptor() -> VersionedDescriptor {
|
||||
VersionedDescriptor::new(
|
||||
Descriptor::default()
|
||||
.with("AFSt", Value::Integer(7))
|
||||
.with(
|
||||
"FrIn",
|
||||
Value::List(vec![
|
||||
Value::Descriptor(
|
||||
Descriptor::default().with("FrID", Value::Integer(-3)).with("FrDl", Value::Integer(3)).with("future", Value::Boolean(true)),
|
||||
),
|
||||
Value::Descriptor(Descriptor::default().with("FrID", Value::Integer(21)).with("FrDl", Value::Integer(53))),
|
||||
]),
|
||||
)
|
||||
.with(
|
||||
"FSts",
|
||||
Value::List(vec![Value::Descriptor(
|
||||
Descriptor::default()
|
||||
.with("FsID", Value::Integer(7))
|
||||
.with("AFrm", Value::Integer(0))
|
||||
.with("FsFr", Value::List(vec![Value::Integer(21), Value::Integer(-3)]))
|
||||
.with("future", Value::RawData(vec![1, 2, 3])),
|
||||
)]),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fn resource(data: &[u8]) -> AnimationResource<'_> {
|
||||
AnimationResource {
|
||||
records: vec![
|
||||
AnimationRecord { key: *b"AnDs", data },
|
||||
AnimationRecord { key: *b"Roll", data: &[0; 8] },
|
||||
AnimationRecord { key: *b"next", data: &[9, 8, 7] },
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_roundtrip_and_typed_views_preserve_order_and_unknown_data() {
|
||||
let data = descriptor().to_bytes();
|
||||
let bytes = resource(&data).to_bytes().unwrap();
|
||||
let parsed = AnimationResource::from_bytes(&bytes).unwrap();
|
||||
assert_eq!(parsed, resource(&data));
|
||||
let d = parsed.records[0].descriptor().unwrap().unwrap();
|
||||
assert_eq!(d, descriptor());
|
||||
let frames = frame_info(&d.descriptor).unwrap();
|
||||
assert_eq!(frames.iter().map(|f| (f.id, f.delay_centiseconds)).collect::<Vec<_>>(), [(-3, Some(3)), (21, Some(53))]);
|
||||
assert_eq!(frames[0].descriptor.get("future"), Some(&Value::Boolean(true)));
|
||||
let sets = frame_sets(&d.descriptor).unwrap();
|
||||
assert_eq!(sets[0].frame_ids, [21, -3], "playback order is not FrIn order or sorted id order");
|
||||
assert_eq!(sets[0].id, 7);
|
||||
assert_eq!(sets[0].active_frame, Some(0));
|
||||
assert_eq!(sets[0].descriptor.get("future"), Some(&Value::RawData(vec![1, 2, 3])));
|
||||
assert!(parsed.records[1].descriptor().is_none());
|
||||
assert_eq!(parsed.to_bytes().unwrap(), bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plugin_signature_not_a_single_resource_id_selects_the_parser() {
|
||||
let data = descriptor().to_bytes();
|
||||
let bytes = resource(&data).to_bytes().unwrap();
|
||||
for id in [4000, 4003, 4999] {
|
||||
let r = ImageResource::new(id, bytes.clone());
|
||||
assert!(matches!(r.parsed(), Some(Ok(ResourceData::Animation(_)))));
|
||||
}
|
||||
for id in [3999, 5000] {
|
||||
assert!(ImageResource::new(id, bytes.clone()).parsed().is_none());
|
||||
}
|
||||
assert!(ImageResource::new(4003, b"different plugin".to_vec()).parsed().is_none());
|
||||
assert!(matches!(ImageResource::new(4003, b"mani".to_vec()).parsed(), Some(Err(_))));
|
||||
assert!(matches!(
|
||||
ImageResource::new(1075, VersionedDescriptor::new(Descriptor::default().with("Vrsn", Value::Integer(1))).to_bytes()).parsed(),
|
||||
Some(Ok(ResourceData::TimelineInfo(_)))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metadata_views_do_not_touch_xmp_or_other_items() {
|
||||
let d = VersionedDescriptor::new(Descriptor::default().with("LaID", Value::Integer(42)).with(
|
||||
"LaSt",
|
||||
Value::List(vec![Value::Descriptor(
|
||||
Descriptor::default()
|
||||
.with("FrLs", Value::List(vec![Value::Integer(-3)]))
|
||||
.with("blendOptions", Value::Descriptor(Descriptor::default().with("Opct", Value::Double(12.5)))),
|
||||
)]),
|
||||
));
|
||||
let items = vec![
|
||||
MetadataItem::new(*b"cust", vec![7, 9, 11]),
|
||||
MetadataItem::new(*b"mlst", d.to_bytes()),
|
||||
MetadataItem::new(*b"tmln", VersionedDescriptor::new(Descriptor::default().with("trackList", Value::List(vec![]))).to_bytes()),
|
||||
MetadataItem::new(*b"next", vec![255; 4]),
|
||||
];
|
||||
let raw = write_shmd(&items);
|
||||
let parsed = parse_shmd(&raw).unwrap();
|
||||
assert!(parsed[0].animation_descriptor().is_none());
|
||||
let layer_descriptor = parsed[1].animation_descriptor().unwrap().unwrap();
|
||||
assert_eq!(layer_descriptor, d);
|
||||
let states = layer_frame_states(&layer_descriptor.descriptor).unwrap();
|
||||
assert_eq!(states.layer_id, 42);
|
||||
assert_eq!(states.states[0].frame_ids, [-3]);
|
||||
assert_eq!(states.states[0].descriptor.get("enab"), None, "missing visibility is not false");
|
||||
assert_eq!(states.states[0].descriptor.get("blendOptions"), Some(&Value::Descriptor(Descriptor::default().with("Opct", Value::Double(12.5)))));
|
||||
assert!(parsed[2].animation_descriptor().unwrap().is_ok());
|
||||
assert!(parsed[3].animation_descriptor().is_none());
|
||||
assert_eq!(write_shmd(&parsed), raw);
|
||||
let mut bad = parsed[1].clone();
|
||||
bad.signature = *b"nope";
|
||||
assert!(bad.animation_descriptor().unwrap().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn psd_and_psb_keep_animation_resources_and_metadata_byte_stable() {
|
||||
let data = descriptor().to_bytes();
|
||||
let raw = resource(&data).to_bytes().unwrap();
|
||||
for version in [Version::Psd, Version::Psb] {
|
||||
let mut file = PsdBuilder::new(1, 1).version(version).build().unwrap();
|
||||
file.resources.push(ImageResource::new(4003, raw.clone()));
|
||||
file.global_blocks.push(TaggedBlock::new(*b"shmd", write_shmd(&[MetadataItem::new(*b"mlst", data.clone())])));
|
||||
let bytes = file.to_bytes().unwrap();
|
||||
let back = PsdFile::from_bytes(&bytes).unwrap();
|
||||
assert_eq!(back.to_bytes().unwrap(), bytes);
|
||||
let animation = back.resource(4003).unwrap();
|
||||
assert!(matches!(animation.parsed(), Some(Ok(ResourceData::Animation(_)))));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_truncation_and_hostile_envelope_length_is_rejected() {
|
||||
let data = descriptor().to_bytes();
|
||||
let bytes = resource(&data).to_bytes().unwrap();
|
||||
for end in 0..bytes.len() {
|
||||
assert!(AnimationResource::from_bytes(&bytes[..end]).is_err(), "prefix {end}");
|
||||
}
|
||||
for at in [8, 20] {
|
||||
for value in [0, 1, u32::MAX] {
|
||||
let mut bad = bytes.clone();
|
||||
bad[at..at + 4].copy_from_slice(&value.to_be_bytes());
|
||||
assert!(AnimationResource::from_bytes(&bad).is_err(), "length {at} = {value}");
|
||||
}
|
||||
}
|
||||
for at in [0, 4, 12] {
|
||||
let mut bad = bytes.clone();
|
||||
bad[at] = b'?';
|
||||
assert!(AnimationResource::from_bytes(&bad).is_err());
|
||||
}
|
||||
let mut trailing = bytes.clone();
|
||||
trailing.push(0);
|
||||
assert!(AnimationResource::from_bytes(&trailing).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn descriptor_padding_truncation_and_unknown_versions() {
|
||||
let raw = descriptor().to_bytes();
|
||||
for end in 0..raw.len() {
|
||||
assert!(parse_descriptor(&raw[..end]).is_err(), "descriptor prefix {end}");
|
||||
}
|
||||
for padding in 0..=3 {
|
||||
let mut padded = raw.clone();
|
||||
padded.extend(vec![0; padding]);
|
||||
assert_eq!(parse_descriptor(&padded).unwrap(), descriptor());
|
||||
}
|
||||
for tail in [vec![0; 4], vec![1], vec![0, 2]] {
|
||||
let mut bad = raw.clone();
|
||||
bad.extend(tail);
|
||||
assert!(parse_descriptor(&bad).is_err());
|
||||
}
|
||||
let mut future = raw.clone();
|
||||
future[..4].copy_from_slice(&17u32.to_be_bytes());
|
||||
let bytes = resource(&future).to_bytes().unwrap();
|
||||
let r = AnimationResource::from_bytes(&bytes).unwrap();
|
||||
assert!(r.records[0].descriptor().unwrap().is_err());
|
||||
assert_eq!(r.to_bytes().unwrap(), bytes, "unsupported typed version must survive raw");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn absent_delays_and_duplicate_ids_are_not_silently_repaired() {
|
||||
let d = Descriptor::default().with(
|
||||
"FrIn",
|
||||
Value::List(vec![
|
||||
Value::Descriptor(Descriptor::default().with("FrID", Value::Integer(4))),
|
||||
Value::Descriptor(Descriptor::default().with("FrID", Value::Integer(4)).with("FrDl", Value::Integer(0))),
|
||||
]),
|
||||
);
|
||||
let frames = frame_info(&d).unwrap();
|
||||
assert_eq!(frames.len(), 2);
|
||||
assert_eq!(frames[0].delay_centiseconds, None);
|
||||
assert_eq!(frames[1].delay_centiseconds, Some(0));
|
||||
let bytes =
|
||||
AnimationResource { records: vec![AnimationRecord { key: *b"next", data: &[] }, AnimationRecord { key: *b"next", data: &[1] }] }.to_bytes().unwrap();
|
||||
assert_eq!(AnimationResource::from_bytes(&bytes).unwrap().to_bytes().unwrap(), bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_frame_and_set_types_return_errors() {
|
||||
for value in [Value::Boolean(false), Value::List(vec![Value::Integer(1)]), Value::List(vec![Value::Descriptor(Descriptor::default())])] {
|
||||
assert!(frame_info(&Descriptor::default().with("FrIn", value.clone())).is_err());
|
||||
assert!(frame_sets(&Descriptor::default().with("FSts", value)).is_err());
|
||||
}
|
||||
for delay in [Value::Integer(-1), Value::Double(3.0)] {
|
||||
let frame = Descriptor::default().with("FrID", Value::Integer(1)).with("FrDl", delay);
|
||||
assert!(frame_info(&Descriptor::default().with("FrIn", Value::List(vec![Value::Descriptor(frame)]))).is_err());
|
||||
}
|
||||
for (key, value) in [("FsID", Value::Boolean(false)), ("AFrm", Value::Double(0.0)), ("FsFr", Value::List(vec![Value::Boolean(true)]))] {
|
||||
let mut set = Descriptor::default().with("FsID", Value::Integer(1)).with("FsFr", Value::List(vec![]));
|
||||
set.items.retain(|(id, _)| !id.is(key));
|
||||
set = set.with(key, value);
|
||||
assert!(frame_sets(&Descriptor::default().with("FSts", Value::List(vec![Value::Descriptor(set)]))).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sparse_layer_states_retain_missing_properties_and_multiple_references() {
|
||||
let first = Descriptor::default()
|
||||
.with("FrLs", Value::List(vec![Value::Integer(21), Value::Integer(-3)]))
|
||||
.with("enab", Value::Boolean(true))
|
||||
.with("Ofst", Value::Descriptor(Descriptor::default().with("Hrzn", Value::Integer(-7)).with("Vrtc", Value::Integer(12))));
|
||||
let second = Descriptor::default().with("FrLs", Value::List(vec![Value::Integer(99)]));
|
||||
let d = Descriptor::default().with("LaID", Value::Integer(8)).with("LaSt", Value::List(vec![Value::Descriptor(first.clone()), Value::Descriptor(second)]));
|
||||
let view = layer_frame_states(&d).unwrap();
|
||||
assert_eq!(view.states[0].frame_ids, [21, -3]);
|
||||
assert_eq!(view.states[0].descriptor, &first);
|
||||
assert_eq!(view.states[1].descriptor.get("Ofst"), None);
|
||||
assert_eq!(view.states[1].descriptor.get("enab"), None);
|
||||
for d in [
|
||||
Descriptor::default(),
|
||||
Descriptor::default().with("LaID", Value::Boolean(true)),
|
||||
Descriptor::default().with("LaID", Value::Integer(8)).with("LaSt", Value::Boolean(true)),
|
||||
Descriptor::default().with("LaID", Value::Integer(8)).with("LaSt", Value::List(vec![Value::Integer(0)])),
|
||||
] {
|
||||
assert!(layer_frame_states(&d).is_err());
|
||||
}
|
||||
for state in [
|
||||
Descriptor::default(),
|
||||
Descriptor::default().with("FrLs", Value::Integer(0)),
|
||||
Descriptor::default().with("FrLs", Value::List(vec![Value::Boolean(true)])),
|
||||
] {
|
||||
let d = Descriptor::default().with("LaID", Value::Integer(8)).with("LaSt", Value::List(vec![Value::Descriptor(state)]));
|
||||
assert!(layer_frame_states(&d).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn arbitrary_animation_bytes_never_panic(data in proptest::collection::vec(any::<u8>(), 0..2048)) {
|
||||
let _ = AnimationResource::from_bytes(&data);
|
||||
let _ = parse_descriptor(&data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opaque_record_roundtrip(data in proptest::collection::vec(any::<u8>(), 0..2048), key in any::<[u8; 4]>()) {
|
||||
let r = AnimationResource { records: vec![AnimationRecord { key, data: &data }] };
|
||||
let bytes = r.to_bytes().unwrap();
|
||||
prop_assert_eq!(AnimationResource::from_bytes(&bytes).unwrap(), r);
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,64 @@ fn collect(dir: &Path, out: &mut Vec<PathBuf>) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The existing pinned ag-psd corpus includes public animation cases. Exercise typed syntax
|
||||
/// against those without adding personal files or new corpus pins.
|
||||
#[test]
|
||||
fn corpus_animation_syntax() {
|
||||
use photocraft_psd::ResourceData;
|
||||
use photocraft_psd::animation::{frame_info, frame_sets, layer_frame_states};
|
||||
use photocraft_psd::metadata::parse_shmd;
|
||||
|
||||
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/psd/ag-psd/read-write");
|
||||
for (case, delay) in [("animation-frame", Some(30)), ("animation-offset", Some(50)), ("animation-effects", None)] {
|
||||
for name in ["src.psd", "expected.psd"] {
|
||||
let path = root.join(case).join(name);
|
||||
let bytes = std::fs::read(&path).unwrap_or_else(|e| panic!("{}: {e}; run cargo xtask corpus --psd", path.display()));
|
||||
let psd = PsdFile::from_bytes(&bytes).unwrap();
|
||||
let mut found = 0;
|
||||
let mut decoded_descriptors = 0;
|
||||
for resource in &psd.resources {
|
||||
if let Some(Ok(ResourceData::Animation(animation))) = resource.parsed() {
|
||||
found += 1;
|
||||
assert_eq!(animation.to_bytes().unwrap(), resource.data);
|
||||
for record in &animation.records {
|
||||
if let Some(d) = record.descriptor() {
|
||||
let d = d.unwrap();
|
||||
decoded_descriptors += 1;
|
||||
let frames = frame_info(&d.descriptor).unwrap();
|
||||
assert_eq!(frames.len(), 2, "{case}/{name}");
|
||||
assert!(frames.iter().all(|f| f.delay_centiseconds == delay), "{case}/{name}");
|
||||
let sets = frame_sets(&d.descriptor).unwrap();
|
||||
assert_eq!(sets.len(), 1);
|
||||
assert_eq!(sets[0].frame_ids.len(), 2);
|
||||
assert!(sets[0].frame_ids.iter().all(|id| frames.iter().any(|f| f.id == *id)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(found, 1, "{case}/{name}");
|
||||
assert_eq!(decoded_descriptors, 1, "expected one decoded AnDs descriptor in {case}/{name}");
|
||||
let mut state_layers = 0;
|
||||
for layer in psd.layers() {
|
||||
for block in &layer.blocks {
|
||||
if block.key == *b"shmd" {
|
||||
for item in parse_shmd(&block.data).unwrap() {
|
||||
if item.key == *b"mlst" {
|
||||
let d = item.animation_descriptor().unwrap().unwrap();
|
||||
let states = layer_frame_states(&d.descriptor).unwrap();
|
||||
assert!(!states.states.is_empty());
|
||||
state_layers += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(state_layers, 2, "{case}/{name}");
|
||||
assert_eq!(psd.to_bytes().unwrap(), bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Files known to be invalid upstream, with the reason. They are reported but
|
||||
/// do not fail the test.
|
||||
const KNOWN_BAD: &[(&str, &str)] =
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! The crate's integration tests, linked into one test binary.
|
||||
|
||||
mod animation;
|
||||
mod atn_contract;
|
||||
mod atn_v12;
|
||||
mod builder;
|
||||
|
||||
@@ -206,6 +206,16 @@ pub(super) mod linux {
|
||||
MembershipParse::Malformed => return CgroupLimits::Unreadable,
|
||||
};
|
||||
let mountinfo = std::fs::read_to_string(proc_root.join("self/mountinfo")).ok();
|
||||
// A sandbox with no cgroup filesystem mounted and no `/sys/fs/cgroup` at all (Flatpak)
|
||||
// shows the process its membership path but no interface files, so no limit is
|
||||
// observable. Failing closed there reported 0 bytes free and refused every PSD on a
|
||||
// machine with tens of GB. A present directory still takes the guarded path below.
|
||||
// Only a confirmed NotFound counts as absent: a stat that fails for another reason
|
||||
// (permissions) leaves the directory possibly present, so that case stays fail-closed.
|
||||
let sys_root_absent = matches!(std::fs::metadata(sys_root), Err(e) if e.kind() == std::io::ErrorKind::NotFound);
|
||||
if sys_root_absent && mountinfo.as_deref().is_some_and(lists_mounts_without_cgroup) {
|
||||
return CgroupLimits::NotConstrained;
|
||||
}
|
||||
|
||||
if let Some(path) = membership.v1_memory {
|
||||
let Some(dir) = resolve_dir(sys_root, mountinfo.as_deref(), path, "cgroup", Some("memory")) else {
|
||||
@@ -336,6 +346,21 @@ pub(super) mod linux {
|
||||
best.map(|(_, _, dir)| dir)
|
||||
}
|
||||
|
||||
/// A cgroup (v1 or v2) filesystem.
|
||||
fn is_cgroup_mount(mount: &Mount) -> bool {
|
||||
mount.fstype == "cgroup" || mount.fstype == "cgroup2"
|
||||
}
|
||||
|
||||
/// `mountinfo` parsed to at least one mount, none of them a cgroup filesystem. An empty or
|
||||
/// garbled file parses to no mounts at all, which proves nothing about cgroups; a live kernel
|
||||
/// always lists the process's own root, so only a real list can show a cgroup mount is absent.
|
||||
/// Every non-empty line must parse: a skipped truncated line could be the cgroup mount.
|
||||
fn lists_mounts_without_cgroup(info: &str) -> bool {
|
||||
let mounts = parse_mountinfo(info);
|
||||
let lines = info.lines().filter(|l| !l.trim().is_empty()).count();
|
||||
!mounts.is_empty() && mounts.len() == lines && !mounts.iter().any(is_cgroup_mount)
|
||||
}
|
||||
|
||||
/// Trims trailing slashes so `Path` equality holds while walking ancestors, keeping `/`.
|
||||
fn normalize_root(point: &str) -> PathBuf {
|
||||
let trimmed = point.trim_end_matches('/');
|
||||
@@ -933,6 +958,33 @@ mod tests {
|
||||
assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::Unreadable, "without metadata the absence of a constraint cannot be established");
|
||||
std::fs::remove_dir_all(proc.parent().unwrap()).unwrap();
|
||||
|
||||
// No cgroup filesystem mounted (Flatpak): the limit is invisible, host memory applies.
|
||||
let (proc, _) = fixture("no-cgroup-mount");
|
||||
let sys = proc.parent().unwrap().join("absent-sys");
|
||||
write(&proc.join("self/cgroup"), "0::/app.slice/app-flatpak.scope\n");
|
||||
write(&proc.join("self/mountinfo"), "25 20 0:23 / /proc rw - proc proc rw\n");
|
||||
assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::NotConstrained);
|
||||
std::fs::remove_dir_all(proc.parent().unwrap()).unwrap();
|
||||
|
||||
// A truncated mount line may be the cgroup mount: with /sys/fs/cgroup absent, fail closed.
|
||||
let (proc, _) = fixture("truncated-mountinfo-line");
|
||||
let sys = proc.parent().unwrap().join("absent-sys");
|
||||
write(&proc.join("self/cgroup"), "0::/app\n");
|
||||
write(&proc.join("self/mountinfo"), "25 20 0:23 / /proc rw - proc proc rw\n30 20 0:26 / /sys/fs/cgroup rw - cgroup2\n");
|
||||
assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::Unreadable);
|
||||
std::fs::remove_dir_all(proc.parent().unwrap()).unwrap();
|
||||
|
||||
// A cgroup2 mount at an alternate path must be honoured while /sys/fs/cgroup is absent.
|
||||
let (proc, _) = fixture("alternate-cgroup-mount");
|
||||
let sys = proc.parent().unwrap().join("absent-sys");
|
||||
let alt = proc.parent().unwrap().join("alt-cgroup");
|
||||
write(&proc.join("self/cgroup"), "0::/app\n");
|
||||
write(&proc.join("self/mountinfo"), &format!("30 20 0:26 / {} rw - cgroup2 cgroup2 rw\n", alt.display()));
|
||||
write(&alt.join("app/memory.max"), "1073741824\n");
|
||||
write(&alt.join("app/memory.current"), "0\n");
|
||||
assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::Limited { ram: Some(1 << 30), swap: None });
|
||||
std::fs::remove_dir_all(proc.parent().unwrap()).unwrap();
|
||||
|
||||
// Available but not delegated anywhere on the path: no cgroup limit applies.
|
||||
let (proc, sys) = fixture("v2-not-enabled");
|
||||
write(&proc.join("self/cgroup"), "0::/app\n");
|
||||
@@ -1153,6 +1205,51 @@ mod tests {
|
||||
std::fs::remove_dir_all(proc.parent().unwrap()).unwrap();
|
||||
}
|
||||
|
||||
/// A missing `/sys/fs/cgroup` only means "no limit visible" when `mountinfo` was read, parsed
|
||||
/// to real mounts and lists no cgroup filesystem; every other combination stays fail-closed.
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn linux_absent_cgroup_root_needs_proof_before_it_means_unconstrained() {
|
||||
let cases: [(&str, Option<&str>); 3] =
|
||||
[("mountinfo-missing", None), ("mountinfo-empty", Some("")), ("mountinfo-garbage", Some("not a mountinfo line\n\n- - -\n"))];
|
||||
for (name, mountinfo) in cases {
|
||||
let (proc, _) = fixture(name);
|
||||
let sys = proc.parent().unwrap().join("absent-sys");
|
||||
write(&proc.join("self/cgroup"), "0::/app\n");
|
||||
if let Some(text) = mountinfo {
|
||||
write(&proc.join("self/mountinfo"), text);
|
||||
}
|
||||
assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::Unreadable, "{name}");
|
||||
std::fs::remove_dir_all(proc.parent().unwrap()).unwrap();
|
||||
}
|
||||
|
||||
// A stat error that is not NotFound (a path component that is a file) may hide a real
|
||||
// directory, so it is not "absent" either.
|
||||
let (proc, _) = fixture("sys-root-not-a-directory");
|
||||
write(&proc.join("self/cgroup"), "0::/app\n");
|
||||
write(&proc.join("self/mountinfo"), "25 20 0:23 / /proc rw - proc proc rw\n");
|
||||
let sys = proc.join("self/cgroup/fs/cgroup");
|
||||
assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::Unreadable);
|
||||
std::fs::remove_dir_all(proc.parent().unwrap()).unwrap();
|
||||
}
|
||||
|
||||
/// A cgroup mount listed at an alternate path means a limit may exist: v1 and v2 alike, and
|
||||
/// when its files cannot be read the answer is fail-closed, not "unconstrained".
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn linux_alternate_cgroup_mounts_with_unreadable_limits_fail_closed() {
|
||||
for (name, membership, fstype, options) in [("alt-v1", "4:memory:/app\n", "cgroup", "rw,memory"), ("alt-v2", "0::/app\n", "cgroup2", "rw")] {
|
||||
let (proc, _) = fixture(name);
|
||||
let sys = proc.parent().unwrap().join("absent-sys");
|
||||
let alt = proc.parent().unwrap().join("alt-cgroup");
|
||||
write(&proc.join("self/cgroup"), membership);
|
||||
write(&proc.join("self/mountinfo"), &format!("30 20 0:26 / {} rw - {fstype} {fstype} {options}\n", alt.display()));
|
||||
std::fs::create_dir_all(alt.join("app")).unwrap();
|
||||
assert_eq!(linux::cgroup_limits(&proc, &sys), linux::CgroupLimits::Unreadable, "{name}");
|
||||
std::fs::remove_dir_all(proc.parent().unwrap()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn linux_snapshot_combines_host_meminfo_and_cgroup_room() {
|
||||
|
||||
@@ -38,6 +38,9 @@ Implemented only from public specifications, papers and observation of files:
|
||||
stored in the files; they were recovered by black-box analysis of CC0 raw.pixls.us samples (the
|
||||
same scenes shot compressed and uncompressed) for LightCraft (storytold/lightcraft#86) and
|
||||
verified there on ~45 files from ~35 bodies.
|
||||
The "lossy after split" fixed-rate section (word size, prefix-to-category tables, bin-centre
|
||||
dequantisation, end-of-strip rule) was likewise derived by black-box analysis of 11 CC0
|
||||
raw.pixls.us files for LightCraft (storytold/lightcraft#633).
|
||||
* Sony cRAW (ARW 2): H. Dietz, "Sony ARW2 Compression: Artifacts And Credible Repair"
|
||||
(Electronic Imaging 2016) and the RawDigger / diglloyd write-ups of the 11 + 7-bit scheme;
|
||||
the exact bit layout and tone-curve scale were established by observation of sample files.
|
||||
@@ -72,14 +75,14 @@ camera colour tables were copied.
|
||||
| DNG | Uncompressed (8–16 bit, packed or not), Deflate (compression 8, predictors 1 and 2), lossless JPEG, lossy JPEG (DNG 1.4 compression 34892: 8-bit baseline JPEG tiles or strips, gray for CFA, YCbCr-coded RGB for LinearRaw; decoded by `zune-jpeg`, the stored 8-bit values then go through the same LinearizationTable / BlackLevel / WhiteLevel as every other depth) and JPEG XL (DNG 1.7, e.g. Samsung Expert RAW; decoded by `jxl-oxide`; not in wasm32 builds, where it is `Unsupported`); strips and tiles; CFA (Bayer) and LinearRaw (three-sample colour, or one-sample monochrome such as Leica Monochrom and film-scanner DNGs, developed as neutral grey with no white balance or colour matrix); LinearizationTable, BlackLevel (+ repeat, DeltaH/V), WhiteLevel, ActiveArea, DefaultCrop, ColorMatrix1/2, CameraCalibration, ForwardMatrix, AnalogBalance, AsShotNeutral / AsShotWhiteXY, BaselineExposure, Orientation, floating-point JPEG XL raw data (DNG 1.7 lossy LinearRaw, as Adobe DNG Converter writes it; scaled by the highest WhiteLevel), OpcodeList2 GainMap (lens shading) and MapPolynomial |
|
||||
| DNG (floating point other than JPEG XL; opcodes other than OpcodeList2 GainMap and MapPolynomial; ProfileGainTableMap) | Unsupported / not applied (reported) |
|
||||
| CR2 | Lossless JPEG with slices, borders and as-shot white balance from the maker note, black measured on the masked border. CR2 has no CFA tag and the row phase varies by model, so it is measured from the data (the green diagonal), with a Canon model-ID table as the fallback (see `src/cr2.rs`) |
|
||||
| CR2 sRAW / mRAW | Unsupported |
|
||||
| CR2 sRAW / mRAW (subsampled YCbCr) | Decoded to camera RGB and developed like LinearRaw: Canon's subsampled lossless JPEG (Y 2×2 or 2×1), BT.601 inverse, bilinear chroma, the per-shot sRAW gains from ColorData (version 15: EOS 6D Mark II, 77D, 200D, 800D; other bodies warn and rely on the fitted camera look). See `src/sraw.rs` |
|
||||
| NEF / NRW, ARW, PEF and other TIFF/EP raws | Uncompressed and lossless-JPEG (incl. Sony lossless ARW) CFA data |
|
||||
| Nikon compressed NEF (compression 34713): lossless and lossy type 1 / 2, 12 and 14 bit | Decoded: fixed Huffman tables + maker-note `0x0096` seeds and curve (see `src/nefc.rs`). BlackLevel `0x003d` is read in 14-bit units. "Lossy after split" files are unsupported (preview fallback) |
|
||||
| Nikon compressed NEF (compression 34713): lossless and lossy type 1 / 2, 12 and 14 bit | Decoded: fixed Huffman tables + maker-note `0x0096` seeds and curve (see `src/nefc.rs`). BlackLevel `0x003d` is read in 14-bit units. "Lossy after split" files (a split row in `0x0096`) are decoded: rows from the split on are fixed-rate words of `bits - 4` bits (storytold/lightcraft#633); a strip that does not end as that rule says falls back to the preview |
|
||||
| Sony compressed ARW ("cRAW", SonyRawFileType 2) | Decoded: 11-bit min/max + 7-bit delta blocks, SonyToneCurve to 14 bits; applied WB and black from plain tags or encrypted SR2SubIFD with the measured common key. If metadata is unreadable, black falls back to 512 and WB to grey-world. Measured ILCE-7 / ILCE-6000 image area; camera colour calibration remains incomplete |
|
||||
| Panasonic / Leica RW2, RawFormat 5 (12- and 14-bit packed) | Decoded, with PanasonicRaw black / white / WB / sensor borders |
|
||||
| Panasonic / Leica RW2, RawFormat 3 and 4 (vendor-compressed, 12-bit) | Decoded; the black level is read 15 counts above the recorded tag; PanasonicRaw white / WB / sensor borders |
|
||||
| Olympus ORF, uncompressed 16-bit (E-1, E-400…) | Decoded, with ImageProcessing black / WB / ValidBits / crop |
|
||||
| Nikon "lossy after split" NEF, Sony "Compressed RAW 2", Pentax compressed PEF, RW2 RawFormat 6 and newer, Olympus compressed ORF | Unsupported: no public description of these codes was found apart from GPL decoder source, which this crate may not use (clean-room). `photocraft-io` opens the embedded JPEG preview instead |
|
||||
| Sony "Compressed RAW 2", Pentax compressed PEF, RW2 RawFormat 6 and newer, Olympus compressed ORF | Unsupported: no public description of these codes was found apart from GPL decoder source, which this crate may not use (clean-room). `photocraft-io` opens the embedded JPEG preview instead |
|
||||
| Fujifilm RAF, uncompressed (X-Trans I–V, GFX and Bayer bodies since about 2010) | Decoded: 16-bit containers (either byte order), 12-bit LSB-first packing, 14-bit packing in 32-bit words; X-Trans 6×6 or Bayer 2×2 layout, black, as-shot WB, crop, RawExposureBias, orientation from the preview's EXIF |
|
||||
| Fujifilm compressed RAF, early FinePix / SuperCCD RAF (no CFA TIFF) | Unsupported (no public description of the compression); preview fallback |
|
||||
| CR3 | Native CRX unsupported; bounded JPEG-track/PRVW preview fallback with CMT1 orientation. I/O labels the result as an 8-bit camera preview |
|
||||
@@ -88,8 +91,8 @@ camera colour tables were copied.
|
||||
Nikon maker-note compression modes 6/10 select checked little-endian packed 12/14-bit
|
||||
rows even when TIFF Compression is 34713. The modern lossy `0x44 0x40` curve uses
|
||||
its smaller encoded domain while preserving sensor ADUs. Valid Nikon CropArea
|
||||
coordinates take precedence over model fallbacks. HE/HE* and lossy-after-split
|
||||
remain unsupported. Four pinned CC0 Z6 files cover both bit depths and both paths.
|
||||
coordinates take precedence over model fallbacks. HE/HE* remain unsupported;
|
||||
lossy-after-split strips are decoded (see the NEF row above). Four pinned CC0 Z6 files cover both bit depths and both paths.
|
||||
|
||||
## Development pipeline
|
||||
|
||||
|
||||
+121
-41
@@ -5,7 +5,8 @@
|
||||
//! lossless JPEG (T.81 process 14) whose decoded sample sequence fills the
|
||||
//! sensor image in vertical slices: the private tag 0xC640 gives the slice
|
||||
//! count, slice width and last slice width; slice 0 is filled row by row,
|
||||
//! then slice 1, and so on. sRAW/mRAW (subsampled YCbCr) is not supported.
|
||||
//! then slice 1, and so on. sRAW / mRAW (subsampled YCbCr) is decoded to
|
||||
//! camera RGB by `sraw.rs`.
|
||||
//!
|
||||
//! Sensor geometry and the as-shot white balance come from the Canon maker
|
||||
//! note (an IFD at the MakerNote offset, values relative to the TIFF header):
|
||||
@@ -45,7 +46,7 @@
|
||||
use crate::error::{RawError, Result};
|
||||
use crate::sensor::{BlackLevels, Cfa, Rect, Sensor};
|
||||
use crate::tiff::{Ifd, Tiff, tag};
|
||||
use crate::{Limits, RawFormat, ljpeg};
|
||||
use crate::{Limits, RawFormat, ljpeg, sraw};
|
||||
|
||||
const CANON_SENSOR_INFO: u16 = 0x00E0;
|
||||
const CANON_COLOR_DATA: u16 = 0x4001;
|
||||
@@ -115,26 +116,38 @@ pub(crate) fn maker_note(t: &Tiff, ifds: &[Ifd]) -> Option<Ifd> {
|
||||
|
||||
/// As-shot white-balance multipliers (R, G, B) from ColorData.
|
||||
fn as_shot_wb(t: &Tiff, mn: &Ifd) -> Option<[f64; 3]> {
|
||||
let e = mn.get(CANON_COLOR_DATA)?;
|
||||
// The entry is UNDEFINED: its bytes are a table of 16-bit words (file byte order), and the
|
||||
// offsets below count words, not bytes. Reads stay inside the entry's own byte count.
|
||||
let bytes = e.unit().checked_mul(e.count as usize)?;
|
||||
let word = |off: usize| {
|
||||
let at = off.checked_mul(2)?.checked_add(e.at)?;
|
||||
(at.checked_add(2)? <= e.at.checked_add(bytes)?).then(|| t.u16_at(at)).flatten().map(f64::from)
|
||||
};
|
||||
// Word offsets of WB_RGGBLevelsAsShot used by the ColorData versions.
|
||||
for off in [0x3F, 0x47, 0x19, 0x22] {
|
||||
let (Some(r), Some(g1), Some(g2), Some(b)) = (word(off), word(off + 1), word(off + 2), word(off + 3)) else {
|
||||
continue;
|
||||
};
|
||||
let g = (g1 + g2) / 2.0;
|
||||
let plausible = (256.0..16384.0).contains(&g) && (g1 - g2).abs() <= g * 0.05 && (0.25..8.0).contains(&(r / g)) && (0.25..8.0).contains(&(b / g));
|
||||
if plausible {
|
||||
return Some([r / g, 1.0, b / g]);
|
||||
}
|
||||
}
|
||||
None
|
||||
[0x3F, 0x47, 0x19, 0x22].into_iter().find_map(|off| color_data_rggb(t, mn, off))
|
||||
}
|
||||
|
||||
/// Word `off` of ColorData. The entry is UNDEFINED: its bytes are a table of 16-bit words (file
|
||||
/// byte order), and the offsets count words, not bytes. Reads stay inside the entry's own byte
|
||||
/// count.
|
||||
fn color_data_word(t: &Tiff, mn: &Ifd, off: usize) -> Option<u16> {
|
||||
let e = mn.get(CANON_COLOR_DATA)?;
|
||||
let bytes = e.unit().checked_mul(e.count as usize)?;
|
||||
let at = off.checked_mul(2)?.checked_add(e.at)?;
|
||||
(at.checked_add(2)? <= e.at.checked_add(bytes)?).then(|| t.u16_at(at)).flatten()
|
||||
}
|
||||
|
||||
/// RGGB levels at ColorData word `off` as multipliers relative to green, when plausible.
|
||||
fn color_data_rggb(t: &Tiff, mn: &Ifd, off: usize) -> Option<[f64; 3]> {
|
||||
let word = |k: usize| color_data_word(t, mn, off.checked_add(k)?).map(f64::from);
|
||||
let (r, g1, g2, b) = (word(0)?, word(1)?, word(2)?, word(3)?);
|
||||
let g = (g1 + g2) / 2.0;
|
||||
let plausible = (256.0..16384.0).contains(&g) && (g1 - g2).abs() <= g * 0.05 && (0.25..8.0).contains(&(r / g)) && (0.25..8.0).contains(&(b / g));
|
||||
plausible.then(|| [r / g, 1.0, b / g])
|
||||
}
|
||||
|
||||
/// ColorData versions whose sRAW gains (see `sraw.rs`) sit at a word offset we observed:
|
||||
/// version 15 (EOS 6D Mark II, 77D, 200D, 800D) at 0x80, just before the daylight preset.
|
||||
const SRAW_GAINS: [(u16, usize); 1] = [(15, 0x80)];
|
||||
|
||||
/// The per-shot gains an sRAW's stored RGB was divided by, relative to green.
|
||||
fn sraw_gains(t: &Tiff, mn: &Ifd) -> Option<[f64; 3]> {
|
||||
let version = color_data_word(t, mn, 0)?;
|
||||
let (_, off) = SRAW_GAINS.iter().find(|(v, _)| *v == version)?;
|
||||
color_data_rggb(t, mn, *off)
|
||||
}
|
||||
|
||||
/// The white (clipping) level: the largest value when a meaningful share of
|
||||
@@ -181,6 +194,86 @@ pub(crate) fn masked_black(data: &[u16], width: usize, area: Rect) -> Option<[f3
|
||||
Some([0, 1, 2, 3].map(|k| sum[k] as f32 / n[k] as f32))
|
||||
}
|
||||
|
||||
/// The slice widths of CR2 tag 0xC640 (count, width, last width), in samples. The tag is SHORT:
|
||||
/// larger values (a LONG in a damaged file) are refused, which also bounds the sum.
|
||||
fn slice_widths(tag: &[u32]) -> Option<Vec<usize>> {
|
||||
let short = 1..=u32::from(u16::MAX);
|
||||
match tag {
|
||||
[n, w, last] if *n < 64 && short.contains(w) && short.contains(last) => {
|
||||
let mut ws = vec![*w as usize; *n as usize];
|
||||
ws.push(*last as usize);
|
||||
Some(ws)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The image area inside `full` from the maker note's SensorInfo ([1] width, [2] height, [5]
|
||||
/// left, [6] top, [7] right, [8] bottom border, inclusive); `full` when absent or empty.
|
||||
fn image_area(t: &Tiff, mn: Option<&Ifd>, full: Rect) -> Rect {
|
||||
let info = mn.map(|m| t.tag_uints(m, CANON_SENSOR_INFO)).unwrap_or_default();
|
||||
let active = match info.get(5..9) {
|
||||
Some(&[l, tp, r, b]) if r > l && b > tp => Rect::new(l as usize, tp as usize, (r - l + 1) as usize, (b - tp + 1) as usize).intersect(&full),
|
||||
_ => full,
|
||||
};
|
||||
if active.is_empty() { full } else { active }
|
||||
}
|
||||
|
||||
/// sRAW / mRAW: subsampled YCbCr, developed as three-sample camera RGB (see `sraw.rs`).
|
||||
fn decode_sraw(t: &Tiff, ifds: &[Ifd], ifd0: &Ifd, slices: &[u32], src: &[u8], frame: ljpeg::Frame, limits: &Limits) -> Result<Sensor> {
|
||||
let per_mcu = frame.h * frame.v + 2;
|
||||
let total = frame.samples().ok_or_else(|| RawError::malformed("CR2 frame too large"))?;
|
||||
let widths = slice_widths(slices).unwrap_or_else(|| vec![frame.width / frame.h * per_mcu]);
|
||||
let row: usize = widths.iter().sum();
|
||||
if row == 0 || !row.is_multiple_of(per_mcu) || !total.is_multiple_of(row) {
|
||||
return Err(RawError::malformed("CR2 sRAW slices do not match the JPEG frame"));
|
||||
}
|
||||
let (width, height) = (row / per_mcu * frame.h, total / row * frame.v);
|
||||
// The RGB output: three u16 per pixel.
|
||||
limits.check(width as u64, height as u64, 6)?;
|
||||
let (_, samples) = ljpeg::decode_subsampled(src, total)?;
|
||||
let planes = sraw::planes(&frame, &samples, &widths)?;
|
||||
drop(samples);
|
||||
|
||||
let mut warnings = Vec::new();
|
||||
let mn = maker_note(t, ifds);
|
||||
let camera_wb = mn.as_ref().and_then(|m| as_shot_wb(t, m));
|
||||
let gains = mn.as_ref().and_then(|m| sraw_gains(t, m));
|
||||
if gains.is_none() {
|
||||
warnings.push("the sRAW colour gains of this camera are not known; colours rely on the look fitted to the camera's JPEG".to_string());
|
||||
}
|
||||
let offset = (1u32 << (frame.precision - 1)) as f32;
|
||||
let data = sraw::camera_rgb(&planes, offset, gains.unwrap_or([1.0; 3]));
|
||||
drop(planes);
|
||||
let full = Rect::new(0, 0, width, height);
|
||||
let active = image_area(t, mn.as_ref(), full);
|
||||
let white = [0, 1, 2].map(|k| sraw::clip_level(&data, k, sraw::FULL_SCALE));
|
||||
Ok(Sensor {
|
||||
format: RawFormat::Cr2,
|
||||
make: t.tag_ascii(ifd0, tag::MAKE),
|
||||
model: t.tag_ascii(ifd0, tag::MODEL),
|
||||
width,
|
||||
height,
|
||||
samples: 3,
|
||||
data,
|
||||
cfa: None,
|
||||
linearization: None,
|
||||
// Y is stored black-subtracted.
|
||||
black: BlackLevels::uniform(0.0),
|
||||
white,
|
||||
active,
|
||||
crop: active,
|
||||
color: Default::default(),
|
||||
camera_wb,
|
||||
orientation: crate::tiff::orientation(t.tag_uint(ifd0, tag::ORIENTATION)),
|
||||
baseline_exposure: 0.0,
|
||||
gain_maps: Vec::new(),
|
||||
tone_curve: Vec::new(),
|
||||
black_point: 0.0,
|
||||
warnings,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result<Sensor> {
|
||||
let ifds = t.all_ifds();
|
||||
let ifd0 = t.ifd_at(t.first_ifd, 0).ok_or_else(|| RawError::malformed("CR2 has no IFD0"))?;
|
||||
@@ -191,23 +284,16 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result<Sensor> {
|
||||
let src = t.bytes(off, len).or_else(|| t.data.get(off..)).ok_or_else(|| RawError::malformed("CR2 raw data lies outside the file"))?;
|
||||
|
||||
let frame = ljpeg::frame(src).map_err(|e| match e {
|
||||
RawError::Unsupported(m) if m.contains("subsampled") => RawError::unsupported("Canon sRAW / mRAW"),
|
||||
RawError::Unsupported(m) if m.contains("subsampled") => RawError::unsupported("this Canon sRAW / mRAW layout"),
|
||||
e => e,
|
||||
})?;
|
||||
let total = frame.samples().ok_or_else(|| RawError::malformed("CR2 frame too large"))?;
|
||||
let slices = t.tag_uints(&raw, tag::CR2_SLICE);
|
||||
let (width, widths) = match slices.as_slice() {
|
||||
[n, w, last] if *n < 64 && *w > 0 && *last > 0 => {
|
||||
let (n, w, last) = (*n as usize, *w as usize, *last as usize);
|
||||
let mut ws = vec![w; n];
|
||||
ws.push(last);
|
||||
(n * w + last, ws)
|
||||
}
|
||||
_ => {
|
||||
let w = frame.width * frame.components;
|
||||
(w, vec![w])
|
||||
}
|
||||
};
|
||||
if frame.is_subsampled() {
|
||||
return decode_sraw(t, &ifds, &ifd0, &slices, src, frame, limits);
|
||||
}
|
||||
let widths = slice_widths(&slices).unwrap_or_else(|| vec![frame.width * frame.components]);
|
||||
let width: usize = widths.iter().sum();
|
||||
if width == 0 || total % width != 0 {
|
||||
return Err(RawError::malformed("CR2 slices do not match the JPEG frame"));
|
||||
}
|
||||
@@ -231,13 +317,7 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result<Sensor> {
|
||||
let mut warnings = Vec::new();
|
||||
let mn = maker_note(t, &ifds);
|
||||
let full = Rect::new(0, 0, width, height);
|
||||
// SensorInfo: [1] width, [2] height, [5] left, [6] top, [7] right, [8] bottom border (inclusive).
|
||||
let info = mn.as_ref().map(|m| t.tag_uints(m, CANON_SENSOR_INFO)).unwrap_or_default();
|
||||
let active = match info.get(5..9) {
|
||||
Some(&[l, tp, r, b]) if r > l && b > tp => Rect::new(l as usize, tp as usize, (r - l + 1) as usize, (b - tp + 1) as usize).intersect(&full),
|
||||
_ => full,
|
||||
};
|
||||
let active = if active.is_empty() { full } else { active };
|
||||
let active = image_area(t, mn.as_ref(), full);
|
||||
// Black: the masked columns left of the image (skipping a few edge columns).
|
||||
let black = if active.x >= 16 {
|
||||
// Only the outer half: the columns next to the image area can be partly exposed.
|
||||
|
||||
+187
-29
@@ -147,6 +147,7 @@ impl Padded {
|
||||
}
|
||||
|
||||
/// Per 2×2 position, per output colour: kernel taps as (offset, weight).
|
||||
#[cfg(test)]
|
||||
fn kernels(phase: [u8; 4], method: Demosaic, stride: usize) -> [[Vec<(isize, f32)>; 3]; 4] {
|
||||
let color = |x: isize, y: isize| phase[((y & 1) * 2 + (x & 1)) as usize];
|
||||
let s = stride as isize;
|
||||
@@ -213,9 +214,8 @@ pub(crate) fn demosaic(p: &Padded, phase: [u8; 4], method: Demosaic, to_xyz: &Ma
|
||||
);
|
||||
return out;
|
||||
}
|
||||
let ks = kernels(phase, method, s);
|
||||
let rows = par::band_rows(w);
|
||||
par::chunks_mut(&mut out, rows * w * 3, |band, chunk| linear_band(&p.data, w, s, phase, method, &ks, band * rows, 0, chunk));
|
||||
par::chunks_mut(&mut out, rows * w * 3, |band, chunk| linear_band(&p.data, w, s, phase, method, band * rows, 0, chunk));
|
||||
out
|
||||
}
|
||||
|
||||
@@ -266,13 +266,12 @@ pub(crate) fn demosaic_fused(
|
||||
});
|
||||
return;
|
||||
}
|
||||
let ks = kernels(phase, method, s);
|
||||
let rows = h.min(FUSED_ROWS);
|
||||
let init = || (vec![0.0f32; rows * w * 3], vec![0.0f32; (rows + 2 * PAD) * s]);
|
||||
for_bands(out, FUSED_ROWS * w * 3, h.div_ceil(FUSED_ROWS), jobs, init, |band, chunk, (buf, win)| {
|
||||
load(win, band * FUSED_ROWS, chunk.len() / (w * 3));
|
||||
let buf = &mut buf[..chunk.len()];
|
||||
linear_band(win, w, s, phase, method, &ks, band * FUSED_ROWS, band * FUSED_ROWS, buf);
|
||||
linear_band(win, w, s, phase, method, band * FUSED_ROWS, band * FUSED_ROWS, buf);
|
||||
emit(buf, chunk);
|
||||
});
|
||||
}
|
||||
@@ -283,40 +282,120 @@ pub(crate) const FUSED_ROWS: usize = 32;
|
||||
/// Rows `y0..` of bilinear or MHC output (`chunk.len() / (w * 3)` of them); `d` is the padded
|
||||
/// plane from image row `dy` on (`dy` 0: all of it).
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn linear_band(d: &[f32], w: usize, s: usize, phase: [u8; 4], method: Demosaic, ks: &[[Vec<(isize, f32)>; 3]; 4], y0: usize, dy: usize, chunk: &mut [f32]) {
|
||||
fn linear_band(d: &[f32], w: usize, s: usize, phase: [u8; 4], method: Demosaic, y0: usize, dy: usize, chunk: &mut [f32]) {
|
||||
for (r, row) in chunk.chunks_exact_mut(w * 3).enumerate() {
|
||||
let y = y0 + r;
|
||||
let base = (y - dy + PAD) * s + PAD;
|
||||
for (x, o) in row.as_chunks_mut::<3>().0.iter_mut().enumerate() {
|
||||
let pos = (y & 1) * 2 + (x & 1);
|
||||
let i = base + x;
|
||||
if method == Demosaic::Bilinear {
|
||||
// Direct Bayer bilinear (the kernels' result without the tap loops).
|
||||
let own = usize::from(phase[pos]).min(2);
|
||||
let (n, so, e, wv) = (d[i - s], d[i + s], d[i + 1], d[i - 1]);
|
||||
if own == 1 {
|
||||
let hc = usize::from(phase[(y & 1) * 2 + ((x + 1) & 1)]).min(2);
|
||||
o[1] = d[i];
|
||||
o[hc] = (e + wv) * 0.5;
|
||||
o[2 - hc] = (n + so) * 0.5;
|
||||
} else {
|
||||
o[own] = d[i];
|
||||
o[1] = (n + so + e + wv) * 0.25;
|
||||
o[2 - own] = (d[i - s - 1] + d[i - s + 1] + d[i + s - 1] + d[i + s + 1]) * 0.25;
|
||||
// The two colours of this row's 2-pixel period; the row kinds a Bayer pattern has get
|
||||
// code of their own with the colours as constants.
|
||||
let (c0, c1) = (usize::from(phase[(y & 1) * 2]).min(2), usize::from(phase[(y & 1) * 2 + 1]).min(2));
|
||||
match (method == Demosaic::Bilinear, c0, c1) {
|
||||
(true, 0, 1) => linear_row::<0, 1, false>(d, base, s, row),
|
||||
(true, 1, 0) => linear_row::<1, 0, false>(d, base, s, row),
|
||||
(true, 2, 1) => linear_row::<2, 1, false>(d, base, s, row),
|
||||
(true, 1, 2) => linear_row::<1, 2, false>(d, base, s, row),
|
||||
(false, 0, 1) => linear_row::<0, 1, true>(d, base, s, row),
|
||||
(false, 1, 0) => linear_row::<1, 0, true>(d, base, s, row),
|
||||
(false, 2, 1) => linear_row::<2, 1, true>(d, base, s, row),
|
||||
(false, 1, 2) => linear_row::<1, 2, true>(d, base, s, row),
|
||||
(bilinear, _, _) => {
|
||||
for (x, o) in row.as_chunks_mut::<3>().0.iter_mut().enumerate() {
|
||||
let (own, hc) = if x & 1 == 0 { (c0, c1) } else { (c1, c0) };
|
||||
*o = if bilinear { bilinear_site(d, base + x, s, own, hc) } else { mhc_site(d, base + x, s, own, hc) };
|
||||
}
|
||||
continue;
|
||||
}
|
||||
for (c, k) in ks[pos].iter().enumerate() {
|
||||
let mut v = 0.0f32;
|
||||
for &(off, wt) in k {
|
||||
v += wt * d[(i as isize + off) as usize];
|
||||
}
|
||||
o[c] = v.clamp(0.0, 1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A row of output whose even pixels are colour `A` and odd pixels colour `B`, MHC or bilinear.
|
||||
fn linear_row<const A: usize, const B: usize, const MHC: bool>(d: &[f32], base: usize, s: usize, row: &mut [f32]) {
|
||||
let site = |i: usize, own: usize, hc: usize| if MHC { mhc_site(d, i, s, own, hc) } else { bilinear_site(d, i, s, own, hc) };
|
||||
let (pairs, rest) = row.as_chunks_mut::<6>();
|
||||
for (j, pair) in pairs.iter_mut().enumerate() {
|
||||
let i = base + 2 * j;
|
||||
pair[..3].copy_from_slice(&site(i, A, B));
|
||||
pair[3..].copy_from_slice(&site(i + 1, B, A));
|
||||
}
|
||||
if let Some(last) = rest.first_chunk_mut::<3>() {
|
||||
*last = site(base + 2 * pairs.len(), A, B);
|
||||
}
|
||||
}
|
||||
|
||||
/// Direct Bayer bilinear (the kernels' result without the tap loops) for the pixel at plane index
|
||||
/// `i` of colour `own` (and, when green, its row neighbours' colour `hc`).
|
||||
#[inline(always)]
|
||||
fn bilinear_site(d: &[f32], i: usize, s: usize, own: usize, hc: usize) -> [f32; 3] {
|
||||
let mut o = [0.0f32; 3];
|
||||
let (n, so, e, wv) = (d[i - s], d[i + s], d[i + 1], d[i - 1]);
|
||||
if own == 1 {
|
||||
o[1] = d[i];
|
||||
o[hc] = (e + wv) * 0.5;
|
||||
o[2 - hc] = (n + so) * 0.5;
|
||||
} else {
|
||||
o[own] = d[i];
|
||||
o[1] = (n + so + e + wv) * 0.25;
|
||||
o[2 - own] = (d[i - s - 1] + d[i - s + 1] + d[i + s - 1] + d[i + s + 1]) * 0.25;
|
||||
}
|
||||
o
|
||||
}
|
||||
|
||||
/// Malvar–He–Cutler for the pixel at plane index `i`, whose colour is `own` (and, when green, its
|
||||
/// row neighbours' `hc`): the kernels of `kernels` written out, the same taps in the same order
|
||||
/// with their weights (kernel / 8, exact in f32), so the sums are those of the tap loop.
|
||||
#[inline(always)]
|
||||
fn mhc_site(d: &[f32], i: usize, s: usize, own: usize, hc: usize) -> [f32; 3] {
|
||||
let mut o = [0.0f32; 3];
|
||||
o[own] = (0.0 + d[i]).clamp(0.0, 1.0);
|
||||
if own == 1 {
|
||||
// R and B at a green site: the row neighbours' colour from the row, the other from the
|
||||
// column.
|
||||
o[hc] = mhc_at_green(d, i, 1, s, s).clamp(0.0, 1.0);
|
||||
o[2 - hc] = mhc_at_green(d, i, s, 1, s).clamp(0.0, 1.0);
|
||||
} else {
|
||||
let v = 0.0
|
||||
+ 0.5 * d[i]
|
||||
+ 0.25 * d[i - 1]
|
||||
+ 0.25 * d[i + 1]
|
||||
+ 0.25 * d[i - s]
|
||||
+ 0.25 * d[i + s]
|
||||
+ -0.125 * d[i - 2]
|
||||
+ -0.125 * d[i + 2]
|
||||
+ -0.125 * d[i - 2 * s]
|
||||
+ -0.125 * d[i + 2 * s];
|
||||
o[1] = v.clamp(0.0, 1.0);
|
||||
let v = 0.0
|
||||
+ 0.75 * d[i]
|
||||
+ 0.25 * d[i - s - 1]
|
||||
+ 0.25 * d[i - s + 1]
|
||||
+ 0.25 * d[i + s - 1]
|
||||
+ 0.25 * d[i + s + 1]
|
||||
+ -0.1875 * d[i - 2]
|
||||
+ -0.1875 * d[i + 2]
|
||||
+ -0.1875 * d[i - 2 * s]
|
||||
+ -0.1875 * d[i + 2 * s];
|
||||
o[2 - own] = v.clamp(0.0, 1.0);
|
||||
}
|
||||
o
|
||||
}
|
||||
|
||||
/// MHC's red or blue at a green site: `a` is the step along the colour's own line (1 along the
|
||||
/// row, `s` down the column), `b` the step across it.
|
||||
#[inline(always)]
|
||||
fn mhc_at_green(d: &[f32], i: usize, a: usize, b: usize, s: usize) -> f32 {
|
||||
0.0 + 0.625 * d[i]
|
||||
+ 0.5 * d[i - a]
|
||||
+ 0.5 * d[i + a]
|
||||
+ -0.125 * d[i - s - 1]
|
||||
+ -0.125 * d[i - s + 1]
|
||||
+ -0.125 * d[i + s - 1]
|
||||
+ -0.125 * d[i + s + 1]
|
||||
+ -0.125 * d[i - 2 * a]
|
||||
+ -0.125 * d[i + 2 * a]
|
||||
+ 0.0625 * d[i - 2 * b]
|
||||
+ 0.0625 * d[i + 2 * b]
|
||||
}
|
||||
|
||||
/// Runs `f(band, chunk, scratch)` over the `unit`-sized chunks of `out` (`bands` of them), with
|
||||
/// at most `jobs` scratch values alive. When every worker may hold one, each band is its own
|
||||
/// rayon task and borrows a scratch from a pool (one per worker at most); otherwise a few groups
|
||||
@@ -1286,6 +1365,85 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// The tap-loop evaluation of bilinear and MHC that `linear_band` replaced.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn linear_band_ref(
|
||||
d: &[f32],
|
||||
w: usize,
|
||||
s: usize,
|
||||
phase: [u8; 4],
|
||||
method: Demosaic,
|
||||
ks: &[[Vec<(isize, f32)>; 3]; 4],
|
||||
y0: usize,
|
||||
dy: usize,
|
||||
chunk: &mut [f32],
|
||||
) {
|
||||
for (r, row) in chunk.chunks_exact_mut(w * 3).enumerate() {
|
||||
let y = y0 + r;
|
||||
let base = (y - dy + PAD) * s + PAD;
|
||||
for (x, o) in row.as_chunks_mut::<3>().0.iter_mut().enumerate() {
|
||||
let pos = (y & 1) * 2 + (x & 1);
|
||||
let i = base + x;
|
||||
if method == Demosaic::Bilinear {
|
||||
// Direct Bayer bilinear (the kernels' result without the tap loops).
|
||||
let own = usize::from(phase[pos]).min(2);
|
||||
let (n, so, e, wv) = (d[i - s], d[i + s], d[i + 1], d[i - 1]);
|
||||
if own == 1 {
|
||||
let hc = usize::from(phase[(y & 1) * 2 + ((x + 1) & 1)]).min(2);
|
||||
o[1] = d[i];
|
||||
o[hc] = (e + wv) * 0.5;
|
||||
o[2 - hc] = (n + so) * 0.5;
|
||||
} else {
|
||||
o[own] = d[i];
|
||||
o[1] = (n + so + e + wv) * 0.25;
|
||||
o[2 - own] = (d[i - s - 1] + d[i - s + 1] + d[i + s - 1] + d[i + s + 1]) * 0.25;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
for (c, k) in ks[pos].iter().enumerate() {
|
||||
let mut v = 0.0f32;
|
||||
for &(off, wt) in k {
|
||||
v += wt * d[(i as isize + off) as usize];
|
||||
}
|
||||
o[c] = v.clamp(0.0, 1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mhc_written_out_gives_the_bits_of_the_tap_loop() {
|
||||
// Every Bayer phase, odd and tiny sizes, values outside 0..1, signed zeros, NaN and infinities.
|
||||
for (w, h) in [(1, 1), (2, 3), (5, 4), (17, 13), (64, 9)] {
|
||||
let mut s = 0x9E37_79B9_7F4A_7C15u64;
|
||||
let mut plane: Vec<f32> = (0..w * h)
|
||||
.map(|_| {
|
||||
s = s.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
|
||||
((s >> 40) as f32 / (1u64 << 24) as f32) * 1.6 - 0.3
|
||||
})
|
||||
.collect();
|
||||
for (k, v) in [0.0, -0.0, f32::NAN, f32::INFINITY, f32::NEG_INFINITY, 1.0, 1e-30].into_iter().enumerate() {
|
||||
let n = plane.len();
|
||||
plane[(k * 7 + 1) % n] = v;
|
||||
}
|
||||
let p = Padded::from_plane(&plane, w, h);
|
||||
for phase in [[0, 1, 1, 2], [2, 1, 1, 0], [1, 0, 2, 1], [1, 2, 0, 1]] {
|
||||
for method in [Demosaic::Bilinear, Demosaic::Mhc] {
|
||||
let ks = kernels(phase, method, p.stride);
|
||||
let mut want = vec![0.0f32; w * h * 3];
|
||||
linear_band_ref(&p.data, w, p.stride, phase, method, &ks, 0, 0, &mut want);
|
||||
let mut got = vec![0.0f32; w * h * 3];
|
||||
linear_band(&p.data, w, p.stride, phase, method, 0, 0, &mut got);
|
||||
// Equal bits; a NaN may come out with another sign or payload (the order the
|
||||
// two evaluations meet two NaNs in), which the encoding maps to the same code.
|
||||
let bad: Vec<_> =
|
||||
got.iter().zip(&want).enumerate().filter(|(_, (a, b))| a.to_bits() != b.to_bits() && !(a.is_nan() && b.is_nan())).collect();
|
||||
assert!(bad.is_empty(), "{w}x{h} {phase:?} {}: {bad:?}", method.id());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lab_table_is_accurate() {
|
||||
let l = LabF::new();
|
||||
|
||||
+164
-25
@@ -118,6 +118,70 @@ impl<'a> Scale<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Row `y` of a LinearRaw (3-sample or monochrome) frame from column `x0` on, `row.len() / 3`
|
||||
/// pixels: [`Scale::value`] times the white balance, clipped. Monochrome data fills all three
|
||||
/// channels with its one sample.
|
||||
///
|
||||
/// The black level of a row depends on the column only through the repeat pattern, so (when
|
||||
/// there are no per-column deltas) it is worked out once per row and phase instead of per sample.
|
||||
fn fill_linear_row(sc: &Scale, mult: &[f32; 3], x0: usize, y: usize, row: &mut [f32]) {
|
||||
let s = sc.s;
|
||||
let spp = s.samples;
|
||||
let w = row.len() / 3;
|
||||
let bl = &s.black;
|
||||
let cols = bl.cols.max(1);
|
||||
if !bl.delta_h.is_empty() || !bl.delta_v.is_empty() || x0 < s.active.x || cols > 16 || !matches!(spp, 1 | 3) {
|
||||
for (i, px) in row.as_chunks_mut::<3>().0.iter_mut().enumerate() {
|
||||
for k in 0..3 {
|
||||
px[k] = (sc.value(x0 + i, y, k.min(spp.saturating_sub(1))) * mult[k]).clamp(0.0, 1.0);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Black level and range per column phase and channel.
|
||||
let ay = y.saturating_sub(s.active.y);
|
||||
let table: Vec<[(f32, f32); 3]> = (0..cols)
|
||||
.map(|p| {
|
||||
[0, 1, 2].map(|k| {
|
||||
let sample = k.min(spp - 1);
|
||||
let black = bl.at(p, ay, sample, spp);
|
||||
(black, s.white[sample.min(2)] - black)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let src = &s.data[(y * s.width + x0) * spp..(y * s.width + x0 + w) * spp];
|
||||
let phase = (x0 - s.active.x) % cols;
|
||||
let lin = |raw: u16| match sc.lin {
|
||||
Some(l) => f32::from(l.get(usize::from(raw)).or_else(|| l.last()).copied().unwrap_or(raw)),
|
||||
None => f32::from(raw),
|
||||
};
|
||||
match spp {
|
||||
3 => fill_samples(row, src.as_chunks::<3>().0.iter().copied(), &table, phase, mult, lin),
|
||||
_ => fill_samples(row, src.iter().map(|r| [*r; 3]), &table, phase, mult, lin),
|
||||
}
|
||||
}
|
||||
|
||||
/// The pixels of [`fill_linear_row`]: `raw` gives each pixel's three raw values (the one sample
|
||||
/// of monochrome data three times), `table` the black level and range per column phase.
|
||||
#[inline(always)]
|
||||
fn fill_samples(row: &mut [f32], raw: impl Iterator<Item = [u16; 3]>, table: &[[(f32, f32); 3]], phase: usize, mult: &[f32; 3], lin: impl Fn(u16) -> f32) {
|
||||
let mut phase = phase;
|
||||
for (px, raw) in row.as_chunks_mut::<3>().0.iter_mut().zip(raw) {
|
||||
let t = &table[phase];
|
||||
phase = if phase + 1 == table.len() { 0 } else { phase + 1 };
|
||||
for k in 0..3 {
|
||||
let (black, range) = t[k];
|
||||
let n = if range.is_nan() || range < 1.0 {
|
||||
0.0
|
||||
} else {
|
||||
let n = (lin(raw[k]) - black) / range;
|
||||
if n.is_finite() { n } else { 0.0 }
|
||||
};
|
||||
px[k] = (n * mult[k]).clamp(0.0, 1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Grey-world neutral (camera-space R, G, B) from unclipped mid-range samples.
|
||||
fn grey_world(s: &Sensor, sc: &Scale) -> [f64; 3] {
|
||||
let c = s.crop;
|
||||
@@ -293,13 +357,7 @@ fn develop(s: &Sensor, opts: &DevelopOptions, allow_fused: bool) -> Result<Devel
|
||||
let linear_rows = |y0: usize, chunk: &mut [f32]| {
|
||||
for (r, row) in chunk.chunks_exact_mut(w * 3).enumerate() {
|
||||
let y = c.y + y0 + r;
|
||||
for i in 0..w {
|
||||
for k in 0..3 {
|
||||
// Monochrome: the one sample in all three channels.
|
||||
let sample = k.min(s.samples.saturating_sub(1));
|
||||
row[i * 3 + k] = (sc.value(c.x + i, y, sample) * mult[k]).clamp(0.0, 1.0);
|
||||
}
|
||||
}
|
||||
fill_linear_row(&sc, &mult, c.x, y, row);
|
||||
for k in 0..s.samples.min(3) {
|
||||
if let Some(g) = gain_row(s, y, c.x, w, k) {
|
||||
for (i, g) in g.into_iter().enumerate() {
|
||||
@@ -707,6 +765,10 @@ pub(crate) fn source_xy(o: u16, w: usize, h: usize, x2: usize, y2: usize) -> (us
|
||||
}
|
||||
}
|
||||
|
||||
/// Side of the square tiles orientations 5-8, which swap rows and columns, move at a time: both
|
||||
/// sides of a tile stay in L1 instead of one of them being walked a column at a time.
|
||||
const TILE: usize = 32;
|
||||
|
||||
/// Applies a TIFF orientation (1–8) to interleaved RGB.
|
||||
pub(crate) fn orient(src: Vec<u16>, w: usize, h: usize, o: u16) -> (Vec<u16>, usize, usize) {
|
||||
if !(2..=8).contains(&o) {
|
||||
@@ -715,25 +777,44 @@ pub(crate) fn orient(src: Vec<u16>, w: usize, h: usize, o: u16) -> (Vec<u16>, us
|
||||
let swap = o >= 5;
|
||||
let (ow, oh) = if swap { (h, w) } else { (w, h) };
|
||||
let mut out = vec![0u16; src.len()];
|
||||
let band = par::band_rows(ow);
|
||||
par::chunks_mut(&mut out, band * ow * 3, |b, chunk| {
|
||||
for (r, row) in chunk.chunks_exact_mut(ow * 3).enumerate() {
|
||||
let y2 = b * band + r;
|
||||
for x2 in 0..ow {
|
||||
let (x, y) = match o {
|
||||
2 => (w - 1 - x2, y2),
|
||||
3 => (w - 1 - x2, h - 1 - y2),
|
||||
4 => (x2, h - 1 - y2),
|
||||
5 => (y2, x2),
|
||||
6 => (y2, h - 1 - x2),
|
||||
7 => (w - 1 - y2, h - 1 - x2),
|
||||
_ => (w - 1 - y2, x2),
|
||||
};
|
||||
let i = (y * w + x) * 3;
|
||||
row[x2 * 3..x2 * 3 + 3].copy_from_slice(&src[i..i + 3]);
|
||||
if swap {
|
||||
// Output pixel (x2, y2) is source pixel `base + x2 * sx + y2 * sy` (see `source_xy`).
|
||||
let (base, sx, sy): (isize, isize, isize) = match o {
|
||||
5 => (0, w as isize, 1),
|
||||
6 => (((h - 1) * w) as isize, -(w as isize), 1),
|
||||
7 => ((h * w - 1) as isize, -(w as isize), -1),
|
||||
_ => ((w - 1) as isize, w as isize, -1),
|
||||
};
|
||||
let px = src.as_chunks::<3>().0;
|
||||
par::chunks_mut(&mut out, TILE * ow * 3, |b, chunk| {
|
||||
let rows = chunk.len() / (ow * 3);
|
||||
for x0 in (0..ow).step_by(TILE) {
|
||||
let x1 = (x0 + TILE).min(ow);
|
||||
for r in 0..rows {
|
||||
let y2 = (b * TILE + r) as isize;
|
||||
for (x2, d) in (x0..x1).zip(chunk[(r * ow + x0) * 3..(r * ow + x1) * 3].as_chunks_mut::<3>().0) {
|
||||
*d = px[(base + x2 as isize * sx + y2 * sy) as usize];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
let band = par::band_rows(ow);
|
||||
par::chunks_mut(&mut out, band * ow * 3, |b, chunk| {
|
||||
for (r, row) in chunk.chunks_exact_mut(ow * 3).enumerate() {
|
||||
let y2 = b * band + r;
|
||||
for x2 in 0..ow {
|
||||
let (x, y) = match o {
|
||||
2 => (w - 1 - x2, y2),
|
||||
3 => (w - 1 - x2, h - 1 - y2),
|
||||
_ => (x2, h - 1 - y2),
|
||||
};
|
||||
let i = (y * w + x) * 3;
|
||||
row[x2 * 3..x2 * 3 + 3].copy_from_slice(&src[i..i + 3]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
(out, ow, oh)
|
||||
}
|
||||
|
||||
@@ -1000,6 +1081,64 @@ mod tests {
|
||||
assert!(near(GammaLut::with_curve(&[]).encode(0.25), code(0.25)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linear_fill_gives_the_bits_of_the_per_sample_scale() {
|
||||
// 3-sample and monochrome frames, black patterns, deltas, a table, an offset crop.
|
||||
for samples in [1usize, 3] {
|
||||
for (rows, cols, deltas) in [(1, 1, false), (2, 2, false), (1, 3, false), (2, 1, true), (1, 17, false), (4, 4, false)] {
|
||||
for table in [false, true] {
|
||||
let (w, h) = (53, 9);
|
||||
let mut s = test_sensor(w, h, None, samples);
|
||||
s.active = crate::Rect::new(2, 1, w - 2, h - 1);
|
||||
s.black.rows = rows;
|
||||
s.black.cols = cols;
|
||||
s.black.values = (0..rows * cols * samples).map(|i| 40.0 + (i * 7 % 23) as f32 + 0.25 * (i % 3) as f32).collect();
|
||||
if deltas {
|
||||
s.black.delta_h = (0..w).map(|i| (i % 5) as f32).collect();
|
||||
s.black.delta_v = (0..h).map(|i| 0.5 * i as f32).collect();
|
||||
}
|
||||
s.white = [4000.0, 3900.5, 0.5];
|
||||
if table {
|
||||
s.linearization = Some((0..3000u32).map(|i| (i * 3 / 2) as u16).collect());
|
||||
}
|
||||
let sc = Scale::new(&s);
|
||||
let mult = [1.3f32, 0.9, f32::INFINITY];
|
||||
for x0 in [0usize, 2, 5] {
|
||||
let n = w - x0 - 1;
|
||||
for y in 0..h {
|
||||
let mut got = vec![0.0f32; n * 3];
|
||||
fill_linear_row(&sc, &mult, x0, y, &mut got);
|
||||
let want: Vec<f32> =
|
||||
(0..n * 3).map(|j| (sc.value(x0 + j / 3, y, (j % 3).min(samples - 1)) * mult[j % 3]).clamp(0.0, 1.0)).collect();
|
||||
assert!(
|
||||
got.iter().zip(&want).all(|(a, b)| a.to_bits() == b.to_bits() || (a.is_nan() && b.is_nan())),
|
||||
"{samples} samples, black {rows}x{cols}, table {table}, x0 {x0}, row {y}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_orientation_moves_each_pixel_to_its_source_position() {
|
||||
// Sizes around the tile side (and 1-pixel strips), all eight orientations.
|
||||
for (w, h) in [(1, 1), (1, 70), (70, 1), (31, 33), (32, 32), (33, 31), (65, 97), (100, 37)] {
|
||||
let src: Vec<u16> = (0..w * h).flat_map(|i| [i as u16, (i >> 4) as u16 ^ 0x5a5a, !(i as u16)]).collect();
|
||||
for o in 1..=8 {
|
||||
let (out, ow, oh) = orient(src.clone(), w, h, o);
|
||||
assert_eq!((ow, oh), if o >= 5 { (h, w) } else { (w, h) });
|
||||
for y2 in 0..oh {
|
||||
for x2 in 0..ow {
|
||||
let (x, y) = source_xy(o, w, h, x2, y2);
|
||||
assert_eq!(out[(y2 * ow + x2) * 3..][..3], src[(y * w + x) * 3..][..3], "{w}x{h} orientation {o} at {x2},{y2}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn orientations() {
|
||||
// 3×2 image, pixel value = index.
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
//! image area (the D800E, the ILCE-7).
|
||||
//!
|
||||
//! Decoded today: DNG (uncompressed, deflate, lossless-JPEG, lossy-JPEG and JPEG XL, strips and
|
||||
//! tiles, CFA and LinearRaw), CR2 (lossless JPEG with Canon slices), uncompressed or
|
||||
//! tiles, CFA and LinearRaw), CR2 (lossless JPEG with Canon slices, and sRAW / mRAW), uncompressed or
|
||||
//! lossless-JPEG TIFF/EP raws (NEF, ARW, PEF… when not vendor-compressed),
|
||||
//! Nikon compressed NEF (lossless and lossy), Sony compressed ARW (cRAW),
|
||||
//! Panasonic RW2 (RawFormat 5 packed and RawFormat 3/4 vendor-compressed),
|
||||
@@ -65,6 +65,7 @@ mod saturation;
|
||||
mod sensor;
|
||||
mod sony;
|
||||
mod sony_metadata;
|
||||
mod sraw;
|
||||
mod tiff;
|
||||
mod tiffep;
|
||||
mod tone;
|
||||
|
||||
+183
-10
@@ -8,8 +8,10 @@
|
||||
//!
|
||||
//! Output is the sample sequence in raster order: `height` rows of `width ×
|
||||
//! components` interleaved samples. Only one interleaved scan holding every
|
||||
//! component with sampling factors 1×1 is supported, which is how DNG, CR2
|
||||
//! and the TIFF-based raws use it.
|
||||
//! component is supported, which is how DNG, CR2 and the TIFF-based raws use
|
||||
//! it; [`decode`] takes sampling factors 1×1 only. Canon's sRAW / mRAW frames
|
||||
//! (three components, the first sampled 2×1 or 2×2) go through
|
||||
//! [`decode_subsampled`], which follows Canon's prediction rather than T.81's.
|
||||
|
||||
use crate::error::{RawError, Result};
|
||||
|
||||
@@ -20,11 +22,21 @@ pub(crate) struct Frame {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub components: usize,
|
||||
/// Horizontal and vertical sampling factors of the first component (1 and 1 unless the
|
||||
/// frame is subsampled; the other components are then sampled 1×1).
|
||||
pub h: usize,
|
||||
pub v: usize,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
/// Samples in the frame: per MCU, `h × v` of the first component and one of each other.
|
||||
pub fn samples(&self) -> Option<usize> {
|
||||
self.width.checked_mul(self.height)?.checked_mul(self.components)
|
||||
let mcus = (self.width / self.h.max(1)).checked_mul(self.height / self.v.max(1))?;
|
||||
mcus.checked_mul(self.h.checked_mul(self.v)?.checked_add(self.components)?.checked_sub(1)?)
|
||||
}
|
||||
|
||||
pub fn is_subsampled(&self) -> bool {
|
||||
self.h != 1 || self.v != 1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -343,14 +355,19 @@ fn parse(data: &[u8]) -> Result<Parsed<'_>> {
|
||||
return Err(RawError::malformed(format!("unsupported SOF3 ({precision}-bit, {width}x{height}, {nf} components)")));
|
||||
}
|
||||
comp_ids.clear();
|
||||
let mut factors = Vec::with_capacity(nf);
|
||||
for c in 0..nf {
|
||||
let s = &seg[6 + 3 * c..9 + 3 * c];
|
||||
if s[1] != 0x11 {
|
||||
return Err(RawError::unsupported("lossless JPEG with subsampled components"));
|
||||
}
|
||||
factors.push(s[1]);
|
||||
comp_ids.push(s[0]);
|
||||
}
|
||||
frame = Some(Frame { precision, width, height, components: nf });
|
||||
// Canon sRAW / mRAW: Y sampled 2×1 or 2×2, Cb and Cr 1×1, whole MCUs only.
|
||||
let (h, v) = match factors.as_slice() {
|
||||
f if f.iter().all(|&s| s == 0x11) => (1, 1),
|
||||
[y @ (0x21 | 0x22), 0x11, 0x11] if width % 2 == 0 && height % usize::from(y & 0x0F) == 0 => (2, usize::from(y & 0x0F)),
|
||||
_ => return Err(RawError::unsupported(format!("lossless JPEG with subsampled components {factors:02x?} ({width}x{height})"))),
|
||||
};
|
||||
frame = Some(Frame { precision, width, height, components: nf, h, v });
|
||||
}
|
||||
0xC0..=0xC2 => return Err(RawError::unsupported("lossy (DCT) JPEG compressed raw data")),
|
||||
0xC5..=0xC7 | 0xC9..=0xCB | 0xCD..=0xCF => return Err(RawError::unsupported("hierarchical or arithmetic-coded JPEG")),
|
||||
@@ -422,6 +439,9 @@ fn parse(data: &[u8]) -> Result<Parsed<'_>> {
|
||||
pub(crate) fn decode(data: &[u8], max_samples: usize) -> Result<(Frame, Vec<u16>)> {
|
||||
let p = parse(data)?;
|
||||
let f = p.frame;
|
||||
if f.is_subsampled() {
|
||||
return Err(RawError::unsupported(format!("lossless JPEG with subsampled components ({}x{} luma)", f.h, f.v)));
|
||||
}
|
||||
let total = f
|
||||
.samples()
|
||||
.filter(|&n| n <= max_samples)
|
||||
@@ -531,10 +551,100 @@ pub(crate) fn decode(data: &[u8], max_samples: usize) -> Result<(Frame, Vec<u16>
|
||||
Ok((f, out))
|
||||
}
|
||||
|
||||
/// Decodes a subsampled frame (Canon sRAW / mRAW: Y sampled 2×1 or 2×2, Cb and Cr 1×1, see
|
||||
/// [`Frame::h`]) into its sample sequence in decode order: per MCU the `h × v` Y samples
|
||||
/// row-major, then Cb, then Cr; `width / h` MCUs make a row and `height / v` rows the frame.
|
||||
///
|
||||
/// Only predictor 1 is supported, with Canon's rule instead of T.81's per-component sample
|
||||
/// grid: each Y sample is predicted from the Y sample decoded just before it (the previous one
|
||||
/// in the MCU, or the last of the previous MCU), Cb and Cr from those of the previous MCU, and
|
||||
/// the first MCU of a row from the first MCU of the row above (from `2^(P−Pt−1)` on the first
|
||||
/// row of a restart interval).
|
||||
pub(crate) fn decode_subsampled(data: &[u8], max_samples: usize) -> Result<(Frame, Vec<u16>)> {
|
||||
let p = parse(data)?;
|
||||
let f = p.frame;
|
||||
let total = f
|
||||
.samples()
|
||||
.filter(|&n| n <= max_samples)
|
||||
.ok_or_else(|| RawError::LimitExceeded(format!("lossless JPEG frame {}x{}x{}", f.width, f.height, f.components)))?;
|
||||
if !f.is_subsampled() || f.components != 3 {
|
||||
return Err(RawError::malformed("lossless JPEG frame is not subsampled YCbCr"));
|
||||
}
|
||||
if p.predictor != 1 {
|
||||
return Err(RawError::unsupported(format!("subsampled lossless JPEG with predictor {}", p.predictor)));
|
||||
}
|
||||
if p.entropy.len().saturating_mul(8) < total / 2 {
|
||||
return Err(RawError::malformed("lossless JPEG data is truncated"));
|
||||
}
|
||||
// Each component's table (components may share one).
|
||||
let [ty, tcb, tcr] = match p.comp_table.as_slice() {
|
||||
&[a, b, c] => [&p.tables[a], &p.tables[b], &p.tables[c]],
|
||||
_ => return Err(RawError::malformed("lossless JPEG scan tables")),
|
||||
};
|
||||
let luma = f.h * f.v;
|
||||
let per_mcu = luma + 2;
|
||||
let mcus = f.width / f.h;
|
||||
let row_len = mcus * per_mcu;
|
||||
if row_len == 0 {
|
||||
return Err(RawError::malformed("empty lossless JPEG frame"));
|
||||
}
|
||||
// The restart interval counts MCUs.
|
||||
if p.restart != 0 && p.restart % mcus != 0 {
|
||||
return Err(RawError::unsupported("lossless JPEG restart interval that is not whole rows"));
|
||||
}
|
||||
let rows_per_interval = if p.restart == 0 { usize::MAX } else { p.restart / mcus };
|
||||
let mut out = vec![0u16; total];
|
||||
let mut bits = Bits::new(p.entropy);
|
||||
let initial = 1i32 << (f.precision - p.point_transform - 1);
|
||||
// First Y, Cb and Cr of the row above: the first MCU's predictors.
|
||||
let mut top = [initial; 3];
|
||||
let mut interval_row = 0usize;
|
||||
for row in out.chunks_exact_mut(row_len) {
|
||||
if interval_row == rows_per_interval {
|
||||
bits.restart()?;
|
||||
interval_row = 0;
|
||||
}
|
||||
if interval_row == 0 {
|
||||
top = [initial; 3];
|
||||
}
|
||||
let (mut y, mut cb, mut cr) = (top[0], top[1], top[2]);
|
||||
for (m, mcu) in row.chunks_exact_mut(per_mcu).enumerate() {
|
||||
let (ys, c) = mcu.split_at_mut(luma);
|
||||
for (k, s) in ys.iter_mut().enumerate() {
|
||||
// The first Y of the row comes from the row above, every other from the previous Y.
|
||||
let pred = if m == 0 && k == 0 { top[0] } else { y };
|
||||
*s = (pred + bits.diff(ty)?) as u16;
|
||||
y = i32::from(*s);
|
||||
if m == 0 && k == 0 {
|
||||
top[0] = y;
|
||||
}
|
||||
}
|
||||
if let [scb, scr] = c {
|
||||
*scb = (cb + bits.diff(tcb)?) as u16;
|
||||
*scr = (cr + bits.diff(tcr)?) as u16;
|
||||
(cb, cr) = (i32::from(*scb), i32::from(*scr));
|
||||
}
|
||||
if m == 0 {
|
||||
(top[1], top[2]) = (cb, cr);
|
||||
}
|
||||
}
|
||||
interval_row += 1;
|
||||
if bits.fake > 64 {
|
||||
return Err(RawError::malformed("lossless JPEG data is truncated"));
|
||||
}
|
||||
}
|
||||
if p.point_transform > 0 {
|
||||
for v in &mut out {
|
||||
*v <<= p.point_transform;
|
||||
}
|
||||
}
|
||||
Ok((f, out))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::testgen::{lj92_encode, lj92_encode_tables};
|
||||
use crate::testgen::{lj92_encode, lj92_encode_sraw, lj92_encode_sraw_tables, lj92_encode_tables};
|
||||
|
||||
/// Deterministic pseudo-random samples with some structure (smooth + noise).
|
||||
fn samples(n: usize, precision: u8, seed: u64) -> Vec<u16> {
|
||||
@@ -559,7 +669,7 @@ mod tests {
|
||||
let v = samples(w * h * comps, precision, u64::from(predictor) * 31 + comps as u64);
|
||||
let j = lj92_encode(&v, w, h, comps, precision, predictor, None);
|
||||
let (f, out) = decode(&j, usize::MAX).unwrap();
|
||||
assert_eq!(f, Frame { precision, width: w, height: h, components: comps });
|
||||
assert_eq!(f, Frame { precision, width: w, height: h, components: comps, h: 1, v: 1 });
|
||||
assert_eq!(out, v, "P={precision} pred={predictor} comps={comps}");
|
||||
}
|
||||
}
|
||||
@@ -591,7 +701,7 @@ mod tests {
|
||||
let v = samples(w * h * comps, 14, u64::from(predictor) * 7 + comps as u64);
|
||||
let j = lj92_encode_tables(&v, w, h, table_of, 14, predictor, restart_rows);
|
||||
let (f, out) = decode(&j, usize::MAX).unwrap();
|
||||
assert_eq!(f, Frame { precision: 14, width: w, height: h, components: comps });
|
||||
assert_eq!(f, Frame { precision: 14, width: w, height: h, components: comps, h: 1, v: 1 });
|
||||
assert_eq!(out, v, "tables={table_of:?} pred={predictor} restart={restart_rows:?}");
|
||||
}
|
||||
}
|
||||
@@ -663,4 +773,67 @@ mod tests {
|
||||
flooded.extend_from_slice(&j[2..]); // the rest of the valid stream
|
||||
assert!(matches!(decode(&flooded, usize::MAX), Err(RawError::Malformed(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subsampled_roundtrip() {
|
||||
for (h, v) in [(2usize, 2usize), (2, 1)] {
|
||||
let (w, ht) = (14, 6);
|
||||
let n = (w / h) * (ht / v) * (h * v + 2);
|
||||
for precision in [12u8, 15, 16] {
|
||||
for restart in [None, Some(1), Some(ht / v)] {
|
||||
let seq = samples(n, precision, h as u64 * 7 + v as u64);
|
||||
let j = lj92_encode_sraw(&seq, w, ht, h, v, precision, restart);
|
||||
let f = frame(&j).unwrap();
|
||||
assert_eq!((f.h, f.v, f.samples()), (h, v, Some(n)));
|
||||
let (_, out) = decode_subsampled(&j, usize::MAX).unwrap();
|
||||
assert_eq!(out, seq, "{h}x{v} P={precision} restart {restart:?}");
|
||||
// The plain decoder refuses subsampled frames instead of mislaying them.
|
||||
assert!(matches!(decode(&j, usize::MAX), Err(RawError::Unsupported(_))));
|
||||
}
|
||||
}
|
||||
}
|
||||
// Extreme values exercise the 16-bit and 32768 difference categories.
|
||||
let seq: Vec<u16> = (0..7 * 3 * 6).map(|i| if (i / 5) % 2 == 0 { 0 } else { 65535 }).collect();
|
||||
let j = lj92_encode_sraw(&seq, 14, 6, 2, 2, 16, Some(1));
|
||||
assert_eq!(decode_subsampled(&j, usize::MAX).unwrap().1, seq);
|
||||
// Y and the chroma on separate tables, or each its own: the scan maps every component
|
||||
// to its table even when components share one.
|
||||
for table_of in [[0, 1, 1], [0, 1, 2], [1, 0, 0]] {
|
||||
let seq = samples(7 * 3 * 6, 15, 5);
|
||||
let j = lj92_encode_sraw_tables(&seq, 14, 6, (2, 2), table_of, 15, Some(1));
|
||||
assert_eq!(decode_subsampled(&j, usize::MAX).unwrap().1, seq, "tables {table_of:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subsampled_hostile_streams_error_without_panicking() {
|
||||
let seq = samples(7 * 3 * 6, 15, 3);
|
||||
let j = lj92_encode_sraw(&seq, 14, 6, 2, 2, 15, Some(1));
|
||||
for n in 0..j.len() {
|
||||
let _ = decode_subsampled(&j[..n], usize::MAX);
|
||||
}
|
||||
let mut s = 0x0123_4567u64;
|
||||
for _ in 0..2000 {
|
||||
let mut b = j.clone();
|
||||
for _ in 0..4 {
|
||||
s = s.wrapping_mul(6364136223846793005).wrapping_add(1);
|
||||
let at = (s >> 33) as usize % b.len();
|
||||
b[at] = (s >> 20) as u8;
|
||||
}
|
||||
let _ = decode_subsampled(&b, 1 << 20);
|
||||
}
|
||||
assert!(matches!(decode_subsampled(&j, 10), Err(RawError::LimitExceeded(_))));
|
||||
// A frame that is not subsampled, or another predictor, is not Canon sRAW.
|
||||
let plain = lj92_encode(&samples(14 * 6 * 3, 15, 1), 14, 6, 3, 15, 1, None);
|
||||
assert!(decode_subsampled(&plain, usize::MAX).is_err());
|
||||
let mut p6 = j.clone();
|
||||
let sos = p6.windows(2).position(|w| w == [0xFF, 0xDA]).unwrap();
|
||||
p6[sos + 11] = 6; // Ss (predictor) after Ns and three component selectors
|
||||
assert!(matches!(decode_subsampled(&p6, usize::MAX), Err(RawError::Unsupported(_))));
|
||||
// Sampling layouts other than Canon's are refused at the frame header.
|
||||
let mut odd = j.clone();
|
||||
let sof = odd.windows(2).position(|w| w == [0xFF, 0xC3]).unwrap();
|
||||
odd[sof + 14] = 0x21; // Cb subsampled too
|
||||
assert!(matches!(frame(&odd), Err(RawError::Unsupported(_))));
|
||||
}
|
||||
}
|
||||
|
||||
+145
-11
@@ -19,7 +19,8 @@
|
||||
//! `crates/raw/src/vendor/nefc.rs`): recovered from CC0 raw.pixls.us samples
|
||||
//! shot in both compressed and uncompressed mode, and verified on ~45 files
|
||||
//! from ~35 bodies (D40 … D850, Df, Z 6, Z 50, 1 J1). Only the format facts
|
||||
//! and the tables were taken over; this module is written against this
|
||||
//! and the tables were taken over (and, for "lossy after split" below, the
|
||||
//! dequantisation function); this module is written against this
|
||||
//! crate's own TIFF reader.
|
||||
//!
|
||||
//! Format:
|
||||
@@ -38,9 +39,23 @@
|
||||
//! samples), not the sensor bit depth; see Bill Claff's NEF Compression tables.
|
||||
//! * Values in the maker note are in the maker note's byte order.
|
||||
//!
|
||||
//! * "Lossy after split" (type 2 only, a non-zero split row at `0x0096` offset
|
||||
//! 562): rows above the split row are Huffman coded as above. Row `split`
|
||||
//! starts at the very next bit; from there every pixel is one fixed-rate
|
||||
//! word of `bits - 4` bits (8 at 12 bit, 10 at 14 bit), MSB first, with no
|
||||
//! row padding: a prefix code (every word but all-ones) for the category `n`,
|
||||
//! followed by the top bits of the T.81 additional bits. The difference is
|
||||
//! the centre of the magnitude bin that was kept, the predictor is the same
|
||||
//! as above, and the running value is not clamped. The section fills the
|
||||
//! strip to its last byte: whole rows (up to 4 more than the image), then
|
||||
//! 0–7 one-bits of padding. These facts, the prefix
|
||||
//! tables and the dequantisation (`fixed_words`, ported from LightCraft's
|
||||
//! `fixed_word_table`) come from LightCraft's black-box analysis of 11 CC0
|
||||
//! raw.pixls.us files (storytold/lightcraft#633), like the Huffman tables above.
|
||||
//!
|
||||
//! Not decoded (reported as unsupported; the embedded preview is used
|
||||
//! instead): "lossy after split" files (non-zero split row), other `0x0096`
|
||||
//! versions and bit depths other than 12 / 14.
|
||||
//! instead): "lossy after split" strips that fail to decode, other
|
||||
//! `0x0096` versions and bit depths other than 12 / 14.
|
||||
|
||||
use crate::Limits;
|
||||
use crate::error::{RawError, Result};
|
||||
@@ -238,6 +253,9 @@ pub(crate) struct DecodeTable {
|
||||
pub encoding: Encoding,
|
||||
/// Predictor seeds `[row parity][column]`.
|
||||
pub seeds: [[i32; 2]; 2],
|
||||
/// First row of the fixed-rate section ("lossy after split"); 0 when the whole strip is
|
||||
/// Huffman coded.
|
||||
pub split: usize,
|
||||
}
|
||||
|
||||
/// Parses maker note `0x0096` for a `bits`-bit image; `le` is the maker
|
||||
@@ -253,6 +271,7 @@ pub(crate) fn parse_table(t: &[u8], le: bool, bits: u32) -> Result<DecodeTable>
|
||||
}
|
||||
let (Some(&v0), Some(&v1)) = (t.first(), t.get(1)) else { return Err(short()) };
|
||||
let mut seeds = [[0i32; 2]; 2];
|
||||
let mut split = 0;
|
||||
for (i, s) in seeds.iter_mut().flatten().enumerate() {
|
||||
*s = i32::from(u16_at(2 + 2 * i).ok_or_else(short)?);
|
||||
}
|
||||
@@ -267,9 +286,11 @@ pub(crate) fn parse_table(t: &[u8], le: bool, bits: u32) -> Result<DecodeTable>
|
||||
if v1 == 0x10 {
|
||||
Encoding::Lossy(points)
|
||||
} else {
|
||||
let split = u16_at(562).unwrap_or(0);
|
||||
if split != 0 {
|
||||
return Err(RawError::unsupported(format!("Nikon \"lossy after split\" compressed NEF (split at row {split})")));
|
||||
split = usize::from(u16_at(562).unwrap_or(0));
|
||||
// Bytes 12.. hold the curve; with more than 275 points it covers byte 562, so a
|
||||
// non-zero value there is not a split row: refused as before (preview fallback).
|
||||
if split != 0 && 12 + 2 * n > 562 {
|
||||
return Err(RawError::unsupported(format!("Nikon compressed NEF: {n}-point curve overlapping the split row field")));
|
||||
}
|
||||
// The modern lossy curve has a smaller *encoded* domain while
|
||||
// its output remains in sensor ADUs. Using 2^bits stretches the
|
||||
@@ -292,11 +313,85 @@ pub(crate) fn parse_table(t: &[u8], le: bool, bits: u32) -> Result<DecodeTable>
|
||||
}
|
||||
_ => return Err(RawError::unsupported(format!("Nikon compressed NEF version {v0:#04x} {v1:#04x}"))),
|
||||
};
|
||||
Ok(DecodeTable { encoding, seeds })
|
||||
Ok(DecodeTable { encoding, seeds, split })
|
||||
}
|
||||
|
||||
/// Difference-category prefix codes of the fixed-rate ("lossy after split") section:
|
||||
/// `(prefix bits, prefix length, category n)`. A word of `bits - 4` bits is a prefix followed by
|
||||
/// the top `bits - 4 - length` additional bits of category `n` (storytold/lightcraft#633; the
|
||||
/// 14-bit `010` prefix never occurred in the analysed files, its category 12 is deduced).
|
||||
const FIXED_12: &[(u32, u32, u32)] = &[
|
||||
(0b00, 2, 9),
|
||||
(0b010, 3, 10),
|
||||
(0b011, 3, 8),
|
||||
(0b100, 3, 7),
|
||||
(0b101, 3, 6),
|
||||
(0b110, 3, 5),
|
||||
(0b1110, 4, 4),
|
||||
(0b11110, 5, 3),
|
||||
(0b111110, 6, 2),
|
||||
(0b1111110, 7, 1),
|
||||
(0b11111110, 8, 0),
|
||||
];
|
||||
const FIXED_14: &[(u32, u32, u32)] = &[
|
||||
(0b00, 2, 8),
|
||||
(0b010, 3, 12),
|
||||
(0b011, 3, 11),
|
||||
(0b100, 3, 10),
|
||||
(0b101, 3, 9),
|
||||
(0b110, 3, 7),
|
||||
(0b1110, 4, 6),
|
||||
(0b11110, 5, 5),
|
||||
(0b111110, 6, 4),
|
||||
(0b1111110, 7, 3),
|
||||
(0b11111110, 8, 2),
|
||||
(0b111111110, 9, 1),
|
||||
(0b1111111110, 10, 0),
|
||||
];
|
||||
|
||||
/// Fixed-rate word → dequantised difference, for `bits`-bit samples; `None` for the all-ones
|
||||
/// word, which no prefix covers.
|
||||
fn fixed_words(bits: u32) -> Vec<Option<i32>> {
|
||||
let (w, codes) = if bits == 12 { (8, FIXED_12) } else { (10, FIXED_14) };
|
||||
let mut lut = vec![None; 1 << w];
|
||||
for &(prefix, len, n) in codes {
|
||||
let kept = w - len;
|
||||
// `dropped` low bits of the T.81 additional bits are not stored: the word gives the
|
||||
// bin, the difference is its centre.
|
||||
let dropped = n.saturating_sub(kept);
|
||||
let half = (1i32 << dropped) >> 1;
|
||||
for m in 0..1u32 << kept {
|
||||
let extra = (m << dropped) as i32;
|
||||
let d = if n == 0 {
|
||||
0
|
||||
} else if extra >= 1 << (n - 1) {
|
||||
extra + half
|
||||
} else if dropped > 0 {
|
||||
extra + half - (1 << n)
|
||||
} else {
|
||||
extra - ((1 << n) - 1)
|
||||
};
|
||||
if let Some(slot) = lut.get_mut(((prefix << kept) | m) as usize) {
|
||||
*slot = Some(d);
|
||||
}
|
||||
}
|
||||
}
|
||||
lut
|
||||
}
|
||||
|
||||
/// Decodes a `w × h` Nikon Huffman-compressed strip with `bits`-bit samples.
|
||||
///
|
||||
/// A "lossy after split" strip that fails to decode for any reason is reported as unsupported,
|
||||
/// not malformed: before these files were decoded, every one was refused as unsupported and
|
||||
/// opened as the camera's embedded preview, and that fallback stays for files the rule does not fit.
|
||||
pub(crate) fn decode(src: &[u8], w: usize, h: usize, bits: u32, table: &DecodeTable) -> Result<Vec<u16>> {
|
||||
match decode_strip(src, w, h, bits, table) {
|
||||
Err(RawError::Malformed(m)) if table.split != 0 => Err(RawError::unsupported(format!("Nikon \"lossy after split\" NEF that does not decode: {m}"))),
|
||||
r => r,
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_strip(src: &[u8], w: usize, h: usize, bits: u32, table: &DecodeTable) -> Result<Vec<u16>> {
|
||||
if bits != 12 && bits != 14 {
|
||||
return Err(RawError::unsupported(format!("Nikon compressed NEF with {bits}-bit samples")));
|
||||
}
|
||||
@@ -316,6 +411,14 @@ pub(crate) fn decode(src: &[u8], w: usize, h: usize, bits: u32, table: &DecodeTa
|
||||
Encoding::Lossless => (&[], (1i32 << bits) - 1),
|
||||
Encoding::Lossy(curve) => (curve, i32::try_from(curve.len()).unwrap_or(i32::MAX) - 1),
|
||||
};
|
||||
if table.split != 0 && table.split >= h {
|
||||
return Err(RawError::malformed(format!("NEF: split row {} for a {h}-row image", table.split)));
|
||||
}
|
||||
if table.split != 0 && !matches!(table.encoding, Encoding::Lossy(_)) {
|
||||
return Err(RawError::malformed("NEF: split row with a lossless table"));
|
||||
}
|
||||
let fixed = if table.split != 0 { fixed_words(bits) } else { Vec::new() };
|
||||
let word = bits - 4;
|
||||
let mut out = vec![0u16; n];
|
||||
let mut stream = Bits::new(src);
|
||||
let mut vpred = table.seeds;
|
||||
@@ -323,8 +426,14 @@ pub(crate) fn decode(src: &[u8], w: usize, h: usize, bits: u32, table: &DecodeTa
|
||||
for (y, row) in out.chunks_exact_mut(w).enumerate() {
|
||||
let seeds = &mut vpred[y & 1];
|
||||
let mut hpred = [0i32; 2];
|
||||
let fixed_rate = table.split != 0 && y >= table.split;
|
||||
for (x, o) in row.iter_mut().enumerate() {
|
||||
let d = diff(&mut stream, &huff).ok_or_else(|| RawError::malformed(format!("NEF: invalid Huffman code in row {y}")))?;
|
||||
let d = if fixed_rate {
|
||||
let code = stream.take(word) as usize;
|
||||
fixed.get(code).copied().flatten().ok_or_else(|| RawError::malformed(format!("NEF: unknown fixed-rate word in row {y}")))?
|
||||
} else {
|
||||
diff(&mut stream, &huff).ok_or_else(|| RawError::malformed(format!("NEF: invalid Huffman code in row {y}")))?
|
||||
};
|
||||
let p = if x < 2 {
|
||||
seeds[x] = seeds[x].saturating_add(d);
|
||||
hpred[x] = seeds[x];
|
||||
@@ -334,15 +443,29 @@ pub(crate) fn decode(src: &[u8], w: usize, h: usize, bits: u32, table: &DecodeTa
|
||||
hpred[x & 1]
|
||||
};
|
||||
let v = p.clamp(0, max);
|
||||
clipped += usize::from(v != p);
|
||||
// The fixed-rate section's running value is not clamped and may leave the range.
|
||||
clipped += usize::from(v != p && !fixed_rate);
|
||||
*o = if lut.is_empty() { v as u16 } else { lut.get(v as usize).copied().unwrap_or(0) };
|
||||
}
|
||||
// The camera pads the strip; reading well past its end means the data
|
||||
// is truncated or corrupt.
|
||||
if stream.consumed > src.len() * 8 + 64 {
|
||||
if stream.consumed > src.len().saturating_mul(8).saturating_add(64) {
|
||||
return Err(RawError::malformed(format!("NEF: compressed data ends in row {y} of {h}")));
|
||||
}
|
||||
}
|
||||
// The fixed-rate section fills the rest of the strip: whole rows of `w` words, then 0–7
|
||||
// one-bits up to the byte boundary (a strip may hold up to 4 rows more than the image). This
|
||||
// checks the section's length, not its content: a strip that ends any other way does not
|
||||
// follow the rule and is refused, but a wrong word inside a well-formed one is not detected.
|
||||
if table.split != 0 {
|
||||
let left = src.len().saturating_mul(8).checked_sub(stream.consumed);
|
||||
let row_bits = w.saturating_mul(word as usize).max(1);
|
||||
let pad = left.map(|l| l % row_bits);
|
||||
let ones = |k: usize| src.last().is_some_and(|b| u32::from(*b) & ((1 << k) - 1) == (1 << k) - 1);
|
||||
if !left.is_some_and(|l| l / row_bits <= 4) || !pad.is_some_and(|k| k < 8 && ones(k)) {
|
||||
return Err(RawError::malformed(format!("NEF: the fixed-rate section from row {} does not end the strip", table.split)));
|
||||
}
|
||||
}
|
||||
// A wrong table or a corrupt stream drifts out of range quickly; real
|
||||
// files stay inside (a handful of edge pixels).
|
||||
if clipped > n / 1000 + 16 {
|
||||
@@ -365,6 +488,17 @@ pub(crate) fn read_compressed(t: &Tiff, raw: &Ifd, note: Option<&(Tiff, Ifd)>, l
|
||||
.and_then(|(n, m)| Some((n.raw(m.get(NIKON_LINEARIZATION_TABLE)?)?, n.le)))
|
||||
.ok_or_else(|| RawError::unsupported("Nikon compressed NEF without a linearization table (maker note 0x96)"))?;
|
||||
let table = parse_table(table.0, table.1, bits)?;
|
||||
match read_strip(t, raw, width, height, bits, &table, limits) {
|
||||
// Until split files were decoded, each was refused as unsupported before the image was
|
||||
// read, and opened as the camera's preview: any failure after that point keeps it so.
|
||||
Err(e @ (RawError::Malformed(_) | RawError::LimitExceeded(_))) if table.split != 0 => {
|
||||
Err(RawError::unsupported(format!("Nikon \"lossy after split\" NEF that does not decode: {e}")))
|
||||
}
|
||||
r => r,
|
||||
}
|
||||
}
|
||||
|
||||
fn read_strip(t: &Tiff, raw: &Ifd, width: usize, height: usize, bits: u32, table: &DecodeTable, limits: &Limits) -> Result<Plane> {
|
||||
limits.check(width as u64, height as u64, 2)?;
|
||||
// One strip in every sample; several would be consecutive parts of the
|
||||
// same bit stream.
|
||||
@@ -376,7 +510,7 @@ pub(crate) fn read_compressed(t: &Tiff, raw: &Ifd, note: Option<&(Tiff, Ifd)>, l
|
||||
let start = first as usize;
|
||||
let end = (last as usize).saturating_add(last_len as usize).min(t.data.len());
|
||||
let src = t.data.get(start..end).filter(|s| !s.is_empty()).ok_or_else(|| RawError::malformed("raw data lies outside the file"))?;
|
||||
let data = decode(src, width, height, bits, &table)?;
|
||||
let data = decode(src, width, height, bits, table)?;
|
||||
Ok(Plane { width, height, samples: 1, bits, data, float_scale: None })
|
||||
}
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ fn image(w: usize, h: usize, max: i32, seed: u32) -> Vec<i32> {
|
||||
|
||||
fn lossless(bits: u32) -> DecodeTable {
|
||||
let s = 1 << (bits - 3);
|
||||
DecodeTable { encoding: Encoding::Lossless, seeds: [[s, s], [s, s]] }
|
||||
DecodeTable { encoding: Encoding::Lossless, seeds: [[s, s], [s, s]], split: 0 }
|
||||
}
|
||||
|
||||
fn lossy_table(bits: u32, le: bool) -> Vec<u8> {
|
||||
@@ -165,7 +165,7 @@ fn unsupported_and_malformed_tables() {
|
||||
let malformed = |r: Result<DecodeTable>| matches!(r, Err(RawError::Malformed(_)));
|
||||
let mut split = lossy_table(12, false);
|
||||
split[562..564].copy_from_slice(&345u16.to_be_bytes());
|
||||
assert!(unsupported(parse_table(&split, false, 12)));
|
||||
assert_eq!(parse_table(&split, false, 12).unwrap().split, 345);
|
||||
assert!(unsupported(parse_table(&[0x49, 0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], false, 12)));
|
||||
assert!(unsupported(parse_table(&lossy_table(12, false), false, 16)));
|
||||
assert!(malformed(parse_table(&[], false, 12)));
|
||||
@@ -244,7 +244,8 @@ fn decodes_through_the_nef_container() {
|
||||
assert_eq!(s.data, img.iter().map(|&v| curve[v as usize]).collect::<Vec<_>>());
|
||||
assert_eq!(s.black.values, vec![150.0; 4]);
|
||||
assert!(crate::develop_sensor(&s, &crate::DevelopOptions::default()).is_ok());
|
||||
// "lossy after split" and a missing table stay unsupported (preview fallback)
|
||||
// a split row over a stream that does not follow the fixed-rate rule, and a missing
|
||||
// table, stay unsupported (preview fallback)
|
||||
let mut split = table;
|
||||
split[562..564].copy_from_slice(&3u16.to_le_bytes());
|
||||
let e = crate::decode(&nef_compressed(w, h, 12, src.clone(), [2, 1, 1, 0], false, vec![(0x0096, Val::Undefined(split))]), &Limits::default());
|
||||
@@ -304,3 +305,277 @@ fn truncated_and_corrupt_streams_error_never_panic() {
|
||||
assert!(decode(&src, usize::MAX, 2, 14, &table).is_err());
|
||||
assert!(decode(&src, 0, 2, 14, &table).is_err());
|
||||
}
|
||||
|
||||
/// Encodes `values` the way a "lossy after split" camera does: Huffman (`t`) for rows above
|
||||
/// `split` (exact differences), then fixed-rate words of `bits - 4` bits, then one-bits up to the
|
||||
/// byte boundary. The fixed-rate words keep only the top bits of large differences, so the
|
||||
/// encoder runs the decoder's own predictor on what it reconstructs (closed loop) and returns the
|
||||
/// strip together with the values the decoder must produce.
|
||||
fn encode_split(values: &[i32], w: usize, t: &Table, seeds: [[i32; 2]; 2], bits: u32, split: usize) -> (Vec<u8>, Vec<i32>) {
|
||||
let codes = codes(t);
|
||||
let (word, fixed) = if bits == 12 { (8, FIXED_12) } else { (10, FIXED_14) };
|
||||
let lut = fixed_words(bits);
|
||||
let mut wr = Writer::default();
|
||||
let mut vpred = seeds;
|
||||
let mut recon = Vec::with_capacity(values.len());
|
||||
for (y, row) in values.chunks(w).enumerate() {
|
||||
let mut hpred = [0i32; 2];
|
||||
for (x, &v) in row.iter().enumerate() {
|
||||
let pred = if x < 2 { vpred[y & 1][x] } else { hpred[x & 1] };
|
||||
let d = v - pred;
|
||||
let k = 32 - d.unsigned_abs().leading_zeros();
|
||||
let extra = if d < 0 { (d + (1 << k) - 1) as u32 } else { d as u32 };
|
||||
let got = if y < split {
|
||||
let (code, len) = codes[k as usize];
|
||||
assert!(len > 0, "no Huffman code for category {k}");
|
||||
wr.put(code, len);
|
||||
wr.put(extra, k);
|
||||
d
|
||||
} else {
|
||||
let &(prefix, len, _) = fixed.iter().find(|c| c.2 == k).unwrap_or_else(|| panic!("no fixed-rate code for category {k}"));
|
||||
let kept = word - len;
|
||||
let field = if k <= kept { extra << (kept - k) } else { extra >> (k - kept) };
|
||||
wr.put(prefix, len);
|
||||
wr.put(field, kept);
|
||||
lut[((prefix << kept) | field) as usize].unwrap()
|
||||
};
|
||||
let r = pred + got;
|
||||
if x < 2 {
|
||||
vpred[y & 1][x] = r;
|
||||
}
|
||||
hpred[x & 1] = r;
|
||||
recon.push(r);
|
||||
}
|
||||
}
|
||||
while wr.n > 0 {
|
||||
wr.put(1, 1);
|
||||
}
|
||||
(wr.out, recon)
|
||||
}
|
||||
|
||||
/// Samples with large jumps (differences up to about ±1000 codes, the largest category both
|
||||
/// lossy Huffman tables and the 12-bit fixed-rate words code) as well as small steps.
|
||||
fn rough(w: usize, h: usize, seed: u32) -> Vec<i32> {
|
||||
let mut s = seed;
|
||||
(0..w * h)
|
||||
.map(|i| {
|
||||
s = s.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
|
||||
let jump = if (s >> 28) < 3 { ((s >> 8) % 900) as i32 - 450 } else { ((s >> 8) % 23) as i32 - 11 };
|
||||
700 + jump + ((i % w) as i32 * 7) % 40
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn split_table(bits: u32, split: usize) -> DecodeTable {
|
||||
let mut table = lossy_table(bits, false);
|
||||
table[562..564].copy_from_slice(&(split as u16).to_be_bytes());
|
||||
let t = parse_table(&table, false, bits).unwrap();
|
||||
assert_eq!(t.split, split);
|
||||
t
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lossy_after_split_known_answers() {
|
||||
for bits in [12u32, 14] {
|
||||
let huff = if bits == 12 { &LOSSY_12 } else { &LOSSY_14 };
|
||||
// Split right after the first row, in the middle and before the last row.
|
||||
for (h, split) in [(7usize, 1usize), (7, 3), (7, 6)] {
|
||||
let w = 24usize;
|
||||
let t = split_table(bits, split);
|
||||
let Encoding::Lossy(curve) = &t.encoding else { panic!() };
|
||||
let max = curve.len() as i32 - 1;
|
||||
// Large jumps: the fixed-rate words drop low bits and decode to bin centres, which
|
||||
// the closed-loop encoder reproduces; the running value may leave 0..=max.
|
||||
let img = rough(w, h, 7 + split as u32);
|
||||
let (src, recon) = encode_split(&img, w, huff, t.seeds, bits, split);
|
||||
let want: Vec<u16> = recon.iter().map(|&r| curve[r.clamp(0, max) as usize]).collect();
|
||||
assert_eq!(decode(&src, w, h, bits, &t).unwrap(), want, "{bits}-bit split {split}");
|
||||
// Some differences really were quantised.
|
||||
assert!(recon.iter().zip(&img).any(|(r, v)| r != v), "{bits}-bit split {split}: nothing quantised");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lossy_after_split_dequantises_to_symmetric_bin_centres() {
|
||||
for bits in [12u32, 14] {
|
||||
let lut = fixed_words(bits);
|
||||
let values: Vec<i32> = lut.iter().flatten().copied().collect();
|
||||
// Every word but the all-ones one codes a difference, and the codes are symmetric.
|
||||
assert_eq!(values.len(), lut.len() - 1, "{bits}-bit");
|
||||
assert!(lut.last().unwrap().is_none());
|
||||
for v in &values {
|
||||
assert!(values.contains(&-v), "{bits}-bit: {v} has no negative twin");
|
||||
}
|
||||
// Small differences are exact; the largest category (10 at 12 bit, 12 at 14 bit) reaches
|
||||
// past half its range.
|
||||
for d in -31..=31 {
|
||||
assert!(values.contains(&d), "{bits}-bit: {d}");
|
||||
}
|
||||
let top = *values.iter().max().unwrap();
|
||||
assert!(top >= if bits == 12 { 1 << 9 } else { 1 << 11 }, "{bits}-bit: {top}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_files_that_break_the_rule_fall_back_to_the_preview() {
|
||||
let unsupported = |r: Result<Vec<u16>>| matches!(r, Err(RawError::Unsupported(_)));
|
||||
for bits in [12u32, 14] {
|
||||
let huff = if bits == 12 { &LOSSY_12 } else { &LOSSY_14 };
|
||||
let (w, h, split) = (24usize, 7usize, 3usize);
|
||||
let t = split_table(bits, split);
|
||||
let (src, _) = encode_split(&gentle(w, h, 600), w, huff, t.seeds, bits, split);
|
||||
assert!(decode(&src, w, h, bits, &t).is_ok());
|
||||
// Trailing data, a truncated strip, a split row past the image, zero padding and the
|
||||
// all-ones word: all unsupported (the embedded preview opens), none a panic.
|
||||
let mut longer = src.clone();
|
||||
longer.extend_from_slice(&[0x12; 9]);
|
||||
assert!(unsupported(decode(&longer, w, h, bits, &t)), "{bits}-bit: trailing data");
|
||||
assert!(unsupported(decode(&src[..src.len() - 12], w, h, bits, &t)), "{bits}-bit: truncated");
|
||||
assert!(unsupported(decode(&src, w, split, bits, &t)), "{bits}-bit: split past the image");
|
||||
let mut zeros = src.clone();
|
||||
let last = zeros.len() - 1;
|
||||
zeros[last] &= 0xf0;
|
||||
assert_ne!(zeros, src, "{bits}-bit: the fixture must end in padding bits");
|
||||
assert!(unsupported(decode(&zeros, w, h, bits, &t)), "{bits}-bit: zero padding");
|
||||
let mut all_ones = src.clone();
|
||||
let fixed_start = all_ones.len() - (w * (h - split) * (bits as usize - 4)).div_ceil(8);
|
||||
all_ones[fixed_start + 2..fixed_start + 6].fill(0xff);
|
||||
assert!(unsupported(decode(&all_ones, w, h, bits, &t)), "{bits}-bit: all-ones word");
|
||||
}
|
||||
// A split row on a lossless table is malformed (no parsed table makes one).
|
||||
let lossless_split = DecodeTable { split: 3, ..lossless(12) };
|
||||
assert!(unsupported(decode(&[0; 64], 24, 7, 12, &lossless_split)));
|
||||
// A curve long enough to cover byte 562 has no split field: refused as before.
|
||||
let mut long = vec![0x44, 0x20];
|
||||
long.extend(std::iter::repeat_n([0u8, 9], 4).flatten());
|
||||
long.extend_from_slice(&513u16.to_be_bytes());
|
||||
long.extend((0..513u32).flat_map(|i| ((i * 7) as u16).to_be_bytes()));
|
||||
assert!(matches!(parse_table(&long, false, 12), Err(RawError::Unsupported(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_nef_decodes_through_the_container() {
|
||||
use crate::testgen::{Val, nef_compressed};
|
||||
use crate::{Limits, decode as decode_raw};
|
||||
let (w, h, split) = (40usize, 6usize, 2usize);
|
||||
let mut table = lossy_table(12, true);
|
||||
table[562..564].copy_from_slice(&(split as u16).to_le_bytes());
|
||||
let t = parse_table(&table, true, 12).unwrap();
|
||||
let Encoding::Lossy(curve) = &t.encoding else { panic!() };
|
||||
let (src, recon) = encode_split(&rough(w, h, 3), w, &LOSSY_12, t.seeds, 12, split);
|
||||
let note = vec![(0x0096, Val::Undefined(table))];
|
||||
let s = decode_raw(&nef_compressed(w, h, 12, src, [2, 1, 1, 0], false, note), &Limits::default()).unwrap();
|
||||
let max = curve.len() as i32 - 1;
|
||||
assert_eq!(s.data, recon.iter().map(|&r| curve[r.clamp(0, max) as usize]).collect::<Vec<_>>());
|
||||
assert!(crate::develop_sensor(&s, &crate::DevelopOptions::default()).is_ok());
|
||||
}
|
||||
|
||||
/// Small steps around a level (every difference below 32), so fixed-rate words keep them whole.
|
||||
fn gentle(w: usize, h: usize, base: i32) -> Vec<i32> {
|
||||
(0..w * h).map(|i| base + ((i % w) as i32 * 3 + (i / w) as i32 * 5) % 13 + if i % 7 == 0 { 7 } else { 0 }).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixed_rate_running_value_is_not_clamped() {
|
||||
// A jump from 900 down to 0 is category 10: its 12-bit word keeps 5 of 10 additional bits
|
||||
// and decodes to -912, so the running value sits at -12. The next same-colour pixel codes +12
|
||||
// exactly and must land on 0 again; clamping the running value would give 12.
|
||||
let (w, h, split) = (12usize, 4usize, 1usize);
|
||||
let t = split_table(12, split);
|
||||
let Encoding::Lossy(curve) = &t.encoding else { panic!() };
|
||||
let img: Vec<i32> = (0..w * h).map(|i| if i % w < 2 { 900 } else { 0 }).collect();
|
||||
let (src, recon) = encode_split(&img, w, &LOSSY_12, t.seeds, 12, split);
|
||||
assert!(recon.iter().any(|&r| r < 0), "{recon:?}");
|
||||
let max = curve.len() as i32 - 1;
|
||||
let want: Vec<u16> = recon.iter().map(|&r| curve[r.clamp(0, max) as usize]).collect();
|
||||
assert_eq!(decode(&src, w, h, 12, &t).unwrap(), want);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixed_rate_words_known_answers() {
|
||||
// Worked by hand from the rule: prefix -> category n; the word keeps the top `kept` of n
|
||||
// additional bits; T.81: a leading 0 bit is a negative difference; the difference is the
|
||||
// centre of the kept bin (low dropped bits = half the bin).
|
||||
let (w12, w14) = (fixed_words(12), fixed_words(14));
|
||||
for (word, want) in [
|
||||
(0b1111_1110, Some(0)), // category 0
|
||||
(0b1110_0000, Some(-15)), // category 4, all 4 bits kept: 0000 = -15
|
||||
(0b1110_1111, Some(15)), // 1111 = +15
|
||||
(0b101_00000, Some(-63)), // category 6, 5 of 6 bits kept: bin -63..-62, centre -63
|
||||
(0b101_10000, Some(33)), // bin 32..33, centre 33
|
||||
(0b010_00011, Some(-912)), // category 10, 5 of 10 bits: 00011xxxxx = 96..127 -> -927..-896
|
||||
(0b010_11111, Some(1008)), // 11111xxxxx = 992..1023, centre 1008
|
||||
(0b1111_1111, None), // the all-ones word codes nothing
|
||||
] {
|
||||
assert_eq!(w12[word], want, "12-bit word {word:#010b}");
|
||||
}
|
||||
for (word, want) in [
|
||||
(0b11_1111_1110, Some(0)), // category 0
|
||||
(0b010_0000000, Some(-4080)), // category 12, 7 of 12 bits: -4095..-4064, centre -4080
|
||||
(0b010_1111111, Some(4080)), // 4064..4095, centre 4080
|
||||
(0b011_1000000, Some(1032)), // category 11, 7 of 11 bits: 1024..1039, centre 1032
|
||||
(0b00_00000000, Some(-255)), // category 8, all 8 bits kept: 00000000 = -255
|
||||
(0b11_1111_1111, None), // the all-ones word
|
||||
] {
|
||||
assert_eq!(w14[word], want, "14-bit word {word:#012b}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn out_of_range_fixed_rate_values_are_not_counted_as_clipped() {
|
||||
// Many jumps down to 0 leave the running value just below 0 (see the test above), far more
|
||||
// than the 16 + n/1000 out-of-range values tolerated in Huffman rows. They are legitimate in
|
||||
// the fixed-rate section and must not fail the decode.
|
||||
let (w, h, split) = (40usize, 5usize, 1usize);
|
||||
let t = split_table(12, split);
|
||||
let Encoding::Lossy(curve) = &t.encoding else { panic!() };
|
||||
let img: Vec<i32> = (0..w * h).map(|i| if (i % w) % 4 < 2 { 900 } else { 0 }).collect();
|
||||
let (src, recon) = encode_split(&img, w, &LOSSY_12, t.seeds, 12, split);
|
||||
assert!(recon.iter().filter(|&&r| r < 0).count() > 16 + w * h / 1000, "{recon:?}");
|
||||
let max = curve.len() as i32 - 1;
|
||||
assert_eq!(decode(&src, w, h, 12, &t).unwrap(), recon.iter().map(|&r| curve[r.clamp(0, max) as usize]).collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_files_failing_after_the_table_still_fall_back_to_the_preview() {
|
||||
use crate::testgen::{Val, nef_compressed};
|
||||
use crate::{Limits, decode as decode_raw};
|
||||
let (w, h, split) = (40usize, 6usize, 2usize);
|
||||
let mut table = lossy_table(12, true);
|
||||
table[562..564].copy_from_slice(&(split as u16).to_le_bytes());
|
||||
let t = parse_table(&table, true, 12).unwrap();
|
||||
let (src, _) = encode_split(&gentle(w, h, 600), w, &LOSSY_12, t.seeds, 12, split);
|
||||
let note = || vec![(0x0096, Val::Undefined(table.clone()))];
|
||||
// The refusal must come from the split path, not from some other part of the file.
|
||||
let unsupported = |r: Result<crate::Sensor>| matches!(&r, Err(RawError::Unsupported(m)) if m.contains("lossy after split"));
|
||||
// Over the size limits, and with the strip cut off by the end of the file.
|
||||
let small = Limits { max_pixels: 100, ..Limits::default() };
|
||||
assert!(unsupported(decode_raw(&nef_compressed(w, h, 12, src.clone(), [2, 1, 1, 0], false, note()), &small)));
|
||||
let file = nef_compressed(w, h, 12, src, [2, 1, 1, 0], false, note());
|
||||
let cut = file.len() - 40;
|
||||
assert!(unsupported(decode_raw(&file[..cut], &Limits::default())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_strips_with_up_to_four_extra_rows_are_accepted() {
|
||||
// A strip may hold whole fixed-rate rows past the image (one body stores 4022 rows for
|
||||
// 4020): up to 4 such rows decode, the image rows unchanged; a fifth is refused.
|
||||
for bits in [12u32, 14] {
|
||||
let huff = if bits == 12 { &LOSSY_12 } else { &LOSSY_14 };
|
||||
let (w, h, split) = (24usize, 7usize, 3usize);
|
||||
let t = split_table(bits, split);
|
||||
let Encoding::Lossy(curve) = &t.encoding else { panic!() };
|
||||
let max = curve.len() as i32 - 1;
|
||||
for extra in 1..=5 {
|
||||
let (src, recon) = encode_split(&gentle(w, h + extra, 600), w, huff, t.seeds, bits, split);
|
||||
let got = decode(&src, w, h, bits, &t);
|
||||
if extra <= 4 {
|
||||
let want: Vec<u16> = recon[..w * h].iter().map(|&r| curve[r.clamp(0, max) as usize]).collect();
|
||||
assert_eq!(got.unwrap(), want, "{bits}-bit, {extra} extra rows");
|
||||
} else {
|
||||
assert!(matches!(got, Err(RawError::Unsupported(_))), "{bits}-bit, {extra} extra rows: {got:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
//! the stated white level. Unclipped samples keep their values, and a file whose data reaches its white
|
||||
//! level, or has no pile-up, is left alone. Same detector as storytold/lightcraft#586.
|
||||
|
||||
use crate::sensor::Sensor;
|
||||
use crate::sensor::{Cfa, Rect, Sensor};
|
||||
|
||||
/// A saturation plateau must hold at least this share of its colour's samples (and 100 of them), as
|
||||
/// many must lie below it (a scene, not a flat field)...
|
||||
@@ -29,6 +29,51 @@ const NEAR_WHITE: f32 = 0.03;
|
||||
/// How far below the plateau the "average value just below it" reaches.
|
||||
const NEIGHBOURHOOD: usize = 64;
|
||||
|
||||
/// Largest number of row bands counted in parallel: each holds a 768 KiB histogram.
|
||||
const MAX_PARTS: usize = 16;
|
||||
|
||||
/// Per-colour histogram of the samples of `active` (and the sample count per colour). Row bands are
|
||||
/// counted in parallel, each row through one repeat of the CFA colours (one `Cfa::color` per pattern
|
||||
/// column instead of one per sample), and the integer counts are summed, so the result does not depend
|
||||
/// on the number of workers.
|
||||
fn colour_histogram(data: &[u16], stride: usize, a: Rect, cfa: &Cfa) -> (Vec<[u32; 3]>, [u64; 3]) {
|
||||
let parts = crate::par::workers().clamp(1, MAX_PARTS.min(a.height.max(1)));
|
||||
let rows_per = a.height.div_ceil(parts).max(1);
|
||||
let pw = cfa.width.max(1);
|
||||
let partial = crate::par::map(a.height.div_ceil(rows_per), |p| {
|
||||
let mut hist = vec![[0u32; 3]; 65536];
|
||||
let mut pattern = vec![0usize; pw];
|
||||
for y in a.y + p * rows_per..(a.y + (p + 1) * rows_per).min(a.y + a.height) {
|
||||
for (k, c) in pattern.iter_mut().enumerate() {
|
||||
*c = usize::from(cfa.color(a.x + k, y)).min(2);
|
||||
}
|
||||
let row = &data[y * stride + a.x..][..a.width];
|
||||
for run in row.chunks(pw) {
|
||||
for (&v, &c) in run.iter().zip(&pattern) {
|
||||
hist[usize::from(v)][c] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
hist
|
||||
});
|
||||
let mut parts = partial.into_iter();
|
||||
let mut hist = parts.next().unwrap_or_else(|| vec![[0u32; 3]; 65536]);
|
||||
for part in parts {
|
||||
for (h, q) in hist.iter_mut().zip(&part) {
|
||||
for c in 0..3 {
|
||||
h[c] += q[c];
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut total = [0u64; 3];
|
||||
for h in &hist {
|
||||
for c in 0..3 {
|
||||
total[c] += u64::from(h[c]);
|
||||
}
|
||||
}
|
||||
(hist, total)
|
||||
}
|
||||
|
||||
impl Sensor {
|
||||
/// The value each colour's samples saturate at, when that is clearly below the stated white level
|
||||
/// (see the module docs). Only CFA data without a linearization table is examined: with a table the
|
||||
@@ -55,16 +100,7 @@ impl Sensor {
|
||||
if lo + NEIGHBOURHOOD >= hi {
|
||||
return none;
|
||||
}
|
||||
let mut hist = vec![[0u32; 3]; 65536];
|
||||
let mut total = [0u64; 3];
|
||||
for y in a.y..a.y + a.height {
|
||||
let row = &self.data[y * self.width + a.x..][..a.width];
|
||||
for (x, &v) in (a.x..).zip(row) {
|
||||
let c = usize::from(cfa.color(x, y)).min(2);
|
||||
total[c] += 1;
|
||||
hist[usize::from(v)][c] += 1;
|
||||
}
|
||||
}
|
||||
let (hist, total) = colour_histogram(&self.data, self.width, a, cfa);
|
||||
let mut out = none;
|
||||
for c in 0..3 {
|
||||
// the most populated value of the upper half of the range
|
||||
@@ -119,6 +155,7 @@ impl Sensor {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::colour_histogram;
|
||||
use crate::*;
|
||||
|
||||
/// A 64 x 64 RGGB mosaic with a gradient, black 528, white `white`, whose samples above `clip` (when
|
||||
@@ -262,4 +299,34 @@ mod tests {
|
||||
s.data.pop();
|
||||
assert_eq!(s.lift_clipped_samples(), 0);
|
||||
}
|
||||
|
||||
/// The per-row pattern and the parallel bands count exactly what a per-sample `Cfa::color` pass does,
|
||||
/// for offset patterns, a 6 x 6 pattern, an inner active area and any number of bands.
|
||||
#[test]
|
||||
fn histogram_matches_the_per_sample_count() {
|
||||
let (w, h) = (53usize, 47usize);
|
||||
let data: Vec<u16> = (0..w * h).map(|i| ((i * 2654435761) >> 7) as u16 % 4096).collect();
|
||||
let xtrans = Cfa { width: 6, height: 6, colors: (0..36).map(|i| [1, 0, 2, 1, 2, 0][(i * 7 + i / 6) % 6]).collect(), origin_x: 5, origin_y: 2 };
|
||||
let mut offset = Cfa::bayer("GBRG").unwrap();
|
||||
offset.origin_x = 3;
|
||||
offset.origin_y = 1;
|
||||
for cfa in [Cfa::bayer("RGGB").unwrap(), offset, xtrans] {
|
||||
for a in [Rect::new(0, 0, w, h), Rect::new(7, 5, 31, 22), Rect::new(1, 3, 1, 1), Rect::new(2, 2, 40, 1), Rect::new(0, 0, 0, 0)] {
|
||||
let mut hist = vec![[0u32; 3]; 65536];
|
||||
let mut total = [0u64; 3];
|
||||
for y in a.y..a.y + a.height {
|
||||
for x in a.x..a.x + a.width {
|
||||
let c = usize::from(cfa.color(x, y)).min(2);
|
||||
total[c] += 1;
|
||||
hist[usize::from(data[y * w + x])][c] += 1;
|
||||
}
|
||||
}
|
||||
for threads in [1, 2, 3, 8] {
|
||||
let pool = rayon::ThreadPoolBuilder::new().num_threads(threads).build().unwrap();
|
||||
let (got, got_total) = pool.install(|| colour_histogram(&data, w, a, &cfa));
|
||||
assert!(got == hist && got_total == total, "{threads} threads, active {a:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,316 @@
|
||||
//! Canon sRAW / mRAW (CR2 with subsampled YCbCr).
|
||||
//!
|
||||
//! The camera demosaics the sensor data and stores it as Y, Cb, Cr in one lossless JPEG frame
|
||||
//! whose Y component is sampled 2×2 (sRAW1, which Canon calls mRAW on the later bodies) or 2×1
|
||||
//! (sRAW2), sliced like the Bayer CR2 data (tag 0xC640, widths in samples). The decoder
|
||||
//! ([`crate::ljpeg::decode_subsampled`]) gives the sample sequence; [`planes`] lays it out,
|
||||
//! [`camera_rgb`] interpolates the chroma and converts to camera RGB, and the result is
|
||||
//! developed like a three-sample LinearRaw: no CFA, the as-shot white balance, the look fitted
|
||||
//! to the embedded JPEG (`photocraft-io`), tone and exposure as for every other raw.
|
||||
//!
|
||||
//! Observed on EOS 6D Mark II mRAW files (ColorData version 15, 4680 × 3132 data):
|
||||
//! * Y is already black-subtracted (the darkest samples are 0) and on the sensor's scale; Cb and
|
||||
//! Cr are colour differences offset by `2^(P−1)`.
|
||||
//! * The stored RGB is camera RGB divided by per-shot gains, an RGGB entry of ColorData (word
|
||||
//! 0x80 in version 15; its green is 1170, not the 1024 of the white-balance presets) that
|
||||
//! roughly neutralises the scene. Multiplying them back gives camera RGB that matches an
|
||||
//! independent converter's linear camera-RGB output of the same files to a diagonal of
|
||||
//! (1, 1, 1) ± 1.5 % and 0.6 % RMS; the as-shot white balance then applies as for Bayer data.
|
||||
//! * The inverse with the BT.601 luma weights (ITU-R BT.601: Y = 0.299 R + 0.587 G + 0.114 B)
|
||||
//! and unscaled differences (Cb = B − Y, Cr = R − Y) leaves no cross-channel terms above 3 %
|
||||
//! in that comparison.
|
||||
//! * Chroma edges follow the luma edges slightly better half an MCU in than at its first
|
||||
//! sample, so the chroma is treated as centred on its MCU and interpolated bilinearly.
|
||||
//!
|
||||
//! Approximate: the chroma siting and the bilinear interpolation (colour edges are softer than
|
||||
//! in a Bayer development; luma keeps full resolution). The gains are read only for ColorData
|
||||
//! versions where their offset was observed; for other bodies they are left at 1, which is a
|
||||
//! diagonal colour error that the matrix fitted to the camera's JPEG absorbs (a warning says
|
||||
//! so). Older bodies (sRAW before about 2009) are documented elsewhere to use other offsets
|
||||
//! and chroma scalings; no such sample was available, so they decode with the same rule.
|
||||
//! Shadow noise below black is clipped at 0 by the conversion.
|
||||
|
||||
use crate::error::{RawError, Result};
|
||||
use crate::ljpeg::Frame;
|
||||
use crate::par;
|
||||
|
||||
/// The de-sliced YCbCr planes: Y at full resolution, Cb and Cr one sample per MCU, stored
|
||||
/// values (chroma still carries its offset).
|
||||
pub(crate) struct Planes {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub h: usize,
|
||||
pub v: usize,
|
||||
pub y: Vec<u16>,
|
||||
pub cb: Vec<u16>,
|
||||
pub cr: Vec<u16>,
|
||||
}
|
||||
|
||||
/// Lays the decoded sample sequence out as planes. `slices` are the slice widths in samples
|
||||
/// (CR2 tag 0xC640; one slice of the whole MCU row when absent): slice 0 is filled MCU row by
|
||||
/// MCU row, then slice 1, and so on, as for the Bayer CR2 data.
|
||||
pub(crate) fn planes(f: &Frame, samples: &[u16], slices: &[usize]) -> Result<Planes> {
|
||||
let (h, v) = (f.h, f.v);
|
||||
let per_mcu = h * v + 2;
|
||||
let total: usize = slices.iter().sum();
|
||||
if total == 0 || !total.is_multiple_of(per_mcu) || slices.iter().any(|w| !w.is_multiple_of(per_mcu)) || !samples.len().is_multiple_of(total) {
|
||||
return Err(RawError::malformed("CR2 sRAW slices do not match the JPEG frame"));
|
||||
}
|
||||
let mcus = total / per_mcu;
|
||||
let rows = samples.len() / total;
|
||||
let (width, height) = (mcus * h, rows * v);
|
||||
let mut p = Planes { width, height, h, v, y: vec![0; width * height], cb: vec![0; mcus * rows], cr: vec![0; mcus * rows] };
|
||||
let mut runs = samples.chunks_exact(per_mcu);
|
||||
let mut x0 = 0usize;
|
||||
for &sw in slices {
|
||||
let n = sw / per_mcu;
|
||||
for r in 0..rows {
|
||||
for mx in x0..x0 + n {
|
||||
let mcu = runs.next().ok_or_else(|| RawError::malformed("CR2 sRAW slice data is short"))?;
|
||||
let (ys, c) = mcu.split_at(h * v);
|
||||
for (k, &s) in ys.iter().enumerate() {
|
||||
let (x, y) = (mx * h + k % h, r * v + k / h);
|
||||
if let Some(d) = p.y.get_mut(y * width + x) {
|
||||
*d = s;
|
||||
}
|
||||
}
|
||||
if let ([cb, cr], Some(dcb), Some(dcr)) = (c, p.cb.get_mut(r * mcus + mx), p.cr.get_mut(r * mcus + mx)) {
|
||||
(*dcb, *dcr) = (*cb, *cr);
|
||||
}
|
||||
}
|
||||
}
|
||||
x0 += n;
|
||||
}
|
||||
Ok(p)
|
||||
}
|
||||
|
||||
/// White level of unclipped frames: 14-bit full scale less Canon's usual black of about 2048.
|
||||
pub(crate) const FULL_SCALE: f32 = 14335.0;
|
||||
|
||||
/// BT.601 luma weights of R and B (G's is the rest).
|
||||
const KR: f32 = 0.299;
|
||||
const KB: f32 = 0.114;
|
||||
|
||||
/// Interpolates the chroma to every pixel and converts to camera RGB, interleaved:
|
||||
/// `R = Y + Cr`, `B = Y + Cb`, `G = (Y − KR·R − KB·B) / KG`, then each channel multiplied by the
|
||||
/// camera's sRAW `gains`. `offset` is the chroma's zero (`2^(P−1)`).
|
||||
pub(crate) fn camera_rgb(p: &Planes, offset: f32, gains: [f64; 3]) -> Vec<u16> {
|
||||
let (w, h) = (p.width, p.height);
|
||||
let cw = w / p.h.max(1);
|
||||
let ch = p.cb.len().checked_div(cw).unwrap_or(0);
|
||||
let mut out = vec![0u16; w * h * 3];
|
||||
if w == 0 || cw == 0 || ch == 0 {
|
||||
return out;
|
||||
}
|
||||
let gains = gains.map(|m| if m.is_finite() && m > 0.0 { m as f32 } else { 1.0 });
|
||||
let kg = 1.0 - KR - KB;
|
||||
// Chroma sample positions: centred on the MCU (see the module docs). Per output column (row):
|
||||
// the two chroma columns (rows) around it and the weight of the second.
|
||||
let taps = |n: usize, f: usize, len: usize| -> Vec<(usize, usize, f32)> {
|
||||
(0..n)
|
||||
.map(|i| {
|
||||
let c = ((i as f32 + 0.5) / f as f32 - 0.5).clamp(0.0, (len - 1) as f32);
|
||||
let a = (c as usize).min(len - 1);
|
||||
let b = (a + 1).min(len - 1);
|
||||
(a, b, c - a as f32)
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
let xs = taps(w, p.h.max(1), cw);
|
||||
let ys = taps(h, p.v.max(1), ch);
|
||||
let band = par::band_rows(w);
|
||||
par::chunks_mut(&mut out, band * w * 3, |b, chunk| {
|
||||
for (r, row) in chunk.chunks_exact_mut(w * 3).enumerate() {
|
||||
let y = b * band + r;
|
||||
let (Some(luma), Some(&(ya, yb, fy))) = (p.y.get(y * w..(y + 1) * w), ys.get(y)) else { continue };
|
||||
let chroma = |plane: &[u16], xa: usize, xb: usize, fx: f32| {
|
||||
let at = |cy: usize, cx: usize| plane.get(cy * cw + cx).map_or(offset, |&v| f32::from(v));
|
||||
let top = at(ya, xa) * (1.0 - fx) + at(ya, xb) * fx;
|
||||
let bottom = at(yb, xa) * (1.0 - fx) + at(yb, xb) * fx;
|
||||
top * (1.0 - fy) + bottom * fy - offset
|
||||
};
|
||||
for ((o, &l), &(xa, xb, fx)) in row.as_chunks_mut::<3>().0.iter_mut().zip(luma).zip(&xs) {
|
||||
let l = f32::from(l);
|
||||
let (cb, cr) = (chroma(&p.cb, xa, xb, fx), chroma(&p.cr, xa, xb, fx));
|
||||
let (red, blue) = (l + cr, l + cb);
|
||||
let green = (l - KR * red - KB * blue) / kg;
|
||||
for (k, v) in [red, green, blue].into_iter().enumerate() {
|
||||
o[k] = (v * gains[k]).round().clamp(0.0, 65535.0) as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
/// White level of channel `k` of interleaved RGB: the largest value when a meaningful share of
|
||||
/// samples piles up there (clipped highlights), else `full`.
|
||||
pub(crate) fn clip_level(rgb: &[u16], k: usize, full: f32) -> f32 {
|
||||
let step = 3 * (rgb.len() / 3 / 2_000_000).max(1);
|
||||
let samples = || rgb.iter().skip(k).step_by(step);
|
||||
let max = samples().copied().max().unwrap_or(0);
|
||||
if max == 0 {
|
||||
return full;
|
||||
}
|
||||
let near = samples().filter(|&&v| u32::from(v) + u32::from(max / 256) >= u32::from(max)).count();
|
||||
if near * 2000 >= samples().count() { f32::from(max) * 0.995 } else { full.max(f32::from(max)) }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::testgen::Cr2SrawSpec;
|
||||
use crate::{DevelopOptions, Limits, decode, develop_sensor};
|
||||
|
||||
const OFFSET: f32 = 16384.0;
|
||||
|
||||
/// A linear ramp of camera RGB (bilinear chroma interpolation reproduces it exactly away
|
||||
/// from the borders).
|
||||
fn camera(x: usize, y: usize) -> [f32; 3] {
|
||||
let (x, y) = (x as f32, y as f32);
|
||||
[1500.0 + 40.0 * x + 10.0 * y, 2500.0 + 15.0 * x + 30.0 * y, 1200.0 + 5.0 * x + 45.0 * y]
|
||||
}
|
||||
|
||||
/// The camera's stored YCbCr for `camera` divided by `gains`: Y per pixel, Cb / Cr at the
|
||||
/// centre of each MCU (the siting the decoder assumes).
|
||||
fn spec(h: usize, v: usize, gains: [f32; 3], slices: Vec<usize>, restart_rows: Option<usize>) -> Cr2SrawSpec {
|
||||
let (w, ht) = (48, 24);
|
||||
let stored = |x: f32, y: f32| {
|
||||
let c = camera(0, 0);
|
||||
let (dx, dy) = (camera(1, 0), camera(0, 1));
|
||||
[0, 1, 2].map(|k| (c[k] + (dx[k] - c[k]) * x + (dy[k] - c[k]) * y) / gains[k])
|
||||
};
|
||||
let ycc = |p: [f32; 3]| {
|
||||
let y = KR * p[0] + (1.0 - KR - KB) * p[1] + KB * p[2];
|
||||
(y, p[2] - y + OFFSET, p[0] - y + OFFSET)
|
||||
};
|
||||
let y = (0..w * ht).map(|i| ycc(stored((i % w) as f32, (i / w) as f32)).0.round() as u16).collect();
|
||||
let (mw, mh) = (w / h, ht / v);
|
||||
let centre = |m: usize, f: usize| (m * f) as f32 + (f as f32 - 1.0) / 2.0;
|
||||
let chroma: Vec<(f32, f32, f32)> = (0..mw * mh).map(|i| ycc(stored(centre(i % mw, h), centre(i / mw, v)))).collect();
|
||||
Cr2SrawSpec {
|
||||
width: w,
|
||||
height: ht,
|
||||
h,
|
||||
v,
|
||||
precision: 15,
|
||||
y,
|
||||
cb: chroma.iter().map(|c| c.1.round() as u16).collect(),
|
||||
cr: chroma.iter().map(|c| c.2.round() as u16).collect(),
|
||||
slices,
|
||||
restart_rows,
|
||||
wb_rggb: Some([2048, 1024, 1024, 1536]),
|
||||
gains: Some([(1170.0 * gains[0]) as u16, 1170, 1170, (1170.0 * gains[2]) as u16]),
|
||||
borders: Some([0, 2, 47, 23]),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sraw_decodes_to_camera_rgb() {
|
||||
let gains = [0.75, 1.0, 0.4];
|
||||
// sRAW1 (2×2: 6 samples an MCU) and sRAW2 (2×1: 4 samples an MCU), with and without
|
||||
// slices and restart intervals.
|
||||
for (h, v, slices, restart) in
|
||||
[(2, 2, vec![], None), (2, 2, vec![48, 48, 48], Some(3)), (2, 2, vec![60, 60, 24], None), (2, 1, vec![], Some(1)), (2, 1, vec![32, 32, 32], None)]
|
||||
{
|
||||
let s = spec(h, v, gains, slices.clone(), restart);
|
||||
let sensor = decode(&s.build(), &Limits::default()).expect("decode");
|
||||
assert_eq!((sensor.width, sensor.height, sensor.samples), (48, 24, 3), "{h}x{v} {slices:?}");
|
||||
assert!(sensor.cfa.is_none());
|
||||
assert_eq!(sensor.crop, crate::Rect::new(0, 2, 48, 22));
|
||||
assert_eq!(sensor.camera_wb, Some([2.0, 1.0, 1.5]));
|
||||
assert!(sensor.warnings.is_empty(), "{:?}", sensor.warnings);
|
||||
for y in 2..22 {
|
||||
for x in 2..46 {
|
||||
let at = (y * 48 + x) * 3;
|
||||
for (k, (&got, want)) in sensor.data[at..at + 3].iter().zip(camera(x, y)).enumerate() {
|
||||
let got = f32::from(got);
|
||||
// Rounding of Y, Cb, Cr and the stored gains (whole 1/1170 steps).
|
||||
assert!((got - want).abs() <= 4.0 + want * 0.002, "{h}x{v} {slices:?} ({x}, {y}) ch {k}: {got} vs {want}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_gains_keep_the_stored_colour_and_say_so() {
|
||||
let mut s = spec(2, 2, [1.0; 3], vec![], None);
|
||||
s.gains = None;
|
||||
let sensor = decode(&s.build(), &Limits::default()).expect("decode");
|
||||
assert!(sensor.warnings.iter().any(|w| w.contains("sRAW colour gains")), "{:?}", sensor.warnings);
|
||||
let want = camera(10, 10);
|
||||
let got = &sensor.data[(10 * 48 + 10) * 3..(10 * 48 + 10) * 3 + 3];
|
||||
assert!(got.iter().zip(want).all(|(&g, w)| (f32::from(g) - w).abs() <= 4.0), "{got:?} vs {want:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sraw_develops_as_linear_rgb() {
|
||||
let s = spec(2, 1, [0.75, 1.0, 0.4], vec![], None);
|
||||
let sensor = decode(&s.build(), &Limits::default()).expect("decode");
|
||||
let d = develop_sensor(&sensor, &DevelopOptions::default()).expect("develop");
|
||||
assert_eq!((d.width, d.height), (48, 22));
|
||||
assert_eq!(d.info.cfa, None);
|
||||
assert_eq!(d.info.wb_multipliers, [2.0, 1.0, 1.5]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn corrupt_sraw_errors_without_panicking() {
|
||||
let b = spec(2, 2, [0.75, 1.0, 0.4], vec![48, 48, 48], Some(2)).build();
|
||||
for n in (0..b.len()).step_by(7) {
|
||||
let _ = decode(&b[..n], &Limits::default());
|
||||
}
|
||||
let mut s = 0x9E37_79B9u64;
|
||||
for _ in 0..1500 {
|
||||
let mut c = b.clone();
|
||||
for _ in 0..3 {
|
||||
s = s.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
|
||||
let at = (s >> 33) as usize % c.len();
|
||||
c[at] = (s >> 20) as u8;
|
||||
}
|
||||
if let Ok(sensor) = decode(&c, &Limits::default()) {
|
||||
let _ = develop_sensor(&sensor, &DevelopOptions::default());
|
||||
}
|
||||
}
|
||||
// Slices that do not split whole MCUs are refused.
|
||||
let mut bad = spec(2, 2, [1.0; 3], vec![48, 48, 48], None).build();
|
||||
let tag = bad.windows(4).position(|w| w == [0x40, 0xC6, 3, 0]).expect("slice tag");
|
||||
let at = u32::from_le_bytes([bad[tag + 8], bad[tag + 9], bad[tag + 10], bad[tag + 11]]) as usize;
|
||||
bad[at + 2..at + 4].copy_from_slice(&47u16.to_le_bytes());
|
||||
assert!(matches!(decode(&bad, &Limits::default()), Err(RawError::Malformed(_))));
|
||||
// The planes refuse a sample count that is not whole slice rows.
|
||||
let f = Frame { precision: 15, width: 4, height: 2, components: 3, h: 2, v: 2 };
|
||||
assert!(planes(&f, &[0; 11], &[12]).is_err());
|
||||
assert!(planes(&f, &[0; 12], &[0]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn white_level_follows_clipping() {
|
||||
// A lone bright sample is not clipping.
|
||||
let mut rgb: Vec<u16> = (0..3 * 10_000).map(|i| (i % 5000) as u16).collect();
|
||||
rgb[0] = 9000;
|
||||
assert_eq!(clip_level(&rgb, 0, 14335.0), 14335.0);
|
||||
for p in rgb.as_chunks_mut::<3>().0.iter_mut().take(100) {
|
||||
p[1] = 12000;
|
||||
}
|
||||
assert!((clip_level(&rgb, 1, 14335.0) - 12000.0 * 0.995).abs() < 1e-3);
|
||||
assert_eq!(clip_level(&[], 2, 14335.0), 14335.0);
|
||||
}
|
||||
|
||||
/// Real files: `PHOTOCRAFT_SRAW_CR2` lists CR2 paths (separated by `:`), e.g. EOS 6D Mark II
|
||||
/// mRAW. Checks the size, the gains and that the development runs.
|
||||
#[test]
|
||||
#[ignore = "needs real sRAW CR2 files in PHOTOCRAFT_SRAW_CR2"]
|
||||
fn real_sraw_files() {
|
||||
let paths = std::env::var("PHOTOCRAFT_SRAW_CR2").expect("PHOTOCRAFT_SRAW_CR2");
|
||||
for path in paths.split(':').filter(|p| !p.is_empty()) {
|
||||
let bytes = std::fs::read(path).expect("read");
|
||||
let s = decode(&bytes, &Limits::default()).expect("decode");
|
||||
assert_eq!(s.samples, 3, "{path}");
|
||||
assert!(s.cfa.is_none() && s.camera_wb.is_some(), "{path}");
|
||||
assert!(s.warnings.is_empty(), "{path}: {:?}", s.warnings);
|
||||
let d = develop_sensor(&s, &DevelopOptions::default()).expect("develop");
|
||||
eprintln!("{path}: {}x{} from {}x{}, white {:?}", d.width, d.height, s.width, s.height, s.white);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,9 @@ mod raf;
|
||||
mod rw2;
|
||||
mod tiff;
|
||||
|
||||
pub use canon::{Cr2Spec, craw_block, craw_encode, nef_compressed, sony_craw, tiff_ep};
|
||||
pub use canon::{Cr2Spec, Cr2SrawSpec, craw_block, craw_encode, nef_compressed, sony_craw, tiff_ep};
|
||||
pub use dng::{DngSpec, DngStorage, baseline_jpeg, gain_map_opcode_list, map_polynomial_opcode_list};
|
||||
pub use lj92::{lj92_encode, lj92_encode_tables};
|
||||
pub use lj92::{lj92_encode, lj92_encode_sraw, lj92_encode_sraw_tables, lj92_encode_tables};
|
||||
pub use orf::orf;
|
||||
pub use raf::{RafPacking, RafSpec, XTRANS, mosaic, mosaic_cfa, scene};
|
||||
pub use rw2::{rw2, rw2_format4, rw2_pack, rw2_v4_pack};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::lj92::lj92_encode;
|
||||
use super::lj92::{lj92_encode, lj92_encode_sraw};
|
||||
use super::tiff::{TiffBuilder, Val};
|
||||
|
||||
// ---------------------------------------------------------------- CR2
|
||||
@@ -94,6 +94,103 @@ impl Cr2Spec {
|
||||
}
|
||||
}
|
||||
|
||||
/// A synthetic Canon sRAW / mRAW CR2: Y, Cb and Cr planes in one subsampled lossless JPEG
|
||||
/// (see `ljpeg::decode_subsampled` and `sraw.rs`).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Cr2SrawSpec {
|
||||
/// Image size in pixels (multiples of `h` and `v`).
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
/// Y sampling factors: 2×2 (sRAW1 / mRAW) or 2×1 (sRAW2).
|
||||
pub h: usize,
|
||||
pub v: usize,
|
||||
pub precision: u8,
|
||||
/// `width * height` Y samples, row-major.
|
||||
pub y: Vec<u16>,
|
||||
/// One Cb and one Cr per MCU (`width / h` by `height / v`), offset by `2^(precision−1)`.
|
||||
pub cb: Vec<u16>,
|
||||
pub cr: Vec<u16>,
|
||||
/// Slice widths in samples (all but the last equal); empty = no slice tag.
|
||||
pub slices: Vec<usize>,
|
||||
/// Restart interval in MCU rows.
|
||||
pub restart_rows: Option<usize>,
|
||||
/// As-shot RGGB levels at ColorData word 0x3F.
|
||||
pub wb_rggb: Option<[u16; 4]>,
|
||||
/// sRAW gains (RGGB) at ColorData word 0x80; ColorData version 15 is written either way.
|
||||
pub gains: Option<[u16; 4]>,
|
||||
/// Image borders: left, top, right, bottom (inclusive), as in SensorInfo.
|
||||
pub borders: Option<[u16; 4]>,
|
||||
}
|
||||
|
||||
impl Cr2SrawSpec {
|
||||
pub fn build(&self) -> Vec<u8> {
|
||||
let (h, v) = (self.h, self.v);
|
||||
let per_mcu = h * v + 2;
|
||||
let (mcus, rows) = (self.width / h, self.height / v);
|
||||
let widths: Vec<usize> = if self.slices.is_empty() { vec![mcus * per_mcu] } else { self.slices.clone() };
|
||||
assert_eq!(widths.iter().sum::<usize>(), mcus * per_mcu);
|
||||
// Slice the MCUs into the JPEG sample order.
|
||||
let mut seq = Vec::with_capacity(mcus * rows * per_mcu);
|
||||
let mut m0 = 0;
|
||||
for &sw in &widths {
|
||||
assert_eq!(sw % per_mcu, 0);
|
||||
let n = sw / per_mcu;
|
||||
for r in 0..rows {
|
||||
for mx in m0..m0 + n {
|
||||
for k in 0..h * v {
|
||||
seq.push(self.y[(r * v + k / h) * self.width + mx * h + k % h]);
|
||||
}
|
||||
seq.push(self.cb[r * mcus + mx]);
|
||||
seq.push(self.cr[r * mcus + mx]);
|
||||
}
|
||||
}
|
||||
m0 += n;
|
||||
}
|
||||
let jpeg = lj92_encode_sraw(&seq, self.width, self.height, h, v, self.precision, self.restart_rows);
|
||||
let mut t = TiffBuilder::default();
|
||||
let jb = t.blob(jpeg.clone());
|
||||
let mut raw = vec![
|
||||
(256, Val::Short(vec![self.width as u16])),
|
||||
(257, Val::Short(vec![self.height as u16])),
|
||||
(259, Val::Short(vec![6])),
|
||||
(273, Val::Blobs(vec![jb])),
|
||||
(279, Val::Long(vec![jpeg.len() as u32])),
|
||||
];
|
||||
if !self.slices.is_empty() {
|
||||
let n = widths.len() - 1;
|
||||
raw.push((50752, Val::Short(vec![n as u16, widths[0] as u16, widths[n] as u16])));
|
||||
}
|
||||
let raw_ifd = t.ifd(raw);
|
||||
let mut mn: Vec<(u16, Val)> = Vec::new();
|
||||
if let Some([l, tp, r, b]) = self.borders {
|
||||
mn.push((0x00E0, Val::Short(vec![34, self.width as u16, self.height as u16, 0, 0, l, tp, r, b, 0, 0, 0, 0, 0, 0, 0, 0])));
|
||||
}
|
||||
let mut cd = vec![0u8; 2 * 1602];
|
||||
cd[0..2].copy_from_slice(&15u16.to_le_bytes());
|
||||
for (at, levels) in [(0x3F, self.wb_rggb), (0x80, self.gains)] {
|
||||
for (i, v) in levels.iter().flatten().enumerate() {
|
||||
cd[2 * (at + i)..2 * (at + i) + 2].copy_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
}
|
||||
mn.push((0x4001, Val::Undefined(cd)));
|
||||
let mi = t.ifd(mn);
|
||||
let exif_ifd = t.ifd(vec![(33434, Val::Rational(vec![(1, 100)])), (37500, Val::IfdBytes(mi))]);
|
||||
let ifd0 = t.ifd(vec![
|
||||
(256, Val::Long(vec![2])),
|
||||
(257, Val::Long(vec![2])),
|
||||
(271, Val::Ascii("Canon".into())),
|
||||
(272, Val::Ascii("Canon EOS Synthetic".into())),
|
||||
(274, Val::Short(vec![1])),
|
||||
(34665, Val::Ifds(vec![exif_ifd])),
|
||||
]);
|
||||
let ifd1 = t.ifd(vec![(256, Val::Long(vec![1]))]);
|
||||
let ifd2 = t.ifd(vec![(256, Val::Long(vec![1]))]);
|
||||
t.chain = vec![ifd0, ifd1, ifd2, raw_ifd];
|
||||
t.cr2_raw_ifd = Some(raw_ifd);
|
||||
t.build()
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- TIFF/EP
|
||||
|
||||
/// A synthetic uncompressed TIFF/EP raw (NEF / ARW-like): IFD0 with Make, the
|
||||
|
||||
+121
-19
@@ -56,7 +56,6 @@ pub fn lj92_encode(samples: &[u16], width: usize, height: usize, components: usi
|
||||
pub fn lj92_encode_tables(samples: &[u16], width: usize, height: usize, table_of: &[u8], precision: u8, predictor: u8, restart_rows: Option<usize>) -> Vec<u8> {
|
||||
let components = table_of.len();
|
||||
assert_eq!(samples.len(), width * height * components);
|
||||
assert!(table_of.iter().all(|&t| t < 4));
|
||||
let row = width * components;
|
||||
let interval = restart_rows.unwrap_or(usize::MAX);
|
||||
// Differences per sample, following T.81 H.1.2.1 prediction rules.
|
||||
@@ -85,16 +84,118 @@ pub fn lj92_encode_tables(samples: &[u16], width: usize, height: usize, table_of
|
||||
_ => (ra + rb) >> 1,
|
||||
}
|
||||
};
|
||||
let mut d = (i32::from(samples[i]) - px) & 0xFFFF;
|
||||
if d > 32768 {
|
||||
d -= 65536;
|
||||
}
|
||||
diffs.push(d);
|
||||
diffs.push(wrap_diff(i32::from(samples[i]) - px));
|
||||
}
|
||||
}
|
||||
}
|
||||
let frame = Frame {
|
||||
width,
|
||||
height,
|
||||
factors: &vec![0x11; components],
|
||||
table_of,
|
||||
pattern: &(0..components as u8).collect::<Vec<_>>(),
|
||||
precision,
|
||||
predictor,
|
||||
restart: restart_rows.map(|r| (r * width, interval.saturating_mul(row))),
|
||||
};
|
||||
lj92_stream(&diffs, &frame)
|
||||
}
|
||||
|
||||
/// A difference modulo 2^16, in −32767..=32768 (T.81 H.1.2.2).
|
||||
fn wrap_diff(d: i32) -> i32 {
|
||||
let d = d & 0xFFFF;
|
||||
if d > 32768 { d - 65536 } else { d }
|
||||
}
|
||||
|
||||
/// Encodes a Canon sRAW / mRAW frame: `seq` is the sample sequence in decode order (per MCU the
|
||||
/// `h × v` Y samples, then Cb and Cr; `width / h` MCUs a row), predicted as
|
||||
/// `ljpeg::decode_subsampled` describes: each Y from the previous Y, Cb and Cr from the previous
|
||||
/// MCU's, the first MCU of a row from the first of the row above. `restart_rows` counts MCU rows.
|
||||
pub fn lj92_encode_sraw(seq: &[u16], width: usize, height: usize, h: usize, v: usize, precision: u8, restart_rows: Option<usize>) -> Vec<u8> {
|
||||
lj92_encode_sraw_tables(seq, width, height, (h, v), [0, 0, 0], precision, restart_rows)
|
||||
}
|
||||
|
||||
/// Like [`lj92_encode_sraw`], but Y, Cb and Cr use Huffman tables `table_of` (0–3).
|
||||
pub fn lj92_encode_sraw_tables(
|
||||
seq: &[u16],
|
||||
width: usize,
|
||||
height: usize,
|
||||
(h, v): (usize, usize),
|
||||
table_of: [u8; 3],
|
||||
precision: u8,
|
||||
restart_rows: Option<usize>,
|
||||
) -> Vec<u8> {
|
||||
let (luma, mcus) = (h * v, width / h);
|
||||
let per_mcu = luma + 2;
|
||||
let row_len = mcus * per_mcu;
|
||||
assert_eq!(seq.len(), row_len * (height / v));
|
||||
let interval = restart_rows.unwrap_or(usize::MAX);
|
||||
let initial = 1i32 << (precision - 1);
|
||||
let mut diffs = Vec::with_capacity(seq.len());
|
||||
let mut top = [initial; 3];
|
||||
for (r, row) in seq.chunks_exact(row_len).enumerate() {
|
||||
if r % interval == 0 {
|
||||
top = [initial; 3];
|
||||
}
|
||||
let (mut y, mut cb, mut cr) = (top[0], top[1], top[2]);
|
||||
for (m, mcu) in row.chunks_exact(per_mcu).enumerate() {
|
||||
for (k, &s) in mcu[..luma].iter().enumerate() {
|
||||
let pred = if m == 0 && k == 0 { top[0] } else { y };
|
||||
diffs.push(wrap_diff(i32::from(s) - pred));
|
||||
y = i32::from(s);
|
||||
if m == 0 && k == 0 {
|
||||
top[0] = y;
|
||||
}
|
||||
}
|
||||
diffs.push(wrap_diff(i32::from(mcu[luma]) - cb));
|
||||
diffs.push(wrap_diff(i32::from(mcu[luma + 1]) - cr));
|
||||
(cb, cr) = (i32::from(mcu[luma]), i32::from(mcu[luma + 1]));
|
||||
if m == 0 {
|
||||
(top[1], top[2]) = (cb, cr);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut pattern = vec![0u8; luma];
|
||||
pattern.extend_from_slice(&[1, 2]);
|
||||
let frame = Frame {
|
||||
width,
|
||||
height,
|
||||
factors: &[(h as u8) << 4 | v as u8, 0x11, 0x11],
|
||||
table_of: &table_of,
|
||||
pattern: &pattern,
|
||||
precision,
|
||||
predictor: 1,
|
||||
restart: restart_rows.map(|r| (r * mcus, interval.saturating_mul(row_len))),
|
||||
};
|
||||
lj92_stream(&diffs, &frame)
|
||||
}
|
||||
|
||||
/// The frame header [`lj92_stream`] writes around the differences.
|
||||
struct Frame<'a> {
|
||||
width: usize,
|
||||
height: usize,
|
||||
/// Each component's sampling factors (`h << 4 | v`).
|
||||
factors: &'a [u8],
|
||||
/// Each component's Huffman table id (0–3).
|
||||
table_of: &'a [u8],
|
||||
/// The component of each difference, repeating: `0, 1, 2` interleaved, or an MCU's Y samples
|
||||
/// then Cb and Cr.
|
||||
pattern: &'a [u8],
|
||||
precision: u8,
|
||||
predictor: u8,
|
||||
/// The restart interval `(DRI value, differences per interval)`.
|
||||
restart: Option<(usize, usize)>,
|
||||
}
|
||||
|
||||
/// Writes SOI, SOF3, one DHT per table id, the restart interval, SOS and the entropy-coded
|
||||
/// `diffs`.
|
||||
fn lj92_stream(diffs: &[i32], f: &Frame<'_>) -> Vec<u8> {
|
||||
let components = f.factors.len();
|
||||
assert_eq!(f.table_of.len(), components);
|
||||
assert!(f.table_of.iter().all(|&t| t < 4));
|
||||
let table = |i: usize| f.table_of[usize::from(f.pattern[i % f.pattern.len()])];
|
||||
// Tables: categories ordered by frequency get increasing code lengths.
|
||||
let mut ids: Vec<u8> = table_of.to_vec();
|
||||
let mut ids: Vec<u8> = f.table_of.to_vec();
|
||||
ids.sort_unstable();
|
||||
ids.dedup();
|
||||
let mut code_of = [[(0u32, 0u32); 17]; 4];
|
||||
@@ -102,7 +203,7 @@ pub fn lj92_encode_tables(samples: &[u16], width: usize, height: usize, table_of
|
||||
for &t in &ids {
|
||||
let mut freq = [0usize; 17];
|
||||
for (i, &d) in diffs.iter().enumerate() {
|
||||
if table_of[i % components] == t {
|
||||
if table(i) == t {
|
||||
freq[category(d) as usize] += 1;
|
||||
}
|
||||
}
|
||||
@@ -133,12 +234,12 @@ pub fn lj92_encode_tables(samples: &[u16], width: usize, height: usize, table_of
|
||||
// SOF3
|
||||
out.extend_from_slice(&[0xFF, 0xC3]);
|
||||
out.extend_from_slice(&((8 + 3 * components) as u16).to_be_bytes());
|
||||
out.push(precision);
|
||||
out.extend_from_slice(&(height as u16).to_be_bytes());
|
||||
out.extend_from_slice(&(width as u16).to_be_bytes());
|
||||
out.push(f.precision);
|
||||
out.extend_from_slice(&(f.height as u16).to_be_bytes());
|
||||
out.extend_from_slice(&(f.width as u16).to_be_bytes());
|
||||
out.push(components as u8);
|
||||
for c in 0..components {
|
||||
out.extend_from_slice(&[c as u8 + 1, 0x11, 0]);
|
||||
for (c, &factor) in f.factors.iter().enumerate() {
|
||||
out.extend_from_slice(&[c as u8 + 1, factor, 0]);
|
||||
}
|
||||
// DHT, one per table id
|
||||
for (t, counts, vals) in &dhts {
|
||||
@@ -148,28 +249,29 @@ pub fn lj92_encode_tables(samples: &[u16], width: usize, height: usize, table_of
|
||||
out.extend_from_slice(counts);
|
||||
out.extend_from_slice(vals);
|
||||
}
|
||||
if let Some(r) = restart_rows {
|
||||
if let Some((dri, _)) = f.restart {
|
||||
out.extend_from_slice(&[0xFF, 0xDD, 0, 4]);
|
||||
out.extend_from_slice(&((r * width) as u16).to_be_bytes());
|
||||
out.extend_from_slice(&(dri as u16).to_be_bytes());
|
||||
}
|
||||
let every = f.restart.map_or(usize::MAX, |(_, n)| n);
|
||||
// SOS
|
||||
out.extend_from_slice(&[0xFF, 0xDA]);
|
||||
out.extend_from_slice(&((6 + 2 * components) as u16).to_be_bytes());
|
||||
out.push(components as u8);
|
||||
for (c, &t) in table_of.iter().enumerate() {
|
||||
for (c, &t) in f.table_of.iter().enumerate() {
|
||||
out.extend_from_slice(&[c as u8 + 1, t << 4]);
|
||||
}
|
||||
out.extend_from_slice(&[predictor, 0, 0]);
|
||||
out.extend_from_slice(&[f.predictor, 0, 0]);
|
||||
let mut w = BitWriter { out, acc: 0, n: 0 };
|
||||
let mut rst = 0u8;
|
||||
for (i, &d) in diffs.iter().enumerate() {
|
||||
if i > 0 && i % (interval.saturating_mul(row)) == 0 {
|
||||
if i > 0 && i % every == 0 {
|
||||
w.flush();
|
||||
w.out.extend_from_slice(&[0xFF, 0xD0 + rst]);
|
||||
rst = (rst + 1) % 8;
|
||||
}
|
||||
let s = category(d);
|
||||
let (c, l) = code_of[usize::from(table_of[i % components])][s as usize];
|
||||
let (c, l) = code_of[usize::from(table(i))][s as usize];
|
||||
w.put(c, l);
|
||||
if (1..16).contains(&s) {
|
||||
let extra = if d < 0 { (d - 1) as u32 & ((1 << s) - 1) } else { d as u32 };
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user