mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Fix allocator policy imports and prune unused wrappers
This commit is contained in:
1 parent
5fccd6347b
commit
4edad0d7be
6 files changed
+7
-123
No files matched your search
@@ -32,8 +32,9 @@ use std::borrow::Cow;
|
||||
use photocraft_doc::{Knockout, Layer, LayerContent};
|
||||
use photocraft_geom::Rect;
|
||||
|
||||
use crate::content::render_content_with;
|
||||
use crate::extents::recycle_mask;
|
||||
use crate::{Buffer, BufferAlloc, Ctx, InfallibleBufferAlloc, composite_atop, composite_layer, effects, mask_vals, render_content, render_content_with};
|
||||
use crate::{Buffer, BufferAlloc, Ctx, effects, mask_vals};
|
||||
|
||||
/// Where a layer stack sits, for knockouts: at the document's top level, and/or inside an
|
||||
/// isolated (non-pass-through) group. Photoshop stops a deep knockout at the bottom of the
|
||||
@@ -99,15 +100,6 @@ pub fn is_background(l: &Layer) -> bool {
|
||||
l.has_background_traits()
|
||||
}
|
||||
|
||||
/// What a deep knockout reveals over `rect`: the document's initial backdrop with its Background
|
||||
/// layer (transparency when it has none, or when it is hidden).
|
||||
pub(crate) fn deep_target(cx: &Ctx, rect: Rect) -> Buffer {
|
||||
match deep_target_with(cx, rect, &InfallibleBufferAlloc) {
|
||||
Ok(buffer) => buffer,
|
||||
Err(never) => match never {},
|
||||
}
|
||||
}
|
||||
|
||||
fn deep_target_with<A: BufferAlloc>(cx: &Ctx, rect: Rect, alloc: &A) -> Result<Buffer, A::Error> {
|
||||
let Some(doc) = cx.doc else { return alloc.filled(rect, [0.0; 4]) };
|
||||
let mut b = crate::multichannel::backdrop_with(doc, rect, alloc)?;
|
||||
@@ -117,18 +109,6 @@ fn deep_target_with<A: BufferAlloc>(cx: &Ctx, rect: Rect, alloc: &A) -> Result<B
|
||||
Ok(b)
|
||||
}
|
||||
|
||||
/// The knockout target of a layer of a stack in `scope`, if it knocks out. `floor` is the
|
||||
/// stack's starting backdrop (`None` at the top level, where a shallow knockout reaches the
|
||||
/// Background too).
|
||||
pub(crate) fn knockout_target<'b>(layer: &Layer, floor: Option<&'b Buffer>, rect: Rect, cx: &Ctx, scope: Scope) -> Option<Cow<'b, Buffer>> {
|
||||
let deep = || if scope.isolated { Buffer::transparent(rect) } else { deep_target(cx, rect) };
|
||||
match layer.advanced.knockout {
|
||||
Knockout::None => None,
|
||||
Knockout::Shallow => Some(floor.map_or_else(|| Cow::Owned(deep()), Cow::Borrowed)),
|
||||
Knockout::Deep => Some(Cow::Owned(deep())),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn knockout_target_with<'b, A: BufferAlloc>(
|
||||
layer: &Layer,
|
||||
floor: Option<&'b Buffer>,
|
||||
@@ -154,20 +134,6 @@ pub(crate) fn needs_floor(layers: &[Layer]) -> bool {
|
||||
layers.iter().any(|l| l.visible && !l.clipped && l.advanced.knockout == Knockout::Shallow)
|
||||
}
|
||||
|
||||
/// The shape a layer knocks out over `rect` (row-major): its content's alpha with its masks;
|
||||
/// with Transparency Shapes Layer off (and for adjustment layers) just the masks.
|
||||
fn knockout_shape(layer: &Layer, rect: Rect, cx: &Ctx) -> Vec<f32> {
|
||||
let n = rect.width() as usize * rect.height() as usize;
|
||||
let masks = || mask_vals(layer, rect, cx).unwrap_or_else(|| vec![1.0; n]);
|
||||
if !layer.advanced.transparency_shapes {
|
||||
return masks();
|
||||
}
|
||||
match render_content(layer, rect, cx) {
|
||||
Some(b) => b.px.iter().map(|p| p[3]).collect(),
|
||||
None => masks(),
|
||||
}
|
||||
}
|
||||
|
||||
fn knockout_shape_with<A: BufferAlloc>(layer: &Layer, rect: Rect, cx: &Ctx, alloc: &A) -> Result<Vec<f32>, A::Error> {
|
||||
let n = rect.width() as usize * rect.height() as usize;
|
||||
let masks = || mask_vals(layer, rect, cx).unwrap_or_else(|| vec![1.0; n]);
|
||||
@@ -186,13 +152,6 @@ fn knockout_shape_with<A: BufferAlloc>(layer: &Layer, rect: Rect, cx: &Ctx, allo
|
||||
///
|
||||
/// `clip_alpha` keeps alpha from growing (clipped layers stay inside their base's alpha; a
|
||||
/// knockout to transparency may still lower it).
|
||||
pub(crate) fn knockout(layer: &Layer, backdrop: &mut Buffer, target: &Buffer, clip_alpha: bool, cx: &Ctx, draw: impl FnOnce(&mut Buffer)) {
|
||||
match knockout_with(layer, backdrop, target, clip_alpha, cx, &InfallibleBufferAlloc, |buffer| { draw(buffer); Ok(()) }) {
|
||||
Ok(()) => {}
|
||||
Err(never) => match never {},
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn knockout_with<A: BufferAlloc>(
|
||||
layer: &Layer,
|
||||
backdrop: &mut Buffer,
|
||||
@@ -246,20 +205,6 @@ pub(crate) fn knockout_with<A: BufferAlloc>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Composite a clipping group's clipped layers atop `base` (the base's isolated content, Blend
|
||||
/// Clipped Layers as Group on). The grouped clipping layers composite in isolation, like an
|
||||
/// isolated group, so a clipped layer's knockout (shallow or deep) stops at the bottom of the
|
||||
/// clipping group: the base's content.
|
||||
pub(crate) fn composite_clipped(clipped: &[Layer], base: &mut Buffer, cx: &Ctx) {
|
||||
let floor = clipped.iter().any(|c| c.visible && c.advanced.knockout != Knockout::None).then(|| base.clone());
|
||||
for c in clipped.iter().filter(|c| c.visible) {
|
||||
match &floor {
|
||||
Some(f) if c.advanced.knockout != Knockout::None => knockout(c, base, f, true, cx, |b| composite_atop(c, b, cx)),
|
||||
_ => composite_atop(c, base, cx),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn composite_clipped_with<A: BufferAlloc>(clipped: &[Layer], base: &mut Buffer, cx: &Ctx, alloc: &A) -> Result<(), A::Error> {
|
||||
let floor = if clipped.iter().any(|c| c.visible && c.advanced.knockout != Knockout::None) { Some(alloc.clone(base)?) } else { None };
|
||||
for c in clipped.iter().filter(|c| c.visible) {
|
||||
@@ -281,36 +226,6 @@ pub(crate) fn clips_individually(layer: &Layer, clipped: &[Layer]) -> bool {
|
||||
/// clipped layer composited in its own mode onto the result, inside the base's shape (its alpha
|
||||
/// with masks, times its opacity). A clipped layer's shallow knockout stops at the backdrop
|
||||
/// beneath the base (the bottom of the clipping group).
|
||||
pub(crate) fn composite_clipped_individually(layer: &Layer, clipped: &[Layer], backdrop: &mut Buffer, cx: &Ctx, ko: Option<&Buffer>, scope: Scope) {
|
||||
let rect = backdrop.rect;
|
||||
let n = backdrop.px.len();
|
||||
let floor = clipped.iter().any(|c| c.visible && c.advanced.knockout == Knockout::Shallow).then(|| backdrop.clone());
|
||||
composite_layer(layer, &[], backdrop, cx, ko, scope);
|
||||
let op = layer.opacity.clamp(0.0, 1.0);
|
||||
let clip: Vec<f32> = match render_content(layer, rect, cx) {
|
||||
Some(b) => b.px.iter().map(|p| p[3] * op).collect(),
|
||||
None => {
|
||||
let mv = mask_vals(layer, rect, cx);
|
||||
let clip = mv.as_ref().map_or_else(|| vec![op; n], |m| m.iter().map(|m| m * op).collect());
|
||||
recycle_mask(mv);
|
||||
clip
|
||||
}
|
||||
};
|
||||
if clip.len() != n {
|
||||
return;
|
||||
}
|
||||
for c in clipped.iter().filter(|c| c.visible) {
|
||||
let before = backdrop.clone();
|
||||
let t = knockout_target(c, floor.as_ref(), rect, cx, Scope { root: false, ..scope });
|
||||
composite_layer(c, &[], backdrop, cx, t.as_deref(), scope);
|
||||
for ((p, b), k) in backdrop.px.iter_mut().zip(&before.px).zip(&clip) {
|
||||
if *k < 1.0 {
|
||||
*p = mix(*b, *p, k.max(0.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn composite_clipped_individually_with<A: BufferAlloc>(
|
||||
layer: &Layer,
|
||||
clipped: &[Layer],
|
||||
|
||||
@@ -7,11 +7,6 @@ use super::{
|
||||
psblend, shape_split,
|
||||
};
|
||||
|
||||
/// Composite an isolated group's sibling list (bottom→top) onto `backdrop` (its own buffer).
|
||||
pub(crate) fn composite_stack(layers: &[Layer], backdrop: &mut Buffer, cx: &Ctx) {
|
||||
composite_stack_at(layers, backdrop, cx, advanced::Scope::ISOLATED);
|
||||
}
|
||||
|
||||
/// Composite a sibling list (bottom→top) onto `backdrop`; `scope` says where the stack sits (see
|
||||
/// [`advanced::Scope`]: it decides what knockouts reveal).
|
||||
pub(crate) fn composite_stack_at(layers: &[Layer], backdrop: &mut Buffer, cx: &Ctx, scope: advanced::Scope) {
|
||||
@@ -225,13 +220,6 @@ thread_local! {
|
||||
/// is mixed back towards `before` (the backdrop without it) where the ranges hide the layer.
|
||||
/// Mixing premultiplied colour by `k` equals compositing the layer at `k` × its alpha, since
|
||||
/// every blend mode's source-over result is linear in the source alpha.
|
||||
fn apply_blend_if(layer: &Layer, before: &Buffer, out: &mut Buffer, cx: &Ctx) {
|
||||
match apply_blend_if_with(layer, before, out, cx, &InfallibleBufferAlloc) {
|
||||
Ok(()) => {}
|
||||
Err(never) => match never {},
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_blend_if_with<A: BufferAlloc>(layer: &Layer, before: &Buffer, out: &mut Buffer, cx: &Ctx, alloc: &A) -> Result<(), A::Error> {
|
||||
// "This Layer" is the layer's own colour. An adjustment layer (no content of its own) is
|
||||
// judged by its result at full strength, before Opacity and its mask mix it in, as Photoshop
|
||||
@@ -275,13 +263,6 @@ fn apply_blend_if_with<A: BufferAlloc>(layer: &Layer, before: &Buffer, out: &mut
|
||||
|
||||
/// Composite `layer` (plus its clipping group) onto `backdrop`, honouring its channel restrictions
|
||||
/// and Blend If, and knocking out to `ko` when it has a knockout (see [`advanced`]).
|
||||
pub(crate) fn composite_layer(layer: &Layer, clipped: &[Layer], backdrop: &mut Buffer, cx: &Ctx, ko: Option<&Buffer>, scope: advanced::Scope) {
|
||||
match composite_layer_with(layer, clipped, backdrop, cx, ko, scope, &InfallibleBufferAlloc) {
|
||||
Ok(()) => {}
|
||||
Err(never) => match never {},
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn composite_layer_with<A: BufferAlloc>(
|
||||
layer: &Layer,
|
||||
clipped: &[Layer],
|
||||
@@ -393,6 +374,7 @@ pub(crate) fn composite_pixel_layer(layer: &Layer, surf: &Surface, backdrop: &mu
|
||||
recycle_mask(mv);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn composite_layer_plain(layer: &Layer, clipped: &[Layer], backdrop: &mut Buffer, cx: &Ctx, scope: advanced::Scope) {
|
||||
match composite_layer_plain_with(layer, clipped, backdrop, cx, scope, &InfallibleBufferAlloc) {
|
||||
Ok(()) => {}
|
||||
@@ -669,13 +651,6 @@ pub(crate) fn texture_ctx<'a>(layer: &Layer, region: Rect, cx: &Ctx<'a>) -> effe
|
||||
|
||||
/// Composite `layer` onto `base` restricted to the base's alpha (clipping mask semantics),
|
||||
/// honouring its channel restrictions and Blend If.
|
||||
pub(crate) fn composite_atop(layer: &Layer, base: &mut Buffer, cx: &Ctx) {
|
||||
match composite_atop_with(layer, base, cx, &InfallibleBufferAlloc) {
|
||||
Ok(()) => {}
|
||||
Err(never) => match never {},
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn composite_atop_with<A: BufferAlloc>(layer: &Layer, base: &mut Buffer, cx: &Ctx, alloc: &A) -> Result<(), A::Error> {
|
||||
let w = channel_weights(layer, cx.mode);
|
||||
let blend_if = blend_if_active(layer, cx.mode);
|
||||
|
||||
@@ -138,6 +138,7 @@ fn render_fill_with<A: BufferAlloc>(
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn surface_to_scratch(s: &Surface, rect: Rect) -> Buffer {
|
||||
let mut buf = Buffer::transparent(rect);
|
||||
s.read_rgba_into(rect, &mut buf.px);
|
||||
|
||||
@@ -44,7 +44,7 @@ use bevel::{bevel_maps, satin_map};
|
||||
#[cfg(test)]
|
||||
use blur::{blur, box_line, dilate, tent, tent_direct};
|
||||
pub use blur::{size_one_kernel, tent_kernel};
|
||||
pub(crate) use composite::{VectorStroke, build_maps_prepared, composite_with_effects_prepared, map_count};
|
||||
pub(crate) use composite::{VectorStroke, build_maps_prepared, composite_with_effects_prepared, composite_with_effects_prepared_with, map_count};
|
||||
pub use composite::{build_maps, composite_with_effects, outline_share, premerges_layer, shadow_see_through, stroke_widths};
|
||||
pub use distance::{FieldKind, STROKE_CHAMFER_MAX, bevel_distance_fields, distance_field, stroke_outside_exact};
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -3,8 +3,9 @@ use super::distance::{INSIDE_EPS, Metric, dist_inside_by, dist_outside_by, local
|
||||
use super::paint::{dissolves_in, glow_map, paint_color, paint_fx, paint_glow, paint_pattern, rgb, shadow_map};
|
||||
use super::{
|
||||
Bevel, BevelStyle, BlendMode, Buffer, Effect, FxMaps, FxPaint, GlobalLight, Layer, MAX_REACH, Map, PREPARED_PATTERN_BYTES, Pattern, Placement,
|
||||
BufferAlloc, InfallibleBufferAlloc, PreparedPatterns, Rect, StrokePosition, psblend,
|
||||
PreparedPatterns, Rect, StrokePosition, psblend,
|
||||
};
|
||||
use crate::{BufferAlloc, InfallibleBufferAlloc};
|
||||
|
||||
/// Build every effect map for `layer` from its alpha `shape` over `rect`.
|
||||
pub fn build_maps(layer: &Layer, shape: Vec<f32>, rect: Rect, light: &GlobalLight, tex: &TextureCtx) -> FxMaps {
|
||||
|
||||
@@ -191,14 +191,6 @@ pub fn inks(doc: &Document, rect: Rect) -> Buffer {
|
||||
buf
|
||||
}
|
||||
|
||||
/// The starting backdrop of a document render: the inks for Multichannel, else transparent.
|
||||
pub(crate) fn backdrop(doc: &Document, rect: Rect) -> Buffer {
|
||||
match backdrop_with(doc, rect, &crate::InfallibleBufferAlloc) {
|
||||
Ok(buffer) => buffer,
|
||||
Err(never) => match never {},
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn backdrop_with<A: crate::BufferAlloc>(doc: &Document, rect: Rect, alloc: &A) -> Result<Buffer, A::Error> {
|
||||
if doc.mode == ColorMode::Multichannel {
|
||||
let mut buf = alloc.filled(rect, [1.0; 4])?;
|
||||
|
||||
Reference in new issue
Block a user