< Summary - Jellyfin

Information
Class: MediaBrowser.Model.Drawing.DrawingUtils
Assembly: MediaBrowser.Model
File(s): /srv/git/jellyfin/MediaBrowser.Model/Drawing/DrawingUtils.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 35
Coverable lines: 35
Total lines: 126
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 34
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Resize(...)0%272160%
ResizeFill(...)0%342180%
GetNewWidth(...)100%210%
GetNewHeight(...)100%210%

File(s)

/srv/git/jellyfin/MediaBrowser.Model/Drawing/DrawingUtils.cs

#LineLine coverage
 1using System;
 2
 3namespace 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        {
 026            int newWidth = size.Width;
 027            int newHeight = size.Height;
 28
 029            if (width > 0 && height > 0)
 30            {
 031                newWidth = width;
 032                newHeight = height;
 33            }
 034            else if (height > 0)
 35            {
 036                newWidth = GetNewWidth(newHeight, newWidth, height);
 037                newHeight = height;
 38            }
 039            else if (width > 0)
 40            {
 041                newHeight = GetNewHeight(newHeight, newWidth, width);
 042                newWidth = width;
 43            }
 44
 045            if (maxHeight > 0 && maxHeight < newHeight)
 46            {
 047                newWidth = GetNewWidth(newHeight, newWidth, maxHeight);
 048                newHeight = maxHeight;
 49            }
 50
 051            if (maxWidth > 0 && maxWidth < newWidth)
 52            {
 053                newHeight = GetNewHeight(newHeight, newWidth, maxWidth);
 054                newWidth = maxWidth;
 55            }
 56
 057            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.
 074            if ((fillWidth is null || fillWidth == 0)
 075                && (fillHeight is null || fillHeight == 0))
 76            {
 077                return size;
 78            }
 79
 080            if (fillWidth is null || fillWidth == 0)
 81            {
 082                fillWidth = 1;
 83            }
 84
 085            if (fillHeight is null || fillHeight == 0)
 86            {
 087                fillHeight = 1;
 88            }
 89
 090            double widthRatio = size.Width / (double)fillWidth;
 091            double heightRatio = size.Height / (double)fillHeight;
 092            double scaleRatio = Math.Min(widthRatio, heightRatio);
 93
 94            // Clamp to current size.
 095            if (scaleRatio < 1)
 96            {
 097                return size;
 98            }
 99
 0100            int newWidth = Convert.ToInt32(Math.Ceiling(size.Width / scaleRatio));
 0101            int newHeight = Convert.ToInt32(Math.Ceiling(size.Height / scaleRatio));
 102
 0103            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)
 0114            => 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)
 0124            => Convert.ToInt32((double)newWidth / currentWidth * currentHeight);
 125    }
 126}