Skip to content

Commit 2152cec

Browse files
Use latest ImageSharp build
1 parent 0a70463 commit 2152cec

36 files changed

+305
-302
lines changed

samples/DrawShapesWithImageSharp/ImageSharpLogo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void SaveLogo(float size, string path)
6262
img.Mutate(i => i.Fill(colors[s], segments[s].Transform(scaler)));
6363
}
6464

65-
img.Mutate(i => i.Fill(new Rgba32(0, 0, 0, 170), new ComplexPolygon(new EllipsePolygon(center, 161f), new EllipsePolygon(center, 61f)).Transform(scaler)));
65+
img.Mutate(i => i.Fill(Color.FromPixel(new Rgba32(0, 0, 0, 170)), new ComplexPolygon(new EllipsePolygon(center, 161f), new EllipsePolygon(center, 61f)).Transform(scaler)));
6666

6767
string fullPath = System.IO.Path.GetFullPath(System.IO.Path.Combine("Output", path));
6868

src/ImageSharp.Drawing/Common/Extensions/GraphicsOptionsExtensions.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
using System.Numerics;
5-
64
namespace SixLabors.ImageSharp.Drawing;
75

86
/// <summary>
@@ -28,20 +26,19 @@ public static bool IsOpaqueColorWithoutBlending(this GraphicsOptions options, Co
2826
return false;
2927
}
3028

31-
if (options.AlphaCompositionMode != PixelAlphaCompositionMode.SrcOver
32-
&& options.AlphaCompositionMode != PixelAlphaCompositionMode.Src)
29+
if (options.AlphaCompositionMode is not PixelAlphaCompositionMode.SrcOver and not PixelAlphaCompositionMode.Src)
3330
{
3431
return false;
3532
}
3633

37-
const float Opaque = 1F;
34+
const float opaque = 1f;
3835

39-
if (options.BlendPercentage != Opaque)
36+
if (options.BlendPercentage != opaque)
4037
{
4138
return false;
4239
}
4340

44-
if (((Vector4)color).W != Opaque)
41+
if (color.ToScaledVector4().W != opaque)
4542
{
4643
return false;
4744
}

src/ImageSharp.Drawing/ImageSharp.Drawing.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
<None Include="..\..\shared-infrastructure\branding\icons\imagesharp.drawing\sixlabors.imagesharp.drawing.128.png" Pack="true" PackagePath="" />
4646
</ItemGroup>
4747
<ItemGroup>
48-
<PackageReference Include="SixLabors.Fonts" Version="2.0.1" />
49-
<PackageReference Include="SixLabors.ImageSharp" Version="4.0.0-alpha.0.7" />
48+
<PackageReference Include="SixLabors.Fonts" Version="2.0.2" />
49+
<PackageReference Include="SixLabors.ImageSharp" Version="4.0.0-alpha.0.4" />
5050
</ItemGroup>
5151
<Import Project="..\..\shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems" Label="Shared" />
5252
</Project>

src/ImageSharp.Drawing/Processing/GradientBrush.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Numerics;
55
using SixLabors.ImageSharp.Drawing.Utilities;
66
using SixLabors.ImageSharp.Memory;
7-
using SixLabors.ImageSharp.PixelFormats;
87

98
namespace SixLabors.ImageSharp.Drawing.Processing;
109

@@ -138,7 +137,7 @@ protected GradientBrushApplicator(
138137
float onLocalGradient = (positionOnCompleteGradient - from.Ratio) / (to.Ratio - from.Ratio);
139138

140139
// TODO: This should use premultiplied vectors to avoid bad blends e.g. red -> brown <- green.
141-
return new Color(Vector4.Lerp((Vector4)from.Color, (Vector4)to.Color, onLocalGradient)).ToPixel<TPixel>();
140+
return Color.FromScaledVector(Vector4.Lerp(from.Color.ToScaledVector4(), to.Color.ToScaledVector4(), onLocalGradient)).ToPixel<TPixel>();
142141
}
143142
}
144143

src/ImageSharp.Drawing/Processing/PathGradientBrush.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public override BrushApplicator<TPixel> CreateApplicator<TPixel>(
8787
this.hasSpecialCenterColor);
8888

8989
private static Color CalculateCenterColor(Color[] colors)
90-
=> new(colors.Select(c => (Vector4)c).Aggregate((p1, p2) => p1 + p2) / colors.Length);
90+
=> Color.FromScaledVector(colors.Select(c => c.ToScaledVector4()).Aggregate((p1, p2) => p1 + p2) / colors.Length);
9191

9292
private static float DistanceBetween(Vector2 p1, Vector2 p2) => (p2 - p1).Length();
9393

