aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/Drawing/DrawingUtils.cs
diff options
context:
space:
mode:
authorWWWesten <4700006+WWWesten@users.noreply.github.com>2021-11-01 23:43:29 +0500
committerGitHub <noreply@github.com>2021-11-01 23:43:29 +0500
commit0a14279e2a21bcb9654a06a2d49e1e4f0cc5329c (patch)
treee1b1bd603b011ca98e5793e356326bf4a35a7050 /MediaBrowser.Model/Drawing/DrawingUtils.cs
parentf2817fef743eeb75a00782ceea363b2d3e7dc9f2 (diff)
parent76eeb8f655424d295e73ced8349c6fefee6ddb12 (diff)
Merge branch 'jellyfin:master' into master
Diffstat (limited to 'MediaBrowser.Model/Drawing/DrawingUtils.cs')
-rw-r--r--MediaBrowser.Model/Drawing/DrawingUtils.cs67
1 files changed, 57 insertions, 10 deletions
diff --git a/MediaBrowser.Model/Drawing/DrawingUtils.cs b/MediaBrowser.Model/Drawing/DrawingUtils.cs
index 9fe85512f..556792768 100644
--- a/MediaBrowser.Model/Drawing/DrawingUtils.cs
+++ b/MediaBrowser.Model/Drawing/DrawingUtils.cs
@@ -3,20 +3,21 @@ using System;
namespace MediaBrowser.Model.Drawing
{
/// <summary>
- /// Class DrawingUtils
+ /// Class DrawingUtils.
/// </summary>
public static class DrawingUtils
{
/// <summary>
- /// Resizes a set of dimensions
+ /// Resizes a set of dimensions.
/// </summary>
- /// <param name="size">The original size object</param>
- /// <param name="width">A new fixed width, if desired</param>
- /// <param name="height">A new fixed height, if desired</param>
- /// <param name="maxWidth">A max fixed width, if desired</param>
- /// <param name="maxHeight">A max fixed height, if desired</param>
- /// <returns>A new size object</returns>
- public static ImageDimensions Resize(ImageDimensions size,
+ /// <param name="size">The original size object.</param>
+ /// <param name="width">A new fixed width, if desired.</param>
+ /// <param name="height">A new fixed height, if desired.</param>
+ /// <param name="maxWidth">A max fixed width, if desired.</param>
+ /// <param name="maxHeight">A max fixed height, if desired.</param>
+ /// <returns>A new size object.</returns>
+ public static ImageDimensions Resize(
+ ImageDimensions size,
int width,
int height,
int maxWidth,
@@ -57,12 +58,58 @@ namespace MediaBrowser.Model.Drawing
}
/// <summary>
+ /// Scale down to fill box.
+ /// Returns original size if both width and height are null or zero.
+ /// </summary>
+ /// <param name="size">The original size object.</param>
+ /// <param name="fillWidth">A new fixed width, if desired.</param>
+ /// <param name="fillHeight">A new fixed height, if desired.</param>
+ /// <returns>A new size object or size.</returns>
+ public static ImageDimensions ResizeFill(
+ ImageDimensions size,
+ int? fillWidth,
+ int? fillHeight)
+ {
+ // Return original size if input is invalid.
+ if ((fillWidth == null || fillWidth == 0)
+ && (fillHeight == null || fillHeight == 0))
+ {
+ return size;
+ }
+
+ if (fillWidth == null || fillWidth == 0)
+ {
+ fillWidth = 1;
+ }
+
+ if (fillHeight == null || fillHeight == 0)
+ {
+ fillHeight = 1;
+ }
+
+ double widthRatio = size.Width / (double)fillWidth;
+ double heightRatio = size.Height / (double)fillHeight;
+ double scaleRatio = Math.Min(widthRatio, heightRatio);
+
+ // Clamp to current size.
+ if (scaleRatio < 1)
+ {
+ return size;
+ }
+
+ int newWidth = Convert.ToInt32(Math.Ceiling(size.Width / scaleRatio));
+ int newHeight = Convert.ToInt32(Math.Ceiling(size.Height / scaleRatio));
+
+ return new ImageDimensions(newWidth, newHeight);
+ }
+
+ /// <summary>
/// Gets the new width.
/// </summary>
/// <param name="currentHeight">Height of the current.</param>
/// <param name="currentWidth">Width of the current.</param>
/// <param name="newHeight">The new height.</param>
- /// <returns>the new width</returns>
+ /// <returns>The new width.</returns>
private static int GetNewWidth(int currentHeight, int currentWidth, int newHeight)
=> Convert.ToInt32((double)newHeight / currentHeight * currentWidth);