Browse Source

Refactors the rotation and adds a few unit tests

Former-commit-id: 1ae4da1184336db1522c1a35f68235c0a28b0413
Former-commit-id: e8a76a404d1d986a5e9e9469c274ce705879b964
pull/17/head
Thomas Broust 11 years ago
parent
commit
6f76ef23f7
  1. 13
      src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs
  2. 2
      src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj
  3. 22
      src/ImageProcessor.UnitTests/Imaging/RotationUnitTests.cs
  4. 16
      src/ImageProcessor.UnitTests/Processors/RotateInsideUnitTests.cs
  5. 1
      src/ImageProcessor/ImageProcessor.csproj
  6. 43
      src/ImageProcessor/Imaging/Rotation.cs
  7. 25
      src/ImageProcessor/Processors/Rotate.cs
  8. 88
      src/ImageProcessor/Processors/RotateInside.cs
  9. 3
      src/ImageProcessor/Properties/AssemblyInfo.cs

13
src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs

@ -466,6 +466,19 @@ namespace ImageProcessor.UnitTests
}
}
[Test]
public void ImageIsRotatedInside()
{
foreach (ImageFactory imageFactory in this.ListInputImages())
{
Image original = (Image)imageFactory.Image.Clone();
imageFactory.RotateInside(45);
imageFactory.Image.Width.Should().Be(original.Width, "because the rotated image dimensions should not have changed");
imageFactory.Image.Height.Should().Be(original.Height, "because the rotated image dimensions should not have changed");
}
}
/// <summary>
/// Tests that the images hue has been altered.
/// </summary>

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

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

22
src/ImageProcessor.UnitTests/Imaging/RotationUnitTests.cs

@ -0,0 +1,22 @@
namespace ImageProcessor.UnitTests.Imaging
{
using System.Drawing;
using FluentAssertions;
using ImageProcessor.Imaging;
using NUnit.Framework;
public class RotationUnitTests
{
[Test]
[TestCase(100, 100, 45, 141, 141)]
[TestCase(100, 100, 30, 137, 137)]
[TestCase(100, 200, 50, 217, 205)]
public void NewSizeAfterRotationIsCalculated(int width, int height, float angle, int expectedWidth, int expectedHeight)
{
Size result = Rotation.NewSizeAfterRotation(width, height, angle);
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");
}
}
}

16
src/ImageProcessor.UnitTests/Processors/RotateInsideUnitTests.cs

@ -0,0 +1,16 @@
namespace ImageProcessor.UnitTests.Processors
{
using System.Collections.Generic;
using ImageProcessor.Processors;
using NUnit.Framework;
public class RotateInsideUnitTests
{
[Test]
[TestCase(100, 100, 15, 150)]
public void ZoomIsCalculated(int width, int height, float angle, float expected)
{
}
}
}

1
src/ImageProcessor/ImageProcessor.csproj

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

43
src/ImageProcessor/Imaging/Rotation.cs

@ -0,0 +1,43 @@
namespace ImageProcessor.Imaging
{
using System;
using System.Drawing;
/// <summary>
/// Provides rotation calculation methods
/// </summary>
internal class Rotation
{
/// <summary>
/// Calculates the new size after rotation.
/// </summary>
/// <param name="width">The width of the image.</param>
/// <param name="height">The height of the image.</param>
/// <param name="angle">The angle of rotation.</param>
/// <returns>The new size of the image</returns>
public static Size NewSizeAfterRotation(int width, int height, float angle)
{
double widthAsDouble = width;
double heightAsDouble = height;
double radians = angle * Math.PI / 180d;
double radiansSin = Math.Sin(radians);
double radiansCos = Math.Cos(radians);
double width1 = (heightAsDouble * radiansSin) + (widthAsDouble * radiansCos);
double height1 = (widthAsDouble * radiansSin) + (heightAsDouble * radiansCos);
// Find dimensions in the other direction
radiansSin = Math.Sin(-radians);
radiansCos = Math.Cos(-radians);
double width2 = (heightAsDouble * radiansSin) + (widthAsDouble * radiansCos);
double height2 = (widthAsDouble * radiansSin) + (heightAsDouble * radiansCos);
// Get the external vertex for the rotation
Size result = new Size();
result.Width = Convert.ToInt32(Math.Max(Math.Abs(width1), Math.Abs(width2)));
result.Height = Convert.ToInt32(Math.Max(Math.Abs(height1), Math.Abs(height2)));
return result;
}
}
}

25
src/ImageProcessor/Processors/Rotate.cs

