Skip to content

Commit

Permalink
Simplified code in FragmentEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
Lehonti committed Nov 8, 2024
1 parent feac889 commit aa38e05
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions Pinta.Effects/Effects/FragmentEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ public FragmentEffect (IServiceProvider services)
public override Task<bool> LaunchConfiguration ()
=> chrome.LaunchSimpleEffectDialog (this);

#region Algorithm Code Ported From PDN
// Algorithm Code Ported From PDN

private static ImmutableArray<PointI> RecalcPointOffsets (int fragments, DegreesAngle rotationDegrees, int distance)
private static ImmutableArray<PointI> RecalcPointOffsets (
int fragments,
DegreesAngle rotationDegrees,
int distance)
{
double pointStep = RadiansAngle.MAX_RADIANS / fragments;

Expand All @@ -70,50 +73,44 @@ private static ImmutableArray<PointI> RecalcPointOffsets (int fragments, Degrees

public override void Render (ImageSurface src, ImageSurface dst, ReadOnlySpan<RectangleI> rois)
{
var pointOffsets = RecalcPointOffsets (Data.Fragments, Data.Rotation, Data.Distance);
var pointOffsets = RecalcPointOffsets (
Data.Fragments,
Data.Rotation,
Data.Distance);

int poLength = pointOffsets.Length;
Span<PointI> pointOffsetsPtr = stackalloc PointI[poLength];
Span<ColorBgra> samples = stackalloc ColorBgra[pointOffsets.Length];

for (int i = 0; i < poLength; ++i)
pointOffsetsPtr[i] = pointOffsets[i];

Span<ColorBgra> samples = stackalloc ColorBgra[poLength];

int src_width = src.Width;
int src_height = src.Height;
Size src_size = src.GetSize ();

ReadOnlySpan<ColorBgra> src_data = src.GetReadOnlyPixelData ();
Span<ColorBgra> 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
{
Expand Down

0 comments on commit aa38e05

Please sign in to comment.