Merge pull request #423 from cdinhoo/codex/effect-input-control-fixes
This commit is contained in:
@@ -171,8 +171,7 @@ const CC_COMPOSITE_MODES: [&str; 16] = [
|
||||
"Stencil Alpha",
|
||||
];
|
||||
|
||||
/// Composites the original over/under/with the effect result. The original is the image as it
|
||||
/// enters this effect (the host does not expose the pre-effect source).
|
||||
/// Composites the original stack input over/under/with the result of preceding effects.
|
||||
fn cc_composite(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
let op = (ctx.params.f("opacity") as f32 / 100.0).clamp(0.0, 1.0);
|
||||
let m = ctx.params.e("compositeOriginal");
|
||||
@@ -194,9 +193,10 @@ fn cc_composite(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
15 => BlendMode::StencilAlpha,
|
||||
_ => BlendMode::Normal,
|
||||
};
|
||||
b.img.data.par_iter_mut().for_each(|px| {
|
||||
let original = ctx.env.host.and_then(|h| h.original()).map(|o| crate::util::fit_buffer(o, b.img.width, b.img.height, b.offset, b.scale));
|
||||
b.img.data.par_iter_mut().enumerate().for_each(|(i, px)| {
|
||||
let result = *px;
|
||||
let orig = result.map(|v| v * op);
|
||||
let orig = original.as_ref().and_then(|o| o.data.get(i)).copied().unwrap_or(result).map(|v| v * op);
|
||||
let mut o = if m == 1 { blend_pixel(BlendMode::Normal, orig, result, 0.5) } else { blend_pixel(mode, result, orig, 0.5) };
|
||||
o[3] = o[3].clamp(0.0, 1.0);
|
||||
if rgb_only {
|
||||
@@ -446,3 +446,53 @@ pub(crate) mod tests {
|
||||
assert!((sc.img.get(4, 3)[0] - 0.75).abs() < 1e-5);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod original_tests {
|
||||
use super::*;
|
||||
use crate::{EffectEnv, EffectHost, Image, LayerPixels};
|
||||
struct Original(Buf);
|
||||
impl EffectHost for Original {
|
||||
fn original(&self) -> Option<&Buf> {
|
||||
Some(&self.0)
|
||||
}
|
||||
fn layer(&self, _: u64, _: bool) -> Option<LayerPixels> {
|
||||
None
|
||||
}
|
||||
fn audio(&self, _: u64, _: f64, _: usize, _: u32) -> Option<Vec<f32>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn composite_preserves_alpha_opacity_and_padded_alignment() {
|
||||
let spec = crate::find("ec.channel.cccomposite").unwrap();
|
||||
let mut params = crate::Params { values: spec.params.iter().map(|p| (p.id.to_string(), p.default.clone())).collect() };
|
||||
let host = Original(Buf { img: Image::filled(1, 1, [0.5, 0.0, 0.0, 0.5]), offset: [0.0; 2], scale: 1.0 });
|
||||
for (mode, opacity, rgb, want) in [
|
||||
(0, 100.0, false, [0.5, 0.0, 0.125, 0.625]),
|
||||
(1, 100.0, false, [0.375, 0.0, 0.25, 0.625]),
|
||||
(0, 50.0, false, [0.25, 0.0, 0.1875, 0.4375]),
|
||||
(0, 100.0, true, [0.2, 0.0, 0.05, 0.25]),
|
||||
] {
|
||||
params.values.insert("compositeOriginal".into(), Value::Enum(mode));
|
||||
params.values.insert("opacity".into(), num(opacity));
|
||||
params.values.insert("rgbOnly".into(), Value::Bool(rgb));
|
||||
let ctx = EffectCtx {
|
||||
params: ¶ms,
|
||||
time: 0.0,
|
||||
layer_size: [1.0; 2],
|
||||
seed: 0,
|
||||
adjustment: false,
|
||||
env: EffectEnv { host: Some(&host), ..Default::default() },
|
||||
};
|
||||
let mut input = Buf { img: Image::filled(1, 1, [0.0, 0.0, 0.25, 0.25]), offset: [0.0; 2], scale: 1.0 };
|
||||
input.pad(1);
|
||||
let out = cc_composite(&ctx, input);
|
||||
for (a, b) in out.img.get(1, 1).iter().zip(want) {
|
||||
assert!((a - b).abs() < 1e-6, "{:?} vs {want:?}", out.img.get(1, 1));
|
||||
}
|
||||
assert_eq!(out.img.get(0, 0), [0.0; 4]);
|
||||
assert_eq!(out.offset, [1.0; 2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,8 +299,14 @@ pub struct LayerPixels {
|
||||
|
||||
/// Services the renderer offers effects that look beyond their own pixels.
|
||||
pub trait EffectHost: Sync {
|
||||
/// Pixels entering the effect stack, in effect space, before any effects. For an
|
||||
/// adjustment layer this is the composition below it, in the effect working space.
|
||||
fn original(&self) -> Option<&Buf> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Layer `id` of the same composition at the current comp time. `masks_and_effects`
|
||||
/// selects "Effects & Masks" over "Source". `None` for missing/self/recursive references.
|
||||
/// selects "Effects & Masks" over "Source". `None` for missing/recursive references; own Source reads are supported.
|
||||
fn layer(&self, id: u64, masks_and_effects: bool) -> Option<LayerPixels>;
|
||||
/// `frames` stereo sample frames (interleaved L R …) of layer `id`'s audio starting at comp
|
||||
/// time `start` seconds, at `rate` Hz. `None` when the layer has no audio.
|
||||
|
||||
@@ -450,6 +450,17 @@ pub fn fit_layer(ctx: &crate::EffectCtx, b: &crate::Buf, other: &crate::LayerPix
|
||||
})
|
||||
}
|
||||
|
||||
/// Resample a buffer into another buffer's pixel grid without stretching its layer space.
|
||||
/// Used to composite the pre-effect image after earlier effects expand or shift the bounds.
|
||||
pub fn fit_buffer(source: &crate::Buf, width: u32, height: u32, offset: [f64; 2], scale: f64) -> Image {
|
||||
let inv = 1.0 / scale.max(1e-9);
|
||||
gen_image(width, height, |x, y| {
|
||||
let sx = (x as f64 + 0.5 - offset[0]) * inv * source.scale + source.offset[0];
|
||||
let sy = (y as f64 + 0.5 - offset[1]) * inv * source.scale + source.offset[1];
|
||||
source.img.sample_bilinear(sx, sy)
|
||||
})
|
||||
}
|
||||
|
||||
/// The layer chosen in layer parameter `id` fitted to `b` (see [`fit_layer`]), or a copy of
|
||||
/// `b`'s own pixels when none is chosen / available (After Effects' "self" behaviour).
|
||||
pub fn layer_or_self(ctx: &crate::EffectCtx, b: &crate::Buf, id: &str, masks_and_effects: bool, stretch: bool) -> Image {
|
||||
|
||||
Reference in New Issue
Block a user