Browse Source

Optimize huffman and make all internal

af/merge-core
James Jackson-South 6 years ago
parent
commit
23fae72391
  1. 6
      src/ImageSharp/Formats/Png/Zlib/Crc32.cs
  2. 4
      src/ImageSharp/Formats/Png/Zlib/DeflateThrowHelper.cs
  3. 2
      src/ImageSharp/Formats/Png/Zlib/Deflater.cs
  4. 87
      src/ImageSharp/Formats/Png/Zlib/DeflaterEngine.cs
  5. 343
      src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs
  6. 477
      src/ImageSharp/Formats/Png/Zlib/DeflaterOutputStream.cs
  7. 19
      src/ImageSharp/Formats/Png/Zlib/DeflaterPendingBuffer.cs
  8. 7
      src/ImageSharp/Formats/Png/Zlib/README.md
  9. 41
      src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs

6
src/ImageSharp/Formats/Png/Zlib/Crc32.cs

@ -1,8 +1,9 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace SixLabors.ImageSharp.Formats.Png.Zlib
{
@ -141,9 +142,10 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
{
this.crc ^= CrcSeed;
ref uint crcTableRef = ref MemoryMarshal.GetReference(CrcTable.AsSpan());
for (int i = 0; i < data.Length; i++)
{
this.crc = CrcTable[(this.crc ^ data[i]) & 0xFF] ^ (this.crc >> 8);
this.crc = Unsafe.Add(ref crcTableRef, (int)((this.crc ^ data[i]) & 0xFF)) ^ (this.crc >> 8);
}
this.crc ^= CrcSeed;

4
src/ImageSharp/Formats/Png/Zlib/DeflateThrowHelper.cs

@ -27,9 +27,9 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
public static void ThrowOutOfRange(string name) => throw new ArgumentOutOfRangeException(name);
[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowFrequencyNotEmpty() => throw new InvalidOperationException("Huffman frequency entry non empty.");
public static void ThrowHeapViolated() => throw new InvalidOperationException("Huffman heap invariant violated.");
[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowHeapViolated() => throw new InvalidOperationException("Huffman heap invariant violated.");
public static void ThrowNoDeflate() => throw new ImageFormatException("Cannot deflate all input.");
}
}

2
src/ImageSharp/Formats/Png/Zlib/Deflater.cs

@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// This class compresses input with the deflate algorithm described in RFC 1951.
/// It has several compression levels and three different strategies described below.
/// </summary>
public sealed class Deflater : IDisposable
internal sealed class Deflater : IDisposable
{
/// <summary>
/// The best and slowest compression level. This tries to find very

87
src/ImageSharp/Formats/Png/Zlib/DeflaterEngine.cs

@ -49,7 +49,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// Low level compression engine for deflate algorithm which uses a 32K sliding window
/// with secondary compression from Huffman/Shannon-Fano codes.
/// </summary>
public sealed unsafe class DeflaterEngine : IDisposable
internal sealed unsafe class DeflaterEngine : IDisposable
{
private const int TooFar = 4096;
@ -109,8 +109,8 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// Note that the array should really be unsigned short, so you need
/// to and the values with 0xFFFF.
/// </summary>
private readonly IMemoryOwner<short> headBuffer;
private MemoryHandle headBufferHandle;
private IMemoryOwner<short> headMemoryOwner;
private MemoryHandle headMemoryHandle;
private readonly Memory<short> head;
private readonly short* pinnedHeadPointer;
@ -121,17 +121,17 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// Note that the array should really be unsigned short, so you need
/// to and the values with 0xFFFF.
/// </summary>
private readonly IMemoryOwner<short> prevBuffer;
private MemoryHandle prevBufferHandle;
private IMemoryOwner<short> prevMemoryOwner;
private MemoryHandle prevMemoryHandle;
private readonly Memory<short> prev;
private readonly short* pinnedPrevPointer;
/// <summary>
/// This array contains the part of the uncompressed stream that
/// is of relevance. The current character is indexed by strstart.
/// is of relevance. The current character is indexed by strstart.
/// </summary>
private readonly IManagedByteBuffer windowBuffer;
private MemoryHandle windowBufferHandle;
private IManagedByteBuffer windowMemoryOwner;
private MemoryHandle windowMemoryHandle;
private readonly byte[] window;
private readonly byte* pinnedWindowPointer;
@ -153,20 +153,20 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
// Create pinned pointers to the various buffers to allow indexing
// without bounds checks.
this.windowBuffer = memoryAllocator.AllocateManagedByteBuffer(2 * DeflaterConstants.WSIZE);
this.window = this.windowBuffer.Array;
this.windowBufferHandle = this.windowBuffer.Memory.Pin();
this.pinnedWindowPointer = (byte*)this.windowBufferHandle.Pointer;
this.windowMemoryOwner = memoryAllocator.AllocateManagedByteBuffer(2 * DeflaterConstants.WSIZE);
this.window = this.windowMemoryOwner.Array;
this.windowMemoryHandle = this.windowMemoryOwner.Memory.Pin();
this.pinnedWindowPointer = (byte*)this.windowMemoryHandle.Pointer;
this.headBuffer = memoryAllocator.Allocate<short>(DeflaterConstants.HASH_SIZE);
this.head = this.headBuffer.Memory;
this.headBufferHandle = this.headBuffer.Memory.Pin();
this.pinnedHeadPointer = (short*)this.headBufferHandle.Pointer;
this.headMemoryOwner = memoryAllocator.Allocate<short>(DeflaterConstants.HASH_SIZE);
this.head = this.headMemoryOwner.Memory;
this.headMemoryHandle = this.headMemoryOwner.Memory.Pin();
this.pinnedHeadPointer = (short*)this.headMemoryHandle.Pointer;
this.prevBuffer = memoryAllocator.Allocate<short>(DeflaterConstants.WSIZE);
this.prev = this.prevBuffer.Memory;
this.prevBufferHandle = this.prevBuffer.Memory.Pin();
this.pinnedPrevPointer = (short*)this.prevBufferHandle.Pointer;
this.prevMemoryOwner = memoryAllocator.Allocate<short>(DeflaterConstants.WSIZE);
this.prev = this.prevMemoryOwner.Memory;
this.prevMemoryHandle = this.prevMemoryOwner.Memory.Pin();
this.pinnedPrevPointer = (short*)this.prevMemoryHandle.Pointer;
// We start at index 1, to avoid an implementation deficiency, that
// we cannot build a repeat pattern at index 0.
@ -377,8 +377,27 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// <inheritdoc/>
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
this.Dispose(true);
if (!this.isDisposed)
{
this.huffman.Dispose();
this.windowMemoryHandle.Dispose();
this.windowMemoryOwner.Dispose();
this.headMemoryHandle.Dispose();
this.headMemoryOwner.Dispose();
this.prevMemoryHandle.Dispose();
this.prevMemoryOwner.Dispose();
this.windowMemoryOwner = null;
this.headMemoryOwner = null;
this.prevMemoryOwner = null;
this.huffman = null;
this.isDisposed = true;
}
GC.SuppressFinalize(this);
}
@ -830,29 +849,5 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
return true;
}
private void Dispose(bool disposing)
{
if (!this.isDisposed)
{
if (disposing)
{
this.huffman.Dispose();
this.windowBufferHandle.Dispose();
this.windowBuffer.Dispose();
this.headBufferHandle.Dispose();
this.headBuffer.Dispose();
this.prevBufferHandle.Dispose();
this.prevBuffer.Dispose();
}
this.huffman = null;
this.isDisposed = true;
}
}
}
}

