diff --git a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs
index ece9aefd0f..30d65562ae 100644
--- a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs
+++ b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs
@@ -264,12 +264,15 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
/// The type of the pixel.
/// The to encode from.
/// The destination buffer to write the encoded alpha data to.
- /// The size of the data in bytes.
+ /// The size of the compressed data in bytes.
+ /// If the size of the data is the same as the pixel count, the compression would not yield in smaller data and is left uncompressed.
+ ///
public int EncodeAlphaImageData(Image image, IMemoryOwner alphaData)
where TPixel : unmanaged, IPixel
{
int width = image.Width;
int height = image.Height;
+ int pixelCount = width * height;
// Convert image pixels to bgra array.
this.ConvertPixelsToBgra(image, width, height);
@@ -278,6 +281,12 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
this.EncodeStream(image);
this.bitWriter.Finish();
int size = this.bitWriter.NumBytes();
+ if (size >= pixelCount)
+ {
+ // Compressing would not yield in smaller data -> leave the data uncompressed.
+ return pixelCount;
+ }
+
this.bitWriter.WriteToBuffer(alphaData.GetSpan());
return size;
}
diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs
index 4b7f3f5c88..60bdee362c 100644
--- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs
+++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs
@@ -302,6 +302,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy
{
int width = image.Width;
int height = image.Height;
+ int pixelCount = width * height;
Span y = this.Y.GetSpan();
Span u = this.U.GetSpan();
Span v = this.V.GetSpan();
@@ -329,10 +330,16 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy
// Extract and encode alpha channel data, if present.
int alphaDataSize = 0;
+ bool alphaCompressionSucceeded = false;
if (hasAlpha)
{
// TODO: This can potentially run in an separate task.
this.AlphaData = AlphaEncoder.EncodeAlpha(image, this.configuration, this.memoryAllocator, this.alphaCompression, out alphaDataSize);
+ if (alphaDataSize < pixelCount)
+ {
+ // Only use compressed data, if the compressed data is actually smaller then the uncompressed data.
+ alphaCompressionSucceeded = true;
+ }
}
// Stats-collection loop.
@@ -376,7 +383,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy
(uint)height,
hasAlpha,
hasAlpha ? this.AlphaData.GetSpan().Slice(0, alphaDataSize) : Span.Empty,
- this.alphaCompression);
+ this.alphaCompression && alphaCompressionSucceeded);
}
///