From 14f7ff8fb74a7cd20eb799e70328439fac3c075d Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Wed, 21 Mar 2018 08:41:47 -0700 Subject: [PATCH] Remove ReverseBytes (replacing with faster BinaryPrimitives calls) --- .../Common/Extensions/ByteExtensions.cs | 33 ------------------- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 12 +++---- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 11 ++++--- 3 files changed, 12 insertions(+), 44 deletions(-) diff --git a/src/ImageSharp/Common/Extensions/ByteExtensions.cs b/src/ImageSharp/Common/Extensions/ByteExtensions.cs index f6c720795..b5b868dea 100644 --- a/src/ImageSharp/Common/Extensions/ByteExtensions.cs +++ b/src/ImageSharp/Common/Extensions/ByteExtensions.cs @@ -12,39 +12,6 @@ namespace SixLabors.ImageSharp /// internal static class ByteExtensions { - /// - /// Optimized reversal algorithm. - /// - /// The byte array. - public static void ReverseBytes(this byte[] source) - { - ReverseBytes(source, 0, source.Length); - } - - /// - /// Optimized reversal algorithm. - /// - /// The byte array. - /// The index. - /// The length. - /// is null. - [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--; - } - } - /// /// Returns a reference to the given position of the array unsafe casted to . /// diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index fbb5c29a4..349fa7745 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/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); } /// diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 7ae075569..55bf1bbec 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/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 /// The value to write. 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 /// The value to write. 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); }