Skip to content

Commit 2e3f3c1

Browse files
Use doubles in ResizeHelper
1 parent 801e026 commit 2e3f3c1

File tree

1 file changed

+47
-47
lines changed

1 file changed

+47
-47
lines changed

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

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public static (Size, Rectangle) CalculateTargetLocationAndBounds(Size sourceSize
4747
const int Min = 1;
4848
if (width == 0 && height > 0)
4949
{
50-
width = (int)MathF.Max(Min, MathF.Round(sourceSize.Width * height / (float)sourceSize.Height));
50+
width = (int)Math.Max(Min, Math.Round(sourceSize.Width * height / (double)sourceSize.Height));
5151
}
5252

5353
if (height == 0 && width > 0)
5454
{
55-
height = (int)MathF.Max(Min, MathF.Round(sourceSize.Height * width / (float)sourceSize.Width));
55+
height = (int)Math.Max(Min, Math.Round(sourceSize.Height * width / (double)sourceSize.Width));
5656
}
5757

5858
switch (options.Mode)
@@ -86,11 +86,11 @@ private static (Size, Rectangle) CalculateBoxPadRectangle(
8686
int sourceHeight = source.Height;
8787

8888
// Fractional variants for preserving aspect ratio.
89-
float percentHeight = MathF.Abs(height / (float)sourceHeight);
90-
float percentWidth = MathF.Abs(width / (float)sourceWidth);
89+
double percentHeight = Math.Abs(height / (double)sourceHeight);
90+
double percentWidth = Math.Abs(width / (double)sourceWidth);
9191

92-
int boxPadHeight = height > 0 ? height : (int)MathF.Round(sourceHeight * percentWidth);
93-
int boxPadWidth = width > 0 ? width : (int)MathF.Round(sourceWidth * percentHeight);
92+
int boxPadHeight = height > 0 ? height : (int)Math.Round(sourceHeight * percentWidth);
93+
int boxPadWidth = width > 0 ? width : (int)Math.Round(sourceWidth * percentHeight);
9494

9595
// Only calculate if upscaling.
9696
if (sourceWidth < boxPadWidth && sourceHeight < boxPadHeight)
@@ -156,7 +156,7 @@ private static (Size, Rectangle) CalculateCropRectangle(
156156
int width,
157157
int height)
158158
{
159-
float ratio;
159+
double ratio;
160160
int sourceWidth = source.Width;
161161
int sourceHeight = source.Height;
162162

@@ -166,26 +166,26 @@ private static (Size, Rectangle) CalculateCropRectangle(
166166
int targetHeight = height;
167167

168168
// Fractional variants for preserving aspect ratio.
169-
float percentHeight = MathF.Abs(height / (float)sourceHeight);
170-
float percentWidth = MathF.Abs(width / (float)sourceWidth);
169+
double percentHeight = Math.Abs(height / (double)sourceHeight);
170+
double percentWidth = Math.Abs(width / (double)sourceWidth);
171171

172172
if (percentHeight < percentWidth)
173173
{
174174
ratio = percentWidth;
175175

176176
if (options.CenterCoordinates.HasValue)
177177
{
178-
float center = -(ratio * sourceHeight) * options.CenterCoordinates.Value.Y;
179-
targetY = (int)MathF.Round(center + (height / 2F));
178+
double center = -(ratio * sourceHeight) * options.CenterCoordinates.Value.Y;
179+
targetY = (int)Math.Round(center + (height / 2F));
180180

181181
if (targetY > 0)
182182
{
183183
targetY = 0;
184184
}
185185

186-
if (targetY < (int)MathF.Round(height - (sourceHeight * ratio)))
186+
if (targetY < (int)Math.Round(height - (sourceHeight * ratio)))
187187
{
188-
targetY = (int)MathF.Round(height - (sourceHeight * ratio));
188+
targetY = (int)Math.Round(height - (sourceHeight * ratio));
189189
}
190190
}
191191
else
@@ -200,33 +200,33 @@ private static (Size, Rectangle) CalculateCropRectangle(
200200
case AnchorPositionMode.Bottom:
201201
case AnchorPositionMode.BottomLeft:
202202
case AnchorPositionMode.BottomRight:
203-
targetY = (int)MathF.Round(height - (sourceHeight * ratio));
203+
targetY = (int)Math.Round(height - (sourceHeight * ratio));
204204
break;
205205
default:
206-
targetY = (int)MathF.Round((height - (sourceHeight * ratio)) / 2F);
206+
targetY = (int)Math.Round((height - (sourceHeight * ratio)) / 2F);
207207
break;
208208
}
209209
}
210210

211-
targetHeight = (int)MathF.Ceiling(sourceHeight * percentWidth);
211+
targetHeight = (int)Math.Ceiling(sourceHeight * percentWidth);
212212
}
213213
else
214214
{
215215
ratio = percentHeight;
216216

217217
if (options.CenterCoordinates.HasValue)
218218
{
219-
float center = -(ratio * sourceWidth) * options.CenterCoordinates.Value.X;
220-
targetX = (int)MathF.Round(center + (width / 2F));
219+
double center = -(ratio * sourceWidth) * options.CenterCoordinates.Value.X;
220+
targetX = (int)Math.Round(center + (width / 2F));
221221

222222
if (targetX > 0)
223223
{
224224
targetX = 0;
225225
}
226226

227-
if (targetX < (int)MathF.Round(width - (sourceWidth * ratio)))
227+
if (targetX < (int)Math.Round(width - (sourceWidth * ratio)))
228228
{
229-
targetX = (int)MathF.Round(width - (sourceWidth * ratio));
229+
targetX = (int)Math.Round(width - (sourceWidth * ratio));
230230
}
231231
}
232232
else
@@ -241,15 +241,15 @@ private static (Size, Rectangle) CalculateCropRectangle(
241241
case AnchorPositionMode.Right:
242242
case AnchorPositionMode.TopRight:
243243
case AnchorPositionMode.BottomRight:
244-
targetX = (int)MathF.Round(width - (sourceWidth * ratio));
244+
targetX = (int)Math.Round(width - (sourceWidth * ratio));
245245
break;
246246
default:
247-
targetX = (int)MathF.Round((width - (sourceWidth * ratio)) / 2F);
247+
targetX = (int)Math.Round((width - (sourceWidth * ratio)) / 2F);
248248
break;
249249
}
250250
}
251251

252-
targetWidth = (int)MathF.Ceiling(sourceWidth * percentHeight);
252+
targetWidth = (int)Math.Ceiling(sourceWidth * percentHeight);
253253
}
254254

255255
// Target image width and height can be different to the rectangle width and height.
@@ -265,20 +265,20 @@ private static (Size, Rectangle) CalculateMaxRectangle(
265265
int targetHeight = height;
266266

267267
// Fractional variants for preserving aspect ratio.
268-
float percentHeight = MathF.Abs(height / (float)source.Height);
269-
float percentWidth = MathF.Abs(width / (float)source.Width);
268+
double percentHeight = Math.Abs(height / (double)source.Height);
269+
double percentWidth = Math.Abs(width / (double)source.Width);
270270

271-
// Integers must be cast to floats to get needed precision
272-
float ratio = height / (float)width;
273-
float sourceRatio = source.Height / (float)source.Width;
271+
// Integers must be cast to doubles to get needed precision
272+
double ratio = height / (double)width;
273+
double sourceRatio = source.Height / (double)source.Width;
274274

275275
if (sourceRatio < ratio)
276276
{
277-
targetHeight = (int)MathF.Round(source.Height * percentWidth);
277+
targetHeight = (int)Math.Round(source.Height * percentWidth);
278278
}
279279
else
280280
{
281-
targetWidth = (int)MathF.Round(source.Width * percentHeight);
281+
targetWidth = (int)Math.Round(source.Width * percentHeight);
282282
}
283283

284284
// Replace the size to match the rectangle.
@@ -307,25 +307,25 @@ private static (Size, Rectangle) CalculateMinRectangle(
307307

308308
if (widthDiff < heightDiff)
309309
{
310-
float sourceRatio = (float)sourceHeight / sourceWidth;
311-
targetHeight = (int)MathF.Round(width * sourceRatio);
310+
double sourceRatio = (double)sourceHeight / sourceWidth;
311+
targetHeight = (int)Math.Round(width * sourceRatio);
312312
}
313313
else if (widthDiff > heightDiff)
314314
{
315-
float sourceRatioInverse = (float)sourceWidth / sourceHeight;
316-
targetWidth = (int)MathF.Round(height * sourceRatioInverse);
315+
double sourceRatioInverse = (double)sourceWidth / sourceHeight;
316+
targetWidth = (int)Math.Round(height * sourceRatioInverse);
317317
}
318318
else
319319
{
320320
if (height > width)
321321
{
322-
float percentWidth = MathF.Abs(width / (float)sourceWidth);
323-
targetHeight = (int)MathF.Round(sourceHeight * percentWidth);
322+
double percentWidth = Math.Abs(width / (double)sourceWidth);
323+
targetHeight = (int)Math.Round(sourceHeight * percentWidth);
324324
}
325325
else
326326
{
327-
float percentHeight = MathF.Abs(height / (float)sourceHeight);
328-
targetWidth = (int)MathF.Round(sourceWidth * percentHeight);
327+
double percentHeight = Math.Abs(height / (double)sourceHeight);
328+
targetWidth = (int)Math.Round(sourceWidth * percentHeight);
329329
}
330330
}
331331

@@ -339,7 +339,7 @@ private static (Size, Rectangle) CalculatePadRectangle(
339339
int width,
340340
int height)
341341
{
342-
float ratio;
342+
double ratio;
343343
int sourceWidth = sourceSize.Width;
344344
int sourceHeight = sourceSize.Height;
345345

@@ -349,13 +349,13 @@ private static (Size, Rectangle) CalculatePadRectangle(
349349
int targetHeight = height;
350350

351351
// Fractional variants for preserving aspect ratio.
352-
float percentHeight = MathF.Abs(height / (float)sourceHeight);
353-
float percentWidth = MathF.Abs(width / (float)sourceWidth);
352+
double percentHeight = Math.Abs(height / (double)sourceHeight);
353+
double percentWidth = Math.Abs(width / (double)sourceWidth);
354354

355355
if (percentHeight < percentWidth)
356356
{
357357
ratio = percentHeight;
358-
targetWidth = (int)MathF.Round(sourceWidth * percentHeight);
358+
targetWidth = (int)Math.Round(sourceWidth * percentHeight);
359359

360360
switch (options.Position)
361361
{
@@ -367,17 +367,17 @@ private static (Size, Rectangle) CalculatePadRectangle(
367367
case AnchorPositionMode.Right:
368368
case AnchorPositionMode.TopRight:
369369
case AnchorPositionMode.BottomRight:
370-
targetX = (int)MathF.Round(width - (sourceWidth * ratio));
370+
targetX = (int)Math.Round(width - (sourceWidth * ratio));
371371
break;
372372
default:
373-
targetX = (int)MathF.Round((width - (sourceWidth * ratio)) / 2F);
373+
targetX = (int)Math.Round((width - (sourceWidth * ratio)) / 2F);
374374
break;
375375
}
376376
}
377377
else
378378
{
379379
ratio = percentWidth;
380-
targetHeight = (int)MathF.Round(sourceHeight * percentWidth);
380+
targetHeight = (int)Math.Round(sourceHeight * percentWidth);
381381

382382
switch (options.Position)
383383
{
@@ -389,10 +389,10 @@ private static (Size, Rectangle) CalculatePadRectangle(
389389
case AnchorPositionMode.Bottom:
390390
case AnchorPositionMode.BottomLeft:
391391
case AnchorPositionMode.BottomRight:
392-
targetY = (int)MathF.Round(height - (sourceHeight * ratio));
392+
targetY = (int)Math.Round(height - (sourceHeight * ratio));
393393
break;
394394
default:
395-
targetY = (int)MathF.Round((height - (sourceHeight * ratio)) / 2F);
395+
targetY = (int)Math.Round((height - (sourceHeight * ratio)) / 2F);
396396
break;
397397
}
398398
}

0 commit comments

Comments
 (0)