|
3 | 3 |
|
4 | 4 | using System; |
5 | 5 | using System.Buffers; |
6 | | -using SixLabors.ImageSharp.Advanced; |
| 6 | +using SixLabors.ImageSharp.Drawing.Shapes; |
| 7 | +using SixLabors.ImageSharp.Drawing.Shapes.Rasterization; |
7 | 8 | using SixLabors.ImageSharp.Memory; |
8 | 9 | using SixLabors.ImageSharp.PixelFormats; |
9 | 10 | using SixLabors.ImageSharp.Processing.Processors; |
@@ -54,127 +55,85 @@ protected override void OnFrameApply(ImageFrame<TPixel> source) |
54 | 55 | return; // no effect inside image; |
55 | 56 | } |
56 | 57 |
|
57 | | - int maxIntersections = region.MaxIntersections; |
58 | | - float subpixelCount = 4; |
| 58 | + int subpixelCount = FillRegionProcessor.MinimumSubpixelCount; |
59 | 59 |
|
60 | 60 | // we need to offset the pixel grid to account for when we outline a path. |
61 | 61 | // basically if the line is [1,2] => [3,2] then when outlining at 1 we end up with a region of [0.5,1.5],[1.5, 1.5],[3.5,2.5],[2.5,2.5] |
62 | 62 | // and this can cause missed fills when not using antialiasing.so we offset the pixel grid by 0.5 in the x & y direction thus causing the# |
63 | 63 | // region to align with the pixel grid. |
64 | 64 | if (graphicsOptions.Antialias) |
65 | 65 | { |
66 | | - subpixelCount = graphicsOptions.AntialiasSubpixelDepth; |
67 | | - if (subpixelCount < 4) |
68 | | - { |
69 | | - subpixelCount = 4; |
70 | | - } |
| 66 | + subpixelCount = Math.Max(subpixelCount, graphicsOptions.AntialiasSubpixelDepth); |
71 | 67 | } |
72 | 68 |
|
73 | | - using (BrushApplicator<TPixel> applicator = brush.CreateApplicator(configuration, graphicsOptions, source, rect)) |
| 69 | + using BrushApplicator<TPixel> applicator = brush.CreateApplicator(configuration, graphicsOptions, source, rect); |
| 70 | + int scanlineWidth = maxX - minX; |
| 71 | + MemoryAllocator allocator = this.Configuration.MemoryAllocator; |
| 72 | + bool scanlineDirty = true; |
| 73 | + |
| 74 | + var scanner = PolygonScanner.Create( |
| 75 | + region.Shape, |
| 76 | + minY, |
| 77 | + maxY, |
| 78 | + subpixelCount, |
| 79 | + shapeOptions.IntersectionRule, |
| 80 | + configuration.MemoryAllocator); |
| 81 | + |
| 82 | + try |
74 | 83 | { |
75 | | - int scanlineWidth = maxX - minX; |
76 | | - MemoryAllocator allocator = this.Configuration.MemoryAllocator; |
77 | | - using (IMemoryOwner<float> bBuffer = allocator.Allocate<float>(maxIntersections)) |
78 | | - using (IMemoryOwner<float> bScanline = allocator.Allocate<float>(scanlineWidth)) |
| 84 | + using IMemoryOwner<float> bScanline = allocator.Allocate<float>(scanlineWidth); |
| 85 | + Span<float> scanline = bScanline.Memory.Span; |
| 86 | + |
| 87 | + while (scanner.MoveToNextPixelLine()) |
79 | 88 | { |
80 | | - bool scanlineDirty = true; |
81 | | - float subpixelFraction = 1f / subpixelCount; |
82 | | - float subpixelFractionPoint = subpixelFraction / subpixelCount; |
| 89 | + if (scanlineDirty) |
| 90 | + { |
| 91 | + scanline.Clear(); |
| 92 | + } |
83 | 93 |
|
84 | | - Span<float> buffer = bBuffer.Memory.Span; |
85 | | - Span<float> scanline = bScanline.Memory.Span; |
| 94 | + scanlineDirty = scanner.ScanCurrentPixelLineInto(minX, 0, scanline); |
86 | 95 |
|
87 | | - for (int y = minY; y < maxY; y++) |
| 96 | + if (scanlineDirty) |
88 | 97 | { |
89 | | - if (scanlineDirty) |
| 98 | + int y = scanner.PixelLineY; |
| 99 | + if (!graphicsOptions.Antialias) |
90 | 100 | { |
91 | | - scanline.Clear(); |
92 | | - scanlineDirty = false; |
93 | | - } |
94 | | - |
95 | | - float yPlusOne = y + 1; |
96 | | - for (float subPixel = y; subPixel < yPlusOne; subPixel += subpixelFraction) |
97 | | - { |
98 | | - int pointsFound = region.Scan(subPixel, buffer, configuration, shapeOptions.IntersectionRule); |
99 | | - if (pointsFound == 0) |
| 101 | + bool hasOnes = false; |
| 102 | + bool hasZeros = false; |
| 103 | + for (int x = 0; x < scanline.Length; x++) |
100 | 104 | { |
101 | | - // nothing on this line, skip |
102 | | - continue; |
103 | | - } |
104 | | - |
105 | | - for (int point = 0; point < pointsFound && point < buffer.Length - 1; point += 2) |
106 | | - { |
107 | | - // points will be paired up |
108 | | - float scanStart = buffer[point] - minX; |
109 | | - float scanEnd = buffer[point + 1] - minX; |
110 | | - int startX = (int)MathF.Floor(scanStart); |
111 | | - int endX = (int)MathF.Floor(scanEnd); |
112 | | - |
113 | | - if (startX >= 0 && startX < scanline.Length) |
| 105 | + if (scanline[x] >= 0.5) |
114 | 106 | { |
115 | | - for (float x = scanStart; x < startX + 1; x += subpixelFraction) |
116 | | - { |
117 | | - scanline[startX] += subpixelFractionPoint; |
118 | | - scanlineDirty = true; |
119 | | - } |
| 107 | + scanline[x] = 1; |
| 108 | + hasOnes = true; |
120 | 109 | } |
121 | | - |
122 | | - if (endX >= 0 && endX < scanline.Length) |
123 | | - { |
124 | | - for (float x = endX; x < scanEnd; x += subpixelFraction) |
125 | | - { |
126 | | - scanline[endX] += subpixelFractionPoint; |
127 | | - scanlineDirty = true; |
128 | | - } |
129 | | - } |
130 | | - |
131 | | - int nextX = startX + 1; |
132 | | - endX = Math.Min(endX, scanline.Length); // reduce to end to the right edge |
133 | | - nextX = Math.Max(nextX, 0); |
134 | | - for (int x = nextX; x < endX; x++) |
| 110 | + else |
135 | 111 | { |
136 | | - scanline[x] += subpixelFraction; |
137 | | - scanlineDirty = true; |
| 112 | + scanline[x] = 0; |
| 113 | + hasZeros = true; |
138 | 114 | } |
139 | 115 | } |
140 | | - } |
141 | 116 |
|
142 | | - if (scanlineDirty) |
143 | | - { |
144 | | - if (!graphicsOptions.Antialias) |
| 117 | + if (isSolidBrushWithoutBlending && hasOnes != hasZeros) |
145 | 118 | { |
146 | | - bool hasOnes = false; |
147 | | - bool hasZeros = false; |
148 | | - for (int x = 0; x < scanlineWidth; x++) |
| 119 | + if (hasOnes) |
149 | 120 | { |
150 | | - if (scanline[x] >= 0.5) |
151 | | - { |
152 | | - scanline[x] = 1; |
153 | | - hasOnes = true; |
154 | | - } |
155 | | - else |
156 | | - { |
157 | | - scanline[x] = 0; |
158 | | - hasZeros = true; |
159 | | - } |
| 121 | + source.GetPixelRowSpan(y).Slice(minX, scanlineWidth).Fill(solidBrushColor); |
160 | 122 | } |
161 | 123 |
|
162 | | - if (isSolidBrushWithoutBlending && hasOnes != hasZeros) |
163 | | - { |
164 | | - if (hasOnes) |
165 | | - { |
166 | | - source.GetPixelRowSpan(y).Slice(minX, scanlineWidth).Fill(solidBrushColor); |
167 | | - } |
168 | | - |
169 | | - continue; |
170 | | - } |
| 124 | + continue; |
171 | 125 | } |
172 | | - |
173 | | - applicator.Apply(scanline, minX, y); |
174 | 126 | } |
| 127 | + |
| 128 | + applicator.Apply(scanline, minX, y); |
175 | 129 | } |
176 | 130 | } |
177 | 131 | } |
| 132 | + finally |
| 133 | + { |
| 134 | + // ref structs can't implement interfaces so technically PolygonScanner is not IDisposable |
| 135 | + scanner.Dispose(); |
| 136 | + } |
178 | 137 | } |
179 | 138 |
|
180 | 139 | private static bool IsSolidBrushWithoutBlending(GraphicsOptions options, IBrush inputBrush, out SolidBrush solidBrush) |
|
0 commit comments