From 0b762244e8afeaf2d930a4f72b59a78f09f03aeb Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 8 Dec 2015 17:28:02 +1100 Subject: [PATCH] Perf improvements for gifs. Former-commit-id: 32a200caf1414859cf79e12f212196591a388bbf Former-commit-id: 4278b74ecfbb233f5770477585d2fba83eb6ce53 Former-commit-id: 945f94750695542293210df6e32a654c7dd8f0d6 --- .../Formats/Gif/GifDecoderCore.cs | 28 ++++++++++--------- src/ImageProcessor/Formats/Gif/GifEncoder.cs | 18 ++++++------ .../Formats/Gif/Quantizer/QuantizedImage.cs | 20 +++++++------ 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/ImageProcessor/Formats/Gif/GifDecoderCore.cs b/src/ImageProcessor/Formats/Gif/GifDecoderCore.cs index ee69a854b..c7f22d9fa 100644 --- a/src/ImageProcessor/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageProcessor/Formats/Gif/GifDecoderCore.cs @@ -7,6 +7,7 @@ namespace ImageProcessor.Formats { using System; using System.IO; + using System.Threading.Tasks; /// /// 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) { diff --git a/src/ImageProcessor/Formats/Gif/GifEncoder.cs b/src/ImageProcessor/Formats/Gif/GifEncoder.cs index a0f344b01..81735f55b 100644 --- a/src/ImageProcessor/Formats/Gif/GifEncoder.cs +++ b/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; /// /// 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); diff --git a/src/ImageProcessor/Formats/Gif/Quantizer/QuantizedImage.cs b/src/ImageProcessor/Formats/Gif/Quantizer/QuantizedImage.cs index 846073e63..668d97d91 100644 --- a/src/ImageProcessor/Formats/Gif/Quantizer/QuantizedImage.cs +++ b/src/ImageProcessor/Formats/Gif/Quantizer/QuantizedImage.cs @@ -6,6 +6,7 @@ namespace ImageProcessor.Formats { using System; + using System.Threading.Tasks; /// /// 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;