@ -104,30 +104,13 @@ namespace ImageProcessor.Processors
/// </remarks>
private Bitmap RotateImage(Image image, float rotateAtX, float rotateAtY, float angle)
{
double widthAsDouble = image.Width;
double heightAsDouble = image.Height;
Size newSize = Imaging.Rotation.NewSizeAfterRotation(image.Width, image.Height, angle);
double radians = angle * Math.PI / 180d;
double radiansSin = Math.Sin(radians);
double radiansCos = Math.Cos(radians);
double width1 = (heightAsDouble * radiansSin) + (widthAsDouble * radiansCos);
double height1 = (widthAsDouble * radiansSin) + (heightAsDouble * radiansCos);
// Find dimensions in the other direction
radiansSin = Math.Sin(-radians);
radiansCos = Math.Cos(-radians);
double width2 = (heightAsDouble * radiansSin) + (widthAsDouble * radiansCos);
double height2 = (widthAsDouble * radiansSin) + (heightAsDouble * radiansCos);
// Get the external vertex for the rotation
int width = Convert.ToInt32(Math.Max(Math.Abs(width1), Math.Abs(width2)));
int height = Convert.ToInt32(Math.Max(Math.Abs(height1), Math.Abs(height2)));
int x = (width - image.Width) / 2;
int y = (height - image.Height) / 2;
int x = (newSize.Width - image.Width) / 2;
int y = (newSize.Height - image.Height) / 2;
// Create a new empty bitmap to hold rotated image
Bitmap newImage = new Bitmap(width, height);
Bitmap newImage = new Bitmap(newSize.Width, newSize.Height);
newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
// Make a graphics object from the empty bitmap

88
src/ImageProcessor/Processors/RotateInside.cs

@ -13,6 +13,8 @@ namespace ImageProcessor.Processors
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using ImageProcessor.Common.Exceptions;
/// <summary>
/// Encapsulates the methods to rotate the inside of an image
@ -37,10 +39,92 @@ namespace ImageProcessor.Processors
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class.
/// </returns>
/// <remarks>
/// Based on <see href="http://math.stackexchange.com/questions/1070853/"/>
/// </remarks>
public Image ProcessImage(ImageFactory factory)
{
// TODO: Implement this method
throw new NotImplementedException();
Bitmap newImage = null;
Image image = factory.Image;
try
{
float angle = this.DynamicParameter;
// Center of the image
float rotateAtX = Math.Abs(image.Width / 2);
float rotateAtY = Math.Abs(image.Height / 2);
// Create a rotated image.
newImage = this.RotateImage(image, rotateAtX, rotateAtY, angle);
image.Dispose();
image = newImage;
}
catch (Exception ex)
{
if (newImage != null)
{
newImage.Dispose();
}
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
return image;
}
private Bitmap RotateImage(Image image, float rotateAtX, float rotateAtY, float angle)
{
double widthAsDouble = image.Width;
double heightAsDouble = image.Height;
double radians = angle * Math.PI / 180d;
double radiansSin = Math.Sin(radians);
double radiansCos = Math.Cos(radians);
double width1 = (heightAsDouble * radiansSin) + (widthAsDouble * radiansCos);
double height1 = (widthAsDouble * radiansSin) + (heightAsDouble * radiansCos);
// Find dimensions in the other direction
radiansSin = Math.Sin(-radians);
radiansCos = Math.Cos(-radians);
double width2 = (heightAsDouble * radiansSin) + (widthAsDouble * radiansCos);
double height2 = (widthAsDouble * radiansSin) + (heightAsDouble * radiansCos);
// Get the external vertex for the rotation
int width = Convert.ToInt32(Math.Max(Math.Abs(width1), Math.Abs(width2)));
int height = Convert.ToInt32(Math.Max(Math.Abs(height1), Math.Abs(height2)));
int x = (width - image.Width) / 2;
int y = (height - image.Height) / 2;
// Create a new empty bitmap to hold rotated image
Bitmap newImage = new Bitmap(width, height);
newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
// Make a graphics object from the empty bitmap
using (Graphics graphics = Graphics.FromImage(newImage))
{
// Reduce the jagged edge.
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
// Put the rotation point in the "center" of the image
graphics.TranslateTransform(rotateAtX + x, rotateAtY + y);
// Rotate the image
graphics.RotateTransform(angle);
// Move the image back
graphics.TranslateTransform(-rotateAtX - x, -rotateAtY - y);
// Draw passed in image onto graphics object
graphics.DrawImage(image, new PointF(x, y));
}
return newImage;
}
}
}

3
src/ImageProcessor/Properties/AssemblyInfo.cs

@ -9,6 +9,7 @@
// --------------------------------------------------------------------------------------------------------------------
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
@ -42,3 +43,5 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below:
[assembly: AssemblyVersion("2.2.0.0")]
[assembly: AssemblyFileVersion("2.2.0.0")]
[assembly: InternalsVisibleTo("ImageProcessor.UnitTests")]
Loading…
Cancel
Save