@@ -12,7 +12,28 @@ namespace SixLabors.ImageSharp.Processing;
12
12
public class AffineTransformBuilder
13
13
{
14
14
private readonly List < Func < Size , Matrix3x2 > > transformMatrixFactories = new ( ) ;
15
- private readonly List < Func < Size , Matrix3x2 > > boundsMatrixFactories = new ( ) ;
15
+
16
+ /// <summary>
17
+ /// Initializes a new instance of the <see cref="AffineTransformBuilder"/> class.
18
+ /// </summary>
19
+ public AffineTransformBuilder ( )
20
+ : this ( TransformSpace . Pixel )
21
+ {
22
+ }
23
+
24
+ /// <summary>
25
+ /// Initializes a new instance of the <see cref="AffineTransformBuilder"/> class.
26
+ /// </summary>
27
+ /// <param name="transformSpace">
28
+ /// The <see cref="TransformSpace"/> to use when applying the affine transform.
29
+ /// </param>
30
+ public AffineTransformBuilder ( TransformSpace transformSpace )
31
+ => this . TransformSpace = transformSpace ;
32
+
33
+ /// <summary>
34
+ /// Gets the <see cref="TransformSpace"/> to use when applying the affine transform.
35
+ /// </summary>
36
+ public TransformSpace TransformSpace { get ; }
16
37
17
38
/// <summary>
18
39
/// Prepends a rotation matrix using the given rotation angle in degrees
@@ -31,8 +52,7 @@ public AffineTransformBuilder PrependRotationDegrees(float degrees)
31
52
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
32
53
public AffineTransformBuilder PrependRotationRadians ( float radians )
33
54
=> this . Prepend (
34
- size => TransformUtils . CreateRotationTransformMatrixRadians ( radians , size ) ,
35
- size => TransformUtils . CreateRotationBoundsMatrixRadians ( radians , size ) ) ;
55
+ size => TransformUtils . CreateRotationTransformMatrixRadians ( radians , size , this . TransformSpace ) ) ;
36
56
37
57
/// <summary>
38
58
/// Prepends a rotation matrix using the given rotation in degrees at the given origin.
@@ -68,9 +88,7 @@ public AffineTransformBuilder AppendRotationDegrees(float degrees)
68
88
/// <param name="radians">The amount of rotation, in radians.</param>
69
89
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
70
90
public AffineTransformBuilder AppendRotationRadians ( float radians )
71
- => this . Append (
72
- size => TransformUtils . CreateRotationTransformMatrixRadians ( radians , size ) ,
73
- size => TransformUtils . CreateRotationBoundsMatrixRadians ( radians , size ) ) ;
91
+ => this . Append ( size => TransformUtils . CreateRotationTransformMatrixRadians ( radians , size , this . TransformSpace ) ) ;
74
92
75
93
/// <summary>
76
94
/// Appends a rotation matrix using the given rotation in degrees at the given origin.
@@ -145,9 +163,7 @@ public AffineTransformBuilder AppendScale(Vector2 scales)
145
163
/// <param name="degreesY">The Y angle, in degrees.</param>
146
164
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
147
165
public AffineTransformBuilder PrependSkewDegrees ( float degreesX , float degreesY )
148
- => this . Prepend (
149
- size => TransformUtils . CreateSkewTransformMatrixDegrees ( degreesX , degreesY , size ) ,
150
- size => TransformUtils . CreateSkewBoundsMatrixDegrees ( degreesX , degreesY , size ) ) ;
166
+ => this . PrependSkewRadians ( GeometryUtilities . DegreeToRadian ( degreesX ) , GeometryUtilities . DegreeToRadian ( degreesY ) ) ;
151
167
152
168
/// <summary>
153
169
/// Prepends a centered skew matrix from the give angles in radians.
@@ -156,9 +172,7 @@ public AffineTransformBuilder PrependSkewDegrees(float degreesX, float degreesY)
156
172
/// <param name="radiansY">The Y angle, in radians.</param>
157
173
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
158
174
public AffineTransformBuilder PrependSkewRadians ( float radiansX , float radiansY )
159
- => this . Prepend (
160
- size => TransformUtils . CreateSkewTransformMatrixRadians ( radiansX , radiansY , size ) ,
161
- size => TransformUtils . CreateSkewBoundsMatrixRadians ( radiansX , radiansY , size ) ) ;
175
+ => this . Prepend ( size => TransformUtils . CreateSkewTransformMatrixRadians ( radiansX , radiansY , size , this . TransformSpace ) ) ;
162
176
163
177
/// <summary>
164
178
/// Prepends a skew matrix using the given angles in degrees at the given origin.
@@ -187,9 +201,7 @@ public AffineTransformBuilder PrependSkewRadians(float radiansX, float radiansY,
187
201
/// <param name="degreesY">The Y angle, in degrees.</param>
188
202
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
189
203
public AffineTransformBuilder AppendSkewDegrees ( float degreesX , float degreesY )
190
- => this . Append (
191
- size => TransformUtils . CreateSkewTransformMatrixDegrees ( degreesX , degreesY , size ) ,
192
- size => TransformUtils . CreateSkewBoundsMatrixDegrees ( degreesX , degreesY , size ) ) ;
204
+ => this . AppendSkewRadians ( GeometryUtilities . DegreeToRadian ( degreesX ) , GeometryUtilities . DegreeToRadian ( degreesY ) ) ;
193
205
194
206
/// <summary>
195
207
/// Appends a centered skew matrix from the give angles in radians.
@@ -198,9 +210,7 @@ public AffineTransformBuilder AppendSkewDegrees(float degreesX, float degreesY)
198
210
/// <param name="radiansY">The Y angle, in radians.</param>
199
211
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
200
212
public AffineTransformBuilder AppendSkewRadians ( float radiansX , float radiansY )
201
- => this . Append (
202
- size => TransformUtils . CreateSkewTransformMatrixRadians ( radiansX , radiansY , size ) ,
203
- size => TransformUtils . CreateSkewBoundsMatrixRadians ( radiansX , radiansY , size ) ) ;
213
+ => this . Append ( size => TransformUtils . CreateSkewTransformMatrixRadians ( radiansX , radiansY , size , this . TransformSpace ) ) ;
204
214
205
215
/// <summary>
206
216
/// Appends a skew matrix using the given angles in degrees at the given origin.
@@ -267,7 +277,7 @@ public AffineTransformBuilder AppendTranslation(Vector2 position)
267
277
public AffineTransformBuilder PrependMatrix ( Matrix3x2 matrix )
268
278
{
269
279
CheckDegenerate ( matrix ) ;
270
- return this . Prepend ( _ => matrix , _ => matrix ) ;
280
+ return this . Prepend ( _ => matrix ) ;
271
281
}
272
282
273
283
/// <summary>
@@ -283,7 +293,7 @@ public AffineTransformBuilder PrependMatrix(Matrix3x2 matrix)
283
293
public AffineTransformBuilder AppendMatrix ( Matrix3x2 matrix )
284
294
{
285
295
CheckDegenerate ( matrix ) ;
286
- return this . Append ( _ => matrix , _ => matrix ) ;
296
+ return this . Append ( _ => matrix ) ;
287
297
}
288
298
289
299
/// <summary>
@@ -340,13 +350,13 @@ public Size GetTransformedSize(Rectangle sourceRectangle)
340
350
// Translate the origin matrix to cater for source rectangle offsets.
341
351
Matrix3x2 matrix = Matrix3x2 . CreateTranslation ( - sourceRectangle . Location ) ;
342
352
343
- foreach ( Func < Size , Matrix3x2 > factory in this . boundsMatrixFactories )
353
+ foreach ( Func < Size , Matrix3x2 > factory in this . transformMatrixFactories )
344
354
{
345
355
matrix *= factory ( size ) ;
346
356
CheckDegenerate ( matrix ) ;
347
357
}
348
358
349
- return TransformUtils . GetTransformedSize ( size , matrix ) ;
359
+ return TransformUtils . GetTransformedSize ( matrix , size , this . TransformSpace ) ;
350
360
}
351
361
352
362
private static void CheckDegenerate ( Matrix3x2 matrix )
@@ -357,17 +367,15 @@ private static void CheckDegenerate(Matrix3x2 matrix)
357
367
}
358
368
}
359
369
360
- private AffineTransformBuilder Prepend ( Func < Size , Matrix3x2 > transformFactory , Func < Size , Matrix3x2 > boundsFactory )
370
+ private AffineTransformBuilder Prepend ( Func < Size , Matrix3x2 > transformFactory )
361
371
{
362
372
this . transformMatrixFactories . Insert ( 0 , transformFactory ) ;
363
- this . boundsMatrixFactories . Insert ( 0 , boundsFactory ) ;
364
373
return this ;
365
374
}
366
375
367
- private AffineTransformBuilder Append ( Func < Size , Matrix3x2 > transformFactory , Func < Size , Matrix3x2 > boundsFactory )
376
+ private AffineTransformBuilder Append ( Func < Size , Matrix3x2 > transformFactory )
368
377
{
369
378
this . transformMatrixFactories . Add ( transformFactory ) ;
370
- this . boundsMatrixFactories . Add ( boundsFactory ) ;
371
379
return this ;
372
380
}
373
381
}
0 commit comments