343
src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs

@ -12,7 +12,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// <summary>
/// Performs Deflate Huffman encoding.
/// </summary>
public sealed unsafe class DeflaterHuffman : IDisposable
internal sealed unsafe class DeflaterHuffman : IDisposable
{
private const int BufferSize = 1 << (DeflaterConstants.DEFAULT_MEM_LEVEL + 6);
@ -194,7 +194,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
int dc = Dcode(dist);
this.distTree.WriteSymbol(pendingBuffer, dc);
bits = (dc / 2) - 1;
bits = (dc >> 1) - 1;
if (bits > 0)
{
this.Pending.WriteBits(dist & ((1 << bits) - 1), bits);
@ -349,7 +349,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
this.distTree.Frequencies[dc]++;
if (dc >= 4)
{
this.extraBits += (dc / 2) - 1;
this.extraBits += (dc >> 1) - 1;
}
return this.IsFull();
@ -443,9 +443,11 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
private readonly int elementCount;
private readonly MemoryAllocator memoryAllocator;
private IMemoryOwner<short> codesMemoryOwner;
private MemoryHandle codesMemoryHandle;
private short* codes;
private readonly short* codes;
private IMemoryOwner<short> frequenciesMemoryOwner;
private MemoryHandle frequenciesMemoryHandle;
@ -455,8 +457,8 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
public Tree(MemoryAllocator memoryAllocator, int elements, int minCodes, int maxLength)
{
this.memoryAllocator = memoryAllocator;
this.elementCount = elements;
this.minNumCodes = minCodes;
this.maxLength = maxLength;
@ -497,27 +499,6 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
public void WriteSymbol(DeflaterPendingBuffer pendingBuffer, int code)
=> pendingBuffer.WriteBits(this.codes[code] & 0xFFFF, this.Length[code]);
/// <summary>
/// Check that all frequencies are zero
/// </summary>
/// <exception cref="InvalidOperationException">
/// At least one frequency is non-zero
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
public void CheckEmpty()
{
bool empty = true;
for (int i = 0; i < this.elementCount; i++)
{
empty &= this.Frequencies[i] == 0;
}
if (!empty)
{
DeflateThrowHelper.ThrowFrequencyNotEmpty();
}
}
/// <summary>
/// Set static codes and length
/// </summary>
@ -569,130 +550,141 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
//
// The binary tree is encoded in an array: 0 is root node and
// the nodes 2*n+1, 2*n+2 are the child nodes of node n.
// Maxes out at 286 * 4
Span<int> heap = stackalloc int[numSymbols];
ref int heapRef = ref MemoryMarshal.GetReference(heap);
int heapLen = 0;
int maxCode = 0;
for (int n = 0; n < numSymbols; n++)
{
int freq = this.Frequencies[n];
if (freq != 0)
{
// Insert n into heap
int pos = heapLen++;
int ppos;
while (pos > 0 && this.Frequencies[Unsafe.Add(ref heapRef, ppos = (pos - 1) / 2)] > freq)
{
Unsafe.Add(ref heapRef, pos) = Unsafe.Add(ref heapRef, ppos);
pos = ppos;
}
heap[pos] = n;
maxCode = n;
}
}
// We could encode a single literal with 0 bits but then we
// don't see the literals. Therefore we force at least two
// literals to avoid this case. We don't care about order in
// this case, both literals get a 1 bit code.
while (heapLen < 2)
{
Unsafe.Add(ref heapRef, heapLen++) = maxCode < 2 ? ++maxCode : 0;
}
this.NumCodes = Math.Max(maxCode + 1, this.minNumCodes);
int numLeafs = heapLen;
int[] childs = new int[(4 * heapLen) - 2];
int[] values = new int[(2 * heapLen) - 1];
int numNodes = numLeafs;
for (int i = 0; i < heapLen; i++)
{
int node = Unsafe.Add(ref heapRef, i);
childs[2 * i] = node;
childs[(2 * i) + 1] = -1;
values[i] = this.Frequencies[node] << 8;
heap[i] = i;
}
// Construct the Huffman tree by repeatedly combining the least two
// frequent nodes.
do
// Maxes out at 286 * 4 so too large for the stack.
using (IMemoryOwner<int> heapMemoryOwner = this.memoryAllocator.Allocate<int>(numSymbols))
{
int first = heap[0];
int last = Unsafe.Add(ref heapRef, --heapLen);
// Propagate the hole to the leafs of the heap
int ppos = 0;
int path = 1;
ref int heapRef = ref MemoryMarshal.GetReference(heapMemoryOwner.Memory.Span);
while (path < heapLen)
int heapLen = 0;
int maxCode = 0;
for (int n = 0; n < numSymbols; n++)
{
if (path + 1 < heapLen && values[Unsafe.Add(ref heapRef, path)] > values[Unsafe.Add(ref heapRef, path + 1)])
int freq = this.Frequencies[n];
if (freq != 0)
{
path++;
// Insert n into heap
int pos = heapLen++;
int ppos;
while (pos > 0 && this.Frequencies[Unsafe.Add(ref heapRef, ppos = (pos - 1) >> 1)] > freq)
{
Unsafe.Add(ref heapRef, pos) = Unsafe.Add(ref heapRef, ppos);
pos = ppos;
}
Unsafe.Add(ref heapRef, pos) = n;
maxCode = n;
}
Unsafe.Add(ref heapRef, ppos) = Unsafe.Add(ref heapRef, path);
ppos = path;
path = (path * 2) + 1;
}
// Now propagate the last element down along path. Normally
// it shouldn't go too deep.
int lastVal = values[last];
while ((path = ppos) > 0 && values[Unsafe.Add(ref heapRef, ppos = (path - 1) / 2)] > lastVal)
// We could encode a single literal with 0 bits but then we
// don't see the literals. Therefore we force at least two
// literals to avoid this case. We don't care about order in
// this case, both literals get a 1 bit code.
while (heapLen < 2)
{
Unsafe.Add(ref heapRef, path) = Unsafe.Add(ref heapRef, ppos);
Unsafe.Add(ref heapRef, heapLen++) = maxCode < 2 ? ++maxCode : 0;
}
Unsafe.Add(ref heapRef, path) = last;
int second = Unsafe.Add(ref heapRef, 0);
this.NumCodes = Math.Max(maxCode + 1, this.minNumCodes);
// Create a new node father of first and second
last = numNodes++;
childs[2 * last] = first;
childs[(2 * last) + 1] = second;
int mindepth = Math.Min(values[first] & 0xFF, values[second] & 0xFF);
values[last] = lastVal = values[first] + values[second] - mindepth + 1;
int numLeafs = heapLen;
int childrenLength = (4 * heapLen) - 2;
using (IMemoryOwner<int> childrenMemoryOwner = this.memoryAllocator.Allocate<int>(childrenLength))
using (IMemoryOwner<int> valuesMemoryOwner = this.memoryAllocator.Allocate<int>((2 * heapLen) - 1))
{
ref int childrenRef = ref MemoryMarshal.GetReference(childrenMemoryOwner.Memory.Span);
ref int valuesRef = ref MemoryMarshal.GetReference(valuesMemoryOwner.Memory.Span);
int numNodes = numLeafs;
// Again, propagate the hole to the leafs
ppos = 0;
path = 1;
for (int i = 0; i < heapLen; i++)
{
int node = Unsafe.Add(ref heapRef, i);
int i2 = 2 * i;
Unsafe.Add(ref childrenRef, i2) = node;
Unsafe.Add(ref childrenRef, i2 + 1) = -1;
Unsafe.Add(ref valuesRef, i) = this.Frequencies[node] << 8;
Unsafe.Add(ref heapRef, i) = i;
}
while (path < heapLen)
{
if (path + 1 < heapLen && values[Unsafe.Add(ref heapRef, path)] > values[Unsafe.Add(ref heapRef, path + 1)])
// Construct the Huffman tree by repeatedly combining the least two
// frequent nodes.
do
{
path++;
int first = Unsafe.Add(ref heapRef, 0);
int last = Unsafe.Add(ref heapRef, --heapLen);
// Propagate the hole to the leafs of the heap
int ppos = 0;
int path = 1;
while (path < heapLen)
{
if (path + 1 < heapLen && Unsafe.Add(ref valuesRef, Unsafe.Add(ref heapRef, path)) > Unsafe.Add(ref valuesRef, Unsafe.Add(ref heapRef, path + 1)))
{
path++;
}
Unsafe.Add(ref heapRef, ppos) = Unsafe.Add(ref heapRef, path);
ppos = path;
path = (path * 2) + 1;
}
// Now propagate the last element down along path. Normally
// it shouldn't go too deep.
int lastVal = Unsafe.Add(ref valuesRef, last);
while ((path = ppos) > 0
&& Unsafe.Add(ref valuesRef, Unsafe.Add(ref heapRef, ppos = (path - 1) >> 1)) > lastVal)
{
Unsafe.Add(ref heapRef, path) = Unsafe.Add(ref heapRef, ppos);
}
Unsafe.Add(ref heapRef, path) = last;
int second = Unsafe.Add(ref heapRef, 0);
// Create a new node father of first and second
last = numNodes++;
Unsafe.Add(ref childrenRef, 2 * last) = first;
Unsafe.Add(ref childrenRef, (2 * last) + 1) = second;
int mindepth = Math.Min(Unsafe.Add(ref valuesRef, first) & 0xFF, Unsafe.Add(ref valuesRef, second) & 0xFF);
Unsafe.Add(ref valuesRef, last) = lastVal = Unsafe.Add(ref valuesRef, first) + Unsafe.Add(ref valuesRef, second) - mindepth + 1;
// Again, propagate the hole to the leafs
ppos = 0;
path = 1;
while (path < heapLen)
{
if (path + 1 < heapLen
&& Unsafe.Add(ref valuesRef, Unsafe.Add(ref heapRef, path)) > Unsafe.Add(ref valuesRef, Unsafe.Add(ref heapRef, path + 1)))
{
path++;
}
Unsafe.Add(ref heapRef, ppos) = Unsafe.Add(ref heapRef, path);
ppos = path;
path = (ppos * 2) + 1;
}
// Now propagate the new element down along path
while ((path = ppos) > 0 && Unsafe.Add(ref valuesRef, Unsafe.Add(ref heapRef, ppos = (path - 1) >> 1)) > lastVal)
{
Unsafe.Add(ref heapRef, path) = Unsafe.Add(ref heapRef, ppos);
}
Unsafe.Add(ref heapRef, path) = last;
}
while (heapLen > 1);
Unsafe.Add(ref heapRef, ppos) = Unsafe.Add(ref heapRef, path);
ppos = path;
path = (ppos * 2) + 1;
}
if (Unsafe.Add(ref heapRef, 0) != (childrenLength >> 1) - 1)
{
DeflateThrowHelper.ThrowHeapViolated();
}
// Now propagate the new element down along path
while ((path = ppos) > 0 && values[Unsafe.Add(ref heapRef, ppos = (path - 1) / 2)] > lastVal)
{
Unsafe.Add(ref heapRef, path) = Unsafe.Add(ref heapRef, ppos);
this.BuildLength(childrenMemoryOwner.Memory.Span);
}
Unsafe.Add(ref heapRef, path) = last;
}
while (heapLen > 1);
if (Unsafe.Add(ref heapRef, 0) != (childs.Length / 2) - 1)
{
DeflateThrowHelper.ThrowHeapViolated();
}
this.BuildLength(childs);
}
/// <summary>
@ -717,10 +709,10 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// </summary>
public void CalcBLFreq(Tree blTree)
{
int max_count; /* max repeat count */
int min_count; /* min repeat count */
int count; /* repeat count of the current code */
int curlen = -1; /* length of current code */
int maxCount; // max repeat count
int minCount; // min repeat count
int count; // repeat count of the current code
int curLen = -1; // length of current code
int i = 0;
while (i < this.NumCodes)
@ -729,37 +721,37 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
int nextlen = this.Length[i];
if (nextlen == 0)
{
max_count = 138;
min_count = 3;
maxCount = 138;
minCount = 3;
}
else
{
max_count = 6;
min_count = 3;
if (curlen != nextlen)
maxCount = 6;
minCount = 3;
if (curLen != nextlen)
{
blTree.Frequencies[nextlen]++;
count = 0;
}
}
curlen = nextlen;
curLen = nextlen;
i++;
while (i < this.NumCodes && curlen == this.Length[i])
while (i < this.NumCodes && curLen == this.Length[i])
{
i++;
if (++count >= max_count)
if (++count >= maxCount)
{
break;
}
}
if (count < min_count)
if (count < minCount)
{
blTree.Frequencies[curlen] += (short)count;
blTree.Frequencies[curLen] += (short)count;
}
else if (curlen != 0)
else if (curLen != 0)
{
blTree.Frequencies[Repeat3To6]++;
}
@ -781,10 +773,10 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// <param name="bitLengthTree">The tree to write.</param>
public void WriteTree(DeflaterPendingBuffer pendingBuffer, Tree bitLengthTree)
{
int max_count; // max repeat count
int min_count; // min repeat count
int count; // repeat count of the current code
int curlen = -1; // length of current code
int maxCount; // max repeat count
int minCount; // min repeat count
int count; // repeat count of the current code
int curLen = -1; // length of current code
int i = 0;
while (i < this.NumCodes)
@ -793,40 +785,40 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
int nextlen = this.Length[i];
if (nextlen == 0)
{
max_count = 138;
min_count = 3;
maxCount = 138;
minCount = 3;
}
else
{
max_count = 6;
min_count = 3;
if (curlen != nextlen)
maxCount = 6;
minCount = 3;
if (curLen != nextlen)
{
bitLengthTree.WriteSymbol(pendingBuffer, nextlen);
count = 0;
}
}
curlen = nextlen;
curLen = nextlen;
i++;
while (i < this.NumCodes && curlen == this.Length[i])
while (i < this.NumCodes && curLen == this.Length[i])
{
i++;
if (++count >= max_count)
if (++count >= maxCount)
{
break;
}
}
if (count < min_count)
if (count < minCount)
{
while (count-- > 0)
{
bitLengthTree.WriteSymbol(pendingBuffer, curlen);
bitLengthTree.WriteSymbol(pendingBuffer, curLen);
}
}
else if (curlen != 0)
else if (curLen != 0)
{
bitLengthTree.WriteSymbol(pendingBuffer, Repeat3To6);
pendingBuffer.WriteBits(count - 3, 2);
@ -844,10 +836,10 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
}
}
private void BuildLength(int[] children)
private void BuildLength(ReadOnlySpan<int> children)
{
int numNodes = children.Length / 2;
int numLeafs = (numNodes + 1) / 2;
int numNodes = children.Length >> 1;
int numLeafs = (numNodes + 1) >> 1;
int overflow = 0;
for (int i = 0; i < this.maxLength; i++)
@ -936,26 +928,17 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!this.isDisposed)
{
if (disposing)
{
this.frequenciesMemoryHandle.Dispose();
this.frequenciesMemoryOwner.Dispose();
this.frequenciesMemoryHandle.Dispose();
this.frequenciesMemoryOwner.Dispose();
this.lengthsMemoryHandle.Dispose();
this.lengthsMemoryOwner.Dispose();
this.lengthsMemoryHandle.Dispose();
this.lengthsMemoryOwner.Dispose();
this.codesMemoryHandle.Dispose();
this.codesMemoryOwner.Dispose();
}
this.codesMemoryHandle.Dispose();
this.codesMemoryOwner.Dispose();
this.frequenciesMemoryOwner = null;
this.lengthsMemoryOwner = null;
@ -963,6 +946,8 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
this.isDisposed = true;
}
GC.SuppressFinalize(this);
}
}
}

477
src/ImageSharp/Formats/Png/Zlib/DeflaterOutputStream.cs

@ -1,466 +1,155 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
// <auto-generated/>
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SixLabors.Memory;
namespace SixLabors.ImageSharp.Formats.Png.Zlib
{
/// <summary>
/// A special stream deflating or compressing the bytes that are
/// written to it. It uses a Deflater to perform actual deflating.<br/>
/// Authors of the original java version : Tom Tromey, Jochen Hoenicke
/// written to it. It uses a Deflater to perform actual deflating.
/// </summary>
public class DeflaterOutputStream : Stream
internal sealed class DeflaterOutputStream : Stream
{
#region Constructors
/// <summary>
/// Creates a new DeflaterOutputStream with the given Deflater and
/// default buffer size.
/// </summary>
/// <param name="baseOutputStream">
/// the output stream where deflated output should be written.
/// </param>
/// <param name="deflater">
/// the underlying deflater.
/// </param>
public DeflaterOutputStream(Stream baseOutputStream, Deflater deflater)
: this(baseOutputStream, deflater, 512)
{
}
private const int BufferLength = 512;
private IManagedByteBuffer memoryOwner;
private readonly byte[] buffer;
private Deflater deflater;
private readonly Stream rawStream;
private bool isDisposed;
/// <summary>
/// Creates a new DeflaterOutputStream with the given Deflater and
/// buffer size.
/// Initializes a new instance of the <see cref="DeflaterOutputStream"/> class.
/// </summary>
/// <param name="baseOutputStream">
/// The output stream where deflated output is written.
/// </param>
/// <param name="deflater">
/// The underlying deflater to use
/// </param>
/// <param name="bufferSize">
/// The buffer size in bytes to use when deflating (minimum value 512)
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// bufsize is less than or equal to zero.
/// </exception>
/// <exception cref="ArgumentException">
/// baseOutputStream does not support writing
/// </exception>
/// <exception cref="ArgumentNullException">
/// deflater instance is null
/// </exception>
public DeflaterOutputStream(Stream baseOutputStream, Deflater deflater, int bufferSize)
/// <param name="memoryAllocator">The memory allocator to use for buffer allocations.</param>
/// <param name="rawStream">The output stream where deflated output is written.</param>
/// <param name="compressionLevel">The compression level.</param>
public DeflaterOutputStream(MemoryAllocator memoryAllocator, Stream rawStream, int compressionLevel)
{
if (baseOutputStream == null)
{
throw new ArgumentNullException(nameof(baseOutputStream));
}
if (baseOutputStream.CanWrite == false)
{
throw new ArgumentException("Must support writing", nameof(baseOutputStream));
}
if (bufferSize < 512)
{
throw new ArgumentOutOfRangeException(nameof(bufferSize));
}
baseOutputStream_ = baseOutputStream;
buffer_ = new byte[bufferSize];
deflater_ = deflater ?? throw new ArgumentNullException(nameof(deflater));
this.rawStream = rawStream;
this.memoryOwner = memoryAllocator.AllocateManagedByteBuffer(BufferLength);
this.buffer = this.memoryOwner.Array;
this.deflater = new Deflater(memoryAllocator, compressionLevel);
}
#endregion Constructors
/// <inheritdoc/>
public override bool CanRead => false;
#region Public API
/// <inheritdoc/>
public override bool CanSeek => false;
/// <summary>
/// Finishes the stream by calling finish() on the deflater.
/// </summary>
/// <exception cref="ImageFormatException">
/// Not all input is deflated
/// </exception>
public virtual void Finish()
{
deflater_.Finish();
while (!deflater_.IsFinished)
{
int len = deflater_.Deflate(buffer_, 0, buffer_.Length);
if (len <= 0)
{
break;
}
/// <inheritdoc/>
public override bool CanWrite => this.rawStream.CanWrite;
baseOutputStream_.Write(buffer_, 0, len);
}
/// <inheritdoc/>
public override long Length => this.rawStream.Length;
if (!deflater_.IsFinished)
/// <inheritdoc/>
public override long Position
{
get
{
throw new ImageFormatException("Can't deflate all input?");
return this.rawStream.Position;
}
baseOutputStream_.Flush();
}
/// <summary>
/// Gets or sets a flag indicating ownership of underlying stream.
/// When the flag is true <see cref="Stream.Dispose()" /> will close the underlying stream also.
/// </summary>
/// <remarks>The default value is true.</remarks>
public bool IsStreamOwner { get; set; } = true;
/// <summary>
/// Allows client to determine if an entry can be patched after its added
/// </summary>
public bool CanPatchEntries
{
get
set
{
return baseOutputStream_.CanSeek;
throw new NotSupportedException();
}
}
#endregion Public API
//#region Encryption
//private string password;
//private ICryptoTransform cryptoTransform_;
/// <inheritdoc/>
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
///// <summary>
///// Returns the 10 byte AUTH CODE to be appended immediately following the AES data stream.
///// </summary>
//protected byte[] AESAuthCode;
/// <inheritdoc/>
public override void SetLength(long value) => throw new NotSupportedException();
///// <summary>
///// Get/set the password used for encryption.
///// </summary>
///// <remarks>When set to null or if the password is empty no encryption is performed</remarks>
//public string Password
//{
// get
// {
// return password;
// }
// set
// {
// if ((value != null) && (value.Length == 0))
// {
// password = null;
// }
// else
// {
// password = value;
// }
// }
//}
/// <inheritdoc/>
public override int ReadByte() => throw new NotSupportedException();
///// <summary>
///// Encrypt a block of data
///// </summary>
///// <param name="buffer">
///// Data to encrypt. NOTE the original contents of the buffer are lost
///// </param>
///// <param name="offset">
///// Offset of first byte in buffer to encrypt
///// </param>
///// <param name="length">
///// Number of bytes in buffer to encrypt
///// </param>
//protected void EncryptBlock(byte[] buffer, int offset, int length)
//{
// cryptoTransform_.TransformBlock(buffer, 0, length, buffer, 0);
//}
/// <inheritdoc/>
public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
///// <summary>
///// Initializes encryption keys based on given <paramref name="password"/>.
///// </summary>
///// <param name="password">The password.</param>
//protected void InitializePassword(string password)
//{
// var pkManaged = new PkzipClassicManaged();
// byte[] key = PkzipClassic.GenerateKeys(ZipStrings.ConvertToArray(password));
// cryptoTransform_ = pkManaged.CreateEncryptor(key, null);
//}
///// <summary>
///// Initializes encryption keys based on given password.
///// </summary>
//protected void InitializeAESPassword(ZipEntry entry, string rawPassword,
// out byte[] salt, out byte[] pwdVerifier)
//{
// salt = new byte[entry.AESSaltLen];
// // Salt needs to be cryptographically random, and unique per file
// if (_aesRnd == null)
// _aesRnd = RandomNumberGenerator.Create();
// _aesRnd.GetBytes(salt);
// int blockSize = entry.AESKeySize / 8; // bits to bytes
// cryptoTransform_ = new ZipAESTransform(rawPassword, salt, blockSize, true);
// pwdVerifier = ((ZipAESTransform)cryptoTransform_).PwdVerifier;
//}
//#endregion Encryption
#region Deflation Support
/// <inheritdoc/>
public override void Flush()
{
this.deflater.Flush();
this.Deflate(true);
this.rawStream.Flush();
}
/// <summary>
/// Deflates everything in the input buffers. This will call
/// <code>def.deflate()</code> until all bytes from the input buffers
/// are processed.
/// </summary>
protected void Deflate()
/// <inheritdoc/>
public override void Write(byte[] buffer, int offset, int count)
{
Deflate(false);
this.deflater.SetInput(buffer, offset, count);
this.Deflate();
}
private void Deflate() => this.Deflate(false);
private void Deflate(bool flushing)
{
while (flushing || !deflater_.IsNeedingInput)
while (flushing || !this.deflater.IsNeedingInput)
{
int deflateCount = deflater_.Deflate(buffer_, 0, buffer_.Length);
int deflateCount = this.deflater.Deflate(this.buffer, 0, BufferLength);
if (deflateCount <= 0)
{
break;
}
//if (cryptoTransform_ != null)
//{
// EncryptBlock(buffer_, 0, deflateCount);
//}
baseOutputStream_.Write(buffer_, 0, deflateCount);
this.rawStream.Write(this.buffer, 0, deflateCount);
}
if (!deflater_.IsNeedingInput)
if (!this.deflater.IsNeedingInput)
{
throw new ImageFormatException("DeflaterOutputStream can't deflate all input?");
DeflateThrowHelper.ThrowNoDeflate();
}
}
#endregion Deflation Support
#region Stream Overrides
/// <summary>
/// Gets value indicating stream can be read from
/// </summary>
public override bool CanRead
private void Finish()
{
get
this.deflater.Finish();
while (!this.deflater.IsFinished)
{
return false;
}
}
int len = this.deflater.Deflate(this.buffer, 0, BufferLength);
if (len <= 0)
{
break;
}
/// <summary>
/// Gets a value indicating if seeking is supported for this stream
/// This property always returns false
/// </summary>
public override bool CanSeek
{
get
{
return false;
this.rawStream.Write(this.buffer, 0, len);
}
}
/// <summary>
/// Get value indicating if this stream supports writing
/// </summary>
public override bool CanWrite
{
get
if (!this.deflater.IsFinished)
{
return baseOutputStream_.CanWrite;
DeflateThrowHelper.ThrowNoDeflate();
}
}
/// <summary>
/// Get current length of stream
/// </summary>
public override long Length
{
get
{
return baseOutputStream_.Length;
}
this.rawStream.Flush();
}
/// <summary>
/// Gets the current position within the stream.
/// </summary>
/// <exception cref="NotSupportedException">Any attempt to set position</exception>
public override long Position
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
get
{
return baseOutputStream_.Position;
}
set
if (this.isDisposed)
{
throw new NotSupportedException("Position property not supported");
return;
}
}
/// <summary>
/// Sets the current position of this stream to the given value. Not supported by this class!
/// </summary>
/// <param name="offset">The offset relative to the <paramref name="origin"/> to seek.</param>
/// <param name="origin">The <see cref="SeekOrigin"/> to seek from.</param>
/// <returns>The new position in the stream.</returns>
/// <exception cref="NotSupportedException">Any access</exception>
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException("DeflaterOutputStream Seek not supported");
}
/// <summary>
/// Sets the length of this stream to the given value. Not supported by this class!
/// </summary>
/// <param name="value">The new stream length.</param>
/// <exception cref="NotSupportedException">Any access</exception>
public override void SetLength(long value)
{
throw new NotSupportedException("DeflaterOutputStream SetLength not supported");
}
/// <summary>
/// Read a byte from stream advancing position by one
/// </summary>
/// <returns>The byte read cast to an int. THe value is -1 if at the end of the stream.</returns>
/// <exception cref="NotSupportedException">Any access</exception>
public override int ReadByte()
{
throw new NotSupportedException("DeflaterOutputStream ReadByte not supported");
}
/// <summary>
/// Read a block of bytes from stream
/// </summary>
/// <param name="buffer">The buffer to store read data in.</param>
/// <param name="offset">The offset to start storing at.</param>
/// <param name="count">The maximum number of bytes to read.</param>
/// <returns>The actual number of bytes read. Zero if end of stream is detected.</returns>
/// <exception cref="NotSupportedException">Any access</exception>
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException("DeflaterOutputStream Read not supported");
}
/// <summary>
/// Flushes the stream by calling <see cref="DeflaterOutputStream.Flush">Flush</see> on the deflater and then
/// on the underlying stream. This ensures that all bytes are flushed.
/// </summary>
public override void Flush()
{
deflater_.Flush();
Deflate(true);
baseOutputStream_.Flush();
}
/// <summary>
/// Calls <see cref="Finish"/> and closes the underlying
/// stream when <see cref="IsStreamOwner"></see> is true.
/// </summary>
protected override void Dispose(bool disposing)
{
if (!isClosed_)
if (disposing)
{
isClosed_ = true;
try
{
Finish();
}
finally
{
if (IsStreamOwner)
{
baseOutputStream_.Dispose();
}
}
this.Finish();
this.deflater.Dispose();
this.memoryOwner.Dispose();
}
}
///// <summary>
///// Get the Auth code for AES encrypted entries
///// </summary>
//protected void GetAuthCodeIfAES()
//{
// if (cryptoTransform_ is ZipAESTransform)
// {
// AESAuthCode = ((ZipAESTransform)cryptoTransform_).GetAuthCode();
// }
//}
/// <summary>
/// Writes a single byte to the compressed output stream.
/// </summary>
/// <param name="value">
/// The byte value.
/// </param>
public override void WriteByte(byte value)
{
byte[] b = new byte[1];
b[0] = value;
Write(b, 0, 1);
}
/// <summary>
/// Writes bytes from an array to the compressed stream.
/// </summary>
/// <param name="buffer">
/// The byte array
/// </param>
/// <param name="offset">
/// The offset into the byte array where to start.
/// </param>
/// <param name="count">
/// The number of bytes to write.
/// </param>
public override void Write(byte[] buffer, int offset, int count)
{
deflater_.SetInput(buffer, offset, count);
Deflate();
this.deflater = null;
this.memoryOwner = null;
this.isDisposed = true;
base.Dispose(disposing);
}
#endregion Stream Overrides
#region Instance Fields
/// <summary>
/// This buffer is used temporarily to retrieve the bytes from the
/// deflater and write them to the underlying output stream.
/// </summary>
private byte[] buffer_;
/// <summary>
/// The deflater which is used to deflate the stream.
/// </summary>
protected Deflater deflater_;
/// <summary>
/// Base stream the deflater depends on.
/// </summary>
protected Stream baseOutputStream_;
private bool isClosed_;
#endregion Instance Fields
#region Static Fields
// Static to help ensure that multiple files within a zip will get different random salt
//private static RandomNumberGenerator _aesRnd = RandomNumberGenerator.Create();
#endregion Static Fields
}
}

