Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/ImageSharp.Drawing/Shapes/Rasterization/PolygonScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,37 @@ private void SkipEdgesBeforeMinY()
int i0 = 1;
int i1 = 0;

// Do fake scans for the lines belonging to edge start and endpoints before minY
// Do fake scans of the lines that start before minY.
// Instead of fake scanning at every possible subpixel Y location,
// only "scan" at start edge Y positions (defined by values in sorted0) and end Y positions (defined by values in sorted1).
// Walk the two lists simultaneously following mergesort logic.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Veeeery cool!

while (this.SubPixelY < this.minY)
{
this.EnterEdges();
this.LeaveEdges();
this.activeEdges.RemoveLeavingEdges();

float y0 = this.edges[this.sorted0[i0]].Y0;
float y1 = this.edges[this.sorted1[i1]].Y1;
bool hasMore0 = i0 < this.sorted0.Length;
bool hasMore1 = i1 < this.sorted1.Length;

if (!hasMore0 && !hasMore1)
{
// The entire polygon is outside the scan region, we skipped all edges,
// scanning will not find any intersections.
break;
}

float y0 = hasMore0 ? this.edges[this.sorted0[i0]].Y0 : float.MaxValue;
float y1 = hasMore1 ? this.edges[this.sorted1[i1]].Y1 : float.MaxValue;

if (y0 < y1)
{
this.SubPixelY = y1;
this.SubPixelY = y0;
i0++;
}
else
{
this.SubPixelY = y0;
this.SubPixelY = y1;
i1++;
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/ImageSharp.Drawing.Tests/Drawing/DrawPathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Numerics;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using Xunit;

namespace SixLabors.ImageSharp.Drawing.Tests.Drawing
Expand Down Expand Up @@ -74,5 +75,25 @@ public void PathExtendingOffEdgeOfImageShouldNotBeCropped<TPixel>(TestImageProvi
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);
}

[Theory]
[WithSolidFilledImages(40, 40, "White", PixelTypes.Rgba32)]
public void DrawPathClippedOnTop<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
PointF[] points =
{
new PointF(10f, -10f),
new PointF(20f, 20f),
new PointF(30f, -30f)
};

IPath path = new PathBuilder().AddLines(points).Build();

provider.VerifyOperation(
image => image.Mutate(x => x.Draw(Color.Black, 1, path)),
appendSourceFileOrDescription: false,
appendPixelTypeToFileName: false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public void NegativeOrientation01(IntersectionRule intersectionRule)
}

[Fact]
public void OutOfBounds()
public void OutOfBounds1()
{
IPath poly = PolygonFactory.CreatePolygon((1, -5), (5, -5), (5, -3), (10, -1), (10, 2), (12, 4), (1, 4));

Expand All @@ -445,6 +445,34 @@ public void OutOfBounds()
this.TestScan(poly, 0, 3, 2, expected);
}

[Fact]
public void OutOfBounds2()
{
IPath poly = PolygonFactory.CreatePolygon((3, -3), (3, 1), (1, 1), (1, -1), (2, -1.5f), (2, 0.5f), (3, -3));
FuzzyFloat[][] expected =
{
new FuzzyFloat[] { 1, 2, 2.14285707, 3 },
new FuzzyFloat[] { 1, 2, 2, 3 },
new FuzzyFloat[] { 1, 3 }
};

this.TestScan(poly, 0, 1, 2, expected);
}

[Fact]
public void AllOutOfBounds()
{
IPath poly = PolygonFactory.CreatePolygon((1, -3), (3, -3), (2, -1));
FuzzyFloat[][] expected =
{
Array.Empty<FuzzyFloat>(),
Array.Empty<FuzzyFloat>(),
Array.Empty<FuzzyFloat>(),
};

this.TestScan(poly, 0, 1, 2, expected);
}

private static (float Y, FuzzyFloat[] X) Empty(float y) => (y, Array.Empty<FuzzyFloat>());

private static FuzzyFloat F(float x, float eps) => new(x, eps);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.