Browse Source

Use MemoryMarshal.Cast to bgr in DecodePixelValues()

pull/1552/head
Brian Popow 6 years ago
parent
commit
36f7ea0035
  1. 23
      src/ImageSharp/Formats/WebP/Lossy/WebpLossyDecoder.cs

23
src/ImageSharp/Formats/WebP/Lossy/WebpLossyDecoder.cs

@ -112,43 +112,38 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Webp.Lossy
} }
} }
private void DecodePixelValues<TPixel>(int width, int height, Span<byte> pixelData, Buffer2D<TPixel> pixels) private void DecodePixelValues<TPixel>(int width, int height, Span<byte> pixelData, Buffer2D<TPixel> decodedPixels)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
int widthMul3 = width * 3; int widthMul3 = width * 3;
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
Span<byte> row = pixelData.Slice(y * widthMul3, widthMul3); Span<byte> row = pixelData.Slice(y * widthMul3, widthMul3);
Span<TPixel> pixelSpan = pixels.GetRowSpan(y); Span<TPixel> decodedPixelRow = decodedPixels.GetRowSpan(y);
PixelOperations<TPixel>.Instance.FromBgr24Bytes( PixelOperations<TPixel>.Instance.FromBgr24Bytes(
this.configuration, this.configuration,
row, row,
pixelSpan, decodedPixelRow,
width); width);
} }
} }
private void DecodePixelValues<TPixel>(int width, int height, Span<byte> pixelData, Buffer2D<TPixel> pixels, IMemoryOwner<byte> alpha) private void DecodePixelValues<TPixel>(int width, int height, Span<byte> pixelData, Buffer2D<TPixel> decodedPixels, IMemoryOwner<byte> alpha)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
TPixel color = default; TPixel color = default;
Span<byte> alphaSpan = alpha.Memory.Span; Span<byte> alphaSpan = alpha.Memory.Span;
Span<Bgr24> pixelsBgr = MemoryMarshal.Cast<byte, Bgr24>(pixelData);
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
// TODO: Can we use span.Length here?
int yMulWidth = y * width; int yMulWidth = y * width;
Span<TPixel> pixelRow = pixels.GetRowSpan(y); Span<TPixel> decodedPixelRow = decodedPixels.GetRowSpan(y);
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
// TODO: Could use cast to Bgr24/Bgra32 then set alpha.
int offset = yMulWidth + x; int offset = yMulWidth + x;
int idxBgr = offset * 3; Bgr24 bgr = pixelsBgr[offset];
byte b = pixelData[idxBgr]; color.FromBgra32(new Bgra32(bgr.R, bgr.G, bgr.B, alphaSpan[offset]));
byte g = pixelData[idxBgr + 1]; decodedPixelRow[x] = color;
byte r = pixelData[idxBgr + 2];
byte a = alphaSpan[offset];
color.FromBgra32(new Bgra32(r, g, b, a));
pixelRow[x] = color;
} }
} }
} }

Loading…
Cancel
Save