Browse Source

Png perf improvements + cleanup

Former-commit-id: ff6ee98a500cab9af4419b6bdcf2aecaaa24e380
Former-commit-id: 0c00eb6ac7ba50eca5e1ee3340c779d8626ae9fe
Former-commit-id: 17ff53ba48bb59112609d4efc1ed2c2edd0ede97
af/merge-core
James Jackson-South 10 years ago
parent
commit
c8acc89ad6
  1. 11
      src/ImageProcessor/Common/Extensions/ByteExtensions.cs
  2. 2
      src/ImageProcessor/Formats/Png/GrayscaleReader.cs
  3. 2
      src/ImageProcessor/Formats/Png/PaletteIndexReader.cs
  4. 11
      src/ImageProcessor/Formats/Png/PngChunk.cs
  5. 11
      src/ImageProcessor/Formats/Png/PngChunkTypes.cs
  6. 11
      src/ImageProcessor/Formats/Png/PngColorTypeInformation.cs
  7. 11
      src/ImageProcessor/Formats/Png/PngDecoder.cs
  8. 25
      src/ImageProcessor/Formats/Png/PngDecoderCore.cs
  9. 5
      src/ImageProcessor/Formats/Png/PngEncoder.cs
  10. 13
      src/ImageProcessor/Formats/Png/PngHeader.cs
  11. 2
      src/ImageProcessor/Formats/Png/README.md
  12. 2
      src/ImageProcessor/Formats/Png/TrueColorReader.cs
  13. 11
      src/ImageProcessor/Formats/Png/Zlib/ZlibDeflateStream.cs
  14. 13
      src/ImageProcessor/Formats/Png/Zlib/ZlibInflateStream.cs

11
src/ImageProcessor/Common/Extensions/ByteExtensions.cs

@ -1,12 +1,7 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ByteExtensions.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// <copyright file="ByteExtensions.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Extension methods for the <see cref="byte" /> struct.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor
{

2
src/ImageProcessor/Formats/Png/GrayscaleReader.cs

@ -8,7 +8,7 @@ namespace ImageProcessor.Formats
/// <summary>
/// Color reader for reading grayscale colors from a png file.
/// </summary>
public sealed class GrayscaleReader : IColorReader
internal sealed class GrayscaleReader : IColorReader
{
/// <summary>
/// Whether t also read the alpha channel.

2
src/ImageProcessor/Formats/Png/PaletteIndexReader.cs

@ -8,7 +8,7 @@ namespace ImageProcessor.Formats
/// <summary>
/// A color reader for reading palette indices from the png file.
/// </summary>
public sealed class PaletteIndexReader : IColorReader
internal sealed class PaletteIndexReader : IColorReader
{
/// <summary>
/// The palette.

11
src/ImageProcessor/Formats/Png/PngChunk.cs

@ -1,12 +1,7 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PngChunk.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// <copyright file="PngChunk.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Stores header information about a chunk.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Formats
{

11
src/ImageProcessor/Formats/Png/PngChunkTypes.cs

@ -1,12 +1,7 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PngChunkTypes.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// <copyright file="PngChunkTypes.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Contains a list of possible chunk type identifiers.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Formats
{

11
src/ImageProcessor/Formats/Png/PngColorTypeInformation.cs

@ -1,12 +1,7 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PngColorTypeInformation.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// <copyright file="PngColorTypeInformation.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Contains information that are required when loading a png with a specific color type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Formats
{

11
src/ImageProcessor/Formats/Png/PngDecoder.cs

@ -1,12 +1,7 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PngDecoder.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// <copyright file="PngDecoder.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encoder for generating an image out of a png encoded stream.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Formats
{

25
src/ImageProcessor/Formats/Png/PngDecoderCore.cs

@ -1,12 +1,7 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PngDecoderCore.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// <copyright file="PngDecoderCore.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Performs the png decoding operation.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Formats
{
@ -21,11 +16,6 @@ namespace ImageProcessor.Formats
/// </summary>
internal class PngDecoderCore
{
/// <summary>
/// The maximum chunk size.
/// </summary>
private const int MaxChunkSize = int.MaxValue / 4 * sizeof(float);
/// <summary>
/// The dictionary of available color types.
/// </summary>
@ -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.
/// </summary>
/// <param name="chunk">The chunk.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if the chunk length exceeds the maximum allowable size.
/// </exception>
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);
}

5
src/ImageProcessor/Formats/Png/PngEncoder.cs

@ -7,6 +7,7 @@ namespace ImageProcessor.Formats
{
using System;
using System.IO;
using System.Threading.Tasks;
/// <summary>
/// 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;

13
src/ImageProcessor/Formats/Png/PngHeader.cs

@ -1,19 +1,14 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PngHeader.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// <copyright file="PngHeader.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Represents the png header chunk.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Formats
{
/// <summary>
/// Represents the png header chunk.
/// </summary>
public sealed class PngHeader
internal sealed class PngHeader
{
/// <summary>
/// Gets or sets the dimension in x-direction of the image in pixels.

2
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

2
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.
/// </summary>
public sealed class TrueColorReader : IColorReader
internal sealed class TrueColorReader : IColorReader
{
/// <summary>
/// Whether t also read the alpha channel.

11
src/ImageProcessor/Formats/Png/Zlib/ZlibOutputStream.cs → src/ImageProcessor/Formats/Png/Zlib/ZlibDeflateStream.cs

@ -1,4 +1,4 @@
// <copyright file="ZlibOutputStream.cs" company="James Jackson-South">
// <copyright file="ZlibDeflateStream.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
@ -9,7 +9,10 @@ namespace ImageProcessor.Formats
using System.IO;
using System.IO.Compression;
internal sealed class ZlibOutputStream : Stream
/// <summary>
/// Provides methods and properties for compressing streams by using the Zlib Deflate algorithm.
/// </summary>
internal sealed class ZlibDeflateStream : Stream
{
/// <summary>
/// The raw stream containing the uncompressed image data.
@ -38,11 +41,11 @@ namespace ImageProcessor.Formats
private DeflateStream deflateStream;
/// <summary>
/// Initializes a new instance of <see cref="ZlibOutputStream"/>
/// Initializes a new instance of <see cref="ZlibDeflateStream"/>
/// </summary>
/// <param name="stream">The stream to compress.</param>
/// <param name="compressionLevel">The compression level.</param>
public ZlibOutputStream(Stream stream, int compressionLevel)
public ZlibDeflateStream(Stream stream, int compressionLevel)
{
this.rawStream = stream;

13
src/ImageProcessor/Formats/Png/Zlib/ZlibInputStream.cs → src/ImageProcessor/Formats/Png/Zlib/ZlibInflateStream.cs

@ -1,11 +1,18 @@

// <copyright file="ZlibInflateStream.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageProcessor.Formats
{
using System;
using System.IO;
using System.IO.Compression;
internal sealed class ZlibInputStream : Stream
/// <summary>
/// Provides methods and properties for decompressing streams by using the Zlib Deflate algorithm.
/// </summary>
internal sealed class ZlibInflateStream : Stream
{
/// <summary>
/// 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;
Loading…
Cancel
Save