Browse Source

more subsampling refactor

pull/322/head
Anton Firszov 9 years ago
parent
commit
fd3931f602
  1. 29
      src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/SubsampleRatio.cs
  2. 32
      src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/YCbCrImage.cs
  3. 6
      tests/ImageSharp.Tests/Formats/Jpg/SubsampleRatioTests.cs

29
src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/SubsampleRatio.cs

@ -1,5 +1,7 @@
namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
{
using SixLabors.Primitives;
/// <summary>
/// Provides enumeration of the various available subsample ratios.
/// https://en.wikipedia.org/wiki/Chroma_subsampling
@ -64,5 +66,32 @@
return SubsampleRatio.Ratio444;
}
/// <summary>
/// Returns the height and width of the chroma components
/// </summary>
/// <param name="ratio">The subsampling ratio.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <returns>The <see cref="Size"/> of the chrominance channel</returns>
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);
}
}
}
}

32
src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/YCbCrImage.cs

@ -38,7 +38,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
/// <param name="ratio">The ratio.</param>
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;
}
/// <summary>
/// Returns the height and width of the chroma components
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="ratio">The subsampling ratio.</param>
/// <returns>The <see cref="Size"/> of the chrominance channel</returns>
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);
}
}
}
}

6
tests/ImageSharp.Tests/Formats/Jpg/YCbCrImageTests.cs → 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);
Loading…
Cancel
Save