Files
photocraft/crates/engine-core/src/cmd/smart_cmds.rs
T

449 lines
21 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Smart Objects and Smart Filters: Layer › Smart Objects, Layer › Smart Filter and
//! Filter › Convert for Smart Filters.
//!
//! A smart object keeps its **source** (embedded file bytes, or a linked path) plus a
//! `transform` and a stack of smart filters; its `cache` is the rendered appearance the
//! compositors draw. The engine owns re-rendering (compose stays a pure function of the
//! document):
//!
//! ```text
//! source bytes ──decode + composite (cached by content hash)──► source image (source pixels)
//! ──transform (exact shift, or bicubic warp)──────► placed (document pixels)
//! ──each visible smart filter, blended (blend/opacity)──► filtered
//! ──filter mask mix (unfiltered ↔ filtered)───────► cache
//! ```
//!
//! The placed stage and the result after each filter are cached too (`stack_cache`), so a
//! re-render starts from the deepest stage it shares with an earlier one.
//!
//! Converting a layer embeds it as an in-memory `.pcraft` bundle of a nested document (exactly
//! lossless, layered, any depth/model). PSD placed layers keep their source in the preserved
//! global `lnk2` block (found by uuid), and keep Photoshop's rendering until something changes,
//! so an unedited PSD still round-trips byte for byte. PSD export (`photocraft_io::smart_map`)
//! writes every smart object back as one: its source embedded in `lnk2` (a `.pcraft` source as a
//! PSB), its smart filters in `filterFX` and its filter mask in `FEid`.
use std::sync::{Arc, Mutex};
use photocraft_algo::resample::translate_surface;
use photocraft_algo::transform::Homography;
use photocraft_color::{BlendMode, ColorMode, PixelFormat, SampleType};
use photocraft_doc::{DocId, Document, Layer, LayerContent, LayerId, LayerMask, Metadata, SmartContentsId, SmartFilter, SmartObject, SmartSource};
use photocraft_geom::{Affine, Rect, Size};
use photocraft_raster::Surface;
use serde_json::{Value, json};
use stack_cache::{Stage, StageKey};
use crate::commands::{CommandSpec, layer_param, parse_blend};
use crate::jobs::JobCtx;
use crate::{EngineError, Result, Session};
pub use crate::state::SmartLink;
/// PSD placed-layer keys that describe the smart object's source; stale once we change it.
const PLACED_KEYS: [&[u8; 4]; 3] = [b"SoLd", b"PlLd", b"SoLE"];
/// Command id of a Photoshop smart filter PhotoCraft does not implement (kept verbatim from PSD).
pub use photocraft_io::smart_map::UNSUPPORTED_FILTER;
fn other(msg: impl Into<String>) -> EngineError {
EngineError::Other(msg.into())
}
fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {
EngineError::BadParams { cmd: cmd.into(), msg: msg.into() }
}
// ---------- source encoding / decoding ----------
/// Encodes a document as embedded smart-object contents: an in-memory `.pcraft` bundle.
pub fn encode_source(doc: &Document) -> Result<Vec<u8>> {
photocraft_format::save_to_bytes(doc, &Default::default()).map_err(|e| other(format!("can't encode smart object contents: {e}")))
}
/// Decodes smart-object contents (a `.pcraft` bundle, PSD/PSB, or any flat image format).
pub fn decode_source(file_name: &str, bytes: &[u8]) -> Result<Document> {
decode_source_classified(file_name, bytes, photocraft_io::svg::is_svg(bytes))
}
/// Reuse the central format decision made by this render operation.
fn decode_source_classified(file_name: &str, bytes: &[u8], svg: bool) -> Result<Document> {
if svg {
// This API has no warning channel. Stack/source caches and contents edits must not
// turn a partial SVG import into silently complete smart-object contents.
return photocraft_io::svg::import_svg_bounded_images_strict(file_name, bytes, 24_000_000)
.map(|imported| imported.document)
.map_err(|e| other(format!("can't read SVG smart object contents: {e}")));
}
crate::file_cmds::import(file_name, bytes).map_err(|e| other(format!("can't read smart object contents \"{file_name}\": {e}")))
}
fn base_name(path: &str) -> String {
path.rsplit(['/', '\\']).next().unwrap_or(path).to_string()
}
// ---------- enablement ----------
fn active_smart(s: &Session) -> std::result::Result<&SmartObject, String> {
let d = s.active().ok_or("no document open")?;
let id = d.active_layer.ok_or("no active layer")?;
match &d.doc.layer(id).ok_or("no active layer")?.content {
LayerContent::Smart(sm) => Ok(sm),
other => Err(format!("the active layer is {} {} layer, not a smart object", other.article(), other.kind_name())),
}
}
fn has_smart(s: &Session) -> std::result::Result<(), String> {
active_smart(s).map(|_| ())
}
/// Layer ▸ Smart Objects ▸ Reset Transform: one smart object on its own, not fully locked. Measured
/// in the reference application (#18287): greyed with Lock All on the object and with two or more
/// layers selected, whatever their kinds. Lock Position or Lock Image Pixels alone, and a smart
/// object inside a locked group, were not measured: they keep the transform's own refusal.
fn can_reset_transform(s: &Session) -> std::result::Result<(), String> {
active_smart(s)?;
let d = s.active().ok_or("no document open")?;
if d.selected_layers().len() > 1 {
return Err("several layers are selected; Reset Transform works on one smart object".into());
}
if d.active_layer.and_then(|id| d.doc.layer(id)).is_some_and(|l| l.locks.all) {
return Err("the smart object is locked (Lock All)".into());
}
Ok(())
}
fn has_linked(s: &Session) -> std::result::Result<(), String> {
match active_smart(s)?.source {
SmartSource::Linked { .. } => Ok(()),
_ => Err("the smart object is embedded".into()),
}
}
fn linked_layers(doc: &Document) -> Vec<LayerId> {
doc.walk()
.into_iter()
.filter(|(_, _, l)| matches!(&l.content, LayerContent::Smart(sm) if matches!(sm.source, SmartSource::Linked { .. })))
.map(|(_, _, l)| l.id)
.collect()
}
fn has_any_linked(s: &Session) -> std::result::Result<(), String> {
let d = s.active().ok_or("no document open")?;
if linked_layers(&d.doc).is_empty() { Err("the document has no linked smart object".into()) } else { Ok(()) }
}
/// Embed All Linked: reads every linked file first, so a missing one refuses the whole step.
fn embed_all_linked(s: &mut Session, _p: &Value) -> Result<Value> {
let doc = &s.active().ok_or(EngineError::NoDocument)?.doc;
let ids = linked_layers(doc);
if ids.is_empty() {
return Err(other("the document has no linked smart object"));
}
let mut sources = Vec::with_capacity(ids.len());
for id in &ids {
let src = &smart(doc, *id)?.source;
let (file_name, bytes) = source_bytes(&doc.metadata, src).ok_or_else(|| {
let path = match src {
SmartSource::Linked { path } => path.as_str(),
SmartSource::Embedded { .. } => "",
};
other(format!("the linked file can't be read: {path}"))
})?;
sources.push(SmartSource::Embedded { file_name, bytes });
}
let parent = doc.id;
let contents: Vec<SmartContentsId> = ids.iter().map(|id| smart(doc, *id).map(|sm| sm.contents_id)).collect::<Result<_>>()?;
s.edit("Embed All Linked Smart Objects", |doc, _| {
for (id, src) in ids.iter().zip(sources) {
smart_mut(doc, *id)?.source = src;
refresh_or_fail(doc, *id)?;
}
Ok(())
})?;
s.smart_links.retain(|l| l.parent != parent || !contents.contains(&l.contents_id));
Ok(json!({"embedded": ids.iter().map(|l| l.0).collect::<Vec<_>>()}))
}
fn has_doc(s: &Session) -> std::result::Result<(), String> {
s.active().map(|_| ()).ok_or_else(|| "no document open".into())
}
fn can_update_all_contents(s: &Session) -> std::result::Result<(), String> {
let doc = &s.active().ok_or("no document open")?.doc;
if matches!(doc.mode, ColorMode::Bitmap | ColorMode::Indexed) && !doc.walk().iter().any(|(_, _, layer)| matches!(&layer.content, LayerContent::Smart(_))) {
return Err("the document has no smart object".into());
}
Ok(())
}
fn has_smart_filters(s: &Session) -> std::result::Result<(), String> {
if active_smart(s)?.smart_filters.is_empty() { Err("the smart object has no smart filters".into()) } else { Ok(()) }
}
/// Delete Filter Mask with `ifPresent` needs only a smart object.
pub(crate) fn smart_layer_active(s: &Session) -> std::result::Result<(), String> {
active_smart(s).map(|_| ())
}
fn has_filter_mask(s: &Session) -> std::result::Result<(), String> {
active_smart(s)?.filter_mask.as_ref().map(|_| ()).ok_or_else(|| "the smart object has no filter mask".into())
}
fn convertible(s: &Session) -> std::result::Result<(), String> {
let mode = s.active().ok_or("no document open")?.doc.mode;
match mode {
ColorMode::Bitmap => return Err("Convert to Smart Object is unavailable in Bitmap mode".into()),
ColorMode::Indexed => return Err("Convert to Smart Object is unavailable in Indexed Color mode".into()),
_ => {}
}
super::enabled::has_layer(s)
}
fn not_smart(s: &Session) -> std::result::Result<(), String> {
super::enabled::has_layer(s)?;
if active_smart(s).is_ok() { Err("the layer is already a smart object".into()) } else { Ok(()) }
}
/// Convert to Smart Object: refused only when the single targeted layer is already a smart
/// object. A multi-layer selection can still be combined (nesting any smart object in it).
fn convertible_to_smart(s: &Session) -> std::result::Result<(), String> {
convertible(s)?;
if crate::layer_multi_cmds::selected(s).len() <= 1 { not_smart(s) } else { Ok(()) }
}
macro_rules! spec {
($id:literal, $label:literal, $menu:expr, $params:literal, $en:expr, $run:expr) => {
CommandSpec { id: $id, label: $label, menu: $menu, shortcut: None, params: $params, enabled: $en, run: $run, journal: true }
};
}
mod contents;
mod filters;
mod placing;
mod rendering;
#[cfg(test)]
mod reset_transform_noop_tests;
mod source;
pub(crate) use contents::on_close;
pub use contents::{commit_child, contents_of, replace_contents_bytes, replace_contents_from_bytes};
use contents::{edit_contents, export_contents, is_smart_child, path_param, replace_contents, set_source, update_all};
use filters::smart_mut;
pub(crate) use filters::{add_smart_filter, add_smart_filter_in, filter_mask_paintable, filter_mask_surface, refresh_after_filter_mask_edit};
use filters::{blending_options, delete_filter, edit_filters, move_filter, set_filter_params, set_filter_visible, smart, transfer_filters};
pub use placing::detach_psd;
pub(crate) use placing::{affine_homography, set_placement, shift_smart, transform_placement};
use placing::{convert, reset_transform, shift_layer, via_copy};
pub use placing::{layer_to_smart, layers_to_smart, placement};
pub use rendering::WarningRenderSources;
use rendering::refresh_or_fail;
pub use rendering::render_with_warnings_using;
#[cfg(test)]
use rendering::{NativeDigests, blend_surfaces, place, render_in};
pub use rendering::{SourceCacheStatistics, source_cache_statistics};
pub use rendering::{apply_smart_filters, purge_stack_cache, refresh, refresh_layer, render, render_below_filter, stack_cache_bytes, stack_image};
pub(crate) use rendering::{render_with_warnings, source_box};
#[cfg(test)]
use source::svg_source_image_in;
pub use source::vector_scale;
pub use source::{SourceImage, purge_source_cache, source_bytes, source_cache_bytes, source_decode_count, source_image, svg_source_image};
use source::{authorize_source, authorized_source_bytes};
pub(crate) use source::{restore_unrendered, take_caches};
const SO: &[&str] = &["Layer", "Smart Objects"];
const SF: &[&str] = &["Layer", "Smart Filter"];
pub fn specs() -> Vec<CommandSpec> {
vec![
spec!(
"layer.smartObjects.convertToSmartObject",
"Convert to Smart Object",
SO,
r##"{"layer":id?} (default: combine selected layers)"##,
convertible_to_smart,
convert
),
spec!(
"filter.convertForSmartFilters",
"Convert for Smart Filters",
&["Filter"],
r##"{"layer":id?} (default: combine selected layers)"##,
not_smart,
convert
),
spec!("layer.smartObjects.newSmartObjectViaCopy", "New Smart Object via Copy", SO, r##"{"layer":id?}"##, has_smart, via_copy),
spec!("layer.smartObjects.rasterize", "Rasterize", SO, r##"{"layer":id?}"##, has_smart, |s, p| s.execute("layer.rasterize.smartObject", p.clone())),
spec!(
"layer.smartObjects.editContents",
"Edit Contents",
SO,
r##"{"layer":id?} → opens the contents as a new document; saving (layer.smartObjects.saveContents) or closing it updates the smart object"##,
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)"##,
can_reset_transform,
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)?}))
}),
spec!("layer.smartObjects.replaceContents", "Replace Contents…", SO, r##"{"layer":id?,"path":str}"##, has_smart, replace_contents),
spec!(
"layer.smartObjects.exportContents",
"Export Contents…",
SO,
r##"{"layer":id?,"path":str (another extension than the contents' own exports them in that format)}"##,
has_smart,
export_contents
),
spec!("layer.smartObjects.relinkToFile", "Relink to File…", SO, r##"{"layer":id?,"path":str}"##, has_smart, |s, p| {
let path = path_param("layer.smartObjects.relinkToFile", p)?.to_string();
set_source(s, p, "Relink to File", false, |_, _| Ok(SmartSource::Linked { path }))
}),
spec!("layer.smartObjects.updateModifiedContent", "Update Modified Content", SO, r##"{"layer":id?}"##, has_linked, |s, p| {
let id = layer_param(s, p)?;
s.edit("Update Modified Smart Object", |doc, _| refresh_or_fail(doc, id))?;
Ok(json!({"layer": id.0}))
}),
spec!(
"layer.smartObjects.updateChanged",
"Update Changed Linked Content",
&[],
"{} — re-render the active document's linked smart objects whose files changed on disk since they were last seen (size or modification time); the desktop app does this by itself every two seconds, as Photoshop does while a document is open. Files seen for the first time are only remembered. One Update Modified Content undo step per file → {updated: [layer ids]}",
|s| s.active().map(|_| ()).ok_or_else(|| "no document open".into()),
crate::link_watch::update_changed
),
spec!("layer.smartObjects.updateAllModifiedContent", "Update All Modified Content", SO, "{}", can_update_all_contents, |s, _| update_all(s)),
spec!(
"layer.smartObjects.convertToLayers",
"Convert to Layers",
SO,
r##"{"layer":id?} (contents unpacked at the placement: one layer, or a group named after the smart object; smart filters are discarded)"##,
has_smart,
unpack::convert_to_layers
),
spec!("layer.smartObjects.convertToEmbedded", "Embed Linked", SO, r##"{"layer":id?}"##, has_linked, |s, p| {
set_source(s, p, "Convert to Embedded", true, |meta, src| {
let (file_name, bytes) = source_bytes(meta, src).ok_or_else(|| other("the linked file can't be read"))?;
Ok(SmartSource::Embedded { file_name, bytes })
})
}),
spec!(
"layer.smartObjects.embedAllLinked",
"Embed All Linked",
SO,
"{} (every linked smart object of the active document becomes embedded in one history step; a linked file that can't be read refuses the whole step and the error names it) -> {embedded: [layer ids]}",
has_any_linked,
embed_all_linked
),
spec!(
"layer.smartObjects.convertToLinked",
"Convert to Linked…",
SO,
r##"{"layer":id?,"path":str} (writes the contents there)"##,
has_smart,
|s, p| {
let path = path_param("layer.smartObjects.convertToLinked", p)?.to_string();
set_source(s, p, "Convert to Linked", false, |meta, src| {
let (_, bytes) = source_bytes(meta, src).ok_or_else(|| other("the smart object's contents are unavailable"))?;
crate::file_cmds::write_file(&path, &bytes)?;
Ok(SmartSource::Linked { path })
})
}
),
// Smart filters
spec!(
"layer.smartFilter.disableSmartFilters",
"Disable Smart Filters",
SF,
r##"{"layer":id?,"enabled":bool? (default: toggle)}"##,
has_smart_filters,
|s, p| {
edit_filters(s, p, "Disable Smart Filters", |sm| {
sm.filters_enabled = p.get("enabled").and_then(Value::as_bool).unwrap_or(!sm.filters_enabled);
Ok(())
})
}
),
spec!(
"layer.smartFilter.deleteFilterMask",
"Delete Filter Mask",
SF,
r##"{"layer":id?,"ifPresent":bool=false (true: a smart object without a filter mask is left as it is, as a Photoshop action's step needs where Photoshop added the mask by default)}"##,
has_filter_mask,
|s, p| {
if p.get("ifPresent").and_then(Value::as_bool) == Some(true) {
let id = layer_param(s, p)?;
if smart(&s.active().ok_or(EngineError::NoDocument)?.doc, id)?.filter_mask.is_none() {
return Ok(Value::Null);
}
}
edit_filters(s, p, "Delete Filter Mask", |sm| {
sm.filter_mask = None;
Ok(())
})
}
),
spec!(
"layer.smartFilter.disableFilterMask",
"Disable Filter Mask",
SF,
r##"{"layer":id?,"enabled":bool? (default: toggle)}"##,
has_filter_mask,
|s, p| {
edit_filters(s, p, "Disable Filter Mask", |sm| {
if let Some(m) = &mut sm.filter_mask {
m.enabled = p.get("enabled").and_then(Value::as_bool).unwrap_or(!m.enabled);
}
Ok(())
})
}
),
spec!(
"layer.smartFilter.blendingOptions",
"Blending Options…",
SF,
r##"{"layer":id?,"index":u32? (0 = bottom; default top),"blend":str?,"opacity":0..1?}"##,
has_smart_filters,
blending_options
),
spec!("layer.smartFilter.clearSmartFilters", "Clear Smart Filters", SF, r##"{"layer":id?}"##, has_smart_filters, |s, p| {
edit_filters(s, p, "Clear Smart Filters", |sm| {
sm.smart_filters.clear();
sm.filter_mask = None;
Ok(())
})
}),
spec!(
"layer.smartFilter.setVisible",
"Show/Hide Smart Filter",
&[],
r##"{"layer":id?,"index":u32?,"visible":bool? (default: toggle)}"##,
has_smart_filters,
set_filter_visible
),
spec!(
"layer.smartFilter.setParams",
"Edit Smart Filter",
&[],
r##"{"layer":id?,"index":u32?,"params":{…} (merged),"replace":bool=false (params replace the stored ones instead)}"##,
has_smart_filters,
set_filter_params
),
spec!("layer.smartFilter.delete", "Delete Smart Filter", &[], r##"{"layer":id?,"index":u32?}"##, has_smart_filters, delete_filter),
spec!("layer.smartFilter.move", "Move Smart Filter", &[], r##"{"layer":id?,"index":u32?,"to":u32}"##, has_smart_filters, move_filter),
spec!(
"layer.smartFilter.transfer",
"Move or Copy Smart Filters",
&[],
r##"{"from":id,"to":id,"filter":u32? (default: all, with the filter mask),"copy":bool=false}"##,
has_doc,
transfer_filters
),
]
}
mod stack_cache;
mod unpack;
#[cfg(test)]
mod indexed_bitmap_tests;
#[cfg(test)]
mod reset_transform_enabled_tests;
#[cfg(test)]
mod tests;