From ec1e4eafaaf991141dd4b6c38af739ca01050cf6 Mon Sep 17 00:00:00 2001 From: Jonas Frost Date: Wed, 18 May 2016 23:12:20 -0700 Subject: [PATCH] Optimized Rotate() to use pre calculated rotation matrix Former-commit-id: f54c50b9425657fa4cc899838b96c9f8a6529017 Former-commit-id: 819dd1dc7b3dd5d2824ef07ce39ef8ee791f8d40 Former-commit-id: 721b476e0647f7eb6f2a4e7fff037354215a7e81 --- src/ImageProcessorCore/Numerics/Point.cs | 23 +++++++++++++++++++++++ src/ImageProcessorCore/Samplers/Rotate.cs | 6 +++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/ImageProcessorCore/Numerics/Point.cs b/src/ImageProcessorCore/Numerics/Point.cs index 370592406..472a1c773 100644 --- a/src/ImageProcessorCore/Numerics/Point.cs +++ b/src/ImageProcessorCore/Numerics/Point.cs @@ -158,6 +158,29 @@ namespace ImageProcessorCore return new Vector2(this.X, this.Y); } + /// + /// Rotates a point around a given a rotation matrix. + /// + /// The point to rotate + /// Rotation matrix used + /// + public static Point Rotate( Point point, Matrix3x2 rotation ) + { + return new Point( Vector2.Transform( point.backingVector, rotation ) ); + } + + /// + /// Creates a rotation matrix for + /// + /// The origin point to rotate around + /// Rotation in degrees + /// + public static Matrix3x2 CreateRotatation( Point origin, float degrees ) + { + float radians = (float)ImageMaths.DegreesToRadians( degrees ); + return Matrix3x2.CreateRotation( radians, origin.backingVector ); + } + /// /// Rotates a point around a given origin by the specified angle in degrees. /// diff --git a/src/ImageProcessorCore/Samplers/Rotate.cs b/src/ImageProcessorCore/Samplers/Rotate.cs index b85667730..bc10e2fdb 100644 --- a/src/ImageProcessorCore/Samplers/Rotate.cs +++ b/src/ImageProcessorCore/Samplers/Rotate.cs @@ -3,6 +3,8 @@ // Licensed under the Apache License, Version 2.0. // +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];