@@ -182,7 +182,8 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
182
182
if ( clearTransparency )
183
183
{
184
184
currentFrame = clonedFrame = currentFrame . Clone ( ) ;
185
- ClearTransparentPixels ( currentFrame , this . backgroundColor . Value ) ;
185
+ currentFrameRegion = currentFrame . PixelBuffer . GetRegion ( ) ;
186
+ ClearTransparentPixels ( in currentFrameRegion , this . backgroundColor . Value ) ;
186
187
}
187
188
188
189
// Do not move this. We require an accurate bit depth for the header chunk.
@@ -217,7 +218,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
217
218
{
218
219
cancellationToken . ThrowIfCancellationRequested ( ) ;
219
220
FrameControl frameControl = new ( ( uint ) this . width , ( uint ) this . height ) ;
220
- this . WriteDataChunks ( frameControl , currentFrame . PixelBuffer . GetRegion ( ) , quantized , stream , false ) ;
221
+ this . WriteDataChunks ( in frameControl , in currentFrameRegion , quantized , stream , false ) ;
221
222
currentFrameIndex ++ ;
222
223
}
223
224
@@ -286,9 +287,10 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
286
287
background ,
287
288
blend ) ;
288
289
290
+ Buffer2DRegion < TPixel > encodingFrameRegion = encodingFrame . PixelBuffer . GetRegion ( bounds ) ;
289
291
if ( clearTransparency )
290
292
{
291
- ClearTransparentPixels ( encodingFrame , background ) ;
293
+ ClearTransparentPixels ( in encodingFrameRegion , background ) ;
292
294
}
293
295
294
296
// Each frame control sequence number must be incremented by the number of frame data chunks that follow.
@@ -308,7 +310,6 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
308
310
paletteQuantizer ,
309
311
default ) ;
310
312
311
- Buffer2DRegion < TPixel > encodingFrameRegion = encodingFrame . PixelBuffer . GetRegion ( bounds ) ;
312
313
sequenceNumber += this . WriteDataChunks ( frameControl , in encodingFrameRegion , quantized , stream , true ) + 1 ;
313
314
314
315
previousFrame = currentFrame ;
@@ -392,27 +393,26 @@ private static PngFrameMetadata GetPngFrameMetadata<TPixel>(ImageFrame<TPixel> f
392
393
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
393
394
/// <param name="clone">The cloned image frame where the transparent pixels will be changed.</param>
394
395
/// <param name="color">The color to change transparent pixels to.</param>
395
- private static void ClearTransparentPixels < TPixel > ( ImageFrame < TPixel > clone , Color color )
396
+ private static void ClearTransparentPixels < TPixel > ( in Buffer2DRegion < TPixel > clone , Color color )
396
397
where TPixel : unmanaged, IPixel < TPixel >
397
- => clone . ProcessPixelRows ( accessor =>
398
+ {
399
+ Rgba32 rgba32 = default ;
400
+ Rgba32 transparent = color ;
401
+ for ( int y = 0 ; y < clone . Height ; y ++ )
398
402
{
399
- // TODO: We should be able to speed this up with SIMD and masking.
400
- Rgba32 rgba32 = default ;
401
- Rgba32 transparent = color ;
402
- for ( int y = 0 ; y < accessor . Height ; y ++ )
403
+ Span < TPixel > row = clone . DangerousGetRowSpan ( y ) ;
404
+ for ( int x = 0 ; x < row . Length ; x ++ )
403
405
{
404
- Span < TPixel > span = accessor . GetRowSpan ( y ) ;
405
- for ( int x = 0 ; x < accessor . Width ; x ++ )
406
- {
407
- span [ x ] . ToRgba32 ( ref rgba32 ) ;
406
+ ref TPixel pixel = ref row [ x ] ;
407
+ pixel . ToRgba32 ( ref rgba32 ) ;
408
408
409
- if ( rgba32 . A is 0 )
410
- {
411
- span [ x ] . FromRgba32 ( transparent ) ;
412
- }
409
+ if ( rgba32 . A is 0 )
410
+ {
411
+ pixel . FromRgba32 ( transparent ) ;
413
412
}
414
413
}
415
- } ) ;
414
+ }
415
+ }
416
416
417
417
/// <summary>
418
418
/// Creates the quantized image and calculates and sets the bit depth.
@@ -1595,7 +1595,7 @@ private void SanitizeAndSetEncoderOptions<TPixel>(
1595
1595
/// <param name="paletteQuantizer">The quantizer containing any previously derived palette.</param>
1596
1596
/// <param name="backgroundColor">The background color.</param>
1597
1597
private IndexedImageFrame < TPixel > ? CreateQuantizedFrame < TPixel > (
1598
- QuantizingImageEncoder encoder ,
1598
+ PngEncoder encoder ,
1599
1599
PngColorType colorType ,
1600
1600
byte bitDepth ,
1601
1601
PngMetadata metadata ,
@@ -1667,7 +1667,22 @@ private void SanitizeAndSetEncoderOptions<TPixel>(
1667
1667
frameQuantizer . AddPaletteColors ( px . GetRegion ( ) ) ;
1668
1668
}
1669
1669
1670
- frameQuantizer . BuildPalette ( encoder . PixelSamplingStrategy , image ) ;
1670
+ if ( encoder . TransparentColorMode == PngTransparentColorMode . Clear )
1671
+ {
1672
+ foreach ( Buffer2DRegion < TPixel > region in encoder . PixelSamplingStrategy . EnumeratePixelRegions ( image ) )
1673
+ {
1674
+ using Buffer2D < TPixel > clone = region . Buffer . CloneRegion ( this . configuration , region . Rectangle ) ;
1675
+ Buffer2DRegion < TPixel > clonedRegion = clone . GetRegion ( ) ;
1676
+
1677
+ ClearTransparentPixels ( in clonedRegion , backgroundColor ) ;
1678
+ frameQuantizer . AddPaletteColors ( clonedRegion ) ;
1679
+ }
1680
+ }
1681
+ else
1682
+ {
1683
+ frameQuantizer . BuildPalette ( encoder . PixelSamplingStrategy , image ) ;
1684
+ }
1685
+
1671
1686
return frameQuantizer . QuantizeFrame ( frame , bounds ) ;
1672
1687
}
1673
1688
0 commit comments