Browse Source

Optimize huffman and make all internal

pull/1054/head
James Jackson-South 7 years ago
parent
commit
d69f664570
  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. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace SixLabors.ImageSharp.Formats.Png.Zlib namespace SixLabors.ImageSharp.Formats.Png.Zlib
{ {
@ -141,9 +142,10 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
{ {
this.crc ^= CrcSeed; this.crc ^= CrcSeed;
ref uint crcTableRef = ref MemoryMarshal.GetReference(CrcTable.AsSpan());
for (int i = 0; i < data.Length; i++) 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; 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); public static void ThrowOutOfRange(string name) => throw new ArgumentOutOfRangeException(name);
[MethodImpl(InliningOptions.ColdPath)] [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)] [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. /// This class compresses input with the deflate algorithm described in RFC 1951.
/// It has several compression levels and three different strategies described below. /// It has several compression levels and three different strategies described below.
/// </summary> /// </summary>
public sealed class Deflater : IDisposable internal sealed class Deflater : IDisposable
{ {
/// <summary> /// <summary>
/// The best and slowest compression level. This tries to find very /// 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 /// Low level compression engine for deflate algorithm which uses a 32K sliding window
/// with secondary compression from Huffman/Shannon-Fano codes. /// with secondary compression from Huffman/Shannon-Fano codes.
/// </summary> /// </summary>
public sealed unsafe class DeflaterEngine : IDisposable internal sealed unsafe class DeflaterEngine : IDisposable
{ {
private const int TooFar = 4096; 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 /// Note that the array should really be unsigned short, so you need
/// to and the values with 0xFFFF. /// to and the values with 0xFFFF.
/// </summary> /// </summary>
private readonly IMemoryOwner<short> headBuffer; private IMemoryOwner<short> headMemoryOwner;
private MemoryHandle headBufferHandle; private MemoryHandle headMemoryHandle;
private readonly Memory<short> head; private readonly Memory<short> head;
private readonly short* pinnedHeadPointer; 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 /// Note that the array should really be unsigned short, so you need
/// to and the values with 0xFFFF. /// to and the values with 0xFFFF.
/// </summary> /// </summary>
private readonly IMemoryOwner<short> prevBuffer; private IMemoryOwner<short> prevMemoryOwner;
private MemoryHandle prevBufferHandle; private MemoryHandle prevMemoryHandle;
private readonly Memory<short> prev; private readonly Memory<short> prev;
private readonly short* pinnedPrevPointer; private readonly short* pinnedPrevPointer;
/// <summary> /// <summary>
/// This array contains the part of the uncompressed stream that /// 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> /// </summary>
private readonly IManagedByteBuffer windowBuffer; private IManagedByteBuffer windowMemoryOwner;
private MemoryHandle windowBufferHandle; private MemoryHandle windowMemoryHandle;
private readonly byte[] window; private readonly byte[] window;
private readonly byte* pinnedWindowPointer; private readonly byte* pinnedWindowPointer;
@ -153,20 +153,20 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
// Create pinned pointers to the various buffers to allow indexing // Create pinned pointers to the various buffers to allow indexing
// without bounds checks. // without bounds checks.
this.windowBuffer = memoryAllocator.AllocateManagedByteBuffer(2 * DeflaterConstants.WSIZE); this.windowMemoryOwner = memoryAllocator.AllocateManagedByteBuffer(2 * DeflaterConstants.WSIZE);
this.window = this.windowBuffer.Array; this.window = this.windowMemoryOwner.Array;
this.windowBufferHandle = this.windowBuffer.Memory.Pin(); this.windowMemoryHandle = this.windowMemoryOwner.Memory.Pin();
this.pinnedWindowPointer = (byte*)this.windowBufferHandle.Pointer; this.pinnedWindowPointer = (byte*)this.windowMemoryHandle.Pointer;
this.headBuffer = memoryAllocator.Allocate<short>(DeflaterConstants.HASH_SIZE); this.headMemoryOwner = memoryAllocator.Allocate<short>(DeflaterConstants.HASH_SIZE);
this.head = this.headBuffer.Memory; this.head = this.headMemoryOwner.Memory;
this.headBufferHandle = this.headBuffer.Memory.Pin(); this.headMemoryHandle = this.headMemoryOwner.Memory.Pin();
this.pinnedHeadPointer = (short*)this.headBufferHandle.Pointer; this.pinnedHeadPointer = (short*)this.headMemoryHandle.Pointer;
this.prevBuffer = memoryAllocator.Allocate<short>(DeflaterConstants.WSIZE); this.prevMemoryOwner = memoryAllocator.Allocate<short>(DeflaterConstants.WSIZE);
this.prev = this.prevBuffer.Memory; this.prev = this.prevMemoryOwner.Memory;
this.prevBufferHandle = this.prevBuffer.Memory.Pin(); this.prevMemoryHandle = this.prevMemoryOwner.Memory.Pin();
this.pinnedPrevPointer = (short*)this.prevBufferHandle.Pointer; this.pinnedPrevPointer = (short*)this.prevMemoryHandle.Pointer;
// We start at index 1, to avoid an implementation deficiency, that // We start at index 1, to avoid an implementation deficiency, that
// we cannot build a repeat pattern at index 0. // we cannot build a repeat pattern at index 0.
@ -377,8 +377,27 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// <inheritdoc/> /// <inheritdoc/>
public void Dispose() public void Dispose()
{ {
// Do not change this code. Put cleanup code in Dispose(bool disposing) above. if (!this.isDisposed)
this.Dispose(true); {
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); GC.SuppressFinalize(this);
} }
@ -830,29 +849,5 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
return true; 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> /// <summary>
/// Performs Deflate Huffman encoding. /// Performs Deflate Huffman encoding.
/// </summary> /// </summary>
public sealed unsafe class DeflaterHuffman : IDisposable internal sealed unsafe class DeflaterHuffman : IDisposable
{ {
private const int BufferSize = 1 << (DeflaterConstants.DEFAULT_MEM_LEVEL + 6); private const int BufferSize = 1 << (DeflaterConstants.DEFAULT_MEM_LEVEL + 6);
@ -194,7 +194,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
int dc = Dcode(dist); int dc = Dcode(dist);
this.distTree.WriteSymbol(pendingBuffer, dc); this.distTree.WriteSymbol(pendingBuffer, dc);
bits = (dc / 2) - 1; bits = (dc >> 1) - 1;
if (bits > 0) if (bits > 0)
{ {
this.Pending.WriteBits(dist & ((1 << bits) - 1), bits); this.Pending.WriteBits(dist & ((1 << bits) - 1), bits);
@ -349,7 +349,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
this.distTree.Frequencies[dc]++; this.distTree.Frequencies[dc]++;
if (dc >= 4) if (dc >= 4)
{ {
this.extraBits += (dc / 2) - 1; this.extraBits += (dc >> 1) - 1;
} }
return this.IsFull(); return this.IsFull();
@ -443,9 +443,11 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
private readonly int elementCount; private readonly int elementCount;
private readonly MemoryAllocator memoryAllocator;
private IMemoryOwner<short> codesMemoryOwner; private IMemoryOwner<short> codesMemoryOwner;
private MemoryHandle codesMemoryHandle; private MemoryHandle codesMemoryHandle;
private short* codes; private readonly short* codes;
private IMemoryOwner<short> frequenciesMemoryOwner; private IMemoryOwner<short> frequenciesMemoryOwner;
private MemoryHandle frequenciesMemoryHandle; private MemoryHandle frequenciesMemoryHandle;
@ -455,8 +457,8 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
public Tree(MemoryAllocator memoryAllocator, int elements, int minCodes, int maxLength) public Tree(MemoryAllocator memoryAllocator, int elements, int minCodes, int maxLength)
{ {
this.memoryAllocator = memoryAllocator;
this.elementCount = elements; this.elementCount = elements;
this.minNumCodes = minCodes; this.minNumCodes = minCodes;
this.maxLength = maxLength; this.maxLength = maxLength;
@ -497,27 +499,6 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
public void WriteSymbol(DeflaterPendingBuffer pendingBuffer, int code) public void WriteSymbol(DeflaterPendingBuffer pendingBuffer, int code)
=> pendingBuffer.WriteBits(this.codes[code] & 0xFFFF, this.Length[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> /// <summary>
/// Set static codes and length /// Set static codes and length
/// </summary> /// </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 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. // the nodes 2*n+1, 2*n+2 are the child nodes of node n.
// Maxes out at 286 * 4 // Maxes out at 286 * 4 so too large for the stack.
Span<int> heap = stackalloc int[numSymbols]; using (IMemoryOwner<int> heapMemoryOwner = this.memoryAllocator.Allocate<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
{ {
int first = heap[0]; ref int heapRef = ref MemoryMarshal.GetReference(heapMemoryOwner.Memory.Span);
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) 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 // We could encode a single literal with 0 bits but then we
// it shouldn't go too deep. // don't see the literals. Therefore we force at least two
int lastVal = values[last]; // literals to avoid this case. We don't care about order in
while ((path = ppos) > 0 && values[Unsafe.Add(ref heapRef, ppos = (path - 1) / 2)] > lastVal) // 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; this.NumCodes = Math.Max(maxCode + 1, this.minNumCodes);
int second = Unsafe.Add(ref heapRef, 0);
// Create a new node father of first and second int numLeafs = heapLen;
last = numNodes++; int childrenLength = (4 * heapLen) - 2;
childs[2 * last] = first; using (IMemoryOwner<int> childrenMemoryOwner = this.memoryAllocator.Allocate<int>(childrenLength))
childs[(2 * last) + 1] = second; using (IMemoryOwner<int> valuesMemoryOwner = this.memoryAllocator.Allocate<int>((2 * heapLen) - 1))
int mindepth = Math.Min(values[first] & 0xFF, values[second] & 0xFF); {
values[last] = lastVal = values[first] + values[second] - mindepth + 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 for (int i = 0; i < heapLen; i++)
ppos = 0; {
path = 1; 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) // Construct the Huffman tree by repeatedly combining the least two
{ // frequent nodes.
if (path + 1 < heapLen && values[Unsafe.Add(ref heapRef, path)] > values[Unsafe.Add(ref heapRef, path + 1)]) 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); if (Unsafe.Add(ref heapRef, 0) != (childrenLength >> 1) - 1)
ppos = path; {
path = (ppos * 2) + 1; DeflateThrowHelper.ThrowHeapViolated();
} }
// Now propagate the new element down along path this.BuildLength(childrenMemoryOwner.Memory.Span);
while ((path = ppos) > 0 && values[Unsafe.Add(ref heapRef, ppos = (path - 1) / 2)] > lastVal)
{
Unsafe.Add(ref heapRef, path) = Unsafe.Add(ref heapRef, ppos);
} }
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> /// <summary>
@ -717,10 +709,10 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// </summary> /// </summary>
public void CalcBLFreq(Tree blTree) public void CalcBLFreq(Tree blTree)
{ {
int max_count; /* max repeat count */ int maxCount; // max repeat count
int min_count; /* min repeat count */ int minCount; // min repeat count
int count; /* repeat count of the current code */ int count; // repeat count of the current code
int curlen = -1; /* length of current code */ int curLen = -1; // length of current code
int i = 0; int i = 0;
while (i < this.NumCodes) while (i < this.NumCodes)
@ -729,37 +721,37 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
int nextlen = this.Length[i]; int nextlen = this.Length[i];
if (nextlen == 0) if (nextlen == 0)
{ {
max_count = 138; maxCount = 138;
min_count = 3; minCount = 3;
} }
else else
{ {
max_count = 6; maxCount = 6;
min_count = 3; minCount = 3;
if (curlen != nextlen) if (curLen != nextlen)
{ {
blTree.Frequencies[nextlen]++; blTree.Frequencies[nextlen]++;
count = 0; count = 0;
} }
} }
curlen = nextlen; curLen = nextlen;
i++; i++;
while (i < this.NumCodes && curlen == this.Length[i]) while (i < this.NumCodes && curLen == this.Length[i])
{ {
i++; i++;
if (++count >= max_count) if (++count >= maxCount)
{ {
break; 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]++; blTree.Frequencies[Repeat3To6]++;
} }
@ -781,10 +773,10 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// <param name="bitLengthTree">The tree to write.</param> /// <param name="bitLengthTree">The tree to write.</param>
public void WriteTree(DeflaterPendingBuffer pendingBuffer, Tree bitLengthTree) public void WriteTree(DeflaterPendingBuffer pendingBuffer, Tree bitLengthTree)
{ {
int max_count; // max repeat count int maxCount; // max repeat count
int min_count; // min repeat count int minCount; // min repeat count
int count; // repeat count of the current code int count; // repeat count of the current code
int curlen = -1; // length of current code int curLen = -1; // length of current code
int i = 0; int i = 0;
while (i < this.NumCodes) while (i < this.NumCodes)
@ -793,40 +785,40 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
int nextlen = this.Length[i]; int nextlen = this.Length[i];
if (nextlen == 0) if (nextlen == 0)
{ {
max_count = 138; maxCount = 138;
min_count = 3; minCount = 3;
} }
else else
{ {
max_count = 6; maxCount = 6;
min_count = 3; minCount = 3;
if (curlen != nextlen) if (curLen != nextlen)
{ {
bitLengthTree.WriteSymbol(pendingBuffer, nextlen); bitLengthTree.WriteSymbol(pendingBuffer, nextlen);
count = 0; count = 0;
} }
} }
curlen = nextlen; curLen = nextlen;
i++; i++;
while (i < this.NumCodes && curlen == this.Length[i]) while (i < this.NumCodes && curLen == this.Length[i])
{ {
i++; i++;
if (++count >= max_count) if (++count >= maxCount)
{ {
break; break;
} }
} }
if (count < min_count) if (count < minCount)
{ {
while (count-- > 0) while (count-- > 0)
{ {
bitLengthTree.WriteSymbol(pendingBuffer, curlen); bitLengthTree.WriteSymbol(pendingBuffer, curLen);
} }
} }
else if (curlen != 0) else if (curLen != 0)
{ {
bitLengthTree.WriteSymbol(pendingBuffer, Repeat3To6); bitLengthTree.WriteSymbol(pendingBuffer, Repeat3To6);
pendingBuffer.WriteBits(count - 3, 2); 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 numNodes = children.Length >> 1;
int numLeafs = (numNodes + 1) / 2; int numLeafs = (numNodes + 1) >> 1;
int overflow = 0; int overflow = 0;
for (int i = 0; i < this.maxLength; i++) for (int i = 0; i < this.maxLength; i++)
@ -936,26 +928,17 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
} }
public void Dispose() public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{ {
if (!this.isDisposed) if (!this.isDisposed)
{ {
if (disposing) this.frequenciesMemoryHandle.Dispose();
{ this.frequenciesMemoryOwner.Dispose();
this.frequenciesMemoryHandle.Dispose();
this.frequenciesMemoryOwner.Dispose();
this.lengthsMemoryHandle.Dispose(); this.lengthsMemoryHandle.Dispose();
this.lengthsMemoryOwner.Dispose(); this.lengthsMemoryOwner.Dispose();
this.codesMemoryHandle.Dispose(); this.codesMemoryHandle.Dispose();
this.codesMemoryOwner.Dispose(); this.codesMemoryOwner.Dispose();
}
this.frequenciesMemoryOwner = null; this.frequenciesMemoryOwner = null;
this.lengthsMemoryOwner = null; this.lengthsMemoryOwner = null;
@ -963,6 +946,8 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
this.isDisposed = true; this.isDisposed = true;
} }
GC.SuppressFinalize(this);
} }
} }
} }

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

@ -1,466 +1,155 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
// <auto-generated/>
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using SixLabors.Memory;
namespace SixLabors.ImageSharp.Formats.Png.Zlib namespace SixLabors.ImageSharp.Formats.Png.Zlib
{ {
/// <summary> /// <summary>
/// A special stream deflating or compressing the bytes that are /// A special stream deflating or compressing the bytes that are
/// written to it. It uses a Deflater to perform actual deflating.<br/> /// written to it. It uses a Deflater to perform actual deflating.
/// Authors of the original java version : Tom Tromey, Jochen Hoenicke
/// </summary> /// </summary>
public class DeflaterOutputStream : Stream internal sealed class DeflaterOutputStream : Stream
{ {
#region Constructors private const int BufferLength = 512;
/// <summary> private IManagedByteBuffer memoryOwner;
/// Creates a new DeflaterOutputStream with the given Deflater and private readonly byte[] buffer;
/// default buffer size. private Deflater deflater;
/// </summary> private readonly Stream rawStream;
/// <param name="baseOutputStream"> private bool isDisposed;
/// 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)
{
}
/// <summary> /// <summary>
/// Creates a new DeflaterOutputStream with the given Deflater and /// Initializes a new instance of the <see cref="DeflaterOutputStream"/> class.
/// buffer size.
/// </summary> /// </summary>
/// <param name="baseOutputStream"> /// <param name="memoryAllocator">The memory allocator to use for buffer allocations.</param>
/// The output stream where deflated output is written. /// <param name="rawStream">The output stream where deflated output is written.</param>
/// </param> /// <param name="compressionLevel">The compression level.</param>
/// <param name="deflater"> public DeflaterOutputStream(MemoryAllocator memoryAllocator, Stream rawStream, int compressionLevel)
/// 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)
{ {
if (baseOutputStream == null) this.rawStream = rawStream;
{ this.memoryOwner = memoryAllocator.AllocateManagedByteBuffer(BufferLength);
throw new ArgumentNullException(nameof(baseOutputStream)); this.buffer = this.memoryOwner.Array;
} this.deflater = new Deflater(memoryAllocator, compressionLevel);
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));
} }
#endregion Constructors /// <inheritdoc/>
public override bool CanRead => false;
#region Public API /// <inheritdoc/>
public override bool CanSeek => false;
/// <summary> /// <inheritdoc/>
/// Finishes the stream by calling finish() on the deflater. public override bool CanWrite => this.rawStream.CanWrite;
/// </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;
}
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(); set
}
/// <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
{ {
return baseOutputStream_.CanSeek; throw new NotSupportedException();
} }
} }
#endregion Public API /// <inheritdoc/>
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
//#region Encryption
//private string password;
//private ICryptoTransform cryptoTransform_;
///// <summary> /// <inheritdoc/>
///// Returns the 10 byte AUTH CODE to be appended immediately following the AES data stream. public override void SetLength(long value) => throw new NotSupportedException();
///// </summary>
//protected byte[] AESAuthCode;
///// <summary> /// <inheritdoc/>
///// Get/set the password used for encryption. public override int ReadByte() => throw new NotSupportedException();
///// </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;
// }
// }
//}
///// <summary> /// <inheritdoc/>
///// Encrypt a block of data public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
///// </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);
//}
///// <summary> /// <inheritdoc/>
///// Initializes encryption keys based on given <paramref name="password"/>. public override void Flush()
///// </summary> {
///// <param name="password">The password.</param> this.deflater.Flush();
//protected void InitializePassword(string password) this.Deflate(true);
//{ this.rawStream.Flush();
// 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
/// <summary> /// <inheritdoc/>
/// Deflates everything in the input buffers. This will call public override void Write(byte[] buffer, int offset, int count)
/// <code>def.deflate()</code> until all bytes from the input buffers
/// are processed.
/// </summary>
protected void Deflate()
{ {
Deflate(false); this.deflater.SetInput(buffer, offset, count);
this.Deflate();
} }
private void Deflate() => this.Deflate(false);
private void Deflate(bool flushing) 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) if (deflateCount <= 0)
{ {
break; 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 private void Finish()
#region Stream Overrides
/// <summary>
/// Gets value indicating stream can be read from
/// </summary>
public override bool CanRead
{ {
get this.deflater.Finish();
while (!this.deflater.IsFinished)
{ {
return false; int len = this.deflater.Deflate(this.buffer, 0, BufferLength);
} if (len <= 0)
} {
break;
}
/// <summary> this.rawStream.Write(this.buffer, 0, len);
/// Gets a value indicating if seeking is supported for this stream
/// This property always returns false
/// </summary>
public override bool CanSeek
{
get
{
return false;
} }
}
/// <summary> if (!this.deflater.IsFinished)
/// Get value indicating if this stream supports writing
/// </summary>
public override bool CanWrite
{
get
{ {
return baseOutputStream_.CanWrite; DeflateThrowHelper.ThrowNoDeflate();
} }
}
/// <summary> this.rawStream.Flush();
/// Get current length of stream
/// </summary>
public override long Length
{
get
{
return baseOutputStream_.Length;
}
} }
/// <summary> /// <inheritdoc/>
/// Gets the current position within the stream. protected override void Dispose(bool disposing)
/// </summary>
/// <exception cref="NotSupportedException">Any attempt to set position</exception>
public override long Position
{ {
get if (this.isDisposed)
{
return baseOutputStream_.Position;
}
set
{ {
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> if (disposing)
/// 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_)
{ {
isClosed_ = true; this.Finish();
this.deflater.Dispose();
try this.memoryOwner.Dispose();
{
Finish();
}
finally
{
if (IsStreamOwner)
{
baseOutputStream_.Dispose();
}
}
} }
}
///// <summary>
///// Get the Auth code for AES encrypted entries
///// </summary>
//protected void GetAuthCodeIfAES()
//{
// if (cryptoTransform_ is ZipAESTransform)
// {
// AESAuthCode = ((ZipAESTransform)cryptoTransform_).GetAuthCode();
// }
//}
/// <summary> this.deflater = null;
/// Writes a single byte to the compressed output stream. this.memoryOwner = null;
/// </summary> this.isDisposed = true;
/// <param name="value"> base.Dispose(disposing);
/// 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();
} }
#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> /// <summary>
/// Stores pending data for writing data to the Deflater. /// Stores pending data for writing data to the Deflater.
/// </summary> /// </summary>
public sealed unsafe class DeflaterPendingBuffer : IDisposable internal sealed unsafe class DeflaterPendingBuffer : IDisposable
{ {
private readonly byte[] buffer; private readonly byte[] buffer;
private readonly byte* pinnedBuffer; private readonly byte* pinnedBuffer;
@ -164,25 +164,16 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// <inheritdoc/> /// <inheritdoc/>
public void Dispose() public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{ {
if (!this.isDisposed) if (!this.isDisposed)
{ {
if (disposing) this.bufferMemoryHandle.Dispose();
{ this.bufferMemoryOwner.Dispose();
this.bufferMemoryHandle.Dispose();
this.bufferMemoryOwner.Dispose();
}
this.bufferMemoryOwner = null; this.bufferMemoryOwner = null;
this.isDisposed = true; 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 Deflatestream implementation adapted from
https://github.com/ygrenier/SharpZipLib.Portable
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 DeflateStream deflateStream;
private DeflaterOutputStream deflateStream; private DeflaterOutputStream deflateStream;
private Deflater deflater;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ZlibDeflateStream"/> class. /// Initializes a new instance of the <see cref="ZlibDeflateStream"/> class.
/// </summary> /// </summary>
@ -92,21 +90,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
this.rawStream.WriteByte(Cmf); this.rawStream.WriteByte(Cmf);
this.rawStream.WriteByte((byte)flg); this.rawStream.WriteByte((byte)flg);
// Initialize the deflate Stream. this.deflateStream = new DeflaterOutputStream(memoryAllocator, this.rawStream, compressionLevel);
// 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);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -116,16 +100,23 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
public override bool CanSeek => false; public override bool CanSeek => false;
/// <inheritdoc/> /// <inheritdoc/>
public override bool CanWrite => true; public override bool CanWrite => this.rawStream.CanWrite;
/// <inheritdoc/> /// <inheritdoc/>
public override long Length => throw new NotSupportedException(); public override long Length => this.rawStream.Length;
/// <inheritdoc/> /// <inheritdoc/>
public override long Position public override long Position
{ {
get => throw new NotSupportedException(); get
set => throw new NotSupportedException(); {
return this.rawStream.Position;
}
set
{
throw new NotSupportedException();
}
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -174,10 +165,6 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
{ {
this.deflateStream.Dispose(); this.deflateStream.Dispose();
this.deflateStream = null; this.deflateStream = null;
// TODO: Remove temporal coupling here.
this.deflater.Dispose();
this.deflater = null;
} }
else else
{ {
@ -195,10 +182,6 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
} }
base.Dispose(disposing); base.Dispose(disposing);
// Call the appropriate methods to clean up
// unmanaged resources here.
// Note disposing is done.
this.isDisposed = true; this.isDisposed = true;
} }
} }

Loading…
Cancel
Save