Browse Source

Move reverse to byte[] extensions

pull/23/head
James Jackson-South 9 years ago
parent
commit
ae91fc8beb
  1. 53
      src/ImageSharp/Common/Extensions/ByteExtensions.cs
  2. 41
      src/ImageSharp/Formats/Png/PngDecoderCore.cs

53
src/ImageSharp/Common/Extensions/ByteExtensions.cs

@ -16,32 +16,31 @@ namespace ImageSharp
/// Converts a byte array to a new array where each value in the original array is represented
/// by a the specified number of bits.
/// </summary>
/// <param name="bytes">The bytes to convert from. Cannot be null.</param>
/// <param name="source">The bytes to convert from. Cannot be null.</param>
/// <param name="bits">The number of bits per value.</param>
/// <returns>The resulting <see cref="T:byte[]"/> array. Is never null.</returns>
/// <exception cref="System.ArgumentNullException"><paramref name="bytes"/> is null.</exception>
/// <exception cref="System.ArgumentNullException"><paramref name="source"/> is null.</exception>
/// <exception cref="System.ArgumentException"><paramref name="bits"/> is less than or equals than zero.</exception>
public static byte[] ToArrayByBitsLength(this byte[] bytes, int bits)
public static byte[] ToArrayByBitsLength(this byte[] source, int bits)
{
Guard.NotNull(bytes, "bytes");
Guard.NotNull(source, nameof(source));
Guard.MustBeGreaterThan(bits, 0, "bits");
byte[] result;
if (bits < 8)
{
result = new byte[bytes.Length * 8 / bits];
// BUGFIX I dont think it should be there, but I am not sure if it breaks something else
// int factor = (int)Math.Pow(2, bits) - 1;
result = new byte[source.Length * 8 / bits];
int mask = 0xFF >> (8 - bits);
int resultOffset = 0;
foreach (byte b in bytes)
// ReSharper disable once ForCanBeConvertedToForeach
for (int i = 0; i < source.Length; i++)
{
byte b = source[i];
for (int shift = 0; shift < 8; shift += bits)
{
int colorIndex = (b >> (8 - bits - shift)) & mask; // * (255 / factor);
int colorIndex = (b >> (8 - bits - shift)) & mask;
result[resultOffset] = (byte)colorIndex;
@ -51,10 +50,42 @@ namespace ImageSharp
}
else
{
result = bytes;
result = source;
}
return result;
}
/// <summary>
/// Optimized <see cref="T:byte[]"/> reversal algorithm.
/// </summary>
/// <param name="source">The byte array.</param>
public static void ReverseBytes(this byte[] source)
{
ReverseBytes(source, 0, source.Length);
}
/// <summary>
/// Optimized <see cref="T:byte[]"/> reversal algorithm.
/// </summary>
/// <param name="source">The byte array.</param>
/// <param name="index">The index.</param>
/// <param name="length">The length.</param>
/// <exception cref="System.ArgumentNullException"><paramref name="source"/> is null.</exception>
public static void ReverseBytes(this byte[] source, int index, int length)
{
Guard.NotNull(source, nameof(source));
int i = index;
int j = index + length - 1;
while (i < j)
{
byte temp = source[i];
source[i] = source[j];
source[j] = temp;
i++;
j--;
}
}
}
}

41
src/ImageSharp/Formats/Png/PngDecoderCore.cs

@ -184,8 +184,8 @@ namespace ImageSharp.Formats
where TColor : struct, IPackedPixel<TPacked>
where TPacked : struct
{
this.ReverseBytes(data, 0, 4);
this.ReverseBytes(data, 4, 4);
data.ReverseBytes(0, 4);
data.ReverseBytes(4, 4);
// 39.3700787 = inches in a meter.
image.HorizontalResolution = BitConverter.ToInt32(data, 0) / 39.3700787d;
@ -482,8 +482,8 @@ namespace ImageSharp.Formats
{
this.header = new PngHeader();
this.ReverseBytes(data, 0, 4);
this.ReverseBytes(data, 4, 4);
data.ReverseBytes(0, 4);
data.ReverseBytes(4, 4);
this.header.Width = BitConverter.ToInt32(data, 0);
this.header.Height = BitConverter.ToInt32(data, 4);
@ -569,7 +569,7 @@ namespace ImageSharp.Formats
throw new ImageFormatException("Image stream is not valid!");
}
this.ReverseBytes(this.crcBuffer);
this.crcBuffer.ReverseBytes();
chunk.Crc = BitConverter.ToUInt32(this.crcBuffer, 0);
@ -634,40 +634,11 @@ namespace ImageSharp.Formats
throw new ImageFormatException("Image stream is not valid!");
}
this.ReverseBytes(this.chunkLengthBuffer);
this.chunkLengthBuffer.ReverseBytes();
chunk.Length = BitConverter.ToInt32(this.chunkLengthBuffer, 0);
return numBytes;
}
/// <summary>
/// Optimized <see cref="T:byte[]"/> reversal algorithm.
/// </summary>
/// <param name="source">The byte array.</param>
private void ReverseBytes(byte[] source)
{
this.ReverseBytes(source, 0, source.Length);
}
/// <summary>
/// Optimized <see cref="T:byte[]"/> reversal algorithm.
/// </summary>
/// <param name="source">The byte array.</param>
/// <param name="index">The index.</param>
/// <param name="length">The length.</param>
private void ReverseBytes(byte[] source, int index, int length)
{
int i = index;
int j = index + length - 1;
while (i < j)
{
byte temp = source[i];
source[i] = source[j];
source[j] = temp;
i++;
j--;
}
}
}
}

Loading…
Cancel
Save