Skip to content

Commit 316fb2e

Browse files
Merge pull request #351 from SixLabors/js/v4-compatibility
ImageSharp v4 compatibility
2 parents 435aae4 + 0def2c9 commit 316fb2e

File tree

54 files changed

+422
-262
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+422
-262
lines changed

.editorconfig

+8-6
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:war
104104
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:warning
105105
dotnet_style_parentheses_in_other_operators = always_for_clarity:suggestion
106106
# Expression-level preferences
107-
dotnet_style_object_initializer = true:warning
108-
dotnet_style_collection_initializer = true:warning
107+
dotnet_style_object_initializer = true:error
108+
dotnet_style_collection_initializer = true:error
109109
dotnet_style_explicit_tuple_names = true:warning
110110
dotnet_style_prefer_inferred_tuple_names = true:warning
111111
dotnet_style_prefer_inferred_anonymous_type_member_names = true:warning
@@ -135,9 +135,9 @@ csharp_style_prefer_null_check_over_type_check = true:warning
135135
# https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/language-rules#c-style-rules
136136
[*.{cs,csx,cake}]
137137
# 'var' preferences
138-
csharp_style_var_for_built_in_types = false:warning
139-
csharp_style_var_when_type_is_apparent = false:warning
140-
csharp_style_var_elsewhere = false:warning
138+
csharp_style_var_for_built_in_types = false:error
139+
csharp_style_var_when_type_is_apparent = false:error
140+
csharp_style_var_elsewhere = false:error
141141
# Expression-bodied members
142142
csharp_style_expression_bodied_methods = true:warning
143143
csharp_style_expression_bodied_constructors = true:warning
@@ -160,7 +160,7 @@ csharp_style_pattern_local_over_anonymous_function = true:warning
160160
csharp_style_deconstructed_variable_declaration = true:warning
161161
csharp_style_prefer_index_operator = true:warning
162162
csharp_style_prefer_range_operator = true:warning
163-
csharp_style_implicit_object_creation_when_type_is_apparent = true:warning
163+
csharp_style_implicit_object_creation_when_type_is_apparent = true:error
164164
# "Null" checking preferences
165165
csharp_style_throw_expression = true:warning
166166
csharp_style_conditional_delegate_call = true:warning
@@ -172,6 +172,8 @@ dotnet_diagnostic.IDE0063.severity = suggestion
172172
csharp_using_directive_placement = outside_namespace:warning
173173
# Modifier preferences
174174
csharp_prefer_static_local_function = true:warning
175+
# Primary constructor preferences
176+
csharp_style_prefer_primary_constructors = false:none
175177

176178
##########################################
177179
# Unnecessary Code Rules

.gitattributes

+10
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,13 @@
133133
*.pnm filter=lfs diff=lfs merge=lfs -text
134134
*.wbmp filter=lfs diff=lfs merge=lfs -text
135135
*.exr filter=lfs diff=lfs merge=lfs -text
136+
*.ico filter=lfs diff=lfs merge=lfs -text
137+
*.cur filter=lfs diff=lfs merge=lfs -text
138+
*.ani filter=lfs diff=lfs merge=lfs -text
139+
*.heic filter=lfs diff=lfs merge=lfs -text
140+
*.hif filter=lfs diff=lfs merge=lfs -text
141+
*.avif filter=lfs diff=lfs merge=lfs -text
142+
###############################################################################
143+
# Handle ICC files by git lfs
144+
###############################################################################
145+
*.icc filter=lfs diff=lfs merge=lfs -text

.github/workflows/build-and-test.yml

