@@ -345,30 +345,6 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
345
345
int expectedSize = this . Mbw * this . Mbh * averageBytesPerMacroBlock ;
346
346
this . bitWriter = new Vp8BitWriter ( expectedSize , this ) ;
347
347
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
-
372
348
// Stats-collection loop.
373
349
this . StatLoop ( width , height , yStride , uvStride ) ;
374
350
it . Init ( ) ;
@@ -406,8 +382,32 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
406
382
ExifProfile exifProfile = this . skipMetadata ? null : metadata . ExifProfile ;
407
383
XmpProfile xmpProfile = this . skipMetadata ? null : metadata . XmpProfile ;
408
384
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 ;
409
390
try
410
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
411
this . bitWriter . WriteEncodedImageToStream (
412
412
stream ,
413
413
exifProfile ,
0 commit comments