diff --git a/crates/compose/src/advanced.rs b/crates/compose/src/advanced.rs index 672d793e52..add4485526 100644 --- a/crates/compose/src/advanced.rs +++ b/crates/compose/src/advanced.rs @@ -137,7 +137,9 @@ pub(crate) fn needs_floor(layers: &[Layer]) -> bool { fn knockout_shape_with(layer: &Layer, rect: Rect, cx: &Ctx, alloc: &A) -> Result, 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]); - if !layer.advanced.transparency_shapes { return Ok(masks()) } + if !layer.advanced.transparency_shapes { + return Ok(masks()); + } match render_content_with(layer, rect, cx, alloc)? { Some(b) => Ok(b.px.iter().map(|p| p[3]).collect()), None => Ok(masks()), @@ -209,7 +211,9 @@ pub(crate) fn composite_clipped_with(clipped: &[Layer], base: &m 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) { match &floor { - Some(f) if c.advanced.knockout != Knockout::None => knockout_with(c, base, f, true, cx, alloc, |b| crate::composite::composite_atop_with(c, b, cx, alloc))?, + Some(f) if c.advanced.knockout != Knockout::None => { + knockout_with(c, base, f, true, cx, alloc, |b| crate::composite::composite_atop_with(c, b, cx, alloc))? + } _ => crate::composite::composite_atop_with(c, base, cx, alloc)?, } } @@ -249,13 +253,17 @@ pub(crate) fn composite_clipped_individually_with( clip } }; - if clip.len() != n { return Ok(()) } + if clip.len() != n { + return Ok(()); + } for c in clipped.iter().filter(|c| c.visible) { let before = alloc.clone(backdrop)?; let t = knockout_target_with(c, floor.as_ref(), rect, cx, Scope { root: false, ..scope }, alloc)?; crate::composite::composite_layer_with(c, &[], backdrop, cx, t.as_deref(), scope, alloc)?; 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)); } + if *k < 1.0 { + *p = mix(*b, *p, k.max(0.0)); + } } } Ok(()) diff --git a/crates/compose/src/composite.rs b/crates/compose/src/composite.rs index e53b492fc5..5c13f249dd 100644 --- a/crates/compose/src/composite.rs +++ b/crates/compose/src/composite.rs @@ -2,10 +2,7 @@ use super::content::{channel_weights, empty_in, render_content_with, restore_cha use super::ctx::Ctx; use super::extents::{effects_frame, mask_k, mask_vals, recycle_mask, shapeless_stroke_bounds}; use super::fx_cache::effect_maps_with; -use super::{ - BlendMode, Buffer, BufferAlloc, Fill, InfallibleBufferAlloc, Layer, LayerContent, Rect, Surface, advanced, blend, effects, - psblend, shape_split, -}; +use super::{BlendMode, Buffer, BufferAlloc, Fill, InfallibleBufferAlloc, Layer, LayerContent, Rect, Surface, advanced, blend, effects, psblend, shape_split}; /// Composite a sibling list (bottom→top) onto `backdrop`; `scope` says where the stack sits (see /// [`advanced::Scope`]: it decides what knockouts reveal). @@ -295,7 +292,14 @@ pub(crate) fn composite_layer_with( Ok(()) } -fn composite_layer_any_with(layer: &Layer, clipped: &[Layer], backdrop: &mut Buffer, cx: &Ctx, scope: advanced::Scope, alloc: &A) -> Result<(), A::Error> { +fn composite_layer_any_with( + layer: &Layer, + clipped: &[Layer], + backdrop: &mut Buffer, + cx: &Ctx, + scope: advanced::Scope, + alloc: &A, +) -> Result<(), A::Error> { if let LayerContent::Group(g) = &layer.content && let Some(ab) = &g.artboard { @@ -460,7 +464,9 @@ fn composite_layer_plain_with( for ((p, w), wo) in backdrop.px.iter_mut().zip(&with.px).zip(&without.px) { let pa = p[3]; let mut pm = [p[0] * pa, p[1] * pa, p[2] * pa, pa]; - for c in 0..3 { pm[c] += w[c] * w[3] - wo[c] * wo[3]; } + for c in 0..3 { + pm[c] += w[c] * w[3] - wo[c] * wo[3]; + } pm[3] += w[3] - wo[3]; let a = pm[3].clamp(0.0, 1.0); *p = if a > 0.0 { [(pm[0] / a).clamp(0.0, 1.0), (pm[1] / a).clamp(0.0, 1.0), (pm[2] / a).clamp(0.0, 1.0), a] } else { [0.0; 4] }; @@ -616,12 +622,7 @@ fn shape_parts_with( /// layers. Fill and stroke are unmasked: the mask applies to them together. Layer effects paint /// the fill's interior effects beneath the vector stroke (psd-tools stroke-composite: a colour /// overlay leaves the stroke, the stroke effect covers it). -fn split_parts_with( - layer: &Layer, - rect: Rect, - cx: &Ctx, - alloc: &A, -) -> Result>)>, A::Error> { +fn split_parts_with(layer: &Layer, rect: Rect, cx: &Ctx, alloc: &A) -> Result>)>, A::Error> { let LayerContent::Shape(sh) = &layer.content else { return Ok(None) }; if sh.stroke.is_none() { return Ok(None); @@ -698,7 +699,9 @@ fn composite_atop_any_with(layer: &Layer, base: &mut Buffer, cx: }; let vstroke = parts.as_ref().map(|(s, m)| effects::VectorStroke { stroke: s, mask: m.as_deref() }); let mut opaque = alloc.clone(base)?; - for p in &mut opaque.px { p[3] = 1.0; } + for p in &mut opaque.px { + p[3] = 1.0; + } let maps = effect_maps_with(layer, rect, cx, alloc)?; effects::composite_with_effects_prepared_with(layer, &content, &mut opaque, &maps, effects_frame(layer, cx.canvas), cx.patterns, vstroke, alloc)?; if let Some((_, mask)) = parts { @@ -706,7 +709,9 @@ fn composite_atop_any_with(layer: &Layer, base: &mut Buffer, cx: } if let Some(m) = &hide { let mut plain = alloc.clone(base)?; - for p in &mut plain.px { p[3] = 1.0; } + for p in &mut plain.px { + p[3] = 1.0; + } advanced::apply_hiding_mask(&mut opaque, &plain, m); } recycle_mask(hide); diff --git a/crates/compose/src/content.rs b/crates/compose/src/content.rs index d42055471a..6e902dab18 100644 --- a/crates/compose/src/content.rs +++ b/crates/compose/src/content.rs @@ -2,7 +2,10 @@ use super::composite::{adjustment_quantum, blend_into}; use super::ctx::Ctx; use super::extents::{effect_shape, effect_source_bounds, mask_vals, recycle_mask, shapeless_stroke_bounds, transparent_outside}; use super::fx_cache::effect_maps; -use super::{BlendMode, Buffer, BufferAlloc, Document, Fill, InfallibleBufferAlloc, Layer, LayerContent, Pattern, Rect, Surface, bounds, effects, gradient_fill, pattern, shape_split}; +use super::{ + BlendMode, Buffer, BufferAlloc, Document, Fill, InfallibleBufferAlloc, Layer, LayerContent, Pattern, Rect, Surface, bounds, effects, gradient_fill, + pattern, shape_split, +}; /// Tile size for parallel rendering (tile results are independent). pub const RENDER_TILE: i32 = 256; diff --git a/crates/compose/src/ctx.rs b/crates/compose/src/ctx.rs index db3b4fcd93..6fe541678d 100644 --- a/crates/compose/src/ctx.rs +++ b/crates/compose/src/ctx.rs @@ -196,22 +196,14 @@ pub(crate) fn prepare_effects(layers: &[Layer], rect: Rect, cx: &Ctx, setting: i } /// Fallible counterpart of [`prepare_effects`], used by the compositor before it starts tiles. -pub(crate) fn prepare_effects_with( - layers: &[Layer], - rect: Rect, - cx: &Ctx, - alloc: &A, - setting: F, -) -> Result<(), A::Error> +pub(crate) fn prepare_effects_with(layers: &[Layer], rect: Rect, cx: &Ctx, alloc: &A, setting: F) -> Result<(), A::Error> where A: BufferAlloc + Sync, F: Fn(&mut dyn FnMut() -> Result<(), A::Error>) -> Result<(), A::Error> + Sync, { let mut todo = Vec::new(); effect_layers(layers, rect, 0, &mut todo); - let build = |l: &std::borrow::Cow| { - setting(&mut || effect_maps_with(l, rect, cx, alloc).map(|_| ())) - }; + let build = |l: &std::borrow::Cow| setting(&mut || effect_maps_with(l, rect, cx, alloc).map(|_| ())); #[cfg(not(target_arch = "wasm32"))] if todo.len() > 1 { use rayon::prelude::*; diff --git a/crates/compose/src/extents.rs b/crates/compose/src/extents.rs index c9cf0d7fad..587689e9bb 100644 --- a/crates/compose/src/extents.rs +++ b/crates/compose/src/extents.rs @@ -1,6 +1,8 @@ use super::content::{render_content_with, surface_to_buffer_with}; use super::ctx::Ctx; -use super::{BlendMode, BufferAlloc, Document, InfallibleBufferAlloc, Layer, LayerContent, Rect, adjustment_quantum, advanced, bounds, effects, masks, shape_split}; +use super::{ + BlendMode, BufferAlloc, Document, InfallibleBufferAlloc, Layer, LayerContent, Rect, adjustment_quantum, advanced, bounds, effects, masks, shape_split, +}; /// Bounds of a layer's own pixels (union over group children; the canvas /// for fill layers; an artboard's board). diff --git a/crates/compose/src/fx_cache.rs b/crates/compose/src/fx_cache.rs index 9d6eb33c07..791a102c59 100644 --- a/crates/compose/src/fx_cache.rs +++ b/crates/compose/src/fx_cache.rs @@ -213,12 +213,7 @@ pub(crate) fn effect_maps(layer: &Layer, rect: Rect, cx: &Ctx) -> std::sync::Arc } } -pub(crate) fn effect_maps_with( - layer: &Layer, - rect: Rect, - cx: &Ctx, - alloc: &A, -) -> Result, A::Error> { +pub(crate) fn effect_maps_with(layer: &Layer, rect: Rect, cx: &Ctx, alloc: &A) -> Result, A::Error> { use std::hash::{Hash, Hasher}; let (mut region, clipped) = effect_region(layer, rect, cx); // Non-local effects keep their whole source region, which a few distant pixels stretch over diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index 898c8faa05..a1376dcf28 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -57,12 +57,12 @@ pub use composite::{adjustment_quantum, blend_if_active, occludes_below, text_ga #[cfg(test)] use composite::{blend_into_fill, blend_into_g, branchless_floor, composite_layer_plain, composite_pixel_layer, pixel_surface, quantize}; use composite::{composite_stack_at, dissolve_noise}; -#[cfg(test)] -use content::render_fill; -pub use content::{RENDER_TILE, channel_weights, fill_frame, layer_shape, render_fill_content, stroke_frame, surface_to_buffer}; use content::empty_in; #[cfg(test)] use content::render_content; +#[cfg(test)] +use content::render_fill; +pub use content::{RENDER_TILE, channel_weights, fill_frame, layer_shape, render_fill_content, stroke_frame, surface_to_buffer}; use ctx::{Ctx, prepare_effects}; #[cfg(test)] use ctx::{LayerKeys, PREPARE_DEPTH, effect_layers, prepare_tones}; @@ -138,8 +138,7 @@ impl Buffer { /// Fallibly clone the pixel storage while keeping the same rectangle. pub fn try_clone(&self) -> Result { let bytes = self.px.len().checked_mul(std::mem::size_of::<[f32; 4]>()).ok_or(photocraft_raster::AllocationError::SizeOverflow)?; - let mut px = photocraft_raster::scratch::try_take(self.px.len(), [0.0; 4]) - .ok_or(photocraft_raster::AllocationError::NotEnoughMemory { bytes })?; + let mut px = photocraft_raster::scratch::try_take(self.px.len(), [0.0; 4]).ok_or(photocraft_raster::AllocationError::NotEnoughMemory { bytes })?; px.copy_from_slice(&self.px); Ok(Self { rect: self.rect, px }) } diff --git a/crates/compose/src/render.rs b/crates/compose/src/render.rs index 7fe0bacf75..14983ed73a 100644 --- a/crates/compose/src/render.rs +++ b/crates/compose/src/render.rs @@ -2,8 +2,8 @@ use super::composite::{composite_stack_at, composite_stack_at_with}; use super::content::RENDER_TILE; use super::ctx::{Ctx, RenderVectorMasks, prepare_effects_with, prepare_tones}; use super::{ - Buffer, BufferAlloc, Document, FallibleBufferAlloc, InfallibleBufferAlloc, Layer, Rect, Surface, adjust, advanced, cmyk, multichannel, native, - native_lab, pattern, psblend, + Buffer, BufferAlloc, Document, FallibleBufferAlloc, InfallibleBufferAlloc, Layer, Rect, Surface, adjust, advanced, cmyk, multichannel, native, native_lab, + pattern, psblend, }; /// Every layer the compositor draws, groups before their children: hidden layers and everything @@ -80,9 +80,7 @@ fn render_tiled_with_alloc(doc: &Document, rect: Rect, ti }); } // Effect maps are built once, here, before any tile needs them (#276). - prepare_effects_with(&doc.layers, rect, cx, alloc, |f| { - photocraft_color::convert::with_cmyk_space(cmyk, || in_blend_space(lab, hdr, f)) - })?; + prepare_effects_with(&doc.layers, rect, cx, alloc, |f| photocraft_color::convert::with_cmyk_space(cmyk, || in_blend_space(lab, hdr, f)))?; let run = |t: Rect| { photocraft_color::convert::with_cmyk_space(cmyk, || -> Result<_, A::Error> { let mut b = multichannel::backdrop_with(doc, t, alloc)?; @@ -194,7 +192,10 @@ mod fallible_bands_tests { use photocraft_geom::Size; use std::sync::atomic::{AtomicUsize, Ordering}; - struct FailBufferAt { fail_at: usize, attempts: AtomicUsize } + struct FailBufferAt { + fail_at: usize, + attempts: AtomicUsize, + } impl FailBufferAt { fn attempt(&self) -> Result<(), usize> { @@ -337,9 +338,17 @@ mod fallible_bands_tests { fn legacy_and_fallible_band_apis_deliver_the_same_pixels() { let doc = nested_document(); let mut legacy = Vec::new(); - render_bands(&doc, doc.bounds(), 8, |band| { legacy.push(band); Ok::<(), ()>(()) }).unwrap(); + render_bands(&doc, doc.bounds(), 8, |band| { + legacy.push(band); + Ok::<(), ()>(()) + }) + .unwrap(); let mut fallible = Vec::new(); - try_render_bands(&doc, doc.bounds(), 8, |band| { fallible.push(band); Ok::<(), ()>(()) }).unwrap(); + try_render_bands(&doc, doc.bounds(), 8, |band| { + fallible.push(band); + Ok::<(), ()>(()) + }) + .unwrap(); assert_eq!(legacy, fallible); } } @@ -362,12 +371,7 @@ pub fn render_bands(doc: &Document, rect: Rect, band_rows: i32, mut sink: imp } /// Fallible `render_bands` variant that reports compositor-owned RGBA buffer allocation failures. -pub fn try_render_bands( - doc: &Document, - rect: Rect, - band_rows: i32, - mut sink: impl FnMut(Buffer) -> Result<(), E>, -) -> Result<(), RenderBandsError> { +pub fn try_render_bands(doc: &Document, rect: Rect, band_rows: i32, mut sink: impl FnMut(Buffer) -> Result<(), E>) -> Result<(), RenderBandsError> { match render_bands_with_alloc(doc, rect, band_rows, &FallibleBufferAlloc, &mut sink) { Ok(()) => Ok(()), Err(RenderBandsCoreError::Allocation(error)) => Err(RenderBandsError::Allocation(error)), @@ -392,8 +396,7 @@ fn render_bands_with_alloc( let mut y = rect.y0; while y < rect.y1 { let y1 = y.saturating_add(rows).min(rect.y1); - let band = render_tiled_with_alloc(doc, Rect::new(rect.x0, y, rect.x1, y1), RENDER_TILE, &cx, alloc) - .map_err(RenderBandsCoreError::Allocation)?; + let band = render_tiled_with_alloc(doc, Rect::new(rect.x0, y, rect.x1, y1), RENDER_TILE, &cx, alloc).map_err(RenderBandsCoreError::Allocation)?; sink(band).map_err(RenderBandsCoreError::Sink)?; y = y1; }