Browse Source

Perf improvements for gifs.

Former-commit-id: 32a200caf1414859cf79e12f212196591a388bbf
Former-commit-id: 4278b74ecfbb233f5770477585d2fba83eb6ce53
Former-commit-id: 945f94750695542293210df6e32a654c7dd8f0d6
pull/17/head
James Jackson-South 10 years ago
parent
commit
0b762244e8
  1. 28
      src/ImageProcessor/Formats/Gif/GifDecoderCore.cs
  2. 18
      src/ImageProcessor/Formats/Gif/GifEncoder.cs
  3. 20
      src/ImageProcessor/Formats/Gif/Quantizer/QuantizedImage.cs

28
src/ImageProcessor/Formats/Gif/GifDecoderCore.cs

@ -7,6 +7,7 @@ namespace ImageProcessor.Formats
{
using System;
using System.IO;
using System.Threading.Tasks;
/// <summary>
/// Performs the gif decoding operation.
@ -404,19 +405,20 @@ namespace ImageProcessor.Formats
{
if (this.graphicsControlExtension.DisposalMethod == DisposalMethod.RestoreToBackground)
{
for (int y = descriptor.Top; y < descriptor.Top + descriptor.Height; y++)
{
for (int x = descriptor.Left; x < descriptor.Left + descriptor.Width; x++)
{
offset = ((y * imageWidth) + x) * 4;
// Stored in r-> g-> b-> a order.
this.currentFrame[offset + 0] = 0;
this.currentFrame[offset + 1] = 0;
this.currentFrame[offset + 2] = 0;
this.currentFrame[offset + 3] = 0;
}
}
Parallel.For(descriptor.Top, descriptor.Top + descriptor.Height,
y =>
{
for (int x = descriptor.Left; x < descriptor.Left + descriptor.Width; x++)
{
offset = ((y * imageWidth) + x) * 4;
// Stored in r-> g-> b-> a order.
this.currentFrame[offset + 0] = 0;
this.currentFrame[offset + 1] = 0;
this.currentFrame[offset + 2] = 0;
this.currentFrame[offset + 3] = 0;
}
});
}
else if (this.graphicsControlExtension.DisposalMethod == DisposalMethod.RestoreToPrevious)
{

18
src/ImageProcessor/Formats/Gif/GifEncoder.cs

@ -8,6 +8,7 @@ namespace ImageProcessor.Formats
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
/// <summary>
/// Image encoder for writing image data to a stream in gif format.
@ -148,15 +149,16 @@ namespace ImageProcessor.Formats
int colorTableLength = (int)Math.Pow(2, bitDepth) * 3;
byte[] colorTable = new byte[colorTableLength];
for (int i = 0; i < pixelCount; i++)
{
int offset = i * 3;
Bgra32 color = Color.ToNonPremultiplied(pallete[i]);
Parallel.For(0, pixelCount,
i =>
{
int offset = i * 3;
Bgra32 color = Color.ToNonPremultiplied(pallete[i]);
colorTable[offset] = color.R;
colorTable[offset + 1] = color.G;
colorTable[offset + 2] = color.B;
}
colorTable[offset] = color.R;
colorTable[offset + 1] = color.G;
colorTable[offset + 2] = color.B;
});
stream.Write(colorTable, 0, colorTableLength);

20
src/ImageProcessor/Formats/Gif/Quantizer/QuantizedImage.cs

@ -6,6 +6,7 @@
namespace ImageProcessor.Formats
{
using System;
using System.Threading.Tasks;
/// <summary>
/// Represents a quantized image where the pixels indexed by a color palette.
@ -72,15 +73,16 @@ namespace ImageProcessor.Formats
int palletCount = this.Palette.Length - 1;
float[] bgraPixels = new float[pixelCount * 4];
for (int i = 0; i < pixelCount; i++)
{
int offset = i * 4;
Color color = this.Palette[Math.Min(palletCount, this.Pixels[i])];
bgraPixels[offset] = color.R;
bgraPixels[offset + 1] = color.G;
bgraPixels[offset + 2] = color.B;
bgraPixels[offset + 3] = color.A;
}
Parallel.For(0, pixelCount,
i =>
{
int offset = i * 4;
Color color = this.Palette[Math.Min(palletCount, this.Pixels[i])];
bgraPixels[offset] = color.R;
bgraPixels[offset + 1] = color.G;
bgraPixels[offset + 2] = color.B;
bgraPixels[offset + 3] = color.A;
});
image.SetPixels(this.Width, this.Height, bgraPixels);
return image;

Loading…
Cancel
Save