From 925c2ffe204bc55154e1b8da6f544ca340e3acf9 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Wed, 30 Jun 2021 18:17:27 +0200 Subject: [PATCH] Add yuv conversion test with argb --- .../Formats/WebP/YuvConversionTests.cs | 110 +++++++++++++++++- 1 file changed, 108 insertions(+), 2 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/WebP/YuvConversionTests.cs b/tests/ImageSharp.Tests/Formats/WebP/YuvConversionTests.cs index 211db14a9c..19820c2efa 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/YuvConversionTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/YuvConversionTests.cs @@ -23,9 +23,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp Configuration config = image.GetConfiguration(); MemoryAllocator memoryAllocator = config.MemoryAllocator; int pixels = image.Width * image.Height; + int uvWidth = (image.Width + 1) >> 1; using System.Buffers.IMemoryOwner yBuffer = memoryAllocator.Allocate(pixels); - using System.Buffers.IMemoryOwner uBuffer = memoryAllocator.Allocate(pixels / 2); - using System.Buffers.IMemoryOwner vBuffer = memoryAllocator.Allocate(pixels / 2); + using System.Buffers.IMemoryOwner uBuffer = memoryAllocator.Allocate(uvWidth * image.Height); + using System.Buffers.IMemoryOwner vBuffer = memoryAllocator.Allocate(uvWidth * image.Height); Span y = yBuffer.GetSpan(); Span u = uBuffer.GetSpan(); Span v = vBuffer.GetSpan(); @@ -127,5 +128,110 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp Assert.True(expectedU.AsSpan().SequenceEqual(u.Slice(0, expectedU.Length))); Assert.True(expectedV.AsSpan().SequenceEqual(v.Slice(0, expectedV.Length))); } + + [Theory] + [WithTestPatternImages(31, 31, PixelTypes.Rgba32)] + public void ConvertRgbToYuv_WithAlpha_Works(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + // arrange + using Image image = provider.GetImage(); + Configuration config = image.GetConfiguration(); + MemoryAllocator memoryAllocator = config.MemoryAllocator; + int pixels = image.Width * image.Height; + int uvWidth = (image.Width + 1) >> 1; + using System.Buffers.IMemoryOwner yBuffer = memoryAllocator.Allocate(pixels); + using System.Buffers.IMemoryOwner uBuffer = memoryAllocator.Allocate(uvWidth * image.Height); + using System.Buffers.IMemoryOwner vBuffer = memoryAllocator.Allocate(uvWidth * image.Height); + Span y = yBuffer.GetSpan(); + Span u = uBuffer.GetSpan(); + Span v = vBuffer.GetSpan(); + byte[] expectedY = + { + 16, 16, 16, 16, 16, 235, 235, 235, 235, 235, 16, 16, 16, 16, 16, 152, 41, 41, 152, 152, 41, 41, 152, + 152, 41, 41, 152, 152, 41, 41, 152, 16, 16, 16, 16, 16, 235, 235, 235, 235, 235, 16, 16, 16, 16, 16, + 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 16, 16, 16, 16, 16, 235, + 235, 235, 235, 235, 16, 16, 16, 16, 16, 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 152, + 41, 41, 152, 16, 16, 16, 16, 16, 235, 235, 235, 235, 235, 16, 16, 16, 16, 16, 152, 41, 41, 152, 152, + 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 16, 16, 16, 16, 16, 235, 235, 235, 235, 235, 16, + 16, 16, 16, 16, 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 235, 235, + 235, 235, 235, 16, 16, 16, 16, 16, 235, 235, 235, 235, 235, 152, 41, 41, 152, 152, 41, 41, 152, 152, + 41, 41, 152, 152, 41, 41, 152, 235, 235, 235, 235, 235, 16, 16, 16, 16, 16, 235, 235, 235, 235, 235, + 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 235, 235, 235, 235, 235, 16, + 16, 16, 16, 16, 235, 235, 235, 235, 235, 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 152, + 41, 41, 152, 235, 235, 235, 235, 235, 16, 16, 16, 16, 16, 235, 235, 235, 235, 235, 152, 41, 41, 152, + 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 235, 235, 235, 235, 235, 16, 16, 16, 16, 16, + 235, 235, 235, 235, 235, 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 16, + 16, 16, 16, 16, 235, 235, 235, 235, 235, 16, 16, 16, 16, 16, 152, 41, 41, 152, 152, 41, 41, 152, + 152, 41, 41, 152, 152, 41, 41, 152, 16, 16, 16, 16, 16, 235, 235, 235, 235, 235, 16, 16, 16, 16, 16, + 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 16, 16, 16, 16, 16, 235, + 235, 235, 235, 235, 16, 16, 16, 16, 16, 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 152, + 41, 41, 152, 16, 16, 16, 16, 16, 235, 235, 235, 235, 235, 16, 16, 16, 16, 16, 152, 41, 41, 152, 152, + 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 16, 16, 16, 16, 16, 235, 235, 235, 235, 235, 16, + 16, 16, 16, 16, 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 152, 41, 41, 152, 82, 82, 82, + 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 81, 158, 170, 118, 130, 182, 65, 142, 220, 103, 155, + 167, 115, 127, 204, 127, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 145, 157, 106, + 118, 170, 118, 130, 207, 90, 142, 154, 103, 114, 192, 115, 127, 82, 82, 82, 82, 82, 82, 82, 82, 82, + 82, 82, 82, 82, 82, 82, 145, 93, 105, 157, 105, 117, 195, 78, 130, 142, 90, 102, 179, 102, 114, 192, + 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 80, 92, 170, 93, 105, 182, 65, 142, 129, + 77, 155, 167, 90, 102, 179, 62, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 80, 157, + 80, 92, 170, 52, 130, 117, 65, 142, 154, 102, 89, 166, 49, 127, 82, 82, 82, 82, 82, 82, 82, 82, 82, + 82, 82, 82, 82, 82, 82, 145, 197, 80, 157, 169, 117, 104, 181, 130, 142, 90, 77, 154, 37, 114, 191, + 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 209, 67, 144, 156, 105, 117, 169, 117, + 129, 206, 64, 141, 153, 102, 179, 166, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, + 55, 132, 144, 92, 169, 156, 104, 116, 194, 77, 129, 141, 89, 166, 178, 101, 81, 81, 81, 81, 81, 81, + 81, 81, 81, 81, 81, 81, 81, 81, 81, 119, 131, 80, 157, 144, 92, 104, 181, 64, 116, 193, 76, 154, + 166, 89, 101, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 119, 67, 144, 156, 79, 91, + 169, 52, 104, 181, 64, 141, 153, 76, 88, 165, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, + 81, 81, 183, 132, 144, 196, 79, 156, 39, 116, 168, 51, 129, 141, 89, 76, 153, 101, 81, 81, 81, 81, + 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 119, 131, 183, 66, 143, 155, 104, 156, 168, 116, 128, + 205, 63, 140, 218, 101, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 119, 196, 54, + 131, 208, 91, 143, 155, 103, 115, 193, 51, 128, 205, 88, 165, 41, 41, 41, 41, 41, 41, 41, 41, 41, + 41, 41, 41, 41, 41, 41, 183, 41, 118, 196, 79, 156, 143, 91, 103, 180, 63, 115, 193, 75, 153, 165, + 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 29, 106, 183, 66, 143, 130, 78, 90, 168, + 116, 103, 180, 63, 140, 152, 75, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 93, + 171, 53, 131, 118, 66, 78, 155, 103, 90, 167, 50, 128, 140, 63, 75 + }; + byte[] expectedU = + { + 128, 128, 128, 128, 128, 128, 128, 133, 240, 139, 240, 139, 240, 139, 240, 139, 128, 128, 128, 128, + 128, 128, 128, 133, 240, 139, 240, 139, 240, 139, 240, 139, 128, 128, 128, 128, 128, 128, 128, 134, + 240, 139, 240, 139, 240, 139, 240, 139, 128, 128, 128, 128, 128, 128, 128, 134, 240, 139, 240, 139, + 240, 139, 240, 139, 128, 128, 128, 128, 128, 128, 128, 134, 240, 139, 240, 139, 240, 139, 240, 139, + 128, 128, 128, 128, 128, 128, 128, 133, 240, 139, 240, 139, 240, 139, 240, 139, 128, 128, 128, 128, + 128, 128, 128, 133, 240, 139, 240, 139, 240, 139, 240, 139, 112, 112, 108, 106, 106, 112, 112, 115, + 174, 157, 172, 129, 191, 129, 204, 112, 90, 90, 90, 90, 90, 90, 90, 90, 161, 92, 116, 141, 99, 155, + 113, 97, 90, 90, 90, 90, 90, 90, 90, 91, 145, 114, 173, 122, 133, 127, 96, 170, 96, 96, 96, 96, 96, + 96, 97, 96, 98, 134, 122, 113, 139, 93, 169, 85, 91, 91, 91, 91, 91, 91, 91, 92, 134, 130, 112, 149, + 105, 139, 146, 110, 91, 91, 91, 91, 91, 91, 91, 90, 164, 117, 149, 127, 128, 166, 107, 129, 159, + 159, 159, 159, 159, 159, 159, 160, 112, 113, 138, 87, 143, 112, 88, 161, 240, 240, 240, 240, 240, + 240, 240, 235, 110, 162, 110, 140, 158, 104, 159, 137, 240, 240, 240, 240, 240, 240, 240, 232, 150, + 108, 140, 161, 80, 157, 162, 128 + }; + byte[] expectedV = + { + 128, 128, 128, 128, 128, 128, 128, 153, 110, 189, 110, 189, 110, 189, 110, 189, 128, 128, 128, 128, + 128, 128, 128, 153, 110, 189, 110, 189, 110, 189, 110, 189, 128, 128, 128, 128, 128, 128, 128, 157, + 110, 189, 110, 189, 110, 189, 110, 189, 128, 128, 128, 128, 128, 128, 128, 160, 110, 189, 110, 189, + 110, 189, 110, 189, 128, 128, 128, 128, 128, 128, 128, 160, 110, 189, 110, 189, 110, 189, 110, 189, + 128, 128, 128, 128, 128, 128, 128, 153, 110, 189, 110, 189, 110, 189, 110, 189, 128, 128, 128, 128, + 128, 128, 128, 153, 110, 189, 110, 189, 110, 189, 110, 189, 175, 175, 186, 193, 193, 175, 175, 173, + 110, 151, 106, 153, 122, 146, 92, 195, 240, 240, 240, 240, 240, 240, 240, 239, 121, 131, 142, 135, + 109, 92, 146, 115, 240, 240, 240, 240, 240, 240, 240, 238, 136, 148, 137, 113, 157, 155, 121, 130, + 155, 155, 155, 155, 155, 155, 155, 154, 135, 96, 88, 142, 136, 105, 138, 116, 81, 81, 81, 81, 81, + 81, 81, 82, 104, 148, 150, 111, 138, 128, 116, 141, 81, 81, 81, 81, 81, 81, 81, 80, 147, 133, 119, + 141, 165, 126, 147, 173, 101, 101, 101, 101, 101, 101, 101, 101, 109, 129, 122, 124, 107, 108, 128, + 138, 110, 110, 110, 110, 110, 110, 110, 110, 137, 151, 127, 114, 131, 139, 142, 120, 110, 110, 110, + 110, 110, 110, 110, 112, 156, 119, 137, 167, 141, 151, 66, 85 + }; + + // act + YuvConversion.ConvertRgbToYuv(image, config, memoryAllocator, y, u, v); + + // assert + Assert.True(expectedY.AsSpan().SequenceEqual(y)); + Assert.True(expectedU.AsSpan().SequenceEqual(u.Slice(0, expectedU.Length))); + Assert.True(expectedV.AsSpan().SequenceEqual(v.Slice(0, expectedV.Length))); + } } }