Skip to content

Commit 302a23f

Browse files
authored
Merge pull request #2412 from SixLabors/bp/Issue2411
Webp: Dont use using statement for encodedAlphaData
2 parents 0a1f05b + db3d748 commit 302a23f

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

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

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -345,29 +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-
if (hasAlpha)
353-
{
354-
// TODO: This can potentially run in an separate task.
355-
using IMemoryOwner<byte> encodedAlphaData = AlphaEncoder.EncodeAlpha(
356-
image,
357-
this.configuration,
358-
this.memoryAllocator,
359-
this.skipMetadata,
360-
this.alphaCompression,
361-
out alphaDataSize);
362-
363-
alphaData = encodedAlphaData.GetSpan();
364-
if (alphaDataSize < pixelCount)
365-
{
366-
// Only use compressed data, if the compressed data is actually smaller then the uncompressed data.
367-
alphaCompressionSucceeded = true;
368-
}
369-
}
370-
371348
// Stats-collection loop.
372349
this.StatLoop(width, height, yStride, uvStride);
373350
it.Init();
@@ -405,16 +382,47 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
405382
ExifProfile exifProfile = this.skipMetadata ? null : metadata.ExifProfile;
406383
XmpProfile xmpProfile = this.skipMetadata ? null : metadata.XmpProfile;
407384

408-
this.bitWriter.WriteEncodedImageToStream(
409-
stream,
410-
exifProfile,
411-
xmpProfile,
412-
metadata.IccProfile,
413-
(uint)width,
414-
(uint)height,
415-
hasAlpha,
416-
alphaData[..alphaDataSize],
417-
this.alphaCompression && alphaCompressionSucceeded);
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;
390+
try
391+
{
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+
411+
this.bitWriter.WriteEncodedImageToStream(
412+
stream,
413+
exifProfile,
414+
xmpProfile,
415+
metadata.IccProfile,
416+
(uint)width,
417+
(uint)height,
418+
hasAlpha,
419+
alphaData[..alphaDataSize],
420+
this.alphaCompression && alphaCompressionSucceeded);
421+
}
422+
finally
423+
{
424+
encodedAlphaData?.Dispose();
425+
}
418426
}
419427

420428
/// <inheritdoc/>

0 commit comments

Comments
 (0)