diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/SubsampleRatio.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/SubsampleRatio.cs index c4d589459..bc83c507b 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/SubsampleRatio.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/SubsampleRatio.cs @@ -1,5 +1,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder { + using SixLabors.Primitives; + /// /// Provides enumeration of the various available subsample ratios. /// https://en.wikipedia.org/wiki/Chroma_subsampling @@ -64,5 +66,32 @@ return SubsampleRatio.Ratio444; } + + /// + /// Returns the height and width of the chroma components + /// + /// The subsampling ratio. + /// The width. + /// The height. + /// The of the chrominance channel + public static Size CalculateChrominanceSize(this SubsampleRatio ratio, int width, int height) + { + switch (ratio) + { + case SubsampleRatio.Ratio422: + return new Size((width + 1) / 2, height); + case SubsampleRatio.Ratio420: + return new Size((width + 1) / 2, (height + 1) / 2); + case SubsampleRatio.Ratio440: + return new Size(width, (height + 1) / 2); + case SubsampleRatio.Ratio411: + return new Size((width + 3) / 4, height); + case SubsampleRatio.Ratio410: + return new Size((width + 3) / 4, (height + 1) / 2); + default: + // Default to 4:4:4 subsampling. + return new Size(width, height); + } + } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/YCbCrImage.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/YCbCrImage.cs index 37844c5d1..7260784ff 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/YCbCrImage.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/YCbCrImage.cs @@ -38,7 +38,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// The ratio. public YCbCrImage(int width, int height, SubsampleRatio ratio) { - Size cSize = CalculateChrominanceSize(width, height, ratio); + Size cSize = ratio.CalculateChrominanceSize(width, height); this.Ratio = ratio; this.YStride = width; @@ -112,35 +112,5 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder { return y * this.YStride; } - - /// - /// Returns the height and width of the chroma components - /// - /// The width. - /// The height. - /// The subsampling ratio. - /// The of the chrominance channel - internal static Size CalculateChrominanceSize( - int width, - int height, - SubsampleRatio ratio) - { - switch (ratio) - { - case SubsampleRatio.Ratio422: - return new Size((width + 1) / 2, height); - case SubsampleRatio.Ratio420: - return new Size((width + 1) / 2, (height + 1) / 2); - case SubsampleRatio.Ratio440: - return new Size(width, (height + 1) / 2); - case SubsampleRatio.Ratio411: - return new Size((width + 3) / 4, height); - case SubsampleRatio.Ratio410: - return new Size((width + 3) / 4, (height + 1) / 2); - default: - // Default to 4:4:4 subsampling. - return new Size(width, height); - } - } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Formats/Jpg/YCbCrImageTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SubsampleRatioTests.cs similarity index 92% rename from tests/ImageSharp.Tests/Formats/Jpg/YCbCrImageTests.cs rename to tests/ImageSharp.Tests/Formats/Jpg/SubsampleRatioTests.cs index c50da7682..6e30e6f80 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/YCbCrImageTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SubsampleRatioTests.cs @@ -9,9 +9,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg using Xunit; using Xunit.Abstractions; - public class YCbCrImageTests + public class SubsampleRatioTests { - public YCbCrImageTests(ITestOutputHelper output) + public SubsampleRatioTests(ITestOutputHelper output) { this.Output = output; } @@ -31,7 +31,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg int expectedDivY) { //this.Output.WriteLine($"RATIO: {ratio}"); - Size size = YCbCrImage.CalculateChrominanceSize(400, 400, ratio); + Size size = ratio.CalculateChrominanceSize(400, 400); //this.Output.WriteLine($"Ch Size: {size}"); Assert.Equal(new Size(400 / expectedDivX, 400 / expectedDivY), size);