Browse Source

Cleanup ByteExtensions

pull/567/head
Jason Nelson 8 years ago
parent
commit
cddab85a5a
  1. 35
      src/ImageSharp/Common/Extensions/ByteExtensions.cs
  2. 2
      src/ImageSharp/Formats/Gif/GifDecoderCore.cs
  3. 12
      src/ImageSharp/Formats/Png/PngDecoderCore.cs

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

@ -12,44 +12,15 @@ namespace SixLabors.ImageSharp
/// </summary>
internal static class ByteExtensions
{
/// <summary>
/// Returns a reference to the given position of the array unsafe casted to <see cref="ImageSharp.PixelFormats.Rgb24"/>.
/// </summary>
/// <param name="bytes">The byte array.</param>
/// <param name="offset">The offset in bytes.</param>
/// <returns>The <see cref="ImageSharp.PixelFormats.Rgb24"/> reference at the given offset.</returns>
[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<byte, Rgb24>(ref bytes[offset]);
}
/// <summary>
/// Returns a reference to the given position of the span unsafe casted to <see cref="ImageSharp.PixelFormats.Rgb24"/>.
/// </summary>
/// <param name="bytes">The byte span.</param>
/// <param name="offset">The offset in bytes.</param>
/// <returns>The <see cref="ImageSharp.PixelFormats.Rgb24"/> reference at the given offset.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref Rgb24 GetRgb24(this Span<byte> bytes, int offset)
{
DebugGuard.MustBeLessThan(offset + 2, bytes.Length, nameof(offset));
return ref Unsafe.As<byte, Rgb24>(ref bytes[offset]);
}
/// <summary>
/// Returns a reference to the given position of the buffer pointed by `baseRef` unsafe casted to <see cref="ImageSharp.PixelFormats.Rgb24"/>.
/// </summary>
/// <param name="baseRef">A reference to the beginning of the buffer</param>
/// <param name="offset">The offset in bytes.</param>
/// <returns>The <see cref="ImageSharp.PixelFormats.Rgb24"/> reference at the given offset.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref Rgb24 GetRgb24(ref byte baseRef, int offset)
public static ref Rgb24 AsRgb24(this Span<byte> bytes)
{
return ref Unsafe.As<byte, Rgb24>(ref Unsafe.Add(ref baseRef, offset));
}
return ref Unsafe.As<byte, Rgb24>(ref bytes[0]);
}
}
}

2
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);
}

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

@ -853,7 +853,7 @@ namespace SixLabors.ImageSharp.Formats.Png
where TPixel : struct, IPixel<TPixel>
{
ReadOnlySpan<byte> newScanline = ToArrayByBitsLength(defilteredScanline, this.bytesPerScanline, this.header.BitDepth);
byte[] pal = this.palette;
Span<byte> 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<byte> newScanline = ToArrayByBitsLength(scanlineBuffer, this.bytesPerScanline, this.header.BitDepth);
var rgba = default(Rgba32);
Span<byte> 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;

Loading…
Cancel
Save