Browse Source

Fixing RotateBounds maths.

Former-commit-id: 8c5aff0a42bb1f05d3a092b638a71cd9c9cad55b
Former-commit-id: 29461154ae78d77b8884fc7ce621ce346079a4ba
pull/17/head
James South 11 years ago
parent
commit
fdba47fbdc
  1. 10
      src/ImageProcessor.Playground/Program.cs
  2. 11
      src/ImageProcessor/Imaging/Helpers/ImageMaths.cs
  3. 27
      src/ImageProcessor/Processors/RotateBounded.cs

10
src/ImageProcessor.Playground/Program.cs

@ -52,7 +52,7 @@ namespace ImageProcessor.PlayGround
// Image mask = Image.FromFile(Path.Combine(resolvedPath, "mask.png"));
// Image overlay = Image.FromFile(Path.Combine(resolvedPath, "imageprocessor.128.png"));
//FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "2008.jpg"));
//FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "new-york.jpg"));
//FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "stretched.jpg"));
//FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "mountain.jpg"));
//FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "Arc-de-Triomphe-France.jpg"));
//FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "Martin-Schoeller-Jack-Nicholson-Portrait.jpeg"));
@ -110,19 +110,19 @@ namespace ImageProcessor.PlayGround
//.DetectEdges(new LaplacianOfGaussianEdgeFilter())
//.EntropyCrop()
//.Halftone(true)
.RotateBounded(5, false)
.RotateBounded(150, false)
//.Rotate(140)
//.Filter(MatrixFilters.Invert)
//.Contrast(50)
//.Filter(MatrixFilters.Comic)
//.Flip()
//.Filter(MatrixFilters.HiSatch)
//.Pixelate(8)
//.Rotate(45)
//.GaussianSharpen(10)
//.Format(new PngFormat() { IsIndexed = true })
//.Format(new PngFormat() { IsIndexed = true })
//.Format(new PngFormat() )
.Save(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\output", fileInfo.Name)));
//.Save(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\output", Path.GetFileNameWithoutExtension(fileInfo.Name) + ".png")));
//.Save(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\output", Path.GetFileNameWithoutExtension(fileInfo.Name) + ".png")));
stopwatch.Stop();
}

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

@ -113,6 +113,7 @@ namespace ImageProcessor.Imaging.Helpers
/// <returns>The new size of the image</returns>
public static Rectangle GetBoundingRotatedRectangle(int width, int height, float angleInDegrees)
{
// Check first clockwise.
double radians = DegreesToRadians(angleInDegrees);
double radiansSin = Math.Sin(radians);
double radiansCos = Math.Cos(radians);
@ -316,14 +317,8 @@ namespace ImageProcessor.Imaging.Helpers
/// <returns>The zoom needed</returns>
public static float ZoomAfterRotation(int imageWidth, int imageHeight, float angleInDegrees)
{
double radians = Math.Abs(DegreesToRadians(angleInDegrees));
double radiansSin = Math.Sin(radians);
double radiansCos = Math.Cos(radians);
double widthRotated = (imageWidth * radiansCos) + (imageHeight * radiansSin);
double heightRotated = (imageWidth * radiansSin) + (imageHeight * radiansCos);
return (float)Math.Max(widthRotated / imageWidth, heightRotated / imageHeight);
Rectangle rectangle = GetBoundingRotatedRectangle(imageWidth, imageHeight, angleInDegrees);
return Math.Max((float)rectangle.Width / imageWidth, (float)rectangle.Height / imageHeight);
}
}
}

27
src/ImageProcessor/Processors/RotateBounded.cs

@ -15,6 +15,7 @@ namespace ImageProcessor.Processors
using System.Drawing;
using System.Drawing.Drawing2D;
using ImageProcessor.Common.Exceptions;
using ImageProcessor.Imaging.Helpers;
/// <summary>
/// Encapsulates the methods to rotate an image without expanding the canvas.
@ -98,13 +99,13 @@ namespace ImageProcessor.Processors
{
Size newSize = new Size(image.Width, image.Height);
float zoom = Imaging.Helpers.ImageMaths.ZoomAfterRotation(image.Width, image.Height, angleInDegrees);
float zoom = ImageMaths.ZoomAfterRotation(image.Width, image.Height, angleInDegrees);
// if we don't keep the image dimensions, calculate the new ones
if (!keepSize)
{
newSize.Width = (int)(newSize.Width / zoom);
newSize.Height = (int)(newSize.Height / zoom);
newSize.Width = (int)Math.Floor(newSize.Width / zoom);
newSize.Height = (int)Math.Floor(newSize.Height / zoom);
}
// Center of the image
@ -143,22 +144,22 @@ namespace ImageProcessor.Processors
}
else
{
// Calculate the difference between the center of the original image and the center
// of the new image.
int x = (image.Width - newSize.Width) / 2;
int y = (image.Height - newSize.Height) / 2;
float previousX = rotateAtX;
float previousY = rotateAtY;
// Put the rotation point in the "center" of the old image
graphics.TranslateTransform(rotateAtX - x, rotateAtY - y);
// Calculate the difference between the center of the original image
// and the center of the new image.
rotateAtX = Math.Abs(newImage.Width / 2);
rotateAtY = Math.Abs(newImage.Height / 2);
// Put the rotation point in the "center" of the image
graphics.TranslateTransform(rotateAtX, rotateAtY);
// Rotate the image
graphics.RotateTransform(angleInDegrees);
// Move the image back
graphics.TranslateTransform(-(rotateAtX - x), -(rotateAtY - y));
// Draw passed in image onto graphics object
graphics.DrawImage(image, new PointF(-x, -y));
graphics.DrawImage(image, new PointF(-previousX, -previousY));
}
}

Loading…
Cancel
Save