2013-11-07 15:57:12 +00:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
namespace MediaBrowser.Model.Drawing
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class DrawingUtils
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class DrawingUtils
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resizes a set of dimensions
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="currentWidth">Width of the current.</param>
|
|
|
|
|
/// <param name="currentHeight">Height of the current.</param>
|
|
|
|
|
/// <param name="scaleFactor">The scale factor.</param>
|
|
|
|
|
/// <returns>ImageSize.</returns>
|
|
|
|
|
public static ImageSize Scale(double currentWidth, double currentHeight, double scaleFactor)
|
|
|
|
|
{
|
2014-05-08 20:09:53 +00:00
|
|
|
|
return Scale(new ImageSize
|
|
|
|
|
{
|
|
|
|
|
Width = currentWidth,
|
|
|
|
|
Height = currentHeight
|
|
|
|
|
|
|
|
|
|
}, scaleFactor);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resizes a set of dimensions
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="size">The size.</param>
|
|
|
|
|
/// <param name="scaleFactor">The scale factor.</param>
|
|
|
|
|
/// <returns>ImageSize.</returns>
|
|
|
|
|
public static ImageSize Scale(ImageSize size, double scaleFactor)
|
|
|
|
|
{
|
|
|
|
|
var newWidth = size.Width * scaleFactor;
|
|
|
|
|
|
2014-05-08 20:09:53 +00:00
|
|
|
|
return Resize(size.Width, size.Height, newWidth, null, null, null);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resizes a set of dimensions
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="currentWidth">Width of the current.</param>
|
|
|
|
|
/// <param name="currentHeight">Height of the current.</param>
|
|
|
|
|
/// <param name="width">The width.</param>
|
|
|
|
|
/// <param name="height">The height.</param>
|
|
|
|
|
/// <param name="maxWidth">A max fixed width, if desired</param>
|
|
|
|
|
/// <param name="maxHeight">A max fixed height, if desired</param>
|
|
|
|
|
/// <returns>ImageSize.</returns>
|
2014-05-08 20:09:53 +00:00
|
|
|
|
public static ImageSize Resize(double currentWidth,
|
|
|
|
|
double currentHeight,
|
|
|
|
|
double? width,
|
|
|
|
|
double? height,
|
|
|
|
|
double? maxWidth,
|
|
|
|
|
double? maxHeight)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2014-05-08 20:09:53 +00:00
|
|
|
|
return Resize(new ImageSize
|
|
|
|
|
{
|
|
|
|
|
Width = currentWidth,
|
|
|
|
|
Height = currentHeight
|
|
|
|
|
|
|
|
|
|
}, width, height, maxWidth, maxHeight);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 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>
|
2014-05-08 20:09:53 +00:00
|
|
|
|
public static ImageSize Resize(ImageSize size,
|
|
|
|
|
double? width,
|
|
|
|
|
double? height,
|
|
|
|
|
double? maxWidth,
|
|
|
|
|
double? maxHeight)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
double newWidth = size.Width;
|
|
|
|
|
double newHeight = size.Height;
|
|
|
|
|
|
|
|
|
|
if (width.HasValue && height.HasValue)
|
|
|
|
|
{
|
|
|
|
|
newWidth = width.Value;
|
|
|
|
|
newHeight = height.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (height.HasValue)
|
|
|
|
|
{
|
|
|
|
|
newWidth = GetNewWidth(newHeight, newWidth, height.Value);
|
|
|
|
|
newHeight = height.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (width.HasValue)
|
|
|
|
|
{
|
|
|
|
|
newHeight = GetNewHeight(newHeight, newWidth, width.Value);
|
|
|
|
|
newWidth = width.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-08 20:09:53 +00:00
|
|
|
|
if (maxHeight.HasValue && maxHeight.Value < newHeight)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
newWidth = GetNewWidth(newHeight, newWidth, maxHeight.Value);
|
|
|
|
|
newHeight = maxHeight.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-08 20:09:53 +00:00
|
|
|
|
if (maxWidth.HasValue && maxWidth.Value < newWidth)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
newHeight = GetNewHeight(newHeight, newWidth, maxWidth.Value);
|
|
|
|
|
newWidth = maxWidth.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new ImageSize { Width = newWidth, Height = 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>System.Double.</returns>
|
|
|
|
|
private static double GetNewWidth(double currentHeight, double currentWidth, double newHeight)
|
|
|
|
|
{
|
|
|
|
|
var scaleFactor = newHeight;
|
|
|
|
|
scaleFactor /= currentHeight;
|
|
|
|
|
scaleFactor *= currentWidth;
|
|
|
|
|
|
|
|
|
|
return scaleFactor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the new height.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="currentHeight">Height of the current.</param>
|
|
|
|
|
/// <param name="currentWidth">Width of the current.</param>
|
|
|
|
|
/// <param name="newWidth">The new width.</param>
|
|
|
|
|
/// <returns>System.Double.</returns>
|
|
|
|
|
private static double GetNewHeight(double currentHeight, double currentWidth, double newWidth)
|
|
|
|
|
{
|
|
|
|
|
var scaleFactor = newWidth;
|
|
|
|
|
scaleFactor /= currentWidth;
|
|
|
|
|
scaleFactor *= currentHeight;
|
|
|
|
|
|
|
|
|
|
return scaleFactor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Struct ImageSize
|
|
|
|
|
/// </summary>
|
|
|
|
|
public struct ImageSize
|
|
|
|
|
{
|
2013-11-07 15:57:12 +00:00
|
|
|
|
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
|
|
|
|
|
|
|
|
|
private double _height;
|
|
|
|
|
private double _width;
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the height.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The height.</value>
|
2013-11-07 15:57:12 +00:00
|
|
|
|
public double Height
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _height;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_height = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the width.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The width.</value>
|
2013-11-07 15:57:12 +00:00
|
|
|
|
public double Width
|
|
|
|
|
{
|
|
|
|
|
get { return _width; }
|
|
|
|
|
set { _width = value; }
|
|
|
|
|
}
|
2013-11-06 21:32:26 +00:00
|
|
|
|
|
|
|
|
|
public bool Equals(ImageSize size)
|
|
|
|
|
{
|
|
|
|
|
return Width.Equals(size.Width) && Height.Equals(size.Height);
|
|
|
|
|
}
|
2013-11-07 15:57:12 +00:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return string.Format("{0}-{1}", Width, Height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ImageSize(string value)
|
|
|
|
|
{
|
|
|
|
|
_width = 0;
|
|
|
|
|
|
|
|
|
|
_height = 0;
|
|
|
|
|
|
|
|
|
|
ParseValue(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ParseValue(string value)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(value))
|
|
|
|
|
{
|
2014-05-08 20:09:53 +00:00
|
|
|
|
string[] parts = value.Split('-');
|
2013-11-07 15:57:12 +00:00
|
|
|
|
|
|
|
|
|
if (parts.Length == 2)
|
|
|
|
|
{
|
|
|
|
|
double val;
|
|
|
|
|
|
|
|
|
|
if (double.TryParse(parts[0], NumberStyles.Any, UsCulture, out val))
|
|
|
|
|
{
|
|
|
|
|
_width = val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (double.TryParse(parts[1], NumberStyles.Any, UsCulture, out val))
|
|
|
|
|
{
|
|
|
|
|
_height = val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|