Skip to content

Commit 2d2b3e1

Browse files
committed
Merge branch 'master' into GradientBrush
2 parents 29a37d0 + 2ca8950 commit 2d2b3e1

File tree

2 files changed

+65
-30
lines changed

2 files changed

+65
-30
lines changed

src/ImageSharp.Drawing/Processing/Drawing/Brushes/SolidBrush{TPixel}.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,23 @@ internal override void Apply(Span<float> scanline, int x, int y)
9090

9191
MemoryManager memoryManager = this.Target.MemoryManager;
9292

93-
using (IBuffer<float> amountBuffer = memoryManager.Allocate<float>(scanline.Length))
93+
if (this.Options.BlendPercentage == 1f)
9494
{
95-
Span<float> amountSpan = amountBuffer.Span;
96-
97-
for (int i = 0; i < scanline.Length; i++)
95+
this.Blender.Blend(memoryManager, destinationRow, destinationRow, this.Colors.Span, scanline);
96+
}
97+
else
98+
{
99+
using (IBuffer<float> amountBuffer = memoryManager.Allocate<float>(scanline.Length))
98100
{
99-
amountSpan[i] = scanline[i] * this.Options.BlendPercentage;
100-
}
101+
Span<float> amountSpan = amountBuffer.Span;
102+
103+
for (int i = 0; i < scanline.Length; i++)
104+
{
105+
amountSpan[i] = scanline[i] * this.Options.BlendPercentage;
106+
}
101107

102-
this.Blender.Blend(memoryManager, destinationRow, destinationRow, this.Colors.Span, amountSpan);
108+
this.Blender.Blend(memoryManager, destinationRow, destinationRow, this.Colors.Span, amountSpan);
109+
}
103110
}
104111
}
105112
}

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

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Threading.Tasks;
6+
using SixLabors.ImageSharp.Advanced;
67
using SixLabors.ImageSharp.Memory;
78
using SixLabors.ImageSharp.PixelFormats;
89
using SixLabors.ImageSharp.Processing.Drawing.Brushes;
@@ -49,39 +50,66 @@ protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle source
4950
int minY = Math.Max(0, startY);
5051
int maxY = Math.Min(source.Height, endY);
5152

52-
// Reset offset if necessary.
53-
if (minX > 0)
54-
{
55-
startX = 0;
56-
}
57-
58-
if (minY > 0)
59-
{
60-
startY = 0;
61-
}
62-
6353
int width = maxX - minX;
6454

65-
using (IBuffer<float> amount = source.MemoryManager.Allocate<float>(width))
66-
using (BrushApplicator<TPixel> applicator = this.brush.CreateApplicator(
67-
source,
68-
sourceRectangle,
69-
this.options))
55+
// If there's no reason for blending, then avoid it.
56+
if (this.IsSolidBrushWithoutBlending(out SolidBrush<TPixel> solidBrush))
7057
{
71-
amount.Span.Fill(1f);
72-
7358
Parallel.For(
7459
minY,
7560
maxY,
7661
configuration.ParallelOptions,
7762
y =>
78-
{
79-
int offsetY = y - startY;
80-
int offsetX = minX - startX;
63+
{
64+
source.GetPixelRowSpan(y).Slice(minX, width).Fill(solidBrush.Color);
65+
});
66+
}
67+
else
68+
{
69+
// Reset offset if necessary.
70+
if (minX > 0)
71+
{
72+
startX = 0;
73+
}
74+
75+
if (minY > 0)
76+
{
77+
startY = 0;
78+
}
79+
80+
using (IBuffer<float> amount = source.MemoryManager.Allocate<float>(width))
81+
using (BrushApplicator<TPixel> applicator = this.brush.CreateApplicator(
82+
source,
83+
sourceRectangle,
84+
this.options))
85+
{
86+
amount.Span.Fill(1f);
87+
88+
Parallel.For(
89+
minY,
90+
maxY,
91+
configuration.ParallelOptions,
92+
y =>
93+
{
94+
int offsetY = y - startY;
95+
int offsetX = minX - startX;
8196

82-
applicator.Apply(amount.Span, offsetX, offsetY);
83-
});
97+
applicator.Apply(amount.Span, offsetX, offsetY);
98+
});
99+
}
84100
}
85101
}
102+
103+
private bool IsSolidBrushWithoutBlending(out SolidBrush<TPixel> solidBrush)
104+
{
105+
solidBrush = this.brush as SolidBrush<TPixel>;
106+
107+
return solidBrush != null
108+
&& ((this.options.BlenderMode == PixelBlenderMode.Normal && this.options.BlendPercentage == 1f
109+
&& solidBrush.Color.ToVector4().W == 1f)
110+
|| (this.options.BlenderMode == PixelBlenderMode.Over && this.options.BlendPercentage == 1f
111+
&& solidBrush.Color.ToVector4().W == 1f)
112+
|| (this.options.BlenderMode == PixelBlenderMode.Src));
113+
}
86114
}
87115
}

0 commit comments

Comments
 (0)