From 8b7effd6ff1694688e93d03a48c5dcddb4efe4f0 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Tue, 18 Sep 2012 15:33:57 -0400 Subject: Moved discovery of loggers and weather providers to MEF. Also added support for third-party image processors, also discovered through MEF. --- MediaBrowser.Controller/Drawing/DrawingUtils.cs | 81 +++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 MediaBrowser.Controller/Drawing/DrawingUtils.cs (limited to 'MediaBrowser.Controller/Drawing/DrawingUtils.cs') diff --git a/MediaBrowser.Controller/Drawing/DrawingUtils.cs b/MediaBrowser.Controller/Drawing/DrawingUtils.cs new file mode 100644 index 000000000..8e2f829b9 --- /dev/null +++ b/MediaBrowser.Controller/Drawing/DrawingUtils.cs @@ -0,0 +1,81 @@ +using System; +using System.Drawing; + +namespace MediaBrowser.Controller.Drawing +{ + public static class DrawingUtils + { + /// + /// Resizes a set of dimensions + /// + public static Size Resize(int currentWidth, int currentHeight, int? width, int? height, int? maxWidth, int? maxHeight) + { + return Resize(new Size(currentWidth, currentHeight), width, height, maxWidth, maxHeight); + } + + /// + /// Resizes a set of dimensions + /// + /// The original size object + /// A new fixed width, if desired + /// A new fixed neight, if desired + /// A max fixed width, if desired + /// A max fixed height, if desired + /// A new size object + public static Size Resize(Size size, int? width, int? height, int? maxWidth, int? maxHeight) + { + decimal newWidth = size.Width; + decimal 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; + } + + if (maxHeight.HasValue && maxHeight < newHeight) + { + newWidth = GetNewWidth(newHeight, newWidth, maxHeight.Value); + newHeight = maxHeight.Value; + } + + if (maxWidth.HasValue && maxWidth < newWidth) + { + newHeight = GetNewHeight(newHeight, newWidth, maxWidth.Value); + newWidth = maxWidth.Value; + } + + return new Size(Convert.ToInt32(newWidth), Convert.ToInt32(newHeight)); + } + + private static decimal GetNewWidth(decimal currentHeight, decimal currentWidth, int newHeight) + { + decimal scaleFactor = newHeight; + scaleFactor /= currentHeight; + scaleFactor *= currentWidth; + + return scaleFactor; + } + + private static decimal GetNewHeight(decimal currentHeight, decimal currentWidth, int newWidth) + { + decimal scaleFactor = newWidth; + scaleFactor /= currentWidth; + scaleFactor *= currentHeight; + + return scaleFactor; + } + } +} -- cgit v1.2.3