Browse Source

Optimized Rotate() to use pre calculated rotation matrix

Former-commit-id: f54c50b9425657fa4cc899838b96c9f8a6529017
Former-commit-id: 819dd1dc7b3dd5d2824ef07ce39ef8ee791f8d40
Former-commit-id: 721b476e0647f7eb6f2a4e7fff037354215a7e81
af/merge-core
Jonas Frost 10 years ago
parent
commit
ec1e4eafaa
  1. 23
      src/ImageProcessorCore/Numerics/Point.cs
  2. 6
      src/ImageProcessorCore/Samplers/Rotate.cs

23
src/ImageProcessorCore/Numerics/Point.cs

@ -158,6 +158,29 @@ namespace ImageProcessorCore
return new Vector2(this.X, this.Y);
}
/// <summary>
/// Rotates a point around a given a rotation matrix.
/// </summary>
/// <param name="point">The point to rotate</param>
/// <param name="rotation">Rotation matrix used</param>
/// <returns></returns>
public static Point Rotate( Point point, Matrix3x2 rotation )
{
return new Point( Vector2.Transform( point.backingVector, rotation ) );
}
/// <summary>
/// Creates a rotation matrix for
/// </summary>
/// <param name="origion">The origin point to rotate around</param>
/// <param name="degrees">Rotation in degrees</param>
/// <returns></returns>
public static Matrix3x2 CreateRotatation( Point origin, float degrees )
{
float radians = (float)ImageMaths.DegreesToRadians( degrees );
return Matrix3x2.CreateRotation( radians, origin.backingVector );
}
/// <summary>
/// Rotates a point around a given origin by the specified angle in degrees.
/// </summary>

6
src/ImageProcessorCore/Samplers/Rotate.cs

@ -3,6 +3,8 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
using System.Numerics;
namespace ImageProcessorCore.Samplers
{
using System.Threading.Tasks;
@ -62,6 +64,8 @@ namespace ImageProcessorCore.Samplers
float widthFactor = source.Width / (float)target.Width;
float heightFactor = source.Height / (float)target.Height;
Matrix3x2 rotation = Point.CreateRotatation( centre, negativeAngle );
Parallel.For(
startY,
endY,
@ -78,7 +82,7 @@ namespace ImageProcessorCore.Samplers
int originX = (int)((x - startX) * widthFactor);
// Rotate at the centre point
Point rotated = Point.Rotate(new Point(originX, originY), centre, negativeAngle);
Point rotated = Point.Rotate(new Point(originX, originY), rotation);
if (sourceRectangle.Contains(rotated.X, rotated.Y))
{
target[x, y] = source[rotated.X, rotated.Y];

Loading…
Cancel
Save