Skip to content

Commit b12ad75

Browse files
committed
Leave alpha data uncompressed, if compression does not yield in smaller data
1 parent cf672b9 commit b12ad75

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,15 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
264264
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
265265
/// <param name="image">The <see cref="Image{TPixel}"/> to encode from.</param>
266266
/// <param name="alphaData">The destination buffer to write the encoded alpha data to.</param>
267-
/// <returns>The size of the data in bytes.</returns>
267+
/// <returns>The size of the compressed data in bytes.
268+
/// 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.
269+
/// </returns>
268270
public int EncodeAlphaImageData<TPixel>(Image<TPixel> image, IMemoryOwner<byte> alphaData)
269271
where TPixel : unmanaged, IPixel<TPixel>
270272
{
271273
int width = image.Width;
272274
int height = image.Height;
275+
int pixelCount = width * height;
273276

274277
// Convert image pixels to bgra array.
275278
this.ConvertPixelsToBgra(image, width, height);
@@ -278,6 +281,12 @@ public int EncodeAlphaImageData<TPixel>(Image<TPixel> image, IMemoryOwner<byte>
278281
this.EncodeStream(image);
279282
this.bitWriter.Finish();
280283
int size = this.bitWriter.NumBytes();
284+
if (size >= pixelCount)
285+
{
286+
// Compressing would not yield in smaller data -> leave the data uncompressed.
287+
return pixelCount;
288+
}
289+
281290
this.bitWriter.WriteToBuffer(alphaData.GetSpan());
282291
return size;
283292
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
302302
{
303303
int width = image.Width;
304304
int height = image.Height;
305+
int pixelCount = width * height;
305306
Span<byte> y = this.Y.GetSpan();
306307
Span<byte> u = this.U.GetSpan();
307308
Span<byte> v = this.V.GetSpan();
@@ -329,10 +330,16 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
329330

330331
// Extract and encode alpha channel data, if present.
331332
int alphaDataSize = 0;
333+
bool alphaCompressionSucceeded = false;
332334
if (hasAlpha)
333335
{
334336
// TODO: This can potentially run in an separate task.
335337
this.AlphaData = AlphaEncoder.EncodeAlpha(image, this.configuration, this.memoryAllocator, this.alphaCompression, out alphaDataSize);
338+
if (alphaDataSize < pixelCount)
339+
{
340+
// Only use compressed data, if the compressed data is actually smaller then the uncompressed data.
341+
alphaCompressionSucceeded = true;
342+
}
336343
}
337344

338345
// Stats-collection loop.
@@ -376,7 +383,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
376383
(uint)height,
377384
hasAlpha,
378385
hasAlpha ? this.AlphaData.GetSpan().Slice(0, alphaDataSize) : Span<byte>.Empty,
379-
this.alphaCompression);
386+
this.alphaCompression && alphaCompressionSucceeded);
380387
}
381388

382389
/// <inheritdoc/>

0 commit comments

Comments
 (0)