Skip to content

Commit e1555fd

Browse files
Fix transform bounds calculations
1 parent aad5cfa commit e1555fd

File tree

8 files changed

+191
-202
lines changed

8 files changed

+191
-202
lines changed

src/ImageSharp/Processing/AffineTransformBuilder.cs

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace SixLabors.ImageSharp.Processing;
1212
public class AffineTransformBuilder
1313
{
1414
private readonly List<Func<Size, Matrix3x2>> transformMatrixFactories = new();
15-
private readonly List<Func<Size, Matrix3x2>> boundsMatrixFactories = new();
1615

1716
/// <summary>
1817
/// Prepends a rotation matrix using the given rotation angle in degrees
@@ -31,8 +30,7 @@ public AffineTransformBuilder PrependRotationDegrees(float degrees)
3130
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
3231
public AffineTransformBuilder PrependRotationRadians(float radians)
3332
=> this.Prepend(
34-
size => TransformUtils.CreateRotationTransformMatrixRadians(radians, size),
35-
size => TransformUtils.CreateRotationBoundsMatrixRadians(radians, size));
33+
size => TransformUtils.CreateRotationTransformMatrixRadians(radians, size));
3634

3735
/// <summary>
3836
/// Prepends a rotation matrix using the given rotation in degrees at the given origin.
@@ -68,9 +66,7 @@ public AffineTransformBuilder AppendRotationDegrees(float degrees)
6866
/// <param name="radians">The amount of rotation, in radians.</param>
6967
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
7068
public AffineTransformBuilder AppendRotationRadians(float radians)
71-
=> this.Append(
72-
size => TransformUtils.CreateRotationTransformMatrixRadians(radians, size),
73-
size => TransformUtils.CreateRotationBoundsMatrixRadians(radians, size));
69+
=> this.Append(size => TransformUtils.CreateRotationTransformMatrixRadians(radians, size));
7470

7571
/// <summary>
7672
/// Appends a rotation matrix using the given rotation in degrees at the given origin.
@@ -145,9 +141,7 @@ public AffineTransformBuilder AppendScale(Vector2 scales)
145141
/// <param name="degreesY">The Y angle, in degrees.</param>
146142
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
147143
public AffineTransformBuilder PrependSkewDegrees(float degreesX, float degreesY)
148-
=> this.Prepend(
149-
size => TransformUtils.CreateSkewTransformMatrixDegrees(degreesX, degreesY, size),
150-
size => TransformUtils.CreateSkewBoundsMatrixDegrees(degreesX, degreesY, size));
144+
=> this.Prepend(size => TransformUtils.CreateSkewTransformMatrixDegrees(degreesX, degreesY, size));
151145

152146
/// <summary>
153147
/// Prepends a centered skew matrix from the give angles in radians.
@@ -156,9 +150,7 @@ public AffineTransformBuilder PrependSkewDegrees(float degreesX, float degreesY)
156150
/// <param name="radiansY">The Y angle, in radians.</param>
157151
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
158152
public AffineTransformBuilder PrependSkewRadians(float radiansX, float radiansY)
159-
=> this.Prepend(
160-
size => TransformUtils.CreateSkewTransformMatrixRadians(radiansX, radiansY, size),
161-
size => TransformUtils.CreateSkewBoundsMatrixRadians(radiansX, radiansY, size));
153+
=> this.Prepend(size => TransformUtils.CreateSkewTransformMatrixRadians(radiansX, radiansY, size));
162154

163155
/// <summary>
164156
/// Prepends a skew matrix using the given angles in degrees at the given origin.
@@ -187,9 +179,7 @@ public AffineTransformBuilder PrependSkewRadians(float radiansX, float radiansY,
187179
/// <param name="degreesY">The Y angle, in degrees.</param>
188180
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
189181
public AffineTransformBuilder AppendSkewDegrees(float degreesX, float degreesY)
190-
=> this.Append(
191-
size => TransformUtils.CreateSkewTransformMatrixDegrees(degreesX, degreesY, size),
192-
size => TransformUtils.CreateSkewBoundsMatrixDegrees(degreesX, degreesY, size));
182+
=> this.Append(size => TransformUtils.CreateSkewTransformMatrixDegrees(degreesX, degreesY, size));
193183

194184
/// <summary>
195185
/// Appends a centered skew matrix from the give angles in radians.
@@ -198,9 +188,7 @@ public AffineTransformBuilder AppendSkewDegrees(float degreesX, float degreesY)
198188
/// <param name="radiansY">The Y angle, in radians.</param>
199189
/// <returns>The <see cref="AffineTransformBuilder"/>.</returns>
200190
public AffineTransformBuilder AppendSkewRadians(float radiansX, float radiansY)
201-
=> this.Append(
202-
size => TransformUtils.CreateSkewTransformMatrixRadians(radiansX, radiansY, size),
203-
size => TransformUtils.CreateSkewBoundsMatrixRadians(radiansX, radiansY, size));
191+
=> this.Append(size => TransformUtils.CreateSkewTransformMatrixRadians(radiansX, radiansY, size));
204192

205193
/// <summary>
206194
/// Appends a skew matrix using the given angles in degrees at the given origin.
@@ -267,7 +255,7 @@ public AffineTransformBuilder AppendTranslation(Vector2 position)
267255
public AffineTransformBuilder PrependMatrix(Matrix3x2 matrix)
268256
{
269257
CheckDegenerate(matrix);
270-
return this.Prepend(_ => matrix, _ => matrix);
258+
return this.Prepend(_ => matrix);
271259
}
272260

273261
/// <summary>
@@ -283,7 +271,7 @@ public AffineTransformBuilder PrependMatrix(Matrix3x2 matrix)
283271
public AffineTransformBuilder AppendMatrix(Matrix3x2 matrix)
284272
{
285273
CheckDegenerate(matrix);
286-
return this.Append(_ => matrix, _ => matrix);
274+
return this.Append(_ => matrix);
287275
}
288276

289277
/// <summary>
@@ -340,13 +328,13 @@ public Size GetTransformedSize(Rectangle sourceRectangle)
340328
// Translate the origin matrix to cater for source rectangle offsets.
341329
Matrix3x2 matrix = Matrix3x2.CreateTranslation(-sourceRectangle.Location);
342330

343-
foreach (Func<Size, Matrix3x2> factory in this.boundsMatrixFactories)
331+
foreach (Func<Size, Matrix3x2> factory in this.transformMatrixFactories)
344332
{
345333
matrix *= factory(size);
346334
CheckDegenerate(matrix);
347335
}
348336

349-
return TransformUtils.GetTransformedSize(size, matrix);
337+
return TransformUtils.GetTransformedSize(matrix, size);
350338
}
351339

