aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/Drawing/DrawingUtils.cs
blob: 0be30b0baf94aecd918b48589cc4f638b177aab4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;

namespace MediaBrowser.Model.Drawing
{
    /// <summary>
    /// Class DrawingUtils.
    /// </summary>
    public static class DrawingUtils
    {
        /// <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>
        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);
        }

        /// <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>
        private static int GetNewWidth(int currentHeight, int currentWidth, int newHeight)
            => Convert.ToInt32((double)newHeight / currentHeight * currentWidth);

        /// <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 int GetNewHeight(int currentHeight, int currentWidth, int newWidth)
            => Convert.ToInt32((double)newWidth / currentWidth * currentHeight);
    }
}