Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ csharp_style_deconstructed_variable_declaration = true:warning
csharp_style_prefer_index_operator = true:warning
csharp_style_prefer_range_operator = true:warning
csharp_style_implicit_object_creation_when_type_is_apparent = true:error
# ReSharper inspection severities
resharper_arrange_object_creation_when_type_evident_highlighting = error
resharper_arrange_object_creation_when_type_not_evident_highlighting = error
# "Null" checking preferences
csharp_style_throw_expression = true:warning
csharp_style_conditional_delegate_call = true:warning
Expand All @@ -174,6 +177,9 @@ csharp_using_directive_placement = outside_namespace:warning
csharp_prefer_static_local_function = true:warning
# Primary constructor preferences
csharp_style_prefer_primary_constructors = false:none
# Collection preferences
dotnet_style_prefer_collection_expression = true:error
resharper_use_collection_expression_highlighting =true:error

##########################################
# Unnecessary Code Rules
Expand Down
18 changes: 9 additions & 9 deletions samples/DrawShapesWithImageSharp/ImageSharpLogo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static void SaveLogo(float size, string path)
// the point are based on a 1206x1206 shape so size requires scaling from there
float scalingFactor = size / 1206;

var center = new Vector2(603);
Vector2 center = new(603);

// segment whose center of rotation should be
var segmentOffset = new Vector2(301.16968f, 301.16974f);
Vector2 segmentOffset = new(301.16968f, 301.16974f);
IPath segment = new Polygon(
new LinearLineSegment(new Vector2(230.54f, 361.0261f), new Vector2(5.8641942f, 361.46031f)),
new CubicBezierLineSegment(
Expand All @@ -30,28 +30,28 @@ public static void SaveLogo(float size, string path)
new Vector2(78.26f, 97.0461f))).Translate(center - segmentOffset);

// we need to create 6 of theses all rotated about the center point
var segments = new List<IPath>();
List<IPath> segments = [];
for (int i = 0; i < 6; i++)
{
float angle = i * ((float)Math.PI / 3);
IPath s = segment.Transform(Matrix3x2.CreateRotation(angle, center));
segments.Add(s);
}

var colors = new List<Color>()
{
List<Color> colors =
[
Color.ParseHex("35a849"),
Color.ParseHex("fcee21"),
Color.ParseHex("ed7124"),
Color.ParseHex("cb202d"),
Color.ParseHex("5f2c83"),
Color.ParseHex("085ba7"),
};
Color.ParseHex("085ba7")
];

var scaler = Matrix3x2.CreateScale(scalingFactor, Vector2.Zero);
Matrix3x2 scaler = Matrix3x2.CreateScale(scalingFactor, Vector2.Zero);

