< Summary - Jellyfin

Information
Class: MediaBrowser.Model.Drawing.DrawingUtils
Assembly: MediaBrowser.Model
File(s): /srv/git/jellyfin/MediaBrowser.Model/Drawing/DrawingUtils.cs
Line coverage
77%
Covered lines: 35
Uncovered lines: 10
Coverable lines: 45
Total lines: 155
Line coverage: 77.7%
Branch coverage
70%
Covered branches: 31
Total branches: 44
Branch coverage: 70.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 5/6/2026 - 12:15:23 AM Line coverage: 0% (0/35) Branch coverage: 0% (0/34) Total lines: 1268/11/2026 - 12:17:00 AM Line coverage: 77.7% (35/45) Branch coverage: 70.4% (31/44) Total lines: 155 5/6/2026 - 12:15:23 AM Line coverage: 0% (0/35) Branch coverage: 0% (0/34) Total lines: 1268/11/2026 - 12:17:00 AM Line coverage: 77.7% (35/45) Branch coverage: 70.4% (31/44) Total lines: 155

Coverage delta

Coverage delta 78 -78

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Resize(...)68.75%191677.77%
ResizeFill(...)61.11%301866.66%
ScaleDownToFit(...)90%1010100%
GetNewWidth(...)100%210%
GetNewHeight(...)100%11100%

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        {
 526            int newWidth = size.Width;
 527            int newHeight = size.Height;
 28
 529            if (width > 0 && height > 0)
 30            {
 131                newWidth = width;
 132                newHeight = height;
 33            }
 434            else if (height > 0)
 35            {
 036                newWidth = GetNewWidth(newHeight, newWidth, height);
 037                newHeight = height;
 38            }
 439            else if (width > 0)
 40            {
 141                newHeight = GetNewHeight(newHeight, newWidth, width);
 142                newWidth = width;
 43            }
 44
 545            if (maxHeight > 0 && maxHeight < newHeight)
 46            {
 047                newWidth = GetNewWidth(newHeight, newWidth, maxHeight);
 048                newHeight = maxHeight;
 49            }
 50
 551            if (maxWidth > 0 && maxWidth < newWidth)
 52            {
 153                newHeight = GetNewHeight(newHeight, newWidth, maxWidth);
 154                newWidth = maxWidth;
 55            }
 56
 557            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.
 574            if ((fillWidth is null || fillWidth == 0)
 575                && (fillHeight is null || fillHeight == 0))
 76            {
 477                return size;
 78            }
 79
 180            if (fillWidth is null || fillWidth == 0)
 81            {
 082                fillWidth = 1;
 83            }
 84
 185            if (fillHeight is null || fillHeight == 0)
 86            {
 087                fillHeight = 1;
 88            }
 89
 190            double widthRatio = size.Width / (double)fillWidth;
 191            double heightRatio = size.Height / (double)fillHeight;
 192            double scaleRatio = Math.Min(widthRatio, heightRatio);
 93
 94            // Clamp to current size.
 195            if (scaleRatio < 1)
 96            {
 197                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        /// Scales a size down uniformly until it fits inside a bounding box.
 108        /// Returns the original size if it already fits, so this never upscales.
 109        /// </summary>
 110        /// <param name="size">The size object.</param>
 111        /// <param name="boundingBox">The box the result has to fit inside.</param>
 112        /// <returns>A new size object, or <paramref name="size"/> if it already fits.</returns>
 113        public static ImageDimensions ScaleDownToFit(ImageDimensions size, ImageDimensions boundingBox)
 114        {
 13115            if (size.Width <= 0 || size.Height <= 0 || boundingBox.Width <= 0 || boundingBox.Height <= 0)
 116            {
 2117                return size;
 118            }
 119
 11120            double widthRatio = size.Width / (double)boundingBox.Width;
 11121            double heightRatio = size.Height / (double)boundingBox.Height;
 11122            double scaleRatio = Math.Max(widthRatio, heightRatio);
 123
 11124            if (scaleRatio <= 1)
 125            {
 5126                return size;
 127            }
 128
 6129            var newWidth = Math.Clamp(Convert.ToInt32(Math.Round(size.Width / scaleRatio)), 1, boundingBox.Width);
 6130            var newHeight = Math.Clamp(Convert.ToInt32(Math.Round(size.Height / scaleRatio)), 1, boundingBox.Height);
 131
 6132            return new ImageDimensions(newWidth, newHeight);
 133        }
 134
 135        /// <summary>
 136        /// Gets the new width.
 137        /// </summary>
 138        /// <param name="currentHeight">Height of the current.</param>
 139        /// <param name="currentWidth">Width of the current.</param>
 140        /// <param name="newHeight">The new height.</param>
 141        /// <returns>The new width.</returns>
 142        private static int GetNewWidth(int currentHeight, int currentWidth, int newHeight)
 0143            => Convert.ToInt32((double)newHeight / currentHeight * currentWidth);
 144
 145        /// <summary>
 146        /// Gets the new height.
 147        /// </summary>
 148        /// <param name="currentHeight">Height of the current.</param>
 149        /// <param name="currentWidth">Width of the current.</param>
 150        /// <param name="newWidth">The new width.</param>
 151        /// <returns>System.Double.</returns>
 152        private static int GetNewHeight(int currentHeight, int currentWidth, int newWidth)
 2153            => Convert.ToInt32((double)newWidth / currentWidth * currentHeight);
 154    }
 155}