From a86b71899ec52c44ddc6c3018e8cc5e9d7ff4d62 Mon Sep 17 00:00:00 2001 From: Andrew Rabert Date: Thu, 27 Dec 2018 18:27:57 -0500 Subject: Add GPL modules --- MediaBrowser.Model/Drawing/DrawingUtils.cs | 91 +++++++++++++++++++++++++ MediaBrowser.Model/Drawing/ImageFormat.cs | 30 +++++++++ MediaBrowser.Model/Drawing/ImageOrientation.cs | 15 +++++ MediaBrowser.Model/Drawing/ImageSize.cs | 93 ++++++++++++++++++++++++++ 4 files changed, 229 insertions(+) create mode 100644 MediaBrowser.Model/Drawing/DrawingUtils.cs create mode 100644 MediaBrowser.Model/Drawing/ImageFormat.cs create mode 100644 MediaBrowser.Model/Drawing/ImageOrientation.cs create mode 100644 MediaBrowser.Model/Drawing/ImageSize.cs (limited to 'MediaBrowser.Model/Drawing') diff --git a/MediaBrowser.Model/Drawing/DrawingUtils.cs b/MediaBrowser.Model/Drawing/DrawingUtils.cs new file mode 100644 index 0000000000..e6235cb06f --- /dev/null +++ b/MediaBrowser.Model/Drawing/DrawingUtils.cs @@ -0,0 +1,91 @@ +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 ImageSize Resize(ImageSize size, + double width, + double height, + double maxWidth, + double maxHeight) + { + double newWidth = size.Width; + double 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 ImageSize { Width = newWidth, Height = newHeight }; + } + + /// + /// Gets the new width. + /// + /// Height of the current. + /// Width of the current. + /// The new height. + /// System.Double. + private static double GetNewWidth(double currentHeight, double currentWidth, double newHeight) + { + double scaleFactor = newHeight; + scaleFactor /= currentHeight; + scaleFactor *= currentWidth; + + return scaleFactor; + } + + /// + /// Gets the new height. + /// + /// Height of the current. + /// Width of the current. + /// The new width. + /// System.Double. + private static double GetNewHeight(double currentHeight, double currentWidth, double newWidth) + { + double scaleFactor = newWidth; + scaleFactor /= currentWidth; + scaleFactor *= currentHeight; + + return scaleFactor; + } + } +} diff --git a/MediaBrowser.Model/Drawing/ImageFormat.cs b/MediaBrowser.Model/Drawing/ImageFormat.cs new file mode 100644 index 0000000000..0172c9754f --- /dev/null +++ b/MediaBrowser.Model/Drawing/ImageFormat.cs @@ -0,0 +1,30 @@ + +namespace MediaBrowser.Model.Drawing +{ + /// + /// Enum ImageOutputFormat + /// + public enum ImageFormat + { + /// + /// The BMP + /// + Bmp, + /// + /// The GIF + /// + Gif, + /// + /// The JPG + /// + Jpg, + /// + /// The PNG + /// + Png, + /// + /// The webp + /// + Webp + } +} diff --git a/MediaBrowser.Model/Drawing/ImageOrientation.cs b/MediaBrowser.Model/Drawing/ImageOrientation.cs new file mode 100644 index 0000000000..c320a82240 --- /dev/null +++ b/MediaBrowser.Model/Drawing/ImageOrientation.cs @@ -0,0 +1,15 @@ + +namespace MediaBrowser.Model.Drawing +{ + public enum ImageOrientation + { + TopLeft = 1, + TopRight = 2, + BottomRight = 3, + BottomLeft = 4, + LeftTop = 5, + RightTop = 6, + RightBottom = 7, + LeftBottom = 8, + } +} diff --git a/MediaBrowser.Model/Drawing/ImageSize.cs b/MediaBrowser.Model/Drawing/ImageSize.cs new file mode 100644 index 0000000000..c2b0291bdb --- /dev/null +++ b/MediaBrowser.Model/Drawing/ImageSize.cs @@ -0,0 +1,93 @@ +using System.Globalization; + +namespace MediaBrowser.Model.Drawing +{ + /// + /// Struct ImageSize + /// + public struct ImageSize + { + private double _height; + private double _width; + + /// + /// Gets or sets the height. + /// + /// The height. + public double Height + { + get + { + return _height; + } + set + { + _height = value; + } + } + + /// + /// Gets or sets the width. + /// + /// The width. + public double Width + { + get { return _width; } + set { _width = value; } + } + + public bool Equals(ImageSize size) + { + return Width.Equals(size.Width) && Height.Equals(size.Height); + } + + public override string ToString() + { + return string.Format("{0}-{1}", Width, Height); + } + + public ImageSize(string value) + { + _width = 0; + + _height = 0; + + ParseValue(value); + } + + public ImageSize(int width, int height) + { + _width = width; + _height = height; + } + + public ImageSize(double width, double height) + { + _width = width; + _height = height; + } + + private void ParseValue(string value) + { + if (!string.IsNullOrEmpty(value)) + { + string[] parts = value.Split('-'); + + if (parts.Length == 2) + { + double val; + + if (double.TryParse(parts[0], NumberStyles.Any, CultureInfo.InvariantCulture, out val)) + { + _width = val; + } + + if (double.TryParse(parts[1], NumberStyles.Any, CultureInfo.InvariantCulture, out val)) + { + _height = val; + } + } + } + } + } +} \ No newline at end of file -- cgit v1.2.3