int dimensions = (int)Math.Ceiling(size);
using (var img = new Image<Rgba32>(dimensions, dimensions))
using (Image<Rgba32> img = new(dimensions, dimensions))
{
img.Mutate(i => i.Fill(Color.Black));
img.Mutate(i => i.Fill(Color.ParseHex("e1e1e1ff"), new EllipsePolygon(center, 600f).Transform(scaler)));
Expand Down
57 changes: 28 additions & 29 deletions samples/DrawShapesWithImageSharp/Program.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion shared-infrastructure
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static IImageProcessingContext Fill(
Brush brush,
Action<PathBuilder> region)
{
var pb = new PathBuilder();
PathBuilder pb = new();
region(pb);

return source.Fill(options, brush, pb.Build());
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp.Drawing/Processing/PatternBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public PatternBrush(Color foreColor, Color backColor, bool[,] pattern)
/// <param name="pattern">The pattern.</param>
internal PatternBrush(Color foreColor, Color backColor, in DenseMatrix<bool> pattern)
{
var foreColorVector = foreColor.ToScaledVector4();
var backColorVector = backColor.ToScaledVector4();
Vector4 foreColorVector = foreColor.ToScaledVector4();
Vector4 backColorVector = backColor.ToScaledVector4();
this.pattern = new DenseMatrix<Color>(pattern.Columns, pattern.Rows);
this.patternVector = new DenseMatrix<Vector4>(pattern.Columns, pattern.Rows);
for (int i = 0; i < pattern.Data.Length; i++)
Expand Down
10 changes: 5 additions & 5 deletions src/ImageSharp.Drawing/Processing/Pens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace SixLabors.ImageSharp.Drawing.Processing;
/// </summary>
public static class Pens
{
private static readonly float[] DashDotPattern = { 3f, 1f, 1f, 1f };
private static readonly float[] DashDotDotPattern = { 3f, 1f, 1f, 1f, 1f, 1f };
private static readonly float[] DottedPattern = { 1f, 1f };
private static readonly float[] DashedPattern = { 3f, 1f };
internal static readonly float[] EmptyPattern = Array.Empty<float>();
private static readonly float[] DashDotPattern = [3f, 1f, 1f, 1f];
private static readonly float[] DashDotDotPattern = [3f, 1f, 1f, 1f, 1f, 1f];
private static readonly float[] DottedPattern = [1f, 1f];
private static readonly float[] DashedPattern = [3f, 1f];
internal static readonly float[] EmptyPattern = [];

/// <summary>
/// Create a solid pen without any drawing patterns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public void Execute()
RectangleF bounds = this.definition.Region.Bounds;

// add some clamping offsets to the brush to account for the target drawing location due to the cloned image not fill the image as expected
var offsetX = 0;
var offsetY = 0;
int offsetX = 0;
int offsetY = 0;
if (bounds.X < 0)
{
offsetX = -(int)MathF.Floor(bounds.X);
Expand All @@ -53,10 +53,10 @@ public void Execute()
offsetY = -(int)MathF.Floor(bounds.Y);
}

var brush = new ImageBrush(clone, bounds, new Point(offsetX, offsetY));
ImageBrush brush = new(clone, bounds, new Point(offsetX, offsetY));

// Grab hold of an image processor that can fill paths with a brush to allow it to do the hard pixel pushing for us
var processor = new FillPathProcessor(this.definition.Options, brush, this.definition.Region);
FillPathProcessor processor = new(this.definition.Options, brush, this.definition.Region);
using IImageProcessor<TPixel> p = processor.CreatePixelSpecificProcessor(this.configuration, this.source, this.sourceRectangle);

// Fill the shape using the image brush
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuratio

if (shape is RectangularPolygon rectPoly)
{
var rectF = new RectangleF(rectPoly.Location, rectPoly.Size);
var rect = (Rectangle)rectF;
RectangleF rectF = new(rectPoly.Location, rectPoly.Size);
Rectangle rect = (Rectangle)rectF;
if (!this.Options.GraphicsOptions.Antialias || rectF == rect)
{
// Cast as in and back are the same or we are using anti-aliasing
Expand All @@ -63,7 +63,7 @@ public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuratio
}

// Clone the definition so we can pass the transformed path.
var definition = new FillPathProcessor(this.Options, this.Brush, shape);
FillPathProcessor definition = new(this.Options, this.Brush, shape);
return new FillPathProcessor<TPixel>(configuration, definition, source, sourceRectangle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private static RichTextOptions ConfigureOptions(RichTextOptions options)
// and not adjust the origin. Any translation should be applied to the path.
if (options.Path is not null && options.Origin != Vector2.Zero)
{
return new(options)
return new RichTextOptions(options)
{
Origin = Vector2.Zero,
Path = options.Path.Translate(options.Origin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public RichTextGlyphRenderer(
this.memoryAllocator = memoryAllocator;
this.defaultPen = pen;
this.defaultBrush = brush;
this.DrawingOperations = new List<DrawingOperation>();
this.DrawingOperations = [];

IPath? path = textOptions.Path;
if (path is not null)
Expand Down Expand Up @@ -115,7 +115,7 @@ protected override void BeginGlyph(in FontRectangle bounds, in GlyphRendererPara
// We need to apply the default transform to the bounds to get the correct size
// for comparison with future glyphs. We can use this cached glyph anywhere in the text block.
RectangleF currentBounds = RectangleF.Transform(
new RectangleF(bounds.Location, new(bounds.Width, bounds.Height)),
new RectangleF(bounds.Location, new SizeF(bounds.Width, bounds.Height)),
this.drawingOptions.Transform);

PointF currentBoundsDelta = currentBounds.Location - ClampToPixel(currentBounds.Location);
Expand Down Expand Up @@ -233,12 +233,12 @@ public override void SetDecoration(TextDecorations textDecorations, Vector2 star
if (textDecorations == TextDecorations.Overline)
{
// CSS overline is drawn above the position, so we need to move it up.
offset = rotated ? new(thickness * .5F, 0) : new(0, -(thickness * .5F));
offset = rotated ? new Vector2(thickness * .5F, 0) : new Vector2(0, -(thickness * .5F));
}
else if (textDecorations == TextDecorations.Underline)
{
// CSS underline is drawn below the position, so we need to move it down.
offset = rotated ? new(-(thickness * .5F), 0) : new(0, thickness * .5F);
offset = rotated ? new Vector2(-(thickness * .5F), 0) : new Vector2(0, thickness * .5F);
}

// We clamp the start and end points to the pixel grid to avoid anti-aliasing.
Expand Down Expand Up @@ -421,7 +421,7 @@ private void FinalizeDecoration(ref TextDecorationDetails? decoration)

// Calculate the transform for this path.
// We cannot use the path builder transform as this path is rendered independently.
FontRectangle rectangle = new(outline.Bounds.Location, new(outline.Bounds.Width, outline.Bounds.Height));
FontRectangle rectangle = new(outline.Bounds.Location, new Vector2(outline.Bounds.Width, outline.Bounds.Height));
Matrix3x2 pathTransform = this.ComputeTransform(in rectangle);
Matrix3x2 defaultTransform = this.drawingOptions.Transform;
outline = outline.Transform(pathTransform * defaultTransform);
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp.Drawing/Processing/RecolorBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public RecolorBrushApplicator(
{
// Offset the requested pixel by the value in the rectangle (the shapes position)
TPixel result = this.Target[x, y];
var background = result.ToVector4();
Vector4 background = result.ToVector4();
float distance = Vector4.DistanceSquared(background, this.sourceColor);
if (distance <= this.threshold)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp.Drawing/Processing/RichTextOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class RichTextOptions : TextOptions
/// <param name="font">The font.</param>
public RichTextOptions(Font font)
: base(font)
=> this.TextRuns = Array.Empty<RichTextRun>();
=> this.TextRuns = [];

/// <summary>
/// Initializes a new instance of the <see cref="RichTextOptions" /> class from properties
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp.Drawing/Shapes/ArcLineSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private static PointF[] EllipticArcFromEndParams(

if (EllipticArcOutOfRange(from, to, radius))
{
return new[] { from, to };
return [from, to];
}

EndpointToCenterArcParams(from, to, ref absRadius, rotation, largeArc, sweep, out Vector2 center, out Vector2 angles);
Expand Down Expand Up @@ -172,7 +172,7 @@ private static Vector2 EllipticArcPoint(Vector2 c, Vector2 r, float xAngle, floa

private static PointF[] EllipticArcToBezierCurve(Vector2 from, Vector2 center, Vector2 radius, float xAngle, float startAngle, float sweepAngle)
{
List<PointF> points = new();
List<PointF> points = [];

float s = startAngle;
float e = s + sweepAngle;
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp.Drawing/Shapes/ClipPathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static IPath Clip(
/// <returns>The clipped <see cref="IPath"/>.</returns>
/// <exception cref="ClipperException">Thrown when an error occurred while attempting to clip the polygon.</exception>
public static IPath Clip(this IPath subjectPath, IEnumerable<IPath> clipPaths)
=> subjectPath.Clip(new(), clipPaths);
=> subjectPath.Clip(new ShapeOptions(), clipPaths);

/// <summary>
/// Clips the specified subject path with the provided clipping paths.
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp.Drawing/Shapes/ComplexPolygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public IPath Transform(Matrix3x2 matrix)
/// <inheritdoc />
public IEnumerable<ISimplePath> Flatten()
{
List<ISimplePath> paths = new();
List<ISimplePath> paths = [];
foreach (IPath path in this.Paths)
{
paths.AddRange(path.Flatten());
Expand Down
10 changes: 5 additions & 5 deletions src/ImageSharp.Drawing/Shapes/CubicBezierLineSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public CubicBezierLineSegment Transform(Matrix3x2 matrix)
return this;
}

var transformedPoints = new PointF[this.controlPoints.Length];
PointF[] transformedPoints = new PointF[this.controlPoints.Length];

for (int i = 0; i < this.controlPoints.Length; i++)
{
Expand All @@ -94,7 +94,7 @@ public CubicBezierLineSegment Transform(Matrix3x2 matrix)

private static PointF[] GetDrawingPoints(PointF[] controlPoints)
{
var drawingPoints = new List<PointF>();
List<PointF> drawingPoints = [];
int curveCount = (controlPoints.Length - 1) / 3;

for (int curveIndex = 0; curveIndex < curveCount; curveIndex++)
Expand All @@ -115,7 +115,7 @@ private static PointF[] GetDrawingPoints(PointF[] controlPoints)

private static List<PointF> FindDrawingPoints(int curveIndex, PointF[] controlPoints)
{
var pointList = new List<PointF>();
List<PointF> pointList = [];

Vector2 left = CalculateBezierPoint(curveIndex, 0, controlPoints);
Vector2 right = CalculateBezierPoint(curveIndex, 1, controlPoints);
Expand Down Expand Up @@ -154,8 +154,8 @@ private static int FindDrawingPoints(
float midT = (t0 + t1) / 2;
Vector2 mid = CalculateBezierPoint(curveIndex, midT, controlPoints);

var leftDirection = Vector2.Normalize(left - mid);
var rightDirection = Vector2.Normalize(right - mid);
Vector2 leftDirection = Vector2.Normalize(left - mid);
Vector2 rightDirection = Vector2.Normalize(right - mid);

if (Vector2.Dot(leftDirection, rightDirection) > DivisionThreshold || Math.Abs(midT - 0.5f) < 0.0001f)
{
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp.Drawing/Shapes/EllipsePolygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ SegmentInfo IPathInternals.PointAlongPath(float distance)

/// <inheritdoc/>
IReadOnlyList<InternalPath> IInternalPathOwner.GetRingsAsInternalPath()
=> new[] { this.innerPath };
=> [this.innerPath];

private static CubicBezierLineSegment CreateSegment(Vector2 location, SizeF size)
{
Expand All @@ -116,7 +116,7 @@ private static CubicBezierLineSegment CreateSegment(Vector2 location, SizeF size
Vector2 pointMplusO = pointM + pointO;

PointF[] points =
{
[
new Vector2(rootLocation.X, pointM.Y),

new Vector2(rootLocation.X, pointMminusO.Y),
Expand All @@ -133,8 +133,8 @@ private static CubicBezierLineSegment CreateSegment(Vector2 location, SizeF size

new Vector2(pointMminusO.X, pointE.Y),
new Vector2(rootLocation.X, pointMplusO.Y),
new Vector2(rootLocation.X, pointM.Y),
};
new Vector2(rootLocation.X, pointM.Y)
];

return new CubicBezierLineSegment(points);
}
Expand Down
6 changes: 3 additions & 3 deletions src/ImageSharp.Drawing/Shapes/EmptyPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public sealed class EmptyPath : IPath
/// <summary>
/// Gets the closed path instance of the empty path
/// </summary>
public static EmptyPath ClosedPath { get; } = new EmptyPath(PathTypes.Closed);
public static EmptyPath ClosedPath { get; } = new(PathTypes.Closed);

/// <summary>
/// Gets the open path instance of the empty path
/// </summary>
public static EmptyPath OpenPath { get; } = new EmptyPath(PathTypes.Open);
public static EmptyPath OpenPath { get; } = new(PathTypes.Open);

/// <inheritdoc />
public PathTypes PathType { get; }
Expand All @@ -32,7 +32,7 @@ public sealed class EmptyPath : IPath
public IPath AsClosedPath() => ClosedPath;

/// <inheritdoc />
public IEnumerable<ISimplePath> Flatten() => Array.Empty<ISimplePath>();
public IEnumerable<ISimplePath> Flatten() => [];

/// <inheritdoc />
public IPath Transform(Matrix3x2 matrix) => this;
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp.Drawing/Shapes/Helpers/ArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static T[] Merge<T>(this T[] source1, T[] source2)
return source1;
}

var target = new T[source1.Length + source2.Length];
T[] target = new T[source1.Length + source2.Length];

for (int i = 0; i < source1.Length; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp.Drawing/Shapes/Helpers/VectorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static class VectorExtensions
/// </returns>
public static bool Equivalent(this PointF source1, PointF source2, float threshold)
{
var abs = Vector2.Abs(source1 - source2);
Vector2 abs = Vector2.Abs(source1 - source2);

return abs.X < threshold && abs.Y < threshold;
}
Expand All @@ -37,7 +37,7 @@ public static bool Equivalent(this PointF source1, PointF source2, float thresho
/// </returns>
public static bool Equivalent(this Vector2 source1, Vector2 source2, float threshold)
{
var abs = Vector2.Abs(source1 - source2);
Vector2 abs = Vector2.Abs(source1 - source2);

return abs.X < threshold && abs.Y < threshold;
}
Expand Down
Loading
Loading