Browse Source

Remove ReverseBytes (replacing with faster BinaryPrimitives calls)

af/merge-core
Jason Nelson 8 years ago
parent
commit
14f7ff8fb7
  1. 33
      src/ImageSharp/Common/Extensions/ByteExtensions.cs
  2. 12
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  3. 11
      src/ImageSharp/Formats/Png/PngEncoderCore.cs

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

@ -12,39 +12,6 @@ namespace SixLabors.ImageSharp
/// </summary>
internal static class ByteExtensions
{
/// <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>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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--;
}
}
/// <summary>
/// Returns a reference to the given position of the array unsafe casted to <see cref="ImageSharp.PixelFormats.Rgb24"/>.
/// </summary>

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

@ -1251,14 +1251,13 @@ namespace SixLabors.ImageSharp.Formats.Png
private void ReadChunkCrc(PngChunk chunk)
{
int numBytes = this.currentStream.Read(this.crcBuffer, 0, 4);
if (numBytes >= 1 && numBytes <= 3)
{
throw new ImageFormatException("Image stream is not valid!");
}
this.crcBuffer.ReverseBytes();
chunk.Crc = BitConverter.ToUInt32(this.crcBuffer, 0);
chunk.Crc = BinaryPrimitives.ReadUInt32BigEndian(this.crcBuffer);
this.crc.Reset();
this.crc.Update(this.chunkTypeBuffer);
@ -1323,15 +1322,14 @@ namespace SixLabors.ImageSharp.Formats.Png
private void ReadChunkLength(PngChunk chunk)
{
int numBytes = this.currentStream.Read(this.chunkLengthBuffer, 0, 4);
if (numBytes < 4)
{
chunk.Length = -1;
return;
}
this.chunkLengthBuffer.ReverseBytes();
chunk.Length = BitConverter.ToInt32(this.chunkLengthBuffer, 0);
chunk.Length = BinaryPrimitives.ReadInt32BigEndian(this.chunkLengthBuffer);
}
/// <summary>

11
src/ImageSharp/Formats/Png/PngEncoderCore.cs

@ -5,6 +5,7 @@ using System;
using System.Buffers.Binary;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats.Png.Filters;
using SixLabors.ImageSharp.Formats.Png.Zlib;
@ -251,9 +252,10 @@ namespace SixLabors.ImageSharp.Formats.Png
/// <param name="value">The value to write.</param>
private static void WriteInteger(Stream stream, int value)
{
byte[] buffer = BitConverter.GetBytes(value);
byte[] buffer = new byte[4];
BinaryPrimitives.WriteInt32BigEndian(buffer, value);
buffer.ReverseBytes();
stream.Write(buffer, 0, 4);
}
@ -264,9 +266,10 @@ namespace SixLabors.ImageSharp.Formats.Png
/// <param name="value">The value to write.</param>
private static void WriteInteger(Stream stream, uint value)
{
byte[] buffer = BitConverter.GetBytes(value);
byte[] buffer = new byte[4];
BinaryPrimitives.WriteUInt32BigEndian(buffer, value);
buffer.ReverseBytes();
stream.Write(buffer, 0, 4);
}

Loading…
Cancel
Save