Browse Source

Fix #233

af/merge-core
James Jackson-South 9 years ago
parent
commit
b6c8cb2e3d
  1. 77
      src/ImageSharp/Processing/Transforms/Options/ResizeHelper.cs
  2. 8
      src/ImageSharp/Processing/Transforms/Resize.cs

77
src/ImageSharp/Processing/Transforms/Options/ResizeHelper.cs

@ -5,7 +5,6 @@
namespace ImageSharp.Processing namespace ImageSharp.Processing
{ {
using System;
using System.Linq; using System.Linq;
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
@ -86,17 +85,17 @@ namespace ImageSharp.Processing
if (options.CenterCoordinates.Any()) if (options.CenterCoordinates.Any())
{ {
float center = -(ratio * sourceHeight) * options.CenterCoordinates.First(); float center = -(ratio * sourceHeight) * options.CenterCoordinates.ToArray()[1];
destinationY = (int)center + (height / 2); destinationY = (int)MathF.Round(center + (height / 2F));
if (destinationY > 0) if (destinationY > 0)
{ {
destinationY = 0; destinationY = 0;
} }
if (destinationY < (int)(height - (sourceHeight * ratio))) if (destinationY < (int)MathF.Round(height - (sourceHeight * ratio)))
{ {
destinationY = (int)(height - (sourceHeight * ratio)); destinationY = (int)MathF.Round(height - (sourceHeight * ratio));
} }
} }
else else
@ -111,10 +110,10 @@ namespace ImageSharp.Processing
case AnchorPosition.Bottom: case AnchorPosition.Bottom:
case AnchorPosition.BottomLeft: case AnchorPosition.BottomLeft:
case AnchorPosition.BottomRight: case AnchorPosition.BottomRight:
destinationY = (int)(height - (sourceHeight * ratio)); destinationY = (int)MathF.Round(height - (sourceHeight * ratio));
break; break;
default: default:
destinationY = (int)((height - (sourceHeight * ratio)) / 2); destinationY = (int)MathF.Round((height - (sourceHeight * ratio)) / 2F);
break; break;
} }
} }
@ -127,17 +126,17 @@ namespace ImageSharp.Processing
if (options.CenterCoordinates.Any()) if (options.CenterCoordinates.Any())
{ {
float center = -(ratio * sourceWidth) * options.CenterCoordinates.ToArray()[1]; float center = -(ratio * sourceWidth) * options.CenterCoordinates.First();
destinationX = (int)center + (width / 2); destinationX = (int)MathF.Round(center + (width / 2F));
if (destinationX > 0) if (destinationX > 0)
{ {
destinationX = 0; destinationX = 0;
} }
if (destinationX < (int)(width - (sourceWidth * ratio))) if (destinationX < (int)MathF.Round(width - (sourceWidth * ratio)))
{ {
destinationX = (int)(width - (sourceWidth * ratio)); destinationX = (int)MathF.Round(width - (sourceWidth * ratio));
} }
} }
else else
@ -152,10 +151,10 @@ namespace ImageSharp.Processing
case AnchorPosition.Right: case AnchorPosition.Right:
case AnchorPosition.TopRight: case AnchorPosition.TopRight:
case AnchorPosition.BottomRight: case AnchorPosition.BottomRight:
destinationX = (int)(width - (sourceWidth * ratio)); destinationX = (int)MathF.Round(width - (sourceWidth * ratio));
break; break;
default: default:
destinationX = (int)((width - (sourceWidth * ratio)) / 2); destinationX = (int)MathF.Round((width - (sourceWidth * ratio)) / 2F);
break; break;
} }
} }
@ -202,7 +201,7 @@ namespace ImageSharp.Processing
if (percentHeight < percentWidth) if (percentHeight < percentWidth)
{ {
ratio = percentHeight; ratio = percentHeight;
destinationWidth = Convert.ToInt32(sourceWidth * percentHeight); destinationWidth = (int)MathF.Round(sourceWidth * percentHeight);
switch (options.Position) switch (options.Position)
{ {
@ -214,17 +213,17 @@ namespace ImageSharp.Processing
case AnchorPosition.Right: case AnchorPosition.Right:
case AnchorPosition.TopRight: case AnchorPosition.TopRight:
case AnchorPosition.BottomRight: case AnchorPosition.BottomRight:
destinationX = (int)(width - (sourceWidth * ratio)); destinationX = (int)MathF.Round(width - (sourceWidth * ratio));
break; break;
default: default:
destinationX = Convert.ToInt32((width - (sourceWidth * ratio)) / 2); destinationX = (int)MathF.Round((width - (sourceWidth * ratio)) / 2F);
break; break;
} }
} }
else else
{ {
ratio = percentWidth; ratio = percentWidth;
destinationHeight = Convert.ToInt32(sourceHeight * percentWidth); destinationHeight = (int)MathF.Round(sourceHeight * percentWidth);
switch (options.Position) switch (options.Position)
{ {
@ -236,10 +235,10 @@ namespace ImageSharp.Processing
case AnchorPosition.Bottom: case AnchorPosition.Bottom:
case AnchorPosition.BottomLeft: case AnchorPosition.BottomLeft:
case AnchorPosition.BottomRight: case AnchorPosition.BottomRight:
destinationY = (int)(height - (sourceHeight * ratio)); destinationY = (int)MathF.Round(height - (sourceHeight * ratio));
break; break;
default: default:
destinationY = (int)((height - (sourceHeight * ratio)) / 2); destinationY = (int)MathF.Round((height - (sourceHeight * ratio)) / 2F);
break; break;
} }
} }
@ -274,8 +273,8 @@ namespace ImageSharp.Processing
float percentHeight = MathF.Abs(height / (float)sourceHeight); float percentHeight = MathF.Abs(height / (float)sourceHeight);
float percentWidth = MathF.Abs(width / (float)sourceWidth); float percentWidth = MathF.Abs(width / (float)sourceWidth);
int boxPadHeight = height > 0 ? height : Convert.ToInt32(sourceHeight * percentWidth); int boxPadHeight = height > 0 ? height : (int)MathF.Round(sourceHeight * percentWidth);
int boxPadWidth = width > 0 ? width : Convert.ToInt32(sourceWidth * percentHeight); int boxPadWidth = width > 0 ? width : (int)MathF.Round(sourceWidth * percentHeight);
// Only calculate if upscaling. // Only calculate if upscaling.
if (sourceWidth < boxPadWidth && sourceHeight < boxPadHeight) if (sourceWidth < boxPadWidth && sourceHeight < boxPadHeight)
@ -356,17 +355,17 @@ namespace ImageSharp.Processing
float percentWidth = MathF.Abs(width / (float)source.Width); float percentWidth = MathF.Abs(width / (float)source.Width);
// Integers must be cast to floats to get needed precision // Integers must be cast to floats to get needed precision
float ratio = (float)options.Size.Height / options.Size.Width; float ratio = options.Size.Height / (float)options.Size.Width;
float sourceRatio = (float)source.Height / source.Width; float sourceRatio = source.Height / (float)source.Width;
if (sourceRatio < ratio) if (sourceRatio < ratio)
{ {
destinationHeight = Convert.ToInt32(source.Height * percentWidth); destinationHeight = (int)MathF.Round(source.Height * percentWidth);
height = destinationHeight; height = destinationHeight;
} }
else else
{ {
destinationWidth = Convert.ToInt32(source.Width * percentHeight); destinationWidth = (int)MathF.Round(source.Width * percentHeight);
width = destinationWidth; width = destinationWidth;
} }
@ -389,35 +388,37 @@ namespace ImageSharp.Processing
{ {
int width = options.Size.Width; int width = options.Size.Width;
int height = options.Size.Height; int height = options.Size.Height;
int sourceWidth = source.Width;
int sourceHeight = source.Height;
int destinationWidth; int destinationWidth;
int destinationHeight; int destinationHeight;
// Don't upscale // Don't upscale
if (width > source.Width || height > source.Height) if (width > sourceWidth || height > sourceHeight)
{ {
options.Size = new Size(source.Width, source.Height); options.Size = new Size(sourceWidth, sourceHeight);
return new Rectangle(0, 0, source.Width, source.Height); return new Rectangle(0, 0, sourceWidth, sourceHeight);
} }
// Fractional variants for preserving aspect ratio. // Fractional variants for preserving aspect ratio.
float percentHeight = MathF.Abs(height / (float)source.Height); float percentHeight = MathF.Abs(height / (float)sourceHeight);
float percentWidth = MathF.Abs(width / (float)source.Width); float percentWidth = MathF.Abs(width / (float)sourceWidth);
float sourceRatio = (float)source.Height / source.Width; float sourceRatio = (float)sourceHeight / sourceWidth;
// Find the shortest distance to go. // Find the shortest distance to go.
int widthDiff = source.Width - width; int widthDiff = sourceWidth - width;
int heightDiff = source.Height - height; int heightDiff = sourceHeight - height;
if (widthDiff < heightDiff) if (widthDiff < heightDiff)
{ {
destinationHeight = Convert.ToInt32(width * sourceRatio); destinationHeight = (int)MathF.Round(width * sourceRatio);
height = destinationHeight; height = destinationHeight;
destinationWidth = width; destinationWidth = width;
} }
else if (widthDiff > heightDiff) else if (widthDiff > heightDiff)
{ {
destinationWidth = Convert.ToInt32(height / sourceRatio); destinationWidth = (int)MathF.Round(height / sourceRatio);
destinationHeight = height; destinationHeight = height;
width = destinationWidth; width = destinationWidth;
} }
@ -426,13 +427,13 @@ namespace ImageSharp.Processing
if (height > width) if (height > width)
{ {
destinationWidth = width; destinationWidth = width;
destinationHeight = Convert.ToInt32(source.Height * percentWidth); destinationHeight = (int)MathF.Round(sourceHeight * percentWidth);
height = destinationHeight; height = destinationHeight;
} }
else else
{ {
destinationHeight = height; destinationHeight = height;
destinationWidth = Convert.ToInt32(source.Width * percentHeight); destinationWidth = (int)MathF.Round(sourceWidth * percentHeight);
width = destinationWidth; width = destinationWidth;
} }
} }
@ -442,4 +443,4 @@ namespace ImageSharp.Processing
return new Rectangle(0, 0, destinationWidth, destinationHeight); return new Rectangle(0, 0, destinationWidth, destinationHeight);
} }
} }
} }