@@ -115,8 +115,8 @@ public Edge(Vector2 start, Vector2 end, Color startColor, Color endColor)
115115
{
116116
this.Start = start;
117117
this.End = end;
118-
this.StartColor = (Vector4)startColor;
119-
this.EndColor = (Vector4)endColor;
118+
this.StartColor = startColor.ToScaledVector4();
119+
this.EndColor = endColor.ToScaledVector4();
120120

121121
this.length = DistanceBetween(this.End, this.Start);
122122
}
@@ -204,7 +204,7 @@ public PathGradientBrushApplicator(
204204
Vector2[] points = edges.Select(s => s.Start).ToArray();
205205

206206
this.center = points.Aggregate((p1, p2) => p1 + p2) / edges.Count;
207-
this.centerColor = (Vector4)centerColor;
207+
this.centerColor = centerColor.ToScaledVector4();
208208
this.hasSpecialCenterColor = hasSpecialCenterColor;
209209
this.centerPixel = centerColor.ToPixel<TPixel>();
210210
this.maxDistance = points.Select(p => p - this.center).Max(d => d.Length());
@@ -240,9 +240,7 @@ public PathGradientBrushApplicator(
240240
+ (u * this.edges[0].EndColor)
241241
+ (v * this.edges[2].StartColor);
242242

243-
TPixel px = default;
244-
px.FromScaledVector4(pointColor);
245-
return px;
243+
return TPixel.FromScaledVector4(pointColor);
246244
}
247245

248246
Vector2 direction = Vector2.Normalize(point - this.center);
@@ -263,9 +261,7 @@ public PathGradientBrushApplicator(
263261

264262
Vector4 color = Vector4.Lerp(edgeColor, this.centerColor, ratio);
265263

266-
TPixel pixel = default;
267-
pixel.FromScaledVector4(color);
268-
return pixel;
264+
return TPixel.FromScaledVector4(color);
269265
}
270266
}
271267

