mirror of https://github.com/SixLabors/ImageSharp
Browse Source
Former-commit-id: 0dad22cb5bc18a9a053c77c189950d711eaf8f02 Former-commit-id: afac0a345b441a0a9ee35818e4b479b89610bce8 Former-commit-id: 5390bc5686e8ed22e86399181e0b7bd857b12aafaf/merge-core
5 changed files with 1458 additions and 0 deletions
@ -0,0 +1,48 @@ |
|||
// <copyright file="BigEndianBitConverter.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.IO |
|||
{ |
|||
/// <summary>
|
|||
/// Implementation of EndianBitConverter which converts to/from big-endian
|
|||
/// byte arrays.
|
|||
/// <remarks>
|
|||
/// Adapted from Miscellaneous Utility Library <see href="http://jonskeet.uk/csharp/miscutil/" />
|
|||
/// This product includes software developed by Jon Skeet and Marc Gravell. Contact <see href="mailto:skeet@pobox.com" />, or see
|
|||
/// <see href="http://www.pobox.com/~skeet/" />.
|
|||
/// </remarks>
|
|||
/// </summary>
|
|||
internal sealed class BigEndianBitConverter : EndianBitConverter |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public override Endianness Endianness => Endianness.BigEndian; |
|||
|
|||
/// <inheritdoc/>
|
|||
public override bool IsLittleEndian() => false; |
|||
|
|||
/// <inheritdoc/>
|
|||
protected internal override void CopyBytesImpl(long value, int bytes, byte[] buffer, int index) |
|||
{ |
|||
int endOffset = index + bytes - 1; |
|||
for (int i = 0; i < bytes; i++) |
|||
{ |
|||
buffer[endOffset - i] = unchecked((byte)(value & 0xff)); |
|||
value = value >> 8; |
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
protected internal override long FromBytes(byte[] buffer, int startIndex, int bytesToConvert) |
|||
{ |
|||
long ret = 0; |
|||
for (int i = 0; i < bytesToConvert; i++) |
|||
{ |
|||
ret = unchecked((ret << 8) | buffer[startIndex + i]); |
|||
} |
|||
|
|||
return ret; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,616 @@ |
|||
// <copyright file="EndianBinaryReader.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.IO |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
using System.Text; |
|||
|
|||
/// <summary>
|
|||
/// Equivalent of <see cref="BinaryReader"/>, but with either endianness, depending on
|
|||
/// the EndianBitConverter it is constructed with. No data is buffered in the
|
|||
/// reader; the client may seek within the stream at will.
|
|||
/// </summary>
|
|||
internal class EndianBinaryReader : IDisposable |
|||
{ |
|||
/// <summary>
|
|||
/// Decoder to use for string conversions.
|
|||
/// </summary>
|
|||
private readonly Decoder decoder; |
|||
|
|||
/// <summary>
|
|||
/// Buffer used for temporary storage before conversion into primitives
|
|||
/// </summary>
|
|||
private readonly byte[] buffer = new byte[16]; |
|||
|
|||
/// <summary>
|
|||
/// Buffer used for temporary storage when reading a single character
|
|||
/// </summary>
|
|||
private readonly char[] charBuffer = new char[1]; |
|||
|
|||
/// <summary>
|
|||
/// Minimum number of bytes used to encode a character
|
|||
/// </summary>
|
|||
private readonly int minBytesPerChar; |
|||
|
|||
/// <summary>
|
|||
/// Whether or not this reader has been disposed yet.
|
|||
/// </summary>
|
|||
private bool disposed; |
|||
|
|||
/// <summary>
|
|||
/// Equivalent of System.IO.BinaryWriter, but with either endianness, depending on
|
|||
/// the EndianBitConverter it is constructed with.
|
|||
/// </summary>
|
|||
/// <param name="bitConverter">Converter to use when reading data</param>
|
|||
/// <param name="stream">Stream to read data from</param>
|
|||
public EndianBinaryReader(EndianBitConverter bitConverter, Stream stream) |
|||
: this(bitConverter, stream, Encoding.UTF8) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Constructs a new binary reader with the given bit converter, reading
|
|||
/// to the given stream, using the given encoding.
|
|||
/// </summary>
|
|||
/// <param name="bitConverter">Converter to use when reading data</param>
|
|||
/// <param name="stream">Stream to read data from</param>
|
|||
/// <param name="encoding">Encoding to use when reading character data</param>
|
|||
public EndianBinaryReader(EndianBitConverter bitConverter, Stream stream, Encoding encoding) |
|||
{ |
|||
// TODO: Use Guard
|
|||
if (bitConverter == null) |
|||
{ |
|||
throw new ArgumentNullException("bitConverter"); |
|||
} |
|||
|
|||
if (stream == null) |
|||
{ |
|||
throw new ArgumentNullException("stream"); |
|||
} |
|||
|
|||
if (encoding == null) |
|||
{ |
|||
throw new ArgumentNullException("encoding"); |
|||
} |
|||
|
|||
if (!stream.CanRead) |
|||
{ |
|||
throw new ArgumentException("Stream isn't writable", "stream"); |
|||
} |
|||
|
|||
this.BaseStream = stream; |
|||
this.BitConverter = bitConverter; |
|||
this.Encoding = encoding; |
|||
this.decoder = encoding.GetDecoder(); |
|||
this.minBytesPerChar = 1; |
|||
|
|||
if (encoding is UnicodeEncoding) |
|||
{ |
|||
this.minBytesPerChar = 2; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the bit converter used to read values from the stream.
|
|||
/// </summary>
|
|||
public EndianBitConverter BitConverter { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the encoding used to read strings
|
|||
/// </summary>
|
|||
public Encoding Encoding { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the underlying stream of the EndianBinaryReader.
|
|||
/// </summary>
|
|||
public Stream BaseStream { get; } |
|||
|
|||
/// <summary>
|
|||
/// Closes the reader, including the underlying stream.
|
|||
/// </summary>
|
|||
public void Close() |
|||
{ |
|||
this.Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Seeks within the stream.
|
|||
/// </summary>
|
|||
/// <param name="offset">Offset to seek to.</param>
|
|||
/// <param name="origin">Origin of seek operation.</param>
|
|||
public void Seek(int offset, SeekOrigin origin) |
|||
{ |
|||
this.CheckDisposed(); |
|||
this.BaseStream.Seek(offset, origin); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a single byte from the stream.
|
|||
/// </summary>
|
|||
/// <returns>The byte read</returns>
|
|||
public byte ReadByte() |
|||
{ |
|||
this.ReadInternal(this.buffer, 1); |
|||
return this.buffer[0]; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a single signed byte from the stream.
|
|||
/// </summary>
|
|||
/// <returns>The byte read</returns>
|
|||
public sbyte ReadSByte() |
|||
{ |
|||
this.ReadInternal(this.buffer, 1); |
|||
return unchecked((sbyte)this.buffer[0]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a boolean from the stream. 1 byte is read.
|
|||
/// </summary>
|
|||
/// <returns>The boolean read</returns>
|
|||
public bool ReadBoolean() |
|||
{ |
|||
this.ReadInternal(this.buffer, 1); |
|||
return this.BitConverter.ToBoolean(this.buffer, 0); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a 16-bit signed integer from the stream, using the bit converter
|
|||
/// for this reader. 2 bytes are read.
|
|||
/// </summary>
|
|||
/// <returns>The 16-bit integer read</returns>
|
|||
public short ReadInt16() |
|||
{ |
|||
this.ReadInternal(this.buffer, 2); |
|||
return this.BitConverter.ToInt16(this.buffer, 0); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a 32-bit signed integer from the stream, using the bit converter
|
|||
/// for this reader. 4 bytes are read.
|
|||
/// </summary>
|
|||
/// <returns>The 32-bit integer read</returns>
|
|||
public int ReadInt32() |
|||
{ |
|||
this.ReadInternal(this.buffer, 4); |
|||
return this.BitConverter.ToInt32(this.buffer, 0); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a 64-bit signed integer from the stream, using the bit converter
|
|||
/// for this reader. 8 bytes are read.
|
|||
/// </summary>
|
|||
/// <returns>The 64-bit integer read</returns>
|
|||
public long ReadInt64() |
|||
{ |
|||
this.ReadInternal(this.buffer, 8); |
|||
return this.BitConverter.ToInt64(this.buffer, 0); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a 16-bit unsigned integer from the stream, using the bit converter
|
|||
/// for this reader. 2 bytes are read.
|
|||
/// </summary>
|
|||
/// <returns>The 16-bit unsigned integer read</returns>
|
|||
public ushort ReadUInt16() |
|||
{ |
|||
this.ReadInternal(this.buffer, 2); |
|||
return this.BitConverter.ToUInt16(this.buffer, 0); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a 32-bit unsigned integer from the stream, using the bit converter
|
|||
/// for this reader. 4 bytes are read.
|
|||
/// </summary>
|
|||
/// <returns>The 32-bit unsigned integer read</returns>
|
|||
public uint ReadUInt32() |
|||
{ |
|||
this.ReadInternal(this.buffer, 4); |
|||
return this.BitConverter.ToUInt32(this.buffer, 0); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a 64-bit unsigned integer from the stream, using the bit converter
|
|||
/// for this reader. 8 bytes are read.
|
|||
/// </summary>
|
|||
/// <returns>The 64-bit unsigned integer read</returns>
|
|||
public ulong ReadUInt64() |
|||
{ |
|||
this.ReadInternal(this.buffer, 8); |
|||
return this.BitConverter.ToUInt64(this.buffer, 0); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a single-precision floating-point value from the stream, using the bit converter
|
|||
/// for this reader. 4 bytes are read.
|
|||
/// </summary>
|
|||
/// <returns>The floating point value read</returns>
|
|||
public float ReadSingle() |
|||
{ |
|||
this.ReadInternal(this.buffer, 4); |
|||
return this.BitConverter.ToSingle(this.buffer, 0); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a double-precision floating-point value from the stream, using the bit converter
|
|||
/// for this reader. 8 bytes are read.
|
|||
/// </summary>
|
|||
/// <returns>The floating point value read</returns>
|
|||
public double ReadDouble() |
|||
{ |
|||
this.ReadInternal(this.buffer, 8); |
|||
return this.BitConverter.ToDouble(this.buffer, 0); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a decimal value from the stream, using the bit converter
|
|||
/// for this reader. 16 bytes are read.
|
|||
/// </summary>
|
|||
/// <returns>The decimal value read</returns>
|
|||
public decimal ReadDecimal() |
|||
{ |
|||
this.ReadInternal(this.buffer, 16); |
|||
return this.BitConverter.ToDecimal(this.buffer, 0); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a single character from the stream, using the character encoding for
|
|||
/// this reader. If no characters have been fully read by the time the stream ends,
|
|||
/// -1 is returned.
|
|||
/// </summary>
|
|||
/// <returns>The character read, or -1 for end of stream.</returns>
|
|||
public int Read() |
|||
{ |
|||
int charsRead = this.Read(this.charBuffer, 0, 1); |
|||
if (charsRead == 0) |
|||
{ |
|||
return -1; |
|||
} |
|||
else |
|||
{ |
|||
return this.charBuffer[0]; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads the specified number of characters into the given buffer, starting at
|
|||
/// the given index.
|
|||
/// </summary>
|
|||
/// <param name="data">The buffer to copy data into</param>
|
|||
/// <param name="index">The first index to copy data into</param>
|
|||
/// <param name="count">The number of characters to read</param>
|
|||
/// <returns>The number of characters actually read. This will only be less than
|
|||
/// the requested number of characters if the end of the stream is reached.
|
|||
/// </returns>
|
|||
public int Read(char[] data, int index, int count) |
|||
{ |
|||
this.CheckDisposed(); |
|||
|
|||
// TODO: Use Guard
|
|||
if (this.buffer == null) |
|||
{ |
|||
throw new ArgumentNullException("buffer"); |
|||
} |
|||
|
|||
if (index < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("index"); |
|||
} |
|||
|
|||
if (count < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("index"); |
|||
} |
|||
|
|||
if (count + index > data.Length) |
|||
{ |
|||
throw new ArgumentException("Not enough space in buffer for specified number of characters starting at specified index"); |
|||
} |
|||
|
|||
int read = 0; |
|||
bool firstTime = true; |
|||
|
|||
// Use the normal buffer if we're only reading a small amount, otherwise
|
|||
// use at most 4K at a time.
|
|||
byte[] byteBuffer = this.buffer; |
|||
|
|||
if (byteBuffer.Length < count * this.minBytesPerChar) |
|||
{ |
|||
byteBuffer = new byte[4096]; |
|||
} |
|||
|
|||
while (read < count) |
|||
{ |
|||
int amountToRead; |
|||
|
|||
// First time through we know we haven't previously read any data
|
|||
if (firstTime) |
|||
{ |
|||
amountToRead = count * this.minBytesPerChar; |
|||
firstTime = false; |
|||
} |
|||
|
|||
// After that we can only assume we need to fully read 'chars left -1' characters
|
|||
// and a single byte of the character we may be in the middle of
|
|||
else |
|||
{ |
|||
amountToRead = ((count - read - 1) * this.minBytesPerChar) + 1; |
|||
} |
|||
|
|||
if (amountToRead > byteBuffer.Length) |
|||
{ |
|||
amountToRead = byteBuffer.Length; |
|||
} |
|||
|
|||
int bytesRead = this.TryReadInternal(byteBuffer, amountToRead); |
|||
if (bytesRead == 0) |
|||
{ |
|||
return read; |
|||
} |
|||
|
|||
int decoded = this.decoder.GetChars(byteBuffer, 0, bytesRead, data, index); |
|||
read += decoded; |
|||
index += decoded; |
|||
} |
|||
|
|||
return read; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads the specified number of bytes into the given buffer, starting at
|
|||
/// the given index.
|
|||
/// </summary>
|
|||
/// <param name="buffer">The buffer to copy data into</param>
|
|||
/// <param name="index">The first index to copy data into</param>
|
|||
/// <param name="count">The number of bytes to read</param>
|
|||
/// <returns>The number of bytes actually read. This will only be less than
|
|||
/// the requested number of bytes if the end of the stream is reached.
|
|||
/// </returns>
|
|||
public int Read(byte[] buffer, int index, int count) |
|||
{ |
|||
this.CheckDisposed(); |
|||
if (buffer == null) |
|||
{ |
|||
throw new ArgumentNullException("buffer"); |
|||
} |
|||
|
|||
if (index < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("index"); |
|||
} |
|||
|
|||
if (count < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("index"); |
|||
} |
|||
|
|||
if (count + index > buffer.Length) |
|||
{ |
|||
throw new ArgumentException("Not enough space in buffer for specified number of bytes starting at specified index"); |
|||
} |
|||
|
|||
int read = 0; |
|||
while (count > 0) |
|||
{ |
|||
int block = this.BaseStream.Read(buffer, index, count); |
|||
if (block == 0) |
|||
{ |
|||
return read; |
|||
} |
|||
|
|||
index += block; |
|||
read += block; |
|||
count -= block; |
|||
} |
|||
|
|||
return read; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads the specified number of bytes, returning them in a new byte array.
|
|||
/// If not enough bytes are available before the end of the stream, this
|
|||
/// method will return what is available.
|
|||
/// </summary>
|
|||
/// <param name="count">The number of bytes to read</param>
|
|||
/// <returns>The bytes read</returns>
|
|||
public byte[] ReadBytes(int count) |
|||
{ |
|||
this.CheckDisposed(); |
|||
if (count < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("count"); |
|||
} |
|||
|
|||
byte[] ret = new byte[count]; |
|||
int index = 0; |
|||
while (index < count) |
|||
{ |
|||
int read = this.BaseStream.Read(ret, index, count - index); |
|||
|
|||
// Stream has finished half way through. That's fine, return what we've got.
|
|||
if (read == 0) |
|||
{ |
|||
byte[] copy = new byte[index]; |
|||
Buffer.BlockCopy(ret, 0, copy, 0, index); |
|||
return copy; |
|||
} |
|||
|
|||
index += read; |
|||
} |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads the specified number of bytes, returning them in a new byte array.
|
|||
/// If not enough bytes are available before the end of the stream, this
|
|||
/// method will throw an IOException.
|
|||
/// </summary>
|
|||
/// <param name="count">The number of bytes to read</param>
|
|||
/// <returns>The bytes read</returns>
|
|||
public byte[] ReadBytesOrThrow(int count) |
|||
{ |
|||
byte[] ret = new byte[count]; |
|||
this.ReadInternal(ret, count); |
|||
return ret; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a 7-bit encoded integer from the stream. This is stored with the least significant
|
|||
/// information first, with 7 bits of information per byte of value, and the top
|
|||
/// bit as a continuation flag. This method is not affected by the endianness
|
|||
/// of the bit converter.
|
|||
/// </summary>
|
|||
/// <returns>The 7-bit encoded integer read from the stream.</returns>
|
|||
public int Read7BitEncodedInt() |
|||
{ |
|||
this.CheckDisposed(); |
|||
|
|||
int ret = 0; |
|||
for (int shift = 0; shift < 35; shift += 7) |
|||
{ |
|||
int b = this.BaseStream.ReadByte(); |
|||
if (b == -1) |
|||
{ |
|||
throw new EndOfStreamException(); |
|||
} |
|||
|
|||
ret = ret | ((b & 0x7f) << shift); |
|||
if ((b & 0x80) == 0) |
|||
{ |
|||
return ret; |
|||
} |
|||
} |
|||
|
|||
// Still haven't seen a byte with the high bit unset? Dodgy data.
|
|||
throw new IOException("Invalid 7-bit encoded integer in stream."); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a 7-bit encoded integer from the stream. This is stored with the most significant
|
|||
/// information first, with 7 bits of information per byte of value, and the top
|
|||
/// bit as a continuation flag. This method is not affected by the endianness
|
|||
/// of the bit converter.
|
|||
/// </summary>
|
|||
/// <returns>The 7-bit encoded integer read from the stream.</returns>
|
|||
public int ReadBigEndian7BitEncodedInt() |
|||
{ |
|||
this.CheckDisposed(); |
|||
|
|||
int ret = 0; |
|||
for (int i = 0; i < 5; i++) |
|||
{ |
|||
int b = this.BaseStream.ReadByte(); |
|||
if (b == -1) |
|||
{ |
|||
throw new EndOfStreamException(); |
|||
} |
|||
|
|||
ret = (ret << 7) | (b & 0x7f); |
|||
if ((b & 0x80) == 0) |
|||
{ |
|||
return ret; |
|||
} |
|||
} |
|||
|
|||
// Still haven't seen a byte with the high bit unset? Dodgy data.
|
|||
throw new IOException("Invalid 7-bit encoded integer in stream."); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a length-prefixed string from the stream, using the encoding for this reader.
|
|||
/// A 7-bit encoded integer is first read, which specifies the number of bytes
|
|||
/// to read from the stream. These bytes are then converted into a string with
|
|||
/// the encoding for this reader.
|
|||
/// </summary>
|
|||
/// <returns>The string read from the stream.</returns>
|
|||
public string ReadString() |
|||
{ |
|||
int bytesToRead = this.Read7BitEncodedInt(); |
|||
|
|||
byte[] data = new byte[bytesToRead]; |
|||
this.ReadInternal(data, bytesToRead); |
|||
return this.Encoding.GetString(data, 0, data.Length); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Disposes of the underlying stream.
|
|||
/// </summary>
|
|||
public void Dispose() |
|||
{ |
|||
if (!this.disposed) |
|||
{ |
|||
this.disposed = true; |
|||
((IDisposable)this.BaseStream).Dispose(); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Checks whether or not the reader has been disposed, throwing an exception if so.
|
|||
/// </summary>
|
|||
private void CheckDisposed() |
|||
{ |
|||
if (this.disposed) |
|||
{ |
|||
throw new ObjectDisposedException("EndianBinaryReader"); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads the given number of bytes from the stream, throwing an exception
|
|||
/// if they can't all be read.
|
|||
/// </summary>
|
|||
/// <param name="data">Buffer to read into</param>
|
|||
/// <param name="size">Number of bytes to read</param>
|
|||
private void ReadInternal(byte[] data, int size) |
|||
{ |
|||
this.CheckDisposed(); |
|||
int index = 0; |
|||
while (index < size) |
|||
{ |
|||
int read = this.BaseStream.Read(data, index, size - index); |
|||
if (read == 0) |
|||
{ |
|||
throw new EndOfStreamException |
|||
( |
|||
string.Format( |
|||
"End of stream reached with {0} byte{1} left to read.", |
|||
size - index, |
|||
size - index == 1 ? "s" : string.Empty)); |
|||
} |
|||
|
|||
index += read; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads the given number of bytes from the stream if possible, returning
|
|||
/// the number of bytes actually read, which may be less than requested if
|
|||
/// (and only if) the end of the stream is reached.
|
|||
/// </summary>
|
|||
/// <param name="data">Buffer to read into</param>
|
|||
/// <param name="size">Number of bytes to read</param>
|
|||
/// <returns>Number of bytes actually read</returns>
|
|||
private int TryReadInternal(byte[] data, int size) |
|||
{ |
|||
this.CheckDisposed(); |
|||
int index = 0; |
|||
while (index < size) |
|||
{ |
|||
int read = this.BaseStream.Read(data, index, size - index); |
|||
if (read == 0) |
|||
{ |
|||
return index; |
|||
} |
|||
|
|||
index += read; |
|||
} |
|||
|
|||
return index; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,724 @@ |
|||
// <copyright file="EndianBitConverter.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.IO |
|||
{ |
|||
using System; |
|||
using System.Diagnostics.CodeAnalysis; |
|||
using System.Runtime.InteropServices; |
|||
|
|||
/// <summary>
|
|||
/// Equivalent of <see cref="System.BitConverter"/>, but with either endianness.
|
|||
/// <remarks>
|
|||
/// Adapted from Miscellaneous Utility Library <see href="http://jonskeet.uk/csharp/miscutil/"/>
|
|||
/// This product includes software developed by Jon Skeet and Marc Gravell. Contact <see href="mailto:skeet@pobox.com"/>, or see
|
|||
/// <see href="http://www.pobox.com/~skeet/"/>.
|
|||
/// </remarks>
|
|||
/// </summary>
|
|||
[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1201:ElementsMustAppearInTheCorrectOrder", Justification = "Reviewed. Suppression is OK here. Better readability.")] |
|||
[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "Reviewed. Suppression is OK here. Better readability.")] |
|||
[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1204:StaticElementsMustAppearBeforeInstanceElements", Justification = "Reviewed. Suppression is OK here. Better readability.")] |
|||
internal abstract class EndianBitConverter |
|||
{ |
|||
#region Endianness of this converter
|
|||
/// <summary>
|
|||
/// Indicates the byte order ("endianness") in which data is converted using this class.
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// Different computer architectures store data using different byte orders. "Big-endian"
|
|||
/// means the most significant byte is on the left end of a word. "Little-endian" means the
|
|||
/// most significant byte is on the right end of a word.
|
|||
/// </remarks>
|
|||
/// <returns>true if this converter is little-endian, false otherwise.</returns>
|
|||
public abstract bool IsLittleEndian(); |
|||
|
|||
/// <summary>
|
|||
/// Gets the byte order ("endianness") in which data is converted using this class.
|
|||
/// </summary>
|
|||
public abstract Endianness Endianness { get; } |
|||
#endregion
|
|||
|
|||
#region Factory properties
|
|||
/// <summary>
|
|||
/// The little-endian bit converter.
|
|||
/// </summary>
|
|||
private static readonly LittleEndianBitConverter LittleConverter = new LittleEndianBitConverter(); |
|||
|
|||
/// <summary>
|
|||
/// Gets a little-endian bit converter instance. The same instance is
|
|||
/// always returned.
|
|||
/// </summary>
|
|||
public static LittleEndianBitConverter Little => LittleConverter; |
|||
|
|||
/// <summary>
|
|||
/// The big-endian bit converter.
|
|||
/// </summary>
|
|||
private static readonly BigEndianBitConverter BigConverter = new BigEndianBitConverter(); |
|||
|
|||
/// <summary>
|
|||
/// Gets a big-endian bit converter instance. The same instance is
|
|||
/// always returned.
|
|||
/// </summary>
|
|||
public static BigEndianBitConverter Big => BigConverter; |
|||
#endregion
|
|||
|
|||
#region Double/primitive conversions
|
|||
/// <summary>
|
|||
/// Converts the specified double-precision floating point number to a
|
|||
/// 64-bit signed integer. Note: the endianness of this converter does not
|
|||
/// affect the returned value.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert. </param>
|
|||
/// <returns>A 64-bit signed integer whose value is equivalent to value.</returns>
|
|||
public long DoubleToInt64Bits(double value) |
|||
{ |
|||
return BitConverter.DoubleToInt64Bits(value); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the specified 64-bit signed integer to a double-precision
|
|||
/// floating point number. Note: the endianness of this converter does not
|
|||
/// affect the returned value.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert. </param>
|
|||
/// <returns>A double-precision floating point number whose value is equivalent to value.</returns>
|
|||
public double Int64BitsToDouble(long value) |
|||
{ |
|||
return BitConverter.Int64BitsToDouble(value); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the specified single-precision floating point number to a
|
|||
/// 32-bit signed integer. Note: the endianness of this converter does not
|
|||
/// affect the returned value.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert. </param>
|
|||
/// <returns>A 32-bit signed integer whose value is equivalent to value.</returns>
|
|||
public int SingleToInt32Bits(float value) |
|||
{ |
|||
return new Int32SingleUnion(value).AsInt32; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the specified 32-bit signed integer to a single-precision floating point
|
|||
/// number. Note: the endianness of this converter does not
|
|||
/// affect the returned value.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert. </param>
|
|||
/// <returns>A single-precision floating point number whose value is equivalent to value.</returns>
|
|||
public float Int32BitsToSingle(int value) |
|||
{ |
|||
return new Int32SingleUnion(value).AsSingle; |
|||
} |
|||
#endregion
|
|||
|
|||
#region To(PrimitiveType) conversions
|
|||
/// <summary>
|
|||
/// Returns a Boolean value converted from one byte at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>true if the byte at startIndex in value is nonzero; otherwise, false.</returns>
|
|||
public bool ToBoolean(byte[] value, int startIndex) |
|||
{ |
|||
CheckByteArgument(value, startIndex, 1); |
|||
return BitConverter.ToBoolean(value, startIndex); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a Unicode character converted from two bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A character formed by two bytes beginning at startIndex.</returns>
|
|||
public char ToChar(byte[] value, int startIndex) |
|||
{ |
|||
return unchecked((char)this.CheckedFromBytes(value, startIndex, 2)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a double-precision floating point number converted from eight bytes
|
|||
/// at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A double precision floating point number formed by eight bytes beginning at startIndex.</returns>
|
|||
public double ToDouble(byte[] value, int startIndex) |
|||
{ |
|||
return this.Int64BitsToDouble(this.ToInt64(value, startIndex)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a single-precision floating point number converted from four bytes
|
|||
/// at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A single precision floating point number formed by four bytes beginning at startIndex.</returns>
|
|||
public float ToSingle(byte[] value, int startIndex) |
|||
{ |
|||
return this.Int32BitsToSingle(this.ToInt32(value, startIndex)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A 16-bit signed integer formed by two bytes beginning at startIndex.</returns>
|
|||
public short ToInt16(byte[] value, int startIndex) |
|||
{ |
|||
return unchecked((short)this.CheckedFromBytes(value, startIndex, 2)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A 32-bit signed integer formed by four bytes beginning at startIndex.</returns>
|
|||
public int ToInt32(byte[] value, int startIndex) |
|||
{ |
|||
return unchecked((int)this.CheckedFromBytes(value, startIndex, 4)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A 64-bit signed integer formed by eight bytes beginning at startIndex.</returns>
|
|||
public long ToInt64(byte[] value, int startIndex) |
|||
{ |
|||
return this.CheckedFromBytes(value, startIndex, 8); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A 16-bit unsigned integer formed by two bytes beginning at startIndex.</returns>
|
|||
public ushort ToUInt16(byte[] value, int startIndex) |
|||
{ |
|||
return unchecked((ushort)this.CheckedFromBytes(value, startIndex, 2)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A 32-bit unsigned integer formed by four bytes beginning at startIndex.</returns>
|
|||
public uint ToUInt32(byte[] value, int startIndex) |
|||
{ |
|||
return unchecked((uint)this.CheckedFromBytes(value, startIndex, 4)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A 64-bit unsigned integer formed by eight bytes beginning at startIndex.</returns>
|
|||
public ulong ToUInt64(byte[] value, int startIndex) |
|||
{ |
|||
return unchecked((ulong)this.CheckedFromBytes(value, startIndex, 8)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Convert the given number of bytes from the given array, from the given start
|
|||
/// position, into a long, using the bytes as the least significant part of the long.
|
|||
/// By the time this is called, the arguments have been checked for validity.
|
|||
/// </summary>
|
|||
/// <param name="value">The bytes to convert</param>
|
|||
/// <param name="startIndex">The index of the first byte to convert</param>
|
|||
/// <param name="bytesToConvert">The number of bytes to use in the conversion</param>
|
|||
/// <returns>The converted number</returns>
|
|||
protected internal abstract long FromBytes(byte[] value, int startIndex, int bytesToConvert); |
|||
|
|||
/// <summary>
|
|||
/// Checks the given argument for validity.
|
|||
/// </summary>
|
|||
/// <param name="value">The byte array passed in</param>
|
|||
/// <param name="startIndex">The start index passed in</param>
|
|||
/// <param name="bytesRequired">The number of bytes required</param>
|
|||
/// <exception cref="ArgumentNullException">value is a null reference</exception>
|
|||
/// <exception cref="ArgumentOutOfRangeException">
|
|||
/// startIndex is less than zero or greater than the length of value minus bytesRequired.
|
|||
/// </exception>
|
|||
[SuppressMessage("ReSharper", "UnusedParameter.Local", Justification = "Keeps code DRY")] |
|||
private static void CheckByteArgument(byte[] value, int startIndex, int bytesRequired) |
|||
{ |
|||
if (value == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(value)); |
|||
} |
|||
|
|||
if (startIndex < 0 || startIndex > value.Length - bytesRequired) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(startIndex)); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Checks the arguments for validity before calling FromBytes
|
|||
/// (which can therefore assume the arguments are valid).
|
|||
/// </summary>
|
|||
/// <param name="value">The bytes to convert after checking</param>
|
|||
/// <param name="startIndex">The index of the first byte to convert</param>
|
|||
/// <param name="bytesToConvert">The number of bytes to convert</param>
|
|||
/// <returns>The <see cref="long"/></returns>
|
|||
private long CheckedFromBytes(byte[] value, int startIndex, int bytesToConvert) |
|||
{ |
|||
CheckByteArgument(value, startIndex, bytesToConvert); |
|||
return this.FromBytes(value, startIndex, bytesToConvert); |
|||
} |
|||
#endregion
|
|||
|
|||
#region ToString conversions
|
|||
/// <summary>
|
|||
/// Returns a String converted from the elements of a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <remarks>All the elements of value are converted.</remarks>
|
|||
/// <returns>
|
|||
/// A String of hexadecimal pairs separated by hyphens, where each pair
|
|||
/// represents the corresponding element in value; for example, "7F-2C-4A".
|
|||
/// </returns>
|
|||
public static string ToString(byte[] value) |
|||
{ |
|||
return BitConverter.ToString(value); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a String converted from the elements of a byte array starting at a specified array position.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <remarks>The elements from array position startIndex to the end of the array are converted.</remarks>
|
|||
/// <returns>
|
|||
/// A String of hexadecimal pairs separated by hyphens, where each pair
|
|||
/// represents the corresponding element in value; for example, "7F-2C-4A".
|
|||
/// </returns>
|
|||
public static string ToString(byte[] value, int startIndex) |
|||
{ |
|||
return BitConverter.ToString(value, startIndex); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a String converted from a specified number of bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <param name="length">The number of bytes to convert.</param>
|
|||
/// <remarks>The length elements from array position startIndex are converted.</remarks>
|
|||
/// <returns>
|
|||
/// A String of hexadecimal pairs separated by hyphens, where each pair
|
|||
/// represents the corresponding element in value; for example, "7F-2C-4A".
|
|||
/// </returns>
|
|||
public static string ToString(byte[] value, int startIndex, int length) |
|||
{ |
|||
return BitConverter.ToString(value, startIndex, length); |
|||
} |
|||
#endregion
|
|||
|
|||
#region Decimal conversions
|
|||
/// <summary>
|
|||
/// Returns a decimal value converted from sixteen bytes
|
|||
/// at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A decimal formed by sixteen bytes beginning at startIndex.</returns>
|
|||
public decimal ToDecimal(byte[] value, int startIndex) |
|||
{ |
|||
// HACK: This always assumes four parts, each in their own endianness,
|
|||
// starting with the first part at the start of the byte array.
|
|||
// On the other hand, there's no real format specified...
|
|||
int[] parts = new int[4]; |
|||
for (int i = 0; i < 4; i++) |
|||
{ |
|||
parts[i] = this.ToInt32(value, startIndex + (i * 4)); |
|||
} |
|||
|
|||
return new decimal(parts); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified decimal value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 16.</returns>
|
|||
public byte[] GetBytes(decimal value) |
|||
{ |
|||
byte[] bytes = new byte[16]; |
|||
int[] parts = decimal.GetBits(value); |
|||
for (int i = 0; i < 4; i++) |
|||
{ |
|||
this.CopyBytesImpl(parts[i], 4, bytes, i * 4); |
|||
} |
|||
|
|||
return bytes; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified decimal value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">A character to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(decimal value, byte[] buffer, int index) |
|||
{ |
|||
int[] parts = decimal.GetBits(value); |
|||
for (int i = 0; i < 4; i++) |
|||
{ |
|||
this.CopyBytesImpl(parts[i], 4, buffer, (i * 4) + index); |
|||
} |
|||
} |
|||
#endregion
|
|||
|
|||
#region GetBytes conversions
|
|||
/// <summary>
|
|||
/// Returns an array with the given number of bytes formed
|
|||
/// from the least significant bytes of the specified value.
|
|||
/// This is used to implement the other GetBytes methods.
|
|||
/// </summary>
|
|||
/// <param name="value">The value to get bytes for</param>
|
|||
/// <param name="bytes">The number of significant bytes to return</param>
|
|||
/// <returns>
|
|||
/// The <see cref="T:byte[]"/>.
|
|||
/// </returns>
|
|||
private byte[] GetBytes(long value, int bytes) |
|||
{ |
|||
byte[] buffer = new byte[bytes]; |
|||
this.CopyBytes(value, bytes, buffer, 0); |
|||
return buffer; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified Boolean value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">A Boolean value.</param>
|
|||
/// <returns>An array of bytes with length 1.</returns>
|
|||
/// <returns>
|
|||
/// The <see cref="T:byte[]"/>.
|
|||
/// </returns>
|
|||
public byte[] GetBytes(bool value) |
|||
{ |
|||
return BitConverter.GetBytes(value); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified Unicode character value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">A character to convert.</param>
|
|||
/// <returns>An array of bytes with length 2.</returns>
|
|||
/// <returns>
|
|||
/// The <see cref="T:byte[]"/>.
|
|||
/// </returns>
|
|||
public byte[] GetBytes(char value) |
|||
{ |
|||
return this.GetBytes(value, 2); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified double-precision floating point value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 8.</returns>
|
|||
public byte[] GetBytes(double value) |
|||
{ |
|||
return this.GetBytes(this.DoubleToInt64Bits(value), 8); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified 16-bit signed integer value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 2.</returns>
|
|||
public byte[] GetBytes(short value) |
|||
{ |
|||
return this.GetBytes(value, 2); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified 32-bit signed integer value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 4.</returns>
|
|||
public byte[] GetBytes(int value) |
|||
{ |
|||
return this.GetBytes(value, 4); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified 64-bit signed integer value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 8.</returns>
|
|||
public byte[] GetBytes(long value) |
|||
{ |
|||
return this.GetBytes(value, 8); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified single-precision floating point value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 4.</returns>
|
|||
public byte[] GetBytes(float value) |
|||
{ |
|||
return this.GetBytes(this.SingleToInt32Bits(value), 4); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified 16-bit unsigned integer value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 2.</returns>
|
|||
public byte[] GetBytes(ushort value) |
|||
{ |
|||
return this.GetBytes(value, 2); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified 32-bit unsigned integer value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 4.</returns>
|
|||
public byte[] GetBytes(uint value) |
|||
{ |
|||
return this.GetBytes(value, 4); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified 64-bit unsigned integer value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 8.</returns>
|
|||
public byte[] GetBytes(ulong value) |
|||
{ |
|||
return this.GetBytes(unchecked((long)value), 8); |
|||
} |
|||
|
|||
#endregion
|
|||
|
|||
#region CopyBytes conversions
|
|||
/// <summary>
|
|||
/// Copies the given number of bytes from the least-specific
|
|||
/// end of the specified value into the specified byte array, beginning
|
|||
/// at the specified index.
|
|||
/// This is used to implement the other CopyBytes methods.
|
|||
/// </summary>
|
|||
/// <param name="value">The value to copy bytes for</param>
|
|||
/// <param name="bytes">The number of significant bytes to copy</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
private void CopyBytes(long value, int bytes, byte[] buffer, int index) |
|||
{ |
|||
if (buffer == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(buffer), "Byte array must not be null"); |
|||
} |
|||
|
|||
if (buffer.Length < index + bytes) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(buffer), "Buffer not big enough for value"); |
|||
} |
|||
|
|||
this.CopyBytesImpl(value, bytes, buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the given number of bytes from the least-specific
|
|||
/// end of the specified value into the specified byte array, beginning
|
|||
/// at the specified index.
|
|||
/// This must be implemented in concrete derived classes, but the implementation
|
|||
/// may assume that the value will fit into the buffer.
|
|||
/// </summary>
|
|||
/// <param name="value">The value to copy bytes for</param>
|
|||
/// <param name="bytes">The number of significant bytes to copy</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
protected internal abstract void CopyBytesImpl(long value, int bytes, byte[] buffer, int index); |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified Boolean value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">A Boolean value.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(bool value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(value ? 1 : 0, 1, buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified Unicode character value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">A character to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(char value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(value, 2, buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified double-precision floating point value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(double value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(this.DoubleToInt64Bits(value), 8, buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified 16-bit signed integer value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(short value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(value, 2, buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified 32-bit signed integer value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(int value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(value, 4, buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified 64-bit signed integer value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(long value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(value, 8, buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified single-precision floating point value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(float value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(this.SingleToInt32Bits(value), 4, buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified 16-bit unsigned integer value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(ushort value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(value, 2, buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified 32-bit unsigned integer value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(uint value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(value, 4, buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified 64-bit unsigned integer value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(ulong value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(unchecked((long)value), 8, buffer, index); |
|||
} |
|||
|
|||
#endregion
|
|||
|
|||
#region Private struct used for Single/Int32 conversions
|
|||
/// <summary>
|
|||
/// Union used solely for the equivalent of DoubleToInt64Bits and vice versa.
|
|||
/// </summary>
|
|||
[StructLayout(LayoutKind.Explicit)] |
|||
private struct Int32SingleUnion |
|||
{ |
|||
/// <summary>
|
|||
/// Int32 version of the value.
|
|||
/// </summary>
|
|||
[FieldOffset(0)] |
|||
private readonly int i; |
|||
|
|||
/// <summary>
|
|||
/// Single version of the value.
|
|||
/// </summary>
|
|||
[FieldOffset(0)] |
|||
private readonly float f; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Int32SingleUnion"/> struct.
|
|||
/// </summary>
|
|||
/// <param name="i">The integer value of the new instance.</param>
|
|||
internal Int32SingleUnion(int i) |
|||
{ |
|||
this.f = 0; // Just to keep the compiler happy
|
|||
this.i = i; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Int32SingleUnion"/> struct.
|
|||
/// </summary>
|
|||
/// <param name="f">
|
|||
/// The floating point value of the new instance.
|
|||
/// </param>
|
|||
internal Int32SingleUnion(float f) |
|||
{ |
|||
this.i = 0; // Just to keep the compiler happy
|
|||
this.f = f; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the value of the instance as an integer.
|
|||
/// </summary>
|
|||
internal int AsInt32 => this.i; |
|||
|
|||
/// <summary>
|
|||
/// Gets the value of the instance as a floating point number.
|
|||
/// </summary>
|
|||
internal float AsSingle => this.f; |
|||
} |
|||
#endregion
|
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
// <copyright file="Endianness.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.IO |
|||
{ |
|||
/// <summary>
|
|||
/// Endianness of a converter
|
|||
/// </summary>
|
|||
internal enum Endianness |
|||
{ |
|||
/// <summary>
|
|||
/// Little endian - least significant byte first
|
|||
/// </summary>
|
|||
LittleEndian, |
|||
|
|||
/// <summary>
|
|||
/// Big endian - most significant byte first
|
|||
/// </summary>
|
|||
BigEndian |
|||
} |
|||
} |
|||
@ -0,0 +1,47 @@ |
|||
// <copyright file="LittleEndianBitConverter.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.IO |
|||
{ |
|||
/// <summary>
|
|||
/// Implementation of EndianBitConverter which converts to/from little-endian
|
|||
/// byte arrays.
|
|||
/// <remarks>
|
|||
/// Adapted from Miscellaneous Utility Library <see href="http://jonskeet.uk/csharp/miscutil/"/>
|
|||
/// This product includes software developed by Jon Skeet and Marc Gravell. Contact <see href="mailto:skeet@pobox.com"/>, or see
|
|||
/// <see href="http://www.pobox.com/~skeet/"/>.
|
|||
/// </remarks>
|
|||
/// </summary>
|
|||
internal sealed class LittleEndianBitConverter : EndianBitConverter |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public override Endianness Endianness => Endianness.LittleEndian; |
|||
|
|||
/// <inheritdoc/>
|
|||
public override bool IsLittleEndian() => true; |
|||
|
|||
/// <inheritdoc/>
|
|||
protected internal override void CopyBytesImpl(long value, int bytes, byte[] buffer, int index) |
|||
{ |
|||
for (int i = 0; i < bytes; i++) |
|||
{ |
|||
buffer[i + index] = unchecked((byte)(value & 0xff)); |
|||
value = value >> 8; |
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
protected internal override long FromBytes(byte[] buffer, int startIndex, int bytesToConvert) |
|||
{ |
|||
long ret = 0; |
|||
for (int i = 0; i < bytesToConvert; i++) |
|||
{ |
|||
ret = unchecked((ret << 8) | buffer[startIndex + bytesToConvert - 1 - i]); |
|||
} |
|||
|
|||
return ret; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue