Skip to content

Commit 837df16

Browse files
Migrate drawing transforms to Matrix4x4
1 parent 233b04e commit 837df16

108 files changed

Lines changed: 845 additions & 431 deletions

File tree

Some content is hidden

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

samples/DrawShapesWithImageSharp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public static void SaveImageWithPath(this IPathCollection collection, IPath shap
267267
{
268268
// Fill the canvas background and draw our shape.
269269
canvas.Fill(Brushes.Solid(Color.DarkBlue));
270-
canvas.Fill(shape, Brushes.Solid(Color.White.WithAlpha(.25F)));
270+
canvas.Fill(Brushes.Solid(Color.White.WithAlpha(.25F)), shape);
271271

272272
// Draw our path collection.
273273
canvas.Fill(Brushes.Solid(Color.HotPink), collection);

samples/WebGPUWindowDemo/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ private static void OnRender(double deltaTime)
304304
{
305305
ref Ball ball = ref balls[i];
306306
EllipsePolygon ellipse = new(ball.X, ball.Y, ball.Radius);
307-
canvas.Fill(ellipse, Brushes.Solid(ball.Color));
307+
canvas.Fill(Brushes.Solid(ball.Color), ellipse);
308308
}
309309

310310
// Flush submits all queued draw operations to the GPU compositor and
@@ -367,7 +367,7 @@ private static void DrawScrollingText(DrawingCanvas<Bgra32> canvas, int w, int h
367367
continue;
368368
}
369369

370-
canvas.Fill(path.Transform(translation), textBrush);
370+
canvas.Fill(textBrush, path.Transform(new Matrix4x4(translation)));
371371
}
372372
}
373373

src/ImageSharp.Drawing.WebGPU/Shaders/CompositeComputeShader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ fn elliptic_gradient_t(x: f32, y: f32, cmd: Params) -> f32 {
275275
276276
if rx_sq < 1e-20 { return 0.0; }
277277
if ry_sq < 1e-20 { return 0.0; }
278-
return rotated_x * rotated_x / rx_sq + rotated_y * rotated_y / ry_sq;
278+
return sqrt(rotated_x * rotated_x / rx_sq + rotated_y * rotated_y / ry_sq);
279279
}
280280
281281
// Sweep (angular) gradient. Computes radians and sweep from raw degrees.

src/ImageSharp.Drawing/ArcLineSegment.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public ArcLineSegment(PointF center, SizeF radius, float rotation, float startAn
9393
/// </summary>
9494
/// <param name="matrix">The transformation matrix.</param>
9595
/// <returns>An <see cref="ArcLineSegment"/> with the matrix applied to it.</returns>
96-
public ILineSegment Transform(Matrix3x2 matrix)
96+
public ILineSegment Transform(Matrix4x4 matrix)
9797
{
9898
if (matrix.IsIdentity)
9999
{
@@ -110,7 +110,7 @@ public ILineSegment Transform(Matrix3x2 matrix)
110110
}
111111

112112
/// <inheritdoc/>
113-
ILineSegment ILineSegment.Transform(Matrix3x2 matrix) => this.Transform(matrix);
113+
ILineSegment ILineSegment.Transform(Matrix4x4 matrix) => this.Transform(matrix);
114114

115115
private static PointF[] EllipticArcFromEndParams(
116116
PointF from,

src/ImageSharp.Drawing/ComplexPolygon.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public ComplexPolygon(params IPath[] paths)
6767
public RectangleF Bounds => this.bounds ??= this.CalcBounds();
6868

6969
/// <inheritdoc/>
70-
public IPath Transform(Matrix3x2 matrix)
70+
public IPath Transform(Matrix4x4 matrix)
7171
{
7272
if (matrix.IsIdentity)
7373
{

src/ImageSharp.Drawing/CubicBezierLineSegment.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public CubicBezierLineSegment(PointF start, PointF controlPoint1, PointF control
8181
/// </summary>
8282
/// <param name="matrix">The matrix.</param>
8383
/// <returns>A line segment with the matrix applied to it.</returns>
84-
public CubicBezierLineSegment Transform(Matrix3x2 matrix)
84+
public CubicBezierLineSegment Transform(Matrix4x4 matrix)
8585
{
8686
if (matrix.IsIdentity)
8787
{
@@ -100,7 +100,7 @@ public CubicBezierLineSegment Transform(Matrix3x2 matrix)
100100
}
101101

102102
/// <inheritdoc/>
103-
ILineSegment ILineSegment.Transform(Matrix3x2 matrix) => this.Transform(matrix);
103+
ILineSegment ILineSegment.Transform(Matrix4x4 matrix) => this.Transform(matrix);
104104

105105
private static PointF[] GetDrawingPoints(PointF[] controlPoints)
106106
{

src/ImageSharp.Drawing/EllipsePolygon.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public EllipsePolygon(float x, float y, float radius)
5959
}
6060

6161
/// <inheritdoc/>
62-
public override IPath Transform(Matrix3x2 matrix)
62+
public override IPath Transform(Matrix4x4 matrix)
6363
{
6464
if (matrix.IsIdentity)
6565
{

src/ImageSharp.Drawing/EmptyPath.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ public sealed class EmptyPath : IPath
3535
public IEnumerable<ISimplePath> Flatten() => [];
3636

3737
/// <inheritdoc />
38-
public IPath Transform(Matrix3x2 matrix) => this;
38+
public IPath Transform(Matrix4x4 matrix) => this;
3939
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.Numerics;
5+
using System.Runtime.CompilerServices;
6+
7+
namespace SixLabors.ImageSharp.Drawing.Helpers;
8+
9+
/// <summary>
10+
/// Provides helper methods for extracting properties from transformation matrices.
11+
/// </summary>
12+
internal static class MatrixUtilities
13+
{
14+
/// <summary>
15+
/// Extracts the average 2D scale factor from a <see cref="Matrix4x4"/>.
16+
/// This is the mean of the X and Y axis scale magnitudes, suitable for
17+
/// uniformly scaling radii under non-uniform or projective transforms.
18+
/// </summary>
19+
/// <param name="matrix">The transformation matrix.</param>
20+
/// <returns>The average scale factor.</returns>
21+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
22+
public static float GetAverageScale(in Matrix4x4 matrix)
23+
{
24+
float sx = MathF.Sqrt((matrix.M11 * matrix.M11) + (matrix.M12 * matrix.M12));
25+
float sy = MathF.Sqrt((matrix.M21 * matrix.M21) + (matrix.M22 * matrix.M22));
26+
return (sx + sy) * 0.5f;
27+
}
28+
}

src/ImageSharp.Drawing/ILineSegment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ public interface ILineSegment
2929
/// </summary>
3030
/// <param name="matrix">The matrix.</param>
3131
/// <returns>A line segment with the matrix applied to it.</returns>
32-
ILineSegment Transform(Matrix3x2 matrix);
32+
ILineSegment Transform(Matrix4x4 matrix);
3333
}

0 commit comments

Comments
 (0)