Import: Interpret Footage ▸ Preserve RGB keeps the file's values (#411)
Cause: float footage (OpenEXR, float TIFF) was always sRGB-encoded on
decode, and Interpret Footage could not turn that off, so an OCIO effect
on the layer converted values that were already encoded.
Fix: After Effects' Interpret Footage ▸ Color ▸ Preserve RGB
(Footage::preserve_rgb; file.interpretFootage {"preserveRgb": true}; a
checkbox in the Interpret Footage form; Remember / Apply Interpretation
carry it). The decoder keeps float values linear, and the working-space
conversion from the colour profile and Interpret As Linear Light don't
apply, so the file's values enter the composition unconverted. The media
cache key includes the flag, so both interpretations decode separately.
Tests: pool::preserve_rgb_reads_float_exr_values_unconverted,
tests_color::preserve_rgb_footage_is_not_converted (managed, linearized
and not), tests_proxy::guess_alpha_invert_and_linear_light (command,
render, Apply Interpretation).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
698ade94ac
commit
d6de68b20d
@@ -6,7 +6,7 @@
|
||||
//! Values stay gamma-encoded: like After Effects, footage pixels enter the working space as they
|
||||
//! are encoded in the file. Alpha follows the footage's [`AlphaMode`] interpretation.
|
||||
|
||||
use effectcraft_project::AlphaMode;
|
||||
use effectcraft_project::{AlphaMode, Footage};
|
||||
use effectcraft_raster::{Image, Px};
|
||||
use filmcraft_color::{Matrix, Range};
|
||||
use filmcraft_frame::{Chroma, PixelData, VideoFrame};
|
||||
@@ -17,11 +17,24 @@ use rayon::prelude::*;
|
||||
pub(crate) struct AlphaOp {
|
||||
mode: AlphaMode,
|
||||
matte: [f32; 3],
|
||||
/// Interpret Footage ▸ Preserve RGB: float values stay as the file stores them (not
|
||||
/// sRGB-encoded).
|
||||
preserve_rgb: bool,
|
||||
}
|
||||
|
||||
impl AlphaOp {
|
||||
pub(crate) fn new(mode: AlphaMode, matte: [f32; 3]) -> Self {
|
||||
Self { mode, matte }
|
||||
Self { mode, matte, preserve_rgb: false }
|
||||
}
|
||||
/// The interpretation of footage `f`.
|
||||
pub(crate) fn of(f: &Footage) -> Self {
|
||||
Self { preserve_rgb: f.preserve_rgb, ..Self::new(f.alpha, f.premul_color) }
|
||||
}
|
||||
/// A linear float value as the compositor takes it: sRGB-encoded, or the file's own value
|
||||
/// under Preserve RGB.
|
||||
#[inline(always)]
|
||||
fn float(self, v: f32) -> f32 {
|
||||
if self.preserve_rgb { v } else { encode(v) }
|
||||
}
|
||||
/// `c` is the file's colour (straight or premultiplied as the mode says), `a` its alpha.
|
||||
#[inline(always)]
|
||||
@@ -72,7 +85,7 @@ pub(crate) fn frame_to_image_in(f: &VideoFrame, op: AlphaOp, mut buf: Vec<Px>) -
|
||||
for (o, s) in row.iter_mut().zip(src.as_chunks::<4>().0.iter()) {
|
||||
let a = s[3];
|
||||
let inv = if a > 0.0 { 1.0 / a } else { 0.0 };
|
||||
let c = [encode(s[0] * inv) * a, encode(s[1] * inv) * a, encode(s[2] * inv) * a];
|
||||
let c = [op.float(s[0] * inv) * a, op.float(s[1] * inv) * a, op.float(s[2] * inv) * a];
|
||||
*o = if op.mode == AlphaMode::Ignore { [c[0], c[1], c[2], 1.0] } else { [c[0], c[1], c[2], a] };
|
||||
}
|
||||
});
|
||||
@@ -231,7 +244,7 @@ fn convert_yuv<T: Sample>(img: &mut Image, w: usize, h: usize, chroma: Chroma, p
|
||||
}
|
||||
|
||||
/// Convert a still decoded by the `image` crate. Float images (OpenEXR, float TIFF) hold linear
|
||||
/// light and are sRGB-encoded here (values above 1 are kept).
|
||||
/// light and are sRGB-encoded here (values above 1 are kept), unless the footage preserves RGB.
|
||||
pub(crate) fn dynamic_to_image(img: &image::DynamicImage, op: AlphaOp) -> Image {
|
||||
use image::DynamicImage as D;
|
||||
let (w, h) = (img.width(), img.height());
|
||||
@@ -251,9 +264,9 @@ pub(crate) fn dynamic_to_image(img: &image::DynamicImage, op: AlphaOp) -> Image
|
||||
let a = s[3];
|
||||
let c = if premul {
|
||||
let inv = if a > 0.0 { 1.0 / a } else { 0.0 };
|
||||
[encode(s[0] * inv) * a, encode(s[1] * inv) * a, encode(s[2] * inv) * a]
|
||||
[op.float(s[0] * inv) * a, op.float(s[1] * inv) * a, op.float(s[2] * inv) * a]
|
||||
} else {
|
||||
[encode(s[0]), encode(s[1]), encode(s[2])]
|
||||
[op.float(s[0]), op.float(s[1]), op.float(s[2])]
|
||||
};
|
||||
*o = op.apply(c, a.clamp(0.0, 1.0));
|
||||
}
|
||||
|
||||
@@ -37,7 +37,9 @@ fn colour_bars(w: u32, h: u32) -> Image {
|
||||
struct Key {
|
||||
path: Arc<str>,
|
||||
frame: i64,
|
||||
alpha: u8,
|
||||
/// The alpha interpretation (bits 0–1) and Preserve RGB (bit 2): they change the decoded
|
||||
/// pixels.
|
||||
interp: u8,
|
||||
matte: [u32; 3],
|
||||
}
|
||||
|
||||
@@ -516,8 +518,9 @@ impl Inner {
|
||||
AlphaMode::Premultiplied => 1,
|
||||
AlphaMode::Ignore => 2,
|
||||
};
|
||||
let interp = alpha | (u8::from(footage.preserve_rgb) << 2);
|
||||
let matte = if footage.alpha == AlphaMode::Premultiplied { footage.premul_color.map(f32::to_bits) } else { [0; 3] };
|
||||
let key = |path: &str, frame| Key { path: path.into(), frame, alpha, matte };
|
||||
let key = |path: &str, frame| Key { path: path.into(), frame, interp, matte };
|
||||
match footage.kind {
|
||||
FootageKind::Sequence if !footage.sequence.is_empty() => {
|
||||
let i = Self::frame_index(footage.frame_rate, t, footage.sequence_frames(), footage.loop_count);
|
||||
@@ -607,7 +610,7 @@ impl Inner {
|
||||
fn prefetch(self: &Arc<Self>, _footage: &Footage, _t: Tick) {}
|
||||
|
||||
fn decode(&self, loc: &Loc, footage: &Footage) -> Result<Image> {
|
||||
let op = AlphaOp::new(footage.alpha, footage.premul_color);
|
||||
let op = AlphaOp::of(footage);
|
||||
let path = &loc.key.path;
|
||||
match loc.media_t {
|
||||
None if &**path == PLACEHOLDER => Ok(colour_bars(footage.width, footage.height)),
|
||||
@@ -759,7 +762,7 @@ mod tests {
|
||||
fn lru_respects_budget_and_recycles() {
|
||||
let mut c = Lru::default();
|
||||
let img = || Arc::new(Image::new(16, 16)); // 4 KiB + overhead
|
||||
let k = |i| Key { path: "a".into(), frame: i, alpha: 0, matte: [0; 3] };
|
||||
let k = |i| Key { path: "a".into(), frame: i, interp: 0, matte: [0; 3] };
|
||||
let budget = 3 * (4096 + 64);
|
||||
for i in 0..10 {
|
||||
c.insert(k(i), img(), budget);
|
||||
@@ -774,4 +777,27 @@ mod tests {
|
||||
assert_eq!(c.take_spare(256).len(), 256);
|
||||
assert!(c.take_spare(17).is_empty());
|
||||
}
|
||||
|
||||
/// #411: Interpret Footage ▸ Preserve RGB reads a float OpenEXR's values as the file stores
|
||||
/// them (no automatic linear → sRGB encode), so an OCIO effect is the only conversion; the
|
||||
/// default interpretation still encodes. Both interpretations of one file decode separately.
|
||||
#[test]
|
||||
fn preserve_rgb_reads_float_exr_values_unconverted() {
|
||||
use exr::prelude::*;
|
||||
let (w, h) = (2usize, 2usize);
|
||||
let ch = |n: &str, v: f32| AnyChannel::new(n, FlatSamples::F32(vec![v; w * h]));
|
||||
let channels = AnyChannels::sort(vec![ch("R", 0.18), ch("G", 0.5), ch("B", 2.0), ch("A", 1.0)].into());
|
||||
let image = exr::image::Image::from_layer(Layer::new((w, h), LayerAttributes::default(), Encoding::FAST_LOSSLESS, channels));
|
||||
let mut bytes = std::io::Cursor::new(Vec::new());
|
||||
image.write().to_buffered(&mut bytes).unwrap();
|
||||
let b: Arc<[u8]> = bytes.into_inner().into();
|
||||
let pool = MediaPool::new();
|
||||
pool.add_bytes("/render.exr", b.clone());
|
||||
let f = crate::probe_bytes("/render.exr", b).unwrap();
|
||||
let encoded = pool.frame_at(&f, Tick::ZERO).unwrap().get(0, 0);
|
||||
assert!((encoded[0] - 0.4613).abs() < 1e-3 && (encoded[1] - 0.7354).abs() < 1e-3 && encoded[2] > 1.3, "sRGB-encoded: {encoded:?}");
|
||||
let raw = pool.frame_at(&Footage { preserve_rgb: true, ..f.clone() }, Tick::ZERO).unwrap().get(0, 0);
|
||||
assert_eq!(raw, [0.18, 0.5, 2.0, 1.0]);
|
||||
assert_eq!(pool.frame_at(&f, Tick::ZERO).unwrap().get(0, 0), encoded, "the default interpretation is cached apart");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user