Browse Source

Switches to rectangle structure for the rotation calculations

Former-commit-id: cc6abe13bd9824d32250738b25486cc5c9195849
Former-commit-id: 5bfec4b1d233e2390c92a5e565e537a84c380c8c
af/merge-core
Thomas Broust 11 years ago
parent
commit
4d9f300aac
  1. 2
      src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj
  2. 22
      src/ImageProcessor.UnitTests/Imaging/Helpers/ImageMathsUnitTests.cs
  3. 1
      src/ImageProcessor/ImageProcessor.csproj
  4. 10
      src/ImageProcessor/Imaging/Helpers/ImageMaths.cs
  5. 2
      src/ImageProcessor/Processors/Rotate.cs

2
src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj

@ -64,7 +64,7 @@
<Compile Include="Imaging\ColorUnitTests.cs" /> <Compile Include="Imaging\ColorUnitTests.cs" />
<Compile Include="Imaging\CropLayerUnitTests.cs" /> <Compile Include="Imaging\CropLayerUnitTests.cs" />
<Compile Include="Imaging\FastBitmapUnitTests.cs" /> <Compile Include="Imaging\FastBitmapUnitTests.cs" />
<Compile Include="Imaging\RotationUnitTests.cs" /> <Compile Include="Imaging\Helpers\ImageMathsUnitTests.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup> <ItemGroup>

22
src/ImageProcessor.UnitTests/Imaging/Helpers/ImageMathsUnitTests.cs

@ -5,20 +5,38 @@
using ImageProcessor.Imaging.Helpers; using ImageProcessor.Imaging.Helpers;
using NUnit.Framework; using NUnit.Framework;
/// <summary>
/// Test harness for the image math unit tests
/// </summary>
public class ImageMathsUnitTests public class ImageMathsUnitTests
{ {
/// <summary>
/// Tests that the bounding rectangle of a rotated image is calculated
/// </summary>
/// <param name="width">The width of the image.</param>
/// <param name="height">The height of the image.</param>
/// <param name="angle">The rotation angle.</param>
/// <param name="expectedWidth">The expected width.</param>
/// <param name="expectedHeight">The expected height.</param>
[Test] [Test]
[TestCase(100, 100, 45, 141, 141)] [TestCase(100, 100, 45, 141, 141)]
[TestCase(100, 100, 30, 137, 137)] [TestCase(100, 100, 30, 137, 137)]
[TestCase(100, 200, 50, 217, 205)] [TestCase(100, 200, 50, 217, 205)]
public void NewSizeAfterRotationIsCalculated(int width, int height, float angle, int expectedWidth, int expectedHeight) public void BoundingRotatedRectangleIsCalculated(int width, int height, float angle, int expectedWidth, int expectedHeight)
{ {
Size result = ImageMaths.GetBoundingRotatedRectangle(width, height, angle); Rectangle result = ImageMaths.GetBoundingRotatedRectangle(width, height, angle);
result.Width.Should().Be(expectedWidth, "because the rotated width should have been calculated"); result.Width.Should().Be(expectedWidth, "because the rotated width should have been calculated");
result.Height.Should().Be(expectedHeight, "because the rotated height should have been calculated"); result.Height.Should().Be(expectedHeight, "because the rotated height should have been calculated");
} }
/// <summary>
/// Tests that the zoom needed for an "inside" rotation is calculated
/// </summary>
/// <param name="imageWidth">Width of the image.</param>
/// <param name="imageHeight">Height of the image.</param>
/// <param name="angle">The rotation angle.</param>
/// <param name="expected">The expected zoom.</param>
[Test] [Test]
[TestCase(100, 100, 45, 1.41f)] [TestCase(100, 100, 45, 1.41f)]
[TestCase(100, 100, 15, 1.22f)] [TestCase(100, 100, 15, 1.22f)]

1
src/ImageProcessor/ImageProcessor.csproj

@ -215,7 +215,6 @@
<Compile Include="Imaging\ResizeMode.cs" /> <Compile Include="Imaging\ResizeMode.cs" />
<Compile Include="Imaging\Resizer.cs" /> <Compile Include="Imaging\Resizer.cs" />
<Compile Include="Imaging\RotateInsideLayer.cs" /> <Compile Include="Imaging\RotateInsideLayer.cs" />
<Compile Include="Imaging\Rotation.cs" />
<Compile Include="Imaging\RoundedCornerLayer.cs" /> <Compile Include="Imaging\RoundedCornerLayer.cs" />
<Compile Include="Imaging\TextLayer.cs" /> <Compile Include="Imaging\TextLayer.cs" />
<Compile Include="Processors\Alpha.cs" /> <Compile Include="Processors\Alpha.cs" />

10
src/ImageProcessor/Imaging/Helpers/ImageMaths.cs

@ -111,7 +111,7 @@ namespace ImageProcessor.Imaging.Helpers
/// <param name="height">The height of the image.</param> /// <param name="height">The height of the image.</param>
/// <param name="angle">The angle of rotation.</param> /// <param name="angle">The angle of rotation.</param>
/// <returns>The new size of the image</returns> /// <returns>The new size of the image</returns>
public static Size GetBoundingRotatedRectangle(int width, int height, float angle) public static Rectangle GetBoundingRotatedRectangle(int width, int height, float angle)
{ {
double widthAsDouble = width; double widthAsDouble = width;
double heightAsDouble = height; double heightAsDouble = height;
@ -129,9 +129,11 @@ namespace ImageProcessor.Imaging.Helpers
double height2 = (widthAsDouble * radiansSin) + (heightAsDouble * radiansCos); double height2 = (widthAsDouble * radiansSin) + (heightAsDouble * radiansCos);
// Get the external vertex for the rotation // Get the external vertex for the rotation
Size result = new Size(); Rectangle result = new Rectangle(
result.Width = Convert.ToInt32(Math.Max(Math.Abs(width1), Math.Abs(width2))); 0,
result.Height = Convert.ToInt32(Math.Max(Math.Abs(height1), Math.Abs(height2))); 0,
Convert.ToInt32(Math.Max(Math.Abs(width1), Math.Abs(width2))),
Convert.ToInt32(Math.Max(Math.Abs(height1), Math.Abs(height2))));
return result; return result;
} }

2
src/ImageProcessor/Processors/Rotate.cs

@ -104,7 +104,7 @@ namespace ImageProcessor.Processors
/// </remarks> /// </remarks>
private Bitmap RotateImage(Image image, float rotateAtX, float rotateAtY, float angle) private Bitmap RotateImage(Image image, float rotateAtX, float rotateAtY, float angle)
{ {
Size newSize = Imaging.Helpers.ImageMaths.GetBoundingRotatedRectangle(image.Width, image.Height, angle); Rectangle newSize = Imaging.Helpers.ImageMaths.GetBoundingRotatedRectangle(image.Width, image.Height, angle);
int x = (newSize.Width - image.Width) / 2; int x = (newSize.Width - image.Width) / 2;
int y = (newSize.Height - image.Height) / 2; int y = (newSize.Height - image.Height) / 2;

Loading…
Cancel
Save