Skip to content

Commit eda9825

Browse files
authored
Merge branch 'master' into sw/clear
2 parents bbc1afe + 25c3921 commit eda9825

2 files changed

Lines changed: 113 additions & 1 deletion

File tree

src/ImageSharp.Drawing/Processing/Processors/Drawing/FillPathProcessor.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using SixLabors.ImageSharp.PixelFormats;
56
using SixLabors.ImageSharp.Processing.Processors;
67

@@ -43,6 +44,21 @@ public FillPathProcessor(ShapeGraphicsOptions options, IBrush brush, IPath shape
4344
/// <inheritdoc />
4445
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle)
4546
where TPixel : unmanaged, IPixel<TPixel>
46-
=> new FillRegionProcessor(this.Options, this.Brush, new ShapeRegion(this.Shape)).CreatePixelSpecificProcessor(configuration, source, sourceRectangle);
47+
{
48+
if (this.Shape is RectangularPolygon rectPoly)
49+
{
50+
var rectF = new RectangleF(rectPoly.Location, rectPoly.Size);
51+
var rect = (Rectangle)rectF;
52+
if (this.Options.GraphicsOptions.Antialias == false || rectF == rect)
53+
{
54+
var interest = Rectangle.Intersect(sourceRectangle, rect);
55+
56+
// cast as in and back are the same or we are using anti-aliasing
57+
return new FillProcessor(this.Options.GraphicsOptions, this.Brush).CreatePixelSpecificProcessor(configuration, source, interest);
58+
}
59+
}
60+
61+
return new FillRegionProcessor(this.Options, this.Brush, new ShapeRegion(this.Shape)).CreatePixelSpecificProcessor(configuration, source, sourceRectangle);
62+
}
4763
}
4864
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using SixLabors.ImageSharp.Drawing.Processing;
6+
using SixLabors.ImageSharp.Drawing.Processing.Processors.Drawing;
7+
using SixLabors.ImageSharp.PixelFormats;
8+
using Xunit;
9+
10+
namespace SixLabors.ImageSharp.Drawing.Tests.Processing
11+
{
12+
public class FillPathProcessorTests
13+
{
14+
[Fact]
15+
public void OtherShape()
16+
{
17+
var imageSize = new Rectangle(0, 0, 500, 500);
18+
var path = new EllipsePolygon(1, 1, 23);
19+
var processor = new FillPathProcessor(new ImageSharp.Drawing.Processing.ShapeGraphicsOptions()
20+
{
21+
GraphicsOptions = {
22+
Antialias = true
23+
}
24+
}, Brushes.Solid(Color.Red), path);
25+
var pixelProcessor = processor.CreatePixelSpecificProcessor<Rgba32>(null, null, imageSize);
26+
27+
Assert.IsType<FillRegionProcessor<Rgba32>>(pixelProcessor);
28+
}
29+
30+
[Fact]
31+
public void RectangleFloatAndAntialias()
32+
{
33+
var imageSize = new Rectangle(0, 0, 500, 500);
34+
var floatRect = new RectangleF(10.5f, 10.5f, 400.6f, 400.9f);
35+
var expectedRect = new Rectangle(10, 10, 400, 400);
36+
var path = new RectangularPolygon(floatRect);
37+
var processor = new FillPathProcessor(new ImageSharp.Drawing.Processing.ShapeGraphicsOptions()
38+
{
39+
GraphicsOptions = {
40+
Antialias = true
41+
}
42+
}, Brushes.Solid(Color.Red), path);
43+
var pixelProcessor = processor.CreatePixelSpecificProcessor<Rgba32>(null, null, imageSize);
44+
45+
Assert.IsType<FillRegionProcessor<Rgba32>>(pixelProcessor);
46+
}
47+
48+
[Fact]
49+
public void IntRectangle()
50+
{
51+
var imageSize = new Rectangle(0, 0, 500, 500);
52+
var expectedRect = new Rectangle(10, 10, 400, 400);
53+
var path = new RectangularPolygon(expectedRect);
54+
var processor = new FillPathProcessor(new ImageSharp.Drawing.Processing.ShapeGraphicsOptions()
55+
{
56+
GraphicsOptions = {
57+
Antialias = true
58+
}
59+
}, Brushes.Solid(Color.Red), path);
60+
var pixelProcessor = processor.CreatePixelSpecificProcessor<Rgba32>(null, null, imageSize);
61+
62+
var fill = Assert.IsType<FillProcessor<Rgba32>>(pixelProcessor);
63+
Assert.Equal(expectedRect, fill.GetProtectedValue<Rectangle>("SourceRectangle"));
64+
}
65+
66+
[Fact]
67+
public void FloatRectAntialiasingOff()
68+
{
69+
var imageSize = new Rectangle(0, 0, 500, 500);
70+
var floatRect = new RectangleF(10.5f, 10.5f, 400.6f, 400.9f);
71+
var expectedRect = new Rectangle(10, 10, 400, 400);
72+
var path = new RectangularPolygon(floatRect);
73+
var processor = new FillPathProcessor(new ImageSharp.Drawing.Processing.ShapeGraphicsOptions()
74+
{
75+
GraphicsOptions = {
76+
Antialias = false
77+
}
78+
}, Brushes.Solid(Color.Red), path);
79+
var pixelProcessor = processor.CreatePixelSpecificProcessor<Rgba32>(null, null, imageSize);
80+
81+
var fill = Assert.IsType<FillProcessor<Rgba32>>(pixelProcessor);
82+
Assert.Equal(expectedRect, fill.GetProtectedValue<Rectangle>("SourceRectangle"));
83+
}
84+
}
85+
86+
internal static class ReflectionHelpers
87+
{
88+
internal static T GetProtectedValue<T>(this object obj, string name)
89+
{
90+
return (T)obj.GetType()
91+
.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.FlattenHierarchy)
92+
.Single(x => x.Name == name)
93+
.GetValue(obj);
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)