diff --git a/src/ImageSharp/Common/Extensions/ByteExtensions.cs b/src/ImageSharp/Common/Extensions/ByteExtensions.cs index a6dc19ded4..f1161eb6f6 100644 --- a/src/ImageSharp/Common/Extensions/ByteExtensions.cs +++ b/src/ImageSharp/Common/Extensions/ByteExtensions.cs @@ -5,10 +5,13 @@ namespace ImageSharp { + using System; using System.Runtime.CompilerServices; + using ImageSharp.PixelFormats; + /// - /// Extension methods for the struct. + /// Extension methods for the struct buffers. /// internal static class ByteExtensions { @@ -44,5 +47,45 @@ namespace ImageSharp j--; } } + + /// + /// Returns a reference to the given position of the array unsafe casted to . + /// + /// The byte array. + /// The offset in bytes. + /// The reference at the given offset. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ref Rgb24 GetRgb24(this byte[] bytes, int offset) + { + DebugGuard.MustBeLessThan(offset + 2, bytes.Length, nameof(offset)); + + return ref Unsafe.As(ref bytes[offset]); + } + + /// + /// Returns a reference to the given position of the span unsafe casted to . + /// + /// The byte span. + /// The offset in bytes. + /// The reference at the given offset. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ref Rgb24 GetRgb24(this Span bytes, int offset) + { + DebugGuard.MustBeLessThan(offset + 2, bytes.Length, nameof(offset)); + + return ref Unsafe.As(ref bytes[offset]); + } + + /// + /// Returns a reference to the given position of the buffer pointed by `baseRef` unsafe casted to . + /// + /// A reference to the beginning of the buffer + /// The offset in bytes. + /// The reference at the given offset. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ref Rgb24 GetRgb24(ref byte baseRef, int offset) + { + return ref Unsafe.As(ref Unsafe.Add(ref baseRef, offset)); + } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index 23eaa502c6..997a77d6c5 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -249,15 +249,11 @@ namespace ImageSharp.Formats for (int y = 0; y < height; y++) { int newY = Invert(y, height, inverted); - - // TODO: Could use PixelOperations here! - this.currentStream.Read(row, 0, row.Length); - int offset = 0; - Span pixelRow = pixels.GetRowSpan(y); + // TODO: Could use PixelOperations here! for (int x = 0; x < arrayWidth; x++) { int colOffset = x * ppb; diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 7092d2dd0a..5b56c4c02f 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -448,7 +448,7 @@ namespace ImageSharp.Formats int indexOffset = index * 3; ref TPixel pixel = ref rowSpan[x]; - rgba.Rgb = Rgb24.AsRgb24(colorTable, indexOffset); + rgba.Rgb = colorTable.GetRgb24(indexOffset); pixel.PackFromRgba32(rgba); } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 12c7bb8033..d2b68978fc 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -643,7 +643,7 @@ namespace ImageSharp.Formats if (rgba.A > 0) { - rgba.Rgb = Rgb24.AsRgb24(palette, pixelOffset); + rgba.Rgb = palette.GetRgb24(pixelOffset); } else { @@ -663,7 +663,7 @@ namespace ImageSharp.Formats int index = newScanline[x + 1]; int pixelOffset = index * 3; - rgba.Rgb = Rgb24.AsRgb24(palette, pixelOffset); + rgba.Rgb = palette.GetRgb24(pixelOffset); color.PackFromRgba32(rgba); row[x] = color; @@ -728,7 +728,7 @@ namespace ImageSharp.Formats if (rgba.A > 0) { - rgba.Rgb = Rgb24.AsRgb24(this.palette, offset); + rgba.Rgb = this.palette.GetRgb24(offset); } else { @@ -748,7 +748,7 @@ namespace ImageSharp.Formats int index = newScanline[o]; int offset = index * 3; - rgba.Rgb = Rgb24.AsRgb24(this.palette, offset); + rgba.Rgb = this.palette.GetRgb24(offset); color.PackFromRgba32(rgba); rowSpan[x] = color; diff --git a/src/ImageSharp/PixelFormats/PixelConversionExtensions.cs b/src/ImageSharp/PixelFormats/PixelConversionExtensions.cs index 98a2083a15..1ea2628951 100644 --- a/src/ImageSharp/PixelFormats/PixelConversionExtensions.cs +++ b/src/ImageSharp/PixelFormats/PixelConversionExtensions.cs @@ -21,7 +21,7 @@ public static void ToXyzBytes(this TPixel pixel, Span bytes, int startIndex) where TPixel : struct, IPixel { - ref Rgb24 dest = ref Rgb24.AsRgb24(bytes, startIndex); + ref Rgb24 dest = ref bytes.GetRgb24(startIndex); pixel.ToRgb24(ref dest); } diff --git a/src/ImageSharp/PixelFormats/Rgb24.cs b/src/ImageSharp/PixelFormats/Rgb24.cs index 1c91635dcc..13dcaff95f 100644 --- a/src/ImageSharp/PixelFormats/Rgb24.cs +++ b/src/ImageSharp/PixelFormats/Rgb24.cs @@ -111,23 +111,5 @@ namespace ImageSharp.PixelFormats { throw new NotImplementedException(); } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ref Rgb24 AsRgb24(byte[] bytes, int offset) - { - return ref Unsafe.As(ref bytes[offset]); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static ref Rgb24 AsRgb24(ref byte baseRef, int offset) - { - return ref Unsafe.As(ref Unsafe.Add(ref baseRef, offset)); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ref Rgb24 AsRgb24(Span bytes, int offset) - { - return ref Unsafe.As(ref bytes[offset]); - } } } \ No newline at end of file