@@ -12,7 +12,28 @@ namespace SixLabors.ImageSharp.Processing;
1212public class AffineTransformBuilder
1313{
1414 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 ; }
1637
1738 /// <summary>
1839 /// Prepends a rotation matrix using the given rotation angle in degrees
@@ -31,8 +52,7 @@ public AffineTransformBuilder PrependRotationDegrees(float degrees)
3152 /// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
3253 public AffineTransformBuilder PrependRotationRadians ( float radians )
3354 => this . Prepend (
34- size => TransformUtils . CreateRotationTransformMatrixRadians ( radians , size ) ,
35- size => TransformUtils . CreateRotationBoundsMatrixRadians ( radians , size ) ) ;
55+ size => TransformUtils . CreateRotationTransformMatrixRadians ( radians , size , this . TransformSpace ) ) ;
3656
3757 /// <summary>
3858 /// Prepends a rotation matrix using the given rotation in degrees at the given origin.
@@ -68,9 +88,7 @@ public AffineTransformBuilder AppendRotationDegrees(float degrees)
6888 /// <param name="radians">The amount of rotation, in radians.</param>
6989 /// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
7090 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 ) ) ;
7492
7593 /// <summary>
7694 /// Appends a rotation matrix using the given rotation in degrees at the given origin.
@@ -145,9 +163,7 @@ public AffineTransformBuilder AppendScale(Vector2 scales)
145163 /// <param name="degreesY">The Y angle, in degrees.</param>
146164 /// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
147165 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 ) ) ;
151167
152168 /// <summary>
153169 /// Prepends a centered skew matrix from the give angles in radians.
@@ -156,9 +172,7 @@ public AffineTransformBuilder PrependSkewDegrees(float degreesX, float degreesY)
156172 /// <param name="radiansY">The Y angle, in radians.</param>
157173 /// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
158174 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 ) ) ;
162176
163177 /// <summary>
164178 /// 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,
187201 /// <param name="degreesY">The Y angle, in degrees.</param>
188202 /// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
189203 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 ) ) ;
193205
194206 /// <summary>
195207 /// Appends a centered skew matrix from the give angles in radians.
@@ -198,9 +210,7 @@ public AffineTransformBuilder AppendSkewDegrees(float degreesX, float degreesY)
198210 /// <param name="radiansY">The Y angle, in radians.</param>
199211 /// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
200212 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 ) ) ;
204214
205215 /// <summary>
206216 /// Appends a skew matrix using the given angles in degrees at the given origin.
@@ -267,7 +277,7 @@ public AffineTransformBuilder AppendTranslation(Vector2 position)
267277 public AffineTransformBuilder PrependMatrix ( Matrix3x2 matrix )
268278 {
269279 CheckDegenerate ( matrix ) ;
270- return this . Prepend ( _ => matrix , _ => matrix ) ;
280+ return this . Prepend ( _ => matrix ) ;
271281 }
272282
273283 /// <summary>
@@ -283,7 +293,7 @@ public AffineTransformBuilder PrependMatrix(Matrix3x2 matrix)
283293 public AffineTransformBuilder AppendMatrix ( Matrix3x2 matrix )
284294 {
285295 CheckDegenerate ( matrix ) ;
286- return this . Append ( _ => matrix , _ => matrix ) ;
296+ return this . Append ( _ => matrix ) ;
287297 }
288298
289299 /// <summary>
@@ -340,13 +350,13 @@ public Size GetTransformedSize(Rectangle sourceRectangle)
340350 // Translate the origin matrix to cater for source rectangle offsets.
341351 Matrix3x2 matrix = Matrix3x2 . CreateTranslation ( - sourceRectangle . Location ) ;
342352
343- foreach ( Func < Size , Matrix3x2 > factory in this . boundsMatrixFactories )
353+ foreach ( Func < Size , Matrix3x2 > factory in this . transformMatrixFactories )
344354 {
345355 matrix *= factory ( size ) ;
346356 CheckDegenerate ( matrix ) ;
347357 }
348358
349- return TransformUtils . GetTransformedSize ( size , matrix ) ;
359+ return TransformUtils . GetTransformedSize ( matrix , size , this . TransformSpace ) ;
350360 }
351361
352362 private static void CheckDegenerate ( Matrix3x2 matrix )
@@ -357,17 +367,15 @@ private static void CheckDegenerate(Matrix3x2 matrix)
357367 }
358368 }
359369
360- private AffineTransformBuilder Prepend ( Func < Size , Matrix3x2 > transformFactory , Func < Size , Matrix3x2 > boundsFactory )
370+ private AffineTransformBuilder Prepend ( Func < Size , Matrix3x2 > transformFactory )
361371 {
362372 this . transformMatrixFactories . Insert ( 0 , transformFactory ) ;
363- this . boundsMatrixFactories . Insert ( 0 , boundsFactory ) ;
364373 return this ;
365374 }
366375
367- private AffineTransformBuilder Append ( Func < Size , Matrix3x2 > transformFactory , Func < Size , Matrix3x2 > boundsFactory )
376+ private AffineTransformBuilder Append ( Func < Size , Matrix3x2 > transformFactory )
368377 {
369378 this . transformMatrixFactories . Add ( transformFactory ) ;
370- this . boundsMatrixFactories . Add ( boundsFactory ) ;
371379 return this ;
372380 }
373381}
0 commit comments