Browse Source

Fixing RotateBounds maths.

Former-commit-id: 8c5aff0a42bb1f05d3a092b638a71cd9c9cad55b
Former-commit-id: 29461154ae78d77b8884fc7ce621ce346079a4ba
af/merge-core
James South 11 years ago
parent
commit
a410a919ad
  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 mask = Image.FromFile(Path.Combine(resolvedPath, "mask.png"));
// Image overlay = Image.FromFile(Path.Combine(resolvedPath, "imageprocessor.128.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, "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, "mountain.jpg"));
//FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "Arc-de-Triomphe-France.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")); //FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "Martin-Schoeller-Jack-Nicholson-Portrait.jpeg"));
@ -110,19 +110,19 @@ namespace ImageProcessor.PlayGround
//.DetectEdges(new LaplacianOfGaussianEdgeFilter()) //.DetectEdges(new LaplacianOfGaussianEdgeFilter())
//.EntropyCrop() //.EntropyCrop()
//.Halftone(true) //.Halftone(true)
.RotateBounded(5, false) .RotateBounded(150, false)
//.Rotate(140)
//.Filter(MatrixFilters.Invert) //.Filter(MatrixFilters.Invert)
//.Contrast(50) //.Contrast(50)
//.Filter(MatrixFilters.Comic) //.Filter(MatrixFilters.Comic)
//.Flip() //.Flip()
//.Filter(MatrixFilters.HiSatch) //.Filter(MatrixFilters.HiSatch)
//.Pixelate(8) //.Pixelate(8)
//.Rotate(45)
//.GaussianSharpen(10) //.GaussianSharpen(10)
//.Format(new PngFormat() { IsIndexed = true }) //.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", 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(); 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> /// <returns>The new size of the image</returns>
public static Rectangle GetBoundingRotatedRectangle(int width, int height, float angleInDegrees) public static Rectangle GetBoundingRotatedRectangle(int width, int height, float angleInDegrees)
{ {
// Check first clockwise.
double radians = DegreesToRadians(angleInDegrees); double radians = DegreesToRadians(angleInDegrees);
double radiansSin = Math.Sin(radians); double radiansSin = Math.Sin(radians);
double radiansCos = Math.Cos(radians); double radiansCos = Math.Cos(radians);
@ -316,14 +317,8 @@ namespace ImageProcessor.Imaging.Helpers
/// <returns>The zoom needed</returns> /// <returns>The zoom needed</returns>
public static float ZoomAfterRotation(int imageWidth, int imageHeight, float angleInDegrees) public static float ZoomAfterRotation(int imageWidth, int imageHeight, float angleInDegrees)
{ {
double radians = Math.Abs(DegreesToRadians(angleInDegrees)); Rectangle rectangle = GetBoundingRotatedRectangle(imageWidth, imageHeight, angleInDegrees);
double radiansSin = Math.Sin(radians); return Math.Max((float)rectangle.Width / imageWidth, (float)rectangle.Height / imageHeight);
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);
} }
} }
} }

27
src/ImageProcessor/Processors/RotateBounded.cs

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

Loading…
Cancel
Save