From c8acc89ad629cfbfb8f16ebba09fe8d0dac35b77 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 22 Jan 2016 10:42:45 +1100 Subject: [PATCH] Png perf improvements + cleanup Former-commit-id: ff6ee98a500cab9af4419b6bdcf2aecaaa24e380 Former-commit-id: 0c00eb6ac7ba50eca5e1ee3340c779d8626ae9fe Former-commit-id: 17ff53ba48bb59112609d4efc1ed2c2edd0ede97 --- .../Common/Extensions/ByteExtensions.cs | 11 +++----- .../Formats/Png/GrayscaleReader.cs | 2 +- .../Formats/Png/PaletteIndexReader.cs | 2 +- src/ImageProcessor/Formats/Png/PngChunk.cs | 11 +++----- .../Formats/Png/PngChunkTypes.cs | 11 +++----- .../Formats/Png/PngColorTypeInformation.cs | 11 +++----- src/ImageProcessor/Formats/Png/PngDecoder.cs | 11 +++----- .../Formats/Png/PngDecoderCore.cs | 25 +++---------------- src/ImageProcessor/Formats/Png/PngEncoder.cs | 5 ++-- src/ImageProcessor/Formats/Png/PngHeader.cs | 13 +++------- src/ImageProcessor/Formats/Png/README.md | 2 ++ .../Formats/Png/TrueColorReader.cs | 2 +- ...ibOutputStream.cs => ZlibDeflateStream.cs} | 11 +++++--- ...libInputStream.cs => ZlibInflateStream.cs} | 13 +++++++--- 14 files changed, 47 insertions(+), 83 deletions(-) rename src/ImageProcessor/Formats/Png/Zlib/{ZlibOutputStream.cs => ZlibDeflateStream.cs} (93%) rename src/ImageProcessor/Formats/Png/Zlib/{ZlibInputStream.cs => ZlibInflateStream.cs} (93%) diff --git a/src/ImageProcessor/Common/Extensions/ByteExtensions.cs b/src/ImageProcessor/Common/Extensions/ByteExtensions.cs index 1fc3784a7..aeca81560 100644 --- a/src/ImageProcessor/Common/Extensions/ByteExtensions.cs +++ b/src/ImageProcessor/Common/Extensions/ByteExtensions.cs @@ -1,12 +1,7 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. // -// -// Extension methods for the struct. -// -// -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor { diff --git a/src/ImageProcessor/Formats/Png/GrayscaleReader.cs b/src/ImageProcessor/Formats/Png/GrayscaleReader.cs index 4dfc69d9a..1d91c8a45 100644 --- a/src/ImageProcessor/Formats/Png/GrayscaleReader.cs +++ b/src/ImageProcessor/Formats/Png/GrayscaleReader.cs @@ -8,7 +8,7 @@ namespace ImageProcessor.Formats /// /// Color reader for reading grayscale colors from a png file. /// - public sealed class GrayscaleReader : IColorReader + internal sealed class GrayscaleReader : IColorReader { /// /// Whether t also read the alpha channel. diff --git a/src/ImageProcessor/Formats/Png/PaletteIndexReader.cs b/src/ImageProcessor/Formats/Png/PaletteIndexReader.cs index af087701b..6128b1244 100644 --- a/src/ImageProcessor/Formats/Png/PaletteIndexReader.cs +++ b/src/ImageProcessor/Formats/Png/PaletteIndexReader.cs @@ -8,7 +8,7 @@ namespace ImageProcessor.Formats /// /// A color reader for reading palette indices from the png file. /// - public sealed class PaletteIndexReader : IColorReader + internal sealed class PaletteIndexReader : IColorReader { /// /// The palette. diff --git a/src/ImageProcessor/Formats/Png/PngChunk.cs b/src/ImageProcessor/Formats/Png/PngChunk.cs index cbd3f9cbe..5bee7c375 100644 --- a/src/ImageProcessor/Formats/Png/PngChunk.cs +++ b/src/ImageProcessor/Formats/Png/PngChunk.cs @@ -1,12 +1,7 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. // -// -// Stores header information about a chunk. -// -// -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Formats { diff --git a/src/ImageProcessor/Formats/Png/PngChunkTypes.cs b/src/ImageProcessor/Formats/Png/PngChunkTypes.cs index 7f39a887d..bfb12961c 100644 --- a/src/ImageProcessor/Formats/Png/PngChunkTypes.cs +++ b/src/ImageProcessor/Formats/Png/PngChunkTypes.cs @@ -1,12 +1,7 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. // -// -// Contains a list of possible chunk type identifiers. -// -// -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Formats { diff --git a/src/ImageProcessor/Formats/Png/PngColorTypeInformation.cs b/src/ImageProcessor/Formats/Png/PngColorTypeInformation.cs index cacb0fb1d..ec7c6fa66 100644 --- a/src/ImageProcessor/Formats/Png/PngColorTypeInformation.cs +++ b/src/ImageProcessor/Formats/Png/PngColorTypeInformation.cs @@ -1,12 +1,7 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. // -// -// Contains information that are required when loading a png with a specific color type. -// -// -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Formats { diff --git a/src/ImageProcessor/Formats/Png/PngDecoder.cs b/src/ImageProcessor/Formats/Png/PngDecoder.cs index 9661f2536..d9e87d898 100644 --- a/src/ImageProcessor/Formats/Png/PngDecoder.cs +++ b/src/ImageProcessor/Formats/Png/PngDecoder.cs @@ -1,12 +1,7 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. // -// -// Encoder for generating an image out of a png encoded stream. -// -// -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Formats { diff --git a/src/ImageProcessor/Formats/Png/PngDecoderCore.cs b/src/ImageProcessor/Formats/Png/PngDecoderCore.cs index 489d69eeb..6ecbe5a86 100644 --- a/src/ImageProcessor/Formats/Png/PngDecoderCore.cs +++ b/src/ImageProcessor/Formats/Png/PngDecoderCore.cs @@ -1,12 +1,7 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. // -// -// Performs the png decoding operation. -// -// -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Formats { @@ -21,11 +16,6 @@ namespace ImageProcessor.Formats /// internal class PngDecoderCore { - /// - /// The maximum chunk size. - /// - private const int MaxChunkSize = int.MaxValue / 4 * sizeof(float); - /// /// The dictionary of available color types. /// @@ -261,7 +251,6 @@ namespace ImageProcessor.Formats int filter = 0, column = -1; using (ZlibInputStream compressedStream = new ZlibInputStream(dataStream)) - //using (InflaterInputStream compressedStream = new InflaterInputStream(dataStream)) { int readByte; while ((readByte = compressedStream.ReadByte()) >= 0) @@ -456,16 +445,8 @@ namespace ImageProcessor.Formats /// Reads the chunk data from the stream. /// /// The chunk. - /// - /// Thrown if the chunk length exceeds the maximum allowable size. - /// private void ReadChunkData(PngChunk chunk) { - if (chunk.Length > MaxChunkSize) - { - throw new ArgumentOutOfRangeException($"Png chunk size '{chunk.Length}' exceeds the maximum '{MaxChunkSize}'"); - } - chunk.Data = new byte[chunk.Length]; this.currentStream.Read(chunk.Data, 0, chunk.Length); } diff --git a/src/ImageProcessor/Formats/Png/PngEncoder.cs b/src/ImageProcessor/Formats/Png/PngEncoder.cs index 786d02a85..850cb5ee0 100644 --- a/src/ImageProcessor/Formats/Png/PngEncoder.cs +++ b/src/ImageProcessor/Formats/Png/PngEncoder.cs @@ -7,6 +7,7 @@ namespace ImageProcessor.Formats { using System; using System.IO; + using System.Threading.Tasks; /// /// Image encoder for writing image data to a stream in png format. @@ -204,7 +205,7 @@ namespace ImageProcessor.Formats int rowLength = (imageBase.Width * 4) + 1; - for (int y = 0; y < imageBase.Height; y++) + Parallel.For(0, imageBase.Height, y => { byte compression = 0; if (y > 0) @@ -252,7 +253,7 @@ namespace ImageProcessor.Formats data[dataOffset + 3] -= color.A; } } - } + }); byte[] buffer; int bufferLength; diff --git a/src/ImageProcessor/Formats/Png/PngHeader.cs b/src/ImageProcessor/Formats/Png/PngHeader.cs index b4ccac005..9b7eacc2f 100644 --- a/src/ImageProcessor/Formats/Png/PngHeader.cs +++ b/src/ImageProcessor/Formats/Png/PngHeader.cs @@ -1,19 +1,14 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. // -// -// Represents the png header chunk. -// -// -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Formats { /// /// Represents the png header chunk. /// - public sealed class PngHeader + internal sealed class PngHeader { /// /// Gets or sets the dimension in x-direction of the image in pixels. diff --git a/src/ImageProcessor/Formats/Png/README.md b/src/ImageProcessor/Formats/Png/README.md index 0b5bd8c81..8ade37956 100644 --- a/src/ImageProcessor/Formats/Png/README.md +++ b/src/ImageProcessor/Formats/Png/README.md @@ -2,3 +2,5 @@ https://github.com/yufeih/Nine.Imaging/ https://imagetools.codeplex.com/ +https://github.com/leonbloy/pngcs + diff --git a/src/ImageProcessor/Formats/Png/TrueColorReader.cs b/src/ImageProcessor/Formats/Png/TrueColorReader.cs index c292fcd56..74028967b 100644 --- a/src/ImageProcessor/Formats/Png/TrueColorReader.cs +++ b/src/ImageProcessor/Formats/Png/TrueColorReader.cs @@ -9,7 +9,7 @@ namespace ImageProcessor.Formats /// Color reader for reading true colors from a png file. Only colors /// with 24 or 32 bit (3 or 4 bytes) per pixel are supported at the moment. /// - public sealed class TrueColorReader : IColorReader + internal sealed class TrueColorReader : IColorReader { /// /// Whether t also read the alpha channel. diff --git a/src/ImageProcessor/Formats/Png/Zlib/ZlibOutputStream.cs b/src/ImageProcessor/Formats/Png/Zlib/ZlibDeflateStream.cs similarity index 93% rename from src/ImageProcessor/Formats/Png/Zlib/ZlibOutputStream.cs rename to src/ImageProcessor/Formats/Png/Zlib/ZlibDeflateStream.cs index 9af156d06..efb57eed2 100644 --- a/src/ImageProcessor/Formats/Png/Zlib/ZlibOutputStream.cs +++ b/src/ImageProcessor/Formats/Png/Zlib/ZlibDeflateStream.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // @@ -9,7 +9,10 @@ namespace ImageProcessor.Formats using System.IO; using System.IO.Compression; - internal sealed class ZlibOutputStream : Stream + /// + /// Provides methods and properties for compressing streams by using the Zlib Deflate algorithm. + /// + internal sealed class ZlibDeflateStream : Stream { /// /// The raw stream containing the uncompressed image data. @@ -38,11 +41,11 @@ namespace ImageProcessor.Formats private DeflateStream deflateStream; /// - /// Initializes a new instance of + /// Initializes a new instance of /// /// The stream to compress. /// The compression level. - public ZlibOutputStream(Stream stream, int compressionLevel) + public ZlibDeflateStream(Stream stream, int compressionLevel) { this.rawStream = stream; diff --git a/src/ImageProcessor/Formats/Png/Zlib/ZlibInputStream.cs b/src/ImageProcessor/Formats/Png/Zlib/ZlibInflateStream.cs similarity index 93% rename from src/ImageProcessor/Formats/Png/Zlib/ZlibInputStream.cs rename to src/ImageProcessor/Formats/Png/Zlib/ZlibInflateStream.cs index bcbd5d3bd..ebf19a285 100644 --- a/src/ImageProcessor/Formats/Png/Zlib/ZlibInputStream.cs +++ b/src/ImageProcessor/Formats/Png/Zlib/ZlibInflateStream.cs @@ -1,11 +1,18 @@ - +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + namespace ImageProcessor.Formats { using System; using System.IO; using System.IO.Compression; - internal sealed class ZlibInputStream : Stream + /// + /// Provides methods and properties for decompressing streams by using the Zlib Deflate algorithm. + /// + internal sealed class ZlibInflateStream : Stream { /// /// A value indicating whether this instance of the given entity has been disposed. @@ -51,7 +58,7 @@ namespace ImageProcessor.Formats // The stream responsible for decompressing the input stream. private DeflateStream deflateStream; - public ZlibInputStream(Stream stream) + public ZlibInflateStream(Stream stream) { this.rawStream = stream;