mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-20 01:14:53 +01:00
Merge remote-tracking branch 'upstream/main' into cache-defaults-3298
This commit is contained in:
commit
9924ebb1e0
171 files changed
+6309
-720
No files matched your search
@@ -28,6 +28,10 @@ jobs:
|
||||
name: Windows installation policy (no app build)
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
# A nightly dry run on main: every release build except macOS, nothing signed or released
|
||||
# (craft-common-actions release.yml `dry-run`). Finds release-only breakage before release day.
|
||||
schedule:
|
||||
- cron: "32 1 * * *"
|
||||
push:
|
||||
branches: [release]
|
||||
workflow_dispatch:
|
||||
@@ -29,6 +33,7 @@ jobs:
|
||||
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
|
||||
uses: storytold/craft-common-actions/.github/workflows/release.yml@main
|
||||
with:
|
||||
dry-run: ${{ github.event_name == 'schedule' }}
|
||||
app-name: effectcraft
|
||||
app-title: EffectCraft
|
||||
version: ${{ inputs.version }}
|
||||
|
||||
@@ -129,6 +129,12 @@ configuration from these Apache-2.0 projects:
|
||||
- `crates/segment/src/mobilesam.rs` follows the MobileSAM, TinyViT and Segment Anything
|
||||
architectures and their Apache-2.0 reference code.
|
||||
|
||||
## Frozen matte regression fixture
|
||||
|
||||
| Asset | Author | Source | Licence |
|
||||
|---|---|---|---|
|
||||
| `crates/engine/src/tests/fixtures/object-matte-0.22.ecproj` | lisn0 | Original work, synthetic project saved by EffectCraft 0.22.0 | MIT OR Apache-2.0 |
|
||||
|
||||
## First-party brand marks
|
||||
|
||||
The ArtCraft name, wordmark and logos are trademarks of the ArtCraft Team. They are not open source and are not
|
||||
|
||||
Generated
+45
-40
@@ -1184,7 +1184,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-update"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"dunce",
|
||||
@@ -1194,6 +1194,7 @@ dependencies = [
|
||||
"image",
|
||||
"log",
|
||||
"plist",
|
||||
"rustix 1.1.5",
|
||||
"semver",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -1420,7 +1421,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"cpal",
|
||||
@@ -1448,7 +1449,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-aep"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-effects",
|
||||
@@ -1462,7 +1463,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-automation"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-engine",
|
||||
"effectcraft-host",
|
||||
@@ -1476,14 +1477,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-av1enc"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"filmcraft-av1",
|
||||
"tempfile",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-cli"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"cpu-time",
|
||||
"effectcraft-automation",
|
||||
@@ -1502,7 +1504,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-color"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
"serde",
|
||||
@@ -1510,7 +1512,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-effects"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"effectcraft-color",
|
||||
@@ -1528,7 +1530,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-engine"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-docpath",
|
||||
@@ -1568,7 +1570,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-export"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-av1enc",
|
||||
"effectcraft-color",
|
||||
@@ -1593,13 +1595,14 @@ dependencies = [
|
||||
"image",
|
||||
"log",
|
||||
"rayon",
|
||||
"tempfile",
|
||||
"thiserror 2.0.21",
|
||||
"web-time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-expr"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"boa_engine",
|
||||
"effectcraft-color",
|
||||
@@ -1616,7 +1619,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-geom"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
"serde",
|
||||
@@ -1624,7 +1627,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-gpu"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"effectcraft-color",
|
||||
@@ -1647,7 +1650,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-helper-host"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"effectcraft-effects",
|
||||
@@ -1658,14 +1661,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-hevcenc"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"filmcraft-hevc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-host"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-engine",
|
||||
"effectcraft-export",
|
||||
@@ -1692,7 +1695,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-interchange"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-keyframe",
|
||||
@@ -1711,7 +1714,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-keyframe"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-geom",
|
||||
@@ -1723,7 +1726,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-lottie"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-docpath",
|
||||
"effectcraft-color",
|
||||
@@ -1741,7 +1744,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-media"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-docpath",
|
||||
"effectcraft-color",
|
||||
@@ -1770,12 +1773,13 @@ dependencies = [
|
||||
"log",
|
||||
"rayon",
|
||||
"serde_json",
|
||||
"tempfile",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-model"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-geom",
|
||||
"image",
|
||||
@@ -1788,14 +1792,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-opusenc"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"filmcraft-opus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-path"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-geom",
|
||||
"effectcraft-keyframe",
|
||||
@@ -1807,7 +1811,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-pdf"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-pdf",
|
||||
"effectcraft-raster",
|
||||
@@ -1823,7 +1827,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-plugin"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-effects",
|
||||
"effectcraft-keyframe",
|
||||
@@ -1835,7 +1839,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-project"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-ecproj-format",
|
||||
"effectcraft-color",
|
||||
@@ -1849,14 +1853,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-psd"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"miniz_oxide 0.9.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-raster"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-geom",
|
||||
@@ -1866,7 +1870,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-render"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-effects",
|
||||
@@ -1890,7 +1894,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-sam2"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"candle-core",
|
||||
@@ -1905,7 +1909,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-script"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"boa_engine",
|
||||
"effectcraft-engine",
|
||||
@@ -1923,7 +1927,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-segment"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"miniz_oxide 0.9.1",
|
||||
"ndarray",
|
||||
@@ -1932,19 +1936,20 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-svg"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-geom",
|
||||
"effectcraft-path",
|
||||
"effectcraft-raster",
|
||||
"kurbo",
|
||||
"log",
|
||||
"rayon",
|
||||
"roxmltree",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-text"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-keyframe",
|
||||
"harfrust",
|
||||
@@ -1956,7 +1961,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-time"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
"serde",
|
||||
@@ -1964,7 +1969,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-track"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-raster",
|
||||
"effectcraft-segment",
|
||||
@@ -1976,7 +1981,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-ui-egui"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"chrono",
|
||||
@@ -2017,11 +2022,11 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-vp9enc"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-web"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-engine",
|
||||
"effectcraft-export",
|
||||
@@ -7848,7 +7853,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7"
|
||||
|
||||
[[package]]
|
||||
name = "xtask"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"pkg-config",
|
||||
|
||||
+2
-2
@@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"]
|
||||
default-members = ["crates/*", "apps/*"]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
edition = "2024"
|
||||
license = "MIT OR Apache-2.0"
|
||||
rust-version = "1.95"
|
||||
@@ -16,7 +16,7 @@ craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-l
|
||||
craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545" }
|
||||
craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ca1b5e2edb8568335c6c14f390307bd5beae8238" }
|
||||
# craft-update: app-specific pretend versions apply to startup and manual checks.
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["egui"] }
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
|
||||
# internal
|
||||
effectcraft-time = { path = "crates/time" }
|
||||
effectcraft-geom = { path = "crates/geom" }
|
||||
|
||||
@@ -1142,11 +1142,37 @@ pub const GLOW_MIDPOINT_MAX: f64 = 0.990_005_493_164_062_5;
|
||||
|
||||
/// The key a popup item is matched by between the two programs: trimmed, lower case, and
|
||||
/// Set Matte's "Red Channel" ... "Alpha Channel" are EffectCraft's "Red" ... "Alpha" (904 E24).
|
||||
///
|
||||
/// Lower case is Unicode's, not only ASCII's: After Effects stores popup items in its UI
|
||||
/// language, so an item can start with a capital outside ASCII (French "Écran"), and its key must
|
||||
/// be the same as for the same word in lower case.
|
||||
pub fn popup_key(item: &str) -> String {
|
||||
let s = item.trim().to_ascii_lowercase();
|
||||
let s = item.trim().to_lowercase();
|
||||
s.strip_suffix(" channel").map(str::to_string).unwrap_or(s)
|
||||
}
|
||||
|
||||
/// The English [`popup_key`] a non-English After Effects popup item name stands for, when this
|
||||
/// table knows it; `None` otherwise. EffectCraft's own popup options are English only, so a
|
||||
/// project saved by an After Effects set to another UI language never matches them by name and
|
||||
/// the parameter falls back to its default (observed on a real "Niveaux" project, French After
|
||||
/// Effects 2026 26.0.0x67: Levels' Channel and Clip to Output Black / White). Add an entry only
|
||||
/// from a confirmed After Effects label, never a guess at a whole menu's translation.
|
||||
pub fn popup_alias(key: &str) -> Option<&'static str> {
|
||||
Some(match key {
|
||||
// French: Levels' Channel popup (`ADBE Easy Levels2-0001`). "Alpha" is the same word in
|
||||
// both languages, so it needs no entry.
|
||||
"rvb" => "rgb",
|
||||
"rouge" => "red",
|
||||
"vert" => "green",
|
||||
"bleu" => "blue",
|
||||
// French: Levels' Clip to Output Black / White popups (`-0008`, `-0009`).
|
||||
"activé" => "on",
|
||||
"désactivé" => "off",
|
||||
"désactivé pour la couleur 32 bpc" => "off for 32 bpc color",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// The table row of an After Effects match name.
|
||||
pub fn by_match_name(match_name: &str) -> Option<&'static EffectMap> {
|
||||
EFFECTS.iter().find(|m| m.match_name == match_name)
|
||||
@@ -1164,3 +1190,17 @@ pub const HUE_SATURATION_MASTER: [&str; 3] = ["hue", "saturation", "lightness"];
|
||||
/// The EffectCraft parameters Levels' RGB channel values (`Extra::Levels`) go to, in the order of
|
||||
/// the data (input black, input white, gamma, output black, output white: 512-516).
|
||||
pub const LEVELS_RGB: [&str; 5] = ["inBlack", "inWhite", "gamma", "outBlack", "outWhite"];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A capital outside ASCII has the same key as its lower case; ASCII keys are unchanged.
|
||||
#[test]
|
||||
fn popup_key_lowers_non_ascii_capitals() {
|
||||
assert_eq!(popup_key("ÉCRAN"), popup_key("écran"));
|
||||
assert_eq!(popup_key(" Désactivé "), "désactivé");
|
||||
assert_eq!(popup_key("Off for 32 bpc Color"), "off for 32 bpc color");
|
||||
assert_eq!(popup_key("Red Channel"), "red");
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@
|
||||
//! import. Hue/Saturation's master values and Levels' values are arbitrary data, decoded by the
|
||||
//! reader (444-446, 512-516).
|
||||
|
||||
use crate::effect_map::{CRYPTOMATTE_OUTPUT, Conv, EFFECTS, EffectMap, Extra, HUE_SATURATION_MASTER, LEVELS_RGB, ParamMap, popup_key};
|
||||
use crate::effect_map::{CRYPTOMATTE_OUTPUT, Conv, EFFECTS, EffectMap, Extra, HUE_SATURATION_MASTER, LEVELS_RGB, ParamMap, popup_alias, popup_key};
|
||||
use crate::{
|
||||
CryptomatteData, CurvesData, DefinitionSource, Effect as AepEffect, EffectDefinitions, EffectParam, ExtractorChannels, HueSaturationData,
|
||||
Layer as AepLayer, LayerRef, LevelsData, MeshData, ParamKind, ParamValue,
|
||||
@@ -83,11 +83,11 @@ pub(crate) fn import_effects<'f, 'a>(
|
||||
}
|
||||
let shown = fx.display_name().unwrap_or_else(|| fx.match_name().to_string());
|
||||
let Some(map) = EFFECTS.iter().find(|m| m.match_name == fx.match_name()) else {
|
||||
col.lose(LossCategory::Effects, format!("{}: '{}' has the effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name()));
|
||||
col.lose_unmapped_effect(fx.match_name(), format!("{}: '{}' has the effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name()));
|
||||
continue;
|
||||
};
|
||||
let Some(spec) = effectcraft_effects::find(map.effect) else {
|
||||
col.lose(LossCategory::Effects, format!("{}: '{}' has the effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name()));
|
||||
col.lose_unmapped_effect(fx.match_name(), format!("{}: '{}' has the effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name()));
|
||||
continue;
|
||||
};
|
||||
if defs.source(&fx) == DefinitionSource::Missing {
|
||||
@@ -422,8 +422,8 @@ pub(crate) fn resolve_layer_refs(
|
||||
}
|
||||
}
|
||||
|
||||
/// The EffectCraft item of each file item (by name, ignoring case and separators); `None` when
|
||||
/// the parameter here is not a popup.
|
||||
/// The EffectCraft item of each file item (by name, ignoring case and separators, and a known
|
||||
/// non-English name for it, [`popup_alias`]); `None` when the parameter here is not a popup.
|
||||
fn popup_mapping(file_items: &[String], ui: &ParamUi) -> Option<Vec<Option<u32>>> {
|
||||
let ParamUi::Popup { options } = ui else { return None };
|
||||
let norm = popup_key;
|
||||
@@ -434,7 +434,12 @@ fn popup_mapping(file_items: &[String], ui: &ParamUi) -> Option<Vec<Option<u32>>
|
||||
if item.trim() == "(-" {
|
||||
return None;
|
||||
}
|
||||
options.iter().position(|o| norm(o) == norm(item)).and_then(|i| u32::try_from(i).ok())
|
||||
let key = norm(item);
|
||||
options
|
||||
.iter()
|
||||
.position(|o| norm(o) == key)
|
||||
.or_else(|| popup_alias(&key).and_then(|a| options.iter().position(|o| norm(o) == a)))
|
||||
.and_then(|i| u32::try_from(i).ok())
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
|
||||
@@ -231,15 +231,21 @@ pub(crate) fn import_transform(
|
||||
}
|
||||
import_expression(&node, prop, col);
|
||||
}
|
||||
if let Some(PropNode::Other(_)) = group.get("ADBE Orientation") {
|
||||
if let Some(PropNode::Other(otst)) = group.get("ADBE Orientation") {
|
||||
match (layer.orientation(), target.get_mut("orientation")) {
|
||||
(Some(o), Some(prop)) if !o.animated && !o.has_expression && sane(&Value::Vec3(o.degrees)) => prop.value = Value::Vec3(o.degrees),
|
||||
(Some(o), Some(prop)) if o.animated || sane(&Value::Vec3(o.degrees)) => {
|
||||
if o.animated {
|
||||
import_orientation_keys(layer, layer_name, tm, prop, col);
|
||||
} else {
|
||||
prop.value = Value::Vec3(o.degrees);
|
||||
}
|
||||
// The expression is stored in the `otst`'s `tdbs` as on any property (#3087).
|
||||
if let Some(node) = otst.child_list(b"tdbs").and_then(AepProp::from_chunk) {
|
||||
import_expression(&node, prop, col);
|
||||
}
|
||||
}
|
||||
// A layer without an orientation (an ambient light) loses nothing with the default.
|
||||
(Some(o), None) if !o.animated && !o.has_expression && o.degrees == [0.0; 3] => {}
|
||||
(Some(o), Some(prop)) if o.animated && !o.has_expression => import_orientation_keys(layer, layer_name, tm, prop, col),
|
||||
(Some(o), Some(_)) if o.has_expression => {
|
||||
col.lose(LossCategory::TransformProperties, format!("{layer_name}: ADBE Orientation expression (not decoded)"));
|
||||
}
|
||||
_ => col.lose(LossCategory::TransformProperties, format!("{layer_name}: ADBE Orientation")),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,8 +98,12 @@ pub struct Loss {
|
||||
pub category: LossCategory,
|
||||
/// How many occurrences (exact).
|
||||
pub count: usize,
|
||||
/// Up to [`MAX_DETAILS`] example lines.
|
||||
/// Example lines: up to [`MAX_DETAILS`], and for effects with no counterpart one more line per
|
||||
/// effect (with its number of instances).
|
||||
pub details: Vec<String>,
|
||||
/// How many of the `count` occurrences no line of `details` describes.
|
||||
#[serde(default)]
|
||||
pub unlisted: usize,
|
||||
}
|
||||
|
||||
/// What arrived in the project.
|
||||
@@ -269,19 +273,58 @@ pub(crate) struct Collector {
|
||||
losses: BTreeMap<LossCategory, Loss>,
|
||||
pub missing_footage: Vec<String>,
|
||||
pub missing_fonts: Vec<String>,
|
||||
/// The effects with no counterpart, by match name: the first instance's report line and how
|
||||
/// many instances there were. They get a line each in [`Collector::finish`], so one that
|
||||
/// comes after the example lines are used up is still named (#3365).
|
||||
unmapped_effects: BTreeMap<String, (String, usize)>,
|
||||
}
|
||||
|
||||
/// How many different unmapped effects get a line of their own (a file names at most this many).
|
||||
const MAX_UNMAPPED_EFFECTS: usize = 100;
|
||||
|
||||
impl Collector {
|
||||
/// Records one loss.
|
||||
pub fn lose(&mut self, category: LossCategory, detail: impl Into<String>) {
|
||||
let loss = self.losses.entry(category).or_insert_with(|| Loss { category, count: 0, details: Vec::new() });
|
||||
let loss = self.losses.entry(category).or_insert_with(|| Loss { category, count: 0, details: Vec::new(), unlisted: 0 });
|
||||
loss.count = loss.count.saturating_add(1);
|
||||
if loss.details.len() < MAX_DETAILS {
|
||||
loss.details.push(detail.into());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finish(self) -> ImportReport {
|
||||
/// Records one instance of an effect with no counterpart. The loss is counted now; its line is
|
||||
/// made in [`Collector::finish`], one per effect with the number of instances, so that many
|
||||
/// other losses cannot hide which effects were dropped.
|
||||
pub fn lose_unmapped_effect(&mut self, match_name: &str, detail: String) {
|
||||
let loss =
|
||||
self.losses.entry(LossCategory::Effects).or_insert_with(|| Loss { category: LossCategory::Effects, count: 0, details: Vec::new(), unlisted: 0 });
|
||||
loss.count = loss.count.saturating_add(1);
|
||||
if let Some(seen) = self.unmapped_effects.get_mut(match_name) {
|
||||
seen.1 = seen.1.saturating_add(1);
|
||||
} else if self.unmapped_effects.len() < MAX_UNMAPPED_EFFECTS {
|
||||
self.unmapped_effects.insert(match_name.to_string(), (detail, 1));
|
||||
}
|
||||
// (Past the limit an effect is only counted: its name is not worth a line of its own.)
|
||||
}
|
||||
|
||||
pub fn finish(mut self) -> ImportReport {
|
||||
// Every line describes one occurrence, except an unmapped effect's, which describes all
|
||||
// its instances.
|
||||
for loss in self.losses.values_mut() {
|
||||
loss.unlisted = loss.count.saturating_sub(loss.details.len());
|
||||
}
|
||||
if let Some(loss) = self.losses.get_mut(&LossCategory::Effects) {
|
||||
// The most used effects first.
|
||||
let mut named: Vec<(String, usize)> = std::mem::take(&mut self.unmapped_effects).into_values().collect();
|
||||
named.sort_by_key(|n| std::cmp::Reverse(n.1));
|
||||
let described = named.iter().fold(0usize, |sum, (_, n)| sum.saturating_add(*n));
|
||||
loss.unlisted = loss.unlisted.saturating_sub(described);
|
||||
let lines = named.into_iter().map(|(first, n)| match n {
|
||||
1 => first,
|
||||
_ => format!("{first}, and {} more like it", n - 1),
|
||||
});
|
||||
loss.details.splice(0..0, lines);
|
||||
}
|
||||
ImportReport {
|
||||
imported: self.imported,
|
||||
losses: self.losses.into_values().collect(),
|
||||
@@ -301,3 +344,65 @@ pub const LIMITATIONS: &[&str] = &[
|
||||
"Text: per-character 3D, text on a path and several style slots of unknown meaning are not decoded (stored values that differ from the default are reported as text layer features).",
|
||||
"Footage that is not a solid is linked by its stored path with its stored frame rate and duration; the importer never opens the files (use Reload Footage after the files are found).",
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// #3365: with other losses filling the 20 example lines, an effect with no counterpart still
|
||||
/// gets its own line, with how many instances were lost. The category's count is unchanged.
|
||||
#[test]
|
||||
fn every_unmapped_effect_is_named_however_many_other_losses_there_are() {
|
||||
let mut col = Collector::default();
|
||||
for i in 0..30 {
|
||||
col.lose(LossCategory::Effects, format!("C: 'L{i}': parameter P not imported"));
|
||||
}
|
||||
for i in 0..25 {
|
||||
col.lose_unmapped_effect("CC Cylinder", format!("C: 'Cyl{i}' has the effect 'CC Cylinder' (CC Cylinder)"));
|
||||
}
|
||||
col.lose_unmapped_effect("Pseudo/", "C: 'D' has the effect 'Dropdown' (Pseudo/)".to_string());
|
||||
let report = col.finish();
|
||||
let effects = report.losses.iter().find(|l| l.category == LossCategory::Effects).unwrap();
|
||||
assert_eq!(effects.count, 56, "30 parameters, 25 cylinders and 1 dropdown");
|
||||
let named = |what: &str| effects.details.iter().filter(|d| d.contains(what)).collect::<Vec<_>>();
|
||||
assert_eq!(named("(CC Cylinder)").len(), 1, "{:?}", effects.details);
|
||||
assert!(named("(CC Cylinder)")[0].contains("24 more"), "{:?}", named("(CC Cylinder)"));
|
||||
// One instance stays the single line it was.
|
||||
assert_eq!(named("(Pseudo/)"), ["C: 'D' has the effect 'Dropdown' (Pseudo/)"]);
|
||||
assert!(effects.details.len() <= MAX_DETAILS + 2, "the examples stay bounded");
|
||||
// 20 parameter lines, the cylinders' line and the dropdown's describe 46; 10 parameters
|
||||
// have no line.
|
||||
assert_eq!(effects.unlisted, 10);
|
||||
// The most used effect comes first.
|
||||
assert!(effects.details[0].contains("(CC Cylinder)"), "{:?}", effects.details);
|
||||
}
|
||||
|
||||
/// #3365: each category says how many of its occurrences have no example line.
|
||||
#[test]
|
||||
fn a_loss_counts_the_occurrences_without_a_line() {
|
||||
let mut col = Collector::default();
|
||||
for i in 0..27 {
|
||||
col.lose(LossCategory::Masks, format!("mask {i}"));
|
||||
}
|
||||
col.lose(LossCategory::ShapeLayers, "one");
|
||||
let report = col.finish();
|
||||
let unlisted = |c| report.losses.iter().find(|l| l.category == c).map(|l| (l.details.len(), l.unlisted));
|
||||
assert_eq!(unlisted(LossCategory::Masks), Some((MAX_DETAILS, 7)));
|
||||
assert_eq!(unlisted(LossCategory::ShapeLayers), Some((1, 0)));
|
||||
}
|
||||
|
||||
/// A file with more than [`MAX_UNMAPPED_EFFECTS`] different unmapped effects names that many
|
||||
/// and counts the rest, whatever room the example lines have.
|
||||
#[test]
|
||||
fn at_most_the_limit_of_unmapped_effects_are_named() {
|
||||
let mut col = Collector::default();
|
||||
for i in 0..=MAX_UNMAPPED_EFFECTS {
|
||||
col.lose_unmapped_effect(&format!("Effect {i}"), format!("C: 'L' has the effect 'Effect {i}' (Effect {i})"));
|
||||
}
|
||||
let report = col.finish();
|
||||
let effects = report.losses.iter().find(|l| l.category == LossCategory::Effects).unwrap();
|
||||
assert_eq!(effects.count, MAX_UNMAPPED_EFFECTS + 1, "every instance is counted");
|
||||
assert_eq!(effects.details.len(), MAX_UNMAPPED_EFFECTS, "{} lines", effects.details.len());
|
||||
assert_eq!(effects.unlisted, 1, "the effect past the limit has no line");
|
||||
}
|
||||
}
|
||||
@@ -457,18 +457,22 @@ fn shift_copy_offset(p: &mut effectcraft_project::Property, cx: &mut Cx<'_, '_>)
|
||||
}
|
||||
}
|
||||
|
||||
/// Reports a property whose only sampled value is the default 1.
|
||||
/// Reports a property whose only sampled value is the default 1. A record in the default state
|
||||
/// holds After Effects' default (docs/aep-format.md 3.4, state), so it holds nothing to lose.
|
||||
fn not_default(g: &AepGroup<'_, '_>, match_name: &str, what: &str, cx: &mut Cx<'_, '_>) {
|
||||
if let Some(PropNode::Property(p)) = g.get(match_name)
|
||||
&& stored(&p)
|
||||
&& (p.is_animated() || p.expression().is_some() || p.static_value().and_then(|v| v.first().copied()).is_some_and(|v| v != 1.0))
|
||||
{
|
||||
cx.col.lose(LossCategory::ShapeLayers, format!("{}: {what} not imported", cx.what));
|
||||
}
|
||||
}
|
||||
|
||||
/// Reports a property whose only sampled value is 0.
|
||||
/// Reports a property whose only sampled value is 0 (a record in the default state, as above,
|
||||
/// holds nothing to lose).
|
||||
fn not_zero(g: &AepGroup<'_, '_>, match_name: &str, what: &str, cx: &mut Cx<'_, '_>) {
|
||||
if let Some(PropNode::Property(p)) = g.get(match_name)
|
||||
&& stored(&p)
|
||||
&& (p.is_animated() || p.expression().is_some() || p.static_value().and_then(|v| v.first().copied()).is_some_and(|v| v != 0.0))
|
||||
{
|
||||
cx.col.lose(LossCategory::ShapeLayers, format!("{}: {what} not imported", cx.what));
|
||||
@@ -490,3 +494,41 @@ fn unknown(g: &AepGroup<'_, '_>, known: &[&str], what: &str, cx: &mut Cx<'_, '_>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::Chunk;
|
||||
|
||||
/// A group holding `match_name` as a one-component property in state `tdsb` with value `v`.
|
||||
fn group_with(match_name: &str, tdsb: u32, v: f64) -> Chunk<'static> {
|
||||
let mut name = match_name.as_bytes().to_vec();
|
||||
name.resize(40, 0);
|
||||
let mut tdb4 = vec![0u8; 124];
|
||||
tdb4[3] = 1;
|
||||
let tdbs = Chunk::list(
|
||||
*b"tdbs",
|
||||
vec![Chunk::data(*b"tdsb", tdsb.to_be_bytes().to_vec()), Chunk::data(*b"tdb4", tdb4), Chunk::data(*b"cdat", v.to_be_bytes().to_vec())],
|
||||
);
|
||||
Chunk::list(*b"tdgp", vec![Chunk::data(*b"tdmn", name), tdbs])
|
||||
}
|
||||
|
||||
fn reported(match_name: &str, tdsb: u32, v: f64, check: fn(&AepGroup<'_, '_>, &str, &str, &mut Cx<'_, '_>)) -> usize {
|
||||
let chunk = group_with(match_name, tdsb, v);
|
||||
let g = AepGroup::from_chunk(&chunk).unwrap();
|
||||
let tm = TimeMap { timescale: 30, frame_duration: 1, rate: crate::import::time::frame_rate(30, 1).unwrap() };
|
||||
let (mut next, mut col) = (1, Collector::default());
|
||||
check(&g, match_name, "it", &mut Cx { what: "L", tm: &tm, ids: &mut Ids(&mut next), col: &mut col });
|
||||
col.finish().losses.iter().map(|l| l.count).sum()
|
||||
}
|
||||
|
||||
/// #3087: a guide rectangle's Shape Direction recorded in the default state (`tdsb` 3, holding
|
||||
/// 2) is After Effects' default and is no loss; written (`tdsb` 1) with 2, it is one.
|
||||
#[test]
|
||||
fn a_default_state_record_is_no_loss() {
|
||||
assert_eq!(reported("ADBE Vector Shape Direction", 3, 2.0, not_default), 0);
|
||||
assert_eq!(reported("ADBE Vector Shape Direction", 1, 2.0, not_default), 1);
|
||||
assert_eq!(reported("ADBE Vector Grad HiLite Length", 3, 5.0, not_zero), 0);
|
||||
assert_eq!(reported("ADBE Vector Grad HiLite Length", 1, 5.0, not_zero), 1);
|
||||
}
|
||||
}
|
||||
@@ -842,3 +842,101 @@ fn a_default_state_colour_record_holds_the_value() {
|
||||
assert_eq!(colour(&default_state), colour(&written));
|
||||
assert_ne!(colour(&default_state), colour(&unchanged), "the recorded colour, not the default");
|
||||
}
|
||||
|
||||
/// #3087: an expression on Orientation arrives with the stored value, as on any property; it was
|
||||
/// dropped together with the value and reported as "not decoded".
|
||||
#[test]
|
||||
fn an_orientation_expression_is_imported_with_its_value() {
|
||||
let bytes = mutate(&mut |c| {
|
||||
if !c.is_list(b"otst") {
|
||||
return;
|
||||
}
|
||||
for child in c.children_mut() {
|
||||
if child.is_list(b"tdbs") {
|
||||
let Body::List { children, .. } = &mut child.body else { continue };
|
||||
for k in children.iter_mut() {
|
||||
if k.is(b"tdsb") {
|
||||
set_data(k, 0, &1u32.to_be_bytes());
|
||||
} else if k.is(b"tdb4") {
|
||||
set_data(k, 120, &[1]);
|
||||
}
|
||||
}
|
||||
children.push(Chunk::data(*b"Utf8", b"[0, time * 90, 0]".to_vec()));
|
||||
} else if child.is_list(b"otky") {
|
||||
for k in child.children_mut() {
|
||||
let degrees: Vec<u8> = [10.0f64, 20.0, 30.0].iter().flat_map(|d| d.to_be_bytes()).collect();
|
||||
set_data(k, 0, °rees);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
let (p, r) = run(&bytes, true);
|
||||
assert_eq!(losses(&r.unwrap(), LossCategory::TransformProperties), Vec::<String>::new());
|
||||
let mut seen = 0;
|
||||
for item in p.items.values() {
|
||||
let ItemKind::Comp(c) = &item.kind else { continue };
|
||||
for layer in &c.layers {
|
||||
let Some(o) = layer.transform().and_then(|t| t.prop("orientation")).filter(|o| o.expr.is_some()) else { continue };
|
||||
assert_eq!(o.value, effectcraft_keyframe::Value::Vec3([10.0, 20.0, 30.0]), "{}", layer.name);
|
||||
assert_eq!(o.expr.as_ref().map(|e| (e.text.as_str(), e.enabled)), Some(("[0, time * 90, 0]", true)), "{}", layer.name);
|
||||
seen += 1;
|
||||
}
|
||||
}
|
||||
assert!(seen > 0);
|
||||
}
|
||||
|
||||
/// `SYNTHETIC` with Levels' Channel and Clip to Output Black / White popups' item names (`pdnm`)
|
||||
/// rewritten to the French labels a French-language After Effects stores there, keeping the same
|
||||
/// item order so the index each popup is set to is unaffected.
|
||||
fn with_french_levels_popups() -> Vec<u8> {
|
||||
let (mut channel, mut clip) = (0, 0);
|
||||
let bytes = mutate(&mut |c| {
|
||||
if !c.is(b"Utf8") {
|
||||
return;
|
||||
}
|
||||
if c.bytes() == Some(b"RGB|Red|Green|Blue|Alpha".as_slice()) {
|
||||
*c = Chunk::data(*b"Utf8", b"RVB|Rouge|Vert|Bleu|Alpha".to_vec());
|
||||
channel += 1;
|
||||
} else if c.bytes() == Some(b"On|Off|Off for 32 bpc Color".as_slice()) {
|
||||
*c = Chunk::data(*b"Utf8", "Activé|Désactivé|Désactivé pour la couleur 32 bpc".as_bytes().to_vec());
|
||||
clip += 1;
|
||||
}
|
||||
});
|
||||
assert_eq!(channel, 2, "Levels' Channel pdnm");
|
||||
assert_eq!(clip, 4, "Levels' two Clip pdnm (Black and White), each stored twice");
|
||||
bytes
|
||||
}
|
||||
|
||||
/// A popup's item names in the file are matched to EffectCraft's own (English) ones by name
|
||||
/// (`popup_key`): a project saved by a French After Effects stores "RVB", "Activé", "Désactivé
|
||||
/// pour la couleur 32 bpc" for Levels' Channel and Clip to Output Black / White (observed in a
|
||||
/// real "Niveaux" effect, After Effects 2026 26.0.0x67). Before `popup_alias`, none of these
|
||||
/// matched EffectCraft's English-only options, so each came back "has no counterpart, default
|
||||
/// used" and the real selection was silently lost.
|
||||
#[test]
|
||||
fn french_levels_popup_items_are_recognized() {
|
||||
let (unchanged, r1) = run(SYNTHETIC, true);
|
||||
let (french, r2) = run(&with_french_levels_popups(), true);
|
||||
let (r1, r2) = (r1.unwrap(), r2.unwrap());
|
||||
let found = losses(&r2, LossCategory::Effects);
|
||||
assert!(!found.iter().any(|d| d.contains("has no counterpart")), "{found:?}");
|
||||
for id in ["channel", "clipToOutputBlack", "clipToOutputWhite"] {
|
||||
let want: Vec<_> = effect_values(&unchanged).into_iter().filter(|(p, _)| p == id).collect();
|
||||
let got: Vec<_> = effect_values(&french).into_iter().filter(|(p, _)| p == id).collect();
|
||||
assert_eq!(got, want, "{id}");
|
||||
}
|
||||
assert_eq!(r1.report.imported.effects, r2.report.imported.effects);
|
||||
}
|
||||
|
||||
/// `popup_alias` must cover every item of a popup it claims to know, not only the one a first
|
||||
/// report happened to show: Channel's French "Rouge", "Vert", "Bleu" ("Alpha" is the same word in
|
||||
/// both languages) alongside the already-covered "RVB" (`effect_map.rs`, reviewed on #3432).
|
||||
#[test]
|
||||
fn french_levels_channel_aliases_cover_every_item() {
|
||||
use effectcraft_aep::effect_map::popup_alias;
|
||||
for (french, english) in [("rvb", "rgb"), ("rouge", "red"), ("vert", "green"), ("bleu", "blue")] {
|
||||
assert_eq!(popup_alias(french), Some(english), "{french}");
|
||||
}
|
||||
// "Alpha" needs no alias: the same word in both languages.
|
||||
assert_eq!(popup_alias("alpha"), None);
|
||||
}
|
||||
@@ -75,6 +75,9 @@ const REFUSED: &[&str] = &[
|
||||
"library.preview",
|
||||
];
|
||||
|
||||
/// Most numbered folders `check_render_folder` inspects for one output; more are refused.
|
||||
const MAX_NUMBERED_FOLDERS: usize = 4096;
|
||||
|
||||
/// Commands that read files named by these parameters.
|
||||
const READS: &[(&str, &[&str])] = &[
|
||||
("file.open", &["path"]),
|
||||
@@ -202,6 +205,80 @@ impl Confinement {
|
||||
if self.roots(access).any(|r| real.starts_with(r)) { Ok(real) } else { Err(denied("it is outside the automation roots")) }
|
||||
}
|
||||
|
||||
/// A render output's folder holds no symbolic link that leads outside the write roots. An
|
||||
/// image sequence is written to numbered names (`out_[#####].png` → `out_00001.png`, and a
|
||||
/// sequence output without `#` gets `_00001` too) that the check of its template never
|
||||
/// resolved, and each frame file is created through whatever link already sits at its name,
|
||||
/// truncating the file it points to. A link that can't be resolved counts as outside: the
|
||||
/// render would create its target. When the frame number is in a folder name, the same goes for
|
||||
/// each existing numbered folder and the folder it holds the frames in.
|
||||
fn check_render_folder(&self, output: &str) -> Result<()> {
|
||||
let real = self.check(output, Access::Write)?;
|
||||
if let Some(dir) = real.parent() {
|
||||
self.check_no_outward_links(dir, output)?;
|
||||
}
|
||||
// The frame number may sit in a folder name (`shots_[##]/f_[#####].png`): the folders the
|
||||
// frames go to are the numbered ones that exist, which the template's own parent is not.
|
||||
let (before, _, after) = effectcraft_engine::project::render_queue::sequence_parts(output);
|
||||
let sep = ['/', '\\'];
|
||||
let Some((dir_tail, rest)) = after.split_once(sep) else { return Ok(()) };
|
||||
let (parent, prefix) = before.rsplit_once(sep).unwrap_or(("", &before));
|
||||
// A parent that doesn't exist has no numbered folders; one that can't be listed might.
|
||||
let scan = if parent.is_empty() { Path::new("/") } else { Path::new(parent) };
|
||||
let entries = match std::fs::read_dir(scan) {
|
||||
Ok(entries) => entries,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
|
||||
Err(e) => return Err(Error::BadArgs(format!("cannot render to `{output}`: its numbered folders in `{}` can't be checked: {e}", scan.display()))),
|
||||
};
|
||||
let mut seen = 0;
|
||||
for e in entries {
|
||||
let e =
|
||||
e.map_err(|e| Error::BadArgs(format!("cannot render to `{output}`: its numbered folders in `{}` can't be checked: {e}", scan.display())))?;
|
||||
let name = e.file_name().to_string_lossy().into_owned();
|
||||
let Some(digits) = name.strip_prefix(prefix).and_then(|n| n.strip_suffix(dir_tail)) else { continue };
|
||||
if !digits.strip_prefix('-').unwrap_or(digits).bytes().all(|b| b.is_ascii_digit()) || digits.is_empty() {
|
||||
continue;
|
||||
}
|
||||
seen += 1;
|
||||
if seen > MAX_NUMBERED_FOLDERS {
|
||||
return Err(Error::BadArgs(format!("cannot render to `{output}`: more than {MAX_NUMBERED_FOLDERS} numbered folders already exist to check")));
|
||||
}
|
||||
// The numbered folder itself may be a link out; `check` resolves every existing part.
|
||||
let frame_file = e.path().join(rest);
|
||||
let frame_file = frame_file.to_string_lossy();
|
||||
self.check(&frame_file, Access::Write)?;
|
||||
if let Some(dir) = Path::new(&*frame_file).parent().and_then(resolve) {
|
||||
self.check_no_outward_links(&dir, output)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `dir` holds no symbolic link that leads outside the write roots.
|
||||
fn check_no_outward_links(&self, dir: &Path, output: &str) -> Result<()> {
|
||||
// A folder that does not exist yet holds no link; one that can't be listed might.
|
||||
let cannot = |e: std::io::Error| Error::BadArgs(format!("cannot render to `{output}`: its folder `{}` can't be checked for links: {e}", dir.display()));
|
||||
let entries = match std::fs::read_dir(dir) {
|
||||
Ok(entries) => entries,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
|
||||
Err(e) => return Err(cannot(e)),
|
||||
};
|
||||
for e in entries {
|
||||
let e = e.map_err(cannot)?;
|
||||
if !e.file_type().map_err(cannot)?.is_symlink() {
|
||||
continue;
|
||||
}
|
||||
let link = e.path();
|
||||
if !link.canonicalize().is_ok_and(|r| self.write.iter().any(|w| r.starts_with(w))) {
|
||||
return Err(Error::BadArgs(format!(
|
||||
"cannot render to `{output}`: its folder holds `{}`, a link that leads outside the automation write roots, where a numbered frame could be written",
|
||||
link.display()
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check engine command `id` with `params` before it runs. Returns what must still be looked
|
||||
/// up and passed to [`Confinement::check_lookup`] for commands that write implicitly.
|
||||
pub fn check_command(&self, id: &str, params: &Value) -> Result<Vec<Lookup>> {
|
||||
@@ -221,6 +298,12 @@ impl Confinement {
|
||||
for step in params.get("steps").and_then(Value::as_array).into_iter().flatten() {
|
||||
let Some(inner) = step.get("command").or_else(|| step.get("id")).and_then(Value::as_str) else { continue };
|
||||
let inner_params = step.get("params").cloned().unwrap_or(Value::Null);
|
||||
// A whole `params` given as a `$N` reference becomes an earlier step's result
|
||||
// only as the batch runs, so none of its file paths would have been checked
|
||||
// (`essential.templateInfo` returns a manifest naming any file at all).
|
||||
if !matches!(inner_params, Value::Null | Value::Object(_)) {
|
||||
return Err(Error::BadArgs(format!("`{inner}` in a batch: `params` must be an object while file access is confined")));
|
||||
}
|
||||
let named = keys_of(READS, inner).or_else(|| keys_of(WRITES, inner)).unwrap_or(&[]);
|
||||
let mixed = keys_of(MIXED_WRITES, inner).unwrap_or(&[]);
|
||||
// A step's `$N` reference is resolved only as the batch runs: never for a file.
|
||||
@@ -258,6 +341,9 @@ impl Confinement {
|
||||
Some(s) => {
|
||||
gave_a_file |= v.is_string();
|
||||
self.check(s, access)?;
|
||||
if QUEUES_AN_OUTPUT.contains(&id) || id.starts_with("renderQueue.") {
|
||||
self.check_render_folder(s)?;
|
||||
}
|
||||
}
|
||||
None => return Err(Error::BadArgs(format!("`{id}`: `{key}` must be a path"))),
|
||||
}
|
||||
@@ -321,11 +407,15 @@ impl Confinement {
|
||||
let items = found.get("items").or(Some(found)).and_then(Value::as_array).cloned().unwrap_or_default();
|
||||
for it in items.iter().filter(|it| it.get("render").and_then(Value::as_bool) != Some(false)) {
|
||||
let extra = it.get("extraOutputs").and_then(Value::as_array).into_iter().flatten().map(|o| o.get("outputPath"));
|
||||
for p in std::iter::once(it.get("outputPath")).chain(extra).chain([it.get("logPath")]).flatten() {
|
||||
for p in std::iter::once(it.get("outputPath")).chain(extra).flatten() {
|
||||
if let Some(p) = p.as_str() {
|
||||
self.check(p, Access::Write)?;
|
||||
self.check_render_folder(p)?;
|
||||
}
|
||||
}
|
||||
if let Some(p) = it.get("logPath").and_then(Value::as_str) {
|
||||
self.check(p, Access::Write)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -789,6 +879,112 @@ mod tests {
|
||||
] {
|
||||
assert!(c.check_command("effect.plugins.install", ¶ms).is_err(), "{params}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_batch_step_whose_params_are_a_reference_is_refused() {
|
||||
let base = std::env::temp_dir().join(format!("ec-confine-batch-ref-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
let (read, write) = (base.join("in"), base.join("out"));
|
||||
for d in [&read, &write] {
|
||||
std::fs::create_dir_all(d).unwrap();
|
||||
}
|
||||
let c = Confinement::new(std::slice::from_ref(&read), std::slice::from_ref(&write)).unwrap();
|
||||
let info = read.join("t.ectemplate").to_string_lossy().into_owned();
|
||||
// Step 2's params are step 1's result, which can name any `path`.
|
||||
let steps = serde_json::json!({"steps": [
|
||||
{"command": "essential.templateInfo", "params": {"path": info}},
|
||||
{"command": "file.saveAs", "params": "$1"},
|
||||
]});
|
||||
let e = c.check_command("engine.batch", &steps).unwrap_err().to_string();
|
||||
assert!(e.contains("must be an object"), "{e}");
|
||||
for params in [serde_json::json!("$1.manifest"), serde_json::json!(["$1"]), serde_json::json!(7)] {
|
||||
let steps = serde_json::json!({"steps": [{"command": "file.saveAs", "params": params}]});
|
||||
assert!(c.check_command("engine.batch", &steps).is_err(), "{params}");
|
||||
}
|
||||
// Object params (or none) are checked as before.
|
||||
let saved = write.join("p.ecproj").to_string_lossy().into_owned();
|
||||
let steps = serde_json::json!({"steps": [{"command": "file.saveAs", "params": {"path": saved}}, {"command": "edit.undo"}]});
|
||||
assert!(c.check_command("engine.batch", &steps).is_ok());
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_render_folder_with_a_link_out_of_the_write_roots_is_refused() {
|
||||
let base = std::env::temp_dir().join(format!("ec-confine-seq-link-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
let (read, write, outside) = (base.join("in"), base.join("out"), base.join("elsewhere"));
|
||||
for d in [&read, &write, &outside] {
|
||||
std::fs::create_dir_all(d).unwrap();
|
||||
}
|
||||
let c = Confinement::new(std::slice::from_ref(&read), std::slice::from_ref(&write)).unwrap();
|
||||
let template = write.join("frames_[#####].png").to_string_lossy().into_owned();
|
||||
let add = serde_json::json!({"output": template});
|
||||
let queue = serde_json::json!({"items": [{"outputPath": template}]});
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok());
|
||||
assert!(c.check_lookup(&Lookup::RenderOutputs, &queue).is_ok());
|
||||
// A link inside the write root is fine.
|
||||
std::os::unix::fs::symlink(write.join("kept.png"), write.join("frames_00002.png")).unwrap();
|
||||
std::fs::write(write.join("kept.png"), b"x").unwrap();
|
||||
assert!(c.check_lookup(&Lookup::RenderOutputs, &queue).is_ok());
|
||||
// Frame 1's name leads outside: rendering would truncate that file.
|
||||
std::os::unix::fs::symlink(outside.join("victim"), write.join("frames_00001.png")).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_err());
|
||||
assert!(c.check_command("render.preRender", &add).is_err());
|
||||
let e = c.check_lookup(&Lookup::RenderOutputs, &queue).unwrap_err().to_string();
|
||||
assert!(e.contains("frames_00001.png"), "{e}");
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_render_folder_that_cannot_be_listed_is_refused() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let base = std::env::temp_dir().join(format!("ec-confine-seq-nolist-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
let write = base.join("out");
|
||||
std::fs::create_dir_all(&write).unwrap();
|
||||
let c = Confinement::new(&[], std::slice::from_ref(&write)).unwrap();
|
||||
let template = write.join("frames_[#####].png").to_string_lossy().into_owned();
|
||||
let add = serde_json::json!({"output": template});
|
||||
// Writable and searchable but not listable: a link in it could not be seen.
|
||||
std::fs::set_permissions(&write, std::fs::Permissions::from_mode(0o300)).unwrap();
|
||||
// A user who can list it anyway (root) sees no difference to test.
|
||||
if std::fs::read_dir(&write).is_err() {
|
||||
assert!(c.check_command("renderQueue.add", &add).is_err());
|
||||
}
|
||||
std::fs::set_permissions(&write, std::fs::Permissions::from_mode(0o700)).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok());
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn numbered_render_folders_with_links_out_of_the_write_roots_are_refused() {
|
||||
let base = std::env::temp_dir().join(format!("ec-confine-seq-dir-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
let (write, outside) = (base.join("out"), base.join("elsewhere"));
|
||||
for d in [&write, &outside] {
|
||||
std::fs::create_dir_all(d).unwrap();
|
||||
}
|
||||
let c = Confinement::new(&[], std::slice::from_ref(&write)).unwrap();
|
||||
let template = write.join("shot_[###]/f_[#####].png").to_string_lossy().into_owned();
|
||||
let add = serde_json::json!({"output": template});
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok(), "no numbered folder exists yet");
|
||||
// A numbered folder inside the roots holding a link out is refused.
|
||||
std::fs::create_dir(write.join("shot_001")).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok());
|
||||
std::os::unix::fs::symlink(outside.join("victim"), write.join("shot_001/f_00001.png")).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_err());
|
||||
std::fs::remove_file(write.join("shot_001/f_00001.png")).unwrap();
|
||||
// A numbered folder that is itself a link out is refused.
|
||||
std::os::unix::fs::symlink(&outside, write.join("shot_002")).unwrap();
|
||||
assert!(c.check_command("render.preRender", &add).is_err());
|
||||
// Unrelated names are left alone.
|
||||
std::fs::remove_file(write.join("shot_002")).unwrap();
|
||||
std::os::unix::fs::symlink(&outside, write.join("shot_notes")).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok());
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,5 @@ workspace = true
|
||||
[dependencies]
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = { workspace = true }
|
||||
filmcraft-av1 = { workspace = true }
|
||||
@@ -57,8 +57,8 @@ fn check(name: &str, cfg: EncoderConfig, pics: &[Pic]) -> Vec<u32> {
|
||||
recons.push(enc.last_reconstruction().expect("reconstruction"));
|
||||
levels.push(enc.last_loop_filter_level().expect("level"));
|
||||
}
|
||||
let dir = std::env::temp_dir().join(format!("effectcraft-av1enc-{}-{name}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).expect("temp dir");
|
||||
let guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = guard.path().to_path_buf();
|
||||
let input = dir.join("in.ivf");
|
||||
let output = dir.join("out.yuv");
|
||||
std::fs::write(&input, ivf(w, h, &packets)).expect("write ivf");
|
||||
|
||||
+50
-8
@@ -325,6 +325,9 @@ pub struct Clut {
|
||||
pub data: Vec<f32>,
|
||||
}
|
||||
|
||||
/// Most interpolating inputs [`Clut::eval`] combines (ICC allows 15).
|
||||
pub const MAX_CLUT_INPUTS: usize = 16;
|
||||
|
||||
impl Clut {
|
||||
/// Multilinear interpolation at `x` (0..1 per input).
|
||||
pub fn eval(&self, x: &[f64]) -> Vec<f64> {
|
||||
@@ -334,26 +337,40 @@ impl Clut {
|
||||
let mut s = self.outputs;
|
||||
for i in (0..n).rev() {
|
||||
stride[i] = s;
|
||||
s *= self.grid[i].max(1);
|
||||
// Saturating: a table with enough axes to overflow cannot hold its data anyway, and a
|
||||
// saturated offset reads nothing (zero) instead of panicking.
|
||||
s = s.saturating_mul(self.grid[i].max(1));
|
||||
}
|
||||
let mut frac = vec![0.0; n];
|
||||
let mut step = vec![0usize; n];
|
||||
for i in 0..n {
|
||||
let g = self.grid[i].max(1);
|
||||
let f = x.get(i).copied().unwrap_or(0.0).clamp(0.0, 1.0) * (g - 1) as f64;
|
||||
let k = (f.floor() as usize).min(g.saturating_sub(2));
|
||||
let mut k = (f.floor() as usize).min(g.saturating_sub(2));
|
||||
frac[i] = if g > 1 { f - k as f64 } else { 0.0 };
|
||||
step[i] = if g > 1 { stride[i] } else { 0 };
|
||||
base += k * stride[i];
|
||||
// The last grid point is a corner of its own, not an interpolation toward the next.
|
||||
if frac[i] >= 1.0 {
|
||||
k += 1;
|
||||
frac[i] = 0.0;
|
||||
}
|
||||
base = base.saturating_add(k.saturating_mul(stride[i]));
|
||||
}
|
||||
let mut out = vec![0.0; self.outputs];
|
||||
for corner in 0..(1usize << n) {
|
||||
// Only the axes that interpolate have two corners: a singleton axis, or one sampled on a
|
||||
// grid point, contributes its lower corner alone. ICC tables have at most 15 inputs; a
|
||||
// table built with more interpolating axes than that evaluates to zero.
|
||||
let active: Vec<usize> = (0..n).filter(|&i| step[i] != 0 && frac[i] != 0.0).collect();
|
||||
if active.len() > MAX_CLUT_INPUTS {
|
||||
return out;
|
||||
}
|
||||
for corner in 0..(1usize << active.len()) {
|
||||
let mut w = 1.0;
|
||||
let mut off = base;
|
||||
for i in 0..n {
|
||||
if corner >> i & 1 == 1 {
|
||||
for (bit, &i) in active.iter().enumerate() {
|
||||
if corner >> bit & 1 == 1 {
|
||||
w *= frac[i];
|
||||
off += step[i];
|
||||
off = off.saturating_add(step[i]);
|
||||
} else {
|
||||
w *= 1.0 - frac[i];
|
||||
}
|
||||
@@ -362,7 +379,7 @@ impl Clut {
|
||||
continue;
|
||||
}
|
||||
for (o, v) in out.iter_mut().enumerate() {
|
||||
*v += w * self.data.get(off + o).copied().unwrap_or(0.0) as f64;
|
||||
*v += w * self.data.get(off.saturating_add(o)).copied().unwrap_or(0.0) as f64;
|
||||
}
|
||||
}
|
||||
out
|
||||
@@ -1052,6 +1069,31 @@ mod tests {
|
||||
assert!(matches!(parse_profile(&write_matrix_profile("m", P709, D65, 2.2)).unwrap(), Profile::Matrix(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clut_corners_count_only_interpolating_axes() {
|
||||
// 64 singleton axes and one interpolating one: two corners, not 2^65.
|
||||
let mut grid = vec![1; 64];
|
||||
grid.push(2);
|
||||
let c = Clut { grid, outputs: 1, data: vec![0.0, 1.0] };
|
||||
let mut x = vec![0.5; 64];
|
||||
x.push(0.25);
|
||||
assert!((c.eval(&x)[0] - 0.25).abs() < 1e-9);
|
||||
// On grid points nothing interpolates.
|
||||
let c = Clut { grid: vec![2; 40], outputs: 1, data: vec![] };
|
||||
assert_eq!(c.eval(&[1.0; 40]), vec![0.0]);
|
||||
// More interpolating axes than ICC allows: zero, at once.
|
||||
assert_eq!(c.eval(&[0.5; 40]), vec![0.0]);
|
||||
// The last grid point of every axis is that table entry, however many axes there are.
|
||||
let mut data = vec![0.0; 1 << 17];
|
||||
data[(1 << 17) - 1] = 0.75;
|
||||
let c = Clut { grid: vec![2; 17], outputs: 1, data };
|
||||
assert!((c.eval(&[1.0; 17])[0] - 0.75).abs() < 1e-9);
|
||||
// Strides that overflow usize (64 two-point axes) read nothing instead of panicking.
|
||||
let c = Clut { grid: vec![2; 64], outputs: 1, data: vec![] };
|
||||
assert_eq!(c.eval(&[1.0; 64]), vec![0.0]);
|
||||
assert_eq!(c.eval(&[0.5; 64]), vec![0.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clut_and_lut3d_interpolate() {
|
||||
// A 2-point identity CLUT on 3 inputs is exact for every point.
|
||||
|
||||
@@ -228,8 +228,21 @@ fn compound_blur(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Camera Lens Blur
|
||||
|
||||
/// Half-open horizontal spans (dy, x_left, x_right) of a polygonal / round iris of radius `r`.
|
||||
/// Largest Camera Lens Blur radius in buffer pixels. Blur Radius goes to 500 in the UI; a project
|
||||
/// or expression value far beyond it would otherwise build an iris table of one row per pixel of
|
||||
/// radius (and sum that many rows per output pixel) before any image size check.
|
||||
pub const CAMERA_LENS_MAX_RADIUS: f64 = 2000.0;
|
||||
|
||||
/// Camera Lens Blur's iris radius in buffer pixels: Blur Radius at the buffer's `scale`, capped
|
||||
/// at [`CAMERA_LENS_MAX_RADIUS`]. Shared by the CPU and GPU paths.
|
||||
pub fn camera_lens_radius(ctx: &EffectCtx, scale: f64) -> f64 {
|
||||
(ctx.params.f("blurRadius").max(0.0) * scale).clamp(0.0, CAMERA_LENS_MAX_RADIUS)
|
||||
}
|
||||
|
||||
/// Half-open horizontal spans (dy, x_left, x_right) of a polygonal / round iris of radius `r`
|
||||
/// (capped at [`CAMERA_LENS_MAX_RADIUS`]).
|
||||
fn iris_spans(r: f64, sides: u32, rotation_deg: f64, roundness: f64) -> Vec<(i64, i64, i64)> {
|
||||
let r = if r.is_nan() { 0.0 } else { r.clamp(0.0, CAMERA_LENS_MAX_RADIUS) };
|
||||
let ri = r.ceil() as i64;
|
||||
let rot = rotation_deg.to_radians();
|
||||
let verts: Vec<(f64, f64)> = (0..sides)
|
||||
@@ -292,7 +305,8 @@ pub fn camera_lens_spans(ctx: &EffectCtx, r: f64) -> (Vec<(i64, i64, i64)>, f64)
|
||||
s.2 = (s.2 as f64 * aspect).round() as i64;
|
||||
}
|
||||
}
|
||||
(spans, r * aspect.max(1.0))
|
||||
// Same cap as iris_spans, so the reach matches the spans for unbounded radii.
|
||||
(spans, r.clamp(0.0, CAMERA_LENS_MAX_RADIUS) * aspect.max(1.0))
|
||||
}
|
||||
|
||||
/// Whether Camera Lens Blur needs more than plain iris spans (Diffraction Fringe or a Blur
|
||||
@@ -302,7 +316,7 @@ pub fn camera_lens_plain(ctx: &EffectCtx) -> bool {
|
||||
}
|
||||
|
||||
fn camera_lens_blur(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
let r = ctx.params.f("blurRadius").max(0.0) * b.scale;
|
||||
let r = camera_lens_radius(ctx, b.scale);
|
||||
if r < 0.5 {
|
||||
return b;
|
||||
}
|
||||
@@ -910,6 +924,19 @@ mod tests {
|
||||
assert!(out.img.get(15 + o, 10 + d)[0] < 0.9, "bright map blurs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iris_spans_cap_a_huge_radius() {
|
||||
// A project's Blur Radius far beyond the UI's 500 builds a bounded table.
|
||||
let max_rows = 2 * CAMERA_LENS_MAX_RADIUS as usize + 1;
|
||||
assert!(iris_spans(1e12, 6, 0.0, 0.0).len() <= max_rows);
|
||||
assert!(iris_spans(f64::INFINITY, 0, 0.0, 0.0).len() <= max_rows);
|
||||
assert_eq!(iris_spans(f64::NAN, 0, 0.0, 0.0), vec![(0, 0, 0)]);
|
||||
let params = Params { values: [("blurRadius".into(), num(1e15))].into_iter().collect() };
|
||||
let ctx = EffectCtx { params: ¶ms, time: 0.0, layer_size: [4.0, 4.0], seed: 1, adjustment: false, env: Default::default() };
|
||||
assert_eq!(camera_lens_radius(&ctx, 1.0), CAMERA_LENS_MAX_RADIUS);
|
||||
assert!(camera_lens_spans(&ctx, camera_lens_radius(&ctx, 1.0)).0.len() <= max_rows);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iris_spans_hexagon_is_symmetric() {
|
||||
let s = iris_spans(10.0, 6, 0.0, 0.0);
|
||||
|
||||
@@ -98,14 +98,14 @@ pub use sim::particle_state;
|
||||
pub use sim3::{PgBlit, PgPlan, playground_plan, playground_state};
|
||||
pub use stylize2::strobe_on;
|
||||
// CPU helpers the GPU kernels share (effectcraft-gpu).
|
||||
pub use blur2::{camera_lens_plain, camera_lens_spans, radial_fast_pad};
|
||||
pub use blur2::{CAMERA_LENS_MAX_RADIUS, camera_lens_plain, camera_lens_radius, camera_lens_spans, radial_fast_pad};
|
||||
pub use generate::{four_color_softening, gen_mode};
|
||||
pub use generate2::pattern_kind as cell_pattern_kind;
|
||||
pub use generate2::{BoltSeg, Contour, Ghost, RadioPlan, Wave, flare_ghosts, lightning_segments, radio_plan};
|
||||
pub use generate3::{
|
||||
Coverage, MarksPlan, PaintPlan, Seg, eyedropper_color, marks_plan, paint_bucket_region, paint_plan, plan_reads_pixels, poly_segs, scribble_style,
|
||||
};
|
||||
pub use noise::GrainLook;
|
||||
pub use noise::{GrainLook, MEDIAN_MAX_RADIUS};
|
||||
pub use textfx::{BoltPlan, TextLook, TextPass, bolt_plan, text_passes};
|
||||
pub use transition::{GridWipe, place_layer};
|
||||
// effectcraft-gpu fx_noise.
|
||||
|
||||
@@ -370,9 +370,16 @@ fn quant(v: f32) -> u16 {
|
||||
(v.clamp(0.0, 1.0) * (NB - 1) as f32).round() as u16
|
||||
}
|
||||
|
||||
/// Largest median window radius (buffer pixels) for Median, Median (Legacy), Dust & Scratches
|
||||
/// and the other median users. Radius goes to 255 in the UI; filling one (2r + 1)² window per
|
||||
/// row costs the square of the radius, so a project value far beyond it is held here (also
|
||||
/// keeping the window's pixel count well inside `u32` / `i32` on the GPU).
|
||||
pub const MEDIAN_MAX_RADIUS: usize = 512;
|
||||
|
||||
/// Per-channel median over a (2r+1)² window of the premultiplied channels (values quantised to
|
||||
/// 512 levels in 0..1, edges repeated).
|
||||
pub(crate) fn median_image(img: &Image, r: usize) -> Image {
|
||||
let r = r.min(MEDIAN_MAX_RADIUS);
|
||||
let (w, h) = (img.width as usize, img.height as usize);
|
||||
if r == 0 || w == 0 || h == 0 {
|
||||
return img.clone();
|
||||
@@ -871,6 +878,17 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn median_radius_is_capped() {
|
||||
// A project's Radius far beyond the UI's 255 takes the capped window, not a
|
||||
// (2r + 1)² fill per row (or an overflow) on a tiny image.
|
||||
let mut img = Image::filled(4, 3, [0.2, 0.3, 0.4, 1.0]);
|
||||
img.set(1, 1, [1.0, 1.0, 1.0, 1.0]);
|
||||
let capped = median_image(&img, MEDIAN_MAX_RADIUS);
|
||||
assert_eq!(median_image(&img, usize::MAX).data, capped.data);
|
||||
assert_eq!(median_image(&img, MEDIAN_MAX_RADIUS + 1000).data, capped.data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn median_matches_brute_force() {
|
||||
let (w, h) = (9u32, 7u32);
|
||||
|
||||
@@ -301,7 +301,8 @@ pub fn frozen_alpha(params: &Params, layer_size: [f64; 2], fps: f64, f: i64, w:
|
||||
let fr = fc.frames.get(&f)?;
|
||||
let [fw, fh] = fc.size;
|
||||
let expected = fw.checked_mul(fh)?;
|
||||
if fw == 0 || fh == 0 || expected > rle::MAX_PLANE_BYTES {
|
||||
// Only the layer's own full-resolution grid (see Roto Brush's frozen mattes).
|
||||
if fc.size != roto::frozen_grid(layer_size) || fw == 0 || fh == 0 || expected > rle::MAX_PLANE_BYTES {
|
||||
return None;
|
||||
}
|
||||
let a = rle::decode_bounded(&fr.a, expected)?;
|
||||
@@ -719,6 +720,12 @@ mod tests {
|
||||
p.values.insert("objectPoint".into(), Value::Vec2([40.0, 40.0]));
|
||||
p.values.insert(FROZEN.into(), Value::Str(fc2.to_json()));
|
||||
assert_eq!(frozen_alpha(&p, [100.0, 80.0], 25.0, 2, 100, 80, None), None);
|
||||
// A compact plane claiming a size other than the layer's grid is not decoded.
|
||||
let mut fc3 = fc.clone();
|
||||
fc3.size = [4000, 4000];
|
||||
fc3.frames.insert(2, roto::freeze_frame(&vec![1.0; 4000 * 4000], &[]));
|
||||
p.values.insert(FROZEN.into(), Value::Str(fc3.to_json()));
|
||||
assert_eq!(frozen_alpha(&p, [100.0, 80.0], 25.0, 2, 100, 80, None), None);
|
||||
}
|
||||
|
||||
/// Mattes frozen with a model stay usable in a session where that model is not loaded (a
|
||||
|
||||
@@ -394,7 +394,7 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
};
|
||||
let render = || popup(&["Full", "Outside", "Inside"]);
|
||||
let mut cyl = vec![
|
||||
p("radius", "Radius", num(100.0), slider(0.0, 400.0, 0.0, 200.0, 1)),
|
||||
p("radius", "Radius", num(100.0), slider(0.0, 30000.0, 0.0, 200.0, 1)),
|
||||
p("position", "Position", pt(0.5, 0.5), ParamUi::Point),
|
||||
p("rotation", "Rotation", num(0.0), ParamUi::Angle),
|
||||
p("rotationX", "Rotation X", num(0.0), ParamUi::Angle),
|
||||
@@ -565,6 +565,23 @@ mod tests {
|
||||
assert_eq!(out.get(50 + 18, 10)[3], 0.0);
|
||||
}
|
||||
|
||||
/// #3368: Radius takes the range After Effects' CC Cylinder has, up to 30000 %, and a cylinder
|
||||
/// that large still renders (nearly flat, fully opaque) instead of clamping to 400 %.
|
||||
#[test]
|
||||
fn cylinder_radius_goes_up_to_thirty_thousand_percent() {
|
||||
let spec = find("ec.perspective.cccylinder").unwrap();
|
||||
let radius = spec.params.iter().find(|p| p.id == "radius").unwrap();
|
||||
assert!(matches!(radius.ui, ParamUi::Slider { min, max, .. } if min == 0.0 && max == 30000.0), "{:?}", radius.ui);
|
||||
// The cylinder's axis on the left edge: at 400 % its radius is 400 / 100 * 100 / 2π ≈ 63.7 px,
|
||||
// so a pixel 70 px along is past it there and inside the 30000 % one.
|
||||
let img = Image::filled(100, 20, [0.5, 0.5, 0.5, 1.0]);
|
||||
let at_edge = |radius: f64| run("ec.perspective.cccylinder", &img, &[("radius", num(radius)), ("position", Value::Vec2([0.0, 10.0]))]).img;
|
||||
let clamped = at_edge(400.0).get(70, 10);
|
||||
assert_eq!(clamped[3], 0.0, "beyond a 400 % cylinder: {clamped:?}");
|
||||
let px = at_edge(30000.0).get(70, 10);
|
||||
assert!(px.iter().all(|v| v.is_finite()) && px[3] > 0.99, "{px:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cylinder_axis_rotations_affect_geometry_and_texture() {
|
||||
let img = Image::filled(100, 100, [0.5, 0.5, 0.5, 1.0]);
|
||||
|
||||
@@ -614,11 +614,72 @@ pub fn triangulate(outline: &[[f64; 2]], opts: &MeshOpts) -> Mesh {
|
||||
Mesh { verts, tris, outline: poly }
|
||||
}
|
||||
|
||||
/// The Expansion range the outline honours, in layer pixels (the property's slider range). A
|
||||
/// project file can store any number; outside this range it is clamped, so the padded grid
|
||||
/// below stays a few hundred pixels larger than the buffer.
|
||||
pub const EXPANSION_RANGE: (f64, f64) = (-100.0, 200.0);
|
||||
/// Most buffer pixels of padding the outline grid takes on each side (Expansion × buffer scale).
|
||||
const MAX_PAD: f64 = 4096.0;
|
||||
/// Most cells the padded outline grid may have (it is expanded with two `f64` distance buffers).
|
||||
const MAX_GRID_CELLS: usize = 1 << 25;
|
||||
/// Most the alpha is shrunk by, per axis, to make the padded grid fit; a layer needing more has no
|
||||
/// outline.
|
||||
const MAX_DOWNSAMPLE: usize = 8;
|
||||
|
||||
/// The outline of the alpha region under `seed` (buffer pixels → layer space via `scale` and
|
||||
/// `offset`), expanded by `expansion` layer pixels.
|
||||
/// `offset`), expanded by `expansion` layer pixels (clamped to [`EXPANSION_RANGE`]). When the
|
||||
/// padded grid would pass [`MAX_PAD`] or [`MAX_GRID_CELLS`] (a high-scale layer with a large
|
||||
/// Expansion), the alpha is traced at a coarser resolution instead of being skipped.
|
||||
pub fn outline(alpha: &[f32], w: usize, h: usize, scale: f64, offset: [f64; 2], seed: [f64; 2], expansion: f64) -> Vec<[f64; 2]> {
|
||||
// Pad so expansion can grow past the buffer edge.
|
||||
let pad = (expansion.max(0.0) * scale).ceil() as usize + 2;
|
||||
let expansion = if expansion.is_finite() { expansion.clamp(EXPANSION_RANGE.0, EXPANSION_RANGE.1) } else { 0.0 };
|
||||
if alpha.len() < w.saturating_mul(h) {
|
||||
return vec![];
|
||||
}
|
||||
let want = (expansion.max(0.0) * scale).ceil();
|
||||
if !want.is_finite() {
|
||||
return vec![];
|
||||
}
|
||||
for k in 1..=MAX_DOWNSAMPLE {
|
||||
let pad = (want / k as f64).ceil();
|
||||
let (kw, kh) = (w.div_ceil(k), h.div_ceil(k));
|
||||
let fits = pad <= MAX_PAD && {
|
||||
let pad = pad as usize + 2;
|
||||
kw.checked_add(2 * pad).zip(kh.checked_add(2 * pad)).and_then(|(pw, ph)| pw.checked_mul(ph)).is_some_and(|c| c <= MAX_GRID_CELLS)
|
||||
};
|
||||
if !fits {
|
||||
continue;
|
||||
}
|
||||
if k == 1 {
|
||||
return outline_padded(alpha, [w, h], scale, offset, seed, expansion, pad as usize + 2);
|
||||
}
|
||||
let small = shrink(alpha, w, h, k);
|
||||
let kf = k as f64;
|
||||
return outline_padded(&small, [kw, kh], scale / kf, [offset[0] / kf, offset[1] / kf], seed, expansion, pad as usize + 2);
|
||||
}
|
||||
vec![]
|
||||
}
|
||||
|
||||
/// `alpha` (`w × h`) with every `k × k` block averaged into one pixel.
|
||||
fn shrink(alpha: &[f32], w: usize, h: usize, k: usize) -> Vec<f32> {
|
||||
let (kw, kh) = (w.div_ceil(k), h.div_ceil(k));
|
||||
let mut out = vec![0.0f32; kw * kh];
|
||||
for y in 0..kh {
|
||||
for x in 0..kw {
|
||||
let (x1, y1) = (((x + 1) * k).min(w), ((y + 1) * k).min(h));
|
||||
let (mut sum, mut n) = (0.0f32, 0u32);
|
||||
for yy in y * k..y1 {
|
||||
for xx in x * k..x1 {
|
||||
sum += alpha[yy * w + xx];
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
out[y * kw + x] = sum / n.max(1) as f32;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn outline_padded(alpha: &[f32], [w, h]: [usize; 2], scale: f64, offset: [f64; 2], seed: [f64; 2], expansion: f64, pad: usize) -> Vec<[f64; 2]> {
|
||||
let (pw, ph) = (w + 2 * pad, h + 2 * pad);
|
||||
let mut on = vec![false; pw * ph];
|
||||
for y in 0..h {
|
||||
|
||||
@@ -106,6 +106,42 @@ fn expansion_grows_the_outline() {
|
||||
assert!((am - pi * 225.0).abs() < 60.0, "{am}");
|
||||
}
|
||||
|
||||
/// An imported Expansion far past the property's range is clamped instead of padding the grid
|
||||
/// by that many pixels (a 1e12 expansion would ask for terabytes).
|
||||
#[test]
|
||||
fn hostile_expansion_is_clamped() {
|
||||
let (w, h) = (40, 40);
|
||||
let alpha = disc_alpha(w, h, [20.0, 20.0], 10.0);
|
||||
let max = signed_area(&outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], EXPANSION_RANGE.1)).abs();
|
||||
// A non-finite Expansion is taken as zero (the unexpanded outline), not clamped to the maximum.
|
||||
let zero = signed_area(&outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], 0.0)).abs();
|
||||
for e in [1e12, f64::MAX, f64::INFINITY, f64::NAN] {
|
||||
let a = signed_area(&outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], e)).abs();
|
||||
let want = if e.is_finite() { max } else { zero };
|
||||
assert!((a - want).abs() < 1.0, "{e}: {a} vs {want}");
|
||||
}
|
||||
assert!(outline(&alpha, w, h, 1e9, [0.0; 2], [20.0, 20.0], 100.0).is_empty());
|
||||
let _ = outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], -1e12);
|
||||
}
|
||||
|
||||
/// A high-scale layer with a large Expansion is traced at a coarser resolution, not skipped, and a
|
||||
/// tiny buffer with a huge padded grid stays within the grid budget.
|
||||
#[test]
|
||||
fn high_scale_expansion_is_traced_coarser_instead_of_skipped() {
|
||||
let (w, h) = (40, 40);
|
||||
let alpha = disc_alpha(w, h, [20.0, 20.0], 10.0);
|
||||
// Scale 32, Expansion 130: 4160 px of padding, past MAX_PAD, so it is shrunk by 2.
|
||||
let scale = 32.0;
|
||||
let o = outline(&alpha, w, h, scale, [0.0; 2], [20.0, 20.0], 130.0);
|
||||
let area = signed_area(&o).abs();
|
||||
// The disc has radius 10 / 32 layer px; expanded by 130.
|
||||
let r = 10.0 / scale + 130.0;
|
||||
assert!((area - std::f64::consts::PI * r * r).abs() < 0.03 * std::f64::consts::PI * r * r, "{area}");
|
||||
// One pixel at scale 20.48 and Expansion 1e12 (clamped): its 8197 px square grid is over budget.
|
||||
let o = outline(&[1.0], 1, 1, 20.48, [0.0; 2], [0.5, 0.5], 1e12);
|
||||
assert!(o.len() >= 3);
|
||||
}
|
||||
|
||||
fn rect_mesh(w: f64, h: f64, tris: f64) -> Mesh {
|
||||
triangulate(&[[0.0, 0.0], [w, 0.0], [w, h], [0.0, h]], &MeshOpts { triangles: tris, density: 50.0, expansion: 0.0 })
|
||||
}
|
||||
|
||||
@@ -378,10 +378,22 @@ pub fn purge() {
|
||||
c.bytes = 0;
|
||||
}
|
||||
|
||||
/// The pixel size of the layer-space grid at `scale` (`round(size · scale)`, at least 1).
|
||||
fn grid_size(layer_size: [f64; 2], scale: f64) -> (u32, u32) {
|
||||
(((layer_size[0] * scale).round() as u32).max(1), ((layer_size[1] * scale).round() as u32).max(1))
|
||||
}
|
||||
|
||||
/// The full-resolution layer grid frozen mattes are stored at. A frozen cache claiming any
|
||||
/// other size did not come from freezing this layer and is not decoded.
|
||||
pub fn frozen_grid(layer_size: [f64; 2]) -> [usize; 2] {
|
||||
let (w, h) = grid_size(layer_size, 1.0);
|
||||
[w as usize, h as usize]
|
||||
}
|
||||
|
||||
/// The layer-space grid (`round(size · scale)` pixels) of a buffer.
|
||||
pub fn layer_grid(buf: &Buf, layer_size: [f64; 2]) -> Image {
|
||||
let s = if buf.scale > 0.0 { buf.scale } else { 1.0 };
|
||||
let (w, h) = (((layer_size[0] * s).round() as u32).max(1), ((layer_size[1] * s).round() as u32).max(1));
|
||||
let (w, h) = grid_size(layer_size, s);
|
||||
let (ox, oy) = (buf.offset[0].round() as i64, buf.offset[1].round() as i64);
|
||||
let mut img = Image::new(w, h);
|
||||
img.rows_mut().for_each(|(y, row)| {
|
||||
@@ -1212,8 +1224,11 @@ pub fn freeze_frame(alpha: &[f32], band: &[u8]) -> FrozenFrame {
|
||||
fn matte_for(ctx: &EffectCtx, f: i64, img: &Arc<Image>, scale: f64) -> Option<(Vec<f32>, Vec<u8>, f64)> {
|
||||
let (w, h) = (img.width as usize, img.height as usize);
|
||||
// Frozen mattes.
|
||||
// Only the layer's own full-resolution grid: a project cannot make a tiny layer decode (and
|
||||
// convert to floats) planes as large as the RLE allows. Any other size falls through to a computed matte.
|
||||
if let Some(fc) = frozen(ctx.params)
|
||||
&& frozen_applies(&fc, ctx.params, ctx.layer_size, ctx.fps())
|
||||
&& fc.size == frozen_grid(ctx.layer_size)
|
||||
&& let Some(fr) = fc.frames.get(&f)
|
||||
{
|
||||
let [fw, fh] = fc.size;
|
||||
@@ -1409,6 +1424,33 @@ mod tests {
|
||||
params
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mismatched_frozen_size_falls_back_to_a_computed_matte() {
|
||||
let mut d = RotoData::default();
|
||||
d.add(Stroke { kind: StrokeKind::Fg, frame: 0, radius: 3.0, points: vec![[35.0, 40.0], [45.0, 40.0]], object: 0 }, 20, [0, 24]);
|
||||
let mut params = params_with(&d);
|
||||
let fc =
|
||||
FrozenCache { key: frozen_key(¶ms, [100.0, 80.0], 25.0), size: [10, 8], frames: BTreeMap::from([(5, freeze_frame(&[1.0; 80], &[0; 80]))]) };
|
||||
let host = Seq;
|
||||
let render = |params: &Params| {
|
||||
let ctx = EffectCtx {
|
||||
params,
|
||||
time: 5.0 / 25.0 + 0.001,
|
||||
layer_size: [100.0, 80.0],
|
||||
seed: 1,
|
||||
adjustment: false,
|
||||
env: EffectEnv { host: Some(&host), frame_rate: 25.0, ..Default::default() },
|
||||
};
|
||||
crate::apply(&specs()[0], &ctx, Buf { img: frame(5.0 / 25.0 + 0.001), offset: [0.0; 2], scale: 1.0 }).img
|
||||
};
|
||||
let computed = render(¶ms);
|
||||
params.values.insert(FROZEN.into(), Value::Str(fc.to_json()));
|
||||
let out = render(¶ms);
|
||||
assert!(out.data[(40 * 100 + 50) as usize][3] > 0.5, "the disk is missing from the output");
|
||||
// The cache's all-opaque matte would leave the background opaque too; the fallback must match a render with no cache.
|
||||
assert!(out.data == computed.data, "the mismatched frozen cache was used");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renders_the_segmented_disk_on_demand() {
|
||||
let mut d = RotoData::default();
|
||||
@@ -1980,6 +2022,18 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Frozen mattes are only decoded at the size the freeze pass stores: the layer grid at
|
||||
/// full resolution.
|
||||
#[test]
|
||||
fn frozen_grid_is_the_full_resolution_layer_grid() {
|
||||
for size in [[100.0, 80.0], [99.6, 0.2], [1.0, 1.0]] {
|
||||
let buf = Buf { img: Image::new(3, 3), offset: [0.0; 2], scale: 1.0 };
|
||||
let g = layer_grid(&buf, size);
|
||||
assert_eq!(frozen_grid(size), [g.width as usize, g.height as usize]);
|
||||
}
|
||||
assert_eq!(frozen_grid([100.0, 80.0]), [100, 80]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saved_tracking_decoders_validate_dimensions_and_plane_lengths() {
|
||||
let valid = rle::encode(&[255, 0, 255, 0]);
|
||||
|
||||
@@ -8,9 +8,11 @@ use super::{Buf, EffectCtx, Image, IntoParallelRefMutIterator, ParallelIterator,
|
||||
/// Steps per second of the stepped particle simulations.
|
||||
pub(crate) const SPS: f64 = 60.0;
|
||||
|
||||
/// Number of whole steps reached at layer time `t`.
|
||||
/// Number of whole steps reached at layer time `t` (at most [`crate::util::MAX_SIM_STEPS`]: the
|
||||
/// per-step histories built from it, such as [`rate_history`], stay bounded however late a
|
||||
/// project places the layer's time).
|
||||
pub(crate) fn steps_at(t: f64) -> u64 {
|
||||
if t <= 0.0 { 0 } else { (t * SPS + 1e-6).floor() as u64 }
|
||||
if t <= 0.0 { 0 } else { ((t * SPS + 1e-6).floor() as u64).min(crate::util::MAX_SIM_STEPS) }
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
||||
@@ -565,7 +565,8 @@ impl PixelMotion {
|
||||
let px = |id: &str| (g(id, 0.0).max(0.0) * scale).round() as u32;
|
||||
PixelMotion {
|
||||
tile,
|
||||
window: ((g("tuning/blockSize", 8.0).max(4.0)) * scale).round().max(4.0) as u32,
|
||||
// Block Size is declared 4..64; a project can store any number.
|
||||
window: ((g("tuning/blockSize", 8.0).clamp(4.0, 64.0)) * scale).round().max(4.0) as u32,
|
||||
global_smoothness: g("tuning/smoothing/globalSmoothness", 0.5).clamp(0.0, 1.0) as f32,
|
||||
local_smoothness: g("tuning/smoothing/localSmoothness", 0.5).clamp(0.0, 1.0) as f32,
|
||||
iterations: g("tuning/smoothing/smoothingIterations", 20.0).clamp(0.0, 200.0) as u32,
|
||||
|
||||
+130
-21
@@ -115,18 +115,34 @@ fn box_row(src: &[f32], dst: &mut [f32], r: usize) {
|
||||
dst.copy_from_slice(src);
|
||||
return;
|
||||
}
|
||||
let get = |i: isize| src[i.clamp(0, n as isize - 1) as usize];
|
||||
let norm = 1.0 / (2 * r + 1) as f32;
|
||||
let mut acc = 0.0f64;
|
||||
for i in -(r as isize)..=(r as isize) {
|
||||
acc += get(i) as f64;
|
||||
// Past the row the window only adds more copies of the edge pixels; the radius is clamped
|
||||
// so the index arithmetic below cannot overflow (the means are unchanged in practice).
|
||||
let r = r.min(MAX_BOX_RADIUS);
|
||||
let (last, ri) = (n - 1, r as i64);
|
||||
let get = |i: i64| src[i.clamp(0, last as i64) as usize];
|
||||
let norm = 1.0 / (2.0 * r as f64 + 1.0);
|
||||
// The first window, -r..=r: the edge pixels it repeats are counted, not visited, so the
|
||||
// work depends on the row length and not on the radius.
|
||||
let inside = r.min(last);
|
||||
let mut acc = (r as f64 + 1.0) * src[0] as f64 + src[1..=inside].iter().map(|v| *v as f64).sum::<f64>();
|
||||
// Only when the window actually repeats the last pixel: 0 * inf would be NaN otherwise.
|
||||
if r > inside {
|
||||
acc += (r - inside) as f64 * src[last] as f64;
|
||||
}
|
||||
for x in 0..n {
|
||||
dst[x] = acc as f32 * norm;
|
||||
acc += get(x as isize + r as isize + 1) as f64 - get(x as isize - r as isize) as f64;
|
||||
dst[x] = (acc * norm) as f32;
|
||||
let xi = x as i64;
|
||||
acc += get(xi + ri + 1) as f64 - get(xi - ri) as f64;
|
||||
}
|
||||
}
|
||||
|
||||
/// Largest box radius [`box_row`] uses (larger radii come from absurd blur amounts in a
|
||||
/// project, such as a Mocha shape's feather).
|
||||
const MAX_BOX_RADIUS: usize = 1 << 30;
|
||||
|
||||
/// Largest sigma [`box_radii`] spreads into boxes (see [`MAX_BOX_RADIUS`]).
|
||||
const MAX_BOX_SIGMA: f64 = 1e8;
|
||||
|
||||
/// Separable box mean with radii `rx`, `ry` (edges repeated).
|
||||
pub fn box_plane(p: &Plane, rx: usize, ry: usize) -> Plane {
|
||||
let mut cur = if rx > 0 { p.map_rows(|s, d| box_row(s, d, rx)) } else { p.clone() };
|
||||
@@ -138,9 +154,10 @@ pub fn box_plane(p: &Plane, rx: usize, ry: usize) -> Plane {
|
||||
|
||||
/// Box radii for `n` passes approximating a Gaussian of `sigma`.
|
||||
pub fn box_radii(sigma: f64, n: usize) -> Vec<usize> {
|
||||
if sigma <= 0.0 {
|
||||
if sigma.is_nan() || sigma <= 0.0 || n == 0 {
|
||||
return vec![0; n];
|
||||
}
|
||||
let sigma = sigma.min(MAX_BOX_SIGMA);
|
||||
let w_ideal = (12.0 * sigma * sigma / n as f64 + 1.0).sqrt();
|
||||
let mut wl = w_ideal.floor() as i64;
|
||||
if wl % 2 == 0 {
|
||||
@@ -545,13 +562,22 @@ struct SimEntry<S> {
|
||||
/// Checkpoint every this many steps.
|
||||
pub const SIM_CHECKPOINT: u64 = 15;
|
||||
|
||||
/// Most steps a simulation runs: three hours of layer time at 60 steps per second. The layer
|
||||
/// time comes from the project (start time, stretch), so without a limit a tiny file could ask
|
||||
/// for years of steps; past this the state stays where it is.
|
||||
pub const MAX_SIM_STEPS: u64 = 60 * 60 * 60 * 3;
|
||||
|
||||
/// Most checkpoints kept per simulation (and made by one run, which spaces them further apart
|
||||
/// as it goes).
|
||||
const MAX_SIM_CHECKPOINTS: usize = 256;
|
||||
|
||||
impl<S: Clone> SimCache<S> {
|
||||
pub const fn new(cap: usize) -> SimCache<S> {
|
||||
SimCache { inner: std::sync::Mutex::new(Vec::new()), cap }
|
||||
}
|
||||
|
||||
/// State after `target` steps. `init` builds step 0; `step(state, i)` advances from step
|
||||
/// `i` to `i + 1`.
|
||||
/// State after `target` steps (at most [`MAX_SIM_STEPS`]). `init` builds step 0;
|
||||
/// `step(state, i)` advances from step `i` to `i + 1`.
|
||||
pub fn run(&self, key: u64, target: u64, init: impl FnOnce() -> S, step: impl FnMut(&mut S, u64)) -> std::sync::Arc<S> {
|
||||
self.run_where(key, target, init, step, |_, _| true)
|
||||
}
|
||||
@@ -569,6 +595,7 @@ impl<S: Clone> SimCache<S> {
|
||||
valid: impl Fn(u64, &S) -> bool,
|
||||
) -> std::sync::Arc<S> {
|
||||
use std::sync::Arc;
|
||||
let target = target.min(MAX_SIM_STEPS);
|
||||
let start = {
|
||||
let mut g = self.inner.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let tick = g.iter().map(|e| e.used).max().unwrap_or(0) + 1;
|
||||
@@ -587,11 +614,17 @@ impl<S: Clone> SimCache<S> {
|
||||
if !had_start {
|
||||
fresh.push((0, Arc::new(st.clone())));
|
||||
}
|
||||
let mut every = SIM_CHECKPOINT;
|
||||
while i < target {
|
||||
step(&mut st, i);
|
||||
i += 1;
|
||||
if i % SIM_CHECKPOINT == 0 && i != target {
|
||||
if i % every == 0 && i != target {
|
||||
fresh.push((i, Arc::new(st.clone())));
|
||||
// A long run keeps a bounded number of states: spread them twice as far apart.
|
||||
if fresh.len() > MAX_SIM_CHECKPOINTS {
|
||||
every = every.saturating_mul(2);
|
||||
fresh.retain(|(s, _)| s % every == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
let out = Arc::new(st);
|
||||
@@ -611,19 +644,23 @@ impl<S: Clone> SimCache<S> {
|
||||
};
|
||||
let e = &mut g[idx];
|
||||
e.used = tick;
|
||||
for (s, st) in fresh {
|
||||
e.checkpoints.retain(|(c, _)| *c != s);
|
||||
e.checkpoints.push((s, st));
|
||||
}
|
||||
// Keep memory bounded: drop unaligned (exact-target) checkpoints first, then thin.
|
||||
if e.checkpoints.len() > 256 {
|
||||
e.checkpoints.retain(|(s, _)| s % SIM_CHECKPOINT == 0);
|
||||
// Fresh checkpoints replace those at the same step (one pass, not one per checkpoint).
|
||||
let steps: std::collections::HashSet<u64> = fresh.iter().map(|(s, _)| *s).collect();
|
||||
e.checkpoints.retain(|(c, _)| !steps.contains(c));
|
||||
e.checkpoints.extend(fresh);
|
||||
// Keep memory bounded: drop unaligned (exact-target) checkpoints first, then thin. The
|
||||
// state just asked for keeps its slot, so asking for it again is answered from the cache.
|
||||
if e.checkpoints.len() > MAX_SIM_CHECKPOINTS {
|
||||
e.checkpoints.retain(|(s, _)| s % SIM_CHECKPOINT == 0 && *s != target);
|
||||
e.checkpoints.sort_by_key(|(s, _)| *s);
|
||||
let mut k = 0;
|
||||
// Drop only the excess, evenly spaced, so the rest keep their seek distance.
|
||||
let (len, excess) = (e.checkpoints.len(), (e.checkpoints.len() + 1).saturating_sub(MAX_SIM_CHECKPOINTS));
|
||||
let mut i = 0;
|
||||
e.checkpoints.retain(|_| {
|
||||
k += 1;
|
||||
k % 2 == 1
|
||||
i += 1;
|
||||
(i * excess) / len == ((i - 1) * excess) / len
|
||||
});
|
||||
e.checkpoints.push((target, out.clone()));
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -801,6 +838,45 @@ mod tests {
|
||||
assert_eq!(d, e);
|
||||
}
|
||||
|
||||
/// Layer time from a project cannot run a simulation without end, and a long run keeps a
|
||||
/// bounded number of checkpoints that still give the same answer when seeking.
|
||||
#[test]
|
||||
fn sim_cache_bounds_steps_and_checkpoints() {
|
||||
let c: SimCache<u64> = SimCache::new(2);
|
||||
let count = |s: &mut u64, _: u64| *s += 1;
|
||||
assert_eq!(*c.run(1, u64::MAX, || 0, count), MAX_SIM_STEPS);
|
||||
let n = c.inner.lock().map(|g| g.iter().map(|e| e.checkpoints.len()).max().unwrap_or(0)).unwrap_or(0);
|
||||
assert!(n <= MAX_SIM_CHECKPOINTS + 1, "{n} checkpoints");
|
||||
let fresh: SimCache<u64> = SimCache::new(2);
|
||||
let step = |s: &mut u64, i: u64| *s = s.wrapping_mul(6364136223846793005).wrapping_add(i);
|
||||
let far = *fresh.run(1, 100_000, || 7, step);
|
||||
let mid = *fresh.run(1, 54_321, || 7, step);
|
||||
let again: SimCache<u64> = SimCache::new(2);
|
||||
assert_eq!(*again.run(1, 54_321, || 7, step), mid);
|
||||
assert_eq!(*again.run(1, 100_000, || 7, step), far);
|
||||
}
|
||||
|
||||
/// A request that leaves the cache over its checkpoint cap still finds its own state there.
|
||||
#[test]
|
||||
fn sim_cache_keeps_the_requested_state_when_trimming() {
|
||||
let c: SimCache<u64> = SimCache::new(2);
|
||||
let steps = std::cell::Cell::new(0u64);
|
||||
let count = |s: &mut u64, _: u64| {
|
||||
steps.set(steps.get() + 1);
|
||||
*s += 1;
|
||||
};
|
||||
let target = SIM_CHECKPOINT * (MAX_SIM_CHECKPOINTS as u64 - 1) + SIM_CHECKPOINT - 1;
|
||||
assert_eq!(*c.run(1, target, || 0, count), target);
|
||||
let n = c.inner.lock().map(|g| g.iter().map(|e| e.checkpoints.len()).max().unwrap_or(0)).unwrap_or(0);
|
||||
assert!(n <= MAX_SIM_CHECKPOINTS, "{n} checkpoints");
|
||||
steps.set(0);
|
||||
assert_eq!(*c.run(1, target, || 0, count), target);
|
||||
assert_eq!(steps.get(), 0, "the second request is answered from the cache");
|
||||
// Only the excess is dropped: the other aligned checkpoints stay.
|
||||
let kept = c.inner.lock().map(|g| g[0].checkpoints.len()).unwrap_or(0);
|
||||
assert_eq!(kept, MAX_SIM_CHECKPOINTS, "trimmed to the cap, not halved");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn poly_helpers() {
|
||||
let sq = [[0.0, 0.0], [10.0, 0.0], [10.0, 10.0], [0.0, 10.0]];
|
||||
@@ -841,4 +917,37 @@ mod tests {
|
||||
let o = guided_filter(&g, &p, 2, 1e-3);
|
||||
assert!(o.data.iter().all(|v| (v - 0.7).abs() < 1e-3));
|
||||
}
|
||||
|
||||
/// The box mean counts repeated edge pixels instead of visiting them: the same means as a
|
||||
/// direct sum, and a huge radius (a Mocha feather of 1e300) costs a row's worth of work.
|
||||
#[test]
|
||||
fn box_row_matches_a_direct_sum_and_bounds_the_radius() {
|
||||
let src = [1.0f32, 2.0, 3.0, 5.0, 8.0];
|
||||
for r in [0usize, 1, 2, 4, 5, 9, 40] {
|
||||
let mut dst = [0.0f32; 5];
|
||||
box_row(&src, &mut dst, r);
|
||||
for (x, d) in dst.iter().enumerate() {
|
||||
let ri = r as i64;
|
||||
let sum: f64 = (x as i64 - ri..=x as i64 + ri).map(|i| src[i.clamp(0, 4) as usize] as f64).sum();
|
||||
let want = (sum / (2 * r + 1) as f64) as f32;
|
||||
assert!((d - want).abs() < 1e-5, "r={r} x={x}: {d} vs {want}");
|
||||
}
|
||||
}
|
||||
// An infinite last pixel inside the window still yields a finite first mean.
|
||||
let mut inf = [0.0f32; 3];
|
||||
box_row(&[1.0, 2.0, f32::INFINITY], &mut inf, 1);
|
||||
assert!((inf[0] - 4.0 / 3.0).abs() < 1e-6, "{inf:?}");
|
||||
let mut one = [0.0f32];
|
||||
box_row(&[0.25], &mut one, usize::MAX);
|
||||
assert!((one[0] - 0.25).abs() < 1e-6);
|
||||
let mut dst = [0.0f32; 5];
|
||||
box_row(&src, &mut dst, usize::MAX);
|
||||
assert!(dst.iter().all(|v| v.is_finite() && (1.0..=8.0).contains(v)), "{dst:?}");
|
||||
for sigma in [1e300, f64::MAX, f64::INFINITY, f64::NAN, -1.0] {
|
||||
assert_eq!(box_radii(sigma, 3).len(), 3);
|
||||
}
|
||||
let p = Plane { w: 4, h: 3, data: vec![0.5; 12] };
|
||||
let g = gauss_plane(&p, 1e300, 1e300);
|
||||
assert!(g.data.iter().all(|v| (v - 0.5).abs() < 1e-4), "{:?}", g.data);
|
||||
}
|
||||
}
|
||||
@@ -344,7 +344,9 @@ pub fn load_lut(src: &str) -> Option<Arc<Lut>> {
|
||||
{
|
||||
return v.clone();
|
||||
}
|
||||
let inline = src.contains('\n') || src.contains("LUT_");
|
||||
// `.cube` text always spans lines (a size header, then data rows); a path never does, even one
|
||||
// whose file name contains `LUT_` (#3454). Trimmed, as the path is, so a trailing newline is a path.
|
||||
let inline = src.trim().contains('\n');
|
||||
let parsed = if inline { parse_cube(src) } else { std::fs::read_to_string(src.trim()).ok().and_then(|t| parse_cube(&t)) }.map(Arc::new);
|
||||
if let Ok(mut m) = cache.lock() {
|
||||
m.insert(src.to_string(), parsed.clone());
|
||||
@@ -484,6 +486,20 @@ mod tests {
|
||||
assert_eq!(out.img, img);
|
||||
}
|
||||
|
||||
/// #3454: a path containing `LUT_` was taken for `.cube` text and the file never loaded.
|
||||
#[test]
|
||||
fn a_path_whose_name_contains_lut_loads_the_file() {
|
||||
let dir = std::env::temp_dir().join(format!("ec-fx-3454-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("LUT_x.cube");
|
||||
std::fs::write(&path, identity_cube(2)).unwrap();
|
||||
let path = path.to_string_lossy().into_owned();
|
||||
assert!(load_lut(&path).is_some(), "{path}");
|
||||
assert!(load_lut(&format!("{path}\r\n")).is_some(), "{path} with a trailing newline");
|
||||
assert!(load_lut(&identity_cube(2)).is_some(), "inline text");
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_d_lut_with_domain() {
|
||||
let lut = parse_cube("LUT_1D_SIZE 2\nDOMAIN_MIN 0 0 0\nDOMAIN_MAX 2 2 2\n1 1 1\n0 0 0\n").unwrap();
|
||||
|
||||
@@ -413,46 +413,46 @@ fn plan_effects_golden_hashes() {
|
||||
const GOLDEN: &[(&str, u64)] = &[
|
||||
("ec.obsolete.lightning/0/8bpc/t0.4", 0xd829bb3a79ec2d61),
|
||||
("ec.obsolete.lightning/0/8bpc/t1.3", 0xb2221364c0defe9b),
|
||||
("ec.obsolete.lightning/0/32bpc/t0.4", 0x828cc4e53d579af7),
|
||||
("ec.obsolete.lightning/0/32bpc/t1.3", 0x15770238cde9e766),
|
||||
("ec.obsolete.lightning/0/32bpc/half/t0.4", 0xf64f2b87fa9a09d3),
|
||||
("ec.obsolete.lightning/0/32bpc/half/t1.3", 0x625a00bd52fde5aa),
|
||||
("ec.obsolete.lightning/0/32bpc/t0.4", 0x38c8b3ca5d03eb81),
|
||||
("ec.obsolete.lightning/0/32bpc/t1.3", 0x552241c6035fa660),
|
||||
("ec.obsolete.lightning/0/32bpc/half/t0.4", 0x6158941311ce44d9),
|
||||
("ec.obsolete.lightning/0/32bpc/half/t1.3", 0xc7c2a804de6140a3),
|
||||
("ec.obsolete.lightning/1/8bpc/t0.4", 0xa7c8cb94beebca84),
|
||||
("ec.obsolete.lightning/1/8bpc/t1.3", 0x6e583b1d1f455099),
|
||||
("ec.obsolete.lightning/1/32bpc/t0.4", 0x2ffd973cc7de98f7),
|
||||
("ec.obsolete.lightning/1/32bpc/t1.3", 0x8f35e6018cac57d7),
|
||||
("ec.obsolete.lightning/1/32bpc/half/t0.4", 0x96c14e911783f8d6),
|
||||
("ec.obsolete.lightning/1/32bpc/half/t1.3", 0x620ee605f56c2068),
|
||||
("ec.obsolete.lightning/1/32bpc/t0.4", 0xe34f30bf669e94d5),
|
||||
("ec.obsolete.lightning/1/32bpc/t1.3", 0xb1ec8fad75bbbc32),
|
||||
("ec.obsolete.lightning/1/32bpc/half/t0.4", 0x9e2934559f25d0d9),
|
||||
("ec.obsolete.lightning/1/32bpc/half/t1.3", 0x8d2f6967b73bbf54),
|
||||
("ec.obsolete.lightning/2/8bpc/t0.4", 0x119e5cedac21cd9b),
|
||||
("ec.obsolete.lightning/2/8bpc/t1.3", 0x04b775fba837ddce),
|
||||
("ec.obsolete.lightning/2/32bpc/t0.4", 0xfee767091adac4ce),
|
||||
("ec.obsolete.lightning/2/32bpc/t1.3", 0x2ae26f9670c93751),
|
||||
("ec.obsolete.lightning/2/32bpc/half/t0.4", 0x99111f77bf26704f),
|
||||
("ec.obsolete.lightning/2/32bpc/half/t1.3", 0x8eb781efc14183c3),
|
||||
("ec.obsolete.lightning/2/32bpc/t0.4", 0x551415572abb270a),
|
||||
("ec.obsolete.lightning/2/32bpc/t1.3", 0x3e1b68b5b9cf2e8c),
|
||||
("ec.obsolete.lightning/2/32bpc/half/t0.4", 0x69b347a75e8ac280),
|
||||
("ec.obsolete.lightning/2/32bpc/half/t1.3", 0xe89cc144b611a083),
|
||||
("ec.generate.advancedlightning/0/8bpc/t0.4", 0x5a53bf1e0236225d),
|
||||
("ec.generate.advancedlightning/0/8bpc/t1.3", 0x5a53bf1e0236225d),
|
||||
("ec.generate.advancedlightning/0/32bpc/t0.4", 0x6817bf5b3583e0a0),
|
||||
("ec.generate.advancedlightning/0/32bpc/t1.3", 0x6817bf5b3583e0a0),
|
||||
("ec.generate.advancedlightning/0/32bpc/half/t0.4", 0x5dcc055f6cca9922),
|
||||
("ec.generate.advancedlightning/0/32bpc/half/t1.3", 0x5dcc055f6cca9922),
|
||||
("ec.generate.advancedlightning/0/32bpc/t0.4", 0x8ae323866971eaf4),
|
||||
("ec.generate.advancedlightning/0/32bpc/t1.3", 0x8ae323866971eaf4),
|
||||
("ec.generate.advancedlightning/0/32bpc/half/t0.4", 0x74b4ba806ab8e78b),
|
||||
("ec.generate.advancedlightning/0/32bpc/half/t1.3", 0x74b4ba806ab8e78b),
|
||||
("ec.generate.advancedlightning/1/8bpc/t0.4", 0xf07f11457fa683d0),
|
||||
("ec.generate.advancedlightning/1/8bpc/t1.3", 0xf07f11457fa683d0),
|
||||
("ec.generate.advancedlightning/1/32bpc/t0.4", 0x1b21531df9187431),
|
||||
("ec.generate.advancedlightning/1/32bpc/t1.3", 0x1b21531df9187431),
|
||||
("ec.generate.advancedlightning/1/32bpc/half/t0.4", 0xf1032a0a9345edf1),
|
||||
("ec.generate.advancedlightning/1/32bpc/half/t1.3", 0xf1032a0a9345edf1),
|
||||
("ec.generate.advancedlightning/1/32bpc/t0.4", 0x1474a59cbdbcb6aa),
|
||||
("ec.generate.advancedlightning/1/32bpc/t1.3", 0x1474a59cbdbcb6aa),
|
||||
("ec.generate.advancedlightning/1/32bpc/half/t0.4", 0x4808561ce9d8a4a6),
|
||||
("ec.generate.advancedlightning/1/32bpc/half/t1.3", 0x4808561ce9d8a4a6),
|
||||
("ec.generate.advancedlightning/2/8bpc/t0.4", 0x62b5d179f05c6a4c),
|
||||
("ec.generate.advancedlightning/2/8bpc/t1.3", 0x62b5d179f05c6a4c),
|
||||
("ec.generate.advancedlightning/2/32bpc/t0.4", 0x192256fe81f73975),
|
||||
("ec.generate.advancedlightning/2/32bpc/t1.3", 0x192256fe81f73975),
|
||||
("ec.generate.advancedlightning/2/32bpc/half/t0.4", 0x953dd51008c3ce31),
|
||||
("ec.generate.advancedlightning/2/32bpc/half/t1.3", 0x953dd51008c3ce31),
|
||||
("ec.generate.advancedlightning/2/32bpc/t0.4", 0x73c4b44db599a5c4),
|
||||
("ec.generate.advancedlightning/2/32bpc/t1.3", 0x73c4b44db599a5c4),
|
||||
("ec.generate.advancedlightning/2/32bpc/half/t0.4", 0x3085cf1b5f6e8311),
|
||||
("ec.generate.advancedlightning/2/32bpc/half/t1.3", 0x3085cf1b5f6e8311),
|
||||
("ec.generate.advancedlightning/3/8bpc/t0.4", 0x57b6ad61bd3d89e0),
|
||||
("ec.generate.advancedlightning/3/8bpc/t1.3", 0x57b6ad61bd3d89e0),
|
||||
("ec.generate.advancedlightning/3/32bpc/t0.4", 0x7e7c9d20e94c8b6c),
|
||||
("ec.generate.advancedlightning/3/32bpc/t1.3", 0x7e7c9d20e94c8b6c),
|
||||
("ec.generate.advancedlightning/3/32bpc/half/t0.4", 0xa37940f5a490a0fe),
|
||||
("ec.generate.advancedlightning/3/32bpc/half/t1.3", 0xa37940f5a490a0fe),
|
||||
("ec.generate.advancedlightning/3/32bpc/t0.4", 0x86eee3d36bfd41f2),
|
||||
("ec.generate.advancedlightning/3/32bpc/t1.3", 0x86eee3d36bfd41f2),
|
||||
("ec.generate.advancedlightning/3/32bpc/half/t0.4", 0xb24444985859d820),
|
||||
("ec.generate.advancedlightning/3/32bpc/half/t1.3", 0xb24444985859d820),
|
||||
("ec.generate.radiowaves/0/8bpc/t0.4", 0xbc3643b8af12c44b),
|
||||
("ec.generate.radiowaves/0/8bpc/t1.3", 0x703e948dc562e132),
|
||||
("ec.generate.radiowaves/0/32bpc/t0.4", 0x2b0db208ee7517c1),
|
||||
@@ -479,8 +479,8 @@ const GOLDEN: &[(&str, u64)] = &[
|
||||
("ec.generate.radiowaves/3/32bpc/half/t1.3", 0xcf62c191e2aa8cc4),
|
||||
("ec.generate.vegas/0/8bpc/t0.4", 0x5e58d44254e9101b),
|
||||
("ec.generate.vegas/0/8bpc/t1.3", 0x5e58d44254e9101b),
|
||||
("ec.generate.vegas/0/32bpc/t0.4", 0xe18712686e402759),
|
||||
("ec.generate.vegas/0/32bpc/t1.3", 0xe18712686e402759),
|
||||
("ec.generate.vegas/0/32bpc/t0.4", 0x2c6093dc655c6dbe),
|
||||
("ec.generate.vegas/0/32bpc/t1.3", 0x2c6093dc655c6dbe),
|
||||
("ec.generate.vegas/0/32bpc/half/t0.4", 0xb037c97234bbd936),
|
||||
("ec.generate.vegas/0/32bpc/half/t1.3", 0xb037c97234bbd936),
|
||||
("ec.generate.vegas/1/8bpc/t0.4", 0x68566eb5dc411cd3),
|
||||
@@ -491,8 +491,8 @@ const GOLDEN: &[(&str, u64)] = &[
|
||||
("ec.generate.vegas/1/32bpc/half/t1.3", 0xe2f8f5f4a12628d6),
|
||||
("ec.generate.vegas/2/8bpc/t0.4", 0x002c48a91aea2dcf),
|
||||
("ec.generate.vegas/2/8bpc/t1.3", 0x002c48a91aea2dcf),
|
||||
("ec.generate.vegas/2/32bpc/t0.4", 0x4f550bbbf8b8316b),
|
||||
("ec.generate.vegas/2/32bpc/t1.3", 0x4f550bbbf8b8316b),
|
||||
("ec.generate.vegas/2/32bpc/t0.4", 0xd703631844687600),
|
||||
("ec.generate.vegas/2/32bpc/t1.3", 0xd703631844687600),
|
||||
("ec.generate.vegas/2/32bpc/half/t0.4", 0x30036df3e56a01ed),
|
||||
("ec.generate.vegas/2/32bpc/half/t1.3", 0x30036df3e56a01ed),
|
||||
("ec.generate.stroke/0/8bpc/t0.4", 0xa580fbfb806d655c),
|
||||
@@ -557,10 +557,10 @@ const GOLDEN: &[(&str, u64)] = &[
|
||||
("ec.generate.paintbucket/1/32bpc/half/t1.3", 0xf5ea3b0a7ac224ed),
|
||||
("ec.generate.paintbucket/2/8bpc/t0.4", 0x530d0a248dc22c72),
|
||||
("ec.generate.paintbucket/2/8bpc/t1.3", 0x530d0a248dc22c72),
|
||||
("ec.generate.paintbucket/2/32bpc/t0.4", 0xe1b655f8815e1357),
|
||||
("ec.generate.paintbucket/2/32bpc/t1.3", 0xe1b655f8815e1357),
|
||||
("ec.generate.paintbucket/2/32bpc/half/t0.4", 0xb95b3b205255cd7e),
|
||||
("ec.generate.paintbucket/2/32bpc/half/t1.3", 0xb95b3b205255cd7e),
|
||||
("ec.generate.paintbucket/2/32bpc/t0.4", 0xf05e50b4261103a3),
|
||||
("ec.generate.paintbucket/2/32bpc/t1.3", 0xf05e50b4261103a3),
|
||||
("ec.generate.paintbucket/2/32bpc/half/t0.4", 0x7e74e09fcc83615c),
|
||||
("ec.generate.paintbucket/2/32bpc/half/t1.3", 0x7e74e09fcc83615c),
|
||||
("ec.generate.paintbucket/3/8bpc/t0.4", 0xa7485b7277e03e66),
|
||||
("ec.generate.paintbucket/3/8bpc/t1.3", 0xa7485b7277e03e66),
|
||||
("ec.generate.paintbucket/3/32bpc/t0.4", 0xc8e06c6eed30448b),
|
||||
@@ -666,16 +666,16 @@ const GOLDEN: &[(&str, u64)] = &[
|
||||
("ec.key.innerouter/0/32bpc/half/t1.3", 0x003941eb2cd18339),
|
||||
("ec.key.innerouter/1/8bpc/t0.4", 0xd7bfea3d9550db56),
|
||||
("ec.key.innerouter/1/8bpc/t1.3", 0xd7bfea3d9550db56),
|
||||
("ec.key.innerouter/1/32bpc/t0.4", 0x62f7705e43cc6a71),
|
||||
("ec.key.innerouter/1/32bpc/t1.3", 0x62f7705e43cc6a71),
|
||||
("ec.key.innerouter/1/32bpc/half/t0.4", 0x22f15f770f22f072),
|
||||
("ec.key.innerouter/1/32bpc/half/t1.3", 0x22f15f770f22f072),
|
||||
("ec.key.innerouter/1/32bpc/t0.4", 0x36661c78c393b52a),
|
||||
("ec.key.innerouter/1/32bpc/t1.3", 0x36661c78c393b52a),
|
||||
("ec.key.innerouter/1/32bpc/half/t0.4", 0x73f19ba1aea7e496),
|
||||
("ec.key.innerouter/1/32bpc/half/t1.3", 0x73f19ba1aea7e496),
|
||||
("ec.key.innerouter/2/8bpc/t0.4", 0x7bdece44f841bb11),
|
||||
("ec.key.innerouter/2/8bpc/t1.3", 0x7bdece44f841bb11),
|
||||
("ec.key.innerouter/2/32bpc/t0.4", 0x5130a1fc09c42120),
|
||||
("ec.key.innerouter/2/32bpc/t1.3", 0x5130a1fc09c42120),
|
||||
("ec.key.innerouter/2/32bpc/half/t0.4", 0x46c0880853618fc8),
|
||||
("ec.key.innerouter/2/32bpc/half/t1.3", 0x46c0880853618fc8),
|
||||
("ec.key.innerouter/2/32bpc/t0.4", 0x093157401c50396e),
|
||||
("ec.key.innerouter/2/32bpc/t1.3", 0x093157401c50396e),
|
||||
("ec.key.innerouter/2/32bpc/half/t0.4", 0xbde58c0668a48529),
|
||||
("ec.key.innerouter/2/32bpc/half/t1.3", 0xbde58c0668a48529),
|
||||
("ec.color.colorlink/0/8bpc/t0.4", 0x539d1da6b2ed744a),
|
||||
("ec.color.colorlink/0/8bpc/t1.3", 0x539d1da6b2ed744a),
|
||||
("ec.color.colorlink/0/32bpc/t0.4", 0x67ed14da67ce1a97),
|
||||
|
||||
@@ -537,18 +537,18 @@ const GOLDEN: &[(&str, u64)] = &[
|
||||
("ec.sim.caustics/1/8bpc/t1.5", 0x282968317c36ccdf),
|
||||
("ec.sim.caustics/1/8bpc/t0.8", 0x282968317c36ccdf),
|
||||
("ec.sim.caustics/1/8bpc/t2.2", 0x282968317c36ccdf),
|
||||
("ec.sim.caustics/1/32bpc/t0.5", 0xc12df8561f396d45),
|
||||
("ec.sim.caustics/1/32bpc/t1.5", 0xc12df8561f396d45),
|
||||
("ec.sim.caustics/1/32bpc/t0.8", 0xc12df8561f396d45),
|
||||
("ec.sim.caustics/1/32bpc/t2.2", 0xc12df8561f396d45),
|
||||
("ec.sim.caustics/1/32bpc/t0.5", 0xaa63d15038f75de2),
|
||||
("ec.sim.caustics/1/32bpc/t1.5", 0xaa63d15038f75de2),
|
||||
("ec.sim.caustics/1/32bpc/t0.8", 0xaa63d15038f75de2),
|
||||
("ec.sim.caustics/1/32bpc/t2.2", 0xaa63d15038f75de2),
|
||||
("ec.sim.caustics/2/8bpc/t0.5", 0x52ab258ae56e9b15),
|
||||
("ec.sim.caustics/2/8bpc/t1.5", 0x52ab258ae56e9b15),
|
||||
("ec.sim.caustics/2/8bpc/t0.8", 0x52ab258ae56e9b15),
|
||||
("ec.sim.caustics/2/8bpc/t2.2", 0x52ab258ae56e9b15),
|
||||
("ec.sim.caustics/2/32bpc/t0.5", 0x9a820d0e7c6d11ea),
|
||||
("ec.sim.caustics/2/32bpc/t1.5", 0x9a820d0e7c6d11ea),
|
||||
("ec.sim.caustics/2/32bpc/t0.8", 0x9a820d0e7c6d11ea),
|
||||
("ec.sim.caustics/2/32bpc/t2.2", 0x9a820d0e7c6d11ea),
|
||||
("ec.sim.caustics/2/32bpc/t0.5", 0x51d4304b505b5462),
|
||||
("ec.sim.caustics/2/32bpc/t1.5", 0x51d4304b505b5462),
|
||||
("ec.sim.caustics/2/32bpc/t0.8", 0x51d4304b505b5462),
|
||||
("ec.sim.caustics/2/32bpc/t2.2", 0x51d4304b505b5462),
|
||||
("ec.sim.waveworld/0/8bpc/t0.5", 0x425d5d639d2a6495),
|
||||
("ec.sim.waveworld/0/8bpc/t1.5", 0x233d7370e580804a),
|
||||
("ec.sim.waveworld/0/8bpc/t0.8", 0x866da86ce5e78eb2),
|
||||
|
||||
@@ -219,7 +219,16 @@ fn fnv(s: &str) -> u64 {
|
||||
/// What the effect's input frames are made of: the layer's source and timing, its masks and the
|
||||
/// effects above the Roto Brush.
|
||||
pub fn input_key(p: &Project, comp: ItemId, layer: &Layer, uid: Uid) -> String {
|
||||
let mut s = crate::warp::signature(p, comp, layer);
|
||||
input_key_with_signature(crate::warp::signature(p, comp, layer), layer, uid)
|
||||
}
|
||||
|
||||
/// Recognize the input key serialized before 0.23 without re-keying its frozen matte.
|
||||
fn input_key_before_placed_page(p: &Project, comp: ItemId, layer: &Layer, uid: Uid) -> Option<String> {
|
||||
let signature = crate::warp::signature_before_placed_page(p, comp, layer)?;
|
||||
Some(input_key_with_signature(signature, layer, uid))
|
||||
}
|
||||
|
||||
fn input_key_with_signature(mut s: String, layer: &Layer, uid: Uid) -> String {
|
||||
if let Some(m) = layer.masks() {
|
||||
s.push_str(&format!("{m:?}"));
|
||||
}
|
||||
@@ -247,7 +256,9 @@ pub(crate) fn sync(before: &Project, after: &mut Project) -> Vec<(ItemId, LayerI
|
||||
for g in fxg.groups().filter(|g| is_roto(g) || is_object_matte(g)) {
|
||||
let key = input_key(after, *cid, l, g.uid);
|
||||
let old = before.comp(*cid).and_then(|bc| bc.layer(l.id)).and_then(|ol| ol.props.find_group(g.uid));
|
||||
if str_of(g, INPUT_KEY) != key {
|
||||
let saved_key = str_of(g, INPUT_KEY);
|
||||
// Keep a matching old key: the frozen cache itself also hashes that key.
|
||||
if saved_key != key && input_key_before_placed_page(after, *cid, l, g.uid).as_deref() != Some(saved_key.as_str()) {
|
||||
set.push((*cid, l.id, g.uid, key));
|
||||
todo.push((*cid, l.id, g.uid));
|
||||
} else if old.is_none_or(|o| guard_of(o) != guard_of(g)) {
|
||||
|
||||
@@ -119,6 +119,20 @@ pub fn signature(p: &Project, comp: ItemId, layer: &Layer) -> String {
|
||||
},
|
||||
_ => String::new(),
|
||||
};
|
||||
signature_with_source(p, comp, layer, &src)
|
||||
}
|
||||
|
||||
/// Before `Footage::placed_page` was added in 0.23, the persisted signature hashed the
|
||||
/// same Debug representation without that last field. Only ordinary footage can match it.
|
||||
pub(crate) fn signature_before_placed_page(p: &Project, comp: ItemId, layer: &Layer) -> Option<String> {
|
||||
let LayerSource::Footage { item } = &layer.source else { return None };
|
||||
let ItemKind::Footage(footage) = &p.item(*item)?.kind else { return None };
|
||||
let src = format!("{footage:?}");
|
||||
let old = src.strip_suffix(", placed_page: false }")?;
|
||||
Some(signature_with_source(p, comp, layer, &format!("{old} }}")))
|
||||
}
|
||||
|
||||
fn signature_with_source(p: &Project, comp: ItemId, layer: &Layer, src: &str) -> String {
|
||||
let rate = p.comp(comp).map(|c| format!("{:?}", c.frame_rate)).unwrap_or_default();
|
||||
let s = format!(
|
||||
"{:?}|{src}|{:?}|{:?}|{:?}|{}|{:?}|{rate}",
|
||||
|
||||
@@ -395,6 +395,9 @@ fn collect_files(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// reads (other footage, a proxy, the project file itself), and then nothing is written.
|
||||
let protected = s.protected_files();
|
||||
let mut used_names = BTreeSet::new();
|
||||
// Where each file name's numbering resumes: a name repeated many times is numbered from its
|
||||
// own file name, not by prefixing the previous candidate again.
|
||||
let mut next_k: BTreeMap<String, usize> = BTreeMap::new();
|
||||
let mut plan: Vec<(ItemId, Vec<(String, String)>)> = vec![];
|
||||
for it in s.project.items.values() {
|
||||
let ItemKind::Footage(f) = &it.kind else { continue };
|
||||
@@ -404,11 +407,17 @@ fn collect_files(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let files: Vec<&String> = if f.sequence.is_empty() { vec![&f.path] } else { f.sequence.iter().collect() };
|
||||
let mut pairs = vec![];
|
||||
for src in files {
|
||||
let mut name = std::path::Path::new(src).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "footage".into());
|
||||
let mut k = 2;
|
||||
while !used_names.insert(name.clone()) {
|
||||
name = format!("{k}_{name}");
|
||||
k += 1;
|
||||
let file = std::path::Path::new(src).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "footage".into());
|
||||
let mut name = file.clone();
|
||||
if !used_names.insert(name.clone()) {
|
||||
let k = next_k.entry(file.clone()).or_insert(2);
|
||||
loop {
|
||||
name = format!("{k}_{file}");
|
||||
*k += 1;
|
||||
if used_names.insert(name.clone()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
let dst = format!("{folder}/(Footage)/{name}");
|
||||
// A file that is already where its copy goes stays (it is not copied onto itself).
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
//! premultiplied): the composite as `R`, `G`, `B`, `A` plus one `<layer name>.R/G/B/A` channel
|
||||
//! set per layer (the layer-prefixed channel convention multi-layer EXR readers use).
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use effectcraft_color::BlendMode;
|
||||
use effectcraft_project::{ItemId, LayerId, Project};
|
||||
use effectcraft_raster::Image;
|
||||
@@ -141,17 +143,22 @@ pub fn layered_psd(s: &Session, cid: ItemId, t: Tick, scale: f64) -> Result<(Vec
|
||||
|
||||
/// A unique EXR layer name (no dots: they separate the layer from the channel). Printable ASCII only:
|
||||
/// the `exr` crate panics on characters it cannot store in a channel name (#1702).
|
||||
fn exr_name(name: &str, used: &mut Vec<String>) -> String {
|
||||
/// `used` holds the names given so far, `next` the suffix each base's search resumes at: many
|
||||
/// layers of one name take a name each without retrying every suffix before theirs.
|
||||
fn exr_name(name: &str, used: &mut HashSet<String>, next: &mut HashMap<String, usize>) -> String {
|
||||
let base: String = name.chars().map(|c| if c == '.' || !(c == ' ' || c.is_ascii_graphic()) { '_' } else { c }).collect();
|
||||
let base = if base.trim().is_empty() { "Layer".to_string() } else { base };
|
||||
let mut n = base.clone();
|
||||
let mut k = 2;
|
||||
while used.contains(&n) {
|
||||
n = format!("{base} {k}");
|
||||
k += 1;
|
||||
if used.insert(base.clone()) {
|
||||
return base;
|
||||
}
|
||||
let k = next.entry(base.clone()).or_insert(2);
|
||||
loop {
|
||||
let n = format!("{base} {k}");
|
||||
*k += 1;
|
||||
if used.insert(n.clone()) {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
used.push(n.clone());
|
||||
n
|
||||
}
|
||||
|
||||
/// The multi-layer EXR bytes of a frame and the channel names.
|
||||
@@ -186,9 +193,9 @@ pub fn layered_exr(s: &Session, cid: ItemId, t: Tick, scale: f64) -> Result<(Vec
|
||||
}
|
||||
};
|
||||
push("", &full);
|
||||
let mut used = vec![];
|
||||
let (mut used, mut next) = (HashSet::new(), HashMap::new());
|
||||
for l in &layers {
|
||||
let n = exr_name(&l.name, &mut used);
|
||||
let n = exr_name(&l.name, &mut used, &mut next);
|
||||
push(&n, &l.image);
|
||||
}
|
||||
let layer = Layer::new((w, h), LayerAttributes::default(), Encoding::SMALL_LOSSLESS, AnyChannels::sort(channels.into()));
|
||||
@@ -248,3 +255,23 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use super::exr_name;
|
||||
|
||||
#[test]
|
||||
fn many_layers_of_one_name_take_a_name_each_in_order() {
|
||||
let (mut used, mut next) = (HashSet::new(), HashMap::new());
|
||||
// `A 3` is taken by a layer of its own before the repeats reach it.
|
||||
let names: Vec<String> = ["A", "A", "A 3", "A", "A.b"].iter().map(|n| exr_name(n, &mut used, &mut next)).collect();
|
||||
assert_eq!(names, ["A", "A 2", "A 3", "A 4", "A_b"]);
|
||||
// 20 000 layers of one name: each found without retrying the suffixes before it.
|
||||
for _ in 0..20_000 {
|
||||
exr_name("Same", &mut used, &mut next);
|
||||
}
|
||||
assert_eq!(next["Same"], 20_001);
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ mod transform;
|
||||
|
||||
use contents::{add_mask, add_shape_item, mask_props, set_text};
|
||||
use create::{add_item, new_adjustment, new_null, new_shape, new_solid, new_text};
|
||||
pub(crate) use create::{color_p, index_p, insert_layer, insert_layer_at, place, position_p};
|
||||
pub(crate) use create::{color_p, fit_span, index_p, insert_layer, insert_layer_at, new_layer_span, place, position_p, show_span};
|
||||
pub(crate) use parent::parent_fixes;
|
||||
use parent::set_parent;
|
||||
use replace::{has_replace_source, replace_source};
|
||||
|
||||
@@ -14,6 +14,39 @@ pub(crate) fn color_p(p: &Value, k: &str) -> Option<[f32; 3]> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Settings ▸ General ▸ Fit New Layers to Work Area: the `(start, end)` that new text, solid,
|
||||
/// shape, null and adjustment layers in `comp` take, or `None` for the whole composition.
|
||||
pub(crate) fn new_layer_span(s: &Session, comp: &effectcraft_project::Comp) -> Option<(Tick, Tick)> {
|
||||
let (start, end) = (comp.work_area.0, comp.work_area.1.min(comp.duration));
|
||||
(s.prefs.general.fit_new_layers_to_work_area && start >= Tick::ZERO && start < end).then_some((start, end))
|
||||
}
|
||||
|
||||
/// Start a new layer at `span`'s start and end it at `span`'s end, when there is a span.
|
||||
pub(crate) fn fit_span(l: &mut Layer, span: Option<(Tick, Tick)>) {
|
||||
if let Some((start, end)) = span {
|
||||
l.start_time = start;
|
||||
l.in_point = start;
|
||||
l.out_point = end;
|
||||
}
|
||||
}
|
||||
|
||||
/// Tools draw on the layer they create: when it was fitted to a span the playhead is outside,
|
||||
/// move `comp`'s playhead to the first frame in the span so the new layer shows. The layer's timing
|
||||
/// is kept. The work area need not sit on frames (a typed start, a frame-rate change), so the
|
||||
/// frame is rounded up from the start, not to the nearest; a span holding no frame moves nothing.
|
||||
pub(crate) fn show_span(s: &mut Session, comp: ItemId, span: Option<(Tick, Tick)>) {
|
||||
let Some(fr) = s.project.comp(comp).map(|c| c.frame_rate) else { return };
|
||||
if let Some((start, end)) = span
|
||||
&& !(start..end).contains(&s.time_of(comp))
|
||||
{
|
||||
let first = fr.snap(start);
|
||||
let first = if first < start { fr.tick_of(fr.frame_at(start).saturating_add(1)) } else { first };
|
||||
if first < end {
|
||||
s.set_time_of(comp, first);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Insert a new layer above the selection (or at the top) and select it.
|
||||
pub(crate) fn insert_layer(proj: &mut Project, st: &mut crate::EditorState, cid: ItemId, layer: Layer) -> Result<LayerId> {
|
||||
insert_layer_at(proj, st, cid, layer, None)
|
||||
@@ -89,10 +122,12 @@ fn new_solid_like(s: &mut Session, p: &Value, adjustment: bool) -> Result<Value>
|
||||
let pixel_aspect = pixel_aspect_p(p)?.unwrap_or(comp.pixel_aspect);
|
||||
let default_name = if adjustment { "Adjustment Layer 1".to_string() } else { "Solid 1".to_string() };
|
||||
let name = str_p(p, "name").map(str::to_string).unwrap_or(default_name);
|
||||
let span = new_layer_span(s, &comp);
|
||||
let id = s.edit(if adjustment { "New Adjustment Layer" } else { "New Solid" }, None, |proj, st| {
|
||||
let folder = solids_folder(proj);
|
||||
let sid = proj.add_item(&name, Label::Red, Some(folder), ItemKind::Solid(Solid { color, width: w, height: h, pixel_aspect }));
|
||||
let mut l = build::layer(proj, &comp, &name, LayerSource::Solid { item: sid }, (w, h), None);
|
||||
fit_span(&mut l, span);
|
||||
if adjustment {
|
||||
l.switches.adjustment = true;
|
||||
l.label = Label::Purple;
|
||||
@@ -186,9 +221,11 @@ pub(super) fn new_text(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
let edit = b_p(p, "edit").unwrap_or(false);
|
||||
let span = new_layer_span(s, &comp);
|
||||
let id = s.edit("New Text Layer", None, |proj, st| {
|
||||
let name: String = layer_name.clone().unwrap_or_else(|| effectcraft_project::text_layer_name(&doc.text));
|
||||
let mut l = build::layer(proj, &comp, if name.is_empty() { "Text" } else { &name }, LayerSource::Text, (comp.width, comp.height), None);
|
||||
fit_span(&mut l, span);
|
||||
// Named after its text, the name follows the text until renamed; a given name does not.
|
||||
l.name_follows_text = Some(layer_name.as_deref().is_none_or(str::is_empty));
|
||||
if let Some(pr) = l.props.prop_mut("text/sourceText") {
|
||||
@@ -213,6 +250,9 @@ pub(super) fn new_text(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
Ok(id)
|
||||
})?;
|
||||
if edit {
|
||||
show_span(s, cid, span);
|
||||
}
|
||||
Ok(json!({"layer": id.0}))
|
||||
}
|
||||
|
||||
@@ -232,8 +272,9 @@ pub(super) fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.and_then(|v| v.as_array())
|
||||
.map(|a| [a.first().and_then(Value::as_f64).unwrap_or(0.0), a.get(1).and_then(Value::as_f64).unwrap_or(0.0)]);
|
||||
let name = str_p(p, "name").unwrap_or("Shape Layer 1").to_string();
|
||||
let span = new_layer_span(s, &comp);
|
||||
let id = s.edit("New Shape Layer", None, |proj, st| {
|
||||
let lid = crate::cmd::shape_tool::new_shape_layer(proj, st, &comp, cid, Some(&name), pos)?;
|
||||
let lid = crate::cmd::shape_tool::new_shape_layer(proj, st, &comp, cid, Some(&name), pos, span)?;
|
||||
let mut next = proj.next_id;
|
||||
let g = crate::cmd::shape_tool::shape_group(&mut Ids(&mut next), &kind, size, &paint);
|
||||
proj.next_id = next;
|
||||
@@ -249,8 +290,10 @@ fn new_simple(s: &mut Session, p: &Value, src: LayerSource, name: &str, label: &
|
||||
let cid = comp_id(s, p)?;
|
||||
let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?.clone();
|
||||
let name = str_p(p, "name").unwrap_or(name).to_string();
|
||||
let span = new_layer_span(s, &comp);
|
||||
let id = s.edit(label, None, |proj, st| {
|
||||
let l = build::layer(proj, &comp, &name, src, (comp.width, comp.height), None);
|
||||
let mut l = build::layer(proj, &comp, &name, src, (comp.width, comp.height), None);
|
||||
fit_span(&mut l, span);
|
||||
insert_layer(proj, st, cid, l)
|
||||
})?;
|
||||
Ok(json!({"layer": id.0}))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Recover moved media without replacing interpretation, sequence ranges or layer timing.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::path::Path;
|
||||
|
||||
use effectcraft_project::{Footage, FootageKind, ItemId, ItemKind};
|
||||
@@ -11,6 +11,8 @@ use crate::{Result, Session, cmd};
|
||||
|
||||
const COMMAND: &str = "file.relinkFootage";
|
||||
const ENTRY_LIMIT: usize = 100_000;
|
||||
/// Candidates listed for one missing item (more than one already makes it ambiguous).
|
||||
const MAX_CANDIDATES: usize = 32;
|
||||
|
||||
fn enabled(_: &Session) -> std::result::Result<(), String> {
|
||||
if cfg!(target_arch = "wasm32") { Err("folder relinking requires the desktop filesystem".into()) } else { Ok(()) }
|
||||
@@ -95,6 +97,8 @@ fn run(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let wait = p.get("wait").and_then(Value::as_bool).unwrap_or(false);
|
||||
s.spawn_task("relink", "Relinking missing footage", wait, move |ctl| {
|
||||
let index = files(&folder, ctl)?;
|
||||
// Each candidate file is probed once, however many missing items share its name.
|
||||
let mut probes: HashMap<String, std::result::Result<Footage, String>> = HashMap::new();
|
||||
let mut rows = vec![];
|
||||
let mut updates = vec![];
|
||||
for (n, (id, old)) in missing.iter().enumerate() {
|
||||
@@ -104,19 +108,21 @@ fn run(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let supported = matches!(old.kind, FootageKind::Still | FootageKind::Sequence | FootageKind::Video | FootageKind::Audio)
|
||||
&& old.layer.is_none() && old.page == 0;
|
||||
let name = Path::new(&old.path).file_name().and_then(std::ffi::OsStr::to_str).unwrap_or("");
|
||||
let paths = index.get(name).cloned().unwrap_or_default();
|
||||
let paths = index.get(name).map_or(&[][..], Vec::as_slice);
|
||||
let mut candidates = vec![];
|
||||
let mut accepted = vec![];
|
||||
for path in paths {
|
||||
// Past a few candidates the item is ambiguous anyway: list no more of them.
|
||||
for path in paths.iter().take(MAX_CANDIDATES) {
|
||||
if ctl.cancelled() {
|
||||
return Err(crate::render_queue::CANCELLED.into());
|
||||
}
|
||||
let mut reason = "source type requires manual replacement".to_string();
|
||||
let mut confidence = "low";
|
||||
if supported {
|
||||
match importer.probe(&path) {
|
||||
Ok(f) if compatible(old, &f) => {
|
||||
if old.kind != FootageKind::Sequence || (!old.sequence.is_empty() && sequence_at(old, &path).is_some()) {
|
||||
let probed = probes.entry(path.clone()).or_insert_with(|| importer.probe(path));
|
||||
match probed {
|
||||
Ok(f) if compatible(old, f) => {
|
||||
if old.kind != FootageKind::Sequence || (!old.sequence.is_empty() && sequence_at(old, path).is_some()) {
|
||||
confidence = "high";
|
||||
reason = "exact filename and compatible source metadata".into();
|
||||
accepted.push(path.clone());
|
||||
@@ -284,6 +290,19 @@ mod tests {
|
||||
assert_eq!(get(&s, id), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many_same_named_candidates_list_at_most_the_cap() {
|
||||
let dir = Fixture::new();
|
||||
let (mut s, id, old) = setup(&dir, "plate.png");
|
||||
for i in 0..MAX_CANDIDATES + 8 {
|
||||
dir.file(&format!("new/{i}/plate.png"), b"valid");
|
||||
}
|
||||
let r = search(&mut s, &dir, false);
|
||||
assert_eq!(r["items"][0]["status"], "ambiguous");
|
||||
assert_eq!(r["items"][0]["candidates"].as_array().unwrap().len(), MAX_CANDIDATES);
|
||||
assert_eq!(get(&s, id), old);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicates_and_invalid_media_are_reported_without_edits() {
|
||||
let dir = Fixture::new();
|
||||
|
||||
@@ -8,6 +8,7 @@ use effectcraft_geom::vec2;
|
||||
use effectcraft_keyframe::{Gradient, ShapePath, Value as KV};
|
||||
use effectcraft_project::build::{self, Ids};
|
||||
use effectcraft_project::{Comp, ItemId, LayerId, LayerSource, Project, PropGroup};
|
||||
use effectcraft_time::Tick;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -263,7 +264,8 @@ pub(crate) fn draw_target(s: &Session, comp: &Comp, p: &Value, cmd: &str) -> Res
|
||||
}
|
||||
|
||||
/// A new empty shape layer ("Shape Layer n" unless `name`) with its Position at `position` (comp
|
||||
/// pixels; else the comp centre), above the selected layer and selected.
|
||||
/// pixels; else the comp centre), above the selected layer and selected. `span` is the
|
||||
/// [`new_layer_span`](super::layer::new_layer_span) the layer takes, if any.
|
||||
pub(crate) fn new_shape_layer(
|
||||
proj: &mut Project,
|
||||
st: &mut EditorState,
|
||||
@@ -271,11 +273,13 @@ pub(crate) fn new_shape_layer(
|
||||
cid: ItemId,
|
||||
name: Option<&str>,
|
||||
position: Option<[f64; 2]>,
|
||||
span: Option<(Tick, Tick)>,
|
||||
) -> Result<LayerId> {
|
||||
let count = comp.layers.iter().filter(|l| matches!(l.source, LayerSource::Shape)).count();
|
||||
let name = name.map(str::to_string).unwrap_or_else(|| format!("Shape Layer {}", count + 1));
|
||||
let mut l = build::layer(proj, comp, &name, LayerSource::Shape, (comp.width, comp.height), None);
|
||||
place(&mut l, position);
|
||||
super::layer::fit_span(&mut l, span);
|
||||
insert_layer(proj, st, cid, l)
|
||||
}
|
||||
|
||||
@@ -346,10 +350,11 @@ fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
_ => "Rectangle Tool",
|
||||
};
|
||||
let name = str_p(p, "name");
|
||||
let span = super::layer::new_layer_span(s, &comp);
|
||||
let (lid, group) = s.edit(label, None, |proj, st| {
|
||||
let lid = match target {
|
||||
Some(l) => l,
|
||||
None => new_shape_layer(proj, st, &comp, cid, name, Some(pos))?,
|
||||
None => new_shape_layer(proj, st, &comp, cid, name, Some(pos), span)?,
|
||||
};
|
||||
let mut next = proj.next_id;
|
||||
let g = shape_group(&mut Ids(&mut next), kind, size, &paint).ok_or_else(|| bad(c, "unknown kind"))?;
|
||||
@@ -358,6 +363,9 @@ fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
st.selected_layers = vec![lid];
|
||||
Ok((lid, group))
|
||||
})?;
|
||||
if target.is_none() {
|
||||
super::layer::show_span(s, cid, span);
|
||||
}
|
||||
Ok(json!({"layer": lid.0, "group": group}))
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +43,9 @@ pub fn display_duration(s: &Session, comp: &Comp, len: Tick) -> String {
|
||||
}
|
||||
|
||||
/// Parse a time typed in the project's Time Display Style (relative `+n`/`-n` from frame
|
||||
/// `current`) into a comp frame.
|
||||
/// `current`) into a comp frame. In Timecode style, in a comp whose Start Timecode is negative, a
|
||||
/// `-` before a timecode with separators (`-0:00:00:12`, `-1.00`) is the sign of a displayed time,
|
||||
/// not a step back; a bare `-n` still steps back.
|
||||
pub fn parse_display_time(s: &Session, comp: &Comp, text: &str, current: i64) -> std::result::Result<i64, String> {
|
||||
let fr = comp.frame_rate;
|
||||
let st = &s.project.settings;
|
||||
@@ -53,6 +55,20 @@ pub fn parse_display_time(s: &Session, comp: &Comp, text: &str, current: i64) ->
|
||||
effectcraft_time::parse_feet_frames(text, pf, current + start).map(|f| f - start).map_err(|e| e.0)
|
||||
}
|
||||
None => {
|
||||
// A comp whose Start Timecode is negative shows the times before 0 with a `-`
|
||||
// (`-0:00:00:12`), so there a `-` before a timecode with separators is the sign of a
|
||||
// displayed time and typing what the display shows lands on the frame it shows. A bare
|
||||
// `-n` still moves n frames back.
|
||||
if st.time_display == TimeDisplayStyle::Timecode
|
||||
&& comp.display_start < Tick::ZERO
|
||||
&& let Some(body) = text.trim().strip_prefix('-').map(str::trim_start)
|
||||
&& body.contains([':', ';', '.', ','])
|
||||
&& !body.starts_with(['+', '-'])
|
||||
{
|
||||
let drop_frame = fr.supports_drop_frame() && body.contains(';');
|
||||
let shown = effectcraft_time::parse_timecode(body, fr, drop_frame, current).map_err(|e| e.0)?;
|
||||
return Ok(shown.saturating_neg().saturating_sub(fr.frame_at(comp.display_start)));
|
||||
}
|
||||
// The Timeline shows non-drop timecode (`:`), so typed `:`/`.` text is read as non-drop
|
||||
// too; only explicit `;` fields mean drop-frame (#890).
|
||||
let drop_frame = fr.supports_drop_frame() && text.contains(';');
|
||||
|
||||
@@ -282,10 +282,11 @@ fn new_path(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
path.vertices.iter().fold(([f64::MAX; 2], [f64::MIN; 2]), |(lo, hi), v| ([lo[0].min(v[0]), lo[1].min(v[1])], [hi[0].max(v[0]), hi[1].max(v[1])]));
|
||||
let bounds = [lo[0], lo[1], hi[0] - lo[0], hi[1] - lo[1]];
|
||||
let name = str_p(p, "name").map(str::to_string);
|
||||
let span = super::layer::new_layer_span(s, &comp);
|
||||
let (lid, group, path_uid) = s.edit("Pen Tool", None, |proj, st| {
|
||||
let lid = match target {
|
||||
Some(l) => l,
|
||||
None => super::shape_tool::new_shape_layer(proj, st, &comp, cid, name.as_deref(), None)?,
|
||||
None => super::shape_tool::new_shape_layer(proj, st, &comp, cid, name.as_deref(), None, span)?,
|
||||
};
|
||||
let mut next = proj.next_id;
|
||||
let mut ids = Ids(&mut next);
|
||||
@@ -300,6 +301,9 @@ fn new_path(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
st.selected_vertices = vec![VertexRef { layer: lid, mask: path_uid, index: n - 1 }];
|
||||
Ok((lid, group, path_uid))
|
||||
})?;
|
||||
if target.is_none() {
|
||||
super::layer::show_span(s, cid, span);
|
||||
}
|
||||
Ok(json!({"layer": lid.0, "group": group, "path": path_uid}))
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
//! The browser has no folders: its file table lists the files picked or dropped so far, so
|
||||
//! picking every frame of a sequence imports it there too.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use effectcraft_project::sequence::{frame_number, missing_frame_ranges, sequence_name, split_frame_number};
|
||||
use effectcraft_project::{Footage, FootageKind};
|
||||
use effectcraft_time::FrameRate;
|
||||
@@ -126,10 +128,22 @@ fn sort_run(files: &mut Vec<String>, alphabetical: bool) {
|
||||
files.dedup();
|
||||
}
|
||||
|
||||
/// The files of the run `path` belongs to, from its folder.
|
||||
fn run_files(services: &dyn Services, path: &str, alphabetical: bool) -> Vec<String> {
|
||||
type RunKey = (String, String, String);
|
||||
|
||||
/// The files of the run `path` belongs to, from its folder. `folders` keeps each folder's files
|
||||
/// by run, listed once for all the files picked from it.
|
||||
fn run_files(services: &dyn Services, path: &str, alphabetical: bool, folders: &mut HashMap<String, HashMap<RunKey, Vec<String>>>) -> Vec<String> {
|
||||
let Some(key) = run_key(path, alphabetical) else { return vec![] };
|
||||
let mut files: Vec<String> = services.list_dir(&key.0).into_iter().filter(|p| run_key(p, alphabetical).as_ref() == Some(&key)).collect();
|
||||
let runs = folders.entry(key.0.clone()).or_insert_with(|| {
|
||||
let mut runs: HashMap<RunKey, Vec<String>> = HashMap::new();
|
||||
for p in services.list_dir(&key.0) {
|
||||
if let Some(k) = run_key(&p, alphabetical) {
|
||||
runs.entry(k).or_default().push(p);
|
||||
}
|
||||
}
|
||||
runs
|
||||
});
|
||||
let mut files = runs.get(&key).cloned().unwrap_or_default();
|
||||
sort_run(&mut files, alphabetical);
|
||||
files
|
||||
}
|
||||
@@ -138,27 +152,41 @@ fn run_files(services: &dyn Services, path: &str, alphabetical: bool) -> Vec<Str
|
||||
/// and (with the Sequence option, or [`SequenceOptions::folders`] for a folder's files) numbered
|
||||
/// stills of one run one [`Source::Sequence`].
|
||||
pub fn group(services: &dyn Services, paths: &[String], opts: SequenceOptions) -> Vec<Source> {
|
||||
// Each folder's runs, listed once for everything that needs them (a picked folder's listing
|
||||
// is kept for the run lookup of its lone files).
|
||||
let mut folders: HashMap<String, HashMap<RunKey, Vec<String>>> = HashMap::new();
|
||||
// Each file, and whether it may join a sequence.
|
||||
let mut files: Vec<(String, bool)> = vec![];
|
||||
for p in paths {
|
||||
if services.is_dir(p) {
|
||||
let mut inside: Vec<String> =
|
||||
services.list_dir(p).into_iter().filter(|f| matches!(crate::media_browser::kind_of(f), Some(k) if k != "project")).collect();
|
||||
let listing = services.list_dir(p);
|
||||
let mut runs: HashMap<RunKey, Vec<String>> = HashMap::new();
|
||||
for f in &listing {
|
||||
if let Some(k) = run_key(f, opts.alphabetical) {
|
||||
runs.entry(k).or_default().push(f.clone());
|
||||
}
|
||||
}
|
||||
if let Some(dir) = runs.keys().next().map(|k| k.0.clone()) {
|
||||
folders.entry(dir).or_insert(runs);
|
||||
}
|
||||
let mut inside: Vec<String> = listing.into_iter().filter(|f| matches!(crate::media_browser::kind_of(f), Some(k) if k != "project")).collect();
|
||||
inside.sort_by_key(|f| file_name(f).to_lowercase());
|
||||
files.extend(inside.into_iter().map(|f| (f, opts.folders)));
|
||||
} else {
|
||||
files.push((p.clone(), opts.sequence));
|
||||
}
|
||||
}
|
||||
// Runs in the order their first file was picked.
|
||||
let mut runs: Vec<((String, String, String), Vec<String>)> = vec![];
|
||||
// Runs in the order their first file was picked (found by key, not by a scan of the runs).
|
||||
let mut runs: Vec<(RunKey, Vec<String>)> = vec![];
|
||||
let mut run_of: HashMap<RunKey, usize> = HashMap::new();
|
||||
let mut out: Vec<Option<Source>> = vec![];
|
||||
let mut slot: Vec<usize> = vec![];
|
||||
for (f, joins) in files {
|
||||
match run_key(&f, opts.alphabetical).filter(|_| joins) {
|
||||
Some(key) => match runs.iter_mut().find(|(k, _)| *k == key) {
|
||||
Some((_, run)) => run.push(f),
|
||||
Some(key) => match run_of.get(&key) {
|
||||
Some(&i) => runs[i].1.push(f),
|
||||
None => {
|
||||
run_of.insert(key.clone(), runs.len());
|
||||
runs.push((key, vec![f]));
|
||||
slot.push(out.len());
|
||||
out.push(None);
|
||||
@@ -173,7 +201,7 @@ pub fn group(services: &dyn Services, paths: &[String], opts: SequenceOptions) -
|
||||
if let [one] = picked.as_slice()
|
||||
&& opts.sequence
|
||||
{
|
||||
let all = run_files(services, one, opts.alphabetical);
|
||||
let all = run_files(services, one, opts.alphabetical, &mut folders);
|
||||
if all.len() >= 2 {
|
||||
picked = all;
|
||||
}
|
||||
@@ -315,6 +343,59 @@ mod tests {
|
||||
assert_eq!(sequences(&t, &strs(&["/s/a_0001.png", "/s/c.png"]), false).len(), 1);
|
||||
}
|
||||
|
||||
/// Thousands of files picked one by one from one folder: the folder is listed once, not
|
||||
/// once per file, and each finds its run by key.
|
||||
#[test]
|
||||
fn picked_files_of_one_folder_list_it_once() {
|
||||
struct Counting(Table, std::sync::atomic::AtomicUsize);
|
||||
impl Services for Counting {
|
||||
fn read_file(&self, path: &str) -> std::io::Result<Vec<u8>> {
|
||||
self.0.read_file(path)
|
||||
}
|
||||
fn write_file(&self, p: &str, b: &[u8]) -> std::io::Result<()> {
|
||||
self.0.write_file(p, b)
|
||||
}
|
||||
fn list_dir(&self, dir: &str) -> Vec<String> {
|
||||
self.1.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
self.0.list_dir(dir)
|
||||
}
|
||||
fn is_dir(&self, path: &str) -> bool {
|
||||
self.0.is_dir(path)
|
||||
}
|
||||
}
|
||||
let names: Vec<&'static str> = (0..2000).map(|i| &*Box::leak(format!("/s/x{i}_1.png").into_boxed_str())).collect();
|
||||
let t = Counting(Table(names.clone()), Default::default());
|
||||
let picked: Vec<String> = names.iter().map(|s| s.to_string()).collect();
|
||||
let g = group(&t, &picked, SequenceOptions::default());
|
||||
assert_eq!(g.len(), 2000);
|
||||
assert_eq!(t.1.load(std::sync::atomic::Ordering::Relaxed), 1);
|
||||
}
|
||||
|
||||
/// A picked folder's listing serves the run lookup of its lone files: one listing in all.
|
||||
#[test]
|
||||
fn a_picked_folder_is_listed_once() {
|
||||
struct Counting(Table, std::sync::atomic::AtomicUsize);
|
||||
impl Services for Counting {
|
||||
fn read_file(&self, path: &str) -> std::io::Result<Vec<u8>> {
|
||||
self.0.read_file(path)
|
||||
}
|
||||
fn write_file(&self, p: &str, b: &[u8]) -> std::io::Result<()> {
|
||||
self.0.write_file(p, b)
|
||||
}
|
||||
fn list_dir(&self, dir: &str) -> Vec<String> {
|
||||
self.1.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
self.0.list_dir(dir)
|
||||
}
|
||||
fn is_dir(&self, path: &str) -> bool {
|
||||
self.0.is_dir(path)
|
||||
}
|
||||
}
|
||||
let t = Counting(Table(vec!["/s/a_0001.png", "/s/b_0001.png", "/s/b_0002.png"]), Default::default());
|
||||
let g = group(&t, &strs(&["/s"]), SequenceOptions { sequence: true, folders: true, alphabetical: false });
|
||||
assert_eq!(g.len(), 2, "{g:?}");
|
||||
assert_eq!(t.1.load(std::sync::atomic::Ordering::Relaxed), 1);
|
||||
}
|
||||
|
||||
/// #431: `file.import` brings picked or dropped numbered stills in as stills; only a folder or
|
||||
/// an explicit `sequence: true` makes a sequence, as After Effects' Sequence checkbox does.
|
||||
#[test]
|
||||
|
||||
@@ -751,6 +751,9 @@ fn footage_dir(s: &Session, name: &str, footage_dir: Option<&str>, project_path:
|
||||
fn extract_footage(s: &Session, proj: &mut Project, entries: &[(String, Vec<u8>)], dir: &str) -> Result<Vec<String>> {
|
||||
let mut written = std::collections::HashMap::new();
|
||||
let mut used = std::collections::HashSet::new();
|
||||
// Where each (item, file) pair's numbering resumes, so repeated names don't retry every
|
||||
// number before theirs.
|
||||
let mut next = std::collections::HashMap::new();
|
||||
for (k, v) in entries.iter().filter(|(k, _)| k.starts_with("media/")) {
|
||||
// `media/<item>/<file>` → one folder; a name another item took gets the item's id.
|
||||
let file = safe_file_name(&base_name(k));
|
||||
@@ -763,10 +766,10 @@ fn extract_footage(s: &Session, proj: &mut Project, entries: &[(String, Vec<u8>)
|
||||
// case-insensitive volume (Windows and macOS as they come), and Win32 drops a trailing
|
||||
// dot or space, so `a.png.` is `a.png` there. The name written stays as it is.
|
||||
let mut name = file.clone();
|
||||
let mut n = 0usize;
|
||||
let n = next.entry((item, same_file_key(&file))).or_insert(0usize);
|
||||
while !used.insert(same_file_key(&name)) {
|
||||
n += 1;
|
||||
name = if n == 1 { format!("{item}-{file}") } else { format!("{item}-{n}-{file}") };
|
||||
*n += 1;
|
||||
name = if *n == 1 { format!("{item}-{file}") } else { format!("{item}-{n}-{file}") };
|
||||
}
|
||||
let out = format!("{dir}/{name}");
|
||||
s.services.store_file(&out, v).map_err(|e| EngineError::Other(format!("cannot write {out}: {e}")))?;
|
||||
|
||||
@@ -477,35 +477,51 @@ pub fn split_at_images(doc: &Doc) -> Vec<Doc> {
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
/// Keep the shapes between images `seg − 1` and `seg`; `seen` counts images passed.
|
||||
fn prune(g: &effectcraft_svg::Group, seg: usize, seen: &mut usize) -> effectcraft_svg::Group {
|
||||
let mut out = g.clone();
|
||||
out.children = vec![];
|
||||
/// `g` without its children, holding `children` instead (nothing below `g` is copied).
|
||||
fn shell(g: &effectcraft_svg::Group, children: Vec<Node>) -> effectcraft_svg::Group {
|
||||
effectcraft_svg::Group {
|
||||
name: g.name.clone(),
|
||||
transform: g.transform,
|
||||
opacity: g.opacity,
|
||||
children,
|
||||
clip: g.clip.clone(),
|
||||
blend: g.blend,
|
||||
mask: g.mask.clone(),
|
||||
isolated: g.isolated,
|
||||
knockout: g.knockout,
|
||||
}
|
||||
}
|
||||
/// One traversal for every segment: the pieces of `g` as (segment, group), by ascending
|
||||
/// segment; `seen` counts images passed. Each shape is copied once, into its own segment.
|
||||
fn split(g: &effectcraft_svg::Group, seen: &mut usize) -> Vec<(usize, effectcraft_svg::Group)> {
|
||||
let mut by: Vec<(usize, Vec<Node>)> = vec![];
|
||||
let mut put = |seg: usize, node: Node| match by.last_mut() {
|
||||
Some(last) if last.0 == seg => last.1.push(node),
|
||||
_ => by.push((seg, vec![node])),
|
||||
};
|
||||
for c in &g.children {
|
||||
match c {
|
||||
Node::Image(_) => *seen += 1,
|
||||
Node::Shape(_) => {
|
||||
if *seen == seg {
|
||||
out.children.push(c.clone());
|
||||
}
|
||||
}
|
||||
Node::Shape(_) => put(*seen, c.clone()),
|
||||
Node::Group(s) => {
|
||||
let p = prune(s, seg, seen);
|
||||
if !p.children.is_empty() {
|
||||
out.children.push(Node::Group(p));
|
||||
for (seg, part) in split(s, seen) {
|
||||
put(seg, Node::Group(part));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
by.into_iter().map(|(seg, children)| (seg, shell(g, children))).collect()
|
||||
}
|
||||
let n = count(&doc.root);
|
||||
(0..=n)
|
||||
.map(|seg| {
|
||||
let mut d = doc.clone();
|
||||
d.root = prune(&doc.root, seg, &mut 0);
|
||||
d
|
||||
})
|
||||
let mut roots: Vec<Option<effectcraft_svg::Group>> = vec![None; n + 1];
|
||||
for (seg, part) in split(&doc.root, &mut 0) {
|
||||
if let Some(slot) = roots.get_mut(seg) {
|
||||
*slot = Some(part);
|
||||
}
|
||||
}
|
||||
roots
|
||||
.into_iter()
|
||||
.map(|root| Doc { width: doc.width, height: doc.height, root: root.unwrap_or_else(|| shell(&doc.root, vec![])), skipped: doc.skipped.clone() })
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -655,3 +671,73 @@ pub fn masks_layer(proj: &mut Project, comp: &Comp, name: &str, outlines: Vec<Sh
|
||||
}
|
||||
l
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use effectcraft_svg::{Geom, Group, Image, Shape};
|
||||
|
||||
fn shape(name: &str) -> Node {
|
||||
let geom = Geom::Rect { x: 0.0, y: 0.0, w: 1.0, h: 1.0, rx: 0.0, ry: 0.0 };
|
||||
Node::Shape(Shape {
|
||||
name: name.into(),
|
||||
geom,
|
||||
transform: Affine::IDENTITY,
|
||||
fill: None,
|
||||
fill_opacity: 1.0,
|
||||
fill_rule: effectcraft_svg::FillRule::NonZero,
|
||||
stroke: None,
|
||||
opacity: 1.0,
|
||||
})
|
||||
}
|
||||
fn image() -> Node {
|
||||
Node::Image(Image { name: "i".into(), transform: Affine::IDENTITY, width: 1, height: 1, rgba: std::sync::Arc::new(vec![0; 4]), opacity: 1.0 })
|
||||
}
|
||||
fn group(name: &str, children: Vec<Node>) -> Node {
|
||||
let mut g = Group::new(name);
|
||||
g.children = children;
|
||||
Node::Group(g)
|
||||
}
|
||||
fn names(g: &Group, out: &mut Vec<String>) {
|
||||
for c in &g.children {
|
||||
match c {
|
||||
Node::Shape(s) => out.push(s.name.clone()),
|
||||
Node::Group(s) => {
|
||||
out.push(format!("<{}", s.name));
|
||||
names(s, out);
|
||||
out.push(format!("{}>", s.name));
|
||||
}
|
||||
Node::Image(_) => out.push("IMG".into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Segment `k` keeps the shapes between image `k - 1` and `k`, with the groups that hold them.
|
||||
#[test]
|
||||
fn split_keeps_each_segments_shapes_and_their_groups() {
|
||||
let mut root = Group::new("root");
|
||||
root.children = vec![
|
||||
shape("a"),
|
||||
image(),
|
||||
group("g1", vec![shape("b"), image(), shape("c"), group("g2", vec![shape("d"), image(), shape("e")])]),
|
||||
image(),
|
||||
group("empty", vec![image()]),
|
||||
shape("f"),
|
||||
];
|
||||
let doc = Doc { width: 10.0, height: 10.0, root, skipped: vec![] };
|
||||
let segs = split_at_images(&doc);
|
||||
assert_eq!(segs.len(), 6);
|
||||
let got: Vec<Vec<String>> = segs
|
||||
.iter()
|
||||
.map(|d| {
|
||||
let mut v = vec![];
|
||||
names(&d.root, &mut v);
|
||||
v
|
||||
})
|
||||
.collect();
|
||||
let want: Vec<Vec<&str>> =
|
||||
vec![vec!["a"], vec!["<g1", "b", "g1>"], vec!["<g1", "c", "<g2", "d", "g2>", "g1>"], vec!["<g1", "<g2", "e", "g2>", "g1>"], vec![], vec!["f"]];
|
||||
assert_eq!(got, want);
|
||||
assert!(segs.iter().all(|d| d.root.name == "root" && d.width == 10.0));
|
||||
}
|
||||
}
|
||||
@@ -509,12 +509,27 @@ impl Session {
|
||||
/// is not for an export to replace. For callers that write the file themselves; call it
|
||||
/// before the work, not after it.
|
||||
pub fn refuse_export(&self, path: &str) -> crate::Result<()> {
|
||||
self.refuse_script_write(path)?;
|
||||
match self.blocking(path) {
|
||||
Some(source) => Err(crate::EngineError::Other(source.refusal(path, Use::Export))),
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
/// `Err` when a script issued the running command ([`Session::running_for_script`]) and
|
||||
/// Settings ▸ Scripting & Expressions ▸ Allow Scripts to Write Files is off. The scripting
|
||||
/// wrappers check this too, but a script can run any command directly (`app.run`, `__exec`),
|
||||
/// so the commands that write a path they are given check here, through
|
||||
/// [`Session::refuse_export`] and [`Session::write_output`].
|
||||
pub fn refuse_script_write(&self, path: &str) -> crate::Result<()> {
|
||||
if self.running_for_script() && !self.prefs.scripting.allow_scripts_write_files {
|
||||
return Err(crate::EngineError::Other(format!(
|
||||
"Unable to write {path}: scripts can't write files unless Settings ▸ Scripting & Expressions ▸ Allow Scripts to Write Files and Access Network is on"
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Write `bytes` to `path` through the host's services, unless the project reads the file
|
||||
/// there ([`Session::refuse_export`]). Every command that writes to a path it is given
|
||||
/// writes through this; Save and Save As (the project's own file) do not.
|
||||
@@ -525,6 +540,7 @@ impl Session {
|
||||
/// [`Session::write_output`] against files already found ([`Session::protected_files`]),
|
||||
/// for a command that writes many.
|
||||
pub(crate) fn write_output_with(&self, protected: &Protected, path: &str, bytes: &[u8]) -> crate::Result<()> {
|
||||
self.refuse_script_write(path)?;
|
||||
if let Some(source) = protected.find(Path::new(path)) {
|
||||
return Err(crate::EngineError::Other(source.refusal(path, Use::Export)));
|
||||
}
|
||||
|
||||
@@ -195,6 +195,15 @@ impl ImageRef {
|
||||
}
|
||||
}
|
||||
|
||||
/// Most edges a ScriptUI `fillPath` is filled with (a path past it fills its first edges).
|
||||
pub const MAX_FILL_EDGES: usize = 65_536;
|
||||
/// Most horizontal bands a ScriptUI fill is cut into (vertices and edge crossings); the topmost are filled.
|
||||
pub const MAX_FILL_BANDS: usize = 262_144;
|
||||
/// Most triangles a ScriptUI fill produces.
|
||||
pub const MAX_FILL_TRIANGLES: usize = 1 << 22;
|
||||
/// Most band-by-edge steps a fill takes while cutting bands.
|
||||
pub const MAX_FILL_WORK: usize = 1 << 31;
|
||||
|
||||
impl PathSeg {
|
||||
/// The subpaths of `path` as polylines (ellipses flattened to `n` segments), each with
|
||||
/// whether it is closed.
|
||||
@@ -248,8 +257,16 @@ impl PathSeg {
|
||||
/// self-intersecting and holed paths fill correctly (frontends' polygon fills are often
|
||||
/// convex-only). Exact trapezoid decomposition: the plane is cut into horizontal bands at
|
||||
/// every vertex and edge crossing; inside a band no edges cross, so each inside span is a
|
||||
/// trapezoid (two triangles).
|
||||
/// trapezoid (two triangles). Edges are compared pairwise and every band looks at every edge,
|
||||
/// on the UI thread, for geometry a script chooses, so a path past [`MAX_FILL_EDGES`] edges,
|
||||
/// [`MAX_FILL_BANDS`] bands, [`MAX_FILL_TRIANGLES`] triangles or [`MAX_FILL_WORK`] steps (edge
|
||||
/// pairs compared, then edges looked at per band) is filled only as far as the limit allows
|
||||
/// (topmost part first) and a warning is logged.
|
||||
pub fn fill_triangles(path: &[PathSeg], n: usize, even_odd: bool) -> Vec<[[f64; 2]; 3]> {
|
||||
Self::fill_triangles_within(path, n, even_odd, [MAX_FILL_EDGES, MAX_FILL_BANDS, MAX_FILL_TRIANGLES, MAX_FILL_WORK])
|
||||
}
|
||||
|
||||
fn fill_triangles_within(path: &[PathSeg], n: usize, even_odd: bool, [max_edges, max_bands, max_triangles, max_work]: [usize; 4]) -> Vec<[[f64; 2]; 3]> {
|
||||
// Non-horizontal edges, top to bottom, with their winding direction.
|
||||
let mut edges: Vec<([f64; 2], [f64; 2], i32)> = vec![];
|
||||
for (pts, _) in PathSeg::polylines(path, n) {
|
||||
@@ -261,15 +278,37 @@ impl PathSeg {
|
||||
edges.push(if a[1] < b[1] { (a, b, 1) } else { (b, a, -1) });
|
||||
}
|
||||
}
|
||||
if edges.len() > max_edges {
|
||||
log::warn!("fillPath: {} edges, filling only the first {max_edges}", edges.len());
|
||||
edges.truncate(max_edges);
|
||||
}
|
||||
if edges.len() < 2 {
|
||||
return vec![];
|
||||
}
|
||||
let x_at = |e: &([f64; 2], [f64; 2], i32), y: f64| e.0[0] + (e.1[0] - e.0[0]) * (y - e.0[1]) / (e.1[1] - e.0[1]);
|
||||
let mut ys: Vec<f64> = edges.iter().flat_map(|e| [e.0[1], e.1[1]]).collect();
|
||||
// Where two edges cross, a band boundary.
|
||||
for i in 0..edges.len() {
|
||||
for j in i + 1..edges.len() {
|
||||
// Where two edges cross, a band boundary. Edges by top, so each is compared only with the
|
||||
// ones that start above its bottom (the pairs that share some height); the pairs and the
|
||||
// boundaries found are counted against the budgets, and past one the rest are not looked
|
||||
// for. A band that holds a crossing that was not looked for is not filled (its edges
|
||||
// change order inside it, which the band loop below tells), so what is drawn is never
|
||||
// outside the path.
|
||||
let mut truncated = false;
|
||||
let mut order: Vec<usize> = (0..edges.len()).collect();
|
||||
order.sort_by(|&a, &b| edges[a].0[1].total_cmp(&edges[b].0[1]));
|
||||
let mut pairs = 0usize;
|
||||
'cross: for (k, &i) in order.iter().enumerate() {
|
||||
for &j in &order[k + 1..] {
|
||||
let (a, b) = (&edges[i], &edges[j]);
|
||||
if b.0[1] >= a.1[1] {
|
||||
break;
|
||||
}
|
||||
pairs += 1;
|
||||
if pairs > max_work || ys.len() > max_bands {
|
||||
log::warn!("fillPath: over its crossing budget; bands with crossings that were not cut are left unfilled");
|
||||
truncated = true;
|
||||
break 'cross;
|
||||
}
|
||||
let (lo, hi) = (a.0[1].max(b.0[1]), a.1[1].min(b.1[1]));
|
||||
if hi <= lo {
|
||||
continue;
|
||||
@@ -282,6 +321,11 @@ impl PathSeg {
|
||||
}
|
||||
ys.sort_by(f64::total_cmp);
|
||||
ys.dedup_by(|a, b| (*a - *b).abs() < 1e-9);
|
||||
if ys.len() > max_bands {
|
||||
log::warn!("fillPath: {} bands, filling only the topmost {max_bands}", ys.len());
|
||||
ys.truncate(max_bands);
|
||||
}
|
||||
let mut work = 0usize;
|
||||
let mut out = vec![];
|
||||
let mut row: Vec<(f64, f64, f64, i32)> = vec![];
|
||||
for w in ys.windows(2) {
|
||||
@@ -290,8 +334,14 @@ impl PathSeg {
|
||||
row.clear();
|
||||
row.extend(edges.iter().filter(|e| e.0[1] < ym && ym < e.1[1]).map(|e| (x_at(e, ym), x_at(e, ya), x_at(e, yb), e.2)));
|
||||
row.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
// With crossings missing, edges that swap places inside the band make its trapezoids
|
||||
// wrong: it is left out.
|
||||
let swapped = truncated && !row.windows(2).all(|w| w[0].1 <= w[1].1 + 1e-9 && w[0].2 <= w[1].2 + 1e-9);
|
||||
let mut wind = 0;
|
||||
for k in 0..row.len().saturating_sub(1) {
|
||||
if swapped {
|
||||
break;
|
||||
}
|
||||
wind += row[k].3;
|
||||
let inside = if even_odd { wind % 2 != 0 } else { wind != 0 };
|
||||
if inside {
|
||||
@@ -300,6 +350,13 @@ impl PathSeg {
|
||||
out.push([[l.1, ya], [r.2, yb], [l.2, yb]]);
|
||||
}
|
||||
}
|
||||
// (Every band's edge scan counts, a band left out included.)
|
||||
work = work.saturating_add(edges.len());
|
||||
if out.len() > max_triangles || work > max_work {
|
||||
log::warn!("fillPath: over its triangle or work budget, filled down to y = {yb}");
|
||||
out.truncate(max_triangles);
|
||||
break;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -951,4 +1008,50 @@ mod tests {
|
||||
let open = vec![PathSeg::M { x: 0.0, y: 0.0 }, PathSeg::L { x: 4.0, y: 0.0 }, PathSeg::L { x: 0.0, y: 4.0 }];
|
||||
assert!((area(&PathSeg::fill_triangles(&open, 48, false)) - 8.0).abs() < 1e-9);
|
||||
}
|
||||
|
||||
/// A script's path past the fill budgets is filled as far as they allow, never emptied.
|
||||
#[test]
|
||||
fn a_fill_past_its_budgets_draws_what_fits() {
|
||||
let area = |t: &[[[f64; 2]; 3]]| {
|
||||
t.iter().map(|t| ((t[1][0] - t[0][0]) * (t[2][1] - t[0][1]) - (t[2][0] - t[0][0]) * (t[1][1] - t[0][1])).abs() / 2.0).sum::<f64>()
|
||||
};
|
||||
let rect = |x: f64, y: f64, w: f64, h: f64| PathSeg::R { x, y, w, h };
|
||||
let rects: Vec<PathSeg> = (0..8).map(|i| rect(2.0 * i as f64, 0.0, 1.0, 10.0)).collect();
|
||||
let full = PathSeg::fill_triangles(&rects, 48, false);
|
||||
assert!((area(&full) - 80.0).abs() < 1e-9);
|
||||
// Edges over the limit: the first edges still fill (4 edges = 2 rectangles).
|
||||
let t = PathSeg::fill_triangles_within(&rects, 48, false, [4, usize::MAX, usize::MAX, usize::MAX]);
|
||||
assert!((area(&t) - 20.0).abs() < 1e-9, "{}", area(&t));
|
||||
// Triangles over the limit: truncated, not empty.
|
||||
let t = PathSeg::fill_triangles_within(&rects, 48, false, [usize::MAX, usize::MAX, 6, usize::MAX]);
|
||||
assert_eq!(t.len(), 6);
|
||||
// Bands over the limit: the topmost band fills.
|
||||
let stair: Vec<PathSeg> = (0..4).map(|i| rect(0.0, i as f64, 1.0, 1.0)).collect();
|
||||
let t = PathSeg::fill_triangles_within(&stair, 48, false, [usize::MAX, 2, usize::MAX, usize::MAX]);
|
||||
assert!(!t.is_empty() && area(&t) < 4.0);
|
||||
// Work over the limit stops the cut.
|
||||
let t = PathSeg::fill_triangles_within(&stair, 48, false, [usize::MAX, usize::MAX, usize::MAX, 1]);
|
||||
assert!(!t.is_empty() && area(&t) < 4.0);
|
||||
// Many crossing edges (every pair of 120 diagonals meets): the crossing scan is held to the
|
||||
// band and work budgets too.
|
||||
let fan: Vec<PathSeg> = (0..60)
|
||||
.flat_map(|i| {
|
||||
let x = i as f64;
|
||||
[PathSeg::M { x, y: 0.0 }, PathSeg::L { x: 60.0 - x, y: 100.0 }, PathSeg::L { x: x + 0.5, y: 100.0 }]
|
||||
})
|
||||
.collect();
|
||||
for limits in [[usize::MAX, 50, usize::MAX, usize::MAX], [usize::MAX, usize::MAX, usize::MAX, 500]] {
|
||||
let t = PathSeg::fill_triangles_within(&fan, 48, false, limits);
|
||||
assert!(area(&t) <= area(&PathSeg::fill_triangles(&fan, 48, false)) + 1e-6, "{limits:?}");
|
||||
}
|
||||
// Bands whose crossings were not looked for are not filled: a bow tie (area 50, crossing at
|
||||
// y = 5) under a crossing budget of one boundary gives no overlapping triangles.
|
||||
let bow = vec![PathSeg::M { x: 0.0, y: 0.0 }, PathSeg::L { x: 10.0, y: 10.0 }, PathSeg::L { x: 0.0, y: 10.0 }, PathSeg::L { x: 10.0, y: 0.0 }];
|
||||
assert!((area(&PathSeg::fill_triangles(&bow, 48, false)) - 50.0).abs() < 1e-9);
|
||||
let t = PathSeg::fill_triangles_within(&bow, 48, false, [usize::MAX, 2, usize::MAX, usize::MAX]);
|
||||
assert!(area(&t) <= 50.0 + 1e-9, "{}", area(&t));
|
||||
// Bands left out still count against the work budget (it ends the scan after the first).
|
||||
let t = PathSeg::fill_triangles_within(&bow, 48, false, [usize::MAX, 2, usize::MAX, 1]);
|
||||
assert!(area(&t) <= 50.0 + 1e-9, "{}", area(&t));
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
use super::{Comp, CompSelection, Event, ItemId, LayerId, Result, Session, Tick, Value, commands};
|
||||
|
||||
/// `t` as a current time of `comp`: a frame within its duration.
|
||||
/// `t` as a current time of `comp`: a frame within its duration. The last frame is the last one
|
||||
/// that starts before the end, so a final partial frame (a duration off the frame grid after a
|
||||
/// frame-rate change) stays reachable; on the grid it is `duration - frame_duration`.
|
||||
fn comp_frame_at(comp: &Comp, t: Tick) -> Tick {
|
||||
comp.frame_rate.snap_nearest(t.min(comp.duration - comp.frame_duration()).max(Tick::ZERO))
|
||||
let last = comp.frame_rate.snap(comp.duration - Tick(1)).max(Tick::ZERO);
|
||||
comp.frame_rate.snap_nearest(t.max(Tick::ZERO)).min(last)
|
||||
}
|
||||
|
||||
impl Session {
|
||||
@@ -114,18 +117,23 @@ impl Session {
|
||||
/// editor is open, so playback with no text editor does no text copying.
|
||||
pub fn set_time(&mut self, t: Tick) {
|
||||
if let Some(c) = self.state.active_comp {
|
||||
let t = self.project.comp(c).map_or(t, |comp| comp_frame_at(comp, t));
|
||||
let edited = commands::text_edit::active_editor_snapshot(self);
|
||||
self.state.times.insert(c, t);
|
||||
if self.prefs.general.sync_time_related_items {
|
||||
// Direct call: this `set_time` owns the editor snapshot for both time changes,
|
||||
// so a synced move does not take a second snapshot or start a second epoch.
|
||||
self.sync_related_times_inner(c, t);
|
||||
}
|
||||
commands::text_edit::after_external_change(self, edited.as_ref());
|
||||
self.set_time_of(c, t);
|
||||
}
|
||||
}
|
||||
|
||||
/// Move `comp`'s current time, as [`Session::set_time`] does for the active comp.
|
||||
pub fn set_time_of(&mut self, c: ItemId, t: Tick) {
|
||||
let t = self.project.comp(c).map_or(t, |comp| comp_frame_at(comp, t));
|
||||
let edited = commands::text_edit::active_editor_snapshot(self);
|
||||
self.state.times.insert(c, t);
|
||||
if self.prefs.general.sync_time_related_items {
|
||||
// Direct call: this `set_time_of` owns the editor snapshot for both time changes,
|
||||
// so a synced move does not take a second snapshot or start a second epoch.
|
||||
self.sync_related_times_inner(c, t);
|
||||
}
|
||||
commands::text_edit::after_external_change(self, edited.as_ref());
|
||||
}
|
||||
|
||||
/// Settings ▸ General ▸ Synchronize Time of All Related Items: moving the current time of
|
||||
/// `comp` moves the current time of the comps nested in it (through their precomp layers'
|
||||
/// timing) and of the comps it is nested in, so every related viewer shows the same moment.
|
||||
@@ -139,8 +147,8 @@ impl Session {
|
||||
|
||||
/// Sets the time of every comp related to `comp` through precomp layers: down into the comps
|
||||
/// it nests (through each layer's timing) and up into the comps nesting it. A seen-set keeps
|
||||
/// cycles and diamonds finite; each found time is clamped to its comp's last frame, snapped to
|
||||
/// the nearest frame and stored in `state.times`. The caller owns the text-editor boundary.
|
||||
/// cycles and diamonds finite; each found time goes through [`comp_frame_at`] (as a direct move
|
||||
/// does) and is stored in `state.times`. The caller owns the text-editor boundary.
|
||||
fn sync_related_times_inner(&mut self, comp: ItemId, t: Tick) {
|
||||
let mut seen = std::collections::BTreeSet::from([comp]);
|
||||
let mut todo = vec![(comp, t)];
|
||||
@@ -170,7 +178,7 @@ impl Session {
|
||||
}
|
||||
for (cid, ct) in found {
|
||||
if let Some(c) = self.project.comp(cid) {
|
||||
let ct = c.frame_rate.snap_nearest(ct.clamp(Tick::ZERO, (c.duration - c.frame_duration()).max(Tick::ZERO)));
|
||||
let ct = comp_frame_at(c, ct);
|
||||
self.state.times.insert(cid, ct);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,15 +103,22 @@ impl Session {
|
||||
effectcraft_project::ItemKind::Footage(f) if f.has_video => {
|
||||
let f = f.clone();
|
||||
Some(Box::new(move || {
|
||||
let img = effectcraft_render::cineon_frame(footage.frame(item, &f, Tick(0))?, &f);
|
||||
// Point-sample down to at most `max_side` on the long side.
|
||||
let step = (img.width.max(img.height) as f32 / max_side.max(1) as f32).ceil().max(1.0) as u32;
|
||||
let (w, h) = (img.width.div_ceil(step), img.height.div_ceil(step));
|
||||
// Vector footage is drawn at the thumbnail's size: its declared size can be a
|
||||
// 16384² canvas, gigabytes of pixels to point-sample down to a few dozen.
|
||||
let scale = (max_side.max(1) as f64 / f.width.max(f.height).max(1) as f64).min(1.0);
|
||||
let src = footage.vector_frame(item, &f, scale).or_else(|| footage.frame(item, &f, Tick(0)))?;
|
||||
let img = effectcraft_render::cineon_frame(src, &f);
|
||||
// Point-sample down to at most `max_side` on the long side (a drawn size one
|
||||
// pixel over it, from rounding, is shrunk by that pixel, not halved).
|
||||
let (long, max) = (u64::from(img.width.max(img.height)), u64::from(max_side.max(1)));
|
||||
let fit = |side: u32| if long <= max { side } else { ((u64::from(side) * max + long / 2) / long).max(1) as u32 };
|
||||
let (w, h) = (fit(img.width), fit(img.height));
|
||||
let full = img.to_rgba8();
|
||||
let mut px = Vec::with_capacity((w * h * 4) as usize);
|
||||
for y in 0..h {
|
||||
for x in 0..w {
|
||||
let i = (((y * step) * img.width + x * step) * 4) as usize;
|
||||
let (sx, sy) = (u64::from(x) * u64::from(img.width) / u64::from(w), u64::from(y) * u64::from(img.height) / u64::from(h));
|
||||
let i = ((sy * u64::from(img.width) + sx) * 4) as usize;
|
||||
px.extend_from_slice(&full[i..i + 4]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,6 +95,7 @@ pub fn pages() -> Vec<Page> {
|
||||
s("general.showToolTips", "Show Tool Tips", B, true),
|
||||
s("general.viewerWheelScrubbing", "Scrub Composition Frames with Mouse Wheel", B, true),
|
||||
s("general.createLayersAtCompStart", "Create Layers at Composition Start Time", B, true),
|
||||
s("general.fitNewLayersToWorkArea", "Fit New Layers to Work Area", B, true),
|
||||
s("general.switchesAffectNestedComps", "Switches Affect Nested Comps", B, true),
|
||||
s("general.defaultSpatialLinear", "Default Spatial Interpolation to Linear", B, true),
|
||||
s("general.preserveConstantVertexCount", "Preserve Constant Vertex and Feather Point Count when Editing Masks", B, true),
|
||||
|
||||
@@ -36,6 +36,9 @@ page!(General {
|
||||
viewer_wheel_scrubbing: bool = false,
|
||||
/// Create New Layers at Composition Start Time (off: at the current time).
|
||||
create_layers_at_comp_start: bool = true,
|
||||
/// Fit New Layers to Work Area: new text, solid, shape, null and adjustment layers span the
|
||||
/// work area instead of the whole composition (off, as in After Effects).
|
||||
fit_new_layers_to_work_area: bool = false,
|
||||
switches_affect_nested_comps: bool = true,
|
||||
/// Default Spatial Interpolation to Linear.
|
||||
default_spatial_linear: bool = false,
|
||||
|
||||
@@ -550,6 +550,10 @@ impl Keymaps {
|
||||
}
|
||||
}
|
||||
|
||||
/// Most conflicts [`ShortcutTable::all_conflicts`] lists: a preset binding every command to one
|
||||
/// key would otherwise list every pair of them.
|
||||
pub const MAX_CONFLICTS: usize = 10_000;
|
||||
|
||||
/// The active preset resolved against every bindable command.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct ShortcutTable {
|
||||
@@ -584,8 +588,15 @@ impl ShortcutTable {
|
||||
}
|
||||
|
||||
/// Every active binding: (keys, bindable).
|
||||
/// Each bindable's keys once: an imported preset can repeat a binding any number of times.
|
||||
pub fn bindings(&self) -> Vec<(&str, &Bindable)> {
|
||||
self.bindables.iter().flat_map(|b| self.keys.get(&b.key).into_iter().flatten().map(move |k| (k.as_str(), b))).collect()
|
||||
self.bindables
|
||||
.iter()
|
||||
.flat_map(|b| {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
self.keys.get(&b.key).into_iter().flatten().filter(move |k| seen.insert(k.as_str())).map(move |k| (k.as_str(), b))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Other bindables using `keys` in an overlapping scope.
|
||||
@@ -607,6 +618,7 @@ impl ShortcutTable {
|
||||
/// Every pair of commands sharing keys in the same scope, or in two contexts (which can be
|
||||
/// active at once): only one of the two can run, or neither ([`pick`]). (Other pairs in
|
||||
/// different scopes are intended, see [`Conflict::overrides`].)
|
||||
/// At most [`MAX_CONFLICTS`] are listed.
|
||||
pub fn all_conflicts(&self) -> Vec<(String, Conflict)> {
|
||||
let mut by_keys: BTreeMap<&str, Vec<&Bindable>> = BTreeMap::new();
|
||||
for (k, b) in self.bindings() {
|
||||
@@ -618,6 +630,9 @@ impl ShortcutTable {
|
||||
for (k, bs) in by_keys {
|
||||
for (i, a) in bs.iter().enumerate() {
|
||||
for b in &bs[i + 1..] {
|
||||
if out.len() >= MAX_CONFLICTS {
|
||||
return out;
|
||||
}
|
||||
if a.scope == b.scope || (is_context(&a.scope) && is_context(&b.scope)) {
|
||||
out.push((
|
||||
a.key.clone(),
|
||||
|
||||
@@ -125,7 +125,13 @@ fn utc_parts(secs: u64, nanos: u32) -> (i64, chrono::NaiveDateTime) {
|
||||
|
||||
/// One log line (without the newline): `<timestamp> <LEVEL> [<thread>] <target>: <message>`.
|
||||
fn format_line(ts: &str, level: log::Level, thread: &str, target: &str, message: &str) -> String {
|
||||
let message = message.strip_suffix('\n').unwrap_or(message);
|
||||
// CRLF becomes LF first, so a message ending in "\r\n" loses its final break like one ending
|
||||
// in "\n" (and its carriage return is not taken for a lone one).
|
||||
let message = message.replace("\r\n", "\n");
|
||||
let message = message.strip_suffix('\n').unwrap_or(&message);
|
||||
// A message's own line breaks (a path or name from a project or a script) continue it on
|
||||
// indented lines, so they can't pass for records of their own.
|
||||
let message = message.replace('\r', "\\r").replace('\n', "\n\t");
|
||||
format!("{ts} {level:<5} [{thread}] {target}: {message}")
|
||||
}
|
||||
|
||||
@@ -523,4 +529,22 @@ mod tests {
|
||||
"2026-10-08T07:59:17.728Z WARN [main] effectcraft_engine::autosave: disk full"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn line_breaks_in_a_message_continue_it_on_indented_lines() {
|
||||
let forged = "cache at /tmp/x\n2026-10-08T07:59:18.000Z ERROR [main] effectcraft_engine: forged\rend";
|
||||
let line = format_line("2026-10-08T07:59:17.728Z", log::Level::Info, "main", "t", forged);
|
||||
assert_eq!(line.lines().count(), 2);
|
||||
assert!(line.lines().nth(1).unwrap().starts_with('\t'), "{line}");
|
||||
assert!(!line.contains('\r'), "{line}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_trailing_crlf_is_one_line_break_not_a_lone_carriage_return() {
|
||||
let line = |m| format_line("t", log::Level::Info, "main", "t", m);
|
||||
assert_eq!(line("cache\r\n"), line("cache"));
|
||||
assert_eq!(line("a\r\nb\r\n"), line("a\nb"));
|
||||
// A lone one, even at the end, is still escaped.
|
||||
assert!(line("cache\r").ends_with("cache\\r"));
|
||||
}
|
||||
}
|
||||
@@ -331,3 +331,17 @@ fn background_pre_render_movies_are_checked_again_before_publication() {
|
||||
assert!(listing(&f.dir).iter().all(|name| !name.starts_with(".effectcraft-prerender-")));
|
||||
}
|
||||
}
|
||||
|
||||
/// Footage files sharing a name are numbered from that name (`3_a.mov`, not `3_2_a.mov`).
|
||||
#[test]
|
||||
fn collect_files_numbers_repeated_names_from_the_name() {
|
||||
let mut f = collecting("collect-repeated", "a.mov");
|
||||
for d in ["in2", "in3"] {
|
||||
std::fs::create_dir_all(f.path(d)).unwrap();
|
||||
f.footage(&format!("{d}/a.mov"));
|
||||
}
|
||||
collect(&mut f).unwrap();
|
||||
let mut names = listing(&f.path("out/(Footage)"));
|
||||
names.sort();
|
||||
assert_eq!(names, ["2_a.mov", "3_a.mov", "a.mov"]);
|
||||
}
|
||||
@@ -0,0 +1,947 @@
|
||||
{
|
||||
"savedBy": "0.22.0",
|
||||
"activeComp": 1,
|
||||
"openComps": [
|
||||
1
|
||||
],
|
||||
"schema": 2,
|
||||
"settings": {
|
||||
"bit_depth": "Bpc8",
|
||||
"linearize": false,
|
||||
"working_space": null,
|
||||
"blend_linear": false,
|
||||
"time_display": "Timecode",
|
||||
"frame_start": 0,
|
||||
"audio_sample_rate": 48000,
|
||||
"gpu_acceleration": true,
|
||||
"color_engine": "Adobe",
|
||||
"hdr": "Clip",
|
||||
"output_space": null,
|
||||
"working_gamma": 2.2
|
||||
},
|
||||
"items": {
|
||||
"1": {
|
||||
"id": 1,
|
||||
"name": "Main",
|
||||
"label": "Sandstone",
|
||||
"comment": "",
|
||||
"parent": null,
|
||||
"kind": {
|
||||
"type": "Comp",
|
||||
"width": 64,
|
||||
"height": 48,
|
||||
"pixel_aspect": 1.0,
|
||||
"frame_rate": {
|
||||
"num": 30,
|
||||
"den": 1
|
||||
},
|
||||
"duration": 25401600000,
|
||||
"display_start": 0,
|
||||
"background": [
|
||||
0.0,
|
||||
0.0,
|
||||
0.0
|
||||
],
|
||||
"work_area": [
|
||||
0,
|
||||
25401600000
|
||||
],
|
||||
"layers": [
|
||||
{
|
||||
"id": 3,
|
||||
"name": "effectcraft-3430-subject.mp4",
|
||||
"source": {
|
||||
"type": "Footage",
|
||||
"item": 2
|
||||
},
|
||||
"label": "Aqua",
|
||||
"comment": "",
|
||||
"start_time": 0,
|
||||
"in_point": 0,
|
||||
"out_point": 25401600000,
|
||||
"stretch": 100.0,
|
||||
"switches": {
|
||||
"video": true,
|
||||
"audio": true,
|
||||
"solo": false,
|
||||
"locked": false,
|
||||
"shy": false,
|
||||
"collapse": false,
|
||||
"quality": "Best",
|
||||
"sampling": "Bilinear",
|
||||
"effects": true,
|
||||
"frame_blend": "Off",
|
||||
"motion_blur": false,
|
||||
"adjustment": false,
|
||||
"three_d": false,
|
||||
"guide": false
|
||||
},
|
||||
"blend_mode": "Normal",
|
||||
"preserve_transparency": false,
|
||||
"track_matte": null,
|
||||
"parent": null,
|
||||
"markers": [],
|
||||
"auto_orient": "Off",
|
||||
"props": {
|
||||
"uid": 4,
|
||||
"match": "layer",
|
||||
"name": "effectcraft-3430-subject.mp4",
|
||||
"kind": {
|
||||
"kind": "Plain"
|
||||
},
|
||||
"enabled": true,
|
||||
"children": [
|
||||
{
|
||||
"node": "Group",
|
||||
"uid": 5,
|
||||
"match": "masks",
|
||||
"name": "Masks",
|
||||
"kind": {
|
||||
"kind": "Plain"
|
||||
},
|
||||
"enabled": true,
|
||||
"children": []
|
||||
},
|
||||
{
|
||||
"node": "Group",
|
||||
"uid": 6,
|
||||
"match": "effects",
|
||||
"name": "Effects",
|
||||
"kind": {
|
||||
"kind": "Plain"
|
||||
},
|
||||
"enabled": true,
|
||||
"children": [
|
||||
{
|
||||
"node": "Group",
|
||||
"uid": 26,
|
||||
"match": "ec.matte.objectmatte",
|
||||
"name": "Object Matte",
|
||||
"kind": {
|
||||
"kind": "Effect",
|
||||
"effect": "ec.matte.objectmatte"
|
||||
},
|
||||
"enabled": true,
|
||||
"children": [
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 27,
|
||||
"match": "objectPoint",
|
||||
"name": "Object Point",
|
||||
"value": {
|
||||
"t": "Vec2",
|
||||
"v": [
|
||||
32.0,
|
||||
24.0
|
||||
]
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Point"
|
||||
},
|
||||
"spatial": true,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 28,
|
||||
"match": "useBox",
|
||||
"name": "Use Box",
|
||||
"value": {
|
||||
"t": "Bool",
|
||||
"v": true
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Checkbox"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": true,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 29,
|
||||
"match": "boxTopLeft",
|
||||
"name": "Box Top Left",
|
||||
"value": {
|
||||
"t": "Vec2",
|
||||
"v": [
|
||||
14.0,
|
||||
8.0
|
||||
]
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Point"
|
||||
},
|
||||
"spatial": true,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 30,
|
||||
"match": "boxBottomRight",
|
||||
"name": "Box Bottom Right",
|
||||
"value": {
|
||||
"t": "Vec2",
|
||||
"v": [
|
||||
50.0,
|
||||
40.0
|
||||
]
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Point"
|
||||
},
|
||||
"spatial": true,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Group",
|
||||
"uid": 32,
|
||||
"match": "objectTracking",
|
||||
"name": "Tracking",
|
||||
"kind": {
|
||||
"kind": "Plain"
|
||||
},
|
||||
"enabled": true,
|
||||
"children": [
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 31,
|
||||
"match": "trackObject",
|
||||
"name": "Track Object",
|
||||
"value": {
|
||||
"t": "Bool",
|
||||
"v": true
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Checkbox"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": true,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 33,
|
||||
"match": "referenceFrame",
|
||||
"name": "Reference Frame",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 0.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Number"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 34,
|
||||
"match": "framesBefore",
|
||||
"name": "Frames Before",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 0.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Slider",
|
||||
"min": 0.0,
|
||||
"max": 600.0,
|
||||
"slider_min": 0.0,
|
||||
"slider_max": 120.0,
|
||||
"decimals": 0
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 35,
|
||||
"match": "framesAfter",
|
||||
"name": "Frames After",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 0.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Slider",
|
||||
"min": 0.0,
|
||||
"max": 600.0,
|
||||
"slider_min": 0.0,
|
||||
"slider_max": 120.0,
|
||||
"decimals": 0
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"node": "Group",
|
||||
"uid": 37,
|
||||
"match": "objectMatte",
|
||||
"name": "Matte",
|
||||
"kind": {
|
||||
"kind": "Plain"
|
||||
},
|
||||
"enabled": true,
|
||||
"children": [
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 36,
|
||||
"match": "feather",
|
||||
"name": "Feather",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 0.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Slider",
|
||||
"min": 0.0,
|
||||
"max": 100.0,
|
||||
"slider_min": 0.0,
|
||||
"slider_max": 20.0,
|
||||
"decimals": 1
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 38,
|
||||
"match": "contrast",
|
||||
"name": "Contrast",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 0.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Slider",
|
||||
"min": 0.0,
|
||||
"max": 100.0,
|
||||
"slider_min": 0.0,
|
||||
"slider_max": 100.0,
|
||||
"decimals": 0
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 39,
|
||||
"match": "shiftEdge",
|
||||
"name": "Shift Edge",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 0.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Slider",
|
||||
"min": -100.0,
|
||||
"max": 100.0,
|
||||
"slider_min": -100.0,
|
||||
"slider_max": 100.0,
|
||||
"decimals": 0
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 40,
|
||||
"match": "invert",
|
||||
"name": "Invert",
|
||||
"value": {
|
||||
"t": "Bool",
|
||||
"v": false
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Checkbox"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": true,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 41,
|
||||
"match": "view",
|
||||
"name": "View",
|
||||
"value": {
|
||||
"t": "Enum",
|
||||
"v": 0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Popup",
|
||||
"options": [
|
||||
"Final Output",
|
||||
"Matte"
|
||||
]
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": true,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 42,
|
||||
"match": "inputKey",
|
||||
"name": "Input Key",
|
||||
"value": {
|
||||
"t": "Str",
|
||||
"v": "cbefe0c85977c4aa"
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Hidden"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": true,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 43,
|
||||
"match": "frozen",
|
||||
"name": "Frozen",
|
||||
"value": {
|
||||
"t": "Str",
|
||||
"v": "{\"key\":\"1a097b69366afd7f\",\"size\":[64,48],\"frames\":{\"0\":{\"a\":\"gBgAkAX/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IACQBQ==\",\"b\":\"AA==\"}}}"
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Hidden"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": true,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"node": "Group",
|
||||
"uid": 15,
|
||||
"match": "transform",
|
||||
"name": "Transform",
|
||||
"kind": {
|
||||
"kind": "Plain"
|
||||
},
|
||||
"enabled": true,
|
||||
"children": [
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 7,
|
||||
"match": "anchor",
|
||||
"name": "Anchor Point",
|
||||
"value": {
|
||||
"t": "Vec3",
|
||||
"v": [
|
||||
32.0,
|
||||
24.0,
|
||||
0.0
|
||||
]
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Point"
|
||||
},
|
||||
"spatial": true,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 2,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 8,
|
||||
"match": "position",
|
||||
"name": "Position",
|
||||
"value": {
|
||||
"t": "Vec3",
|
||||
"v": [
|
||||
32.0,
|
||||
24.0,
|
||||
0.0
|
||||
]
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Point"
|
||||
},
|
||||
"spatial": true,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 2,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 9,
|
||||
"match": "scale",
|
||||
"name": "Scale",
|
||||
"value": {
|
||||
"t": "Vec3",
|
||||
"v": [
|
||||
100.0,
|
||||
100.0,
|
||||
100.0
|
||||
]
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Percent"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 2,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 10,
|
||||
"match": "orientation",
|
||||
"name": "Orientation",
|
||||
"value": {
|
||||
"t": "Vec3",
|
||||
"v": [
|
||||
0.0,
|
||||
0.0,
|
||||
0.0
|
||||
]
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Angle"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": true,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 11,
|
||||
"match": "rotationX",
|
||||
"name": "X Rotation",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 0.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Angle"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": true,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 12,
|
||||
"match": "rotationY",
|
||||
"name": "Y Rotation",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 0.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Angle"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": true,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 13,
|
||||
"match": "rotation",
|
||||
"name": "Rotation",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 0.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Angle"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 14,
|
||||
"match": "opacity",
|
||||
"name": "Opacity",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 100.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Slider",
|
||||
"min": 0.0,
|
||||
"max": 100.0,
|
||||
"slider_min": 0.0,
|
||||
"slider_max": 100.0,
|
||||
"decimals": 0
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"node": "Group",
|
||||
"uid": 16,
|
||||
"match": "materialOptions",
|
||||
"name": "Material Options",
|
||||
"kind": {
|
||||
"kind": "Plain"
|
||||
},
|
||||
"enabled": true,
|
||||
"children": [
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 17,
|
||||
"match": "castsShadows",
|
||||
"name": "Casts Shadows",
|
||||
"value": {
|
||||
"t": "Enum",
|
||||
"v": 0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Popup",
|
||||
"options": [
|
||||
"Off",
|
||||
"On",
|
||||
"Only"
|
||||
]
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": true,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 18,
|
||||
"match": "lightTransmission",
|
||||
"name": "Light Transmission",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 0.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Percent"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 19,
|
||||
"match": "acceptsShadows",
|
||||
"name": "Accepts Shadows",
|
||||
"value": {
|
||||
"t": "Enum",
|
||||
"v": 1
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Popup",
|
||||
"options": [
|
||||
"Off",
|
||||
"On",
|
||||
"Only"
|
||||
]
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": true,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 20,
|
||||
"match": "acceptsLights",
|
||||
"name": "Accepts Lights",
|
||||
"value": {
|
||||
"t": "Bool",
|
||||
"v": true
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Checkbox"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": true,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 21,
|
||||
"match": "ambient",
|
||||
"name": "Ambient",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 100.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Percent"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 22,
|
||||
"match": "diffuse",
|
||||
"name": "Diffuse",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 50.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Percent"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 23,
|
||||
"match": "specularIntensity",
|
||||
"name": "Specular Intensity",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 50.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Percent"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 24,
|
||||
"match": "specularShininess",
|
||||
"name": "Specular Shininess",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 5.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Percent"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
{
|
||||
"node": "Prop",
|
||||
"uid": 25,
|
||||
"match": "metal",
|
||||
"name": "Metal",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 100.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Percent"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"markers": [],
|
||||
"shutter_angle": 180.0,
|
||||
"shutter_phase": -90.0,
|
||||
"motion_blur_samples": 16,
|
||||
"motion_blur_adaptive_limit": 128,
|
||||
"renderer": "Classic3D",
|
||||
"hide_shy": false,
|
||||
"enable_motion_blur": true,
|
||||
"enable_frame_blending": true,
|
||||
"draft_3d": false,
|
||||
"preserve_frame_rate": false,
|
||||
"preserve_resolution": false,
|
||||
"poster_time": 0,
|
||||
"global_light": {
|
||||
"angle": {
|
||||
"uid": 0,
|
||||
"match": "globalLightAngle",
|
||||
"name": "Global Light Angle",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 120.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Angle"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
},
|
||||
"altitude": {
|
||||
"uid": 0,
|
||||
"match": "globalLightAltitude",
|
||||
"name": "Global Light Altitude",
|
||||
"value": {
|
||||
"t": "Scalar",
|
||||
"v": 30.0
|
||||
},
|
||||
"ui": {
|
||||
"kind": "Angle"
|
||||
},
|
||||
"spatial": false,
|
||||
"hold_only": false,
|
||||
"three_d_only": false,
|
||||
"two_d_only": false,
|
||||
"shown_dims": 0,
|
||||
"static_only": false
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"2": {
|
||||
"id": 2,
|
||||
"name": "effectcraft-3430-subject.mp4",
|
||||
"label": "Aqua",
|
||||
"comment": "",
|
||||
"parent": null,
|
||||
"kind": {
|
||||
"type": "Footage",
|
||||
"path": "/tmp/effectcraft-3430-subject.mp4",
|
||||
"kind": "Video",
|
||||
"width": 64,
|
||||
"height": 48,
|
||||
"pixel_aspect": 1.0,
|
||||
"frame_rate": {
|
||||
"num": 30,
|
||||
"den": 1
|
||||
},
|
||||
"native_rate": null,
|
||||
"duration": 25401600000,
|
||||
"has_video": true,
|
||||
"has_audio": false,
|
||||
"alpha": "Ignore",
|
||||
"premul_color": [
|
||||
0.0,
|
||||
0.0,
|
||||
0.0
|
||||
],
|
||||
"loop_count": 1,
|
||||
"codec": "H.264 (High)",
|
||||
"missing": false,
|
||||
"color_profile": "Rec709"
|
||||
}
|
||||
}
|
||||
},
|
||||
"next_id": 44
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
asset: object-matte-0.22.ecproj
|
||||
title: One frozen Object Matte from EffectCraft 0.22.0
|
||||
author: lisn0
|
||||
source: original work
|
||||
license: MIT OR Apache-2.0
|
||||
license-file: ../../../../../LICENSE-MIT
|
||||
added: 2026-11-11 by lisn0
|
||||
notes: Saved by the released 0.22.0 CLI using a generated 64x48 orange rectangle on a blue background. Contains one frozen alpha plane. The source path is a generic test path, and the source video is unnecessary for the state-preservation regression test.
|
||||
@@ -603,6 +603,35 @@ fn object_matte_background_job_cancel_and_freeze() {
|
||||
assert_eq!(status(&mut s)["frozen"], true);
|
||||
}
|
||||
|
||||
/// A queue edit preserves a frozen matte made before the footage signature gained placed_page.
|
||||
#[test]
|
||||
fn render_queue_keeps_frozen_object_matte_with_legacy_footage_key() {
|
||||
use effectcraft_effects::roto::{FROZEN, INPUT_KEY};
|
||||
use effectcraft_keyframe::Value::Str;
|
||||
|
||||
hold_roto_cache_test_lock();
|
||||
// Saved by the released 0.22.0 CLI, independently of the compatibility helper.
|
||||
let project = effectcraft_project::Project::from_json(include_str!("fixtures/object-matte-0.22.ecproj")).unwrap();
|
||||
let mut s = Session::default();
|
||||
s.replace_project(project, None);
|
||||
let cid = effectcraft_project::ItemId(1);
|
||||
let clip = LayerId(3);
|
||||
let uid = 26;
|
||||
s.open_comp(cid);
|
||||
let params = |s: &Session| crate::roto::params_static(s.project.comp(cid).unwrap().layer(clip).unwrap().props.find_group(uid).unwrap());
|
||||
let legacy = params(&s).get(INPUT_KEY).unwrap().clone();
|
||||
let saved = params(&s).get(FROZEN).unwrap().clone();
|
||||
let layer = s.project.comp(cid).unwrap().layer(clip).unwrap();
|
||||
assert_ne!(legacy, Str(crate::roto::input_key(&s.project, cid, layer, uid)));
|
||||
|
||||
s.execute_checked("renderQueue.add", json!({"comp": cid.0, "format": "png", "output": "matte.png"})).unwrap();
|
||||
assert_eq!(params(&s).get(INPUT_KEY), Some(&legacy));
|
||||
assert_eq!(params(&s).get(FROZEN), Some(&saved));
|
||||
|
||||
s.execute_checked("layer.timing", json!({"layer": clip.0, "out": 0.05})).unwrap();
|
||||
assert_eq!(params(&s).get(FROZEN), Some(&Str(String::new())));
|
||||
}
|
||||
|
||||
/// Computed Object Matte segmentations go to the persistent disk cache: a second session on the
|
||||
/// same folder and project gets them back without segmenting (its frames cannot even be read
|
||||
/// here), and another prompt (other keys) finds nothing.
|
||||
|
||||
@@ -389,6 +389,127 @@ fn new_layers_start_at_the_current_time_when_asked() {
|
||||
assert_eq!(s.active_comp().unwrap().layers[0].start_time.seconds().round(), 2.0);
|
||||
}
|
||||
|
||||
/// Settings ▸ General ▸ Fit New Layers to Work Area: new text, solid, shape, null and adjustment
|
||||
/// layers span the work area, ahead of the start-time setting; footage keeps its own length.
|
||||
#[test]
|
||||
fn new_layers_fit_the_work_area_when_asked() {
|
||||
let mut s = session_with_comp();
|
||||
s.execute("comp.workArea", json!({"start": 2, "end": 5})).unwrap();
|
||||
let span = |s: &Session| {
|
||||
let l = &s.active_comp().unwrap().layers[0];
|
||||
let r = |t: effectcraft_time::Tick| (t.seconds() * 10.0).round() / 10.0;
|
||||
(r(l.start_time), r(l.in_point), r(l.out_point))
|
||||
};
|
||||
s.execute("layer.newSolid", json!({})).unwrap();
|
||||
assert_eq!(span(&s), (0.0, 0.0, 10.0), "off by default");
|
||||
|
||||
s.execute("prefs.set", json!({"key": "general.fitNewLayersToWorkArea", "value": true})).unwrap();
|
||||
s.execute("prefs.set", json!({"key": "general.createLayersAtCompStart", "value": false})).unwrap();
|
||||
// The tools that draw on their new layer (Type, shape tools, Pen) move the playhead into it;
|
||||
// the menu commands leave it.
|
||||
for (cmd, p, draws) in [
|
||||
("layer.newSolid", json!({}), false),
|
||||
("layer.newAdjustment", json!({}), false),
|
||||
("layer.newNull", json!({}), false),
|
||||
("layer.newText", json!({"text": "a"}), false),
|
||||
("layer.newText", json!({"text": "a", "edit": true}), true),
|
||||
("layer.newShape", json!({"kind": "rect"}), false),
|
||||
("shape.newShape", json!({"kind": "ellipse"}), true),
|
||||
("shape.newPath", json!({"vertices": [[0, 0], [10, 0], [10, 10]]}), true),
|
||||
] {
|
||||
s.set_time(effectcraft_time::Tick::from_seconds_f64(7.0));
|
||||
s.state.selected_layers.clear();
|
||||
s.state.text_edit = None;
|
||||
s.execute(cmd, p).unwrap();
|
||||
assert_eq!(span(&s), (2.0, 2.0, 5.0), "{cmd}");
|
||||
assert_eq!(s.time().seconds().round(), if draws { 2.0 } else { 7.0 }, "{cmd} playhead");
|
||||
}
|
||||
s.set_time(effectcraft_time::Tick::from_seconds_f64(7.0));
|
||||
|
||||
let nested = s.execute("comp.new", json!({"name": "Nested", "duration": 3, "open": false})).unwrap()["comp"].as_u64().unwrap();
|
||||
s.execute("layer.addItem", json!({"item": nested})).unwrap();
|
||||
assert_eq!(span(&s), (7.0, 7.0, 10.0), "footage and comps keep their own timing");
|
||||
}
|
||||
|
||||
/// A tool command aimed at another comp moves that comp's playhead into its work area and
|
||||
/// leaves the active comp's playhead where it was.
|
||||
#[test]
|
||||
fn fitted_layers_in_another_comp_move_that_comps_playhead() {
|
||||
let mut s = session_with_comp();
|
||||
let active = s.active_comp_id().unwrap();
|
||||
s.execute("prefs.set", json!({"key": "general.fitNewLayersToWorkArea", "value": true})).unwrap();
|
||||
let other = effectcraft_project::ItemId(s.execute("comp.new", json!({"name": "Other", "duration": 10, "open": false})).unwrap()["comp"].as_u64().unwrap());
|
||||
s.execute("comp.workArea", json!({"comp": other.0, "start": 2, "end": 5})).unwrap();
|
||||
s.execute("comp.workArea", json!({"start": 8, "end": 9})).unwrap();
|
||||
s.set_time(effectcraft_time::Tick::from_seconds_f64(7.0));
|
||||
for (cmd, p) in [
|
||||
("layer.newText", json!({"comp": other.0, "text": "a", "edit": true})),
|
||||
("shape.newShape", json!({"comp": other.0, "kind": "ellipse"})),
|
||||
("shape.newPath", json!({"comp": other.0, "vertices": [[0, 0], [10, 0], [10, 10]]})),
|
||||
] {
|
||||
s.set_time_of(other, effectcraft_time::Tick::from_seconds_f64(7.0));
|
||||
s.state.selected_layers.clear();
|
||||
s.state.text_edit = None;
|
||||
s.execute(cmd, p).unwrap();
|
||||
assert_eq!(s.time_of(other).seconds().round(), 2.0, "{cmd}: the target comp's playhead");
|
||||
assert_eq!(s.time_of(active).seconds().round(), 7.0, "{cmd}: the active comp's playhead");
|
||||
}
|
||||
}
|
||||
|
||||
/// A work area off the frame grid (set at 25 fps, then the comp went to 30 fps): the playhead
|
||||
/// goes to the first frame inside it, not the nearest frame before its start.
|
||||
#[test]
|
||||
fn fitted_layers_off_the_frame_grid_move_the_playhead_inside_them() {
|
||||
let mut s = Session::default();
|
||||
s.execute("comp.new", json!({"name": "Grid", "duration": 2, "frameRate": 25})).unwrap();
|
||||
s.execute("prefs.set", json!({"key": "general.fitNewLayersToWorkArea", "value": true})).unwrap();
|
||||
s.execute("comp.workArea", json!({"start": 0.04, "end": 0.08})).unwrap();
|
||||
s.execute("comp.settings", json!({"frameRate": 30})).unwrap();
|
||||
for (cmd, p) in [("layer.newText", json!({"text": "a", "edit": true})), ("shape.newPath", json!({"vertices": [[0, 0], [10, 0], [10, 10]]}))] {
|
||||
s.set_time(effectcraft_time::Tick::from_seconds_f64(1.0));
|
||||
s.state.selected_layers.clear();
|
||||
s.state.text_edit = None;
|
||||
s.execute(cmd, p).unwrap();
|
||||
let l = &s.active_comp().unwrap().layers[0];
|
||||
let t = s.time();
|
||||
assert!(l.in_point <= t && t < l.out_point, "{cmd}: {t:?} in {:?}..{:?}", l.in_point, l.out_point);
|
||||
assert_eq!(t, effectcraft_time::Tick::from_seconds_f64(2.0 / 30.0), "{cmd}: the first frame inside");
|
||||
}
|
||||
}
|
||||
|
||||
/// A duration off the frame grid (2.01 s set at 100 fps, then 24 fps) keeps its final partial
|
||||
/// frame reachable: a layer fitted to a work area inside it gets the playhead on that frame.
|
||||
#[test]
|
||||
fn fitted_layers_in_the_final_partial_frame_get_the_playhead() {
|
||||
let mut s = Session::default();
|
||||
s.execute("comp.new", json!({"name": "Tail", "duration": 2.01, "frameRate": 100})).unwrap();
|
||||
s.execute("prefs.set", json!({"key": "general.fitNewLayersToWorkArea", "value": true})).unwrap();
|
||||
s.execute("comp.workArea", json!({"start": 2.0, "end": 2.01})).unwrap();
|
||||
s.execute("comp.settings", json!({"frameRate": 24})).unwrap();
|
||||
s.set_time(effectcraft_time::Tick::ZERO);
|
||||
s.execute("shape.newPath", json!({"vertices": [[0, 0], [10, 0], [10, 10]]})).unwrap();
|
||||
let l = &s.active_comp().unwrap().layers[0];
|
||||
let t = s.time();
|
||||
assert!(l.in_point <= t && t < l.out_point, "{t:?} in {:?}..{:?}", l.in_point, l.out_point);
|
||||
assert_eq!(t, effectcraft_time::Tick::from_seconds_f64(2.0), "frame 48 of 24 fps");
|
||||
// The playhead can't go past that last frame.
|
||||
s.set_time(effectcraft_time::Tick::from_seconds_f64(5.0));
|
||||
assert_eq!(s.time(), effectcraft_time::Tick::from_seconds_f64(2.0));
|
||||
}
|
||||
|
||||
/// Synchronize Time of All Related Items reaches a nested comp's final partial frame as a direct
|
||||
/// move does: the inner comp is 2.01 s (set at 100 fps, then 24 fps), the outer sits at 2.0 s.
|
||||
#[test]
|
||||
fn synced_time_reaches_a_nested_final_partial_frame() {
|
||||
let mut s = Session::default();
|
||||
let inner = s.execute("comp.new", json!({"name": "Inner", "duration": 2.01, "frameRate": 100, "open": false})).unwrap()["comp"].as_u64().unwrap();
|
||||
s.execute("comp.settings", json!({"comp": inner, "frameRate": 24})).unwrap();
|
||||
s.execute("comp.new", json!({"name": "Outer", "duration": 4, "frameRate": 24})).unwrap();
|
||||
s.execute("layer.addItem", json!({"item": inner, "time": 0})).unwrap();
|
||||
s.set_time(effectcraft_time::Tick::from_seconds_f64(2.0));
|
||||
assert_eq!(s.time_of(effectcraft_project::ItemId(inner)), effectcraft_time::Tick::from_seconds_f64(2.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_spatial_interpolation_linear() {
|
||||
let mut s = session_with_comp();
|
||||
@@ -674,6 +795,26 @@ fn context_shortcuts_on_the_same_keys_conflict() {
|
||||
assert!(t.conflicts(b, "F9").iter().any(|c| c.key == "test.paint" && !c.overrides));
|
||||
}
|
||||
|
||||
/// A preset repeating one binding many times lists it once, and conflicts stay bounded.
|
||||
#[test]
|
||||
fn repeated_bindings_are_one_binding() {
|
||||
let mut s = Session::default();
|
||||
s.set_ui_commands(vec![
|
||||
crate::shortcuts::UiCommand { id: "test.a".into(), label: "A".into(), shortcut: Some("F9".into()), scope: Some("Timeline".into()) },
|
||||
crate::shortcuts::UiCommand { id: "test.b".into(), label: "B".into(), shortcut: Some("F9".into()), scope: Some("Timeline".into()) },
|
||||
]);
|
||||
let mut t = s.shortcuts().clone();
|
||||
t.keys.insert("test.a".into(), vec!["F9".into(); 10_000]);
|
||||
assert_eq!(t.bindings().iter().filter(|(_, b)| b.key == "test.a").count(), 1);
|
||||
assert_eq!(t.all_conflicts().iter().filter(|(a, c)| a == "test.a" && c.key == "test.b").count(), 1);
|
||||
// Many commands on one key: the listing stops at its cap.
|
||||
for i in 0..200 {
|
||||
t.bindables.push(crate::shortcuts::Bindable { key: format!("test.many{i}"), ..t.find("test.b").unwrap().clone() });
|
||||
t.keys.insert(format!("test.many{i}"), vec!["F9".into()]);
|
||||
}
|
||||
assert_eq!(t.all_conflicts().len(), crate::shortcuts::MAX_CONFLICTS);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- auto-save / recovery
|
||||
|
||||
/// The names in `d`, sorted.
|
||||
|
||||
@@ -649,6 +649,31 @@ fn time_set_keeps_the_frame_shown_in_the_current_time_field() {
|
||||
assert_eq!(r["frame"], 13);
|
||||
}
|
||||
|
||||
/// A comp that starts before 0 shows its first frames as negative timecode (`-0:00:00:12`).
|
||||
/// Typing back what the current-time display shows (Go to Time opens with it) stays on that frame;
|
||||
/// the leading `-` used to move back from the current frame instead. A bare `-n` still steps back,
|
||||
/// and in a comp that starts at 0 a `-` stays relative.
|
||||
#[test]
|
||||
fn time_set_round_trips_negative_displayed_timecode() {
|
||||
let mut s = Session::default();
|
||||
s.execute("comp.new", json!({"name": "Pre-roll", "width": 64, "height": 64, "frameRate": 24, "duration": 4, "startTime": -1})).unwrap();
|
||||
for frame in [0_i64, 1, 12, 23, 24, 25, 60] {
|
||||
let r = s.execute("time.set", json!({"frame": frame})).unwrap();
|
||||
let display = r["display"].as_str().unwrap().to_string();
|
||||
let r = s.execute("time.set", json!({"timecode": display})).unwrap();
|
||||
assert_eq!(r["frame"], frame, "displayed {display}");
|
||||
}
|
||||
s.execute("time.set", json!({"frame": 12})).unwrap();
|
||||
assert_eq!(s.execute("time.set", json!({"timecode": "-0:00:00:12"})).unwrap()["display"], "-0:00:00:12");
|
||||
assert_eq!(s.execute("time.set", json!({"timecode": "-5"})).unwrap()["frame"], 7, "a bare -n steps back");
|
||||
assert_eq!(s.execute("time.set", json!({"timecode": "-0:00:00:20"})).unwrap()["frame"], 4);
|
||||
assert!(s.execute("time.set", json!({"timecode": "- -1:00"})).is_err(), "a second sign is still an error");
|
||||
|
||||
s.execute("comp.settings", json!({"startTime": 0})).unwrap();
|
||||
s.execute("time.set", json!({"frame": 12})).unwrap();
|
||||
assert_eq!(s.execute("time.set", json!({"timecode": "-0:00:00:02"})).unwrap()["frame"], 10, "relative when the comp starts at 0");
|
||||
}
|
||||
|
||||
/// Frames display style: typed digits are a frame count, so 00100 is frame 100, not 1 s (#530).
|
||||
#[test]
|
||||
fn time_set_reads_frames_style_digits_as_a_frame_count() {
|
||||
|
||||
@@ -383,3 +383,58 @@ fn psd_with_damaged_layer_section_imports_the_flattened_image() {
|
||||
let ItemKind::Footage(f) = &s.project.item(item).unwrap().kind else { panic!() };
|
||||
assert!(f.layer.is_none(), "the merged image, not a Photoshop layer");
|
||||
}
|
||||
|
||||
/// A vector footage thumbnail is drawn at the thumbnail's size, never at its declared size.
|
||||
#[test]
|
||||
fn vector_footage_thumbnails_are_drawn_at_thumbnail_size() {
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use effectcraft_project::{Footage, ItemId};
|
||||
use effectcraft_raster::Image;
|
||||
use effectcraft_time::Tick;
|
||||
|
||||
struct Vector(Mutex<Vec<f64>>);
|
||||
impl crate::render::FootageSource for Vector {
|
||||
fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option<Arc<Image>> {
|
||||
panic!("the full-size frame was decoded");
|
||||
}
|
||||
fn vector_frame(&self, _: ItemId, f: &Footage, scale: f64) -> Option<Arc<Image>> {
|
||||
self.0.lock().unwrap().push(scale);
|
||||
Some(Arc::new(Image::new((f.width as f64 * scale).ceil() as u32, (f.height as f64 * scale).ceil() as u32)))
|
||||
}
|
||||
}
|
||||
let source = Arc::new(Vector(Mutex::new(vec![])));
|
||||
let mut s = Session { footage: source.clone(), ..Default::default() };
|
||||
let f = Footage { path: "/m/huge.svg".into(), codec: "SVG".into(), width: 16_000, height: 8_000, has_video: true, ..Default::default() };
|
||||
let item = std::sync::Arc::make_mut(&mut s.project).add_item("huge.svg", effectcraft_color::Label::Sandstone, None, ItemKind::Footage(f));
|
||||
let (w, h, _) = (s.thumbnail_job(item, Tick::ZERO, 64).unwrap())().unwrap();
|
||||
assert_eq!((w, h), (64, 32));
|
||||
assert_eq!(*source.0.lock().unwrap(), [64.0 / 16_000.0]);
|
||||
}
|
||||
|
||||
/// A vector footage thumbnail whose drawn size is a pixel over the requested one is shrunk by
|
||||
/// that pixel, not halved by an integer step.
|
||||
#[test]
|
||||
fn a_vector_thumbnail_one_pixel_over_is_not_halved() {
|
||||
use std::sync::Arc;
|
||||
|
||||
use effectcraft_project::{Footage, ItemId};
|
||||
use effectcraft_raster::Image;
|
||||
use effectcraft_time::Tick;
|
||||
|
||||
struct Over;
|
||||
impl crate::render::FootageSource for Over {
|
||||
fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option<Arc<Image>> {
|
||||
None
|
||||
}
|
||||
fn vector_frame(&self, _: ItemId, _: &Footage, _: f64) -> Option<Arc<Image>> {
|
||||
Some(Arc::new(Image::new(64, 64)))
|
||||
}
|
||||
}
|
||||
let mut s = Session { footage: Arc::new(Over), ..Default::default() };
|
||||
let f = Footage { path: "/m/sq.svg".into(), codec: "SVG".into(), width: 121, height: 121, has_video: true, ..Default::default() };
|
||||
let item = std::sync::Arc::make_mut(&mut s.project).add_item("sq.svg", effectcraft_color::Label::Sandstone, None, ItemKind::Footage(f));
|
||||
let (w, h, px) = (s.thumbnail_job(item, Tick::ZERO, 63).unwrap())().unwrap();
|
||||
assert_eq!((w, h), (63, 63));
|
||||
assert_eq!(px.len(), 63 * 63 * 4);
|
||||
}
|
||||
@@ -31,6 +31,7 @@ thiserror = { workspace = true }
|
||||
log = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = { workspace = true }
|
||||
effectcraft-media = { workspace = true }
|
||||
effectcraft-effects = { workspace = true }
|
||||
effectcraft-color = { workspace = true }
|
||||
|
||||
@@ -122,6 +122,11 @@ pub struct JobOptions<'a> {
|
||||
/// Settings ▸ Memory & CPU ▸ GPU Frames Rendered at Once: 1 to 3, or `None` for Auto (from
|
||||
/// the device memory a frame measured, #3335). `EFFECTCRAFT_GPU_INFLIGHT` overrides it.
|
||||
pub gpu_frames: Option<usize>,
|
||||
/// Reads the bytes of memory the system can hand out now (`None`: unknown), for Auto's CPU
|
||||
/// frames in flight (#1615): read at most once per job, when a job renders on the CPU after
|
||||
/// a GPU frame measured a frame's working memory. `None`: CPU frames are capped only by
|
||||
/// `EFFECTCRAFT_CPU_INFLIGHT`.
|
||||
pub free_memory: Option<&'a (dyn Fn() -> Option<u64> + Sync)>,
|
||||
}
|
||||
|
||||
/// The layer caches of a Render Queue run (see [`JobOptions::caches`]): one per compositor,
|
||||
|
||||
@@ -137,6 +137,17 @@ pub(crate) fn cpu_inflight(var: Option<&str>) -> Option<usize> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Auto's CPU frames in flight (#1615): as many frames as half of the `free` bytes the system
|
||||
/// can hand out hold at `frame` bytes each, 1 to `threads`. `frame` is a GPU frame's measured
|
||||
/// working memory: both compositors keep 16-byte RGBA pixels (`[f32; 4]`, `Rgba32Float`), and
|
||||
/// the other half is headroom for the layer caches, decoded footage, the encoder and the
|
||||
/// estimate being low. `None` (not capped) when either is unknown or `frame` is 0.
|
||||
pub(crate) fn auto_cpu_frames(frame: Option<u64>, free: Option<u64>, threads: usize) -> Option<usize> {
|
||||
let (frame, free) = (frame.filter(|&f| f > 0)?, free?);
|
||||
let n = free / 2 / frame;
|
||||
Some(usize::try_from(n).unwrap_or(usize::MAX).clamp(1, threads.max(1)))
|
||||
}
|
||||
|
||||
/// The frames-in-flight caps of a batch: `gpu` (#879) for the GPU's memory, `cpu` (#1615) for
|
||||
/// the process's.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
@@ -253,6 +264,9 @@ pub(crate) struct Cx<'a> {
|
||||
gpu_frames: Mutex<Option<(usize, String)>>,
|
||||
/// A frame has rendered on the GPU compositor: its memory can be measured.
|
||||
gpu_rendered: std::sync::atomic::AtomicBool,
|
||||
/// Auto's CPU frames in flight once settled ([`Cx::auto_cpu_frames`]; `Some(None)`: not
|
||||
/// capped).
|
||||
cpu_frames: Mutex<Option<Option<usize>>>,
|
||||
/// (output frame, seconds) of every rendered frame (Plus Per Frame Info logs).
|
||||
pub frame_times: Option<Mutex<Vec<(u64, f64)>>>,
|
||||
/// Files written to an overflow folder.
|
||||
@@ -337,6 +351,7 @@ impl<'a> Cx<'a> {
|
||||
choice: Mutex::new(None),
|
||||
gpu_frames: Mutex::new(None),
|
||||
gpu_rendered: Default::default(),
|
||||
cpu_frames: Mutex::new(None),
|
||||
frame_times,
|
||||
overflowed: Mutex::new(vec![]),
|
||||
written: Mutex::new(vec![]),
|
||||
@@ -555,9 +570,11 @@ impl<'a> Cx<'a> {
|
||||
let timed = |on: crate::JobRenderer, frames: &[u64]| {
|
||||
let warm = render(first, on);
|
||||
// The GPU's frames in flight settle with its warm-up frame (Auto measures it).
|
||||
// The CPU's frames are held to what the job would render at once on it, from the
|
||||
// GPU frames just measured (#1615): uncapped, they could exhaust RAM here already.
|
||||
let caps = match on {
|
||||
crate::JobRenderer::Gpu => Inflight { gpu: Some(self.gpu_inflight()), cpu: cpu_cap },
|
||||
crate::JobRenderer::Cpu => Inflight { gpu: None, cpu: cpu_cap },
|
||||
crate::JobRenderer::Cpu => Inflight { gpu: None, cpu: cpu_cap.or_else(|| self.auto_cpu_frames()) },
|
||||
};
|
||||
let t0 = Instant::now();
|
||||
let gpu_live = || on == crate::JobRenderer::Cpu || self.job.accel.is_some_and(|a| a.available());
|
||||
@@ -660,7 +677,48 @@ impl<'a> Cx<'a> {
|
||||
|
||||
/// The GPU and CPU frames-in-flight caps every frame loop shares (#1615).
|
||||
fn inflight(&self) -> Inflight {
|
||||
Inflight { gpu: self.gpu_cap(), cpu: cpu_inflight(std::env::var(CPU_INFLIGHT_ENV).ok().as_deref()) }
|
||||
Inflight { gpu: self.gpu_cap(), cpu: self.cpu_cap() }
|
||||
}
|
||||
|
||||
/// CPU frames in flight (#1615): `EFFECTCRAFT_CPU_INFLIGHT`, else Auto's
|
||||
/// ([`Cx::auto_cpu_frames`]) while the job renders on the CPU (it chose the CPU's
|
||||
/// compositor, or the GPU stopped for good). `None`: not capped.
|
||||
fn cpu_cap(&self) -> Option<usize> {
|
||||
if let Some(n) = cpu_inflight(std::env::var(CPU_INFLIGHT_ENV).ok().as_deref()) {
|
||||
return Some(n);
|
||||
}
|
||||
let on_cpu = self.backend() == crate::JobRenderer::Cpu || self.job.accel.is_some_and(|a| !a.available());
|
||||
if on_cpu { self.auto_cpu_frames() } else { None }
|
||||
}
|
||||
|
||||
/// Auto's CPU frames in flight ([`auto_cpu_frames`]), settled once per job from the working
|
||||
/// memory a GPU frame measured and the memory the system has free
|
||||
/// ([`crate::JobOptions::free_memory`]). `None` (not capped, as before #1615) until a GPU
|
||||
/// frame has rendered, and in a job that never renders one (a Software Rendering project):
|
||||
/// there is no per-frame estimate then.
|
||||
fn auto_cpu_frames(&self) -> Option<usize> {
|
||||
if let Some(n) = *self.cpu_frames.lock().unwrap_or_else(std::sync::PoisonError::into_inner) {
|
||||
return n;
|
||||
}
|
||||
if !self.gpu_rendered.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
return None;
|
||||
}
|
||||
// Read outside the lock: the allocator report walks every allocation, and reading the
|
||||
// system's memory can take a second (PowerShell on Windows).
|
||||
let frame = self.job.accel.and_then(|a| a.frame_memory()).map(|m| m.frame);
|
||||
let free = self.job.options.free_memory.and_then(|read| read());
|
||||
let n = auto_cpu_frames(frame, free, rayon::current_num_threads());
|
||||
let mut slot = self.cpu_frames.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
*slot.get_or_insert_with(|| {
|
||||
let mib = |b: Option<u64>| b.map_or_else(|| "unknown".to_string(), |b| format!("{} MiB", b >> 20));
|
||||
let (frame, free) = (mib(frame), mib(free));
|
||||
let label = &self.job.options.label;
|
||||
match n {
|
||||
Some(n) => log::info!(target: "effectcraft_export", "{label}: {n} CPU frame(s) at once (Auto: {frame} per frame on the GPU, {free} free)"),
|
||||
None => log::info!(target: "effectcraft_export", "{label}: CPU frames not capped (Auto: {frame} per frame on the GPU, {free} free)"),
|
||||
}
|
||||
n
|
||||
})
|
||||
}
|
||||
|
||||
/// Output frame `i`: sampled (fields, pulldown), at the colour depth, cropped and resized.
|
||||
@@ -1285,6 +1343,75 @@ mod tests {
|
||||
assert_eq!(describe_choice(&cx.choice().unwrap()), "Software Rendering (measured on the first frames: GPU 117.0 ms/frame, CPU 68.0 ms/frame)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_cpu_frames_fit_half_the_free_memory() {
|
||||
// #1615: 16 threads at 1378 MiB a frame (a GTX 1050 Ti user's comp) with 10 GiB free
|
||||
// render 3 frames at once, not 16.
|
||||
assert_eq!(auto_cpu_frames(Some(1378 << 20), Some(10 << 30), 16), Some(3));
|
||||
assert_eq!(auto_cpu_frames(Some(1 << 28), Some(64 << 30), 16), Some(16), "at most one per thread");
|
||||
assert_eq!(auto_cpu_frames(Some(4 << 30), Some(2 << 30), 16), Some(1), "at least one");
|
||||
assert_eq!(auto_cpu_frames(Some(1 << 30), Some(0), 8), Some(1));
|
||||
assert_eq!(auto_cpu_frames(Some(1 << 20), Some(u64::MAX), 0), Some(1));
|
||||
// Unknown: not capped, as before.
|
||||
assert_eq!(auto_cpu_frames(None, Some(8 << 30), 16), None);
|
||||
assert_eq!(auto_cpu_frames(Some(1 << 30), None, 16), None);
|
||||
assert_eq!(auto_cpu_frames(Some(0), Some(8 << 30), 16), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_job_that_chose_the_cpu_after_measuring_the_gpu_caps_its_frames() {
|
||||
// #1615: a GPU project that measured the CPU faster rendered one frame per thread at
|
||||
// once, so a heavy comp ran out of RAM (an exit without an error, 0.23.1 on Windows).
|
||||
// Its frames now fit half the free memory at the GPU's measured size per frame.
|
||||
use effectcraft_project::render_queue::{OutputFormat, OutputModule, RenderSettings};
|
||||
if std::env::var_os(CPU_INFLIGHT_ENV).is_some() || std::env::var_os(RENDERER_ENV).is_some() {
|
||||
return;
|
||||
}
|
||||
let (settings, output) = (RenderSettings::default(), OutputModule::for_format(OutputFormat::PngSequence));
|
||||
let mut p = Project::default();
|
||||
p.settings.gpu_acceleration = true;
|
||||
let comp = Comp::new(16, 8, effectcraft_time::FrameRate::FPS_25, Tick::from_seconds_f64(1.0));
|
||||
let cid = p.add_item("Comp", effectcraft_color::Label::Sandstone, None, effectcraft_project::ItemKind::Comp(comp.into()));
|
||||
let accel = MemAccel(effectcraft_render::FrameMemory { budget: 3 << 30, peak: 2 << 30, frame: 1 << 30, reserve: 0 });
|
||||
let reads = AtomicUsize::new(0);
|
||||
let free = || {
|
||||
reads.fetch_add(1, Ordering::SeqCst);
|
||||
Some(6u64 << 30)
|
||||
};
|
||||
let job = Job {
|
||||
project: &p,
|
||||
footage: &effectcraft_render::NoFootage,
|
||||
expr: None,
|
||||
accel: Some(&accel),
|
||||
comp: cid,
|
||||
settings: &settings,
|
||||
output: &output,
|
||||
path: "/c/f_[#####].png",
|
||||
sink: None,
|
||||
nested_switches: true,
|
||||
options: crate::JobOptions { free_memory: Some(&free), ..Default::default() },
|
||||
};
|
||||
let pool = rayon::ThreadPoolBuilder::new().num_threads(16).build().unwrap();
|
||||
pool.install(|| {
|
||||
let cx = Cx::new(&job);
|
||||
assert_eq!(cx.inflight().cpu, None, "nothing measured yet");
|
||||
let c = cx.comp().unwrap();
|
||||
cx.frame_on(c, 0, crate::JobRenderer::Gpu);
|
||||
assert_eq!(cx.inflight().cpu, None, "the GPU renders: its own cap holds");
|
||||
assert_eq!(reads.load(Ordering::SeqCst), 0, "the free memory is read only for CPU frames");
|
||||
cx.decide(Choice { renderer: crate::JobRenderer::Cpu, gpu_ms: Some(1417.3), cpu_ms: Some(655.8), why: "measured on the first frames" });
|
||||
assert_eq!(cx.inflight(), Inflight { gpu: None, cpu: Some(3) }, "3 GiB of 6 GiB free at 1 GiB a frame");
|
||||
assert_eq!(cx.inflight().cpu, Some(3));
|
||||
assert_eq!(reads.load(Ordering::SeqCst), 1, "settled once per job");
|
||||
});
|
||||
// Without a free-memory reading the job renders uncapped, as before.
|
||||
let job = Job { options: Default::default(), ..job };
|
||||
let cx = Cx::new(&job);
|
||||
cx.frame_on(cx.comp().unwrap(), 0, crate::JobRenderer::Gpu);
|
||||
cx.decide(Choice { renderer: crate::JobRenderer::Cpu, gpu_ms: Some(2.0), cpu_ms: Some(1.0), why: "measured on the first frames" });
|
||||
assert_eq!(cx.inflight().cpu, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cpu_inflight_is_off_unless_a_positive_number() {
|
||||
assert_eq!(cpu_inflight(None), None);
|
||||
|
||||
@@ -244,12 +244,14 @@ pub(crate) async fn webm(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut Sta
|
||||
let pad = (fs - pcm_left.len() % fs) % fs + fs;
|
||||
pcm_left.extend(std::iter::repeat_n(0.0, pad));
|
||||
}
|
||||
while pcm_left.len() >= fs {
|
||||
let pkt = enc.encode_float(&pcm_left[..fs]);
|
||||
pcm_left.drain(..fs);
|
||||
// Packets are read in place and the encoded samples dropped once: draining each packet
|
||||
// from the front would shift the rest of the batch's audio every 20 ms.
|
||||
for packet in pcm_left.chunks_exact(fs) {
|
||||
let pkt = enc.encode_float(packet);
|
||||
frames.push(Frame { ms: packets as i64 * 20, track: 2, key: true, prev_ms: None, data: pkt, alpha: None });
|
||||
packets += 1;
|
||||
}
|
||||
pcm_left.drain(..pcm_left.len() / fs * fs);
|
||||
};
|
||||
|
||||
let batch = batch_size();
|
||||
|
||||
@@ -125,12 +125,14 @@ pub(crate) async fn webm_av1(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut
|
||||
let pad = (fs - pcm_left.len() % fs) % fs + fs;
|
||||
pcm_left.extend(std::iter::repeat_n(0.0, pad));
|
||||
}
|
||||
while pcm_left.len() >= fs {
|
||||
let pkt = enc.encode_float(&pcm_left[..fs]);
|
||||
pcm_left.drain(..fs);
|
||||
// Packets are read in place and the encoded samples dropped once: draining each packet
|
||||
// from the front would shift the rest of the batch's audio every 20 ms.
|
||||
for packet in pcm_left.chunks_exact(fs) {
|
||||
let pkt = enc.encode_float(packet);
|
||||
blocks.push(Block { ms: packets as i64 * 20, track: 2, key: true, data: pkt });
|
||||
packets += 1;
|
||||
}
|
||||
pcm_left.drain(..pcm_left.len() / fs * fs);
|
||||
};
|
||||
|
||||
let batch = batch_size();
|
||||
|
||||
@@ -342,7 +342,8 @@ fn ffmpeg_oracle_decodes_ogg_opus() {
|
||||
}
|
||||
}
|
||||
let (enc, packets) = encode(&pcm, channels, rate);
|
||||
let path = std::env::temp_dir().join(format!("effectcraft-opusenc-{}-{channels}.opus", std::process::id()));
|
||||
let guard = tempfile::tempdir().unwrap();
|
||||
let path = guard.path().join("out.opus");
|
||||
std::fs::write(&path, ogg_opus(&enc, &packets, len)).unwrap();
|
||||
let out = Command::new("ffmpeg")
|
||||
.args(["-v", "error", "-i"])
|
||||
|
||||
@@ -389,8 +389,8 @@ fn wav_and_aiff_channels_and_formats() {
|
||||
#[test]
|
||||
fn cancelled_export_removes_only_files_it_opened() {
|
||||
use effectcraft_export::ExportError;
|
||||
let dir = std::env::temp_dir().join(format!("ec-cancel-owned-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let guard = tempfile::tempdir().unwrap();
|
||||
let dir = guard.path().to_path_buf();
|
||||
let (p, cid) = project(FrameRate::new(25, 1), |_, _| {});
|
||||
struct Overflow;
|
||||
impl StorageQuota for Overflow {
|
||||
|
||||
@@ -130,8 +130,8 @@ fn ffmpeg_decodes_our_stream() {
|
||||
ivf.extend_from_slice(&(frame.len() as u32).to_le_bytes());
|
||||
ivf.extend_from_slice(&0u64.to_le_bytes());
|
||||
ivf.extend_from_slice(&frame);
|
||||
let dir = std::env::temp_dir().join(format!("effectcraft-vp9-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let guard = tempfile::tempdir().unwrap();
|
||||
let dir = guard.path().to_path_buf();
|
||||
let path = dir.join("a.ivf");
|
||||
std::fs::write(&path, &ivf).unwrap();
|
||||
let out = std::process::Command::new("ffmpeg")
|
||||
@@ -291,8 +291,8 @@ fn ffmpeg_matches(w: usize, h: usize, frames: &[Vec<u8>], recon: &[[Vec<u8>; 3]]
|
||||
ivf.extend_from_slice(&(t as u64).to_le_bytes());
|
||||
ivf.extend_from_slice(f);
|
||||
}
|
||||
let dir = std::env::temp_dir().join(format!("effectcraft-vp9-{name}-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let guard = tempfile::Builder::new().prefix(&format!("effectcraft-vp9-{name}-")).tempdir().unwrap();
|
||||
let dir = guard.path().to_path_buf();
|
||||
let path = dir.join("a.ivf");
|
||||
std::fs::write(&path, &ivf).unwrap();
|
||||
let out = std::process::Command::new("ffmpeg")
|
||||
|
||||
@@ -234,7 +234,7 @@ function __cubicBezier(curve) {
|
||||
x1 = curve[0]; y1 = curve[1]; x2 = curve[2]; y2 = curve[3];
|
||||
}
|
||||
if (typeof x1 !== 'number' || typeof y1 !== 'number' || typeof x2 !== 'number' || typeof y2 !== 'number' ||
|
||||
!isFinite(x1) || !isFinite(y1) || !isFinite(x2) || !isFinite(y2)) {
|
||||
!Number.isFinite(x1) || !Number.isFinite(y1) || !Number.isFinite(x2) || !Number.isFinite(y2)) {
|
||||
return __lin;
|
||||
}
|
||||
x1 = Math.max(0, Math.min(1, x1));
|
||||
@@ -418,8 +418,8 @@ function hslToRgb(c) {
|
||||
function hexToRgb(hex) {
|
||||
var s = String(hex).replace(/^#|^0x/i, '');
|
||||
if (s.length === 3) s = s[0] + s[0] + s[1] + s[1] + s[2] + s[2];
|
||||
var n = parseInt(s.slice(0, 6), 16);
|
||||
var a = s.length >= 8 ? parseInt(s.slice(6, 8), 16) / 255 : 1;
|
||||
var n = Number.parseInt(s.slice(0, 6), 16);
|
||||
var a = s.length >= 8 ? Number.parseInt(s.slice(6, 8), 16) / 255 : 1;
|
||||
return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255, a];
|
||||
}
|
||||
|
||||
@@ -502,7 +502,7 @@ function __child(c, l, group, key) {
|
||||
// it (default 1), the layer at the top (#1619).
|
||||
function __ancestor(c, l, path, countUp) {
|
||||
var n = countUp === undefined ? 1 : __v(countUp);
|
||||
if (typeof n !== 'number' || !(n >= 1) || !isFinite(n)) __err('propertyGroup(' + countUp + '): the count must be a number of 1 or more');
|
||||
if (typeof n !== 'number' || !(n >= 1) || !Number.isFinite(n)) __err('propertyGroup(' + countUp + '): the count must be a number of 1 or more');
|
||||
n = Math.floor(n);
|
||||
var info = __h(__ANCESTOR, c, l, path, n);
|
||||
if (info === 'layer') return __layer(c, l);
|
||||
|
||||
@@ -274,7 +274,9 @@ pub(crate) fn box_passes(e: &mut Enc, img: &GpuImage, rx: &[usize], ry: &[usize]
|
||||
let mut cur = img.clone();
|
||||
let passes = rx.iter().map(|&r| (false, r)).chain(ry.iter().map(|&r| (true, r))).filter(|p| p.1 > 0);
|
||||
for (vertical, r) in passes {
|
||||
let block = if vertical { (4 * r).clamp(32, 256) } else { 256 } as u32;
|
||||
// Vertical sums restart every `block` rows as the CPU's do; a row is one running sum, as
|
||||
// on the CPU (restarting it rounds differently).
|
||||
let block = if vertical { (4 * r).clamp(32, 256) as u32 } else { cur.width.max(1) };
|
||||
let (lines, n) = if vertical { (cur.width, cur.height) } else { (cur.height, cur.width) };
|
||||
let mut p = Params::default();
|
||||
p.u[0] = [r as u32, repeat as u32, block, 0];
|
||||
|
||||
@@ -133,7 +133,7 @@ fn radial_fast(e: &mut Enc, ctx: &EffectCtx, mut b: GBuf) -> Option<GBuf> {
|
||||
}
|
||||
|
||||
fn camera_lens(e: &mut Enc, ctx: &EffectCtx, mut b: GBuf) -> Option<GBuf> {
|
||||
let r = ctx.params.f("blurRadius").max(0.0) * b.scale;
|
||||
let r = effectcraft_effects::camera_lens_radius(ctx, b.scale);
|
||||
if r < 0.5 {
|
||||
return Some(b);
|
||||
}
|
||||
|
||||
@@ -202,6 +202,8 @@ pub(crate) fn fractal(e: &mut Enc, ctx: &EffectCtx, b: GBuf, turbulent: bool) ->
|
||||
|
||||
/// noise::median_image (of the straight colour with alpha 1 when `straight`).
|
||||
pub(crate) fn median_image(e: &mut Enc, img: &GpuImage, r: usize, straight: bool) -> GpuImage {
|
||||
// As on the CPU: the window's radius is capped, so its first fill stays bounded.
|
||||
let r = r.min(effectcraft_effects::MEDIAN_MAX_RADIUS);
|
||||
let q = run(e, "fxn_quant", &with_u0([straight as u32, 0, 0, 0]), img, None, None);
|
||||
// Preserve the CPU's rounded level / 511 values before the strict Dust & Scratches
|
||||
// threshold. GPU division (even residual-corrected) may differ by one ulp.
|
||||
|
||||
@@ -270,7 +270,7 @@ fn line_get(horizontal: bool, line: i32, i: i32, n: i32, repeat: bool) -> vec4<f
|
||||
}
|
||||
|
||||
// One box pass of radius r along rows (horizontal) or columns, as running sums over blocks of
|
||||
// `block` pixels (each block restarts its sum, as the CPU's vertical pass does).
|
||||
// `block` pixels (each block restarts its sum, as the CPU's vertical pass does; rows are one block).
|
||||
fn box_pass(gid: vec3<u32>, horizontal: bool) {
|
||||
let dims = out_dims();
|
||||
let r = i32(P.u[0].x);
|
||||
|
||||
@@ -732,6 +732,35 @@ fn small_blurs_fall_back_to_the_cpu() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Horizontal GPU box passes used to restart the running sum every 256 px; the CPU keeps one sum
|
||||
/// per row, so images wider than 256 disagreed in f32 rounding. Width 320 forces that path.
|
||||
/// Exact on llvmpipe (CI lavapipe), which fails without the fix; other adapters use the usual
|
||||
/// Bpc32 tolerance because a tighter bound can fail on unmeasured hardware and does not catch
|
||||
/// the regression (Metal still differs by ~1e-6 with or without the fix).
|
||||
#[test]
|
||||
fn box_blur_horizontal_rows_match_cpu_beyond_256() {
|
||||
let Some(g) = gpu() else { return };
|
||||
let img = pattern(11, 320, 48);
|
||||
let up = g.ctx.upload_image(&img).expect("upload");
|
||||
let tol = if on_llvmpipe() { 0.0 } else { tolerance(BitDepth::Bpc32) };
|
||||
let check_case = |name: &str, want: Image, got: Image| {
|
||||
assert_eq!((got.width, got.height), (want.width, want.height), "{name}: size");
|
||||
let d = diff(&want, &got, tol);
|
||||
assert_eq!(d.over, 0, "{name}: max {} at {:?} on {}", d.max, d.worst, g.ctx.name);
|
||||
};
|
||||
for (name, r) in [("box r5×3", 5usize), ("box r25×3", 25)] {
|
||||
let mut e = crate::context::Enc::new(&g.ctx);
|
||||
let rx = [r, r, r];
|
||||
let out = crate::effects::box_passes(&mut e, &up, &rx, &rx, false);
|
||||
check_case(name, effectcraft_raster::box_blur(&img, r, r, 3, false), e.download(&out).expect("download"));
|
||||
}
|
||||
for (name, sigma) in [("gauss σ10", 10.0f64), ("gauss σ40", 40.0)] {
|
||||
let mut e = crate::context::Enc::new(&g.ctx);
|
||||
let out = crate::effects::gaussian_blur(&mut e, &up, sigma, sigma, false);
|
||||
check_case(name, effectcraft_raster::gaussian_blur(&img, sigma, sigma, false), e.download(&out).expect("download"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gpu_effects_match_cpu() {
|
||||
// Sigma 3 px and up and even radii, so the half-scale pass stays on the GPU too (#1939).
|
||||
|
||||
@@ -65,6 +65,7 @@ impl Exporter for FileExporter {
|
||||
caches: job.caches.map(|c| effectcraft_export::JobCaches { gpu: &c.gpu, cpu: &c.cpu }),
|
||||
renderer: job.renderer,
|
||||
gpu_frames: job.gpu_frames,
|
||||
free_memory: Some(&free_memory),
|
||||
},
|
||||
nested_switches: job.nested_switches,
|
||||
};
|
||||
@@ -87,6 +88,11 @@ impl Exporter for FileExporter {
|
||||
}
|
||||
}
|
||||
|
||||
/// The memory the system can hand out now, for an export's Auto CPU frames in flight (#1615).
|
||||
fn free_memory() -> Option<u64> {
|
||||
effectcraft_engine::sysinfo::memory().map(|m| m.available)
|
||||
}
|
||||
|
||||
/// A new session with media, import, expressions, scripting and export enabled.
|
||||
pub fn session() -> Session {
|
||||
let mut s = Session {
|
||||
|
||||
@@ -547,6 +547,7 @@ fn script_imports_footage_and_saves() {
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let q = |n: &str| serde_json::to_string(&dir.join(n).to_string_lossy()).unwrap();
|
||||
let mut s = super::session();
|
||||
s.execute("prefs.set", json!({"key": "scripting.allowScriptsWriteFiles", "value": true})).unwrap();
|
||||
let code = format!(
|
||||
r#"
|
||||
var c = app.project.items.addComp("Src", 32, 32, 1, 1, 10);
|
||||
|
||||
@@ -276,8 +276,8 @@ pub(super) fn wat_plugin(id: &str, api: i32, render: &str) -> String {
|
||||
|
||||
#[test]
|
||||
fn plugin_loading_reports_bad_modules_and_survives_failing_renders() {
|
||||
let dir = std::env::temp_dir().join(format!("effectcraft-host-plugins-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let guard = tempfile::tempdir().unwrap();
|
||||
let dir = guard.path().to_path_buf();
|
||||
let files = [
|
||||
("a-good.wasm", wat_plugin("org.test.host.good", 1, "i32.const 0").into_bytes()),
|
||||
("b-garbage.wasm", b"\0asm\x01\0\0\0garbage".to_vec()),
|
||||
|
||||
@@ -303,16 +303,41 @@ impl Exporter<'_> {
|
||||
}
|
||||
|
||||
/// The bin mirroring a Project panel folder.
|
||||
///
|
||||
/// Walks up the folder's parents without recursion: a damaged or hostile project can hold a
|
||||
/// parent cycle (or a very long chain), which recursed until the stack ran out. A folder seen
|
||||
/// twice in one walk ends it, and the cycle's topmost folder becomes a top-level bin.
|
||||
fn bin(&mut self, folder: Option<ItemId>) -> Option<fp::BinId> {
|
||||
let f = folder?;
|
||||
if let Some(b) = self.bins.get(&f) {
|
||||
return Some(*b);
|
||||
// The uncached folders from `f` up, and the bin they hang from.
|
||||
let mut chain: Vec<ItemId> = vec![];
|
||||
let mut in_chain = std::collections::HashSet::new();
|
||||
let mut parent = None;
|
||||
let mut at = Some(f);
|
||||
while let Some(id) = at {
|
||||
if let Some(b) = self.bins.get(&id) {
|
||||
parent = Some(*b);
|
||||
break;
|
||||
}
|
||||
if !in_chain.insert(id) {
|
||||
self.warn("a folder that contains itself was exported as a top-level bin");
|
||||
break;
|
||||
}
|
||||
let Some(it) = self.p.item(id) else { break };
|
||||
chain.push(id);
|
||||
at = it.parent;
|
||||
}
|
||||
let it = self.p.item(f)?;
|
||||
let parent = self.bin(it.parent);
|
||||
let b = self.fp.add_bin(&it.name, parent);
|
||||
self.bins.insert(f, b);
|
||||
Some(b)
|
||||
if chain.is_empty() {
|
||||
// `f` is cached already (`parent` is its bin), or is not an item (`None`).
|
||||
return parent;
|
||||
}
|
||||
for id in chain.into_iter().rev() {
|
||||
let name = self.p.item(id).map(|it| it.name.clone()).unwrap_or_default();
|
||||
let b = self.fp.add_bin(&name, parent);
|
||||
self.bins.insert(id, b);
|
||||
parent = Some(b);
|
||||
}
|
||||
parent
|
||||
}
|
||||
|
||||
fn footage_item(&mut self, item: ItemId, f: &Footage) -> fp::ItemId {
|
||||
@@ -615,12 +640,34 @@ impl Exporter<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `c` or a comp nested in it (at any depth) has a layer of footage with audio.
|
||||
///
|
||||
/// A walk with a stack and a visited set, as in `Project::comp_contains`: recursion never ended
|
||||
/// on a precomp cycle, and re-walked a comp shared by many layers once per path (exponential
|
||||
/// for a chain of comps that each use the next one twice).
|
||||
fn comp_has_audio(p: &Project, c: &Comp) -> bool {
|
||||
c.layers.iter().any(|l| match &l.source {
|
||||
LayerSource::Footage { item } => matches!(p.item(*item).map(|i| &i.kind), Some(ItemKind::Footage(f)) if f.has_audio),
|
||||
LayerSource::Comp { item } => p.comp(*item).is_some_and(|c| comp_has_audio(p, c)),
|
||||
_ => false,
|
||||
})
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut stack = vec![c];
|
||||
while let Some(c) = stack.pop() {
|
||||
for l in &c.layers {
|
||||
match &l.source {
|
||||
LayerSource::Footage { item } => {
|
||||
if matches!(p.item(*item).map(|i| &i.kind), Some(ItemKind::Footage(f)) if f.has_audio) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
LayerSource::Comp { item } => {
|
||||
if seen.insert(*item)
|
||||
&& let Some(nested) = p.comp(*item)
|
||||
{
|
||||
stack.push(nested);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn differs(v: &Value, def: [f64; 2]) -> bool {
|
||||
@@ -703,3 +750,37 @@ fn media_info(name: &str, kind: MediaKind, duration: Tick, video: Option<VideoSp
|
||||
fn media_clip(media: fp::MediaRef, info: MediaInfo) -> fp::MediaClip {
|
||||
fp::MediaClip { media, info, interpret: Default::default(), mark_in: None, mark_out: None, markers: vec![], offline: false, proxy: None, identity: None }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A folder's bin is the same one each time it is asked for (a cached folder once answered
|
||||
/// `None`, so a folder's second item went to the project root); a non-item has no bin.
|
||||
#[test]
|
||||
fn folder_bins_are_reused_once_cached() {
|
||||
let mut p = Project::default();
|
||||
let a = p.add_item("A", effectcraft_color::Label::None, None, ItemKind::Folder);
|
||||
let b = p.add_item("B", effectcraft_color::Label::None, Some(a), ItemKind::Folder);
|
||||
let (opts, pre) = (ExportOptions::default(), HashMap::new());
|
||||
let mut ex = Exporter {
|
||||
p: &p,
|
||||
opts: &opts,
|
||||
pre: &pre,
|
||||
fp: fp::Project::new("x"),
|
||||
out: ExportOutput::default(),
|
||||
seqs: HashMap::new(),
|
||||
media: HashMap::new(),
|
||||
bins: HashMap::new(),
|
||||
stack: vec![],
|
||||
};
|
||||
let bb = ex.bin(Some(b));
|
||||
assert!(bb.is_some());
|
||||
assert_eq!(ex.bin(Some(b)), bb);
|
||||
let ba = ex.bin(Some(a));
|
||||
assert!(ba.is_some() && ba != bb);
|
||||
assert_eq!(ex.bin(Some(a)), ba);
|
||||
assert_eq!(ex.bin(Some(ItemId(u64::MAX))), None);
|
||||
assert_eq!(ex.bin(None), None);
|
||||
}
|
||||
}
|
||||
@@ -177,10 +177,12 @@ pub fn import(
|
||||
}
|
||||
let folder_name = if opts.name.is_empty() { "Imported Timeline".to_string() } else { opts.name.clone() };
|
||||
res.folder = project.add_item(&folder_name, Label::None, None, ItemKind::Folder);
|
||||
let warned = res.warnings.iter().cloned().collect();
|
||||
let mut im = Importer {
|
||||
src: &imported.project,
|
||||
project,
|
||||
res,
|
||||
warned,
|
||||
items: HashMap::new(),
|
||||
solids: HashMap::new(),
|
||||
solids_folder: None,
|
||||
@@ -273,6 +275,8 @@ struct Importer<'a> {
|
||||
src: &'a fp::Project,
|
||||
project: &'a mut Project,
|
||||
res: ImportResult,
|
||||
/// The warnings in `res`, to say each once without searching them all every time.
|
||||
warned: HashSet<String>,
|
||||
/// Source item → created item (footage, comp).
|
||||
items: HashMap<fp::ItemId, ItemId>,
|
||||
/// Generator / adjustment solids by (source item, width, height).
|
||||
@@ -290,7 +294,7 @@ struct Importer<'a> {
|
||||
impl Importer<'_> {
|
||||
fn warn(&mut self, m: impl Into<String>) {
|
||||
let m = m.into();
|
||||
if !self.res.warnings.contains(&m) {
|
||||
if self.warned.insert(m.clone()) {
|
||||
self.res.warnings.push(m);
|
||||
}
|
||||
}
|
||||
@@ -429,23 +433,8 @@ impl Importer<'_> {
|
||||
comp.markers = seq.markers.iter().map(marker_in).collect();
|
||||
// Video: the top track's clips first (layer #1); later clips of a track above earlier ones
|
||||
// so incoming clips of dissolves cover outgoing ones.
|
||||
for track in seq.video_tracks.iter().rev() {
|
||||
let mut items: Vec<&fp::TrackItem> = track.items.iter().collect();
|
||||
items.sort_by_key(|i| std::cmp::Reverse(i.start));
|
||||
for ti in items {
|
||||
if let Some(l) = self.clip_layer(&comp, seq, track, ti) {
|
||||
comp.layers.push(l);
|
||||
}
|
||||
}
|
||||
}
|
||||
for track in &seq.audio_tracks {
|
||||
let mut items: Vec<&fp::TrackItem> = track.items.iter().collect();
|
||||
items.sort_by_key(|i| std::cmp::Reverse(i.start));
|
||||
for ti in items {
|
||||
if let Some(l) = self.clip_layer(&comp, seq, track, ti) {
|
||||
comp.layers.push(l);
|
||||
}
|
||||
}
|
||||
for track in seq.video_tracks.iter().rev().chain(&seq.audio_tracks) {
|
||||
self.track_layers(&mut comp, seq, track);
|
||||
}
|
||||
if !seq.caption_tracks.is_empty() {
|
||||
self.warn("caption tracks are not imported");
|
||||
@@ -508,7 +497,45 @@ impl Importer<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
fn clip_layer(&mut self, comp: &Comp, seq: &fp::Sequence, track: &fp::Track, ti: &fp::TrackItem) -> Option<Layer> {
|
||||
/// A track's clips as layers of `comp`, the latest first.
|
||||
fn track_layers(&mut self, comp: &mut Comp, seq: &fp::Sequence, track: &fp::Track) {
|
||||
let mut items: Vec<&fp::TrackItem> = track.items.iter().collect();
|
||||
items.sort_by_key(|i| std::cmp::Reverse(i.start));
|
||||
// Each clip's transitions, found once per track: scanning every transition of the track
|
||||
// for every clip made a long flat edit (many clips and many transitions) quadratic.
|
||||
let mut transitions: HashMap<fp::ClipId, Vec<&fp::Transition>> = HashMap::new();
|
||||
for tr in &track.transitions {
|
||||
for clip in [tr.from, tr.to].into_iter().flatten() {
|
||||
let list = transitions.entry(clip).or_default();
|
||||
// A transition from and to the same clip still counts once.
|
||||
if !list.last().is_some_and(|last| std::ptr::eq(*last, tr)) {
|
||||
list.push(tr);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut any = false;
|
||||
for ti in items {
|
||||
let own = transitions.get(&ti.id).map(Vec::as_slice).unwrap_or_default();
|
||||
if let Some(l) = self.clip_layer(comp, seq, track, ti, own) {
|
||||
comp.layers.push(l);
|
||||
any = true;
|
||||
}
|
||||
}
|
||||
// What the track's transitions become, said once a clip of the track was imported.
|
||||
if any && track.kind != fp::TrackKind::Audio {
|
||||
for tr in &track.transitions {
|
||||
let id = tr.effect.effect.as_str();
|
||||
if !matches!(id, "cross_dissolve" | "dip_to_black" | "dip_to_white" | "film_dissolve" | "additive_dissolve" | "non_additive_dissolve") {
|
||||
self.warn(format!("transition \"{}\" was imported as a cross dissolve", tr.effect.def().map(|d| d.name).unwrap_or(id)));
|
||||
}
|
||||
}
|
||||
}
|
||||
if any && track.transitions.iter().any(|tr| tr.effect.effect == "dip_to_white") {
|
||||
self.warn("Dip to White was imported as a dip to the background");
|
||||
}
|
||||
}
|
||||
|
||||
fn clip_layer(&mut self, comp: &Comp, seq: &fp::Sequence, track: &fp::Track, ti: &fp::TrackItem, transitions: &[&fp::Transition]) -> Option<Layer> {
|
||||
let audio_track = track.kind == fp::TrackKind::Audio;
|
||||
let (source, size, has_audio) = self.source_of(ti, comp.width, comp.height)?;
|
||||
let is_adjustment = matches!(self.src.item(ti.item).map(|i| &i.kind), Some(fp::ItemKind::AdjustmentLayer { .. }));
|
||||
@@ -561,18 +588,9 @@ impl Importer<'_> {
|
||||
}
|
||||
// Transitions extend the layer over their handles.
|
||||
let mut fades: Vec<(Tick, Tick, f64, f64)> = vec![];
|
||||
for tr in &track.transitions {
|
||||
for tr in transitions {
|
||||
let (ts, te) = (tick_in(tr.start), tick_in(tr.end()));
|
||||
let id = tr.effect.effect.as_str();
|
||||
let dip = id.starts_with("dip_to");
|
||||
if !audio_track
|
||||
&& !matches!(id, "cross_dissolve" | "dip_to_black" | "dip_to_white" | "film_dissolve" | "additive_dissolve" | "non_additive_dissolve")
|
||||
{
|
||||
self.warn(format!("transition \"{}\" was imported as a cross dissolve", tr.effect.def().map(|d| d.name).unwrap_or(id)));
|
||||
}
|
||||
if dip && id == "dip_to_white" {
|
||||
self.warn("Dip to White was imported as a dip to the background");
|
||||
}
|
||||
let dip = tr.effect.effect.starts_with("dip_to");
|
||||
let mid = ts + Tick((te - ts).0 / 2);
|
||||
if tr.to == Some(ti.id) {
|
||||
l.in_point = l.in_point.min(ts);
|
||||
|
||||
@@ -787,3 +787,98 @@ fn deeply_nested_documents_error_instead_of_overflowing_the_stack() {
|
||||
assert_eq!(super::import::xml_depth(b"<a><!-- <b><b> --><c x='>'/><![CDATA[<d>]]><e></e></a>"), 2);
|
||||
assert_eq!(super::import::json_depth(br#"{"a": "[[[{{", "b": [[1], {"c": "\\\"["}]}"#), 3);
|
||||
}
|
||||
|
||||
/// A hostile project: footage in a folder whose parents loop back to it. Export walked the
|
||||
/// folder's parents recursively and never reached the cache insert, so the stack ran out.
|
||||
#[test]
|
||||
fn export_survives_a_folder_parent_cycle() {
|
||||
let (mut p, cid) = ae_project();
|
||||
let a = p.add_item("A", effectcraft_color::Label::None, None, ItemKind::Folder);
|
||||
let b = p.add_item("B", effectcraft_color::Label::None, Some(a), ItemKind::Folder);
|
||||
p.items.get_mut(&a).unwrap().parent = Some(b);
|
||||
let footage: Vec<ItemId> = p.items.values().filter(|i| matches!(i.kind, ItemKind::Footage(_))).map(|i| i.id).collect();
|
||||
for f in footage {
|
||||
p.items.get_mut(&f).unwrap().parent = Some(a);
|
||||
}
|
||||
let out = export(&p, cid, &ExportOptions::default(), &HashMap::new()).unwrap();
|
||||
assert!(out.warnings.iter().any(|w| w.contains("contains itself")), "{:?}", out.warnings);
|
||||
}
|
||||
|
||||
/// Precomps that use each other, and a chain of comps that each use the next one twice: the
|
||||
/// audio check recursed forever on the first and 2^depth times on the second.
|
||||
#[test]
|
||||
fn export_audio_check_ends_on_precomp_cycles_and_shared_chains() {
|
||||
let mut p = Project::default();
|
||||
let a = footage_item(&mut p, "/media/a.mov");
|
||||
let comp = || Comp::new(W, H, FrameRate::new(25, 1), t(2.0));
|
||||
// A chain of 60 comps, each using the next twice; the last one is silent footage.
|
||||
let mut next = {
|
||||
let mut c = comp();
|
||||
let l = effectcraft_project::build::layer(&mut p, &c, "a", LayerSource::Footage { item: a }, (W, H), None);
|
||||
c.layers.push(l);
|
||||
comp_item(&mut p, "Leaf", c)
|
||||
};
|
||||
for i in 0..60 {
|
||||
let mut c = comp();
|
||||
for n in 0..2 {
|
||||
let l = effectcraft_project::build::layer(&mut p, &c, &format!("use {n}"), LayerSource::Comp { item: next }, (W, H), None);
|
||||
c.layers.push(l);
|
||||
}
|
||||
next = comp_item(&mut p, &format!("Chain {i}"), c);
|
||||
}
|
||||
let started = std::time::Instant::now();
|
||||
export(&p, next, &ExportOptions::default(), &HashMap::new()).unwrap();
|
||||
assert!(started.elapsed() < std::time::Duration::from_secs(30), "{:?}", started.elapsed());
|
||||
// Two comps nesting each other.
|
||||
let x = comp_item(&mut p, "X", comp());
|
||||
let mut y = comp();
|
||||
let l = effectcraft_project::build::layer(&mut p, &y, "x", LayerSource::Comp { item: x }, (W, H), None);
|
||||
y.layers.push(l);
|
||||
let y = comp_item(&mut p, "Y", y);
|
||||
let mut xc = comp();
|
||||
let l = effectcraft_project::build::layer(&mut p, &xc, "y", LayerSource::Comp { item: y }, (W, H), None);
|
||||
xc.layers.push(l);
|
||||
if let Some(ItemKind::Comp(c)) = p.items.get_mut(&x).map(|i| &mut i.kind) {
|
||||
*c = Arc::new(xc);
|
||||
}
|
||||
export(&p, x, &ExportOptions::default(), &HashMap::new()).unwrap();
|
||||
}
|
||||
|
||||
/// A flat edit with many clips and many transitions: each clip finds its own transitions (it
|
||||
/// scanned every transition of the track per clip), and they still extend and fade it.
|
||||
#[test]
|
||||
fn import_finds_each_clips_transitions_without_scanning_the_track() {
|
||||
let mut p = fp::Project::new("Flat");
|
||||
let a = media(&mut p, "a.png", "/media/a.mov", filmcraft_media::MediaKind::Movie, true, false, None);
|
||||
let settings = fp::SequenceSettings { width: W, height: H, frame_rate: rate(), ..Default::default() };
|
||||
let main = p.new_sequence("Main", settings, 1, 0, None);
|
||||
const N: usize = 500;
|
||||
let mut items = Vec::with_capacity(N);
|
||||
let mut transitions = Vec::with_capacity(N);
|
||||
for i in 0..N {
|
||||
let ti = item(&mut p, a, fp::TrackKind::Video, i as f64 * 0.4, 0.0, 0.4);
|
||||
if let Some(prev) = items.last().map(|x: &fp::TrackItem| x.id) {
|
||||
transitions.push(fp::Transition {
|
||||
id: fp::TransitionId(p.alloc_id()),
|
||||
effect: fp::find_effect("cross_dissolve").unwrap().instance(),
|
||||
start: secs(i as f64 * 0.4 - 0.08),
|
||||
duration: secs(0.16),
|
||||
from: Some(prev),
|
||||
to: Some(ti.id),
|
||||
align: fp::TransitionAlign::CenterAtCut,
|
||||
reverse: false,
|
||||
});
|
||||
}
|
||||
items.push(ti);
|
||||
}
|
||||
let seq = p.sequence_mut(main).unwrap();
|
||||
seq.video_tracks[0].items = items;
|
||||
seq.video_tracks[0].transitions = transitions;
|
||||
let (bytes, _) = fc::export(&p, main, fc::Format::Otio, &Default::default()).unwrap();
|
||||
let (q, r) = import_doc(&bytes, "Flat.otio", &mut probe);
|
||||
let m = q.comp(r.comps[0]).unwrap();
|
||||
assert_eq!(m.layers.len(), N);
|
||||
// A middle clip reaches over both of its transitions' handles.
|
||||
let mid = m.layers.iter().find(|l| near(l.in_point, t(100.0 - 0.08))).expect("clip 250 extended into its incoming transition");
|
||||
assert!(near(mid.out_point, t(100.4 + 0.08)), "{}", mid.out_point.seconds());
|
||||
}
|
||||
@@ -165,6 +165,27 @@ fn gradient_tracks_sample_separately() {
|
||||
assert_eq!((empty.color_at(0.3), empty.opacity_at(0.3)), ([1.0; 4], 1.0));
|
||||
}
|
||||
|
||||
/// A gradient's stop count multiplies every painted pixel's sampling: plain JSON with more than
|
||||
/// `MAX_GRADIENT_STOPS` is refused, and a longer stored list is sampled through its first ones.
|
||||
#[test]
|
||||
fn gradient_stop_count_is_bounded() {
|
||||
use crate::value::MAX_GRADIENT_STOPS;
|
||||
let g = Value::Gradient(Gradient::default());
|
||||
let many: Vec<serde_json::Value> = (0..=MAX_GRADIENT_STOPS).map(|_| serde_json::json!("#ff0000")).collect();
|
||||
assert_eq!(g.coerce_json(&serde_json::Value::Array(many.clone())), None);
|
||||
assert!(g.coerce_json(&serde_json::Value::Array(many[..MAX_GRADIENT_STOPS].to_vec())).is_some());
|
||||
let ops: Vec<serde_json::Value> = (0..=MAX_GRADIENT_STOPS).map(|i| serde_json::json!([i as f64 / MAX_GRADIENT_STOPS as f64, 1.0])).collect();
|
||||
assert_eq!(g.coerce_json(&serde_json::json!({"colors": ["#000000", "#ffffff"], "opacities": ops})), None);
|
||||
// The tagged (typed) form is bounded the same way.
|
||||
let tagged = Value::Gradient(Gradient { colors: (0..=MAX_GRADIENT_STOPS).map(|i| (i as f64, [0.0; 4])).collect(), opacities: vec![(0.0, 1.0)] });
|
||||
assert_eq!(g.coerce_json(&tagged.to_json()), None);
|
||||
// A stored gradient with a huge tail: stops past the limit don't take part in sampling.
|
||||
let mut colors: Vec<(f64, [f32; 4])> = (0..MAX_GRADIENT_STOPS).map(|i| (i as f64 / MAX_GRADIENT_STOPS as f64 * 0.5, [0.0, 0.0, 0.0, 1.0])).collect();
|
||||
colors.extend((0..100_000).map(|_| (1.0, [1.0, 1.0, 1.0, 1.0])));
|
||||
let big = Gradient { colors, opacities: vec![(0.0, 1.0)] };
|
||||
assert_eq!(big.sample(1.0), [0.0, 0.0, 0.0, 1.0]);
|
||||
}
|
||||
|
||||
/// #612: the editor saves the typed form, so one colour stop (which the plain JSON parser
|
||||
/// rejects) and each stop's RGBA come back exactly.
|
||||
#[test]
|
||||
|
||||
@@ -977,7 +977,19 @@ impl TextDoc {
|
||||
if r.start >= r.end && n > 0 {
|
||||
return Ok(true);
|
||||
}
|
||||
self.apply_style(r.start..r.end.max(r.start + usize::from(n == 0)), |s| {
|
||||
// Empty text: apply_style ignores the bounds, so a fixed range avoids `start + 1` overflow.
|
||||
let range = if n == 0 { 0..1 } else { r.start..r.end };
|
||||
// Check every affected run first, so a value one run refuses changes none of them.
|
||||
let mut err = None;
|
||||
self.apply_style(range.clone(), |s| {
|
||||
if let Err(e) = apply_char_attr(&mut s.clone(), key, v) {
|
||||
err.get_or_insert(e);
|
||||
}
|
||||
});
|
||||
if let Some(e) = err {
|
||||
return Err(e);
|
||||
}
|
||||
self.apply_style(range, |s| {
|
||||
let _ = apply_char_attr(s, key, v);
|
||||
});
|
||||
return Ok(true);
|
||||
@@ -1057,6 +1069,11 @@ pub const CHAR_ATTRS: &[&str] = &[
|
||||
pub const PARA_ATTRS: &[&str] =
|
||||
&["justify", "indentLeft", "indentRight", "indentFirst", "spaceBefore", "spaceAfter", "direction", "composer", "hangingPunctuation"];
|
||||
|
||||
/// Most variable font axes one style can set. Real fonts have a handful (OpenType registers
|
||||
/// five; custom axes rarely bring the total past twenty); a `variations` object with more is
|
||||
/// rejected before any of it is applied.
|
||||
pub const MAX_VARIATION_AXES: usize = 64;
|
||||
|
||||
/// Set one character attribute; Ok(false) for keys that aren't character attributes.
|
||||
pub fn apply_char_attr(s: &mut CharStyle, key: &str, v: &J) -> Result<bool, String> {
|
||||
match key {
|
||||
@@ -1173,20 +1190,28 @@ pub fn apply_char_attr(s: &mut CharStyle, key: &str, v: &J) -> Result<bool, Stri
|
||||
"variations" => match v {
|
||||
J::Null => s.variations.clear(),
|
||||
J::Object(m) => {
|
||||
if m.is_empty() {
|
||||
s.variations.clear();
|
||||
if m.len() > MAX_VARIATION_AXES {
|
||||
return Err(format!("variations: at most {MAX_VARIATION_AXES} axes"));
|
||||
}
|
||||
// One keyed merge (the result sorted by tag), applied only when every entry is valid.
|
||||
let mut axes: std::collections::BTreeMap<String, f32> = if m.is_empty() { Default::default() } else { s.variations.iter().cloned().collect() };
|
||||
for (tag, val) in m {
|
||||
if tag.is_empty() || tag.len() > 4 {
|
||||
return Err(format!("variations: `{tag}` is not an axis tag"));
|
||||
}
|
||||
s.variations.retain(|(t, _)| t != tag);
|
||||
match val {
|
||||
J::Null => {}
|
||||
_ => s.variations.push((tag.clone(), val.as_f64().ok_or("variations: expected {tag: number}")? as f32)),
|
||||
J::Null => {
|
||||
axes.remove(tag);
|
||||
}
|
||||
_ => {
|
||||
axes.insert(tag.clone(), val.as_f64().ok_or("variations: expected {tag: number}")? as f32);
|
||||
}
|
||||
}
|
||||
}
|
||||
s.variations.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
if axes.len() > MAX_VARIATION_AXES {
|
||||
return Err(format!("variations: at most {MAX_VARIATION_AXES} axes"));
|
||||
}
|
||||
s.variations = axes.into_iter().collect();
|
||||
}
|
||||
_ => return Err("variations: expected {tag: number, …}".into()),
|
||||
},
|
||||
@@ -1321,6 +1346,44 @@ pub fn word_at(text: &str, ci: usize) -> Range<usize> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn variations_merge_by_tag_and_reject_too_many_axes() {
|
||||
let mut doc = TextDoc::plain("ab");
|
||||
assert!(doc.set_attr("variations", &serde_json::json!({"wght": 650, "wdth": 90}), None).unwrap());
|
||||
assert!(doc.set_attr("variations", &serde_json::json!({"wght": 700, "wdth": null, "opsz": 12}), None).unwrap());
|
||||
assert_eq!(doc.style_at(0).variations, vec![("opsz".to_string(), 12.0), ("wght".to_string(), 700.0)]);
|
||||
// More axes than any font has: refused up front, the style untouched.
|
||||
let many: serde_json::Map<String, J> = (0..MAX_VARIATION_AXES + 1).map(|i| (format!("a{i:03}"), J::from(1.0))).collect();
|
||||
assert!(doc.set_attr("variations", &J::Object(many), None).is_err());
|
||||
assert_eq!(doc.style_at(0).variations.len(), 2);
|
||||
// A merge that would grow past the limit is refused too.
|
||||
let fill: serde_json::Map<String, J> = (0..MAX_VARIATION_AXES - 2).map(|i| (format!("b{i:03}"), J::from(1.0))).collect();
|
||||
assert!(doc.set_attr("variations", &J::Object(fill), None).unwrap());
|
||||
assert_eq!(doc.style_at(0).variations.len(), MAX_VARIATION_AXES);
|
||||
// (The probe on an empty style passes; the styles already at the limit keep their axes.)
|
||||
let _ = doc.set_attr("variations", &serde_json::json!({"zzzz": 1}), None);
|
||||
assert_eq!(doc.style_at(0).variations.len(), MAX_VARIATION_AXES);
|
||||
assert!(!doc.style_at(0).variations.iter().any(|(t, _)| t == "zzzz"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refused_variation_leaves_every_run_unchanged() {
|
||||
// "ab" is at the axis limit; "cd" has none. Three new axes over both runs must be refused
|
||||
// for both, not applied to "cd" only.
|
||||
let mut doc = TextDoc::plain("abcd");
|
||||
let fill: serde_json::Map<String, J> = (0..MAX_VARIATION_AXES).map(|i| (format!("b{i:03}"), J::from(1.0))).collect();
|
||||
assert!(doc.set_attr("variations", &J::Object(fill), Some(0..2)).unwrap());
|
||||
assert!(doc.set_attr("variations", &serde_json::json!({"x1": 1, "x2": 1, "x3": 1}), None).is_err());
|
||||
assert_eq!(doc.style_at(3).variations.len(), 0);
|
||||
assert_eq!(doc.style_at(0).variations.len(), MAX_VARIATION_AXES);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_document_accepts_an_extreme_range_without_overflow() {
|
||||
let mut doc = TextDoc::plain("");
|
||||
assert!(doc.set_attr("size", &J::from(20), Some(usize::MAX..usize::MAX)).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mixed_line_breaks_keep_original_byte_and_character_ranges() {
|
||||
let text = "é\r\nβ\rx\n\n";
|
||||
|
||||
@@ -210,7 +210,13 @@ impl Gradient {
|
||||
}
|
||||
}
|
||||
|
||||
/// Most stops (colour or opacity) a gradient is sampled through. Gradients are sampled per
|
||||
/// pixel, so the stop count multiplies every painted pixel's work: stops past this are ignored
|
||||
/// when sampling, and a gradient written as JSON with more is refused.
|
||||
pub const MAX_GRADIENT_STOPS: usize = 256;
|
||||
|
||||
fn sample_stops<T: Copy>(stops: &[(f64, T)], t: f64, lerp: impl Fn(T, T, f64) -> T) -> Option<T> {
|
||||
let stops = &stops[..stops.len().min(MAX_GRADIENT_STOPS)];
|
||||
let first = stops.first()?;
|
||||
if t <= first.0 {
|
||||
return Some(first.1);
|
||||
@@ -243,7 +249,7 @@ fn gradient_json(j: &serde_json::Value) -> Option<Gradient> {
|
||||
}
|
||||
fn stops(a: &[J]) -> Option<Vec<(f64, [f32; 4])>> {
|
||||
let n = a.len();
|
||||
if n < 2 {
|
||||
if !(2..=MAX_GRADIENT_STOPS).contains(&n) {
|
||||
return None;
|
||||
}
|
||||
a.iter()
|
||||
@@ -259,6 +265,7 @@ fn gradient_json(j: &serde_json::Value) -> Option<Gradient> {
|
||||
J::Object(o) => {
|
||||
let colors = stops(o.get("colors")?.as_array()?)?;
|
||||
let op = match o.get("opacities") {
|
||||
Some(J::Array(a)) if a.len() > MAX_GRADIENT_STOPS => return None,
|
||||
Some(J::Array(a)) => Some(
|
||||
a.iter()
|
||||
.map(|s| {
|
||||
@@ -469,9 +476,10 @@ impl Value {
|
||||
}
|
||||
Value::Layer(_) => Some(Value::Layer(j.as_u64())),
|
||||
Value::Path(_) => serde_json::from_value::<ShapePath>(j.clone()).ok().map(Value::Path).or_else(|| serde_json::from_value::<Value>(j.clone()).ok()),
|
||||
Value::Gradient(_) => {
|
||||
serde_json::from_value::<Value>(j.clone()).ok().filter(|v| matches!(v, Value::Gradient(_))).or_else(|| gradient_json(j).map(Value::Gradient))
|
||||
}
|
||||
Value::Gradient(_) => serde_json::from_value::<Value>(j.clone())
|
||||
.ok()
|
||||
.filter(|v| matches!(v, Value::Gradient(g) if g.colors.len() <= MAX_GRADIENT_STOPS && g.opacities.len() <= MAX_GRADIENT_STOPS))
|
||||
.or_else(|| gradient_json(j).map(Value::Gradient)),
|
||||
}
|
||||
}
|
||||
/// JSON for display/automation (numbers/arrays for numeric kinds).
|
||||
|
||||
@@ -21,6 +21,9 @@ use serde_json::{Value as Json, json};
|
||||
|
||||
use crate::Timebase;
|
||||
|
||||
/// Most distinct warnings an import or export reports.
|
||||
const MAX_WARNINGS: usize = 200;
|
||||
|
||||
/// Export state shared by the whole document.
|
||||
pub(crate) struct Ex {
|
||||
pub tb: Timebase,
|
||||
@@ -31,7 +34,8 @@ pub(crate) struct Ex {
|
||||
impl Ex {
|
||||
pub fn warn(&mut self, w: impl Into<String>) {
|
||||
let w = w.into();
|
||||
if !self.warnings.contains(&w) {
|
||||
// Each warning is looked for among the earlier ones: past a screenful, more add nothing.
|
||||
if self.warnings.len() < MAX_WARNINGS && !self.warnings.contains(&w) {
|
||||
self.warnings.push(w);
|
||||
}
|
||||
}
|
||||
@@ -46,7 +50,8 @@ pub(crate) struct Im {
|
||||
impl Im {
|
||||
pub fn warn(&mut self, w: impl Into<String>) {
|
||||
let w = w.into();
|
||||
if !self.warnings.contains(&w) {
|
||||
// Each warning is looked for among the earlier ones: past a screenful, more add nothing.
|
||||
if self.warnings.len() < MAX_WARNINGS && !self.warnings.contains(&w) {
|
||||
self.warnings.push(w);
|
||||
}
|
||||
}
|
||||
|
||||
+18
-10
@@ -145,16 +145,18 @@ fn hex(c: [f32; 3]) -> String {
|
||||
format!("#{:02x}{:02x}{:02x}", b(c[0]), b(c[1]), b(c[2]))
|
||||
}
|
||||
|
||||
fn mime_of(path: &str) -> &'static str {
|
||||
let lower = path.to_ascii_lowercase();
|
||||
if lower.ends_with(".jpg") || lower.ends_with(".jpeg") {
|
||||
"image/jpeg"
|
||||
} else if lower.ends_with(".webp") {
|
||||
"image/webp"
|
||||
} else if lower.ends_with(".gif") {
|
||||
"image/gif"
|
||||
/// The image type `bytes` hold, by their signature; `None` for anything else.
|
||||
pub(crate) fn image_mime(bytes: &[u8]) -> Option<&'static str> {
|
||||
if bytes.starts_with(b"\x89PNG\r\n\x1a\n") {
|
||||
Some("image/png")
|
||||
} else if bytes.starts_with(&[0xff, 0xd8, 0xff]) {
|
||||
Some("image/jpeg")
|
||||
} else if bytes.starts_with(b"GIF87a") || bytes.starts_with(b"GIF89a") {
|
||||
Some("image/gif")
|
||||
} else if bytes.starts_with(b"RIFF") && bytes.get(8..12) == Some(b"WEBP") {
|
||||
Some("image/webp")
|
||||
} else {
|
||||
"image/png"
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,7 +288,13 @@ impl Doc<'_> {
|
||||
self.ex.warn(format!("{}: cannot read image `{}` (layer exported as a null)", l.name, f.path));
|
||||
return None;
|
||||
};
|
||||
let uri = format!("data:{};base64,{}", mime_of(&f.path), crate::base64::encode(&bytes));
|
||||
// What a project calls a still is embedded only when it is one: the path can name any
|
||||
// file (an imported animation's asset), and the export would carry its bytes verbatim.
|
||||
let Some(mime) = image_mime(&bytes) else {
|
||||
self.ex.warn(format!("{}: `{}` is not a PNG, JPEG, GIF or WebP image (layer exported as a null)", l.name, f.path));
|
||||
return None;
|
||||
};
|
||||
let uri = format!("data:{mime};base64,{}", crate::base64::encode(&bytes));
|
||||
self.assets.push(json!({"id": id, "nm": it.name, "w": f.width, "h": f.height, "u": "", "p": uri, "e": 1}));
|
||||
self.done.insert(id.clone());
|
||||
Some(id)
|
||||
|
||||
+30
-20
@@ -1,6 +1,6 @@
|
||||
//! Lottie document → EffectCraft compositions (the inverse of [`crate::export`]).
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::sync::Arc;
|
||||
|
||||
use effectcraft_color::Label;
|
||||
@@ -17,6 +17,9 @@ use crate::anim::{self, Im, import_prop};
|
||||
use crate::export::{fx_by_id, fx_by_type, fx_params, mask_mode_from};
|
||||
use crate::{LottieError, Timebase, shapes, text};
|
||||
|
||||
/// Longest font family or style name kept (each text key using the font holds a copy).
|
||||
const MAX_FONT_NAME_CHARS: usize = 256;
|
||||
|
||||
/// An embedded image asset handed to the caller to store (the returned path becomes the
|
||||
/// footage item's file).
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -129,9 +132,12 @@ pub fn import(
|
||||
Ctx { im: Im { tb, warnings: vec![] }, assets: BTreeMap::new(), fonts: vec![], store, base_dir, items: vec![], folder: None, name: name.clone() };
|
||||
if let Some(list) = doc.get("fonts").and_then(|f| f.get("list")).and_then(Json::as_array) {
|
||||
for fo in list {
|
||||
// Every text key referring to the font copies its family and style: keep them to
|
||||
// a font name's length.
|
||||
let short = |v: &str| v.chars().take(MAX_FONT_NAME_CHARS).collect::<String>();
|
||||
let n = s(fo, "fName").unwrap_or("").to_string();
|
||||
let fam = s(fo, "fFamily").unwrap_or(&n).to_string();
|
||||
let st = s(fo, "fStyle").unwrap_or("Regular").to_string();
|
||||
let fam = short(s(fo, "fFamily").unwrap_or(&n));
|
||||
let st = short(s(fo, "fStyle").unwrap_or("Regular"));
|
||||
cx.fonts.push((n, fam, st));
|
||||
}
|
||||
}
|
||||
@@ -150,11 +156,12 @@ pub fn import(
|
||||
let duration = tb.tick(op);
|
||||
// Pass 1: asset items (precomps empty for now, images stored).
|
||||
let assets = doc.get("assets").and_then(Json::as_array).cloned().unwrap_or_default();
|
||||
let layer_sizes = precomp_layer_sizes(&doc, &assets);
|
||||
for a in &assets {
|
||||
let Some(id) = s(a, "id") else { continue };
|
||||
if a.get("layers").is_some() {
|
||||
let folder = cx.folder(project);
|
||||
let layer = precomp_layer_size(&doc, &assets, id);
|
||||
let layer = layer_sizes.get(id).copied();
|
||||
let aw = f(a, "w").map(|v| v as u32).or(layer.map(|l| l.0)).unwrap_or(w);
|
||||
let ah = f(a, "h").map(|v| v as u32).or(layer.map(|l| l.1)).unwrap_or(h);
|
||||
let arate = f(a, "fr").map(FrameRate::from_f64).unwrap_or(rate);
|
||||
@@ -255,17 +262,20 @@ pub fn import(
|
||||
Ok(ImportResult { comp: id, items: cx.items, warnings: cx.im.warnings })
|
||||
}
|
||||
|
||||
/// The `w`/`h` of the first precomp layer, in the main comp or in any asset, that shows asset `id`.
|
||||
/// lottie-spec gives a precomposition asset no size: the layer's `w`/`h` are its clipping rect.
|
||||
fn precomp_layer_size(doc: &Json, assets: &[Json], id: &str) -> Option<(u32, u32)> {
|
||||
/// By asset id, the `w`/`h` of the first precomp layer, in the main comp or in any asset, that
|
||||
/// shows it. lottie-spec gives a precomposition asset no size: the layer's `w`/`h` are its
|
||||
/// clipping rect. One pass over all layers, not one per asset.
|
||||
fn precomp_layer_sizes<'a>(doc: &'a Json, assets: &'a [Json]) -> HashMap<&'a str, (u32, u32)> {
|
||||
let mut out = HashMap::new();
|
||||
let lists = doc.get("layers").into_iter().chain(assets.iter().filter_map(|a| a.get("layers")));
|
||||
lists.filter_map(Json::as_array).flatten().find_map(|l| {
|
||||
if l.get("ty").and_then(Json::as_u64) != Some(0) || s(l, "refId") != Some(id) {
|
||||
return None;
|
||||
for l in lists.filter_map(Json::as_array).flatten() {
|
||||
if l.get("ty").and_then(Json::as_u64) != Some(0) {
|
||||
continue;
|
||||
}
|
||||
let (lw, lh) = f(l, "w").zip(f(l, "h"))?;
|
||||
Some((lw.round().max(1.0) as u32, lh.round().max(1.0) as u32))
|
||||
})
|
||||
let (Some(id), Some((lw, lh))) = (s(l, "refId"), f(l, "w").zip(f(l, "h"))) else { continue };
|
||||
out.entry(id).or_insert((lw.round().max(1.0) as u32, lh.round().max(1.0) as u32));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn transform(im: &Im, ks: &Json, tr: &mut PropGroup, next: &mut u64) {
|
||||
@@ -411,8 +421,8 @@ fn layer_source(cx: &mut Ctx, project: &mut Project, comp: &Comp, j: &Json) -> O
|
||||
fn build_layers(cx: &mut Ctx, project: &mut Project, comp: &mut Comp, layers: &[Json]) {
|
||||
let tb = cx.im.tb;
|
||||
let mut by_ind: BTreeMap<i64, LayerId> = BTreeMap::new();
|
||||
// (layer id, parent ind, matte: (kind, explicit tp ind), json position)
|
||||
let mut pending: Vec<(LayerId, Option<i64>, Option<(MatteKind, Option<i64>)>, usize)> = vec![];
|
||||
// (layer id, its index in `comp.layers`, parent ind, matte: (kind, explicit tp ind), json position)
|
||||
let mut pending: Vec<(LayerId, usize, Option<i64>, Option<(MatteKind, Option<i64>)>, usize)> = vec![];
|
||||
let mut pos_ind: Vec<Option<i64>> = vec![];
|
||||
for (ji, j) in layers.iter().enumerate() {
|
||||
let ind = j.get("ind").and_then(Json::as_i64);
|
||||
@@ -463,8 +473,8 @@ fn build_layers(cx: &mut Ctx, project: &mut Project, comp: &mut Comp, layers: &[
|
||||
let mut ids = Ids(&mut next);
|
||||
match &source {
|
||||
LayerSource::Shape => {
|
||||
let items = j.get("shapes").and_then(Json::as_array).cloned().unwrap_or_default();
|
||||
let groups = shapes::import_items(&mut cx.im, &mut ids, &items);
|
||||
let mut items = j.get("shapes").and_then(Json::as_array).into_iter().flatten();
|
||||
let groups = shapes::import_items(&mut cx.im, &mut ids, &mut items);
|
||||
if let Some(c) = l.props.sub_mut("contents") {
|
||||
c.children.extend(groups.into_iter().map(Node::Group));
|
||||
}
|
||||
@@ -507,10 +517,10 @@ fn build_layers(cx: &mut Ctx, project: &mut Project, comp: &mut Comp, layers: &[
|
||||
_ => None,
|
||||
}
|
||||
.map(|k| (k, j.get("tp").and_then(Json::as_i64)));
|
||||
pending.push((l.id, parent, matte, ji));
|
||||
pending.push((l.id, comp.layers.len(), parent, matte, ji));
|
||||
comp.layers.push(l);
|
||||
}
|
||||
for (id, parent, matte, ji) in pending {
|
||||
for (id, at, parent, matte, ji) in pending {
|
||||
let parent_id = parent.and_then(|p| by_ind.get(&p).copied()).filter(|p| *p != id);
|
||||
let matte_id = matte.and_then(|(k, tp)| {
|
||||
let target = match tp {
|
||||
@@ -520,7 +530,7 @@ fn build_layers(cx: &mut Ctx, project: &mut Project, comp: &mut Comp, layers: &[
|
||||
};
|
||||
target.filter(|t| *t != id).map(|t| TrackMatte { layer: t, kind: k })
|
||||
});
|
||||
if let Some(l) = comp.layer_mut(id) {
|
||||
if let Some(l) = comp.layers.get_mut(at) {
|
||||
l.parent = parent_id;
|
||||
l.track_matte = matte_id;
|
||||
}
|
||||
|
||||
@@ -436,7 +436,7 @@ fn import_shape_transform(im: &Im, j: &Json, tr: &mut PropGroup) {
|
||||
}
|
||||
|
||||
/// Import Lottie shape items into shape contents groups.
|
||||
pub(crate) fn import_items(im: &mut Im, ids: &mut Ids, items: &[Json]) -> Vec<PropGroup> {
|
||||
pub(crate) fn import_items(im: &mut Im, ids: &mut Ids, items: &mut dyn Iterator<Item = &Json>) -> Vec<PropGroup> {
|
||||
let mut out = vec![];
|
||||
for j in items {
|
||||
let ty = j.get("ty").and_then(Json::as_str).unwrap_or("");
|
||||
@@ -447,9 +447,10 @@ pub(crate) fn import_items(im: &mut Im, ids: &mut Ids, items: &[Json]) -> Vec<Pr
|
||||
};
|
||||
let mut g = match ty {
|
||||
"gr" => {
|
||||
let list = j.get("it").and_then(Json::as_array).cloned().unwrap_or_default();
|
||||
let inner: Vec<Json> = list.iter().filter(|i| i.get("ty").and_then(Json::as_str) != Some("tr")).cloned().collect();
|
||||
let children = import_items(im, ids, &inner);
|
||||
// Borrowed, not cloned: each nesting level copying its subtree costs memory
|
||||
// quadratic in the depth.
|
||||
let list = j.get("it").and_then(Json::as_array).map(Vec::as_slice).unwrap_or_default();
|
||||
let children = import_items(im, ids, &mut list.iter().filter(|i| i.get("ty").and_then(Json::as_str) != Some("tr")));
|
||||
let mut g = build::shape_group(ids, "Group 1", children);
|
||||
if let Some(tr) = list.iter().find(|i| i.get("ty").and_then(Json::as_str) == Some("tr"))
|
||||
&& let Some(t) = g.sub_mut("transform")
|
||||
|
||||
@@ -725,7 +725,7 @@ fn effects_expressions_and_warnings() {
|
||||
#[test]
|
||||
fn images_embed_as_base64_and_come_back() {
|
||||
let (mut p, cid) = setup();
|
||||
let png: Vec<u8> = vec![0x89, b'P', b'N', b'G', 1, 2, 3, 4, 5];
|
||||
let png: Vec<u8> = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a, 1, 2, 3, 4, 5];
|
||||
let fid = p.add_item(
|
||||
"pic.png",
|
||||
Label::Lavender,
|
||||
@@ -775,6 +775,17 @@ fn images_embed_as_base64_and_come_back() {
|
||||
assert!(f.path.starts_with("/tmp/image_") && f.path.ends_with(".png"), "{}", f.path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_signatures_are_matched_in_full() {
|
||||
use crate::export::image_mime;
|
||||
assert_eq!(image_mime(b"\x89PNG\r\n\x1a\n\0"), Some("image/png"));
|
||||
assert_eq!(image_mime(b"GIF89a\0"), Some("image/gif"));
|
||||
assert_eq!(image_mime(b"GIF87a\0"), Some("image/gif"));
|
||||
// Near matches (a prefix of the signature, or text that starts with it) are not images.
|
||||
assert_eq!(image_mime(b"\x89PNG\x01\x02\x03\x04"), None);
|
||||
assert_eq!(image_mime(b"GIF8 is a text file"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dotlottie_archive_roundtrip() {
|
||||
let (mut p, cid) = setup();
|
||||
@@ -985,3 +996,69 @@ fn per_corner_roundness_exports_equal_radii_and_warns_when_mixed() {
|
||||
assert_eq!(rc(&json)["r"]["k"], json!(5.0));
|
||||
assert!(!warnings.iter().any(|w| w.contains("per-corner")), "{warnings:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many_layers_and_precomps_import_with_their_parents_and_sizes() {
|
||||
// 5 000 null layers each parented to the previous, and 2 000 empty precomp assets: the
|
||||
// relationship and size passes index the layers instead of searching them per layer. (Each
|
||||
// has its own name: `Comp::unique_layer_name` numbering one repeated name is a separate cost.)
|
||||
let n = 5_000;
|
||||
let layers: Vec<Json> = (1..=n).map(|i| json!({"ty": 3, "nm": format!("N{i}"), "ind": i, "parent": i - 1, "ip": 0, "op": 30, "st": 0, "ks": {}})).collect();
|
||||
let mut assets: Vec<Json> = (0..2_000).map(|i| json!({"id": format!("c{i}"), "layers": []})).collect();
|
||||
assets.push(json!({"id": "last", "layers": []}));
|
||||
let mut layers = layers;
|
||||
layers.push(json!({"ty": 0, "ind": n + 1, "refId": "last", "w": 50, "h": 40, "ip": 0, "op": 30, "st": 0, "ks": {}}));
|
||||
let doc = json!({"w": 200, "h": 120, "fr": 30, "ip": 0, "op": 30, "assets": assets, "layers": layers});
|
||||
let mut q = Project::default();
|
||||
let nc = import(&mut q, doc.to_string().as_bytes(), "Imported", "", &mut |_| None).unwrap().comp;
|
||||
let c = q.comp(nc).unwrap();
|
||||
assert_eq!(c.layers.len(), n as usize + 1);
|
||||
assert_eq!(c.layers.get(0).unwrap().parent, None);
|
||||
assert_eq!(c.layers.get(1).unwrap().parent, Some(c.layers.get(0).unwrap().id));
|
||||
let LayerSource::Comp { item } = c.layers.get(n as usize).unwrap().source else { panic!("not a precomp") };
|
||||
let pc = q.comp(item).unwrap();
|
||||
assert_eq!((pc.width, pc.height), (50, 40));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deeply_nested_shape_groups_and_long_font_names_import() {
|
||||
let mut g = json!({"ty": "rc", "s": {"a": 0, "k": [10, 10]}, "p": {"a": 0, "k": [0, 0]}, "r": {"a": 0, "k": 0}});
|
||||
// 50 levels: each is two levels of JSON, within serde_json's recursion limit of 128.
|
||||
for _ in 0..50 {
|
||||
g = json!({"ty": "gr", "it": [g, {"ty": "tr", "o": {"a": 0, "k": 100}}]});
|
||||
}
|
||||
let family = "F".repeat(100_000);
|
||||
let keys: Vec<Json> = (0..50).map(|i| json!({"t": i, "s": {"t": "x", "f": "A", "s": 10}})).collect();
|
||||
let doc = json!({"w": 200, "h": 120, "fr": 30, "ip": 0, "op": 60,
|
||||
"fonts": {"list": [{"fName": "A", "fFamily": family, "fStyle": "Regular"}]},
|
||||
"layers": [
|
||||
{"ty": 4, "ind": 1, "ip": 0, "op": 60, "st": 0, "ks": {}, "shapes": [g]},
|
||||
{"ty": 5, "ind": 2, "ip": 0, "op": 60, "st": 0, "ks": {}, "t": {"d": {"k": keys}}}
|
||||
]});
|
||||
let mut q = Project::default();
|
||||
let nc = import(&mut q, doc.to_string().as_bytes(), "Imported", "", &mut |_| None).unwrap().comp;
|
||||
let c = q.comp(nc).unwrap();
|
||||
let st = c.layers.get(1).unwrap().props.prop("text/sourceText").unwrap();
|
||||
assert_eq!(st.keys.len(), 50);
|
||||
let Value::Text(d) = &st.keys[0].value else { panic!("not text") };
|
||||
assert_eq!(d.font.len(), 256);
|
||||
}
|
||||
|
||||
/// A footage item called a still whose file is not an image is not embedded.
|
||||
#[test]
|
||||
fn only_image_bytes_are_embedded() {
|
||||
let (mut p, cid) = setup();
|
||||
let fid = p.add_item(
|
||||
"secret.png",
|
||||
Label::Lavender,
|
||||
None,
|
||||
ItemKind::Footage(Footage { path: "/virtual/secret.png".into(), kind: FootageKind::Still, width: 8, height: 8, has_video: true, ..Default::default() }),
|
||||
);
|
||||
let c = comp(&p, cid);
|
||||
let l = build::layer(&mut p, &c, "secret.png", LayerSource::Footage { item: fid }, (8, 8), None);
|
||||
push(&mut p, cid, l);
|
||||
let read = |path: &str| (path == "/virtual/secret.png").then(|| b"root:x:0:0:root:/root:/bin/sh\n".to_vec());
|
||||
let res = export_comp(&p, cid, &ExportOptions::default(), &read).unwrap();
|
||||
assert!(res.json["assets"].as_array().is_none_or(|a| a.is_empty()), "{}", res.json["assets"]);
|
||||
assert!(res.warnings.iter().any(|w| w.contains("not a PNG")), "{:?}", res.warnings);
|
||||
}
|
||||
@@ -310,7 +310,8 @@ pub(crate) fn import_text(im: &mut Im, ids: &mut Ids, t: &Json, tg: &mut PropGro
|
||||
st.value = Value::Text(Box::new(first.clone()));
|
||||
}
|
||||
if docs.len() > 1 {
|
||||
st.keys = docs.iter().map(|(f, d)| effectcraft_keyframe::Keyframe::new(im.tb.tick(*f), Value::Text(Box::new(d.clone()))).hold()).collect();
|
||||
// Moved into the keys, not cloned: the documents are not kept twice.
|
||||
st.keys = docs.into_iter().map(|(f, d)| effectcraft_keyframe::Keyframe::new(im.tb.tick(f), Value::Text(Box::new(d))).hold()).collect();
|
||||
}
|
||||
}
|
||||
if let Some(m) = t.get("m")
|
||||
|
||||
@@ -39,5 +39,6 @@ thiserror = { workspace = true }
|
||||
log = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = { workspace = true }
|
||||
# (tests render comps that read layers through effects; already a dependency of effectcraft-render)
|
||||
effectcraft-effects = { workspace = true }
|
||||
@@ -747,8 +747,8 @@ fn sequence_gaps_hold_the_previous_frame() {
|
||||
/// parallax project's 21120-px plates were decoded and resampled for every frame at Quarter).
|
||||
#[test]
|
||||
fn stills_at_reduced_sizes_are_resampled_once() {
|
||||
let dir = std::env::temp_dir().join(format!("ec-still-size-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).expect("temp dir");
|
||||
let guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = guard.path().to_path_buf();
|
||||
let p = dir.join("plate.png");
|
||||
let px = |x: u32, y: u32| {
|
||||
image::Rgba([(x * 7 % 256) as u8, (y * 13 % 256) as u8, ((x ^ y) % 256) as u8, if (x + y).is_multiple_of(5) { 0 } else { 255 - (x % 128) as u8 }])
|
||||
|
||||
@@ -311,6 +311,9 @@ fn frame(v: &[P]) -> (P, f64) {
|
||||
(c, r)
|
||||
}
|
||||
|
||||
/// Vertex counts above this skip the shape contexts (quadratic to build) and search the starting
|
||||
/// vertex coarsely; no mask comes near it (the Add Vertices option itself stops at 2000).
|
||||
const MAX_CONTEXT_VERTICES: usize = 4096;
|
||||
const RBINS: usize = 5;
|
||||
const ABINS: usize = 12;
|
||||
|
||||
@@ -412,10 +415,18 @@ pub fn correspond(a: &ShapePath, b: &ShapePath, o: &InterpOpts) -> (ShapePath, S
|
||||
}
|
||||
if !o.first_vertices_match {
|
||||
let n = ra.len();
|
||||
let use_sc = o.quality > 0.0;
|
||||
let large = n > MAX_CONTEXT_VERTICES;
|
||||
let use_sc = o.quality > 0.0 && !large;
|
||||
let (sa, sb) = if use_sc { (Some(shape_contexts(&ra.vertices)), Some(shape_contexts(&rb.vertices))) } else { (None, None) };
|
||||
// Quality limits the offsets examined exhaustively (coarse step), then refines.
|
||||
let step = if o.quality >= 50.0 { 1 } else { ((n as f64 / 24.0).ceil() as usize).max(1) };
|
||||
// (A very large shape tries about 3 √n offsets, not n, each costing a pass over it.)
|
||||
let step = if large {
|
||||
(n as f64).sqrt().ceil() as usize
|
||||
} else if o.quality >= 50.0 {
|
||||
1
|
||||
} else {
|
||||
((n as f64 / 24.0).ceil() as usize).max(1)
|
||||
};
|
||||
let mut best = (f64::MAX, 0usize);
|
||||
let mut k = 0;
|
||||
while k < n {
|
||||
@@ -592,4 +603,22 @@ mod tests {
|
||||
let l2 = (lin.vertices[1][0] - lin.vertices[0][0]).hypot(lin.vertices[1][1] - lin.vertices[0][1]);
|
||||
assert!(l2 < 80.0);
|
||||
}
|
||||
|
||||
/// A shape with tens of thousands of vertices is matched without the quadratic shape contexts
|
||||
/// and exhaustive offset search.
|
||||
#[test]
|
||||
fn very_large_shapes_are_matched_in_bounded_time() {
|
||||
let n = 30_000;
|
||||
let circle = |rot: f64| -> ShapePath {
|
||||
let pts: Vec<P> = (0..n)
|
||||
.map(|i| [100.0 * (rot + i as f64 * std::f64::consts::TAU / n as f64).cos(), 100.0 * (rot + i as f64 * std::f64::consts::TAU / n as f64).sin()])
|
||||
.collect();
|
||||
ShapePath::polygon(&pts, true)
|
||||
};
|
||||
let o = InterpOpts { one_to_one: true, quality: 100.0, ..Default::default() };
|
||||
let t = std::time::Instant::now();
|
||||
let (a, b) = correspond(&circle(0.0), &circle(1.0), &o);
|
||||
assert_eq!((a.len(), b.len()), (n, n));
|
||||
assert!(t.elapsed().as_secs() < 30, "{:?}", t.elapsed());
|
||||
}
|
||||
}
|
||||
+176
-38
@@ -55,37 +55,85 @@ pub fn trace(field: &[f32], w: usize, h: usize, o: &TraceOpts) -> Vec<Traced> {
|
||||
return vec![];
|
||||
}
|
||||
let loops = contours(field, w, h, o.threshold);
|
||||
let mut polys: Vec<(Vec<[f64; 2]>, Vec<[f64; 2]>, f64)> = loops
|
||||
// (raw loop, kept raw indices, simplified loop, area)
|
||||
let mut polys: Vec<(Vec<[f64; 2]>, Vec<usize>, Vec<[f64; 2]>, f64)> = loops
|
||||
.into_iter()
|
||||
.filter_map(|raw| {
|
||||
let a = signed_area(&raw);
|
||||
if a.abs() < o.min_area.max(0.0) || raw.len() < 3 {
|
||||
return None;
|
||||
}
|
||||
let simp = simplify_closed(&raw, o.tolerance.max(0.01));
|
||||
if simp.len() < 3 {
|
||||
let idx = simplify_idx(&raw, o.tolerance.max(0.01));
|
||||
if idx.len() < 3 {
|
||||
return None;
|
||||
}
|
||||
Some((raw, simp, a))
|
||||
})
|
||||
.collect();
|
||||
// Depth: how many other loops contain a point of this loop.
|
||||
let depths: Vec<usize> = (0..polys.len())
|
||||
.map(|i| {
|
||||
let p = polys[i].1[0];
|
||||
(0..polys.len()).filter(|&j| j != i && polys[j].2.abs() > polys[i].2.abs() && point_in_poly(p, &polys[j].1)).count()
|
||||
let simp = idx.iter().map(|&i| raw[i]).collect();
|
||||
Some((raw, idx, simp, a))
|
||||
})
|
||||
.collect();
|
||||
let depths = nesting_depths(&polys.iter().map(|p| (p.2.as_slice(), p.3.abs())).collect::<Vec<_>>(), w, h);
|
||||
let mut out: Vec<Traced> = polys
|
||||
.drain(..)
|
||||
.zip(depths)
|
||||
.map(|((raw, simp, a), depth)| Traced { path: fit_loop(&raw, &simp, o.corner_roundness.clamp(0.0, 1.0)), depth, area: a })
|
||||
.map(|((raw, idx, simp, a), depth)| Traced { path: fit_loop(&raw, &idx, &simp, o.corner_roundness.clamp(0.0, 1.0)), depth, area: a })
|
||||
.collect();
|
||||
// Outer outlines first, then holes, islands…; larger first within a depth.
|
||||
out.sort_by(|a, b| a.depth.cmp(&b.depth).then(b.area.abs().total_cmp(&a.area.abs())));
|
||||
out
|
||||
}
|
||||
|
||||
/// Grid cell (pixels) of the index [`nesting_depths`] finds candidate containers with.
|
||||
const DEPTH_CELL: f64 = 32.0;
|
||||
|
||||
/// Most grid cells one loop's box is entered into; a box covering more is kept in a short list
|
||||
/// every point is tested against, so nested full-image contours can't fill the grid.
|
||||
const MAX_BOX_CELLS: usize = 64;
|
||||
|
||||
/// Per loop (simplified points, |area|): how many other, larger loops contain its first point.
|
||||
/// Only loops whose bounding box holds that point can contain it, so each loop's box is entered
|
||||
/// into a coarse grid over the `w × h` field and a point is tested against the loops of its
|
||||
/// cell alone: many separate regions no longer test every pair.
|
||||
fn nesting_depths(polys: &[(&[[f64; 2]], f64)], w: usize, h: usize) -> Vec<usize> {
|
||||
// Loops lie within the field padded by half a pixel each side.
|
||||
let (gw, gh) = (((w as f64 + 1.0) / DEPTH_CELL).ceil() as usize + 1, ((h as f64 + 1.0) / DEPTH_CELL).ceil() as usize + 1);
|
||||
let cell = |v: f64, n: usize| (((v + 0.5) / DEPTH_CELL).floor().max(0.0) as usize).min(n - 1);
|
||||
let boxes: Vec<[f64; 4]> = polys
|
||||
.iter()
|
||||
.map(|(p, _)| {
|
||||
p.iter().fold([f64::INFINITY, f64::INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY], |b, q| {
|
||||
[b[0].min(q[0]), b[1].min(q[1]), b[2].max(q[0]), b[3].max(q[1])]
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let mut grid: Vec<Vec<usize>> = vec![Vec::new(); gw * gh];
|
||||
let mut wide: Vec<usize> = Vec::new();
|
||||
for (j, b) in boxes.iter().enumerate() {
|
||||
let (x0, x1, y0, y1) = (cell(b[0], gw), cell(b[2], gw), cell(b[1], gh), cell(b[3], gh));
|
||||
if (x1 - x0 + 1).saturating_mul(y1 - y0 + 1) > MAX_BOX_CELLS {
|
||||
wide.push(j);
|
||||
continue;
|
||||
}
|
||||
for cy in y0..=y1 {
|
||||
for cx in x0..=x1 {
|
||||
grid[cy * gw + cx].push(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
(0..polys.len())
|
||||
.map(|i| {
|
||||
let p = polys[i].0[0];
|
||||
grid[cell(p[1], gh) * gw + cell(p[0], gw)]
|
||||
.iter()
|
||||
.chain(&wide)
|
||||
.filter(|&&j| {
|
||||
let b = boxes[j];
|
||||
j != i && polys[j].1 > polys[i].1 && p[0] >= b[0] && p[0] <= b[2] && p[1] >= b[1] && p[1] <= b[3] && point_in_poly(p, polys[j].0)
|
||||
})
|
||||
.count()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Marching squares: closed polylines (pixel coordinates).
|
||||
fn contours(field: &[f32], w: usize, h: usize, thr: f32) -> Vec<Vec<[f64; 2]>> {
|
||||
// Padded grid: sample (i, j) of the padded grid is pixel (i - 1, j - 1), outside = -inf.
|
||||
@@ -233,23 +281,37 @@ fn seg_dist(p: [f64; 2], a: [f64; 2], b: [f64; 2]) -> f64 {
|
||||
((p[0] - a[0] - t * dx).powi(2) + (p[1] - a[1] - t * dy).powi(2)).sqrt()
|
||||
}
|
||||
|
||||
/// Douglas–Peucker on an open polyline: kept indices.
|
||||
fn dp(p: &[[f64; 2]], lo: usize, hi: usize, tol: f64, keep: &mut Vec<usize>) {
|
||||
if hi <= lo + 1 {
|
||||
return;
|
||||
}
|
||||
let (mut best, mut bi) = (0.0, lo);
|
||||
for (k, q) in p.iter().enumerate().take(hi).skip(lo + 1) {
|
||||
let d = seg_dist(*q, p[lo], p[hi]);
|
||||
if d > best {
|
||||
best = d;
|
||||
bi = k;
|
||||
/// Distance evaluations Douglas–Peucker may spend per point of a loop. Balanced splits need
|
||||
/// about log2(n); splits that keep landing next to an end would need up to n.
|
||||
const DP_WORK_PER_POINT: usize = 64;
|
||||
|
||||
/// Douglas–Peucker on an open polyline: kept indices (unordered). Spans wait on an explicit
|
||||
/// stack (no recursion depth to overflow), and once `budget` distance evaluations are spent the
|
||||
/// spans still waiting keep all their points: the outline stays exact, just less simplified.
|
||||
fn dp(p: &[[f64; 2]], lo: usize, hi: usize, tol: f64, budget: &mut usize, keep: &mut Vec<usize>) {
|
||||
let mut spans = vec![(lo, hi)];
|
||||
while let Some((lo, hi)) = spans.pop() {
|
||||
if hi <= lo + 1 {
|
||||
continue;
|
||||
}
|
||||
if *budget < hi - lo - 1 {
|
||||
keep.extend(lo + 1..hi);
|
||||
continue;
|
||||
}
|
||||
*budget -= hi - lo - 1;
|
||||
let (mut best, mut bi) = (0.0, lo);
|
||||
for (k, q) in p.iter().enumerate().take(hi).skip(lo + 1) {
|
||||
let d = seg_dist(*q, p[lo], p[hi]);
|
||||
if d > best {
|
||||
best = d;
|
||||
bi = k;
|
||||
}
|
||||
}
|
||||
if best > tol {
|
||||
keep.push(bi);
|
||||
spans.push((lo, bi));
|
||||
spans.push((bi, hi));
|
||||
}
|
||||
}
|
||||
if best > tol {
|
||||
dp(p, lo, bi, tol, keep);
|
||||
keep.push(bi);
|
||||
dp(p, bi, hi, tol, keep);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,27 +327,23 @@ fn simplify_idx(p: &[[f64; 2]], tol: f64) -> Vec<usize> {
|
||||
let mut ext: Vec<[f64; 2]> = p.to_vec();
|
||||
ext.push(p[0]);
|
||||
let mut keep = vec![0];
|
||||
dp(&ext, 0, far, tol, &mut keep);
|
||||
let mut budget = n.saturating_mul(DP_WORK_PER_POINT);
|
||||
dp(&ext, 0, far, tol, &mut budget, &mut keep);
|
||||
keep.push(far);
|
||||
dp(&ext, far, n, tol, &mut keep);
|
||||
dp(&ext, far, n, tol, &mut budget, &mut keep);
|
||||
keep.sort_unstable();
|
||||
keep.dedup();
|
||||
keep.retain(|&i| i < n);
|
||||
keep
|
||||
}
|
||||
|
||||
fn simplify_closed(p: &[[f64; 2]], tol: f64) -> Vec<[f64; 2]> {
|
||||
simplify_idx(p, tol).into_iter().map(|i| p[i]).collect()
|
||||
}
|
||||
|
||||
/// Fit cubics through the simplified vertices of a loop (`raw` holds the full contour).
|
||||
fn fit_loop(raw: &[[f64; 2]], simp: &[[f64; 2]], round: f64) -> ShapePath {
|
||||
/// Fit cubics through the simplified vertices of a loop (`raw` holds the full contour, `idx`
|
||||
/// the raw index of each simplified vertex, as [`simplify_idx`] kept them).
|
||||
fn fit_loop(raw: &[[f64; 2]], idx: &[usize], simp: &[[f64; 2]], round: f64) -> ShapePath {
|
||||
let n = simp.len();
|
||||
if round <= 1e-6 {
|
||||
return ShapePath::polygon(simp, true);
|
||||
}
|
||||
// Raw index of each simplified vertex (they are raw points).
|
||||
let idx: Vec<usize> = simp.iter().map(|s| raw.iter().position(|r| r == s).unwrap_or(0)).collect();
|
||||
let pt = |a: [f64; 2]| Point::new(a[0], a[1]);
|
||||
let v = |a: [f64; 2], b: [f64; 2]| Vec2::new(b[0] - a[0], b[1] - a[1]);
|
||||
// Per vertex: (direction leaving, direction arriving), blended between chord and smooth.
|
||||
@@ -349,6 +407,86 @@ mod tests {
|
||||
fill_coverage(&paths, &Mat3::IDENTITY, w as u32, h as u32, FillRule::EvenOdd).data
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nesting_depths_match_every_pair() {
|
||||
// Rings within rings beside many separate dots: the grid lookup gives the same depths
|
||||
// as testing every pair.
|
||||
let (w, h) = (200, 120);
|
||||
let mut field = vec![0.0f32; w * h];
|
||||
for y in 0..h {
|
||||
for x in 0..w {
|
||||
let (fx, fy) = (x as f64 + 0.5 - 60.0, y as f64 + 0.5 - 60.0);
|
||||
let r = (fx * fx + fy * fy).sqrt();
|
||||
let ring = r < 56.0 && ((r / 8.0) as usize).is_multiple_of(2);
|
||||
let dot = x >= 130 && x % 6 < 3 && y % 6 < 3;
|
||||
field[y * w + x] = if ring || dot { 1.0 } else { 0.0 };
|
||||
}
|
||||
}
|
||||
let opts = TraceOpts { min_area: 1.0, ..Default::default() };
|
||||
let t = trace(&field, w, h, &opts);
|
||||
assert!(t.len() > 200, "{}", t.len());
|
||||
let polys: Vec<(Vec<[f64; 2]>, f64)> = t.iter().map(|t| (t.path.vertices.clone(), t.area.abs())).collect();
|
||||
let refs: Vec<(&[[f64; 2]], f64)> = polys.iter().map(|(p, a)| (p.as_slice(), *a)).collect();
|
||||
let fast = nesting_depths(&refs, w, h);
|
||||
let pairs: Vec<usize> =
|
||||
(0..refs.len()).map(|i| (0..refs.len()).filter(|&j| j != i && refs[j].1 > refs[i].1 && point_in_poly(refs[i].0[0], refs[j].0)).count()).collect();
|
||||
assert_eq!(fast, pairs);
|
||||
assert_eq!(fast, t.iter().map(|t| t.depth).collect::<Vec<_>>());
|
||||
assert!(fast.iter().max() >= Some(&5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nesting_depths_count_wide_boxes_kept_out_of_the_grid() {
|
||||
// Concentric squares whose boxes each cover far more than MAX_BOX_CELLS cells.
|
||||
let (w, h) = (4000, 4000);
|
||||
let squares: Vec<Vec<[f64; 2]>> = (0..40)
|
||||
.map(|k| {
|
||||
let (a, b) = (10.0 + 40.0 * k as f64, 3990.0 - 40.0 * k as f64);
|
||||
vec![[a, a], [b, a], [b, b], [a, b]]
|
||||
})
|
||||
.collect();
|
||||
let refs: Vec<(&[[f64; 2]], f64)> = squares.iter().map(|p| (p.as_slice(), (p[1][0] - p[0][0]).powi(2))).collect();
|
||||
assert_eq!(nesting_depths(&refs, w, h), (0..40).collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simplification_work_is_bounded_and_keeps_the_outline() {
|
||||
// Out of budget, the spans still waiting keep every point: nothing is lost, the outline
|
||||
// is just less simplified.
|
||||
let p: Vec<[f64; 2]> = (0..12).map(|i| [i as f64, if i % 2 == 0 { 0.0 } else { 5.0 }]).collect();
|
||||
let (mut budget, mut keep) = (0, vec![]);
|
||||
dp(&p, 0, 11, 0.01, &mut budget, &mut keep);
|
||||
keep.sort_unstable();
|
||||
assert_eq!(keep, (1..11).collect::<Vec<_>>());
|
||||
// With a budget, the result matches the plain recursion's on an ordinary loop.
|
||||
let (mut budget, mut keep) = (usize::MAX, vec![]);
|
||||
dp(&p, 0, 11, 0.01, &mut budget, &mut keep);
|
||||
keep.sort_unstable();
|
||||
assert_eq!(keep, (1..11).collect::<Vec<_>>());
|
||||
// A long, finely zig-zagging loop: simplified within a budget linear in its length.
|
||||
let n = 200_000;
|
||||
let ring: Vec<[f64; 2]> = (0..n)
|
||||
.map(|i| {
|
||||
let a = i as f64 / n as f64 * std::f64::consts::TAU;
|
||||
let r = 1000.0 + if i % 2 == 0 { 0.0 } else { 3.0 };
|
||||
[r * a.cos(), r * a.sin()]
|
||||
})
|
||||
.collect();
|
||||
let idx = simplify_idx(&ring, 0.5);
|
||||
assert!(idx.windows(2).all(|w| w[0] < w[1]) && idx.len() >= 3 && *idx.last().unwrap() < n);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fit_uses_the_kept_indices() {
|
||||
// A contour that passes the same point twice: the fit follows the simplifier's index,
|
||||
// not the first raw point with those coordinates.
|
||||
let raw = vec![[0.0, 0.0], [10.0, 0.0], [10.0, 10.0], [0.0, 0.0], [-10.0, 10.0], [-10.0, 0.0]];
|
||||
let idx = vec![0, 2, 3, 4];
|
||||
let simp: Vec<[f64; 2]> = idx.iter().map(|&i| raw[i]).collect();
|
||||
let path = fit_loop(&raw, &idx, &simp, 0.5);
|
||||
assert_eq!(path.vertices, simp);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn traces_a_disc_and_a_square_with_a_hole() {
|
||||
let (w, h) = (120, 80);
|
||||
|
||||
@@ -220,6 +220,56 @@ fn disc(out: &mut BezPath, c: Point, r: f64) {
|
||||
}
|
||||
}
|
||||
|
||||
/// `path` with each curve whose extent is over 10⁷ × `tol` replaced by lines flattened at 10⁻⁷ of
|
||||
/// that extent. `None` when a coordinate is not finite.
|
||||
fn coarsen_huge(path: &BezPath, tol: f64) -> Option<BezPath> {
|
||||
use kurbo::Shape as _;
|
||||
let mut out = BezPath::new();
|
||||
let (mut cur, mut start) = (Point::ZERO, Point::ZERO);
|
||||
for el in path.elements() {
|
||||
let seg = match *el {
|
||||
PathEl::MoveTo(p) => {
|
||||
(cur, start) = (p, p);
|
||||
out.push(*el);
|
||||
continue;
|
||||
}
|
||||
PathEl::LineTo(p) => {
|
||||
cur = p;
|
||||
out.push(*el);
|
||||
continue;
|
||||
}
|
||||
PathEl::ClosePath => {
|
||||
cur = start;
|
||||
out.push(*el);
|
||||
continue;
|
||||
}
|
||||
PathEl::QuadTo(_, p) | PathEl::CurveTo(_, _, p) => {
|
||||
let mut one = BezPath::new();
|
||||
one.move_to(cur);
|
||||
one.push(*el);
|
||||
(one, p)
|
||||
}
|
||||
};
|
||||
let (one, end) = seg;
|
||||
let bb = one.bounding_box();
|
||||
let extent = bb.width().max(bb.height());
|
||||
if !extent.is_finite() {
|
||||
return None;
|
||||
}
|
||||
if extent * 1e-7 > tol {
|
||||
kurbo::flatten(one.elements().iter().copied(), extent * 1e-7, |e| {
|
||||
if let PathEl::LineTo(p) = e {
|
||||
out.line_to(p);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
out.push(*el);
|
||||
}
|
||||
cur = end;
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// Outline of a tapered / waved stroke (local space; `res_scale` = device/local scale).
|
||||
pub fn outline(paths: &[BezPath], style: &StrokeStyle, res_scale: f64) -> Option<BezPath> {
|
||||
if style.width <= 0.0 {
|
||||
@@ -230,7 +280,11 @@ pub fn outline(paths: &[BezPath], style: &StrokeStyle, res_scale: f64) -> Option
|
||||
let half = style.width / 2.0;
|
||||
for path in paths {
|
||||
// Fine enough for the width profile: ½ device pixel, at most ~20 000 samples per path.
|
||||
let rough = flatten(path, 0.1 / rs, f64::INFINITY);
|
||||
// Curves with huge extents are first cut into lines at a tolerance of 10⁻⁷ of their own
|
||||
// extent, so none subdivides into billions of points before the step below applies;
|
||||
// every other segment (small curves in a large path included) keeps the ½-pixel tolerance.
|
||||
let Some(path) = coarsen_huge(path, 0.1 / rs) else { continue };
|
||||
let rough = flatten(&path, 0.1 / rs, f64::INFINITY);
|
||||
for poly0 in rough {
|
||||
let len0 = poly0.len();
|
||||
if len0 <= 0.0 {
|
||||
@@ -396,4 +450,37 @@ mod tests {
|
||||
let area: f32 = m.data.iter().sum();
|
||||
assert!(area > 240.0 * 6.0 * 0.8 && area < 240.0 * 6.0 * 1.05, "{area}");
|
||||
}
|
||||
|
||||
/// A curve with enormous coordinates is approximated within a bounded number of points.
|
||||
#[test]
|
||||
fn huge_coordinates_do_not_explode_the_first_approximation() {
|
||||
let mut p = BezPath::new();
|
||||
p.move_to((0.0, 0.0));
|
||||
p.curve_to((1e13, 0.0), (-1e13, 1e13), (1e13, 1e13));
|
||||
let st = StrokeStyle {
|
||||
width: 4.0,
|
||||
taper: Some(Taper { percent: true, start_len: 50.0, end_len: 50.0, start_width: 0.0, end_width: 0.0, start_ease: 0.0, end_ease: 0.0 }),
|
||||
..Default::default()
|
||||
};
|
||||
let t = std::time::Instant::now();
|
||||
let o = outline(&[p], &st, 1.0).unwrap();
|
||||
assert!(o.elements().len() < 200_000, "{}", o.elements().len());
|
||||
assert!(t.elapsed().as_secs() < 20);
|
||||
}
|
||||
|
||||
/// Small curves keep their shape in a path that also has a huge segment.
|
||||
#[test]
|
||||
fn small_curves_survive_a_huge_segment() {
|
||||
let mut p = BezPath::new();
|
||||
p.move_to((0.0, 0.0));
|
||||
p.curve_to((100.0, 0.0), (0.0, 100.0), (0.0, 0.0));
|
||||
p.line_to((1e10, 0.0));
|
||||
let c = coarsen_huge(&p, 0.1).unwrap();
|
||||
assert_eq!(c.elements().len(), p.elements().len(), "nothing here is huge enough to cut");
|
||||
let mut q = BezPath::new();
|
||||
q.move_to((0.0, 0.0));
|
||||
q.curve_to((1e13, 0.0), (-1e13, 1e13), (1e13, 1e13));
|
||||
let c = coarsen_huge(&q, 0.1).unwrap();
|
||||
assert!(c.elements().iter().skip(1).all(|e| matches!(e, PathEl::LineTo(_))) && c.elements().len() < 10_000);
|
||||
}
|
||||
}
|
||||
+32
-4
@@ -7,6 +7,9 @@ use effectcraft_svg::{Affine, BezPath, FillRule, Geom, Group, Node, Paint, Shape
|
||||
/// PostScript program, or the PDF content stream.
|
||||
pub(crate) const MAX_PATH: usize = 1 << 20;
|
||||
|
||||
/// The deepest the open groups nest (the render tree and its consumers recurse through them).
|
||||
const MAX_DEPTH: usize = 256;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Kind {
|
||||
Plain,
|
||||
@@ -20,6 +23,9 @@ pub(crate) struct Builder {
|
||||
pub top_layers: Vec<bool>,
|
||||
pub skipped: Vec<String>,
|
||||
pub shapes: usize,
|
||||
/// Groups the finished nodes will sit under once placed (an enclosing builder's open groups
|
||||
/// and the pattern or mask groups around them), counted against [`MAX_DEPTH`].
|
||||
base: usize,
|
||||
}
|
||||
|
||||
pub(crate) fn group(name: &str) -> Group {
|
||||
@@ -28,13 +34,25 @@ pub(crate) fn group(name: &str) -> Group {
|
||||
|
||||
impl Builder {
|
||||
pub fn new() -> Builder {
|
||||
Builder { stack: vec![(group("page"), Kind::Plain)], top_layers: vec![], skipped: vec![], shapes: 0 }
|
||||
Builder { stack: vec![(group("page"), Kind::Plain)], top_layers: vec![], skipped: vec![], shapes: 0, base: 0 }
|
||||
}
|
||||
|
||||
/// A builder for content that will be placed inside this one's open groups and `wrap` more
|
||||
/// groups (a pattern's `Pattern` and `Tile`, a soft mask's two), so its nesting limit counts
|
||||
/// them.
|
||||
pub fn nested(&self, wrap: usize) -> Builder {
|
||||
Builder { base: self.base.saturating_add(self.stack.len()).saturating_add(wrap), ..Builder::new() }
|
||||
}
|
||||
|
||||
pub fn depth(&self) -> usize {
|
||||
self.stack.len()
|
||||
}
|
||||
|
||||
/// `wrap` more groups fit under the open ones within [`MAX_DEPTH`].
|
||||
pub fn has_room(&self, wrap: usize) -> bool {
|
||||
self.base.saturating_add(self.stack.len()).saturating_add(wrap) <= MAX_DEPTH
|
||||
}
|
||||
|
||||
pub fn skip(&mut self, what: &str) {
|
||||
if !self.skipped.iter().any(|s| s == what) {
|
||||
self.skipped.push(what.to_string());
|
||||
@@ -62,23 +80,33 @@ impl Builder {
|
||||
}
|
||||
}
|
||||
|
||||
/// Open `g` as the group drawn into from now on; past [`MAX_DEPTH`] the group is dropped
|
||||
/// (what would have been drawn inside it goes to the open group) and noted as skipped.
|
||||
fn push_group(&mut self, g: Group, kind: Kind) {
|
||||
if self.base.saturating_add(self.stack.len()) >= MAX_DEPTH {
|
||||
self.skip("group nesting limit");
|
||||
return;
|
||||
}
|
||||
self.stack.push((g, kind));
|
||||
}
|
||||
|
||||
/// Children drawn from now on are clipped by `path` (page space) until the group closes.
|
||||
pub fn clip(&mut self, path: BezPath, rule: FillRule) {
|
||||
let mut g = group("Clip Group");
|
||||
g.clip.push((path, rule));
|
||||
// Clipping is not a transparency group: blend modes inside reach the backdrop.
|
||||
g.isolated = false;
|
||||
self.stack.push((g, Kind::Clip));
|
||||
self.push_group(g, Kind::Clip);
|
||||
}
|
||||
|
||||
/// Open an optional-content layer (or a named group).
|
||||
pub fn open(&mut self, name: &str, layer: bool) {
|
||||
self.stack.push((group(name), if layer { Kind::Layer } else { Kind::Plain }));
|
||||
self.push_group(group(name), if layer { Kind::Layer } else { Kind::Plain });
|
||||
}
|
||||
|
||||
/// Open a group (blend mode, soft mask) closed with the graphics state.
|
||||
pub fn open_group(&mut self, g: Group) {
|
||||
self.stack.push((g, Kind::Plain));
|
||||
self.push_group(g, Kind::Plain);
|
||||
}
|
||||
|
||||
/// Add a finished node (an image, pattern tiles).
|
||||
|
||||
+24
-2
@@ -401,8 +401,10 @@ fn mode(r: &mut Reader) -> Option<Mode> {
|
||||
/// (index parity: even indices start black runs), and the one after it.
|
||||
fn b1b2(refl: &[usize], a0: i64, white: bool, columns: usize) -> (usize, usize) {
|
||||
let want = if white { 0 } else { 1 };
|
||||
let mut i = 0;
|
||||
while i < refl.len() && (refl[i] as i64 <= a0 || i % 2 != want) {
|
||||
// The changing elements are in order, so the first past `a0` is found by bisection (a scan
|
||||
// from the start made every row quadratic in its transitions).
|
||||
let mut i = refl.partition_point(|&c| c as i64 <= a0);
|
||||
if i % 2 != want {
|
||||
i += 1;
|
||||
}
|
||||
let b1 = refl.get(i).copied().unwrap_or(columns);
|
||||
@@ -804,4 +806,24 @@ pub(crate) mod tests {
|
||||
let _ = decode(&garbage, &Params { k, columns: 1, rows: 0, ..Params::default() }, 1 << 20);
|
||||
}
|
||||
}
|
||||
|
||||
/// `b1b2` by bisection agrees with the scan it replaced, for every position and colour
|
||||
/// over a dense reference row.
|
||||
#[test]
|
||||
fn b1b2_by_bisection_matches_the_scan() {
|
||||
let scan = |refl: &[usize], a0: i64, white: bool, columns: usize| {
|
||||
let want = if white { 0 } else { 1 };
|
||||
let mut i = 0;
|
||||
while i < refl.len() && (refl[i] as i64 <= a0 || i % 2 != want) {
|
||||
i += 1;
|
||||
}
|
||||
(refl.get(i).copied().unwrap_or(columns), refl.get(i + 1).copied().unwrap_or(columns))
|
||||
};
|
||||
let refl: Vec<usize> = (0..64).map(|i| i * 3 / 2).collect();
|
||||
for a0 in -1..100 {
|
||||
for white in [true, false] {
|
||||
assert_eq!(b1b2(&refl, a0, white, 100), scan(&refl, a0, white, 100), "a0 {a0} white {white}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+77
-7
@@ -538,16 +538,31 @@ impl Cff {
|
||||
|
||||
/// A glyph's outline in glyph space (`font_matrix` maps it to text space) and its advance.
|
||||
pub fn outline(&self, gid: u16) -> Option<(BezPath, f64)> {
|
||||
self.outline_depth(gid, 0)
|
||||
self.outline_depth(gid, 0, &mut { MAX_GLYPH_OPS })
|
||||
}
|
||||
|
||||
fn outline_depth(&self, gid: u16, depth: usize) -> Option<(BezPath, f64)> {
|
||||
/// `ops` is the glyph's instruction budget, shared by the base glyph and its seac components.
|
||||
fn outline_depth(&self, gid: u16, depth: usize, ops: &mut usize) -> Option<(BezPath, f64)> {
|
||||
let r = self.charstrings.get(gid as usize)?.clone();
|
||||
let fd = self.fdselect.get(gid as usize).copied().unwrap_or(0) as usize;
|
||||
let fd = fd.min(self.lsubrs.len() - 1);
|
||||
let mut st =
|
||||
T2 { cff: self, fd, path: BezPath::new(), x: 0.0, y: 0.0, stack: vec![], stems: 0, width: None, open: false, done: false, depth: 0, seac: None };
|
||||
let mut st = T2 {
|
||||
cff: self,
|
||||
fd,
|
||||
path: BezPath::new(),
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
stack: vec![],
|
||||
stems: 0,
|
||||
width: None,
|
||||
open: false,
|
||||
done: false,
|
||||
depth: 0,
|
||||
ops: *ops,
|
||||
seac: None,
|
||||
};
|
||||
st.run(&self.data[r]);
|
||||
*ops = st.ops;
|
||||
if st.open {
|
||||
st.path.close_path();
|
||||
}
|
||||
@@ -563,10 +578,10 @@ impl Cff {
|
||||
&& depth < 2
|
||||
{
|
||||
let find = |code: u8| crate::encoding::Base::Standard.name(code).and_then(|n| self.gid_by_name(&n));
|
||||
if let Some((bp, _)) = find(b).and_then(|g| self.outline_depth(g, depth + 1)) {
|
||||
if let Some((bp, _)) = find(b).and_then(|g| self.outline_depth(g, depth + 1, ops)) {
|
||||
path.extend(bp);
|
||||
}
|
||||
if let Some((ap, _)) = find(a).and_then(|g| self.outline_depth(g, depth + 1)) {
|
||||
if let Some((ap, _)) = find(a).and_then(|g| self.outline_depth(g, depth + 1, ops)) {
|
||||
path.extend(kurbo::Affine::translate((adx, ady)) * ap);
|
||||
}
|
||||
}
|
||||
@@ -646,9 +661,16 @@ struct T2<'a> {
|
||||
open: bool,
|
||||
done: bool,
|
||||
depth: usize,
|
||||
/// Instructions this glyph may still run, across its subroutine calls.
|
||||
ops: usize,
|
||||
seac: Option<(f64, f64, u8, u8)>,
|
||||
}
|
||||
|
||||
/// The most charstring instructions one glyph runs: subroutines that branch (two calls each,
|
||||
/// ten levels deep) would otherwise multiply a small font into unbounded work and geometry.
|
||||
/// A real glyph runs a few hundred.
|
||||
pub(crate) const MAX_GLYPH_OPS: usize = 100_000;
|
||||
|
||||
impl T2<'_> {
|
||||
fn move_to(&mut self, dx: f64, dy: f64) {
|
||||
if self.open {
|
||||
@@ -698,6 +720,11 @@ impl T2<'_> {
|
||||
}
|
||||
let mut i = 0;
|
||||
while i < cs.len() && !self.done {
|
||||
let Some(left) = self.ops.checked_sub(1) else {
|
||||
self.done = true;
|
||||
return;
|
||||
};
|
||||
self.ops = left;
|
||||
let b0 = cs[i];
|
||||
i += 1;
|
||||
match b0 {
|
||||
@@ -958,6 +985,12 @@ impl T2<'_> {
|
||||
/// named by custom strings.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn write_test_cff(glyphs: &[(&str, Vec<u8>)]) -> Vec<u8> {
|
||||
write_test_cff_with_gsubrs(glyphs, &[])
|
||||
}
|
||||
|
||||
/// [`write_test_cff`] with global subroutines.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn write_test_cff_with_gsubrs(glyphs: &[(&str, Vec<u8>)], gsubrs: &[Vec<u8>]) -> Vec<u8> {
|
||||
fn idx(items: &[Vec<u8>]) -> Vec<u8> {
|
||||
let mut out = (items.len() as u16).to_be_bytes().to_vec();
|
||||
if items.is_empty() {
|
||||
@@ -983,7 +1016,7 @@ pub(crate) fn write_test_cff(glyphs: &[(&str, Vec<u8>)]) -> Vec<u8> {
|
||||
let names = idx(&[b"Test".to_vec()]);
|
||||
let strings: Vec<Vec<u8>> = glyphs.iter().skip(1).map(|(n, _)| n.as_bytes().to_vec()).collect();
|
||||
let strings_idx = idx(&strings);
|
||||
let gsubrs = idx(&[]);
|
||||
let gsubrs = idx(gsubrs);
|
||||
let cs = idx(&glyphs.iter().map(|(_, c)| c.clone()).collect::<Vec<_>>());
|
||||
// charset format 0: SIDs of glyphs 1.. (custom strings from 391).
|
||||
let mut charset = vec![0u8];
|
||||
@@ -1090,6 +1123,43 @@ mod tests {
|
||||
assert!(p.elements().is_empty());
|
||||
}
|
||||
|
||||
/// A global subroutine that calls itself ten times: 10^10 calls within the depth limit of
|
||||
/// ten. The glyph stops at its instruction budget instead.
|
||||
#[test]
|
||||
fn branching_subroutines_stop_at_the_instruction_budget() {
|
||||
// `-107 callgsubr` (bias 107) is subroutine 0.
|
||||
let call = [32u8, 29];
|
||||
let mut subr = call.repeat(10);
|
||||
subr.push(11);
|
||||
let mut cs = call.to_vec();
|
||||
cs.push(14);
|
||||
let font = write_test_cff_with_gsubrs(&[(".notdef", vec![14]), ("A", cs)], &[subr]);
|
||||
let cff = Cff::parse(&font).unwrap();
|
||||
let (p, _) = cff.outline(cff.gid_by_name("A").unwrap()).unwrap();
|
||||
assert!(p.elements().is_empty());
|
||||
}
|
||||
|
||||
/// The instruction budget is one per glyph: a seac whose base glyph spends all of it leaves
|
||||
/// nothing for the accent, instead of each component getting a fresh budget.
|
||||
#[test]
|
||||
fn seac_components_share_the_instruction_budget() {
|
||||
let call = [32u8, 29];
|
||||
let mut subr = call.repeat(10);
|
||||
subr.push(11);
|
||||
// A square, then the branching subroutine that exhausts the budget.
|
||||
let mut a = vec![139 + 100, 139 + 100, 139 - 100, 6];
|
||||
a.extend(call);
|
||||
a.push(14);
|
||||
// `adx ady bchar achar endchar` with both components 'A' (standard code 65).
|
||||
let seac = vec![139 + 10, 139 + 20, 139 + 65, 139 + 65, 14];
|
||||
let font = write_test_cff_with_gsubrs(&[(".notdef", vec![14]), ("A", a), ("Aacute", seac)], &[subr]);
|
||||
let cff = Cff::parse(&font).unwrap();
|
||||
let (alone, _) = cff.outline(cff.gid_by_name("A").unwrap()).unwrap();
|
||||
let (both, _) = cff.outline(cff.gid_by_name("Aacute").unwrap()).unwrap();
|
||||
assert!(!alone.elements().is_empty());
|
||||
assert_eq!(both.elements().len(), alone.elements().len(), "the accent gets no budget after the base spent it");
|
||||
}
|
||||
|
||||
/// A CFF whose top DICT points its Private DICT at offsets near `usize::MAX`: `size` and `off`
|
||||
/// come from the file, and reading the Private DICT must not overflow `off + size`.
|
||||
#[test]
|
||||
|
||||
+122
-26
@@ -215,19 +215,44 @@ pub enum PsOp {
|
||||
Proc(Vec<PsOp>),
|
||||
}
|
||||
|
||||
fn ps_parse(lx: &mut crate::object::Lexer, depth: usize) -> Vec<PsOp> {
|
||||
/// Parse a procedure. `ops` counts the tokens read (those skipped as unsupported included); past
|
||||
/// [`MAX_CALC_OPS`] the rest is not read, so an oversized program is never fully scanned or built
|
||||
/// (the caller rejects it when `ops` ends past the cap).
|
||||
fn ps_parse(lx: &mut crate::object::Lexer, depth: usize, ops: &mut usize) -> Vec<PsOp> {
|
||||
let mut out = vec![];
|
||||
while let Some(o) = lx.next() {
|
||||
match o {
|
||||
Obj::Num(n) => out.push(PsOp::Num(n)),
|
||||
Obj::Bool(b) => out.push(PsOp::Num(if b { 1.0 } else { 0.0 })),
|
||||
Obj::Op(op) if op == "{" && depth < 32 => out.push(PsOp::Proc(ps_parse(lx, depth + 1))),
|
||||
// Nested too deep: skip the procedure's opening brace (never push it as an operator).
|
||||
Obj::Op(op) if op == "{" => {}
|
||||
Obj::Op(op) if op == "}" => return out,
|
||||
Obj::Op(op) => out.push(PsOp::Op(op)),
|
||||
_ => {}
|
||||
loop {
|
||||
// A calculator holds no arrays or dictionaries: one is skipped as a single token without
|
||||
// being built (the lexer would allocate every element before this counted it).
|
||||
if lx.at_compound() {
|
||||
if *ops > MAX_CALC_OPS {
|
||||
break;
|
||||
}
|
||||
lx.skip_compound();
|
||||
*ops += 1;
|
||||
continue;
|
||||
}
|
||||
let Some(o) = lx.next() else { break };
|
||||
if *ops > MAX_CALC_OPS {
|
||||
break;
|
||||
}
|
||||
let op = match o {
|
||||
Obj::Num(n) => PsOp::Num(n),
|
||||
Obj::Bool(b) => PsOp::Num(if b { 1.0 } else { 0.0 }),
|
||||
Obj::Op(op) if op == "{" && depth < 32 => PsOp::Proc(ps_parse(lx, depth + 1, ops)),
|
||||
// Nested too deep: skip the procedure's opening brace (never push it as an operator).
|
||||
Obj::Op(op) if op == "{" => {
|
||||
*ops += 1;
|
||||
continue;
|
||||
}
|
||||
Obj::Op(op) if op == "}" => return out,
|
||||
Obj::Op(op) => PsOp::Op(op),
|
||||
_ => {
|
||||
*ops += 1;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
*ops += 1;
|
||||
out.push(op);
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -243,6 +268,15 @@ fn ps_push(st: &mut Vec<f64>, v: f64) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The most operators and operands a calculator program may hold (nested procedures included).
|
||||
/// It has no loops, so this bounds what one evaluation runs; real programs hold a few dozen.
|
||||
const MAX_CALC_OPS: usize = 4096;
|
||||
|
||||
/// The operators and operands in `code`, nested procedures included.
|
||||
fn ps_len(code: &[PsOp]) -> usize {
|
||||
code.iter().map(|op| if let PsOp::Proc(p) = op { 1 + ps_len(p) } else { 1 }).sum()
|
||||
}
|
||||
|
||||
fn ps_run(code: &[PsOp], st: &mut Vec<f64>, depth: usize) {
|
||||
if depth > 32 {
|
||||
return;
|
||||
@@ -431,6 +465,12 @@ impl Budget {
|
||||
Budget { nodes: MAX_NODES, samples: MAX_SAMPLES, overrun: false }
|
||||
}
|
||||
|
||||
/// Charge `n` retained values (coefficients, bounds, program operators) as nodes, eight to a
|
||||
/// node, so a cached copy of a function is charged for what it holds, not just its node.
|
||||
fn take_values(&mut self, n: usize) -> bool {
|
||||
self.take_nodes(n.div_ceil(8))
|
||||
}
|
||||
|
||||
/// Charge `n` nodes; `false` (and the read is abandoned) once the cap is passed.
|
||||
fn take_nodes(&mut self, n: usize) -> bool {
|
||||
match self.nodes.checked_sub(n) {
|
||||
@@ -484,7 +524,8 @@ impl Func {
|
||||
/// [`Func::read`] nested `depth` levels deep, charging `budget`: unsupported past [`MAX_DEPTH`]
|
||||
/// or once the budget is spent.
|
||||
fn read_at(file: &File, o: &Obj, depth: usize, budget: &mut Budget) -> Func {
|
||||
if depth >= MAX_DEPTH || !budget.take_nodes(1) {
|
||||
// Charged first: the fallback past the depth limit is a node a copy keeps too.
|
||||
if !budget.take_nodes(1) || depth >= MAX_DEPTH {
|
||||
return Func::Unsupported;
|
||||
}
|
||||
let o = file.resolve(o);
|
||||
@@ -499,12 +540,14 @@ impl Func {
|
||||
let dom = file.get(d, "Domain").map(|x| file.nums(x)).unwrap_or_default();
|
||||
let domain = [dom.first().copied().unwrap_or(0.0), dom.get(1).copied().unwrap_or(1.0)];
|
||||
match file.get_num(d, "FunctionType").unwrap_or(-1.0) as i32 {
|
||||
2 => Func::Exp {
|
||||
domain,
|
||||
c0: file.get(d, "C0").map(|x| file.nums(x)).unwrap_or_else(|| vec![0.0]),
|
||||
c1: file.get(d, "C1").map(|x| file.nums(x)).unwrap_or_else(|| vec![1.0]),
|
||||
n: file.get_num(d, "N").unwrap_or(1.0),
|
||||
},
|
||||
2 => {
|
||||
let c0 = file.get(d, "C0").map(|x| file.nums(x)).unwrap_or_else(|| vec![0.0]);
|
||||
let c1 = file.get(d, "C1").map(|x| file.nums(x)).unwrap_or_else(|| vec![1.0]);
|
||||
if !budget.take_values(c0.len() + c1.len()) {
|
||||
return Func::Unsupported;
|
||||
}
|
||||
Func::Exp { domain, c0, c1, n: file.get_num(d, "N").unwrap_or(1.0) }
|
||||
}
|
||||
3 => {
|
||||
// A stitching function's pieces: no element cap (a repeating gradient lists one
|
||||
// piece once per repeat); the budget bounds the total work.
|
||||
@@ -513,12 +556,12 @@ impl Func {
|
||||
None => Some(vec![]),
|
||||
};
|
||||
let Some(funcs) = funcs else { return Func::Unsupported };
|
||||
Func::Stitch {
|
||||
domain,
|
||||
funcs,
|
||||
bounds: file.get(d, "Bounds").map(|x| file.nums(x)).unwrap_or_default(),
|
||||
encode: file.get(d, "Encode").map(|x| file.nums(x)).unwrap_or_default(),
|
||||
let bounds = file.get(d, "Bounds").map(|x| file.nums(x)).unwrap_or_default();
|
||||
let encode = file.get(d, "Encode").map(|x| file.nums(x)).unwrap_or_default();
|
||||
if !budget.take_values(bounds.len() + encode.len()) {
|
||||
return Func::Unsupported;
|
||||
}
|
||||
Func::Stitch { domain, funcs, bounds, encode }
|
||||
}
|
||||
0 => {
|
||||
let Obj::Stream(sd, raw) = o else { return Func::Unsupported };
|
||||
@@ -537,6 +580,10 @@ impl Func {
|
||||
if !budget.take_samples(count) {
|
||||
return Func::Unsupported;
|
||||
}
|
||||
// The stream must hold the table: missing bytes are not made up as zeros.
|
||||
if count.checked_mul(bps).is_none_or(|bits| bits.div_ceil(8) > data.len()) {
|
||||
return Func::Unsupported;
|
||||
}
|
||||
let enc = file.get(d, "Encode").map(|x| file.nums(x)).unwrap_or_default();
|
||||
let encode = [enc.first().copied().unwrap_or(0.0), enc.get(1).copied().unwrap_or(size as f64 - 1.0)];
|
||||
let decode = file.get(d, "Decode").map(|x| file.nums(x)).filter(|v| v.len() >= outputs * 2).unwrap_or_else(|| range.clone());
|
||||
@@ -567,10 +614,15 @@ impl Func {
|
||||
let Some(data) = decode_stream(file, sd, raw) else { return Func::Unsupported };
|
||||
let mut lx = crate::object::Lexer::new(&data, 0);
|
||||
// The program is one procedure: `{ … }`.
|
||||
let code = match ps_parse(&mut lx, 0).into_iter().next() {
|
||||
let mut scanned = 0;
|
||||
let code = match ps_parse(&mut lx, 0, &mut scanned).into_iter().next() {
|
||||
Some(PsOp::Proc(p)) => p,
|
||||
_ => return Func::Unsupported,
|
||||
};
|
||||
let ops = ps_len(&code);
|
||||
if scanned > MAX_CALC_OPS || ops > MAX_CALC_OPS || !budget.take_values(ops) {
|
||||
return Func::Unsupported;
|
||||
}
|
||||
Func::Calc { domain: dom, range: file.get(d, "Range").map(|x| file.nums(x)).unwrap_or_default(), code }
|
||||
}
|
||||
_ => Func::Unsupported,
|
||||
@@ -756,12 +808,56 @@ mod tests {
|
||||
assert_eq!(sample_count(1 << 22, usize::MAX / (1 << 22) + 1), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_calculator_program_is_cut_short() {
|
||||
let src = format!("{{ {}}}", "1 ".repeat(MAX_CALC_OPS + 10));
|
||||
let mut lx = crate::object::Lexer::new(src.as_bytes(), 0);
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() };
|
||||
assert_eq!(ps_len(&code), MAX_CALC_OPS + 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arrays_and_dictionaries_in_a_calculator_are_skipped_whole() {
|
||||
// Strings with brackets, nested arrays and a dictionary are one skipped token each; the
|
||||
// operators around them still parse.
|
||||
let src = b"{ 1 [ (a]b) [1 2] << /K [3 4] /S (>>) >> 5 ] 2 add << /A 1 >> mul }";
|
||||
let mut lx = crate::object::Lexer::new(src, 0);
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() };
|
||||
assert_eq!(code, vec![PsOp::Num(1.0), PsOp::Num(2.0), PsOp::Op("add".into()), PsOp::Op("mul".into())]);
|
||||
// A huge array is skipped once the cap is passed, not read to its end.
|
||||
let src = format!("{{ [ {}] 1 }}", "0 ".repeat(MAX_CALC_OPS * 100));
|
||||
let mut lx = crate::object::Lexer::new(src.as_bytes(), 0);
|
||||
let mut scanned = 0;
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut scanned).into_iter().next() else { panic!() };
|
||||
assert_eq!(code, vec![PsOp::Num(1.0)]);
|
||||
// A large string inside a skipped array is passed over, not decoded: escapes, nested
|
||||
// parentheses, a hexadecimal string and names all end where the lexer would end them.
|
||||
let big = format!("{{ [ ({}) <{}> /{} (a\\)b(c)) 7 ] 1 }}", "x".repeat(1 << 20), "ab".repeat(1 << 19), "N".repeat(1 << 10));
|
||||
let mut lx = crate::object::Lexer::new(big.as_bytes(), 0);
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() };
|
||||
assert_eq!(code, vec![PsOp::Num(1.0)]);
|
||||
// An unclosed one ends at the end of the data.
|
||||
let mut lx = crate::object::Lexer::new(b"[ 1 2 ", 0);
|
||||
lx.skip_compound();
|
||||
assert_eq!(lx.pos, 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skipped_calculator_tokens_count_toward_the_cap() {
|
||||
// `null` is not a calculator token: skipped, but still scanned.
|
||||
let src = format!("{{ {}}}", "null ".repeat(MAX_CALC_OPS + 10));
|
||||
let mut lx = crate::object::Lexer::new(src.as_bytes(), 0);
|
||||
let mut scanned = 0;
|
||||
ps_parse(&mut lx, 0, &mut scanned);
|
||||
assert!(scanned > MAX_CALC_OPS && scanned < MAX_CALC_OPS + 10, "parsing stops once the cap is passed ({scanned})");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn calculator_functions() {
|
||||
// Two inputs → three outputs: { 2 copy mul 3 1 roll add 2 div dup 0.5 gt { pop 1 } if 0 }
|
||||
let code = b"{ 2 copy mul 3 1 roll add 2 div dup 0.5 gt { pop 1 } { 0.25 mul } ifelse 0 }";
|
||||
let mut lx = crate::object::Lexer::new(code, 0);
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0).into_iter().next() else { panic!() };
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() };
|
||||
let f = Func::Calc { domain: vec![0.0, 1.0, 0.0, 1.0], range: vec![0.0, 1.0, 0.0, 1.0, 0.0, 1.0], code };
|
||||
assert_eq!(f.eval(&[0.5, 0.4]), vec![0.2, 0.1125, 0.0]);
|
||||
assert_eq!(f.eval(&[1.0, 0.8]), vec![0.8, 1.0, 0.0]);
|
||||
@@ -771,7 +867,7 @@ mod tests {
|
||||
fn run(src: &str, mut st: Vec<f64>) -> Vec<f64> {
|
||||
let src = format!("{{ {src} }}");
|
||||
let mut lx = crate::object::Lexer::new(src.as_bytes(), 0);
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0).into_iter().next() else { panic!() };
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() };
|
||||
ps_run(&code, &mut st, 0);
|
||||
st
|
||||
}
|
||||
|
||||
+16
-1
@@ -211,6 +211,12 @@ fn utf16(b: &[u8]) -> String {
|
||||
String::from_utf16_lossy(&u)
|
||||
}
|
||||
|
||||
/// The most code → text mappings a CMap keeps (more than Unicode has code points), and the
|
||||
/// longest destination string a `bfrange` is expanded with: a range of 65,536 codes would
|
||||
/// otherwise multiply a long destination into gigabytes of text.
|
||||
const MAX_UNICODE_MAPPINGS: usize = 1 << 20;
|
||||
const MAX_RANGE_DST: usize = 256;
|
||||
|
||||
fn parse_cmap(data: &[u8]) -> CMap {
|
||||
let mut m = CMap::default();
|
||||
let mut lx = Lexer::new(data, 0);
|
||||
@@ -239,6 +245,9 @@ fn parse_cmap(data: &[u8]) -> CMap {
|
||||
}
|
||||
"bfchar" => {
|
||||
for p in a.as_chunks::<2>().0 {
|
||||
if m.unicode.len() >= MAX_UNICODE_MAPPINGS {
|
||||
break;
|
||||
}
|
||||
if let (Obj::Str(src), Obj::Str(dst)) = (&p[0], &p[1]) {
|
||||
m.unicode.insert(be(src), utf16(dst));
|
||||
}
|
||||
@@ -252,9 +261,12 @@ fn parse_cmap(data: &[u8]) -> CMap {
|
||||
continue;
|
||||
}
|
||||
match &p[2] {
|
||||
Obj::Str(dst) => {
|
||||
Obj::Str(dst) if dst.len() <= MAX_RANGE_DST => {
|
||||
let mut d = dst.clone();
|
||||
for c in lo..=hi {
|
||||
if m.unicode.len() >= MAX_UNICODE_MAPPINGS {
|
||||
break;
|
||||
}
|
||||
m.unicode.insert(c, utf16(&d));
|
||||
if let Some(last) = d.last_mut() {
|
||||
*last = last.wrapping_add(1);
|
||||
@@ -263,6 +275,9 @@ fn parse_cmap(data: &[u8]) -> CMap {
|
||||
}
|
||||
Obj::Array(list) => {
|
||||
for (k, s) in list.iter().enumerate() {
|
||||
if m.unicode.len() >= MAX_UNICODE_MAPPINGS {
|
||||
break;
|
||||
}
|
||||
if let (Obj::Str(s), Some(c)) = (s, lo.checked_add(k as u32)) {
|
||||
m.unicode.insert(c, utf16(s));
|
||||
}
|
||||
|
||||
@@ -82,6 +82,91 @@ impl<'a> Lexer<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the next token opens an array or a dictionary (skipping white space first).
|
||||
pub fn at_compound(&mut self) -> bool {
|
||||
self.skip_ws();
|
||||
match self.data.get(self.pos) {
|
||||
Some(b'[') => true,
|
||||
Some(b'<') => self.data.get(self.pos + 1) == Some(&b'<'),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Skip the array or dictionary at the current position (see [`Lexer::at_compound`]) as
|
||||
/// [`Lexer::next`] would read it, without building it: for readers that have no use for one
|
||||
/// and must not allocate a vector per element of a hostile stream.
|
||||
pub fn skip_compound(&mut self) {
|
||||
let mut open = 0usize;
|
||||
loop {
|
||||
self.skip_ws();
|
||||
match self.data.get(self.pos) {
|
||||
None => return,
|
||||
Some(b'[') => {
|
||||
open += 1;
|
||||
self.pos += 1;
|
||||
}
|
||||
Some(b'<') if self.data.get(self.pos + 1) == Some(&b'<') => {
|
||||
open += 1;
|
||||
self.pos += 2;
|
||||
}
|
||||
Some(b']') | Some(b'>') if self.data[self.pos] == b']' || self.data.get(self.pos + 1) == Some(&b'>') => {
|
||||
open = open.saturating_sub(1);
|
||||
self.pos += if self.data[self.pos] == b']' { 1 } else { 2 };
|
||||
if open == 0 {
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Anything else is one token (a string, name, number or word), passed over
|
||||
// without building it.
|
||||
Some(_) => self.skip_scalar(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Pass over one token that is not an array or dictionary bracket, as [`Lexer::next`] reads
|
||||
/// it but without building its value.
|
||||
fn skip_scalar(&mut self) {
|
||||
let Some(&c) = self.data.get(self.pos) else { return };
|
||||
match c {
|
||||
b'(' => {
|
||||
// A literal string: balanced parentheses, a backslash escapes the next byte.
|
||||
let mut depth = 0usize;
|
||||
while let Some(&d) = self.data.get(self.pos) {
|
||||
self.pos += 1;
|
||||
match d {
|
||||
b'\\' => self.pos += 1,
|
||||
b'(' => depth += 1,
|
||||
b')' => {
|
||||
depth -= 1;
|
||||
if depth == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
self.pos = self.pos.min(self.data.len());
|
||||
}
|
||||
b'<' => {
|
||||
// A hexadecimal string.
|
||||
while self.data.get(self.pos).is_some_and(|d| *d != b'>') {
|
||||
self.pos += 1;
|
||||
}
|
||||
self.pos = (self.pos + 1).min(self.data.len());
|
||||
}
|
||||
b'/' => {
|
||||
self.pos += 1;
|
||||
self.word();
|
||||
}
|
||||
b']' | b'{' | b'}' | b')' | b'>' => self.pos += 1,
|
||||
_ => {
|
||||
if self.word().is_empty() {
|
||||
self.pos += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn word(&mut self) -> &'a [u8] {
|
||||
let s = self.pos;
|
||||
while self.pos < self.data.len() && !is_white(self.data[self.pos]) && !is_delim(self.data[self.pos]) {
|
||||
|
||||
+14
-5
@@ -669,7 +669,12 @@ impl<'a> Interp<'a> {
|
||||
let bc = f.get(&sm, "BC").map(|b| f.nums(b)).unwrap_or_default();
|
||||
let gcs = f.get_dict(&gd, "Group").and_then(|gr| f.get(gr, "CS")).map(|c| color_space(f, c, Some(res))).unwrap_or(Cs::Rgb);
|
||||
let backdrop = if bc.is_empty() { [0.0; 3] } else { gcs.to_rgb(&bc) };
|
||||
let saved_b = std::mem::replace(&mut self.b, Builder::new());
|
||||
// The mask it replaces closes first: its groups don't enclose the new one.
|
||||
if let Some(d) = self.gs.mask_depth {
|
||||
self.close_to(d);
|
||||
}
|
||||
let inner = self.b.nested(2);
|
||||
let saved_b = std::mem::replace(&mut self.b, inner);
|
||||
let ctm = self.gs.ctm;
|
||||
let saved_gs = std::mem::replace(&mut self.gs, GState { ctm, ..GState::default() });
|
||||
let saved_stack = std::mem::take(&mut self.saved);
|
||||
@@ -681,9 +686,6 @@ impl<'a> Interp<'a> {
|
||||
for s in sk {
|
||||
self.b.skip(&s);
|
||||
}
|
||||
if let Some(d) = self.gs.mask_depth {
|
||||
self.close_to(d);
|
||||
}
|
||||
let mut content = Group::new("Soft Mask");
|
||||
content.children = nodes;
|
||||
let mut grp = Group::new("Soft Mask Group");
|
||||
@@ -784,8 +786,15 @@ impl<'a> Interp<'a> {
|
||||
if depth > 8 {
|
||||
return;
|
||||
}
|
||||
// The `Pattern` and `Tile` wrappers must fit under the open groups too.
|
||||
if !self.b.has_room(2) {
|
||||
self.b.skip("group nesting limit");
|
||||
return;
|
||||
}
|
||||
// The cell, drawn once in pattern space.
|
||||
let saved_b = std::mem::replace(&mut self.b, Builder::new());
|
||||
// The cell ends up under the open groups and this pattern's `Pattern` and `Tile`.
|
||||
let inner = self.b.nested(2);
|
||||
let saved_b = std::mem::replace(&mut self.b, inner);
|
||||
let saved_gs = std::mem::take(&mut self.gs);
|
||||
let saved_stack = std::mem::take(&mut self.saved);
|
||||
let saved_path = std::mem::take(&mut self.path);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use super::eps::find_from;
|
||||
use super::exec::MAX_STACK;
|
||||
use super::scanner::Src;
|
||||
use super::text::encoding_array;
|
||||
use super::value::{Flow, V, array, matrix_of, matrix_val, new_dict};
|
||||
@@ -181,8 +182,13 @@ impl Interp {
|
||||
match self.pop() {
|
||||
V::Num(k) => {
|
||||
let k = (k.max(0.0) as usize).min(self.st.len());
|
||||
let tail: Vec<V> = self.st[self.st.len() - k..].to_vec();
|
||||
self.st.extend(tail);
|
||||
// A copy past the operand limit copies nothing, and the values copied
|
||||
// count as operations (one for eight): repeated doubling spends the budget.
|
||||
if self.st.len().saturating_add(k) <= MAX_STACK {
|
||||
self.charge(k / 8);
|
||||
let tail: Vec<V> = self.st[self.st.len() - k..].to_vec();
|
||||
self.st.extend(tail);
|
||||
}
|
||||
}
|
||||
V::Array(dst) => {
|
||||
let s = self.pop().items().unwrap_or_default();
|
||||
@@ -507,12 +513,16 @@ impl Interp {
|
||||
fn try_collection_op(&mut self, n: &str) -> bool {
|
||||
match n {
|
||||
// Arrays and strings.
|
||||
// The storage made counts as operations (a value, or eight bytes, each), so a loop
|
||||
// of large constructors spends the budget rather than the memory.
|
||||
"array" => {
|
||||
let k = self.popn().clamp(0.0, 1e6) as usize;
|
||||
self.charge(k);
|
||||
self.push(array(vec![V::Null; k]));
|
||||
}
|
||||
"string" => {
|
||||
let k = self.popn().clamp(0.0, 1e7) as usize;
|
||||
self.charge(k / 8);
|
||||
self.push(V::Str(Rc::new(RefCell::new(vec![0; k]))));
|
||||
}
|
||||
"length" => {
|
||||
|
||||
@@ -19,7 +19,8 @@ pub fn eps_parts(bytes: &[u8]) -> Option<(&[u8], [f64; 4])> {
|
||||
if v.len() == 4 && v[2] > v[0] && v[3] > v[1] {
|
||||
return Some([v[0], v[1], v[2], v[3]]);
|
||||
}
|
||||
from = s;
|
||||
// A rejected comment is done with: the scan goes on past its line, not over it again.
|
||||
from = e;
|
||||
}
|
||||
None
|
||||
};
|
||||
@@ -31,8 +32,15 @@ pub(super) fn find_from(hay: &[u8], needle: &[u8], from: usize) -> Option<usize>
|
||||
hay.get(from..)?.windows(needle.len()).position(|w| w == needle).map(|p| p + from)
|
||||
}
|
||||
|
||||
fn rfind_before(hay: &[u8], needle: &[u8], to: usize) -> Option<usize> {
|
||||
hay.get(..to)?.windows(needle.len()).rposition(|w| w == needle)
|
||||
/// Every position of `needle` in `hay`, in order (one pass over the file).
|
||||
fn positions(hay: &[u8], needle: &[u8]) -> Vec<usize> {
|
||||
let mut out = vec![];
|
||||
let mut from = 0;
|
||||
while let Some(p) = find_from(hay, needle, from) {
|
||||
out.push(p);
|
||||
from = p + needle.len();
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The name after `key` (`/FontName /Name`).
|
||||
@@ -48,20 +56,26 @@ fn name_after(b: &[u8], key: &[u8]) -> Option<String> {
|
||||
/// … cleartomark`, hexadecimal or binary) and CFF font sets (`… <length> StartData <bytes>`).
|
||||
pub(super) fn embedded_fonts(ps: &[u8]) -> HashMap<String, (Rc<Vec<u8>>, bool)> {
|
||||
let mut out = HashMap::new();
|
||||
// The headers are found once, in one pass each; every `eexec` then takes the last header
|
||||
// before it that no earlier `eexec` has used, so no prefix of the file is searched again.
|
||||
let mut heads = positions(ps, b"%!PS-AdobeFont");
|
||||
heads.extend(positions(ps, b"%!FontType1"));
|
||||
heads.sort_unstable();
|
||||
let names = positions(ps, b"/FontName");
|
||||
let mut from = 0;
|
||||
let mut used = 0;
|
||||
while let Some(ee) = find_from(ps, b"eexec", from) {
|
||||
from = ee + 5;
|
||||
let start = [b"%!PS-AdobeFont".as_slice(), b"%!FontType1"]
|
||||
.iter()
|
||||
.filter_map(|h| rfind_before(ps, h, ee))
|
||||
.max()
|
||||
.or_else(|| rfind_before(ps, b"/FontName", ee).map(|p| p.saturating_sub(512)));
|
||||
let last_before = |v: &[usize]| v[..v.partition_point(|&p| p < ee)].last().copied().filter(|&p| p >= used);
|
||||
// The fallback look-back stops at the previous font's end, so it cannot pick up its header.
|
||||
let start = last_before(&heads).or_else(|| last_before(&names).map(|p| p.saturating_sub(512).max(used)));
|
||||
let Some(start) = start else { continue };
|
||||
let end = find_from(ps, b"cleartomark", ee).map_or(ps.len(), |p| p + 11);
|
||||
if let Some(name) = name_after(&ps[start..ee], b"/FontName") {
|
||||
out.insert(name, (Rc::new(ps[start..end].to_vec()), false));
|
||||
}
|
||||
from = end;
|
||||
used = end;
|
||||
}
|
||||
let mut from = 0;
|
||||
while let Some(sd) = find_from(ps, b"StartData", from) {
|
||||
@@ -77,3 +91,30 @@ pub(super) fn embedded_fonts(ps: &[u8]) -> HashMap<String, (Rc<Vec<u8>>, bool)>
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::embedded_fonts;
|
||||
|
||||
#[test]
|
||||
fn font_discovery_looks_back_once_per_header() {
|
||||
// 20,000 `eexec` words before any font header: each used to search the whole prefix
|
||||
// for a header again. The font after them is still found.
|
||||
let mut ps = b"%!PS-Adobe-3.0 EPSF-3.0\n".to_vec();
|
||||
ps.extend_from_slice("eexec ".repeat(20_000).as_bytes());
|
||||
ps.extend_from_slice(b"\n%!PS-AdobeFont-1.0: Real\n/FontName /Real def\ncurrentfile eexec\n0000\ncleartomark\n");
|
||||
let fonts = embedded_fonts(&ps);
|
||||
assert_eq!(fonts.len(), 1);
|
||||
assert!(fonts.contains_key("Real"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn header_less_font_after_a_short_one_does_not_take_its_header() {
|
||||
let a = "%!PS-AdobeFont-1.0: A\n/FontName /A def\ncurrentfile eexec\n0000\ncleartomark\n";
|
||||
let b = "/FontName /B def\ncurrentfile eexec\n0000\ncleartomark\n";
|
||||
let ps = format!("%!PS-Adobe-3.0 EPSF-3.0\n{a}{b}");
|
||||
let fonts = embedded_fonts(ps.as_bytes());
|
||||
let (data, _) = fonts.get("B").expect("second font found");
|
||||
assert!(!String::from_utf8_lossy(data).contains("AdobeFont"), "{}", String::from_utf8_lossy(data));
|
||||
}
|
||||
}
|
||||
Loaded 100 of 171 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user