From aa38e05560101a7c85634f6f6e1bc2b222eba5e3 Mon Sep 17 00:00:00 2001 From: Lehonti Ramos <17771375+Lehonti@users.noreply.github.com> Date: Fri, 8 Nov 2024 22:14:57 +0100 Subject: [PATCH] Simplified code in `FragmentEffect` --- Pinta.Effects/Effects/FragmentEffect.cs | 53 ++++++++++++------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/Pinta.Effects/Effects/FragmentEffect.cs b/Pinta.Effects/Effects/FragmentEffect.cs index 74b5222ad..23279e235 100644 --- a/Pinta.Effects/Effects/FragmentEffect.cs +++ b/Pinta.Effects/Effects/FragmentEffect.cs @@ -47,9 +47,12 @@ public FragmentEffect (IServiceProvider services) public override Task LaunchConfiguration () => chrome.LaunchSimpleEffectDialog (this); - #region Algorithm Code Ported From PDN + // Algorithm Code Ported From PDN - private static ImmutableArray RecalcPointOffsets (int fragments, DegreesAngle rotationDegrees, int distance) + private static ImmutableArray RecalcPointOffsets ( + int fragments, + DegreesAngle rotationDegrees, + int distance) { double pointStep = RadiansAngle.MAX_RADIANS / fragments; @@ -70,50 +73,44 @@ private static ImmutableArray RecalcPointOffsets (int fragments, Degrees public override void Render (ImageSurface src, ImageSurface dst, ReadOnlySpan rois) { - var pointOffsets = RecalcPointOffsets (Data.Fragments, Data.Rotation, Data.Distance); + var pointOffsets = RecalcPointOffsets ( + Data.Fragments, + Data.Rotation, + Data.Distance); - int poLength = pointOffsets.Length; - Span pointOffsetsPtr = stackalloc PointI[poLength]; + Span samples = stackalloc ColorBgra[pointOffsets.Length]; - for (int i = 0; i < poLength; ++i) - pointOffsetsPtr[i] = pointOffsets[i]; - - Span samples = stackalloc ColorBgra[poLength]; - - int src_width = src.Width; - int src_height = src.Height; + Size src_size = src.GetSize (); ReadOnlySpan src_data = src.GetReadOnlyPixelData (); Span dst_data = dst.GetPixelData (); foreach (RectangleI rect in rois) { - for (int y = rect.Top; y <= rect.Bottom; y++) { - - var dst_row = dst_data.Slice (y * src_width, src_width); + foreach (var pixel in Utility.GeneratePixelOffsets (rect, src_size)) { - for (int x = rect.Left; x <= rect.Right; x++) { + int sampleCount = 0; - int sampleCount = 0; + for (int i = 0; i < pointOffsets.Length; ++i) { - for (int i = 0; i < poLength; ++i) { + int u = pixel.coordinates.X - pointOffsets[i].X; + int v = pixel.coordinates.Y - pointOffsets[i].Y; - int u = x - pointOffsetsPtr[i].X; - int v = y - pointOffsetsPtr[i].Y; + if (u < 0 || u >= src_size.Width || v < 0 || v >= src_size.Height) + continue; - if (u < 0 || u >= src_width || v < 0 || v >= src_height) - continue; + samples[sampleCount] = src.GetColorBgra ( + src_data, + src_size.Width, + new (u, v)); - samples[sampleCount] = src.GetColorBgra (src_data, src_width, new (u, v)); - ++sampleCount; - } - - dst_row[x] = ColorBgra.Blend (samples[..sampleCount]); + ++sampleCount; } + + dst_data[pixel.memoryOffset] = ColorBgra.Blend (samples[..sampleCount]); } } } - #endregion public sealed class FragmentData : EffectData {