Merge remote-tracking branch 'upstream/main' into codex/effectcraft-eps-shared-cutover

# Conflicts:
#	Cargo.toml
This commit is contained in:
FredApps committed 2026-11-16 08:28:12 +01:00
commit 790b065d53
13 files changed
+280 -393

No files matched your search

Generated
+37 -2
View File
@@ -1103,6 +1103,15 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-cms"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=9fd926dc93be7d4723e6eb4f484c7233d91d3e94#9fd926dc93be7d4723e6eb4f484c7233d91d3e94"
dependencies = [
"md-5",
"rayon",
]
[[package]]
name = "craft-codepage"
version = "0.1.0"
@@ -1153,9 +1162,13 @@ source = "git+https://github.com/storytold/craft-libs?rev=808730f49b9637b7909fe2
[[package]]
name = "craft-exr"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9"
source = "git+https://github.com/storytold/craft-libs?rev=bc3e521bd37e53f2b5b4c55278111b55fd9a40f9#bc3e521bd37e53f2b5b4c55278111b55fd9a40f9"
dependencies = [
"exr",
"flate2",
"half",
"serde_json",
"thiserror 2.0.21",
]
[[package]]
@@ -1194,8 +1207,9 @@ dependencies = [
[[package]]
name = "craft-lottie"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9"
source = "git+https://github.com/storytold/craft-libs?rev=2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f#2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f"
dependencies = [
"craft-zip",
"serde_json",
"thiserror 2.0.21",
]
@@ -1387,6 +1401,16 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f#2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f"
dependencies = [
"flate2",
"thiserror 2.0.21",
"zip 9.1.3",
]
[[package]]
name = "crc32fast"
version = "1.5.2"
@@ -1691,6 +1715,7 @@ name = "effectcraft-effects"
version = "0.24.1"
dependencies = [
"chrono",
"craft-cms",
"effectcraft-color",
"effectcraft-geom",
"effectcraft-keyframe",
@@ -4095,6 +4120,16 @@ dependencies = [
"rawpointer",
]
[[package]]
name = "md-5"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
dependencies = [
"cfg-if",
"digest",
]
[[package]]
name = "memchr"
version = "2.8.3"
+2 -2
View File
@@ -18,12 +18,12 @@ craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs
craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-ps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", default-features = false, features = ["read"] }
craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "bc3e521bd37e53f2b5b4c55278111b55fd9a40f9" }
craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f" }
craft-mesh = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", default-features = false, features = ["stl", "obj", "gltf", "fbx"] }
craft-aep = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-doc = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
+1
View File
@@ -10,6 +10,7 @@ rust-version.workspace = true
workspace = true
[dependencies]
craft-cms = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "9fd926dc93be7d4723e6eb4f484c7233d91d3e94" }
effectcraft-geom = { workspace = true }
effectcraft-color = { workspace = true }
effectcraft-keyframe = { workspace = true }
+2 -33
View File
@@ -35,39 +35,8 @@ pub fn parse_spimtx(text: &str) -> Option<FileXform> {
/// Parse a Sony Imageworks `.spi1d` 1D LUT: `From <min> <max>`, `Length <n>`, `Components
/// <1 | 3>`, then `{` the values `}`.
pub fn parse_spi1d(text: &str) -> Option<Lut> {
let (mut from, mut len, mut comps) = ([0.0f32, 1.0], 0usize, 1usize);
let mut values: Vec<f32> = vec![];
let mut body = false;
for line in text.lines().map(str::trim).filter(|l| !l.is_empty()) {
if body {
if line.starts_with('}') {
break;
}
for t in line.split_whitespace() {
values.push(t.parse().ok()?);
}
continue;
}
let mut t = line.split_whitespace();
match t.next()? {
"{" => body = true,
"From" => from = [t.next()?.parse().ok()?, t.next()?.parse().ok()?],
"Length" => len = t.next()?.parse().ok()?,
"Components" => comps = t.next()?.parse().ok()?,
_ => {}
}
}
// A 1D table is interpolated by index, so cap it like the lattices (2²⁴ entries).
if !(2..=MAX_1D).contains(&len)
|| !matches!(comps, 1 | 3)
|| values.len() != len.checked_mul(comps)?
|| from[1] <= from[0]
|| from.iter().any(|v| !v.is_finite())
{
return None;
}
let d1 = values.chunks_exact(comps).map(|c| if comps == 3 { [c[0], c[1], c[2]] } else { [c[0]; 3] }).collect();
Some(Lut { n1: len, d1, n3: 0, d3: vec![], dmin: [from[0]; 3], dmax: [from[1]; 3] })
let table = craft_cms::spi1d::parse(text)?;
Some(Lut { n1: table.values.len(), d1: table.values, n3: 0, d3: vec![], dmin: [table.domain[0]; 3], dmax: [table.domain[1]; 3] })
}
/// Parse a Sony Imageworks `.spi3d` lattice: `SPILUT 1.0`, `3 3`, the sizes, then one
+7
View File
@@ -116,6 +116,13 @@ fn spi_files_parse() {
assert!((back[0] - 1.0).abs() < 1e-5 && (back[2] - 2.0).abs() < 1e-5, "{back:?}");
let three = parse_spi1d("Version 1\nFrom 0 1\nLength 2\nComponents 3\n{\n0 0 0\n1 0.5 2\n}\n").unwrap();
assert_eq!(three.d1, vec![[0.0; 3], [1.0, 0.5, 2.0]]);
let eof = parse_spi1d("Length 2\nFrom -1 2\n{\n0 1").unwrap();
assert_eq!(eof.n1, 2);
assert_eq!(eof.dmin, [-1.0; 3]);
assert_eq!(eof.dmax, [2.0; 3]);
assert_eq!(eof.n3, 0);
assert!(eof.d3.is_empty());
assert!(parse_spi1d("Length 2\n{\n0 1 2").is_none());
let mut spi3d = String::from("SPILUT 1.0\n3 3\n2 2 2\n");
for (r, g, b) in (0..8).map(|i| (i & 1, (i >> 1) & 1, i >> 2)) {
spi3d += &format!("{r} {g} {b} {} {} {}\n", r as f32 * 0.5, g, b);
+1 -1
View File
@@ -17,7 +17,7 @@ fn export_lottie(s: &mut Session, p: &Value) -> Result<Value> {
let dot = path.to_ascii_lowercase().ends_with(".lottie");
let bytes = if dot {
let stem = std::path::Path::new(&path).file_stem().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
effectcraft_lottie::to_dotlottie(&res, &stem)
effectcraft_lottie::try_to_dotlottie(&res, &stem).map_err(|e| EngineError::Other(e.to_string()))?
} else {
res.to_string_compact().into_bytes()
};
+9 -3
View File
@@ -505,11 +505,17 @@ pub fn export_comp(project: &Project, comp: ItemId, opts: &ExportOptions, read:
/// Package an export as a dotLottie archive (`manifest.json` + `animations/<id>.json`).
pub fn to_dotlottie(res: &ExportResult, id: &str) -> Vec<u8> {
try_to_dotlottie(res, id).unwrap_or_default()
}
/// Package an export, reporting serialization or ZIP failures before any output is written.
pub fn try_to_dotlottie(res: &ExportResult, id: &str) -> Result<Vec<u8>, LottieError> {
let id: String = id.chars().map(|c| if c.is_ascii_alphanumeric() || c == '_' || c == '-' { c } else { '_' }).collect();
let id = if id.is_empty() { "animation".to_string() } else { id };
let manifest = json!({"version": "1", "generator": "EffectCraft", "author": "", "animations": [{"id": id, "speed": 1, "loop": true}]});
crate::zip::store(&[
("manifest.json".into(), serde_json::to_vec(&manifest).unwrap_or_default()),
(format!("animations/{id}.json"), serde_json::to_vec(&res.json).unwrap_or_default()),
crate::zip::try_store(&[
("manifest.json".into(), serde_json::to_vec(&manifest).map_err(|e| LottieError::Invalid(e.to_string()))?),
(format!("animations/{id}.json"), serde_json::to_vec(&res.json).map_err(|e| LottieError::Invalid(e.to_string()))?),
])
.map_err(|e| LottieError::Invalid(format!("dotLottie ZIP: {e}")))
}
+4 -4
View File
@@ -41,7 +41,7 @@ pub struct ImportResult {
pub warnings: Vec<String>,
}
/// The Lottie document inside raw bytes (plain JSON, or a stored dotLottie archive), read into
/// The Lottie document inside raw bytes (plain JSON, or a dotLottie archive), read into
/// `craft-lottie`'s model within its default limits.
fn document(bytes: &[u8]) -> Result<Animation, LottieError> {
let read = |b: &[u8]| {
@@ -51,12 +51,12 @@ fn document(bytes: &[u8]) -> Result<Animation, LottieError> {
})
};
if bytes.starts_with(b"PK") {
let entries = crate::zip::read_stored(bytes);
let entries = crate::zip::read_entries(bytes).map_err(|e| LottieError::Parse(format!("dotLottie ZIP: {e}")))?;
let anim = entries
.iter()
.find(|(n, _)| n.starts_with("animations/") && n.ends_with(".json"))
.or_else(|| entries.iter().find(|(n, _)| n.ends_with(".json") && n != "manifest.json"))
.ok_or_else(|| LottieError::Parse("dotLottie archive without an uncompressed animation".into()))?;
.ok_or_else(|| LottieError::Parse("dotLottie archive without a JSON animation".into()))?;
return read(&anim.1);
}
read(bytes)
@@ -102,7 +102,7 @@ fn external_image(stored: &str, base_dir: &str) -> Result<String, String> {
}
}
/// Import a Lottie document (JSON or a stored `.lottie` archive) into `project` as a new
/// Import a Lottie document (JSON or a `.lottie` archive) into `project` as a new
/// composition. `store` saves embedded images and returns the file path to use;
/// `base_dir` resolves external image references.
pub fn import(
+1 -1
View File
@@ -30,7 +30,7 @@ pub mod zip;
use effectcraft_color::BlendMode;
use effectcraft_time::{FrameRate, TICKS_PER_SECOND, Tick};
pub use export::{ExportOptions, ExportResult, export_comp, to_dotlottie};
pub use export::{ExportOptions, ExportResult, export_comp, to_dotlottie, try_to_dotlottie};
pub use import::{ImportResult, ImportedImage, import};
#[derive(Debug, thiserror::Error)]
+36 -1
View File
@@ -12,7 +12,7 @@ use effectcraft_render::render_frame;
use effectcraft_time::{FrameRate, Tick};
use serde_json::{Value as Json, json};
use crate::{ExportOptions, export_comp, import};
use crate::{ExportOptions, LottieError, export_comp, import};
fn s(x: f64) -> Tick {
Tick::from_seconds_f64(x)
@@ -799,6 +799,32 @@ fn dotlottie_archive_roundtrip() {
assert_eq!(q.comp(imp.comp).unwrap().layers.len(), 1);
}
#[test]
fn dotlottie_archive_without_animation_is_rejected() {
let zip = craft_lottie::zip::try_store(&[("manifest.json".into(), b"{}".to_vec())]).unwrap();
let mut project = Project::default();
let err = import(&mut project, &zip, "empty archive", "", &mut |_| None).unwrap_err();
assert!(matches!(err, LottieError::Parse(message) if message.contains("without a JSON animation")));
}
#[test]
fn deflated_dotlottie_archive_imports() {
let (mut p, cid) = setup();
let l = solid_layer(&mut p, cid, [0.0, 1.0, 0.0], 40, 40);
push(&mut p, cid, l);
let res = export_comp(&p, cid, &ExportOptions::default(), &no_read).unwrap();
let manifest = serde_json::to_vec(&serde_json::json!({"version":"1","animations":[{"id":"main"}]})).unwrap();
let animation = serde_json::to_vec(&res.json).unwrap();
let zip = craft_lottie::zip::try_store_with_compression(
&[("manifest.json".into(), manifest), ("animations/main.json".into(), animation)],
craft_lottie::zip::Compression::Deflated,
)
.unwrap();
let mut q = Project::default();
let imp = import(&mut q, &zip, "compressed", "", &mut |_| None).unwrap();
assert_eq!(q.comp(imp.comp).unwrap().layers.len(), 1);
}
// ---------------------------------------------------------------- hand-written fixtures
fn fixture(name: &str) -> (Project, ItemId, Vec<String>) {
@@ -1062,3 +1088,12 @@ fn only_image_bytes_are_embedded() {
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);
}
#[test]
fn dotlottie_export_rejects_an_unrepresentable_entry_name() {
let (p, cid) = setup();
let res = export_comp(&p, cid, &ExportOptions::default(), &no_read).unwrap();
let err = crate::try_to_dotlottie(&res, &"a".repeat(usize::from(u16::MAX))).unwrap_err();
assert!(matches!(err, LottieError::Invalid(message) if message.contains("dotLottie ZIP")));
assert_eq!(crate::try_to_dotlottie(&res, "Main").unwrap(), crate::to_dotlottie(&res, "Main"));
}
+2 -120
View File
@@ -1,121 +1,3 @@
//! A minimal ZIP writer (stored entries, PKWARE APPNOTE 6.3 local headers + central directory)
//! for `.lottie` (dotLottie) archives.
//! Shared stored-entry ZIP functions used by dotLottie imports and exports.
fn crc32(data: &[u8]) -> u32 {
let mut crc = 0xFFFF_FFFFu32;
for &b in data {
crc ^= b as u32;
for _ in 0..8 {
crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 };
}
}
!crc
}
/// Build a ZIP archive with the given (name, bytes) entries, uncompressed.
pub fn store(entries: &[(String, Vec<u8>)]) -> Vec<u8> {
let mut out = Vec::new();
let mut central = Vec::new();
let put16 = |v: &mut Vec<u8>, x: u16| v.extend_from_slice(&x.to_le_bytes());
let put32 = |v: &mut Vec<u8>, x: u32| v.extend_from_slice(&x.to_le_bytes());
for (name, data) in entries {
let offset = out.len() as u32;
let crc = crc32(data);
let n = name.as_bytes();
// Local file header.
put32(&mut out, 0x0403_4b50);
put16(&mut out, 20); // version needed
put16(&mut out, 0x0800); // flags: UTF-8 names
put16(&mut out, 0); // stored
put16(&mut out, 0); // time
put16(&mut out, 0x21); // date (1980-01-01)
put32(&mut out, crc);
put32(&mut out, data.len() as u32);
put32(&mut out, data.len() as u32);
put16(&mut out, n.len() as u16);
put16(&mut out, 0);
out.extend_from_slice(n);
out.extend_from_slice(data);
// Central directory record.
put32(&mut central, 0x0201_4b50);
put16(&mut central, 20); // made by
put16(&mut central, 20);
put16(&mut central, 0x0800);
put16(&mut central, 0);
put16(&mut central, 0);
put16(&mut central, 0x21);
put32(&mut central, crc);
put32(&mut central, data.len() as u32);
put32(&mut central, data.len() as u32);
put16(&mut central, n.len() as u16);
put16(&mut central, 0); // extra
put16(&mut central, 0); // comment
put16(&mut central, 0); // disk
put16(&mut central, 0); // internal attrs
put32(&mut central, 0); // external attrs
put32(&mut central, offset);
central.extend_from_slice(n);
}
let cd_offset = out.len() as u32;
let cd_len = central.len() as u32;
out.extend_from_slice(&central);
put32(&mut out, 0x0605_4b50);
put16(&mut out, 0);
put16(&mut out, 0);
put16(&mut out, entries.len() as u16);
put16(&mut out, entries.len() as u16);
put32(&mut out, cd_len);
put32(&mut out, cd_offset);
put16(&mut out, 0);
out
}
/// Whether an entry name is a relative path inside the archive: not absolute (`/…`, `\…`), no
/// drive (`C:…`) and no `..` component.
fn is_relative_name(name: &str) -> bool {
let drive = name.as_bytes().get(1) == Some(&b':');
!name.starts_with(['/', '\\']) && !drive && !name.split(['/', '\\']).any(|c| c == "..")
}
/// Read the stored (uncompressed) entries of a ZIP archive written by [`store`] or any other
/// writer that did not compress. Compressed entries and entries whose names are not relative
/// paths inside the archive are skipped.
pub fn read_stored(data: &[u8]) -> Vec<(String, Vec<u8>)> {
let mut out = vec![];
let mut i = 0usize;
let u16_at = |p: usize| data.get(p..p + 2).map(|b| u16::from_le_bytes([b[0], b[1]]) as usize);
let u32_at = |p: usize| data.get(p..p + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as usize);
while u32_at(i) == Some(0x0403_4b50) {
let (Some(method), Some(size), Some(nlen), Some(xlen)) = (u16_at(i + 8), u32_at(i + 18), u16_at(i + 26), u16_at(i + 28)) else { break };
let name_start = i + 30;
let body = name_start + nlen + xlen;
let (Some(name), Some(bytes)) = (data.get(name_start..name_start + nlen), data.get(body..body + size)) else { break };
let name = String::from_utf8_lossy(name).to_string();
if method == 0 && is_relative_name(&name) {
out.push((name, bytes.to_vec()));
}
i = body + size;
}
out
}
#[cfg(test)]
mod tests {
#[test]
fn crc_and_roundtrip() {
assert_eq!(super::crc32(b"123456789"), 0xCBF4_3926);
let z = super::store(&[("a.json".into(), b"{}".to_vec()), ("b/c.txt".into(), b"hello".to_vec())]);
let back = super::read_stored(&z);
assert_eq!(back.len(), 2);
assert_eq!(back[1].0, "b/c.txt");
assert_eq!(back[1].1, b"hello");
}
#[test]
fn entries_outside_the_archive_root_are_skipped() {
let names = ["../up.txt", "a/../../up.txt", "/abs/x", "\\abs\\x", "C:\\x", "c:x", "a\\..\\x", "ok/x.txt", "ok/..x"];
let z = super::store(&names.iter().map(|n| (n.to_string(), b"x".to_vec())).collect::<Vec<_>>());
let back: Vec<String> = super::read_stored(&z).into_iter().map(|(n, _)| n).collect();
assert_eq!(back, ["ok/x.txt", "ok/..x"]);
}
}
pub use craft_lottie::zip::{Limits, read_entries, read_entries_with_limits, read_stored, store, try_store, try_store_with_compression};
+2 -2
View File
@@ -23,7 +23,7 @@ effectcraft-model = { workspace = true }
filmcraft-codecs = { workspace = true }
filmcraft-media = { workspace = true }
craft-dpx = { workspace = true }
craft-exr = { workspace = true }
craft-exr = { workspace = true, features = ["decode"] }
craft-gif = { workspace = true }
craft-webp = { workspace = true }
craft-audio = { workspace = true }
@@ -32,7 +32,6 @@ filmcraft-mpegts = { workspace = true }
filmcraft-frame = { workspace = true }
filmcraft-color = { workspace = true }
filmcraft-time = { workspace = true }
exr = { workspace = true }
image = { workspace = true, features = ["gif", "webp", "tiff", "bmp", "exr", "tga", "hdr"] }
rayon = { workspace = true }
serde_json = { workspace = true }
@@ -40,6 +39,7 @@ thiserror = { workspace = true }
log = { workspace = true }
[dev-dependencies]
exr = { workspace = true }
# Writes the animated GIFs of the tests.
gif = { workspace = true }
tempfile = { workspace = true }
+176 -224
View File
@@ -1,5 +1,5 @@
//! Auxiliary channels from multi-layer OpenEXR files (depth, object / material IDs, normals,
//! Cryptomatte, …) for the 3D Channel effects, read with the pure-Rust `exr` crate.
//! Cryptomatte, …) for the 3D Channel effects, read through the shared `craft-exr` decoder.
//!
//! Every channel of every layer becomes a plane named `layer.channel` (or just `channel` for
//! the unnamed layer), placed in the display window (pixels outside a layer's data window are
@@ -18,12 +18,6 @@ use std::sync::{Arc, Mutex, OnceLock, PoisonError};
use effectcraft_raster::channels3d::{BACKGROUND_DEPTH, split_channel};
use effectcraft_raster::{AuxChannels, Image};
use effectcraft_render::reads::{ReadFile, ReadFiles};
use exr::block::UncompressedBlock;
use exr::block::chunk::TileCoordinates;
use exr::image::read::image::ReadImage;
use exr::image::read::layers::{ChannelsReader, ReadAllLayers, ReadChannels};
use exr::meta::attribute::SampleType;
use exr::meta::header::Header;
use crate::convert::{AlphaOp, float_to_image};
@@ -35,179 +29,20 @@ const MAX_DISPLAY_PIXELS: usize = 1 << 28;
use craft_exr::channels::{is_depth, part_channel_name as channel_name};
pub use craft_exr::cryptomatte::parse_manifest;
/// The part name channels are prefixed with (`None`: unnamed).
fn part_name(header: &Header) -> Option<String> {
header.own_attributes.layer_name.as_ref().map(|t| t.to_string()).filter(|s| !s.is_empty())
}
/// Why an OpenEXR file's pixels can't be decoded when its headers name a compression this build
/// can't decompress (HTJ2K, OpenEXR 3.4's High-Throughput JPEG 2000): the decoders only said
/// "no non-deep rgb channels" or showed a transparent frame (#482). `None` for other files.
pub fn unsupported_compression(bytes: &[u8]) -> Option<&'static str> {
use exr::compression::Compression;
let meta = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(bytes), false).ok()?;
meta.headers
let mut limits = craft_exr::decode::Limits::default();
limits.max_pixels = u64::MAX;
limits.max_bytes = u64::MAX;
craft_exr::decode::parts(bytes, &limits)
.ok()?
.iter()
.any(|h| matches!(h.compression, Compression::HTJ2K32 | Compression::HTJ2K256))
.any(|part| matches!(part.compression.as_str(), "HTJ2K32" | "HTJ2K256"))
.then_some("OpenEXR HTJ2K compression isn't supported yet: re-save the file with ZIP, PIZ or DWAA compression")
}
/// Which channels a decode keeps, by plane name, and the display window they are placed in
/// (the first part's, as the `exr` crate reports the image's).
struct Planes<'k> {
keep: &'k dyn Fn(&str) -> bool,
display: exr::meta::attribute::IntegerBounds,
}
/// One part's kept channels while it is decoded: each line of samples goes straight into its
/// channel's display-window plane (no copy of the part's samples in their own type first).
struct PlanesReader {
/// Per channel of the part: its plane name, sample type and plane (`None`: not kept).
planes: Vec<Option<(String, SampleType, Vec<f32>)>>,
/// The data window's position in the display window, and the data window's and display
/// window's sizes.
offset: (i64, i64),
layer: (usize, usize),
display: (usize, usize),
f16s: Vec<exr::prelude::f16>,
u32s: Vec<u32>,
f32s: Vec<f32>,
/// The part's channel indices of exact `R`, `G`, `B` and `A`, when it has R, G and B.
rgb: Option<([usize; 3], Option<usize>)>,
/// The part's display window differs from the first part's.
own_display: bool,
}
/// A decoded part: its kept planes (in channel order) and what [`PlanesReader`] found.
struct PartPlanes {
planes: Vec<(String, Vec<f32>)>,
/// Positions in `planes` of the part's R, G, B (and A) channels, when it has them and keeps
/// them all.
rgb: Option<([usize; 3], Option<usize>)>,
has_rgb: bool,
exact: bool,
}
impl<'s> ReadChannels<'s> for Planes<'_> {
type Reader = PlanesReader;
fn create_channels_reader(&'s self, header: &Header) -> exr::error::Result<PlanesReader> {
if header.deep {
return Err(exr::error::Error::NotSupported("deep data".into()));
}
let dw = self.display;
let display = (dw.size.0, dw.size.1);
// Each channel is a display-window plane: a 60000² window over a 1×1 data window was a
// multi-gigabyte allocation per channel (#2141).
let n = display.0.checked_mul(display.1).filter(|n| *n > 0 && *n <= MAX_DISPLAY_PIXELS).ok_or(exr::error::Error::Invalid("display window".into()))?;
let prefix = part_name(header);
let pos = header.own_attributes.layer_position;
let offset = (i64::from(pos.0) - i64::from(dw.position.0), i64::from(pos.1) - i64::from(dw.position.1));
let planes = header
.channels
.list
.iter()
.map(|ch| {
let name = channel_name(prefix.as_deref(), &ch.name.to_string());
let fill = if is_depth(&name) { BACKGROUND_DEPTH } else { 0.0 };
(self.keep)(&name).then(|| (name, ch.sample_type, vec![fill; n]))
})
.collect();
let find = |c: &str| header.channels.find_index_of_channel(&exr::meta::attribute::Text::from(c));
let rgb = match (find("R"), find("G"), find("B")) {
(Some(r), Some(g), Some(b)) => Some(([r, g, b], find("A"))),
_ => None,
};
Ok(PlanesReader {
planes,
offset,
layer: (header.layer_size.0, header.layer_size.1),
display,
f16s: vec![],
u32s: vec![],
f32s: vec![],
rgb,
own_display: header.shared_attributes.display_window != dw,
})
}
}
impl ChannelsReader for PlanesReader {
type Channels = PartPlanes;
fn filter_block(&self, tile: TileCoordinates) -> bool {
tile.is_largest_resolution_level() && self.planes.iter().any(Option::is_some)
}
fn read_block(&mut self, header: &Header, block: UncompressedBlock) -> exr::error::UnitResult {
let ((lw, lh), (w, h)) = (self.layer, self.display);
for line in block.lines(&header.channels) {
let Some(Some((_, kind, plane))) = self.planes.get_mut(line.location.channel) else { continue };
let (x0, y, count) = (line.location.position.0, line.location.position.1, line.location.sample_count);
// The samples of data-window row `y`, columns `x0..x0 + count`, at display row `ty`
// from display column `tx0`; clipped to the display window.
if y >= lh || x0.checked_add(count).is_none_or(|end| end > lw) {
continue;
}
let Some(ty) = usize::try_from(y as i64 + self.offset.1).ok().filter(|ty| *ty < h) else { continue };
let tx0 = x0 as i64 + self.offset.0;
let skip = usize::try_from(-tx0).unwrap_or(0).min(count);
let tx = usize::try_from(tx0.max(0)).unwrap_or(usize::MAX);
let take = (count - skip).min(w.saturating_sub(tx));
let start = ty.checked_mul(w).and_then(|row| row.checked_add(tx));
let Some(dst) = start.filter(|_| take > 0).and_then(|s| plane.get_mut(s..s.checked_add(take)?)) else { continue };
match kind {
SampleType::F16 => {
self.f16s.resize(count, exr::prelude::f16::ZERO);
line.read_samples_into_slice(&mut self.f16s)?;
for (d, s) in dst.iter_mut().zip(self.f16s.iter().skip(skip)) {
*d = s.to_f32();
}
}
SampleType::F32 => {
self.f32s.resize(count, 0.0);
line.read_samples_into_slice(&mut self.f32s)?;
for (d, s) in dst.iter_mut().zip(self.f32s.iter().skip(skip)) {
*d = *s;
}
}
SampleType::U32 => {
self.u32s.resize(count, 0);
line.read_samples_into_slice(&mut self.u32s)?;
for (d, s) in dst.iter_mut().zip(self.u32s.iter().skip(skip)) {
*d = *s as f32;
}
}
}
}
Ok(())
}
fn into_channels(self) -> PartPlanes {
// Positions of the kept channels among the kept planes.
let mut at = vec![None; self.planes.len()];
let mut planes = Vec::with_capacity(self.planes.len());
for (i, p) in self.planes.into_iter().enumerate() {
if let Some((name, _, data)) = p {
if let Some(slot) = at.get_mut(i) {
*slot = Some(planes.len());
}
planes.push((name, data));
}
}
let kept = |i: usize| at.get(i).copied().flatten();
let has_rgb = self.rgb.is_some();
let rgb = self.rgb.and_then(|([r, g, b], a)| {
let a = match a {
Some(a) => Some(kept(a)?),
None => None,
};
Some(([kept(r)?, kept(g)?, kept(b)?], a))
});
PartPlanes { planes, rgb, has_rgb, exact: !self.own_display }
}
}
/// How the `image` crate's OpenEXR decoder would show a file: the first part with exact `R`, `G`
/// and `B` channels (alpha `A` when it has one, else opaque), placed in the display window with
/// 0 outside the data window.
@@ -253,73 +88,117 @@ enum Refused {
TooLarge(String),
}
/// Bytes the planes of the channels `keep` names would take (`None`: not a readable header).
fn planes_bytes(meta: &exr::meta::MetaData, keep: &dyn Fn(&str) -> bool) -> Option<usize> {
let dw = meta.headers.first()?.shared_attributes.display_window;
let n = dw.size.0.checked_mul(dw.size.1)?;
let mut kept = 0usize;
for header in meta.headers.iter() {
let prefix = part_name(header);
kept = kept.saturating_add(header.channels.list.iter().filter(|ch| keep(&channel_name(prefix.as_deref(), &ch.name.to_string()))).count());
}
Some(n.saturating_mul(kept).saturating_mul(std::mem::size_of::<f32>()))
/// Bytes the selected channels occupy in each part's own display window. The channel cache
/// reserves this before decoding so concurrent imports cannot exceed its soft budget.
fn planes_bytes(infos: &[craft_exr::decode::PartInfo], keep: &dyn Fn(&str) -> bool) -> Option<usize> {
infos.first()?;
let total = infos.iter().try_fold(0u64, |total, part| {
let count = part.channels.iter().filter(|channel| keep(&channel_name(part.name.as_deref(), &channel.name))).count() as u64;
if count == 0 {
return Some(total);
}
let pixels = u64::from(part.display_window.width).checked_mul(u64::from(part.display_window.height))?;
total.checked_add(pixels.checked_mul(count)?.checked_mul(4)?)
})?;
usize::try_from(total).ok()
}
/// Decode the channels of an EXR file's bytes that `keep` names (by plane name).
/// Decode selected EXR channels through craft-exr, which places each selected block directly
/// into its bounded display-window plane.
fn decode(bytes: &[u8], keep: &dyn Fn(&str) -> bool) -> Result<Decoded, Refused> {
let meta = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(bytes), false).map_err(|_| Refused::Unreadable)?;
let display = meta.headers.first().ok_or(Refused::Unreadable)?.shared_attributes.display_window;
let need = planes_bytes(&meta, keep).ok_or(Refused::Unreadable)?;
if need > MAX_DECODE_BYTES {
let gb = |b: usize| b as f64 / f64::from(1u32 << 30);
use craft_exr::decode::{self, Error as ExrError, Limits};
let mut limits = Limits::default();
limits.max_pixels = MAX_DISPLAY_PIXELS as u64;
limits.max_bytes = MAX_DECODE_BYTES as u64;
// Header-only checks must not charge channels this caller does not decode. The shared display
// decoder independently checks the exact selected display planes before allocating them. It
// also bounds a selected part's data window across all of its channels (decoded blocks carry
// every channel before the unused ones are dropped), so a part whose full channel set exceeds
// `max_bytes` is refused even when the kept planes alone would fit.
let mut header_limits = limits;
header_limits.max_width = u32::MAX;
header_limits.max_height = u32::MAX;
header_limits.max_pixels = u64::MAX;
header_limits.max_bytes = u64::MAX;
let infos = decode::parts(bytes, &header_limits).map_err(|e| match e {
ExrError::LimitExceeded(message) => Refused::TooLarge(message),
_ => Refused::Unreadable,
})?;
let display = infos.first().ok_or(Refused::Unreadable)?.display_window;
let needed = planes_bytes(&infos, keep).filter(|bytes| *bytes > 0).ok_or(Refused::Unreadable)?;
if needed > MAX_DECODE_BYTES {
let gb = |b: u64| b as f64 / f64::from(1u32 << 30);
return Err(Refused::TooLarge(format!(
"its {}×{} OpenEXR channels need {:.1} GB of memory, more than the {:.1} GB one file may use",
display.size.0,
display.size.1,
gb(need),
gb(MAX_DECODE_BYTES)
display.width,
display.height,
gb(needed as u64),
gb(MAX_DECODE_BYTES as u64),
)));
}
let read = ReadImage::new(ReadAllLayers { read_channels: Planes { keep, display } }, |_| {});
let img = read.from_buffered(std::io::Cursor::new(bytes)).map_err(|_| Refused::Unreadable)?;
let dw = img.attributes.display_window;
let mut aux = AuxChannels::new(dw.size.0 as u32, dw.size.1 as u32, 1.0);
let mut aux = AuxChannels::new(display.width, display.height, 1.0);
let mut crypto: HashMap<String, (Option<String>, Option<String>)> = HashMap::new();
let mut collect_meta = |other: &HashMap<exr::meta::attribute::Text, exr::meta::attribute::AttributeValue>| {
for (k, v) in other {
let key = k.to_string();
let mut rgb = Rgb::None;
for info in infos {
for (key, value) in &info.text_attributes {
let Some(rest) = key.strip_prefix("cryptomatte/") else { continue };
let Some((id, field)) = rest.split_once('/') else { continue };
let exr::meta::attribute::AttributeValue::Text(t) = v else { continue };
let e = crypto.entry(id.to_string()).or_default();
let entry = crypto.entry(id.to_string()).or_default();
match field {
"name" => e.0 = Some(t.to_string()),
"manifest" => e.1 = Some(t.to_string()),
"name" => entry.0 = Some(value.clone()),
"manifest" => entry.1 = Some(value.clone()),
_ => {}
}
}
};
collect_meta(&img.attributes.other);
let mut rgb = Rgb::None;
for layer in img.layer_data {
collect_meta(&layer.attributes.other);
let part = layer.channel_data;
if rgb == Rgb::None && part.has_rgb {
let prefix = info.name.as_deref();
let exact = info.display_window == display;
let rgb_indices = ["R", "G", "B"].map(|wanted| info.channels.iter().position(|channel| channel.name == wanted));
let has_rgb = rgb_indices.iter().all(Option::is_some);
let alpha_index = info.channels.iter().position(|channel| channel.name == "A");
// Metadata above still belongs to the file, but an ignored part must not allocate or
// impose its pixel/deep-data limits on the selected picture channels.
if !info.channels.iter().any(|channel| keep(&channel_name(prefix, &channel.name))) {
if rgb == Rgb::None && has_rgb {
rgb = Rgb::Unsupported;
}
continue;
}
let mut part = decode::decode_display_part(
bytes,
info.index,
keep,
&|name| {
if is_depth(name) { BACKGROUND_DEPTH } else { 0.0 }
},
&limits,
)
.map_err(|error| match error {
ExrError::LimitExceeded(message) => Refused::TooLarge(message),
_ => Refused::Unreadable,
})?;
if rgb == Rgb::None && has_rgb {
let base = aux.channels.len();
rgb = match part.rgb {
Some(([r, g, b], a)) if part.exact => {
let picked = [Some(r), Some(g), Some(b), a];
let depth = picked.iter().flatten().any(|i| part.planes.get(*i).is_some_and(|(n, _)| is_depth(n)));
if depth { Rgb::Unsupported } else { Rgb::Planes([base + r, base + g, base + b], a.map(|a| base + a)) }
let locate = |raw: usize| {
let name = channel_name(prefix, &info.channels.get(raw)?.name);
part.channels.iter().position(|channel| channel.name == name)
};
rgb = match (rgb_indices[0].and_then(locate), rgb_indices[1].and_then(locate), rgb_indices[2].and_then(locate)) {
(Some(r), Some(g), Some(b)) if exact => {
let a = alpha_index.and_then(locate);
let depth =
[Some(r), Some(g), Some(b), a].into_iter().flatten().any(|i| part.channels.get(i).is_some_and(|channel| is_depth(&channel.name)));
if depth { Rgb::Unsupported } else { Rgb::Planes([base + r, base + g, base + b], a.map(|i| base + i)) }
}
_ => Rgb::Unsupported,
};
}
aux.channels.extend(part.planes);
aux.channels.extend(part.channels.drain(..).map(|channel| (channel.name, channel.samples)));
}
for (_, (name, manifest)) in crypto {
if let (Some(n), Some(m)) = (name, manifest) {
aux.manifests.push((n, parse_manifest(&m)));
if let (Some(name), Some(manifest)) = (name, manifest) {
aux.manifests.push((name, parse_manifest(&manifest)));
}
}
aux.manifests.sort();
@@ -389,11 +268,16 @@ fn picture_names(aux: &AuxChannels) -> Option<([String; 3], Option<String>)> {
/// headers, then only those channels decoded; in a multi-part file the other parts' pixels are
/// not even decompressed), with the names of its planes.
fn picture_only(bytes: &[u8]) -> Result<(Decoded, [String; 3], Option<String>), Refused> {
let meta = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(bytes), false).map_err(|_| Refused::Unreadable)?;
let mut limits = craft_exr::decode::Limits::default();
limits.max_width = u32::MAX;
limits.max_height = u32::MAX;
limits.max_pixels = u64::MAX;
limits.max_bytes = u64::MAX;
let meta = craft_exr::decode::parts(bytes, &limits).map_err(|_| Refused::Unreadable)?;
let mut names = AuxChannels::new(0, 0, 1.0);
for header in meta.headers.iter() {
let prefix = part_name(header);
names.channels.extend(header.channels.list.iter().map(|ch| (channel_name(prefix.as_deref(), &ch.name.to_string()), vec![])));
for part in meta {
let prefix = part.name.as_deref();
names.channels.extend(part.channels.iter().map(|ch| (channel_name(prefix, &ch.name), vec![])));
}
let (rgb, a) = picture_names(&names).ok_or(Refused::Unreadable)?;
// Planes are looked up by name ignoring case, so every channel that could answer is kept.
@@ -679,8 +563,13 @@ impl ChannelCache {
/// Decode the file at `path` whose bytes are `bytes` for `slot` (see [`ChannelCache::get`]).
fn decode_file(&self, path: &str, slot: &Slot, bytes: &[u8]) -> Option<Arc<Decoded>> {
// The planes it will allocate count against the budget while it decodes.
if let Ok(meta) = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(bytes), false)
&& let Some(need) = planes_bytes(&meta, &|_| true)
let mut limits = craft_exr::decode::Limits::default();
limits.max_width = u32::MAX;
limits.max_height = u32::MAX;
limits.max_pixels = u64::MAX;
limits.max_bytes = u64::MAX;
if let Ok(infos) = craft_exr::decode::parts(bytes, &limits)
&& let Some(need) = planes_bytes(&infos, &|_| true)
{
self.account(slot, need.min(MAX_DECODE_BYTES));
}
@@ -1264,6 +1153,69 @@ mod tests {
assert!(aux.get("ViewLayer.Combined.R").is_some());
}
#[test]
fn multipart_fallback_filters_channels_and_reserves_display_planes() {
use exr::prelude::*;
let part = |name: &str, pos: (i32, i32), size: (usize, usize), channels: Vec<AnyChannel<FlatSamples>>| {
let attrs = LayerAttributes { layer_name: Some(Text::from(name)), layer_position: Vec2(pos.0, pos.1), ..Default::default() };
Layer::new(size, attrs, Encoding::FAST_LOSSLESS, AnyChannels::sort(channels.into()))
};
let (w, h) = (2usize, 1usize);
let bytes = file(
vec![
part("Depth", (0, 0), (1, 1), vec![AnyChannel::new("Z", FlatSamples::F32(vec![7.0]))]),
part(
"Beauty",
(1, 1),
(w, h),
vec![
AnyChannel::new("R", FlatSamples::F32(vec![0.1; w * h])),
AnyChannel::new("G", FlatSamples::F32(vec![0.2; w * h])),
AnyChannel::new("B", FlatSamples::F32(vec![0.3; w * h])),
],
),
],
IntegerBounds::from_dimensions((3, 2)),
);
let mut limits = craft_exr::decode::Limits::default();
limits.max_width = u32::MAX;
limits.max_height = u32::MAX;
limits.max_pixels = u64::MAX;
limits.max_bytes = u64::MAX;
let parts = craft_exr::decode::parts(&bytes, &limits).unwrap();
assert_eq!(planes_bytes(&parts, &|name| name == "Beauty.R"), Some(3 * 2 * 4));
assert_eq!(planes_bytes(&parts, &|_| false), Some(0));
// Different display windows must not reserve every part at the first part's size.
let mut varied = parts.clone();
varied[0].display_window.width = 1;
varied[0].display_window.height = 1;
varied[1].display_window.width = 10;
varied[1].display_window.height = 20;
assert_eq!(planes_bytes(&varied, &|_| true), Some(4 + 10 * 20 * 3 * 4));
assert_eq!(planes_bytes(&varied, &|name| name == "Beauty.R"), Some(10 * 20 * 4));
varied[1].display_window.width = u32::MAX;
varied[1].display_window.height = u32::MAX;
assert_eq!(planes_bytes(&varied, &|_| true), None);
assert_eq!(planes_bytes(&varied, &|name| name == "Depth.Z"), Some(4));
// Skipping the RGB part preserves the first-RGB fallback state and depth samples.
let depth = decode(&bytes, &|name| name == "Depth.Z").unwrap();
assert_eq!(depth.rgb, Rgb::Unsupported);
assert_eq!(depth.aux.channels.len(), 1);
assert_eq!(depth.aux.get("Depth.Z").unwrap()[0], 7.0);
let selected = decode(&bytes, &|name| name == "Beauty.R").unwrap();
assert_eq!(selected.aux.channels.iter().map(|(name, _)| name.as_str()).collect::<Vec<_>>(), ["Beauty.R"]);
assert_eq!(selected.aux.get("Beauty.R").unwrap(), [0.0, 0.0, 0.0, 0.0, 0.1, 0.1]);
// The old app decoder selected the named beauty part when the first part was depth-only,
// and placed its data into the shared display window; retain that layered-picture result.
let image = layered_image(&bytes).unwrap().to_rgba32f();
assert_eq!(image.get_pixel(1, 1).0, [0.1, 0.2, 0.3, 1.0]);
assert_eq!(image.get_pixel(0, 0).0, [0.0, 0.0, 0.0, 1.0]);
}
/// Straight RGBA of [`layered_image`] at pixel 0 for a file of single-value channels.
fn picture<S: AsRef<str>>(channels: &[(S, f32)]) -> Vec<f32> {
use exr::prelude::*;