| | 1 | | using System; |
| | 2 | |
|
| | 3 | | namespace MediaBrowser.Model.Drawing |
| | 4 | | { |
| | 5 | | /// <summary> |
| | 6 | | /// Class DrawingUtils. |
| | 7 | | /// </summary> |
| | 8 | | public static class DrawingUtils |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Resizes a set of dimensions. |
| | 12 | | /// </summary> |
| | 13 | | /// <param name="size">The original size object.</param> |
| | 14 | | /// <param name="width">A new fixed width, if desired.</param> |
| | 15 | | /// <param name="height">A new fixed height, if desired.</param> |
| | 16 | | /// <param name="maxWidth">A max fixed width, if desired.</param> |
| | 17 | | /// <param name="maxHeight">A max fixed height, if desired.</param> |
| | 18 | | /// <returns>A new size object.</returns> |
| | 19 | | public static ImageDimensions Resize( |
| | 20 | | ImageDimensions size, |
| | 21 | | int width, |
| | 22 | | int height, |
| | 23 | | int maxWidth, |
| | 24 | | int maxHeight) |
| | 25 | | { |
| 0 | 26 | | int newWidth = size.Width; |
| 0 | 27 | | int newHeight = size.Height; |
| | 28 | |
|
| 0 | 29 | | if (width > 0 && height > 0) |
| | 30 | | { |
| 0 | 31 | | newWidth = width; |
| 0 | 32 | | newHeight = height; |
| | 33 | | } |
| 0 | 34 | | else if (height > 0) |
| | 35 | | { |
| 0 | 36 | | newWidth = GetNewWidth(newHeight, newWidth, height); |
| 0 | 37 | | newHeight = height; |
| | 38 | | } |
| 0 | 39 | | else if (width > 0) |
| | 40 | | { |
| 0 | 41 | | newHeight = GetNewHeight(newHeight, newWidth, width); |
| 0 | 42 | | newWidth = width; |
| | 43 | | } |
| | 44 | |
|
| 0 | 45 | | if (maxHeight > 0 && maxHeight < newHeight) |
| | 46 | | { |
| 0 | 47 | | newWidth = GetNewWidth(newHeight, newWidth, maxHeight); |
| 0 | 48 | | newHeight = maxHeight; |
| | 49 | | } |
| | 50 | |
|
| 0 | 51 | | if (maxWidth > 0 && maxWidth < newWidth) |
| | 52 | | { |
| 0 | 53 | | newHeight = GetNewHeight(newHeight, newWidth, maxWidth); |
| 0 | 54 | | newWidth = maxWidth; |
| | 55 | | } |
| | 56 | |
|
| 0 | 57 | | return new ImageDimensions(newWidth, newHeight); |
| | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Scale down to fill box. |
| | 62 | | /// Returns original size if both width and height are null or zero. |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="size">The original size object.</param> |
| | 65 | | /// <param name="fillWidth">A new fixed width, if desired.</param> |
| | 66 | | /// <param name="fillHeight">A new fixed height, if desired.</param> |
| | 67 | | /// <returns>A new size object or size.</returns> |
| | 68 | | public static ImageDimensions ResizeFill( |
| | 69 | | ImageDimensions size, |
| | 70 | | int? fillWidth, |
| | 71 | | int? fillHeight) |
| | 72 | | { |
| | 73 | | // Return original size if input is invalid. |
| 0 | 74 | | if ((fillWidth is null || fillWidth == 0) |
| 0 | 75 | | && (fillHeight is null || fillHeight == 0)) |
| | 76 | | { |
| 0 | 77 | | return size; |
| | 78 | | } |
| | 79 | |
|
| 0 | 80 | | if (fillWidth is null || fillWidth == 0) |
| | 81 | | { |
| 0 | 82 | | fillWidth = 1; |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | if (fillHeight is null || fillHeight == 0) |
| | 86 | | { |
| 0 | 87 | | fillHeight = 1; |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | double widthRatio = size.Width / (double)fillWidth; |
| 0 | 91 | | double heightRatio = size.Height / (double)fillHeight; |
| 0 | 92 | | double scaleRatio = Math.Min(widthRatio, heightRatio); |
| | 93 | |
|
| | 94 | | // Clamp to current size. |
| 0 | 95 | | if (scaleRatio < 1) |
| | 96 | | { |
| 0 | 97 | | return size; |
| | 98 | | } |
| | 99 | |
|
| 0 | 100 | | int newWidth = Convert.ToInt32(Math.Ceiling(size.Width / scaleRatio)); |
| 0 | 101 | | int newHeight = Convert.ToInt32(Math.Ceiling(size.Height / scaleRatio)); |
| | 102 | |
|
| 0 | 103 | | return new ImageDimensions(newWidth, newHeight); |
| | 104 | | } |
| | 105 | |
|
| | 106 | | /// <summary> |
| | 107 | | /// Gets the new width. |
| | 108 | | /// </summary> |
| | 109 | | /// <param name="currentHeight">Height of the current.</param> |
| | 110 | | /// <param name="currentWidth">Width of the current.</param> |
| | 111 | | /// <param name="newHeight">The new height.</param> |
| | 112 | | /// <returns>The new width.</returns> |
| | 113 | | private static int GetNewWidth(int currentHeight, int currentWidth, int newHeight) |
| 0 | 114 | | => Convert.ToInt32((double)newHeight / currentHeight * currentWidth); |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Gets the new height. |
| | 118 | | /// </summary> |
| | 119 | | /// <param name="currentHeight">Height of the current.</param> |
| | 120 | | /// <param name="currentWidth">Width of the current.</param> |
| | 121 | | /// <param name="newWidth">The new width.</param> |
| | 122 | | /// <returns>System.Double.</returns> |
| | 123 | | private static int GetNewHeight(int currentHeight, int currentWidth, int newWidth) |
| 0 | 124 | | => Convert.ToInt32((double)newWidth / currentWidth * currentHeight); |
| | 125 | | } |
| | 126 | | } |