using System;
namespace MediaBrowser.Model.Drawing
{
///
/// Class DrawingUtils.
///
public static class DrawingUtils
{
///
/// Resizes a set of dimensions.
///
/// The original size object.
/// A new fixed width, if desired.
/// A new fixed height, if desired.
/// A max fixed width, if desired.
/// A max fixed height, if desired.
/// A new size object.
public static ImageDimensions Resize(
ImageDimensions size,
int width,
int height,
int maxWidth,
int maxHeight)
{
int newWidth = size.Width;
int newHeight = size.Height;
if (width > 0 && height > 0)
{
newWidth = width;
newHeight = height;
}
else if (height > 0)
{
newWidth = GetNewWidth(newHeight, newWidth, height);
newHeight = height;
}
else if (width > 0)
{
newHeight = GetNewHeight(newHeight, newWidth, width);
newWidth = width;
}
if (maxHeight > 0 && maxHeight < newHeight)
{
newWidth = GetNewWidth(newHeight, newWidth, maxHeight);
newHeight = maxHeight;
}
if (maxWidth > 0 && maxWidth < newWidth)
{
newHeight = GetNewHeight(newHeight, newWidth, maxWidth);
newWidth = maxWidth;
}
return new ImageDimensions(newWidth, newHeight);
}
///
/// Scale down to fill box.
/// Returns original size if both width and height are null or zero.
///
/// The original size object.
/// A new fixed width, if desired.
/// A new fixed height, if desired.
/// A new size object or size.
public static ImageDimensions ResizeFill(
ImageDimensions size,
int? fillWidth,
int? fillHeight)
{
// Return original size if input is invalid.
if ((fillWidth is null || fillWidth == 0)
&& (fillHeight is null || fillHeight == 0))
{
return size;
}
if (fillWidth is null || fillWidth == 0)
{
fillWidth = 1;
}
if (fillHeight is 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);
}
///
/// Scales a size down uniformly until it fits inside a bounding box.
/// Returns the original size if it already fits, so this never upscales.
///
/// The size object.
/// The box the result has to fit inside.
/// A new size object, or if it already fits.
public static ImageDimensions ScaleDownToFit(ImageDimensions size, ImageDimensions boundingBox)
{
if (size.Width <= 0 || size.Height <= 0 || boundingBox.Width <= 0 || boundingBox.Height <= 0)
{
return size;
}
double widthRatio = size.Width / (double)boundingBox.Width;
double heightRatio = size.Height / (double)boundingBox.Height;
double scaleRatio = Math.Max(widthRatio, heightRatio);
if (scaleRatio <= 1)
{
return size;
}
var newWidth = Math.Clamp(Convert.ToInt32(Math.Round(size.Width / scaleRatio)), 1, boundingBox.Width);
var newHeight = Math.Clamp(Convert.ToInt32(Math.Round(size.Height / scaleRatio)), 1, boundingBox.Height);
return new ImageDimensions(newWidth, newHeight);
}
///
/// Gets the new width.
///
/// Height of the current.
/// Width of the current.
/// The new height.
/// The new width.
private static int GetNewWidth(int currentHeight, int currentWidth, int newHeight)
=> Convert.ToInt32((double)newHeight / currentHeight * currentWidth);
///
/// Gets the new height.
///
/// Height of the current.
/// Width of the current.
/// The new width.
/// System.Double.
private static int GetNewHeight(int currentHeight, int currentWidth, int newWidth)
=> Convert.ToInt32((double)newWidth / currentWidth * currentHeight);
}
}