Skip to content

Commit 3f1b4fa

Browse files
Merge pull request #289 from SixLabors/js/nullable-enable
Enable nullability checks
2 parents b7402f4 + 6ef0a7b commit 3f1b4fa

31 files changed

+166
-211
lines changed

src/ImageSharp.Drawing/Processing/Brush.cs

Lines changed: 2 additions & 4 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-
#nullable disable
5-
64
namespace SixLabors.ImageSharp.Drawing.Processing;
75

86
/// <summary>
@@ -37,10 +35,10 @@ public abstract BrushApplicator<TPixel> CreateApplicator<TPixel>(
3735
where TPixel : unmanaged, IPixel<TPixel>;
3836

3937
/// <inheritdoc/>
40-
public abstract bool Equals(Brush other);
38+
public abstract bool Equals(Brush? other);
4139

4240
/// <inheritdoc/>
43-
public override bool Equals(object obj) => this.Equals(obj as Brush);
41+
public override bool Equals(object? obj) => this.Equals(obj as Brush);
4442

4543
/// <inheritdoc/>
4644
public abstract override int GetHashCode();

src/ImageSharp.Drawing/Processing/DrawingOptionsDefaultsExtensions.cs

Lines changed: 2 additions & 4 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-
#nullable disable
5-
64
using System.Numerics;
75

86
namespace SixLabors.ImageSharp.Drawing.Processing;
@@ -49,7 +47,7 @@ public static void SetDrawingTransform(this Configuration configuration, Matrix3
4947
/// <returns>The matrix.</returns>
5048
public static Matrix3x2 GetDrawingTransform(this IImageProcessingContext context)
5149
{
52-
if (context.Properties.TryGetValue(DrawingTransformMatrixKey, out object options) && options is Matrix3x2 go)
50+
if (context.Properties.TryGetValue(DrawingTransformMatrixKey, out object? options) && options is Matrix3x2 go)
5351
{
5452
return go;
5553
}
@@ -66,7 +64,7 @@ public static Matrix3x2 GetDrawingTransform(this IImageProcessingContext context
6664
/// <returns>The globally configured default matrix.</returns>
6765
public static Matrix3x2 GetDrawingTransform(this Configuration configuration)
6866
{
69-
if (configuration.Properties.TryGetValue(DrawingTransformMatrixKey, out object options) && options is Matrix3x2 go)
67+
if (configuration.Properties.TryGetValue(DrawingTransformMatrixKey, out object? options) && options is Matrix3x2 go)
7068
{
7169
return go;
7270
}

src/ImageSharp.Drawing/Processing/Extensions/DrawTextExtensions.cs

Lines changed: 6 additions & 8 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-
#nullable disable
5-
64
using SixLabors.Fonts;
75
using SixLabors.ImageSharp.Drawing.Processing.Processors.Text;
86

@@ -163,8 +161,8 @@ public static IImageProcessingContext DrawText(
163161
this IImageProcessingContext source,
164162
RichTextOptions textOptions,
165163
string text,
166-
Brush brush,
167-
Pen pen) =>
164+
Brush? brush,
165+
Pen? pen) =>
168166
source.DrawText(source.GetDrawingOptions(), textOptions, text, brush, pen);
169167

170168
/// <summary>
@@ -221,8 +219,8 @@ public static IImageProcessingContext DrawText(
221219
DrawingOptions drawingOptions,
222220
string text,
223221
Font font,
224-
Brush brush,
225-
Pen pen,
222+
Brush? brush,
223+
Pen? pen,
226224
PointF location)
227225
{
228226
RichTextOptions textOptions = new(font) { Origin = location };
@@ -244,7 +242,7 @@ public static IImageProcessingContext DrawText(
244242
DrawingOptions drawingOptions,
245243
RichTextOptions textOptions,
246244
string text,
247-
Brush brush,
248-
Pen pen)
245+
Brush? brush,
246+
Pen? pen)
249247
=> source.ApplyProcessor(new DrawTextProcessor(drawingOptions, textOptions, text, brush, pen));
250248
}

src/ImageSharp.Drawing/Processing/GradientBrush.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected GradientBrush(GradientRepetitionMode repetitionMode, params ColorStop[
3232
protected ColorStop[] ColorStops { get; }
3333

3434
/// <inheritdoc />
35-
public override bool Equals(Brush other)
35+
public override bool Equals(Brush? other)
3636
{
3737
if (other is GradientBrush brush)
3838
{

src/ImageSharp.Drawing/Processing/ImageBrush.cs

Lines changed: 3 additions & 10 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-
#nullable disable
5-
64
using System.Buffers;
75
using SixLabors.ImageSharp.Memory;
86

@@ -47,7 +45,7 @@ internal ImageBrush(Image image, RectangleF region)
4745
}
4846

4947
/// <inheritdoc />
50-
public override bool Equals(Brush other)
48+
public override bool Equals(Brush? other)
5149
{
5250
if (other is ImageBrush ib)
5351
{
@@ -83,10 +81,8 @@ public override BrushApplicator<TPixel> CreateApplicator<TPixel>(
8381
private class ImageBrushApplicator<TPixel> : BrushApplicator<TPixel>
8482
where TPixel : unmanaged, IPixel<TPixel>
8583
{
86-
private ImageFrame<TPixel> sourceFrame;
87-
88-
private Image<TPixel> sourceImage;
89-
84+
private readonly ImageFrame<TPixel> sourceFrame;
85+
private readonly Image<TPixel> sourceImage;
9086
private readonly bool shouldDisposeImage;
9187

9288
/// <summary>
@@ -158,10 +154,7 @@ protected override void Dispose(bool disposing)
158154
this.sourceImage?.Dispose();
159155
}
160156

161-
this.sourceImage = null;
162-
this.sourceFrame = null;
163157
this.isDisposed = true;
164-
165158
base.Dispose(disposing);
166159
}
167160

src/ImageSharp.Drawing/Processing/LinearGradientBrush.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public LinearGradientBrush(
3232
}
3333

3434
/// <inheritdoc/>
35-
public override bool Equals(Brush other)
35+
public override bool Equals(Brush? other)
3636
{
3737
if (other is LinearGradientBrush brush)
3838
{

src/ImageSharp.Drawing/Processing/PathGradientBrush.cs

Lines changed: 18 additions & 19 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-
#nullable disable
5-
64
using System.Numerics;
75
using SixLabors.ImageSharp.Drawing.Utilities;
86

@@ -76,7 +74,7 @@ public PathGradientBrush(PointF[] points, Color[] colors, Color centerColor)
7674
}
7775

7876
/// <inheritdoc />
79-
public override bool Equals(Brush other)
77+
public override bool Equals(Brush? other)
8078
{
8179
if (other is PathGradientBrush brush)
8280
{
@@ -123,7 +121,7 @@ private static Color CalculateCenterColor(Color[] colors)
123121
return new Color(colors.Select(c => (Vector4)c).Aggregate((p1, p2) => p1 + p2) / colors.Length);
124122
}
125123

126-
private static float DistanceBetween(PointF p1, PointF p2) => ((Vector2)(p2 - p1)).Length();
124+
private static float DistanceBetween(Vector2 p1, Vector2 p2) => (p2 - p1).Length();
127125

128126
private readonly struct Intersection
129127
{
@@ -178,13 +176,14 @@ public Vector4 ColorAt(float distance)
178176

179177
public Vector4 ColorAt(PointF point) => this.ColorAt(DistanceBetween(point, this.Start));
180178

181-
public bool Equals(Edge other)
182-
=> other.Start == this.Start &&
183-
other.End == this.End &&
184-
other.StartColor.Equals(this.StartColor) &&
185-
other.EndColor.Equals(this.EndColor);
179+
public bool Equals(Edge? other)
180+
=> other != null &&
181+
other.Start == this.Start &&
182+
other.End == this.End &&
183+
other.StartColor.Equals(this.StartColor) &&
184+
other.EndColor.Equals(this.EndColor);
186185

187-
public override bool Equals(object obj) => this.Equals(obj as Edge);
186+
public override bool Equals(object? obj) => this.Equals(obj as Edge);
188187

189188
public override int GetHashCode()
190189
=> HashCode.Combine(this.Start, this.End, this.StartColor, this.EndColor);
@@ -249,7 +248,7 @@ public PathGradientBrushApplicator(
249248
{
250249
get
251250
{
252-
var point = new Vector2(x, y);
251+
Vector2 point = new(x, y);
253252

254253
if (point == this.center)
255254
{
@@ -278,7 +277,7 @@ public PathGradientBrushApplicator(
278277
return px;
279278
}
280279

281-
var direction = Vector2.Normalize(point - this.center);
280+
Vector2 direction = Vector2.Normalize(point - this.center);
282281
Vector2 end = point + (direction * this.maxDistance);
283282

284283
(Edge Edge, Vector2 Point)? isc = this.FindIntersection(point, end);
@@ -294,7 +293,7 @@ public PathGradientBrushApplicator(
294293
float length = DistanceBetween(intersection, this.center);
295294
float ratio = length > 0 ? DistanceBetween(intersection, point) / length : 0;
296295

297-
var color = Vector4.Lerp(edgeColor, this.centerColor, ratio);
296+
Vector4 color = Vector4.Lerp(edgeColor, this.centerColor, ratio);
298297

299298
TPixel pixel = default;
300299
pixel.FromScaledVector4(color);
@@ -305,8 +304,8 @@ public PathGradientBrushApplicator(
305304
/// <inheritdoc />
306305
public override void Apply(Span<float> scanline, int x, int y)
307306
{
308-
Span<float> amounts = this.blenderBuffers.AmountSpan.Slice(0, scanline.Length);
309-
Span<TPixel> overlays = this.blenderBuffers.OverlaySpan.Slice(0, scanline.Length);
307+
Span<float> amounts = this.blenderBuffers.AmountSpan[..scanline.Length];
308+
Span<TPixel> overlays = this.blenderBuffers.OverlaySpan[..scanline.Length];
310309
float blendPercentage = this.Options.BlendPercentage;
311310

312311
// TODO: Remove bounds checks.
@@ -355,7 +354,7 @@ protected override void Dispose(bool disposing)
355354
{
356355
Vector2 ip = default;
357356
Vector2 closestIntersection = default;
358-
Edge closestEdge = null;
357+
Edge? closestEdge = null;
359358
const float minDistance = float.MaxValue;
360359
foreach (Edge edge in this.edges)
361360
{
@@ -385,9 +384,9 @@ private static bool FindPointOnTriangle(Vector2 v1, Vector2 v2, Vector2 v3, Vect
385384
Vector2 pv2 = point - v2;
386385
Vector2 pv3 = point - v3;
387386

388-
var d1 = Vector3.Cross(new Vector3(e1.X, e1.Y, 0), new Vector3(pv1.X, pv1.Y, 0));
389-
var d2 = Vector3.Cross(new Vector3(e2.X, e2.Y, 0), new Vector3(pv2.X, pv2.Y, 0));
390-
var d3 = Vector3.Cross(new Vector3(e3.X, e3.Y, 0), new Vector3(pv3.X, pv3.Y, 0));
387+
Vector3 d1 = Vector3.Cross(new Vector3(e1.X, e1.Y, 0), new Vector3(pv1.X, pv1.Y, 0));
388+
Vector3 d2 = Vector3.Cross(new Vector3(e2.X, e2.Y, 0), new Vector3(pv2.X, pv2.Y, 0));
389+
Vector3 d3 = Vector3.Cross(new Vector3(e3.X, e3.Y, 0), new Vector3(pv3.X, pv3.Y, 0));
391390

392391
if (Math.Sign(Vector3.Dot(d1, d2)) * Math.Sign(Vector3.Dot(d1, d3)) == -1 || Math.Sign(Vector3.Dot(d1, d2)) * Math.Sign(Vector3.Dot(d2, d3)) == -1)
393392
{

src/ImageSharp.Drawing/Processing/PatternBrush.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ internal PatternBrush(PatternBrush brush)
8484
}
8585

8686
/// <inheritdoc />
87-
public override bool Equals(Brush other)
87+
public override bool Equals(Brush? other)
8888
{
8989
if (other is PatternBrush sb)
9090
{

src/ImageSharp.Drawing/Processing/PatternPen.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public PatternPen(PenOptions options)
6363
}
6464

6565
/// <inheritdoc/>
66-
public override bool Equals(Pen other)
66+
public override bool Equals(Pen? other)
6767
{
6868
if (other is PatternPen)
6969
{

src/ImageSharp.Drawing/Processing/Pen.cs

Lines changed: 7 additions & 8 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-
#nullable disable
5-
64
namespace SixLabors.ImageSharp.Drawing.Processing;
75

86
/// <summary>
@@ -52,7 +50,9 @@ protected Pen(Brush strokeFill, float strokeWidth)
5250
/// <param name="strokePattern">The stroke pattern.</param>
5351
protected Pen(Brush strokeFill, float strokeWidth, float[] strokePattern)
5452
{
53+
Guard.NotNull(strokeFill, nameof(strokeFill));
5554
Guard.MustBeGreaterThan(strokeWidth, 0, nameof(strokeWidth));
55+
Guard.NotNull(strokePattern, nameof(strokePattern));
5656

5757
this.StrokeFill = strokeFill;
5858
this.StrokeWidth = strokeWidth;
@@ -104,19 +104,18 @@ public IPath GeneratePath(IPath path)
104104
public abstract IPath GeneratePath(IPath path, float strokeWidth);
105105

106106
/// <inheritdoc/>
107-
public virtual bool Equals(Pen other)
108-
=> this.StrokeWidth == other.StrokeWidth
107+
public virtual bool Equals(Pen? other)
108+
=> other != null
109+
&& this.StrokeWidth == other.StrokeWidth
109110
&& this.JointStyle == other.JointStyle
110111
&& this.EndCapStyle == other.EndCapStyle
111112
&& this.StrokeFill.Equals(other.StrokeFill)
112113
&& this.StrokePattern.SequenceEqual(other.StrokePattern);
113114

114115
/// <inheritdoc/>
115-
public override bool Equals(object obj) => this.Equals(obj as Pen);
116+
public override bool Equals(object? obj) => this.Equals(obj as Pen);
116117

117118
/// <inheritdoc/>
118119
public override int GetHashCode()
119-
120-
// TODO: StrokePattern should be part of the hash-code.
121-
=> HashCode.Combine(this.StrokeWidth, this.JointStyle, this.EndCapStyle, this.StrokeFill);
120+
=> HashCode.Combine(this.StrokeWidth, this.JointStyle, this.EndCapStyle, this.StrokeFill, this.pattern);
122121
}

0 commit comments

Comments
 (0)