Skip to content

Commit c29e5de

Browse files
Merge pull request #119 from SixLabors/js/fix-108
Fix IndexOutOfRangeException when drawing lines outside of image
2 parents 5b168f9 + 15dad17 commit c29e5de

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

src/ImageSharp.Drawing/Shapes/Rasterization/PolygonScanner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ private void SkipEdgesBeforeMinY()
151151

152152
if (y0 < y1)
153153
{
154-
this.SubPixelY = y0;
154+
this.SubPixelY = y1;
155155
i0++;
156156
}
157157
else
158158
{
159-
this.SubPixelY = y1;
159+
this.SubPixelY = y0;
160160
i1++;
161161
}
162162
}

tests/ImageSharp.Drawing.Tests/Issues/Issue_28.cs renamed to tests/ImageSharp.Drawing.Tests/Issues/Issue_28_108.cs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,72 +10,87 @@
1010

1111
namespace SixLabors.ImageSharp.Drawing.Tests.Issues
1212
{
13-
public class Issue_28
13+
public class Issue_28_108
1414
{
1515
private Rgba32 red = Color.Red.ToRgba32();
1616

17-
[Fact]
18-
public void DrawingLineAtTopShouldDisplay()
17+
[Theory]
18+
[InlineData(1F)]
19+
[InlineData(1.5F)]
20+
[InlineData(2F)]
21+
[InlineData(3F)]
22+
public void DrawingLineAtTopShouldDisplay(float stroke)
1923
{
2024
using var image = new Image<Rgba32>(Configuration.Default, 100, 100, Color.Black);
2125
image.Mutate(x => x
2226
.SetGraphicsOptions(g => g.Antialias = false)
2327
.DrawLines(
2428
this.red,
25-
1f,
29+
stroke,
2630
new PointF(0, 0),
2731
new PointF(100, 0)));
2832

2933
IEnumerable<(int x, int y)> locations = Enumerable.Range(0, 100).Select(i => (x: i, y: 0));
3034
Assert.All(locations, l => Assert.Equal(this.red, image[l.x, l.y]));
3135
}
3236

33-
[Fact]
34-
public void DrawingLineAtBottomShouldDisplay()
37+
[Theory]
38+
[InlineData(1F)]
39+
[InlineData(1.5F)]
40+
[InlineData(2F)]
41+
[InlineData(3F)]
42+
public void DrawingLineAtBottomShouldDisplay(float stroke)
3543
{
3644
using var image = new Image<Rgba32>(Configuration.Default, 100, 100, Color.Black);
3745
image.Mutate(x => x
3846
.SetGraphicsOptions(g => g.Antialias = false)
3947
.DrawLines(
4048
this.red,
41-
1f,
49+
stroke,
4250
new PointF(0, 99),
4351
new PointF(100, 99)));
4452

4553
IEnumerable<(int x, int y)> locations = Enumerable.Range(0, 100).Select(i => (x: i, y: 99));
4654
Assert.All(locations, l => Assert.Equal(this.red, image[l.x, l.y]));
4755
}
4856

49-
[Fact]
50-
public void DrawingLineAtLeftShouldDisplay()
57+
[Theory]
58+
[InlineData(1F)]
59+
[InlineData(1.5F)]
60+
[InlineData(2F)]
61+
[InlineData(3F)]
62+
public void DrawingLineAtLeftShouldDisplay(float stroke)
5163
{
5264
using var image = new Image<Rgba32>(Configuration.Default, 100, 100, Color.Black);
5365
image.Mutate(x => x
5466
.SetGraphicsOptions(g => g.Antialias = false)
5567
.DrawLines(
5668
this.red,
57-
1f,
69+
stroke,
5870
new PointF(0, 0),
5971
new PointF(0, 99)));
6072

6173
IEnumerable<(int x, int y)> locations = Enumerable.Range(0, 100).Select(i => (x: 0, y: i));
6274
Assert.All(locations, l => Assert.Equal(this.red, image[l.x, l.y]));
6375
}
6476

65-
[Fact]
66-
public void DrawingLineAtRightShouldDisplay()
77+
[Theory]
78+
[InlineData(1F)]
79+
[InlineData(1.5F)]
80+
[InlineData(2F)]
81+
[InlineData(3F)]
82+
public void DrawingLineAtRightShouldDisplay(float stroke)
6783
{
6884
using var image = new Image<Rgba32>(Configuration.Default, 100, 100, Color.Black);
6985
image.Mutate(x => x
7086
.SetGraphicsOptions(g => g.Antialias = false)
7187
.DrawLines(
7288
this.red,
73-
1f,
89+
stroke,
7490
new PointF(99, 0),
7591
new PointF(99, 99)));
7692

7793
IEnumerable<(int x, int y)> locations = Enumerable.Range(0, 100).Select(i => (x: 99, y: i));
78-
7994
Assert.All(locations, l => Assert.Equal(this.red, image[l.x, l.y]));
8095
}
8196
}

tests/ImageSharp.Drawing.Tests/Shapes/Scan/PolygonScannerTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,25 @@ public void NegativeOrientation01(IntersectionRule intersectionRule)
426426
this.TestScan(poly, 0, 2, 2, expected, intersectionRule);
427427
}
428428

429+
[Fact]
430+
public void OutOfBounds()
431+
{
432+
IPath poly = PolygonFactory.CreatePolygon((1, -5), (5, -5), (5, -3), (10, -1), (10, 2), (12, 4), (1, 4));
433+
434+
FuzzyFloat[][] exected =
435+
{
436+
new FuzzyFloat[] { 1, 10 },
437+
new FuzzyFloat[] { 1, 10 },
438+
new FuzzyFloat[] { 1, 10 },
439+
new FuzzyFloat[] { 1, 10 },
440+
new FuzzyFloat[] { 1, 10 },
441+
new FuzzyFloat[] { 1, 10.5 },
442+
new FuzzyFloat[] { 1, 11 },
443+
};
444+
445+
this.TestScan(poly, 0, 3, 2, exected);
446+
}
447+
429448
private static (float y, FuzzyFloat[] x) Empty(float y) => (y, new FuzzyFloat[0]);
430449

431450
private static FuzzyFloat F(float x, float eps) => new FuzzyFloat(x, eps);

0 commit comments

Comments
 (0)