@@ -135,13 +135,15 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
135
135
136
136
// Write the LSD.
137
137
image . Frames . RootFrame . Metadata . TryGetGifMetadata ( out GifFrameMetadata ? frameMetadata ) ;
138
- int index = GetTransparentIndex ( quantized , frameMetadata ) ;
139
- if ( index == - 1 )
138
+
139
+ int transparentIndex = GetTransparentIndex ( quantized , frameMetadata ) ;
140
+ byte backgroundIndex = unchecked ( ( byte ) transparentIndex ) ;
141
+ if ( transparentIndex == - 1 )
140
142
{
141
- index = gifMetadata . BackgroundColor ;
143
+ backgroundIndex = gifMetadata . BackgroundColor ;
142
144
}
143
145
144
- this . WriteLogicalScreenDescriptor ( metadata , image . Width , image . Height , index , useGlobalTable , stream ) ;
146
+ this . WriteLogicalScreenDescriptor ( metadata , image . Width , image . Height , backgroundIndex , useGlobalTable , stream ) ;
145
147
146
148
if ( useGlobalTable )
147
149
{
@@ -158,14 +160,15 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
158
160
this . WriteApplicationExtensions ( stream , image . Frames . Count , gifMetadata . RepeatCount , xmpProfile ) ;
159
161
}
160
162
161
- this . EncodeFrames ( stream , image , quantized , quantized . Palette . ToArray ( ) ) ;
163
+ this . EncodeFrames ( stream , image , backgroundIndex , quantized , quantized . Palette . ToArray ( ) ) ;
162
164
163
165
stream . WriteByte ( GifConstants . EndIntroducer ) ;
164
166
}
165
167
166
168
private void EncodeFrames < TPixel > (
167
169
Stream stream ,
168
170
Image < TPixel > image ,
171
+ byte backgroundIndex ,
169
172
IndexedImageFrame < TPixel > quantized ,
170
173
ReadOnlyMemory < TPixel > palette )
171
174
where TPixel : unmanaged, IPixel < TPixel >
@@ -197,7 +200,17 @@ private void EncodeFrames<TPixel>(
197
200
paletteQuantizer = new ( this . configuration , this . quantizer ! . Options , palette ) ;
198
201
}
199
202
200
- this . EncodeFrame ( stream , frame , i , useLocal , frameMetadata , indices , ref previousQuantized , ref quantized ! , ref paletteQuantizer ) ;
203
+ this . EncodeFrame (
204
+ stream ,
205
+ frame ,
206
+ i ,
207
+ useLocal ,
208
+ frameMetadata ,
209
+ indices ,
210
+ backgroundIndex ,
211
+ ref previousQuantized ,
212
+ ref quantized ! ,
213
+ ref paletteQuantizer ) ;
201
214
202
215
// Clean up for the next run.
203
216
if ( quantized != previousQuantized )
@@ -222,6 +235,7 @@ private void EncodeFrame<TPixel>(
222
235
bool useLocal ,
223
236
GifFrameMetadata ? metadata ,
224
237
Buffer2D < byte > indices ,
238
+ byte backgroundIndex ,
225
239
ref IndexedImageFrame < TPixel > previousQuantized ,
226
240
ref IndexedImageFrame < TPixel > quantized ,
227
241
ref PaletteQuantizer < TPixel > globalPaletteQuantizer )
@@ -256,10 +270,16 @@ private void EncodeFrame<TPixel>(
256
270
quantized = globalPaletteQuantizer . QuantizeFrame ( frame , frame . Bounds ( ) ) ;
257
271
transparencyIndex = GetTransparentIndex ( quantized , metadata ) ;
258
272
273
+ byte replacementIndex = unchecked ( ( byte ) transparencyIndex ) ;
274
+ if ( transparencyIndex == - 1 )
275
+ {
276
+ replacementIndex = backgroundIndex ;
277
+ }
278
+
259
279
// De-duplicate pixels comparing to the previous frame.
260
280
// Only global is supported for now as the color palettes as the operation required to compare
261
281
// and offset the index lookups is too expensive for local palettes.
262
- DeDuplicatePixels ( previousQuantized , quantized , indices , transparencyIndex ) ;
282
+ DeDuplicatePixels ( previousQuantized , quantized , indices , replacementIndex ) ;
263
283
}
264
284
265
285
this . bitDepth = ColorNumerics . GetBitsNeededForColorDepth ( quantized . Palette . Length ) ;
@@ -290,11 +310,9 @@ private static void DeDuplicatePixels<TPixel>(
290
310
IndexedImageFrame < TPixel > background ,
291
311
IndexedImageFrame < TPixel > source ,
292
312
Buffer2D < byte > indices ,
293
- int transparencyIndex )
313
+ byte replacementIndex )
294
314
where TPixel : unmanaged, IPixel < TPixel >
295
315
{
296
- // TODO: This should be the background color if not transparent.
297
- byte replacementIndex = unchecked ( ( byte ) transparencyIndex ) ;
298
316
for ( int y = 0 ; y < background . Height ; y ++ )
299
317
{
300
318
ref byte backgroundRowBase = ref MemoryMarshal . GetReference ( background . DangerousGetRowSpan ( y ) ) ;
@@ -421,7 +439,7 @@ private void WriteLogicalScreenDescriptor(
421
439
ImageMetadata metadata ,
422
440
int width ,
423
441
int height ,
424
- int backgroundIndex ,
442
+ byte backgroundIndex ,
425
443
bool useGlobalTable ,
426
444
Stream stream )
427
445
{
@@ -459,7 +477,7 @@ private void WriteLogicalScreenDescriptor(
459
477
width : ( ushort ) width ,
460
478
height : ( ushort ) height ,
461
479
packed : packedValue ,
462
- backgroundColorIndex : unchecked ( ( byte ) backgroundIndex ) ,
480
+ backgroundColorIndex : backgroundIndex ,
463
481
ratio ) ;
464
482
465
483
Span < byte > buffer = stackalloc byte [ 20 ] ;
0 commit comments