From 0036208fb4ef2e05a8ea34016528d92ee3d142e4 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 12 Jan 2017 17:25:06 +1100 Subject: [PATCH] Fix concurrent png decoding --- src/ImageSharp.Formats.Png/PngDecoderCore.cs | 38 ++++++++++---------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/ImageSharp.Formats.Png/PngDecoderCore.cs b/src/ImageSharp.Formats.Png/PngDecoderCore.cs index ed5862e07..7ed38b6e8 100644 --- a/src/ImageSharp.Formats.Png/PngDecoderCore.cs +++ b/src/ImageSharp.Formats.Png/PngDecoderCore.cs @@ -129,7 +129,7 @@ namespace ImageSharp.Formats /// Decodes the stream to the image. /// /// The pixel format. - /// The image to decode to. + /// The image to decode to. /// The stream containing image data. /// /// Thrown if the stream does not contain and end chunk. @@ -139,7 +139,7 @@ namespace ImageSharp.Formats /// public void Decode(Image image, Stream stream) where TColor : struct, IPackedPixel, IEquatable - { + { Image currentImage = image; this.currentStream = stream; this.currentStream.Skip(8); @@ -259,11 +259,11 @@ namespace ImageSharp.Formats /// Reads the data chunk containing physical dimension data. /// /// The pixel format. - /// The image to read to. + /// The image to read to. /// The data containing physical data. private void ReadPhysicalChunk(Image image, byte[] data) where TColor : struct, IPackedPixel, IEquatable - { + { data.ReverseBytes(0, 4); data.ReverseBytes(4, 4); @@ -322,11 +322,11 @@ namespace ImageSharp.Formats /// Reads the scanlines within the image. /// /// The pixel format. - /// The containing data. + /// The containing data. /// The pixel data. private void ReadScanlines(MemoryStream dataStream, PixelAccessor pixels) where TColor : struct, IPackedPixel, IEquatable - { + { this.bytesPerPixel = this.CalculateBytesPerPixel(); this.bytesPerScanline = this.CalculateScanlineLength(this.header.Width) + 1; this.bytesPerSample = 1; @@ -353,15 +353,16 @@ namespace ImageSharp.Formats /// Decodes the raw pixel data row by row /// /// The pixel format. - /// The compressed pixel data stream. + /// The compressed pixel data stream. /// The image pixel accessor. private void DecodePixelData(Stream compressedStream, PixelAccessor pixels) where TColor : struct, IPackedPixel, IEquatable - { + { byte[] previousScanline = ArrayPool.Shared.Rent(this.bytesPerScanline); byte[] scanline = ArrayPool.Shared.Rent(this.bytesPerScanline); - // Zero out the previousScanline, because the bytes that are rented from the arraypool may not be zero. + // Zero out the scanlines, because the bytes that are rented from the arraypool may not be zero. + Array.Clear(scanline, 0, this.bytesPerScanline); Array.Clear(previousScanline, 0, this.bytesPerScanline); try @@ -425,11 +426,11 @@ namespace ImageSharp.Formats /// /// /// The pixel format. - /// The compressed pixel data stream. + /// The compressed pixel data stream. /// The image pixel accessor. private void DecodeInterlacedPixelData(Stream compressedStream, PixelAccessor pixels) where TColor : struct, IPackedPixel, IEquatable - { + { byte[] previousScanline = ArrayPool.Shared.Rent(this.bytesPerScanline); byte[] scanline = ArrayPool.Shared.Rent(this.bytesPerScanline); @@ -437,7 +438,8 @@ namespace ImageSharp.Formats { for (int pass = 0; pass < 7; pass++) { - // Zero out the previousScanline, because the bytes that are rented from the arraypool may not be zero. + // Zero out the scanlines, because the bytes that are rented from the arraypool may not be zero. + Array.Clear(scanline, 0, this.bytesPerScanline); Array.Clear(previousScanline, 0, this.bytesPerScanline); int y = Adam7FirstRow[pass]; @@ -512,12 +514,12 @@ namespace ImageSharp.Formats /// Processes the de-filtered scanline filling the image pixel data /// /// The pixel format. - /// The de-filtered scanline + /// The de-filtered scanline /// The current image row. /// The image pixels private void ProcessDefilteredScanline(byte[] defilteredScanline, int row, PixelAccessor pixels) where TColor : struct, IPackedPixel, IEquatable - { + { TColor color = default(TColor); switch (this.PngColorType) { @@ -635,14 +637,14 @@ namespace ImageSharp.Formats /// Processes the interlaced de-filtered scanline filling the image pixel data /// /// The pixel format. - /// The de-filtered scanline + /// The de-filtered scanline /// The current image row. /// The image pixels /// The column start index. Always 0 for none interlaced images. /// The column increment. Always 1 for none interlaced images. private void ProcessInterlacedDefilteredScanline(byte[] defilteredScanline, int row, PixelAccessor pixels, int pixelOffset = 0, int increment = 1) where TColor : struct, IPackedPixel, IEquatable - { + { TColor color = default(TColor); switch (this.PngColorType) @@ -755,12 +757,12 @@ namespace ImageSharp.Formats /// Reads a text chunk containing image properties from the data. /// /// The pixel format. - /// The image to decode to. + /// The image to decode to. /// The containing data. /// The maximum length to read. private void ReadTextChunk(Image image, byte[] data, int length) where TColor : struct, IPackedPixel, IEquatable - { + { int zeroIndex = 0; for (int i = 0; i < length; i++)