19
src/ImageSharp/Formats/Png/Zlib/DeflaterPendingBuffer.cs

@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// <summary>
/// Stores pending data for writing data to the Deflater.
/// </summary>
public sealed unsafe class DeflaterPendingBuffer : IDisposable
internal sealed unsafe class DeflaterPendingBuffer : IDisposable
{
private readonly byte[] buffer;
private readonly byte* pinnedBuffer;
@ -164,25 +164,16 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// <inheritdoc/>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!this.isDisposed)
{
if (disposing)
{
this.bufferMemoryHandle.Dispose();
this.bufferMemoryOwner.Dispose();
}
this.bufferMemoryHandle.Dispose();
this.bufferMemoryOwner.Dispose();
this.bufferMemoryOwner = null;
this.isDisposed = true;
}
GC.SuppressFinalize(this);
}
}
}

7
src/ImageSharp/Formats/Png/Zlib/README.md

@ -1,2 +1,5 @@
Adler32.cs and Crc32.cs have been copied from
https://github.com/ygrenier/SharpZipLib.Portable
Deflatestream implementation adapted from
https://github.com/icsharpcode/SharpZipLib
LIcensed under MIT

41
src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs

@ -41,8 +41,6 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
// private DeflateStream deflateStream;
private DeflaterOutputStream deflateStream;
private Deflater deflater;
/// <summary>
/// Initializes a new instance of the <see cref="ZlibDeflateStream"/> class.
/// </summary>
@ -92,21 +90,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
this.rawStream.WriteByte(Cmf);
this.rawStream.WriteByte((byte)flg);
// Initialize the deflate Stream.
// CompressionLevel level = CompressionLevel.Optimal;
//
// if (compressionLevel >= 1 && compressionLevel <= 5)
// {
// level = CompressionLevel.Fastest;
// }
// else if (compressionLevel == 0)
// {
// level = CompressionLevel.NoCompression;
// }
this.deflater = new Deflater(memoryAllocator, compressionLevel);
this.deflateStream = new DeflaterOutputStream(this.rawStream, this.deflater) { IsStreamOwner = false };
// this.deflateStream = new DeflateStream(this.rawStream, level, true);
this.deflateStream = new DeflaterOutputStream(memoryAllocator, this.rawStream, compressionLevel);
}
/// <inheritdoc/>
@ -116,16 +100,23 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
public override bool CanSeek => false;
/// <inheritdoc/>
public override bool CanWrite => true;
public override bool CanWrite => this.rawStream.CanWrite;
/// <inheritdoc/>
public override long Length => throw new NotSupportedException();
public override long Length => this.rawStream.Length;
/// <inheritdoc/>
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
get
{
return this.rawStream.Position;
}
set
{
throw new NotSupportedException();
}
}
/// <inheritdoc/>
@ -174,10 +165,6 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
{
this.deflateStream.Dispose();
this.deflateStream = null;
// TODO: Remove temporal coupling here.
this.deflater.Dispose();
this.deflater = null;
}
else
{
@ -195,10 +182,6 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
}
base.Dispose(disposing);
// Call the appropriate methods to clean up
// unmanaged resources here.
// Note disposing is done.
this.isDisposed = true;
}
}

Loading…
Cancel
Save