Browse Source

Merge pull request #2173 from SixLabors/bp/Issue2171

Fix issue when writing lossy webp with alpha data
pull/2183/head
James Jackson-South 4 years ago
committed by GitHub
parent
commit
1db6b213b4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs
  2. 3
      tests/ImageSharp.Tests/Formats/WebP/Vp8LHistogramTests.cs
  3. 2
      tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs
  4. 18
      tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs
  5. 2
      tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs
  6. 5
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

2
src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs

@ -382,7 +382,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy
(uint)width, (uint)width,
(uint)height, (uint)height,
hasAlpha, hasAlpha,
alphaData, alphaData.Slice(0, alphaDataSize),
this.alphaCompression && alphaCompressionSucceeded); this.alphaCompression && alphaCompressionSucceeded);
} }

3
tests/ImageSharp.Tests/Formats/WebP/Vp8LHistogramTests.cs

@ -7,8 +7,9 @@ using SixLabors.ImageSharp.Formats.Webp.Lossless;
using SixLabors.ImageSharp.Tests.TestUtilities; using SixLabors.ImageSharp.Tests.TestUtilities;
using Xunit; using Xunit;
namespace SixLabors.ImageSharp.Tests.Formats.WebP namespace SixLabors.ImageSharp.Tests.Formats.Webp
{ {
[Trait("Format", "Webp")]
public class Vp8LHistogramTests public class Vp8LHistogramTests
{ {
private static void RunAddVectorTest() private static void RunAddVectorTest()

2
tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs

@ -5,7 +5,7 @@ using SixLabors.ImageSharp.Formats.Webp.Lossy;
using SixLabors.ImageSharp.Tests.TestUtilities; using SixLabors.ImageSharp.Tests.TestUtilities;
using Xunit; using Xunit;
namespace SixLabors.ImageSharp.Tests.Formats.WebP namespace SixLabors.ImageSharp.Tests.Formats.Webp
{ {
[Trait("Format", "Webp")] [Trait("Format", "Webp")]
public class Vp8ResidualTests public class Vp8ResidualTests

18
tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs

@ -7,6 +7,7 @@ using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.TestUtilities; using SixLabors.ImageSharp.Tests.TestUtilities;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs;
using Xunit; using Xunit;
using static SixLabors.ImageSharp.Tests.TestImages.Webp; using static SixLabors.ImageSharp.Tests.TestImages.Webp;
@ -268,9 +269,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
} }
[Theory] [Theory]
[WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, false)] [WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, false, 64020)]
[WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, true)] [WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, true, 16200)]
public void Encode_Lossy_WithAlpha_Works<TPixel>(TestImageProvider<TPixel> provider, bool compressed) public void Encode_Lossy_WithAlpha_Works<TPixel>(TestImageProvider<TPixel> provider, bool compressed, int expectedFileSize)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
var encoder = new WebpEncoder() var encoder = new WebpEncoder()
@ -280,7 +281,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
}; };
using Image<TPixel> image = provider.GetImage(); using Image<TPixel> image = provider.GetImage();
image.VerifyEncoder(provider, "webp", $"with_alpha_compressed_{compressed}", encoder, ImageComparer.Tolerant(0.04f)); string encodedFile = image.VerifyEncoder(
provider,
"webp",
$"with_alpha_compressed_{compressed}",
encoder,
ImageComparer.Tolerant(0.04f),
referenceDecoder: new MagickReferenceDecoder());
int encodedBytes = File.ReadAllBytes(encodedFile).Length;
Assert.True(encodedBytes <= expectedFileSize);
} }
[Theory] [Theory]

2
tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs

@ -26,7 +26,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
public MagickReferenceDecoder(bool validate) => this.validate = validate; public MagickReferenceDecoder(bool validate) => this.validate = validate;
public static MagickReferenceDecoder Instance { get; } = new MagickReferenceDecoder(); public static MagickReferenceDecoder Instance { get; } = new();
private static void FromRgba32Bytes<TPixel>(Configuration configuration, Span<byte> rgbaBytes, IMemoryGroup<TPixel> destinationGroup) private static void FromRgba32Bytes<TPixel>(Configuration configuration, Span<byte> rgbaBytes, IMemoryGroup<TPixel> destinationGroup)
where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel<TPixel> where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel<TPixel>

5
tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

@ -661,7 +661,8 @@ namespace SixLabors.ImageSharp.Tests
/// Loads the expected image with a reference decoder + compares it to <paramref name="image"/>. /// Loads the expected image with a reference decoder + compares it to <paramref name="image"/>.
/// Also performs a debug save using <see cref="ImagingTestCaseUtility.SaveTestOutputFile{TPixel}"/>. /// Also performs a debug save using <see cref="ImagingTestCaseUtility.SaveTestOutputFile{TPixel}"/>.
/// </summary> /// </summary>
internal static void VerifyEncoder<TPixel>( /// <returns>The path to the encoded output file.</returns>
internal static string VerifyEncoder<TPixel>(
this Image<TPixel> image, this Image<TPixel> image,
ITestImageProvider provider, ITestImageProvider provider,
string extension, string extension,
@ -687,6 +688,8 @@ namespace SixLabors.ImageSharp.Tests
ImageComparer comparer = customComparer ?? ImageComparer.Exact; ImageComparer comparer = customComparer ?? ImageComparer.Exact;
comparer.VerifySimilarity(encodedImage, image); comparer.VerifySimilarity(encodedImage, image);
} }
return actualOutputFile;
} }
internal static AllocatorBufferCapacityConfigurator LimitAllocatorBufferCapacity<TPixel>( internal static AllocatorBufferCapacityConfigurator LimitAllocatorBufferCapacity<TPixel>(

Loading…
Cancel
Save