+23-17
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,50 @@ jobs:
1818
- ${{ contains(github.event.pull_request.labels.*.name, 'arch:arm32') || contains(github.event.pull_request.labels.*.name, 'arch:arm64') }}
1919
options:
2020
- os: ubuntu-latest
21-
framework: net7.0
22-
sdk: 7.0.x
21+
framework: net9.0
22+
sdk: 9.0.x
2323
sdk-preview: true
2424
runtime: -x64
2525
codecov: false
2626
- os: macos-13 # macos-latest runs on arm64 runners where libgdiplus is unavailable
27-
framework: net7.0
28-
sdk: 7.0.x
27+
framework: net9.0
28+
sdk: 9.0.x
2929
sdk-preview: true
3030
runtime: -x64
3131
codecov: false
3232
- os: windows-latest
33-
framework: net7.0
34-
sdk: 7.0.x
33+
framework: net9.0
34+
sdk: 9.0.x
3535
sdk-preview: true
3636
runtime: -x64
3737
codecov: false
3838
- os: buildjet-4vcpu-ubuntu-2204-arm
39-
framework: net7.0
40-
sdk: 7.0.x
39+
framework: net9.0
40+
sdk: 9.0.x
4141
sdk-preview: true
4242
runtime: -x64
4343
codecov: false
44+
4445
- os: ubuntu-latest
45-
framework: net6.0
46-
sdk: 6.0.x
46+
framework: net8.0
47+
sdk: 8.0.x
4748
runtime: -x64
4849
codecov: false
4950
- os: macos-13 # macos-latest runs on arm64 runners where libgdiplus is unavailable
50-
framework: net6.0
51-
sdk: 6.0.x
51+
framework: net8.0
52+
sdk: 8.0.x
5253
runtime: -x64
5354
codecov: false
5455
- os: windows-latest
55-
framework: net6.0
56-
sdk: 6.0.x
56+
framework: net8.0
57+
sdk: 8.0.x
5758
runtime: -x64
58-
codecov: true
59+
codecov: false
60+
- os: buildjet-4vcpu-ubuntu-2204-arm
61+
framework: net8.0
62+
sdk: 8.0.x
63+
runtime: -x64
64+
codecov: false
5965
exclude:
6066
- isARM: false
6167
options:
@@ -110,14 +116,14 @@ jobs:
110116
uses: actions/setup-dotnet@v4
111117
with:
112118
dotnet-version: |
113-
6.0.x
119+
8.0.x
114120
115121
- name: DotNet Setup Preview
116122
if: ${{ matrix.options.sdk-preview == true }}
117123
uses: actions/setup-dotnet@v4
118124
with:
119125
dotnet-version: |
120-
7.0.x
126+
9.0.x
121127
122128
- name: DotNet Build
123129
if: ${{ matrix.options.sdk-preview != true }}

samples/DrawShapesWithImageSharp/DrawShapesWithImageSharp.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
<Choose>
99
<When Condition="$(SIXLABORS_TESTING_PREVIEW) == true">
1010
<PropertyGroup>
11-
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
11+
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
1212
</PropertyGroup>
1313
</When>
1414
<Otherwise>
1515
<PropertyGroup>
16-
<TargetFrameworks>net6.0</TargetFrameworks>
16+
<TargetFrameworks>net8.0</TargetFrameworks>
1717
</PropertyGroup>
1818
</Otherwise>
1919
</Choose>

samples/DrawShapesWithImageSharp/ImageSharpLogo.cs

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static bool IsOpaqueColorWithoutBlending(this GraphicsOptions options, Co
4141
return false;
4242
}
4343

44-
if (((Vector4)color).W != Opaque)
44+
if (color.ToScaledVector4().W != Opaque)
4545
{
4646
return false;
4747
}

src/ImageSharp.Drawing/ImageSharp.Drawing.csproj

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
<PackageTags>Image Draw Shape Path Font</PackageTags>
1313
<Description>An extension to ImageSharp that allows the drawing of images, paths, and text.</Description>
1414
<Configurations>Debug;Release</Configurations>
15+
<IsTrimmable>true</IsTrimmable>
1516
</PropertyGroup>
1617

1718
<PropertyGroup>
18-
<!--Bump to V2 prior to tagged release.-->
19-
<MinVerMinimumMajorMinor>2.0</MinVerMinimumMajorMinor>
19+
<!--Bump to V3 prior to tagged release.-->
20+
<MinVerMinimumMajorMinor>3.0</MinVerMinimumMajorMinor>
2021
</PropertyGroup>
2122

2223
<!-- This enables the nullable analysis and treats all nullable warnings as error-->
@@ -28,13 +29,12 @@
2829
<Choose>
2930
<When Condition="$(SIXLABORS_TESTING_PREVIEW) == true">
3031
<PropertyGroup>
31-
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
32-
<IsTrimmable>true</IsTrimmable>
32+
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
3333
</PropertyGroup>
3434
</When>
3535
<Otherwise>
3636
<PropertyGroup>
37-
<TargetFrameworks>net6.0</TargetFrameworks>
37+
<TargetFrameworks>net8.0</TargetFrameworks>
3838
<IsTrimmable>true</IsTrimmable>
3939
</PropertyGroup>
4040
</Otherwise>
@@ -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.8" />
49-
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.6" />
48+
<PackageReference Include="SixLabors.Fonts" Version="2.0.9" />
49+
<PackageReference Include="SixLabors.ImageSharp" Version="4.0.0-alpha.0.18" />
5050
</ItemGroup>
5151
<Import Project="..\..\shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems" Label="Shared" />
5252
</Project>

