Skip to content

Commit 1db6b21

Browse files
Merge pull request #2173 from SixLabors/bp/Issue2171
Fix issue when writing lossy webp with alpha data
2 parents 4c822ad + a21c90e commit 1db6b21

File tree

6 files changed

+23
-9
lines changed

6 files changed

+23
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
382382
(uint)width,
383383
(uint)height,
384384
hasAlpha,
385-
alphaData,
385+
alphaData.Slice(0, alphaDataSize),
386386
this.alphaCompression && alphaCompressionSucceeded);
387387
}
388388

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
using SixLabors.ImageSharp.Tests.TestUtilities;
88
using Xunit;
99

10-
namespace SixLabors.ImageSharp.Tests.Formats.WebP
10+
namespace SixLabors.ImageSharp.Tests.Formats.Webp
1111
{
12+
[Trait("Format", "Webp")]
1213
public class Vp8LHistogramTests
1314
{
1415
private static void RunAddVectorTest()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using SixLabors.ImageSharp.Tests.TestUtilities;
66
using Xunit;
77

8-
namespace SixLabors.ImageSharp.Tests.Formats.WebP
8+
namespace SixLabors.ImageSharp.Tests.Formats.Webp
99
{
1010
[Trait("Format", "Webp")]
1111
public class Vp8ResidualTests

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using SixLabors.ImageSharp.PixelFormats;
88
using SixLabors.ImageSharp.Tests.TestUtilities;
99
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
10+
using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs;
1011
using Xunit;
1112
using static SixLabors.ImageSharp.Tests.TestImages.Webp;
1213

@@ -268,9 +269,9 @@ public void Encode_Lossy_WithDifferentMethodsAndQuality_Works<TPixel>(TestImageP
268269
}
269270

270271
[Theory]
271-
[WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, false)]
272-
[WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, true)]
273-
public void Encode_Lossy_WithAlpha_Works<TPixel>(TestImageProvider<TPixel> provider, bool compressed)
272+
[WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, false, 64020)]
273+
[WithFile(TestImages.Png.Transparency, PixelTypes.Rgba32, true, 16200)]
274+
public void Encode_Lossy_WithAlpha_Works<TPixel>(TestImageProvider<TPixel> provider, bool compressed, int expectedFileSize)
274275
where TPixel : unmanaged, IPixel<TPixel>
275276
{
276277
var encoder = new WebpEncoder()
@@ -280,7 +281,16 @@ public void Encode_Lossy_WithAlpha_Works<TPixel>(TestImageProvider<TPixel> provi
280281
};
281282

282283
using Image<TPixel> image = provider.GetImage();
283-
image.VerifyEncoder(provider, "webp", $"with_alpha_compressed_{compressed}", encoder, ImageComparer.Tolerant(0.04f));
284+
string encodedFile = image.VerifyEncoder(
285+
provider,
286+
"webp",
287+
$"with_alpha_compressed_{compressed}",
288+
encoder,
289+
ImageComparer.Tolerant(0.04f),
290+
referenceDecoder: new MagickReferenceDecoder());
291+
292+
int encodedBytes = File.ReadAllBytes(encodedFile).Length;
293+
Assert.True(encodedBytes <= expectedFileSize);
284294
}
285295

286296
[Theory]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public MagickReferenceDecoder()
2626

2727
public MagickReferenceDecoder(bool validate) => this.validate = validate;
2828

29-
public static MagickReferenceDecoder Instance { get; } = new MagickReferenceDecoder();
29+
public static MagickReferenceDecoder Instance { get; } = new();
3030

3131
private static void FromRgba32Bytes<TPixel>(Configuration configuration, Span<byte> rgbaBytes, IMemoryGroup<TPixel> destinationGroup)
3232
where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel<TPixel>

tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,8 @@ internal static void VerifyOperation<TPixel>(
661661
/// Loads the expected image with a reference decoder + compares it to <paramref name="image"/>.
662662
/// Also performs a debug save using <see cref="ImagingTestCaseUtility.SaveTestOutputFile{TPixel}"/>.
663663
/// </summary>
664-
internal static void VerifyEncoder<TPixel>(
664+
/// <returns>The path to the encoded output file.</returns>
665+
internal static string VerifyEncoder<TPixel>(
665666
this Image<TPixel> image,
666667
ITestImageProvider provider,
667668
string extension,
@@ -687,6 +688,8 @@ internal static void VerifyEncoder<TPixel>(
687688
ImageComparer comparer = customComparer ?? ImageComparer.Exact;
688689
comparer.VerifySimilarity(encodedImage, image);
689690
}
691+
692+
return actualOutputFile;
690693
}
691694

692695
internal static AllocatorBufferCapacityConfigurator LimitAllocatorBufferCapacity<TPixel>(

0 commit comments

Comments
 (0)