Browse Source

Improved performance of GifDecoder.

af/merge-core
Dirk Lemstra 10 years ago
parent
commit
5b216667aa
  1. 95
      src/ImageSharp/Formats/Gif/GifDecoderCore.cs

95
src/ImageSharp/Formats/Gif/GifDecoderCore.cs

@ -357,74 +357,59 @@ namespace ImageSharp.Formats
using (PixelAccessor<TColor> pixelAccessor = image.Lock()) using (PixelAccessor<TColor> pixelAccessor = image.Lock())
{ {
using (PixelArea<TColor> pixelRow = new PixelArea<TColor>(imageWidth, ComponentOrder.XYZW)) for (int y = descriptor.Top; y < descriptor.Top + descriptor.Height; y++)
{ {
for (int y = descriptor.Top; y < descriptor.Top + descriptor.Height; y++) // Check if this image is interlaced.
int writeY; // the target y offset to write to
if (descriptor.InterlaceFlag)
{ {
// Check if this image is interlaced. // If so then we read lines at predetermined offsets.
int writeY; // the target y offset to write to // When an entire image height worth of offset lines has been read we consider this a pass.
if (descriptor.InterlaceFlag) // With each pass the number of offset lines changes and the starting line changes.
if (interlaceY >= descriptor.Height)
{ {
// If so then we read lines at predetermined offsets. interlacePass++;
// When an entire image height worth of offset lines has been read we consider this a pass. switch (interlacePass)
// With each pass the number of offset lines changes and the starting line changes.
if (interlaceY >= descriptor.Height)
{ {
interlacePass++; case 1:
switch (interlacePass) interlaceY = 4;
{ break;
case 1: case 2:
interlaceY = 4; interlaceY = 2;
break; interlaceIncrement = 4;
case 2: break;
interlaceY = 2; case 3:
interlaceIncrement = 4; interlaceY = 1;
break; interlaceIncrement = 2;
case 3: break;
interlaceY = 1;
interlaceIncrement = 2;
break;
}
} }
}
writeY = interlaceY + descriptor.Top; writeY = interlaceY + descriptor.Top;
interlaceY += interlaceIncrement; interlaceY += interlaceIncrement;
} }
else else
{ {
writeY = y; writeY = y;
} }
pixelAccessor.CopyTo(pixelRow, writeY, descriptor.Left); for (int x = descriptor.Left; x < descriptor.Left + descriptor.Width; x++)
{
int index = indices[i];
byte* pixelBase = pixelRow.PixelBase; if (this.graphicsControlExtension == null ||
for (int x = 0; x < descriptor.Width; x++) this.graphicsControlExtension.TransparencyFlag == false ||
this.graphicsControlExtension.TransparencyIndex != index)
{ {
int index = indices[i]; int indexOffset = index * 3;
if (this.graphicsControlExtension == null ||
this.graphicsControlExtension.TransparencyFlag == false ||
this.graphicsControlExtension.TransparencyIndex != index)
{
int indexOffset = index * 3;
*(pixelBase + 0) = colorTable[indexOffset];
*(pixelBase + 1) = colorTable[indexOffset + 1];
*(pixelBase + 2) = colorTable[indexOffset + 2];
*(pixelBase + 3) = 255;
// TODO: This is actually 200us faster in benchmarking.
// int indexOffset = index * 3;
// TColor pixel = default(TColor);
// pixel.PackFromBytes(colorTable[indexOffset], colorTable[indexOffset + 1], colorTable[indexOffset + 2], 255);
// currentPixels[x, writeY] = pixel;
}
i++; TColor pixel = default(TColor);
pixelBase += 4; pixel.PackFromBytes(colorTable[indexOffset], colorTable[indexOffset + 1], colorTable[indexOffset + 2], 255);
pixelAccessor[x, writeY] = pixel;
} }
pixelAccessor.CopyFrom(pixelRow, writeY, descriptor.Left); i++;
} }
} }
} }

Loading…
Cancel
Save