src/ImageSharp.Drawing/Processing/GradientBrush.cs

+5-2
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,11 @@ 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(
141+
Vector4.Lerp(
142+
from.Color.ToScaledVector4(),
143+
to.Color.ToScaledVector4(),
144+
onLocalGradient)).ToPixel<TPixel>();
142145
}
143146
}
144147

src/ImageSharp.Drawing/Processing/PathGradientBrush.cs

+9-22
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ public sealed class PathGradientBrush : Brush
2222
/// <param name="colors">Array of colors that correspond to each point in the polygon.</param>
2323
public PathGradientBrush(PointF[] points, Color[] colors)
2424
{
25-
if (points == null)
26-
{
27-
throw new ArgumentNullException(nameof(points));
28-
}
25+
ArgumentNullException.ThrowIfNull(points);
2926

3027
if (points.Length < 3)
3128
{
@@ -34,10 +31,7 @@ public PathGradientBrush(PointF[] points, Color[] colors)
3431
"There must be at least 3 lines to construct a path gradient brush.");
3532
}
3633

37-
if (colors == null)
38-
{
39-
throw new ArgumentNullException(nameof(colors));
40-
}
34+
ArgumentNullException.ThrowIfNull(colors);
4135

4236
if (colors.Length == 0)
4337
{
@@ -106,10 +100,7 @@ public override BrushApplicator<TPixel> CreateApplicator<TPixel>(
106100

107101
private static Color CalculateCenterColor(Color[] colors)
108102
{
109-
if (colors == null)
110-
{
111-
throw new ArgumentNullException(nameof(colors));
112-
}
103+
ArgumentNullException.ThrowIfNull(colors);
113104

114105
if (colors.Length == 0)
115106
{
@@ -118,7 +109,7 @@ private static Color CalculateCenterColor(Color[] colors)
118109
"One or more color is needed to construct a path gradient brush.");
119110
}
120111

121-
return new Color(colors.Select(c => (Vector4)c).Aggregate((p1, p2) => p1 + p2) / colors.Length);
112+
return Color.FromScaledVector(colors.Select(c => c.ToScaledVector4()).Aggregate((p1, p2) => p1 + p2) / colors.Length);
122113
}
123114

124115
private static float DistanceBetween(Vector2 p1, Vector2 p2) => (p2 - p1).Length();
@@ -147,8 +138,8 @@ public Edge(Vector2 start, Vector2 end, Color startColor, Color endColor)
147138
{
148139
this.Start = start;
149140
this.End = end;
150-
this.StartColor = (Vector4)startColor;
151-
this.EndColor = (Vector4)endColor;
141+
this.StartColor = startColor.ToScaledVector4();
142+
this.EndColor = endColor.ToScaledVector4();
152143

153144
this.length = DistanceBetween(this.End, this.Start);
154145
}
@@ -236,7 +227,7 @@ public PathGradientBrushApplicator(
236227
Vector2[] points = edges.Select(s => s.Start).ToArray();
237228

238229
this.center = points.Aggregate((p1, p2) => p1 + p2) / edges.Count;
239-
this.centerColor = (Vector4)centerColor;
230+
this.centerColor = centerColor.ToScaledVector4();
240231
this.hasSpecialCenterColor = hasSpecialCenterColor;
241232
this.centerPixel = centerColor.ToPixel<TPixel>();
242233
this.maxDistance = points.Select(p => p - this.center).Max(d => d.Length());
@@ -272,9 +263,7 @@ public PathGradientBrushApplicator(
272263
+ (u * this.edges[0].EndColor)
273264
+ (v * this.edges[2].StartColor);
274265

275-
TPixel px = default;
276-
px.FromScaledVector4(pointColor);
277-
return px;
266+
return TPixel.FromScaledVector4(pointColor);
278267
}
279268

280269
Vector2 direction = Vector2.Normalize(point - this.center);
@@ -295,9 +284,7 @@ public PathGradientBrushApplicator(
295284

296285
Vector4 color = Vector4.Lerp(edgeColor, this.centerColor, ratio);
297286

298-
TPixel pixel = default;
299-
pixel.FromScaledVector4(color);
300-
return pixel;
287+
return TPixel.FromScaledVector4(color);
301288
}
302289
}
303290

src/ImageSharp.Drawing/Processing/PatternBrush.cs

+2-2
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+
var foreColorVector = foreColor.ToScaledVector4();
58+
var 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++)

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

