aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/Drawing/DrawingUtils.cs
blob: e95b5e3752790865f14c535355f22ffa76f76598 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System.Globalization;

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)
        {
            return Scale(new ImageSize { Width = currentWidth, Height = currentHeight }, scaleFactor);
        }

        /// <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;

            return Resize(size.Width, size.Height, newWidth);
        }

        /// <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>
        public static ImageSize Resize(double currentWidth, double currentHeight, double? width = null, double? height = null, double? maxWidth = null, double? maxHeight = null)
        {
            return Resize(new ImageSize { Width = currentWidth, Height = currentHeight }, width, height, maxWidth, maxHeight);
        }

        /// <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 ImageSize Resize(ImageSize size, double? width = null, double? height = null, double? maxWidth = null, double? maxHeight = null)
        {
            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;
            }

            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 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
    {
        private static readonly CultureInfo UsCulture = new CultureInfo("en-US");

        private double _height;
        private double _width;

        /// <summary>
        /// Gets or sets the height.
        /// </summary>
        /// <value>The height.</value>
        public double Height
        {
            get
            {
                return _height;
            }
            set
            {
                _height = value;
            }
        }

        /// <summary>
        /// Gets or sets the width.
        /// </summary>
        /// <value>The width.</value>
        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);
        }

        private void ParseValue(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                var parts = value.Split('-');

                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;
                    }
                }
            }
        }
    }
}