diff --git a/src/ImageSharp/Common/Extensions/ByteExtensions.cs b/src/ImageSharp/Common/Extensions/ByteExtensions.cs index 00eab47fc..89cfe6974 100644 --- a/src/ImageSharp/Common/Extensions/ByteExtensions.cs +++ b/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. /// - /// The bytes to convert from. Cannot be null. + /// The bytes to convert from. Cannot be null. /// The number of bits per value. /// The resulting array. Is never null. - /// is null. + /// is null. /// is less than or equals than zero. - 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; } + + /// + /// 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. + 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--; + } + } } } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index ecbc28a98..deb4ace41 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -184,8 +184,8 @@ namespace ImageSharp.Formats where TColor : struct, IPackedPixel 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; } - - /// - /// Optimized reversal algorithm. - /// - /// The byte array. - private void ReverseBytes(byte[] source) - { - this.ReverseBytes(source, 0, source.Length); - } - - /// - /// Optimized reversal algorithm. - /// - /// The byte array. - /// The index. - /// The length. - 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--; - } - } } }