@@ -135,7 +135,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
135
135
byte backgroundIndex = unchecked ( ( byte ) transparencyIndex ) ;
136
136
if ( transparencyIndex == - 1 )
137
137
{
138
- backgroundIndex = gifMetadata . BackgroundColor ;
138
+ backgroundIndex = gifMetadata . BackgroundColorIndex ;
139
139
}
140
140
141
141
// Get the number of bits.
@@ -236,18 +236,19 @@ private void EncodeFirstFrame<TPixel>(
236
236
{
237
237
this . WriteGraphicalControlExtension ( metadata , transparencyIndex , stream ) ;
238
238
239
- Buffer2DRegion < byte > region = ( ( IPixelSource ) quantized ) . PixelBuffer . GetRegion ( ) ;
239
+ Buffer2D < byte > indices = ( ( IPixelSource ) quantized ) . PixelBuffer ;
240
+ Rectangle interest = indices . FullRectangle ( ) ;
240
241
bool useLocal = this . colorTableMode == GifColorTableMode . Local || ( metadata ? . ColorTableMode == GifColorTableMode . Local ) ;
241
242
int bitDepth = ColorNumerics . GetBitsNeededForColorDepth ( quantized . Palette . Length ) ;
242
243
243
- this . WriteImageDescriptor ( region . Rectangle , useLocal , bitDepth , stream ) ;
244
+ this . WriteImageDescriptor ( interest , useLocal , bitDepth , stream ) ;
244
245
245
246
if ( useLocal )
246
247
{
247
248
this . WriteColorTable ( quantized , bitDepth , stream ) ;
248
249
}
249
250
250
- this . WriteImageData ( region , stream , quantized . Palette . Length , transparencyIndex ) ;
251
+ this . WriteImageData ( indices , interest , stream , quantized . Palette . Length , transparencyIndex ) ;
251
252
}
252
253
253
254
private void EncodeAdditionalFrame < TPixel > (
@@ -322,20 +323,20 @@ private void EncodeAdditionalFrame<TPixel>(
322
323
transparencyIndex = GetTransparentIndex ( quantized , metadata ) ;
323
324
324
325
// Trim down the buffer to the minimum size required.
325
- // Buffer2DRegion <byte> region = ((IPixelSource)quantized).PixelBuffer.GetRegion() ;
326
- Buffer2DRegion < byte > region = TrimTransparentPixels ( ( ( IPixelSource ) quantized ) . PixelBuffer , transparencyIndex ) ;
326
+ Buffer2D < byte > indices = ( ( IPixelSource ) quantized ) . PixelBuffer ;
327
+ Rectangle interest = TrimTransparentPixels ( indices , transparencyIndex ) ;
327
328
328
329
this . WriteGraphicalControlExtension ( metadata , transparencyIndex , stream ) ;
329
330
330
331
int bitDepth = ColorNumerics . GetBitsNeededForColorDepth ( quantized . Palette . Length ) ;
331
- this . WriteImageDescriptor ( region . Rectangle , useLocal , bitDepth , stream ) ;
332
+ this . WriteImageDescriptor ( interest , useLocal , bitDepth , stream ) ;
332
333
333
334
if ( useLocal )
334
335
{
335
336
this . WriteColorTable ( quantized , bitDepth , stream ) ;
336
337
}
337
338
338
- this . WriteImageData ( region , stream , quantized . Palette . Length , transparencyIndex ) ;
339
+ this . WriteImageData ( indices , interest , stream , quantized . Palette . Length , transparencyIndex ) ;
339
340
}
340
341
341
342
private void DeDuplicatePixels < TPixel > (
@@ -399,11 +400,11 @@ private void DeDuplicatePixels<TPixel>(
399
400
}
400
401
}
401
402
402
- private static Buffer2DRegion < byte > TrimTransparentPixels ( Buffer2D < byte > buffer , int transparencyIndex )
403
+ private static Rectangle TrimTransparentPixels ( Buffer2D < byte > buffer , int transparencyIndex )
403
404
{
404
405
if ( transparencyIndex < 0 )
405
406
{
406
- return buffer . GetRegion ( ) ;
407
+ return buffer . FullRectangle ( ) ;
407
408
}
408
409
409
410
byte trimmableIndex = unchecked ( ( byte ) transparencyIndex ) ;
@@ -596,7 +597,7 @@ private static Buffer2DRegion<byte> TrimTransparentPixels(Buffer2D<byte> buffer,
596
597
if ( top == bottom || left == right )
597
598
{
598
599
// The entire image is transparent.
599
- return buffer . GetRegion ( ) ;
600
+ return buffer . FullRectangle ( ) ;
600
601
}
601
602
602
603
if ( ! isTransparentRow )
@@ -605,7 +606,7 @@ private static Buffer2DRegion<byte> TrimTransparentPixels(Buffer2D<byte> buffer,
605
606
bottom = buffer . Height ;
606
607
}
607
608
608
- return buffer . GetRegion ( Rectangle . FromLTRB ( left , top , Math . Min ( right + 1 , buffer . Width ) , Math . Min ( bottom + 1 , buffer . Height ) ) ) ;
609
+ return Rectangle . FromLTRB ( left , top , Math . Min ( right + 1 , buffer . Width ) , Math . Min ( bottom + 1 , buffer . Height ) ) ;
609
610
}
610
611
611
612
/// <summary>
@@ -923,11 +924,14 @@ private void WriteColorTable<TPixel>(IndexedImageFrame<TPixel> image, int bitDep
923
924
/// Writes the image pixel data to the stream.
924
925
/// </summary>
925
926
/// <param name="indices">The <see cref="Buffer2DRegion{Byte}"/> containing indexed pixels.</param>
927
+ /// <param name="interest">The region of interest.</param>
926
928
/// <param name="stream">The stream to write to.</param>
927
929
/// <param name="paletteLength">The length of the frame color palette.</param>
928
930
/// <param name="transparencyIndex">The index of the color used to represent transparency.</param>
929
- private void WriteImageData ( Buffer2DRegion < byte > indices , Stream stream , int paletteLength , int transparencyIndex )
931
+ private void WriteImageData ( Buffer2D < byte > indices , Rectangle interest , Stream stream , int paletteLength , int transparencyIndex )
930
932
{
933
+ Buffer2DRegion < byte > region = indices . GetRegion ( interest ) ;
934
+
931
935
// Pad the bit depth when required for encoding the image data.
932
936
// This is a common trick which allows to use out of range indexes for transparency and avoid allocating a larger color palette
933
937
// as decoders skip indexes that are out of range.
@@ -936,6 +940,6 @@ private void WriteImageData(Buffer2DRegion<byte> indices, Stream stream, int pal
936
940
: 0 ;
937
941
938
942
using LzwEncoder encoder = new ( this . memoryAllocator , ColorNumerics . GetBitsNeededForColorDepth ( paletteLength + padding ) ) ;
939
- encoder . Encode ( indices , stream ) ;
943
+ encoder . Encode ( region , stream ) ;
940
944
}
941
945
}
0 commit comments