+1-1
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

+2-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,8 @@ public RecolorBrushApplicator(
104104
this.targetColorPixel = targetColor;
105105

106106
// 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));
107+
TPixel maxColor = TPixel.FromVector4(new Vector4(float.MaxValue));
108+
TPixel minColor = TPixel.FromVector4(new Vector4(float.MinValue));
111109
this.threshold = Vector4.DistanceSquared(maxColor.ToVector4(), minColor.ToVector4()) * threshold;
112110
this.blenderBuffers = new ThreadLocalBlenderBuffers<TPixel>(configuration.MemoryAllocator, source.Width);
113111
}

src/ImageSharp.Drawing/Shapes/Rasterization/ScanEdgeCollection.Build.cs

+3
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,13 @@ static void RoundY(ReadOnlySpan<PointF> vertices, Span<float> destination, float
208208
[MethodImpl(MethodImplOptions.AggressiveInlining)]
209209
private static Vector128<float> AdvSimdShuffle(Vector128<float> a, Vector128<float> b, byte control)
210210
{
211+
// TODO: Review the codegen here. Might be better just looping.
212+
#pragma warning disable CA1857 // A constant is expected for the parameter
211213
Vector128<float> result = Vector128.Create(AdvSimd.Extract(a, (byte)(control & 0x3)));
212214
result = AdvSimd.Insert(result, 1, AdvSimd.Extract(a, (byte)((control >> 2) & 0x3)));
213215
result = AdvSimd.Insert(result, 2, AdvSimd.Extract(b, (byte)((control >> 4) & 0x3)));
214216
result = AdvSimd.Insert(result, 3, AdvSimd.Extract(b, (byte)((control >> 6) & 0x3)));
217+
#pragma warning restore CA1857 // A constant is expected for the parameter
215218

216219
return result;
217220
}

tests/Directory.Build.targets

+10-6
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919
<ItemGroup>
2020
<!-- Test Dependencies -->
2121
<PackageReference Update="BenchmarkDotNet" Version="0.13.1" />
22-
<PackageReference Update="Magick.NET-Q16-AnyCPU" Version="12.2.2" />
23-
<PackageReference Update="Microsoft.DotNet.RemoteExecutor" Version="6.0.0-beta.21311.3" />
24-
<PackageReference Update="Microsoft.DotNet.XUnitExtensions" Version="6.0.0-beta.21311.3" />
25-
<PackageReference Update="Moq" Version="4.16.1" />
22+
<!--
23+
Do not update to 14+ yet. There's differnce in how the BMP decoder handles rounding in 16 bit images.
24+
See https://github.com/ImageMagick/ImageMagick/commit/27a0a9c37f18af9c8d823a3ea076f600843b553c
25+
-->
26+
<PackageReference Update="Magick.NET-Q16-AnyCPU" Version="13.10.0" />
27+
<PackageReference Update="Microsoft.DotNet.RemoteExecutor" Version="8.0.0-beta.23580.1" />
28+
<PackageReference Update="Microsoft.DotNet.XUnitExtensions" Version="8.0.0-beta.23580.1" />
29+
<PackageReference Update="Moq" Version="4.20.72" />
2630
<PackageReference Include="runtime.osx.10.10-x64.CoreCompat.System.Drawing" Version="5.8.64" Condition="'$(IsOSX)'=='true'" />
27-
<PackageReference Update="System.Drawing.Common" Version="5.0.2" />
31+
<PackageReference Update="System.Drawing.Common" Version="6.0.0" />
2832
<PackageReference Update="GeoJSON.Net" Version="1.2.19" />
29-
<PackageReference Update="SkiaSharp" Version="2.80.3" />
33+
<PackageReference Update="SkiaSharp" Version="2.88.9" />
3034
</ItemGroup>
3135

3236
</Project>

0 commit comments

Comments
 (0)