@@ -329,60 +329,68 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
329
329
bool alphaCompressionSucceeded = false ;
330
330
using var alphaEncoder = new AlphaEncoder ( ) ;
331
331
Span < byte > alphaData = Span < byte > . Empty ;
332
- if ( hasAlpha )
332
+ IMemoryOwner < byte > encodedAlphaData = null ;
333
+ try
333
334
{
334
- // TODO: This can potentially run in an separate task.
335
- IMemoryOwner < byte > encodedAlphaData = alphaEncoder . EncodeAlpha ( image , this . configuration , this . memoryAllocator , this . alphaCompression , out alphaDataSize ) ;
336
- alphaData = encodedAlphaData . GetSpan ( ) ;
337
- if ( alphaDataSize < pixelCount )
335
+ if ( hasAlpha )
338
336
{
339
- // Only use compressed data, if the compressed data is actually smaller then the uncompressed data.
340
- alphaCompressionSucceeded = true ;
337
+ // TODO: This can potentially run in an separate task.
338
+ encodedAlphaData = alphaEncoder . EncodeAlpha ( image , this . configuration , this . memoryAllocator , this . alphaCompression , out alphaDataSize ) ;
339
+ alphaData = encodedAlphaData . GetSpan ( ) ;
340
+ if ( alphaDataSize < pixelCount )
341
+ {
342
+ // Only use compressed data, if the compressed data is actually smaller then the uncompressed data.
343
+ alphaCompressionSucceeded = true ;
344
+ }
341
345
}
342
- }
343
346
344
- // Stats-collection loop.
345
- this . StatLoop ( width , height , yStride , uvStride ) ;
346
- it . Init ( ) ;
347
- it . InitFilter ( ) ;
348
- var info = new Vp8ModeScore ( ) ;
349
- var residual = new Vp8Residual ( ) ;
350
- do
351
- {
352
- bool dontUseSkip = ! this . Proba . UseSkipProba ;
353
- info . Clear ( ) ;
354
- it . Import ( y , u , v , yStride , uvStride , width , height , false ) ;
355
-
356
- // Warning! order is important: first call VP8Decimate() and
357
- // *then* decide how to code the skip decision if there's one.
358
- if ( ! this . Decimate ( it , ref info , this . rdOptLevel ) || dontUseSkip )
359
- {
360
- this . CodeResiduals ( it , info , residual ) ;
361
- }
362
- else
347
+ // Stats-collection loop.
348
+ this . StatLoop ( width , height , yStride , uvStride ) ;
349
+ it . Init ( ) ;
350
+ it . InitFilter ( ) ;
351
+ var info = new Vp8ModeScore ( ) ;
352
+ var residual = new Vp8Residual ( ) ;
353
+ do
363
354
{
364
- it . ResetAfterSkip ( ) ;
355
+ bool dontUseSkip = ! this . Proba . UseSkipProba ;
356
+ info . Clear ( ) ;
357
+ it . Import ( y , u , v , yStride , uvStride , width , height , false ) ;
358
+
359
+ // Warning! order is important: first call VP8Decimate() and
360
+ // *then* decide how to code the skip decision if there's one.
361
+ if ( ! this . Decimate ( it , ref info , this . rdOptLevel ) || dontUseSkip )
362
+ {
363
+ this . CodeResiduals ( it , info , residual ) ;
364
+ }
365
+ else
366
+ {
367
+ it . ResetAfterSkip ( ) ;
368
+ }
369
+
370
+ it . SaveBoundary ( ) ;
365
371
}
372
+ while ( it . Next ( ) ) ;
366
373
367
- it . SaveBoundary ( ) ;
374
+ // Store filter stats.
375
+ this . AdjustFilterStrength ( ) ;
376
+
377
+ // Write bytes from the bitwriter buffer to the stream.
378
+ ImageMetadata metadata = image . Metadata ;
379
+ metadata . SyncProfiles ( ) ;
380
+ this . bitWriter . WriteEncodedImageToStream (
381
+ stream ,
382
+ metadata . ExifProfile ,
383
+ metadata . XmpProfile ,
384
+ ( uint ) width ,
385
+ ( uint ) height ,
386
+ hasAlpha ,
387
+ alphaData . Slice ( 0 , alphaDataSize ) ,
388
+ this . alphaCompression && alphaCompressionSucceeded ) ;
389
+ }
390
+ finally
391
+ {
392
+ encodedAlphaData ? . Dispose ( ) ;
368
393
}
369
- while ( it . Next ( ) ) ;
370
-
371
- // Store filter stats.
372
- this . AdjustFilterStrength ( ) ;
373
-
374
- // Write bytes from the bitwriter buffer to the stream.
375
- ImageMetadata metadata = image . Metadata ;
376
- metadata . SyncProfiles ( ) ;
377
- this . bitWriter . WriteEncodedImageToStream (
378
- stream ,
379
- metadata . ExifProfile ,
380
- metadata . XmpProfile ,
381
- ( uint ) width ,
382
- ( uint ) height ,
383
- hasAlpha ,
384
- alphaData ,
385
- this . alphaCompression && alphaCompressionSucceeded ) ;
386
394
}
387
395
388
396
/// <inheritdoc/>
0 commit comments