8
src/ImageSharp/Processing/Transforms/Resize.cs

@ -29,12 +29,12 @@ namespace ImageSharp
// Ensure size is populated across both dimensions. // Ensure size is populated across both dimensions.
if (options.Size.Width == 0 && options.Size.Height > 0) if (options.Size.Width == 0 && options.Size.Height > 0)
{ {
options.Size = new Size(source.Width * options.Size.Height / source.Height, options.Size.Height); options.Size = new Size((int)MathF.Round(source.Width * options.Size.Height / (float)source.Height), options.Size.Height);
} }
if (options.Size.Height == 0 && options.Size.Width > 0) if (options.Size.Height == 0 && options.Size.Width > 0)
{ {
options.Size = new Size(options.Size.Width, source.Height * options.Size.Width / source.Width); options.Size = new Size(options.Size.Width, (int)MathF.Round(source.Height * options.Size.Width / (float)source.Width));
} }
Rectangle targetRectangle = ResizeHelper.CalculateTargetLocationAndBounds(source, options); Rectangle targetRectangle = ResizeHelper.CalculateTargetLocationAndBounds(source, options);
@ -158,13 +158,13 @@ namespace ImageSharp
{ {
if (width == 0 && height > 0) if (width == 0 && height > 0)
{ {
width = source.Width * height / source.Height; width = (int)MathF.Round(source.Width * height / (float)source.Height);
targetRectangle.Width = width; targetRectangle.Width = width;
} }
if (height == 0 && width > 0) if (height == 0 && width > 0)
{ {
height = source.Height * width / source.Width; height = (int)MathF.Round(source.Height * width / (float)source.Width);
targetRectangle.Height = height; targetRectangle.Height = height;
} }

Loading…
Cancel
Save