From 665bfac0b33904b1dcda6de889f43f43a94ad3f8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 21:55:05 -0500 Subject: [PATCH] export: cut over exr_out to craft-exr (#3937) * export: cut over exr_out to craft-exr * fix(export): keep exr in dev-dependencies for tests --- Cargo.lock | 3 +- Cargo.toml | 2 +- crates/export/Cargo.toml | 3 +- crates/export/src/exr_out.rs | 107 ++++++++++++----------------------- 4 files changed, 42 insertions(+), 73 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c8bb8f50f..01e0b6fd4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1314,7 +1314,7 @@ 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=5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87#5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" +source = "git+https://github.com/storytold/craft-libs?rev=3b8e426a260695af840a2cd79a064d40778f939e#3b8e426a260695af840a2cd79a064d40778f939e" dependencies = [ "exr", "flate2", @@ -2091,6 +2091,7 @@ name = "effectcraft-export" version = "0.24.1" dependencies = [ "craft-audio 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87)", + "craft-exr", "craft-gif", "craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87)", "craft-video 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87)", diff --git a/Cargo.toml b/Cargo.toml index 832eeef8c..6fbb4f986 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ 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 = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } 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 = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } +craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "3b8e426a260695af840a2cd79a064d40778f939e" } craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "fa4741d0bbba8da303dd1bb1a93cbcb78e7747a0" } craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "77686f24fe8665c21369f1b593f4d65a973e2a45" } diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml index 1bf883df3..6b93a8a7c 100644 --- a/crates/export/Cargo.toml +++ b/crates/export/Cargo.toml @@ -18,14 +18,15 @@ craft-video = { workspace = true } craft-audio = { workspace = true } craft-media = { workspace = true } craft-gif = { workspace = true } +craft-exr = { workspace = true, features = ["decode"] } image = { workspace = true, features = ["png", "jpeg", "tiff", "exr", "tga"] } -exr = { workspace = true } rayon = { workspace = true } web-time = { workspace = true } thiserror = { workspace = true } log = { workspace = true } [dev-dependencies] +exr = { workspace = true } gif = { workspace = true } tempfile = { workspace = true } effectcraft-media = { workspace = true } diff --git a/crates/export/src/exr_out.rs b/crates/export/src/exr_out.rs index 43467629a..cefa81a07 100644 --- a/crates/export/src/exr_out.rs +++ b/crates/export/src/exr_out.rs @@ -12,40 +12,25 @@ use std::io::{Seek, Write}; +use craft_exr::decode::{Compression, Result, SampleDepth}; use effectcraft_project::render_queue::{ExrBitDepth, ExrCompression}; -use exr::meta::attribute::Chromaticities; -use exr::prelude::{Blocks, Compression, Encoding, Image, Layer, LayerAttributes, LineOrder, SpecificChannels, Vec2, WritableImage, f16}; -/// The largest finite half float. -const HALF_MAX: f32 = 65_504.0; - -/// A sample as half float: rounded to the nearest half (ties to even), held to ±65 504 so an -/// over-range render doesn't turn into infinity. NaN stays NaN. -fn to_half(v: f32) -> f16 { - f16::from_f32(v.clamp(-HALF_MAX, HALF_MAX)) -} - -/// The `exr` crate's compression for an Output Module setting. +/// The `craft-exr` compression for an Output Module setting. pub fn compression(c: ExrCompression) -> Compression { match c { - ExrCompression::None => Compression::Uncompressed, - ExrCompression::Rle => Compression::RLE, - ExrCompression::Zips => Compression::ZIP1, - ExrCompression::Zip => Compression::ZIP16, - ExrCompression::Piz => Compression::PIZ, - ExrCompression::Pxr24 => Compression::PXR24, + ExrCompression::None => Compression::None, + ExrCompression::Rle => Compression::Rle, + ExrCompression::Zips => Compression::Zip1, + ExrCompression::Zip => Compression::Zip16, + ExrCompression::Piz => Compression::Piz, + ExrCompression::Pxr24 => Compression::Pxr24, ExrCompression::B44 => Compression::B44, - ExrCompression::B44a => Compression::B44A, - ExrCompression::Dwaa => Compression::DWAA(None), - ExrCompression::Dwab => Compression::DWAB(None), + ExrCompression::B44a => Compression::B44a, + ExrCompression::Dwaa => Compression::Dwaa, + ExrCompression::Dwab => Compression::Dwab, } } -/// Scan lines, top to bottom, with compression `c`. -fn encoding(c: ExrCompression) -> Encoding { - Encoding { compression: compression(c), blocks: Blocks::ScanLines, line_order: LineOrder::Increasing } -} - /// Write `w`×`h` RGBA pixels (`data`: four samples per pixel as they are to be written, /// premultiplied, rows top to bottom) as one OpenEXR frame, its blocks compressed in parallel as before. A pixel `data` /// doesn't cover is written as transparent black. @@ -58,56 +43,38 @@ pub fn write_rgba( (w, h): (usize, usize), (c, depth): (ExrCompression, ExrBitDepth), chroma: Option<[[f32; 2]; 4]>, -) -> exr::error::UnitResult { - let chroma = chroma.map(|[red, green, blue, white]| { - let v = |[x, y]: [f32; 2]| Vec2(x, y); - Chromaticities { red: v(red), green: v(green), blue: v(blue), white: v(white) } - }); - let row = w.saturating_mul(4); - let px = move |p: Vec2| -> [f32; 4] { - let o = p.y().saturating_mul(row).saturating_add(p.x().saturating_mul(4)); - match data.get(o..o.saturating_add(4)) { - Some(&[r, g, b, a]) => [r, g, b, a], - _ => [0.0; 4], - } +) -> Result<()> { + let sample_depth = match depth { + ExrBitDepth::Float => SampleDepth::Float, + ExrBitDepth::Half => SampleDepth::Half, }; - match depth { - ExrBitDepth::Float => { - let mut image = Image::from_layer(Layer::new( - (w, h), - LayerAttributes::default(), - encoding(c), - SpecificChannels::rgba(move |p: Vec2| { - let [r, g, b, a] = px(p); - (r, g, b, a) - }), - )); - image.attributes.chromaticities = chroma; - image.write().to_buffered(file) - } - ExrBitDepth::Half => { - let mut image = Image::from_layer(Layer::new( - (w, h), - LayerAttributes::default(), - encoding(c), - SpecificChannels::rgba(move |p: Vec2| { - let [r, g, b, a] = px(p).map(to_half); - (r, g, b, a) - }), - )); - image.attributes.chromaticities = chroma; - image.write().to_buffered(file) - } - } + craft_exr::decode::write_rgba(file, data, w as u32, h as u32, sample_depth, compression(c), chroma) } #[cfg(test)] mod tests { use super::*; - use exr::prelude::{ReadChannels, ReadLayers}; - use exr::prelude::{SampleType, read}; + use exr::prelude::{ + Encoding, Image, Layer, LayerAttributes, LineOrder, ReadChannels, ReadLayers, SampleType, SpecificChannels, Vec2, WritableImage, f16, read, + }; use std::io::Cursor; + fn exr_compression(c: ExrCompression) -> exr::compression::Compression { + use exr::compression::Compression as C; + match c { + ExrCompression::None => C::Uncompressed, + ExrCompression::Rle => C::RLE, + ExrCompression::Zips => C::ZIP1, + ExrCompression::Zip => C::ZIP16, + ExrCompression::Piz => C::PIZ, + ExrCompression::Pxr24 => C::PXR24, + ExrCompression::B44 => C::B44, + ExrCompression::B44a => C::B44A, + ExrCompression::Dwaa => C::DWAA(None), + ExrCompression::Dwab => C::DWAB(None), + } + } + /// A frame with a smooth gradient, grain, over-range highlights, a hard-edged high-contrast /// block and a soft alpha ramp (premultiplied), like a render with text over a plate. fn frame(w: usize, h: usize) -> Vec { @@ -172,7 +139,7 @@ mod tests { }; let ty = if depth == ExrBitDepth::Half { SampleType::F16 } else { SampleType::F32 }; for c in ExrCompression::ALL { - assert_eq!(c.is_lossy(depth), !compression(c).is_lossless_for(ty), "{c:?} {depth:?}"); + assert_eq!(c.is_lossy(depth), !exr_compression(c).is_lossless_for(ty), "{c:?} {depth:?}"); let bytes = write(&src, (w, h), c, depth); if c.is_lossy(depth) { assert!(bytes.len() < write(&src, (w, h), ExrCompression::None, depth).len(), "{c:?} {depth:?}: smaller than None"); @@ -205,7 +172,7 @@ mod tests { let meta = exr::meta::MetaData::read_from_buffered(Cursor::new(&bytes), false).unwrap(); assert_eq!(meta.headers.len(), 1); assert_eq!(meta.headers[0].blocks, exr::meta::BlockDescription::ScanLines, "{c:?}"); - assert_eq!(meta.headers[0].compression, compression(c), "{c:?}"); + assert_eq!(meta.headers[0].compression, exr_compression(c), "{c:?}"); assert_eq!(meta.headers[0].line_order, LineOrder::Increasing); } }