mirror of https://github.com/SixLabors/ImageSharp
9 changed files with 239 additions and 341 deletions
@ -1,21 +0,0 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
// <auto-generated/>
|
|||
namespace SixLabors.ImageSharp.Formats.Png.Zlib |
|||
{ |
|||
/// <summary>
|
|||
/// This class stores the pending output of the Deflater.
|
|||
///
|
|||
/// author of the original java version : Jochen Hoenicke
|
|||
/// </summary>
|
|||
public class DeflaterPending : PendingBuffer |
|||
{ |
|||
/// <summary>
|
|||
/// Construct instance with default buffer size
|
|||
/// </summary>
|
|||
public DeflaterPending() : base(DeflaterConstants.PENDING_BUF_SIZE) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,187 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.Buffers; |
|||
using System.Runtime.CompilerServices; |
|||
using SixLabors.Memory; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Png.Zlib |
|||
{ |
|||
/// <summary>
|
|||
/// Stores pending data for writing data to the Deflater.
|
|||
/// </summary>
|
|||
public sealed unsafe class DeflaterPendingBuffer : IDisposable |
|||
{ |
|||
private readonly byte[] buffer; |
|||
private readonly byte* pinnedBuffer; |
|||
private readonly IManagedByteBuffer managedBuffer; |
|||
private MemoryHandle handle; |
|||
|
|||
private int start; |
|||
private int end; |
|||
private uint bits; |
|||
private bool isDisposed; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DeflaterPendingBuffer"/> class.
|
|||
/// </summary>
|
|||
/// <param name="memoryAllocator">The memory allocator to use for buffer allocations.</param>
|
|||
public DeflaterPendingBuffer(MemoryAllocator memoryAllocator) |
|||
{ |
|||
this.buffer = new byte[DeflaterConstants.PENDING_BUF_SIZE]; |
|||
this.managedBuffer = memoryAllocator.AllocateManagedByteBuffer(DeflaterConstants.PENDING_BUF_SIZE); |
|||
this.buffer = this.managedBuffer.Array; |
|||
this.handle = this.managedBuffer.Memory.Pin(); |
|||
this.pinnedBuffer = (byte*)this.handle.Pointer; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the number of bits written to the buffer.
|
|||
/// </summary>
|
|||
public int BitCount { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets a value indicating whether indicates the buffer has been flushed.
|
|||
/// </summary>
|
|||
public bool IsFlushed => this.end == 0; |
|||
|
|||
/// <summary>
|
|||
/// Clear internal state/buffers.
|
|||
/// </summary>
|
|||
[MethodImpl(InliningOptions.ShortMethod)] |
|||
public void Reset() => this.start = this.end = this.BitCount = 0; |
|||
|
|||
/// <summary>
|
|||
/// Write a short value to buffer LSB first.
|
|||
/// </summary>
|
|||
/// <param name="value">The value to write.</param>
|
|||
[MethodImpl(InliningOptions.ShortMethod)] |
|||
public void WriteShort(int value) |
|||
{ |
|||
byte* pinned = this.pinnedBuffer; |
|||
pinned[this.end++] = unchecked((byte)value); |
|||
pinned[this.end++] = unchecked((byte)(value >> 8)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write a block of data to the internal buffer.
|
|||
/// </summary>
|
|||
/// <param name="block">The data to write.</param>
|
|||
/// <param name="offset">The offset of first byte to write.</param>
|
|||
/// <param name="length">The number of bytes to write.</param>
|
|||
[MethodImpl(InliningOptions.ShortMethod)] |
|||
public void WriteBlock(byte[] block, int offset, int length) |
|||
{ |
|||
Unsafe.CopyBlockUnaligned(ref this.buffer[this.end], ref block[offset], unchecked((uint)length)); |
|||
this.end += length; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Aligns internal buffer on a byte boundary.
|
|||
/// </summary>
|
|||
[MethodImpl(InliningOptions.ShortMethod)] |
|||
public void AlignToByte() |
|||
{ |
|||
if (this.BitCount > 0) |
|||
{ |
|||
byte* pinned = this.pinnedBuffer; |
|||
pinned[this.end++] = unchecked((byte)this.bits); |
|||
if (this.BitCount > 8) |
|||
{ |
|||
pinned[this.end++] = unchecked((byte)(this.bits >> 8)); |
|||
} |
|||
} |
|||
|
|||
this.bits = 0; |
|||
this.BitCount = 0; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write bits to internal buffer
|
|||
/// </summary>
|
|||
/// <param name="b">source of bits</param>
|
|||
/// <param name="count">number of bits to write</param>
|
|||
[MethodImpl(InliningOptions.ShortMethod)] |
|||
public void WriteBits(int b, int count) |
|||
{ |
|||
this.bits |= (uint)(b << this.BitCount); |
|||
this.BitCount += count; |
|||
if (this.BitCount >= 16) |
|||
{ |
|||
byte* pinned = this.pinnedBuffer; |
|||
pinned[this.end++] = unchecked((byte)this.bits); |
|||
pinned[this.end++] = unchecked((byte)(this.bits >> 8)); |
|||
this.bits >>= 16; |
|||
this.BitCount -= 16; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write a short value to internal buffer most significant byte first
|
|||
/// </summary>
|
|||
/// <param name="value">The value to write</param>
|
|||
[MethodImpl(InliningOptions.ShortMethod)] |
|||
public void WriteShortMSB(int value) |
|||
{ |
|||
byte* pinned = this.pinnedBuffer; |
|||
pinned[this.end++] = unchecked((byte)(value >> 8)); |
|||
pinned[this.end++] = unchecked((byte)value); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Flushes the pending buffer into the given output array.
|
|||
/// If the output array is to small, only a partial flush is done.
|
|||
/// </summary>
|
|||
/// <param name="output">The output array.</param>
|
|||
/// <param name="offset">The offset into output array.</param>
|
|||
/// <param name="length">The maximum number of bytes to store.</param>
|
|||
/// <returns>The number of bytes flushed.</returns>
|
|||
public int Flush(byte[] output, int offset, int length) |
|||
{ |
|||
if (this.BitCount >= 8) |
|||
{ |
|||
this.pinnedBuffer[this.end++] = unchecked((byte)this.bits); |
|||
this.bits >>= 8; |
|||
this.BitCount -= 8; |
|||
} |
|||
|
|||
if (length > this.end - this.start) |
|||
{ |
|||
length = this.end - this.start; |
|||
|
|||
Unsafe.CopyBlockUnaligned(ref output[offset], ref this.buffer[this.start], unchecked((uint)length)); |
|||
this.start = 0; |
|||
this.end = 0; |
|||
} |
|||
else |
|||
{ |
|||
Unsafe.CopyBlockUnaligned(ref output[offset], ref this.buffer[this.start], unchecked((uint)length)); |
|||
this.start += length; |
|||
} |
|||
|
|||
return length; |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public void Dispose() |
|||
{ |
|||
this.Dispose(true); |
|||
GC.SuppressFinalize(this); |
|||
} |
|||
|
|||
private void Dispose(bool disposing) |
|||
{ |
|||
if (!this.isDisposed) |
|||
{ |
|||
if (disposing) |
|||
{ |
|||
this.handle.Dispose(); |
|||
this.managedBuffer.Dispose(); |
|||
} |
|||
|
|||
this.isDisposed = true; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,276 +0,0 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
// <auto-generated/>
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Png.Zlib |
|||
{ |
|||
/// <summary>
|
|||
/// This class is general purpose class for writing data to a buffer.
|
|||
///
|
|||
/// It allows you to write bits as well as bytes
|
|||
/// Based on DeflaterPending.java
|
|||
///
|
|||
/// author of the original java version : Jochen Hoenicke
|
|||
/// </summary>
|
|||
public class PendingBuffer |
|||
{ |
|||
#region Instance Fields
|
|||
|
|||
/// <summary>
|
|||
/// Internal work buffer
|
|||
/// </summary>
|
|||
private readonly byte[] buffer; |
|||
|
|||
private int start; |
|||
private int end; |
|||
|
|||
private uint bits; |
|||
private int bitCount; |
|||
|
|||
#endregion Instance Fields
|
|||
|
|||
#region Constructors
|
|||
|
|||
/// <summary>
|
|||
/// construct instance using default buffer size of 4096
|
|||
/// </summary>
|
|||
public PendingBuffer() : this(4096) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// construct instance using specified buffer size
|
|||
/// </summary>
|
|||
/// <param name="bufferSize">
|
|||
/// size to use for internal buffer
|
|||
/// </param>
|
|||
public PendingBuffer(int bufferSize) |
|||
{ |
|||
buffer = new byte[bufferSize]; |
|||
} |
|||
|
|||
#endregion Constructors
|
|||
|
|||
/// <summary>
|
|||
/// Clear internal state/buffers
|
|||
/// </summary>
|
|||
public void Reset() |
|||
{ |
|||
start = end = bitCount = 0; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write a byte to buffer
|
|||
/// </summary>
|
|||
/// <param name="value">
|
|||
/// The value to write
|
|||
/// </param>
|
|||
public void WriteByte(int value) |
|||
{ |
|||
#if DebugDeflation
|
|||
if (DeflaterConstants.DEBUGGING && (start != 0) ) |
|||
{ |
|||
throw new ImageFormatException("Debug check: start != 0"); |
|||
} |
|||
#endif
|
|||
buffer[end++] = unchecked((byte)value); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write a short value to buffer LSB first
|
|||
/// </summary>
|
|||
/// <param name="value">
|
|||
/// The value to write.
|
|||
/// </param>
|
|||
public void WriteShort(int value) |
|||
{ |
|||
#if DebugDeflation
|
|||
if (DeflaterConstants.DEBUGGING && (start != 0) ) |
|||
{ |
|||
throw new ImageFormatException("Debug check: start != 0"); |
|||
} |
|||
#endif
|
|||
buffer[end++] = unchecked((byte)value); |
|||
buffer[end++] = unchecked((byte)(value >> 8)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// write an integer LSB first
|
|||
/// </summary>
|
|||
/// <param name="value">The value to write.</param>
|
|||
public void WriteInt(int value) |
|||
{ |
|||
#if DebugDeflation
|
|||
if (DeflaterConstants.DEBUGGING && (start != 0) ) |
|||
{ |
|||
throw new ImageFormatException("Debug check: start != 0"); |
|||
} |
|||
#endif
|
|||
buffer[end++] = unchecked((byte)value); |
|||
buffer[end++] = unchecked((byte)(value >> 8)); |
|||
buffer[end++] = unchecked((byte)(value >> 16)); |
|||
buffer[end++] = unchecked((byte)(value >> 24)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write a block of data to buffer
|
|||
/// </summary>
|
|||
/// <param name="block">data to write</param>
|
|||
/// <param name="offset">offset of first byte to write</param>
|
|||
/// <param name="length">number of bytes to write</param>
|
|||
public void WriteBlock(byte[] block, int offset, int length) |
|||
{ |
|||
#if DebugDeflation
|
|||
if (DeflaterConstants.DEBUGGING && (start != 0) ) |
|||
{ |
|||
throw new ImageFormatException("Debug check: start != 0"); |
|||
} |
|||
#endif
|
|||
System.Array.Copy(block, offset, buffer, end, length); |
|||
end += length; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// The number of bits written to the buffer
|
|||
/// </summary>
|
|||
public int BitCount |
|||
{ |
|||
get |
|||
{ |
|||
return bitCount; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Align internal buffer on a byte boundary
|
|||
/// </summary>
|
|||
public void AlignToByte() |
|||
{ |
|||
#if DebugDeflation
|
|||
if (DeflaterConstants.DEBUGGING && (start != 0) ) |
|||
{ |
|||
throw new ImageFormatException("Debug check: start != 0"); |
|||
} |
|||
#endif
|
|||
if (bitCount > 0) |
|||
{ |
|||
buffer[end++] = unchecked((byte)bits); |
|||
if (bitCount > 8) |
|||
{ |
|||
buffer[end++] = unchecked((byte)(bits >> 8)); |
|||
} |
|||
} |
|||
bits = 0; |
|||
bitCount = 0; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write bits to internal buffer
|
|||
/// </summary>
|
|||
/// <param name="b">source of bits</param>
|
|||
/// <param name="count">number of bits to write</param>
|
|||
public void WriteBits(int b, int count) |
|||
{ |
|||
#if DebugDeflation
|
|||
if (DeflaterConstants.DEBUGGING && (start != 0) ) |
|||
{ |
|||
throw new ImageFormatException("Debug check: start != 0"); |
|||
} |
|||
|
|||
// if (DeflaterConstants.DEBUGGING) {
|
|||
// //Console.WriteLine("writeBits("+b+","+count+")");
|
|||
// }
|
|||
#endif
|
|||
bits |= (uint)(b << bitCount); |
|||
bitCount += count; |
|||
if (bitCount >= 16) |
|||
{ |
|||
buffer[end++] = unchecked((byte)bits); |
|||
buffer[end++] = unchecked((byte)(bits >> 8)); |
|||
bits >>= 16; |
|||
bitCount -= 16; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write a short value to internal buffer most significant byte first
|
|||
/// </summary>
|
|||
/// <param name="s">value to write</param>
|
|||
public void WriteShortMSB(int s) |
|||
{ |
|||
#if DebugDeflation
|
|||
if (DeflaterConstants.DEBUGGING && (start != 0) ) |
|||
{ |
|||
throw new ImageFormatException("Debug check: start != 0"); |
|||
} |
|||
#endif
|
|||
buffer[end++] = unchecked((byte)(s >> 8)); |
|||
buffer[end++] = unchecked((byte)s); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Indicates if buffer has been flushed
|
|||
/// </summary>
|
|||
public bool IsFlushed |
|||
{ |
|||
get |
|||
{ |
|||
return end == 0; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Flushes the pending buffer into the given output array. If the
|
|||
/// output array is to small, only a partial flush is done.
|
|||
/// </summary>
|
|||
/// <param name="output">The output array.</param>
|
|||
/// <param name="offset">The offset into output array.</param>
|
|||
/// <param name="length">The maximum number of bytes to store.</param>
|
|||
/// <returns>The number of bytes flushed.</returns>
|
|||
public int Flush(byte[] output, int offset, int length) |
|||
{ |
|||
if (bitCount >= 8) |
|||
{ |
|||
buffer[end++] = unchecked((byte)bits); |
|||
bits >>= 8; |
|||
bitCount -= 8; |
|||
} |
|||
|
|||
if (length > end - start) |
|||
{ |
|||
length = end - start; |
|||
System.Array.Copy(buffer, start, output, offset, length); |
|||
start = 0; |
|||
end = 0; |
|||
} |
|||
else |
|||
{ |
|||
System.Array.Copy(buffer, start, output, offset, length); |
|||
start += length; |
|||
} |
|||
return length; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Convert internal buffer to byte array.
|
|||
/// Buffer is empty on completion
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// The internal buffer contents converted to a byte array.
|
|||
/// </returns>
|
|||
public byte[] ToByteArray() |
|||
{ |
|||
AlignToByte(); |
|||
|
|||
byte[] result = new byte[end - start]; |
|||
System.Array.Copy(buffer, start, result, 0, result.Length); |
|||
start = 0; |
|||
end = 0; |
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue