Browse Source

Revert "Use doubles in ResizeHelper"

This reverts commit 2e3f3c1e39.
js/color-alpha-handling
James Jackson-South 6 years ago
parent
commit
3cc5f81a79
  1. 94
      src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeHelper.cs

94
src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeHelper.cs

@ -47,12 +47,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
const int Min = 1; const int Min = 1;
if (width == 0 && height > 0) if (width == 0 && height > 0)
{ {
width = (int)Math.Max(Min, Math.Round(sourceSize.Width * height / (double)sourceSize.Height)); width = (int)MathF.Max(Min, MathF.Round(sourceSize.Width * height / (float)sourceSize.Height));
} }
if (height == 0 && width > 0) if (height == 0 && width > 0)
{ {
height = (int)Math.Max(Min, Math.Round(sourceSize.Height * width / (double)sourceSize.Width)); height = (int)MathF.Max(Min, MathF.Round(sourceSize.Height * width / (float)sourceSize.Width));
} }
switch (options.Mode) switch (options.Mode)
@ -86,11 +86,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
int sourceHeight = source.Height; int sourceHeight = source.Height;
// Fractional variants for preserving aspect ratio. // Fractional variants for preserving aspect ratio.
double percentHeight = Math.Abs(height / (double)sourceHeight); float percentHeight = MathF.Abs(height / (float)sourceHeight);
double percentWidth = Math.Abs(width / (double)sourceWidth); float percentWidth = MathF.Abs(width / (float)sourceWidth);
int boxPadHeight = height > 0 ? height : (int)Math.Round(sourceHeight * percentWidth); int boxPadHeight = height > 0 ? height : (int)MathF.Round(sourceHeight * percentWidth);
int boxPadWidth = width > 0 ? width : (int)Math.Round(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)
@ -156,7 +156,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
int width, int width,
int height) int height)
{ {
double ratio; float ratio;
int sourceWidth = source.Width; int sourceWidth = source.Width;
int sourceHeight = source.Height; int sourceHeight = source.Height;
@ -166,8 +166,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
int targetHeight = height; int targetHeight = height;
// Fractional variants for preserving aspect ratio. // Fractional variants for preserving aspect ratio.
double percentHeight = Math.Abs(height / (double)sourceHeight); float percentHeight = MathF.Abs(height / (float)sourceHeight);
double percentWidth = Math.Abs(width / (double)sourceWidth); float percentWidth = MathF.Abs(width / (float)sourceWidth);
if (percentHeight < percentWidth) if (percentHeight < percentWidth)
{ {
@ -175,17 +175,17 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
if (options.CenterCoordinates.HasValue) if (options.CenterCoordinates.HasValue)
{ {
double center = -(ratio * sourceHeight) * options.CenterCoordinates.Value.Y; float center = -(ratio * sourceHeight) * options.CenterCoordinates.Value.Y;
targetY = (int)Math.Round(center + (height / 2F)); targetY = (int)MathF.Round(center + (height / 2F));
if (targetY > 0) if (targetY > 0)
{ {
targetY = 0; targetY = 0;
} }
if (targetY < (int)Math.Round(height - (sourceHeight * ratio))) if (targetY < (int)MathF.Round(height - (sourceHeight * ratio)))
{ {
targetY = (int)Math.Round(height - (sourceHeight * ratio)); targetY = (int)MathF.Round(height - (sourceHeight * ratio));
} }
} }
else else
@ -200,15 +200,15 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
case AnchorPositionMode.Bottom: case AnchorPositionMode.Bottom:
case AnchorPositionMode.BottomLeft: case AnchorPositionMode.BottomLeft:
case AnchorPositionMode.BottomRight: case AnchorPositionMode.BottomRight:
targetY = (int)Math.Round(height - (sourceHeight * ratio)); targetY = (int)MathF.Round(height - (sourceHeight * ratio));
break; break;
default: default:
targetY = (int)Math.Round((height - (sourceHeight * ratio)) / 2F); targetY = (int)MathF.Round((height - (sourceHeight * ratio)) / 2F);
break; break;
} }
} }
targetHeight = (int)Math.Ceiling(sourceHeight * percentWidth); targetHeight = (int)MathF.Ceiling(sourceHeight * percentWidth);
} }
else else
{ {
@ -216,17 +216,17 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
if (options.CenterCoordinates.HasValue) if (options.CenterCoordinates.HasValue)
{ {
double center = -(ratio * sourceWidth) * options.CenterCoordinates.Value.X; float center = -(ratio * sourceWidth) * options.CenterCoordinates.Value.X;
targetX = (int)Math.Round(center + (width / 2F)); targetX = (int)MathF.Round(center + (width / 2F));
if (targetX > 0) if (targetX > 0)
{ {
targetX = 0; targetX = 0;
} }
if (targetX < (int)Math.Round(width - (sourceWidth * ratio))) if (targetX < (int)MathF.Round(width - (sourceWidth * ratio)))
{ {
targetX = (int)Math.Round(width - (sourceWidth * ratio)); targetX = (int)MathF.Round(width - (sourceWidth * ratio));
} }
} }
else else
@ -241,15 +241,15 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
case AnchorPositionMode.Right: case AnchorPositionMode.Right:
case AnchorPositionMode.TopRight: case AnchorPositionMode.TopRight:
case AnchorPositionMode.BottomRight: case AnchorPositionMode.BottomRight:
targetX = (int)Math.Round(width - (sourceWidth * ratio)); targetX = (int)MathF.Round(width - (sourceWidth * ratio));
break; break;
default: default:
targetX = (int)Math.Round((width - (sourceWidth * ratio)) / 2F); targetX = (int)MathF.Round((width - (sourceWidth * ratio)) / 2F);
break; break;
} }
} }
targetWidth = (int)Math.Ceiling(sourceWidth * percentHeight); targetWidth = (int)MathF.Ceiling(sourceWidth * percentHeight);
} }
// Target image width and height can be different to the rectangle width and height. // Target image width and height can be different to the rectangle width and height.
@ -265,20 +265,20 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
int targetHeight = height; int targetHeight = height;
// Fractional variants for preserving aspect ratio. // Fractional variants for preserving aspect ratio.
double percentHeight = Math.Abs(height / (double)source.Height); float percentHeight = MathF.Abs(height / (float)source.Height);
double percentWidth = Math.Abs(width / (double)source.Width); float percentWidth = MathF.Abs(width / (float)source.Width);
// Integers must be cast to doubles to get needed precision // Integers must be cast to floats to get needed precision
double ratio = height / (double)width; float ratio = height / (float)width;
double sourceRatio = source.Height / (double)source.Width; float sourceRatio = source.Height / (float)source.Width;
if (sourceRatio < ratio) if (sourceRatio < ratio)
{ {
targetHeight = (int)Math.Round(source.Height * percentWidth); targetHeight = (int)MathF.Round(source.Height * percentWidth);
} }
else else
{ {
targetWidth = (int)Math.Round(source.Width * percentHeight); targetWidth = (int)MathF.Round(source.Width * percentHeight);
} }
// Replace the size to match the rectangle. // Replace the size to match the rectangle.
@ -307,25 +307,25 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
if (widthDiff < heightDiff) if (widthDiff < heightDiff)
{ {
double sourceRatio = (double)sourceHeight / sourceWidth; float sourceRatio = (float)sourceHeight / sourceWidth;
targetHeight = (int)Math.Round(width * sourceRatio); targetHeight = (int)MathF.Round(width * sourceRatio);
} }
else if (widthDiff > heightDiff) else if (widthDiff > heightDiff)
{ {
double sourceRatioInverse = (double)sourceWidth / sourceHeight; float sourceRatioInverse = (float)sourceWidth / sourceHeight;
targetWidth = (int)Math.Round(height * sourceRatioInverse); targetWidth = (int)MathF.Round(height * sourceRatioInverse);
} }
else else
{ {
if (height > width) if (height > width)
{ {
double percentWidth = Math.Abs(width / (double)sourceWidth); float percentWidth = MathF.Abs(width / (float)sourceWidth);
targetHeight = (int)Math.Round(sourceHeight * percentWidth); targetHeight = (int)MathF.Round(sourceHeight * percentWidth);
} }
else else
{ {
double percentHeight = Math.Abs(height / (double)sourceHeight); float percentHeight = MathF.Abs(height / (float)sourceHeight);
targetWidth = (int)Math.Round(sourceWidth * percentHeight); targetWidth = (int)MathF.Round(sourceWidth * percentHeight);
} }
} }
@ -339,7 +339,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
int width, int width,
int height) int height)
{ {
double ratio; float ratio;
int sourceWidth = sourceSize.Width; int sourceWidth = sourceSize.Width;
int sourceHeight = sourceSize.Height; int sourceHeight = sourceSize.Height;
@ -349,13 +349,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
int targetHeight = height; int targetHeight = height;
// Fractional variants for preserving aspect ratio. // Fractional variants for preserving aspect ratio.
double percentHeight = Math.Abs(height / (double)sourceHeight); float percentHeight = MathF.Abs(height / (float)sourceHeight);
double percentWidth = Math.Abs(width / (double)sourceWidth); float percentWidth = MathF.Abs(width / (float)sourceWidth);
if (percentHeight < percentWidth) if (percentHeight < percentWidth)
{ {
ratio = percentHeight; ratio = percentHeight;
targetWidth = (int)Math.Round(sourceWidth * percentHeight); targetWidth = (int)MathF.Round(sourceWidth * percentHeight);
switch (options.Position) switch (options.Position)
{ {
@ -367,17 +367,17 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
case AnchorPositionMode.Right: case AnchorPositionMode.Right:
case AnchorPositionMode.TopRight: case AnchorPositionMode.TopRight:
case AnchorPositionMode.BottomRight: case AnchorPositionMode.BottomRight:
targetX = (int)Math.Round(width - (sourceWidth * ratio)); targetX = (int)MathF.Round(width - (sourceWidth * ratio));
break; break;
default: default:
targetX = (int)Math.Round((width - (sourceWidth * ratio)) / 2F); targetX = (int)MathF.Round((width - (sourceWidth * ratio)) / 2F);
break; break;
} }
} }
else else
{ {
ratio = percentWidth; ratio = percentWidth;
targetHeight = (int)Math.Round(sourceHeight * percentWidth); targetHeight = (int)MathF.Round(sourceHeight * percentWidth);
switch (options.Position) switch (options.Position)
{ {
@ -389,10 +389,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
case AnchorPositionMode.Bottom: case AnchorPositionMode.Bottom:
case AnchorPositionMode.BottomLeft: case AnchorPositionMode.BottomLeft:
case AnchorPositionMode.BottomRight: case AnchorPositionMode.BottomRight:
targetY = (int)Math.Round(height - (sourceHeight * ratio)); targetY = (int)MathF.Round(height - (sourceHeight * ratio));
break; break;
default: default:
targetY = (int)Math.Round((height - (sourceHeight * ratio)) / 2F); targetY = (int)MathF.Round((height - (sourceHeight * ratio)) / 2F);
break; break;
} }
} }

Loading…
Cancel
Save