src/ImageSharp.Drawing/Processing/PatternBrush.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public PatternBrush(Color foreColor, Color backColor, bool[,] pattern)
5454
/// <param name="pattern">The pattern.</param>
5555
internal PatternBrush(Color foreColor, Color backColor, in DenseMatrix<bool> pattern)
5656
{
57-
var foreColorVector = (Vector4)foreColor;
58-
var backColorVector = (Vector4)backColor;
57+
Vector4 foreColorVector = foreColor.ToScaledVector4();
58+
Vector4 backColorVector = backColor.ToScaledVector4();
5959
this.pattern = new DenseMatrix<Color>(pattern.Columns, pattern.Rows);
6060
this.patternVector = new DenseMatrix<Vector4>(pattern.Columns, pattern.Rows);
6161
for (int i = 0; i < pattern.Data.Length; i++)
@@ -156,8 +156,8 @@ public PatternBrushApplicator(
156156
public override void Apply(Span<float> scanline, int x, int y)
157157
{
158158
int patternY = y % this.pattern.Rows;
159-
Span<float> amounts = this.blenderBuffers.AmountSpan.Slice(0, scanline.Length);
160-
Span<TPixel> overlays = this.blenderBuffers.OverlaySpan.Slice(0, scanline.Length);
159+
Span<float> amounts = this.blenderBuffers.AmountSpan[..scanline.Length];
160+
Span<TPixel> overlays = this.blenderBuffers.OverlaySpan[..scanline.Length];
161161

162162
for (int i = 0; i < scanline.Length; i++)
163163
{

src/ImageSharp.Drawing/Processing/Pen.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ protected Pen(Brush strokeFill, float strokeWidth)
5151
protected Pen(Brush strokeFill, float strokeWidth, float[] strokePattern)
5252
{
5353
Guard.NotNull(strokeFill, nameof(strokeFill));
54-
Guard.MustBeGreaterThan(strokeWidth, 0, nameof(strokeWidth));
54+
55+
// Guard.MustBeGreaterThan(strokeWidth, 0, nameof(strokeWidth));
5556
Guard.NotNull(strokePattern, nameof(strokePattern));
5657

5758
this.StrokeFill = strokeFill;
58-
this.StrokeWidth = strokeWidth;
59+
this.StrokeWidth = strokeWidth > 0 ? strokeWidth : 1;
5960
this.pattern = strokePattern;
6061
}
6162

src/ImageSharp.Drawing/Processing/Processors/Text/RichTextGlyphRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ protected override void BeginGlyph(in FontRectangle bounds, in GlyphRendererPara
144144

145145
/// <inheritdoc/>
146146
public void SetColor(GlyphColor color)
147-
=> this.currentColor = new Color(new Rgba32(color.Red, color.Green, color.Blue, color.Alpha));
147+
=> this.currentColor = Color.FromPixel(new Rgba32(color.Red, color.Green, color.Blue, color.Alpha));
148148

149149
public override TextDecorations EnabledDecorations()
150150
{

src/ImageSharp.Drawing/Processing/RecolorBrush.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,14 @@ public RecolorBrushApplicator(
100100
float threshold)
101101
: base(configuration, options, source)
102102
{
103-
this.sourceColor = sourceColor.ToVector4();
103+
this.sourceColor = sourceColor.ToScaledVector4();
104104
this.targetColorPixel = targetColor;
105105

106+
// TODO: Review this. We can skip the conversion from/to Vector4.
106107
// Lets hack a min max extremes for a color space by letting the IPackedPixel clamp our values to something in the correct spaces :)
107-
var maxColor = default(TPixel);
108-
maxColor.FromVector4(new Vector4(float.MaxValue));
109-
var minColor = default(TPixel);
110-
minColor.FromVector4(new Vector4(float.MinValue));
111-
this.threshold = Vector4.DistanceSquared(maxColor.ToVector4(), minColor.ToVector4()) * threshold;
108+
TPixel maxColor = TPixel.FromScaledVector4(Vector4.One);
109+
TPixel minColor = TPixel.FromVector4(Vector4.Zero);
110+
this.threshold = Vector4.DistanceSquared(maxColor.ToScaledVector4(), minColor.ToScaledVector4()) * threshold;
112111
this.blenderBuffers = new ThreadLocalBlenderBuffers<TPixel>(configuration.MemoryAllocator, source.Width);
113112
}
114113

@@ -118,7 +117,7 @@ public RecolorBrushApplicator(
118117
{
119118
// Offset the requested pixel by the value in the rectangle (the shapes position)
120119
TPixel result = this.Target[x, y];
121-
var background = result.ToVector4();
120+
Vector4 background = result.ToScaledVector4();
122121
float distance = Vector4.DistanceSquared(background, this.sourceColor);
123122
if (distance <= this.threshold)
124123
{
@@ -136,8 +135,8 @@ public RecolorBrushApplicator(
136135
/// <inheritdoc />
137136
public override void Apply(Span<float> scanline, int x, int y)
138137
{
139-
Span<float> amounts = this.blenderBuffers.AmountSpan.Slice(0, scanline.Length);
140-
Span<TPixel> overlays = this.blenderBuffers.OverlaySpan.Slice(0, scanline.Length);
138+
Span<float> amounts = this.blenderBuffers.AmountSpan[..scanline.Length];
139+
Span<TPixel> overlays = this.blenderBuffers.OverlaySpan[..scanline.Length];
141140

142141
for (int i = 0; i < scanline.Length; i++)
143142
{
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.Runtime.CompilerServices;
5+
using BenchmarkDotNet.Attributes;
6+
using SixLabors.ImageSharp.Drawing.Processing;
7+
using SixLabors.ImageSharp.Drawing.Tests;
8+
using SixLabors.ImageSharp.PixelFormats;
9+
using SixLabors.ImageSharp.Processing;
10+
11+
namespace SixLabors.ImageSharp.Drawing.Benchmarks.Drawing;
12+
13+
[ShortRunJob]
14+
public class EllipseStressTest
15+
{
16+
private Image<Rgba32> image;
17+
private readonly int width = 2560;
18+
private readonly int height = 1369;
19+
private readonly Random random = new();
20+
21+
[GlobalSetup]
22+
public void Setup() => this.image = new(this.width, this.height, Color.White.ToPixel<Rgba32>());
23+
24+
[Benchmark]
25+
public void DrawImageSharp()
26+
{
27+
for (int i = 0; i < 20_000; i++)
28+
{
29+
Color brushColor = Color.FromPixel(new Rgba32((byte)this.Rand(255), (byte)this.Rand(255), (byte)this.Rand(255), (byte)this.Rand(255)));
30+
Color penColor = Color.FromPixel(new Rgba32((byte)this.Rand(255), (byte)this.Rand(255), (byte)this.Rand(255), (byte)this.Rand(255)));
31+
32+
float r = this.Rand(20f) + 1f;
33+
float x = this.Rand(this.width);
34+
float y = this.Rand(this.height);
35+
EllipsePolygon ellipse = new(new PointF(x, y), r);
36+
this.image.Mutate(
37+
m =>
38+
m.Fill(Brushes.Solid(brushColor), ellipse)
39+
.Draw(Pens.Solid(penColor, this.Rand(5)), ellipse));
40+
}
41+
}
42+
43+
[GlobalCleanup]
44+
public void Cleanup()
45+
{
46+
this.image.SaveAsPng(TestEnvironment.GetFullPath("artifacts\\ellipse-stress.png"));
47+
this.image.Dispose();
48+
}
49+
50+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
51+
private float Rand(float x)
52+
=> ((float)(((this.random.Next() << 15) | this.random.Next()) & 0x3FFFFFFF) % 1000000) * x / 1000000f;
53+
}

0 commit comments

Comments
 (0)