352340
private static void CheckDegenerate(Matrix3x2 matrix)
@@ -357,17 +345,15 @@ private static void CheckDegenerate(Matrix3x2 matrix)
357345
}
358346
}
359347

360-
private AffineTransformBuilder Prepend(Func<Size, Matrix3x2> transformFactory, Func<Size, Matrix3x2> boundsFactory)
348+
private AffineTransformBuilder Prepend(Func<Size, Matrix3x2> transformFactory)
361349
{
362350
this.transformMatrixFactories.Insert(0, transformFactory);
363-
this.boundsMatrixFactories.Insert(0, boundsFactory);
364351
return this;
365352
}
366353

367-
private AffineTransformBuilder Append(Func<Size, Matrix3x2> transformFactory, Func<Size, Matrix3x2> boundsFactory)
354+
private AffineTransformBuilder Append(Func<Size, Matrix3x2> transformFactory)
368355
{
369356
this.transformMatrixFactories.Add(transformFactory);
370-
this.boundsMatrixFactories.Add(boundsFactory);
371357
return this;
372358
}
373359
}

src/ImageSharp/Processing/Processors/Transforms/Linear/LinearTransformUtility.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static float GetSamplingRadius<TResampler>(in TResampler sampler, int sou
4343
/// <returns>The <see cref="int"/>.</returns>
4444
[MethodImpl(InliningOptions.ShortMethod)]
4545
public static int GetRangeStart(float radius, float center, int min, int max)
46-
=> Numerics.Clamp((int)MathF.Ceiling(center - radius), min, max);
46+
=> Numerics.Clamp((int)MathF.Floor(center - radius), min, max);
4747

4848
/// <summary>
4949
/// Gets the end position (inclusive) for a sampling range given
@@ -56,5 +56,5 @@ public static int GetRangeStart(float radius, float center, int min, int max)
5656
/// <returns>The <see cref="int"/>.</returns>
5757
[MethodImpl(InliningOptions.ShortMethod)]
5858
public static int GetRangeEnd(float radius, float center, int min, int max)
59-
=> Numerics.Clamp((int)MathF.Floor(center + radius), min, max);
59+
=> Numerics.Clamp((int)MathF.Ceiling(center + radius), min, max);
6060
}

src/ImageSharp/Processing/Processors/Transforms/Linear/RotateProcessor.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ public RotateProcessor(float degrees, Size sourceSize)
2929
public RotateProcessor(float degrees, IResampler sampler, Size sourceSize)
3030
: this(
3131
TransformUtils.CreateRotationTransformMatrixDegrees(degrees, sourceSize),
32-
TransformUtils.CreateRotationBoundsMatrixDegrees(degrees, sourceSize),
3332
sampler,
3433
sourceSize)
3534
=> this.Degrees = degrees;
3635

3736
// Helper constructor
38-
private RotateProcessor(Matrix3x2 rotationMatrix, Matrix3x2 boundsMatrix, IResampler sampler, Size sourceSize)
39-
: base(rotationMatrix, sampler, TransformUtils.GetTransformedSize(sourceSize, boundsMatrix))
37+
private RotateProcessor(Matrix3x2 rotationMatrix, IResampler sampler, Size sourceSize)
38+
: base(rotationMatrix, sampler, TransformUtils.GetTransformedSize(rotationMatrix, sourceSize))
4039
{
4140
}
4241

src/ImageSharp/Processing/Processors/Transforms/Linear/SkewProcessor.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public SkewProcessor(float degreesX, float degreesY, Size sourceSize)
3131
public SkewProcessor(float degreesX, float degreesY, IResampler sampler, Size sourceSize)
3232
: this(
3333
TransformUtils.CreateSkewTransformMatrixDegrees(degreesX, degreesY, sourceSize),
34-
TransformUtils.CreateSkewBoundsMatrixDegrees(degreesX, degreesY, sourceSize),
3534
sampler,
3635
sourceSize)
3736
{
@@ -40,8 +39,8 @@ public SkewProcessor(float degreesX, float degreesY, IResampler sampler, Size so
4039
}
4140

4241
// Helper constructor:
43-
private SkewProcessor(Matrix3x2 skewMatrix, Matrix3x2 boundsMatrix, IResampler sampler, Size sourceSize)
44-
: base(skewMatrix, sampler, TransformUtils.GetTransformedSize(sourceSize, boundsMatrix))
42+
private SkewProcessor(Matrix3x2 skewMatrix, IResampler sampler, Size sourceSize)
43+
: base(skewMatrix, sampler, TransformUtils.GetTransformedSize(skewMatrix, sourceSize))
4544
{
4645
}
4746

0 commit comments

Comments
 (0)