Skip to content

Commit db3d748

Browse files
committed
Move EncodeAlpha into try catch block
1 parent 8441b23 commit db3d748

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -345,30 +345,6 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
345345
int expectedSize = this.Mbw * this.Mbh * averageBytesPerMacroBlock;
346346
this.bitWriter = new Vp8BitWriter(expectedSize, this);
347347

348-
// Extract and encode alpha channel data, if present.
349-
int alphaDataSize = 0;
350-
bool alphaCompressionSucceeded = false;
351-
Span<byte> alphaData = Span<byte>.Empty;
352-
IMemoryOwner<byte> encodedAlphaData = null;
353-
if (hasAlpha)
354-
{
355-
// TODO: This can potentially run in an separate task.
356-
encodedAlphaData = AlphaEncoder.EncodeAlpha(
357-
image,
358-
this.configuration,
359-
this.memoryAllocator,
360-
this.skipMetadata,
361-
this.alphaCompression,
362-
out alphaDataSize);
363-
364-
alphaData = encodedAlphaData.GetSpan();
365-
if (alphaDataSize < pixelCount)
366-
{
367-
// Only use compressed data, if the compressed data is actually smaller then the uncompressed data.
368-
alphaCompressionSucceeded = true;
369-
}
370-
}
371-
372348
// Stats-collection loop.
373349
this.StatLoop(width, height, yStride, uvStride);
374350
it.Init();
@@ -406,8 +382,32 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
406382
ExifProfile exifProfile = this.skipMetadata ? null : metadata.ExifProfile;
407383
XmpProfile xmpProfile = this.skipMetadata ? null : metadata.XmpProfile;
408384

385+
// Extract and encode alpha channel data, if present.
386+
int alphaDataSize = 0;
387+
bool alphaCompressionSucceeded = false;
388+
Span<byte> alphaData = Span<byte>.Empty;
389+
IMemoryOwner<byte> encodedAlphaData = null;
409390
try
410391
{
392+
if (hasAlpha)
393+
{
394+
// TODO: This can potentially run in an separate task.
395+
encodedAlphaData = AlphaEncoder.EncodeAlpha(
396+
image,
397+
this.configuration,
398+
this.memoryAllocator,
399+
this.skipMetadata,
400+
this.alphaCompression,
401+
out alphaDataSize);
402+
403+
alphaData = encodedAlphaData.GetSpan();
404+
if (alphaDataSize < pixelCount)
405+
{
406+
// Only use compressed data, if the compressed data is actually smaller then the uncompressed data.
407+
alphaCompressionSucceeded = true;
408+
}
409+
}
410+
411411
this.bitWriter.WriteEncodedImageToStream(
412412
stream,
413413
exifProfile,

0 commit comments

Comments
 (0)