diff --git a/src/ImageSharp/Common/Extensions/ByteExtensions.cs b/src/ImageSharp/Common/Extensions/ByteExtensions.cs index b5b868dea..ec53063e6 100644 --- a/src/ImageSharp/Common/Extensions/ByteExtensions.cs +++ b/src/ImageSharp/Common/Extensions/ByteExtensions.cs @@ -12,44 +12,15 @@ namespace SixLabors.ImageSharp /// internal static class ByteExtensions { - /// - /// 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) + public static ref Rgb24 AsRgb24(this Span bytes) { - return ref Unsafe.As(ref Unsafe.Add(ref baseRef, offset)); - } + return ref Unsafe.As(ref bytes[0]); + } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 1900d0df0..6c03bd2b1 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -461,7 +461,7 @@ namespace SixLabors.ImageSharp.Formats.Gif int indexOffset = index * 3; ref TPixel pixel = ref Unsafe.Add(ref rowRef, x); - rgba.Rgb = colorTable.GetRgb24(indexOffset); + rgba.Rgb = colorTable.Slice(indexOffset).AsRgb24(); pixel.PackFromRgba32(rgba); } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 8fefcb480..20a650e1f 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -853,7 +853,7 @@ namespace SixLabors.ImageSharp.Formats.Png where TPixel : struct, IPixel { ReadOnlySpan newScanline = ToArrayByBitsLength(defilteredScanline, this.bytesPerScanline, this.header.BitDepth); - byte[] pal = this.palette; + Span pal = this.palette; var color = default(TPixel); var rgba = default(Rgba32); @@ -868,7 +868,7 @@ namespace SixLabors.ImageSharp.Formats.Png int pixelOffset = index * 3; rgba.A = this.paletteAlpha.Length > index ? this.paletteAlpha[index] : (byte)255; - rgba.Rgb = pal.GetRgb24(pixelOffset); + rgba.Rgb = pal.Slice(pixelOffset).AsRgb24(); color.PackFromRgba32(rgba); row[x] = color; @@ -883,7 +883,7 @@ namespace SixLabors.ImageSharp.Formats.Png int index = newScanline[x]; int pixelOffset = index * 3; - rgba.Rgb = pal.GetRgb24(pixelOffset); + rgba.Rgb = pal.Slice(pixelOffset).AsRgb24(); color.PackFromRgba32(rgba); row[x] = color; @@ -946,9 +946,11 @@ namespace SixLabors.ImageSharp.Formats.Png ReadOnlySpan newScanline = ToArrayByBitsLength(scanlineBuffer, this.bytesPerScanline, this.header.BitDepth); var rgba = default(Rgba32); + Span pal = this.palette; if (this.paletteAlpha != null && this.paletteAlpha.Length > 0) { + // If the alpha palette is not null and has one or more entries, this means, that the image contains an alpha // channel and we should try to read it. for (int x = pixelOffset, o = 0; x < this.header.Width; x += increment, o++) @@ -957,7 +959,7 @@ namespace SixLabors.ImageSharp.Formats.Png int offset = index * 3; rgba.A = this.paletteAlpha.Length > index ? this.paletteAlpha[index] : (byte)255; - rgba.Rgb = this.palette.GetRgb24(offset); + rgba.Rgb = pal.Slice(offset).AsRgb24(); color.PackFromRgba32(rgba); rowSpan[x] = color; @@ -972,7 +974,7 @@ namespace SixLabors.ImageSharp.Formats.Png int index = newScanline[o]; int offset = index * 3; - rgba.Rgb = this.palette.GetRgb24(offset); + rgba.Rgb = pal.Slice(offset).AsRgb24(); color.PackFromRgba32(rgba); rowSpan[x] = color;