Skip to content

Commit 1a6b465

Browse files
Cleanup based on feedback
1 parent c0ca0cc commit 1a6b465

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/ImageSharp/Formats/Gif/GifEncoderCore.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
135135
byte backgroundIndex = unchecked((byte)transparencyIndex);
136136
if (transparencyIndex == -1)
137137
{
138-
backgroundIndex = gifMetadata.BackgroundColor;
138+
backgroundIndex = gifMetadata.BackgroundColorIndex;
139139
}
140140

141141
// Get the number of bits.
@@ -236,18 +236,19 @@ private void EncodeFirstFrame<TPixel>(
236236
{
237237
this.WriteGraphicalControlExtension(metadata, transparencyIndex, stream);
238238

239-
Buffer2DRegion<byte> region = ((IPixelSource)quantized).PixelBuffer.GetRegion();
239+
Buffer2D<byte> indices = ((IPixelSource)quantized).PixelBuffer;
240+
Rectangle interest = indices.FullRectangle();
240241
bool useLocal = this.colorTableMode == GifColorTableMode.Local || (metadata?.ColorTableMode == GifColorTableMode.Local);
241242
int bitDepth = ColorNumerics.GetBitsNeededForColorDepth(quantized.Palette.Length);
242243

243-
this.WriteImageDescriptor(region.Rectangle, useLocal, bitDepth, stream);
244+
this.WriteImageDescriptor(interest, useLocal, bitDepth, stream);
244245

245246
if (useLocal)
246247
{
247248
this.WriteColorTable(quantized, bitDepth, stream);
248249
}
249250

250-
this.WriteImageData(region, stream, quantized.Palette.Length, transparencyIndex);
251+
this.WriteImageData(indices, interest, stream, quantized.Palette.Length, transparencyIndex);
251252
}
252253

253254
private void EncodeAdditionalFrame<TPixel>(
@@ -322,20 +323,20 @@ private void EncodeAdditionalFrame<TPixel>(
322323
transparencyIndex = GetTransparentIndex(quantized, metadata);
323324

324325
// 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);
327328

328329
this.WriteGraphicalControlExtension(metadata, transparencyIndex, stream);
329330

330331
int bitDepth = ColorNumerics.GetBitsNeededForColorDepth(quantized.Palette.Length);
331-
this.WriteImageDescriptor(region.Rectangle, useLocal, bitDepth, stream);
332+
this.WriteImageDescriptor(interest, useLocal, bitDepth, stream);
332333

333334
if (useLocal)
334335
{
335336
this.WriteColorTable(quantized, bitDepth, stream);
336337
}
337338

338-
this.WriteImageData(region, stream, quantized.Palette.Length, transparencyIndex);
339+
this.WriteImageData(indices, interest, stream, quantized.Palette.Length, transparencyIndex);
339340
}
340341

341342
private void DeDuplicatePixels<TPixel>(
@@ -399,11 +400,11 @@ private void DeDuplicatePixels<TPixel>(
399400
}
400401
}
401402

402-
private static Buffer2DRegion<byte> TrimTransparentPixels(Buffer2D<byte> buffer, int transparencyIndex)
403+
private static Rectangle TrimTransparentPixels(Buffer2D<byte> buffer, int transparencyIndex)
403404
{
404405
if (transparencyIndex < 0)
405406
{
406-
return buffer.GetRegion();
407+
return buffer.FullRectangle();
407408
}
408409

409410
byte trimmableIndex = unchecked((byte)transparencyIndex);
@@ -596,7 +597,7 @@ private static Buffer2DRegion<byte> TrimTransparentPixels(Buffer2D<byte> buffer,
596597
if (top == bottom || left == right)
597598
{
598599
// The entire image is transparent.
599-
return buffer.GetRegion();
600+
return buffer.FullRectangle();
600601
}
601602

602603
if (!isTransparentRow)
@@ -605,7 +606,7 @@ private static Buffer2DRegion<byte> TrimTransparentPixels(Buffer2D<byte> buffer,
605606
bottom = buffer.Height;
606607
}
607608

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));
609610
}
610611

611612
/// <summary>
@@ -923,11 +924,14 @@ private void WriteColorTable<TPixel>(IndexedImageFrame<TPixel> image, int bitDep
923924
/// Writes the image pixel data to the stream.
924925
/// </summary>
925926
/// <param name="indices">The <see cref="Buffer2DRegion{Byte}"/> containing indexed pixels.</param>
927+
/// <param name="interest">The region of interest.</param>
926928
/// <param name="stream">The stream to write to.</param>
927929
/// <param name="paletteLength">The length of the frame color palette.</param>
928930
/// <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)
930932
{
933+
Buffer2DRegion<byte> region = indices.GetRegion(interest);
934+
931935
// Pad the bit depth when required for encoding the image data.
932936
// This is a common trick which allows to use out of range indexes for transparency and avoid allocating a larger color palette
933937
// as decoders skip indexes that are out of range.
@@ -936,6 +940,6 @@ private void WriteImageData(Buffer2DRegion<byte> indices, Stream stream, int pal
936940
: 0;
937941

938942
using LzwEncoder encoder = new(this.memoryAllocator, ColorNumerics.GetBitsNeededForColorDepth(paletteLength + padding));
939-
encoder.Encode(indices, stream);
943+
encoder.Encode(region, stream);
940944
}
941945
}

src/ImageSharp/Formats/Gif/GifMetadata.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private GifMetadata(GifMetadata other)
2323
{
2424
this.RepeatCount = other.RepeatCount;
2525
this.ColorTableMode = other.ColorTableMode;
26-
this.BackgroundColor = other.BackgroundColor;
26+
this.BackgroundColorIndex = other.BackgroundColorIndex;
2727

2828
if (other.GlobalColorTable?.Length > 0)
2929
{
@@ -58,7 +58,7 @@ private GifMetadata(GifMetadata other)
5858
/// Gets or sets the index at the <see cref="GlobalColorTable"/> for the background color.
5959
/// The background color is the color used for those pixels on the screen that are not covered by an image.
6060
/// </summary>
61-
public byte BackgroundColor { get; set; }
61+
public byte BackgroundColorIndex { get; set; }
6262

6363
/// <summary>
6464
/// Gets or sets the collection of comments about the graphics, credits, descriptions or any

0 commit comments

Comments
 (0)