Browse Source

document and refactor GetRgb24()

af/merge-core
Anton Firszov 9 years ago
parent
commit
ae70af3159
  1. 45
      src/ImageSharp/Common/Extensions/ByteExtensions.cs
  2. 6
      src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
  3. 2
      src/ImageSharp/Formats/Gif/GifDecoderCore.cs
  4. 8
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  5. 2
      src/ImageSharp/PixelFormats/PixelConversionExtensions.cs
  6. 18
      src/ImageSharp/PixelFormats/Rgb24.cs

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

@ -5,10 +5,13 @@
namespace ImageSharp namespace ImageSharp
{ {
using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using ImageSharp.PixelFormats;
/// <summary> /// <summary>
/// Extension methods for the <see cref="byte"/> struct. /// Extension methods for the <see cref="byte"/> struct buffers.
/// </summary> /// </summary>
internal static class ByteExtensions internal static class ByteExtensions
{ {
@ -44,5 +47,45 @@ namespace ImageSharp
j--; j--;
} }
} }
/// <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)
{
return ref Unsafe.As<byte, Rgb24>(ref Unsafe.Add(ref baseRef, offset));
}
} }
} }

6
src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

@ -249,15 +249,11 @@ namespace ImageSharp.Formats
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
int newY = Invert(y, height, inverted); int newY = Invert(y, height, inverted);
// TODO: Could use PixelOperations here!
this.currentStream.Read(row, 0, row.Length); this.currentStream.Read(row, 0, row.Length);
int offset = 0; int offset = 0;
Span<TPixel> pixelRow = pixels.GetRowSpan(y); Span<TPixel> pixelRow = pixels.GetRowSpan(y);
// TODO: Could use PixelOperations here!
for (int x = 0; x < arrayWidth; x++) for (int x = 0; x < arrayWidth; x++)
{ {
int colOffset = x * ppb; int colOffset = x * ppb;

2
src/ImageSharp/Formats/Gif/GifDecoderCore.cs

@ -448,7 +448,7 @@ namespace ImageSharp.Formats
int indexOffset = index * 3; int indexOffset = index * 3;
ref TPixel pixel = ref rowSpan[x]; ref TPixel pixel = ref rowSpan[x];
rgba.Rgb = Rgb24.AsRgb24(colorTable, indexOffset); rgba.Rgb = colorTable.GetRgb24(indexOffset);
pixel.PackFromRgba32(rgba); pixel.PackFromRgba32(rgba);
} }

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

@ -643,7 +643,7 @@ namespace ImageSharp.Formats
if (rgba.A > 0) if (rgba.A > 0)
{ {
rgba.Rgb = Rgb24.AsRgb24(palette, pixelOffset); rgba.Rgb = palette.GetRgb24(pixelOffset);
} }
else else
{ {
@ -663,7 +663,7 @@ namespace ImageSharp.Formats
int index = newScanline[x + 1]; int index = newScanline[x + 1];
int pixelOffset = index * 3; int pixelOffset = index * 3;
rgba.Rgb = Rgb24.AsRgb24(palette, pixelOffset); rgba.Rgb = palette.GetRgb24(pixelOffset);
color.PackFromRgba32(rgba); color.PackFromRgba32(rgba);
row[x] = color; row[x] = color;
@ -728,7 +728,7 @@ namespace ImageSharp.Formats
if (rgba.A > 0) if (rgba.A > 0)
{ {
rgba.Rgb = Rgb24.AsRgb24(this.palette, offset); rgba.Rgb = this.palette.GetRgb24(offset);
} }
else else
{ {
@ -748,7 +748,7 @@ namespace ImageSharp.Formats
int index = newScanline[o]; int index = newScanline[o];
int offset = index * 3; int offset = index * 3;
rgba.Rgb = Rgb24.AsRgb24(this.palette, offset); rgba.Rgb = this.palette.GetRgb24(offset);
color.PackFromRgba32(rgba); color.PackFromRgba32(rgba);
rowSpan[x] = color; rowSpan[x] = color;

2
src/ImageSharp/PixelFormats/PixelConversionExtensions.cs

@ -21,7 +21,7 @@
public static void ToXyzBytes<TPixel>(this TPixel pixel, Span<byte> bytes, int startIndex) public static void ToXyzBytes<TPixel>(this TPixel pixel, Span<byte> bytes, int startIndex)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
ref Rgb24 dest = ref Rgb24.AsRgb24(bytes, startIndex); ref Rgb24 dest = ref bytes.GetRgb24(startIndex);
pixel.ToRgb24(ref dest); pixel.ToRgb24(ref dest);
} }

18
src/ImageSharp/PixelFormats/Rgb24.cs

@ -111,23 +111,5 @@ namespace ImageSharp.PixelFormats
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref Rgb24 AsRgb24(byte[] bytes, int offset)
{
return ref Unsafe.As<byte, Rgb24>(ref bytes[offset]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ref Rgb24 AsRgb24(ref byte baseRef, int offset)
{
return ref Unsafe.As<byte, Rgb24>(ref Unsafe.Add(ref baseRef, offset));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref Rgb24 AsRgb24(Span<byte> bytes, int offset)
{
return ref Unsafe.As<byte, Rgb24>(ref bytes[offset]);
}
} }
} }
Loading…
Cancel
Save