From 2bfe960d6a9f8a5a0be8071fb7094856469ffdce Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 8 Oct 2019 19:50:13 +0200 Subject: [PATCH] Unified reading rle images --- src/ImageSharp/Formats/Tga/TgaDecoderCore.cs | 98 +++++--------------- 1 file changed, 25 insertions(+), 73 deletions(-) diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index 0627be98c..224a14131 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -84,7 +84,7 @@ namespace SixLabors.ImageSharp.Formats.Tga case 24: if (this.fileHeader.ImageType.IsRunLengthEncoded()) { - this.ReadRle24(pixels, this.fileHeader.Width, this.fileHeader.Height); + this.ReadRle(this.fileHeader.Width, this.fileHeader.Height, pixels, 3); } else { @@ -96,7 +96,7 @@ namespace SixLabors.ImageSharp.Formats.Tga case 32: if (this.fileHeader.ImageType.IsRunLengthEncoded()) { - this.ReadRle32(pixels, this.fileHeader.Width, this.fileHeader.Height); + this.ReadRle(this.fileHeader.Width, this.fileHeader.Height, pixels, 4); } else { @@ -197,64 +197,6 @@ namespace SixLabors.ImageSharp.Formats.Tga } } - private void ReadRle24(Buffer2D pixels, int width, int height) - where TPixel : struct, IPixel - { - TPixel color = default; - using (IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height * 3, AllocationOptions.Clean)) - { - Span bufferSpan = buffer.GetSpan(); - this.UncompressRle24(width, height, bufferSpan); - for (int y = 0; y < height; y++) - { - Span pixelRow = pixels.GetRowSpan(this.fileHeader.Height - y - 1); - for (int x = 0; x < width; x++) - { - int idx = (y * width * 3) + (x * 3); - color.FromBgr24(Unsafe.As(ref bufferSpan[idx])); - pixelRow[x] = color; - } - } - } - } - - private void UncompressRle24(int width, int height, Span buffer) - { - int uncompressedPixels = 0; - var pixel = new byte[3]; - int totalPixels = width * height; - while (uncompressedPixels < totalPixels) - { - byte runLengthByte = (byte)this.currentStream.ReadByte(); - - // The high bit of a run length packet is set to 1. - int highBit = runLengthByte >> 7; - if (highBit == 1) - { - int runLength = runLengthByte & 127; - this.currentStream.Read(pixel, 0, 3); - int bufferIdx = uncompressedPixels * 3; - for (int i = 0; i < runLength + 1; i++, uncompressedPixels++) - { - pixel.AsSpan().CopyTo(buffer.Slice(bufferIdx)); - bufferIdx += 3; - } - } - else - { - // Non-run-length encoded packet. - int runLength = runLengthByte; - int bufferIdx = uncompressedPixels * 3; - for (int i = 0; i < runLength + 1; i++, uncompressedPixels++) - { - this.currentStream.Read(pixel, 0, 3); - pixel.AsSpan().CopyTo(buffer.Slice(bufferIdx)); - bufferIdx += 3; - } - } - } - } - private void ReadBgra32(Buffer2D pixels) where TPixel : struct, IPixel { @@ -273,31 +215,41 @@ namespace SixLabors.ImageSharp.Formats.Tga } } - private void ReadRle32(Buffer2D pixels, int width, int height) + private void ReadRle(int width, int height, Buffer2D pixels, int bytesPerPixel) where TPixel : struct, IPixel { TPixel color = default; - using (IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height * 4, AllocationOptions.Clean)) + using (IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height * bytesPerPixel, AllocationOptions.Clean)) { Span bufferSpan = buffer.GetSpan(); - this.UncompressRle32(width, height, bufferSpan); + this.UncompressRle(width, height, bufferSpan, bytesPerPixel); for (int y = 0; y < height; y++) { Span pixelRow = pixels.GetRowSpan(this.fileHeader.Height - y - 1); + int rowStartIdx = y * width * bytesPerPixel; for (int x = 0; x < width; x++) { - int idx = (y * width * 4) + (x * 4); - color.FromBgra32(Unsafe.As(ref bufferSpan[idx])); + int idx = rowStartIdx + (x * bytesPerPixel); + switch (bytesPerPixel) + { + case 4: + color.FromBgra32(Unsafe.As(ref bufferSpan[idx])); + break; + case 3: + color.FromBgr24(Unsafe.As(ref bufferSpan[idx])); + break; + } + pixelRow[x] = color; } } } } - private void UncompressRle32(int width, int height, Span buffer) + private void UncompressRle(int width, int height, Span buffer, int bytesPerPixel) { int uncompressedPixels = 0; - var pixel = new byte[4]; + var pixel = new byte[bytesPerPixel]; int totalPixels = width * height; while (uncompressedPixels < totalPixels) { @@ -308,24 +260,24 @@ namespace SixLabors.ImageSharp.Formats.Tga if (highBit == 1) { int runLength = runLengthByte & 127; - this.currentStream.Read(pixel, 0, 4); - int bufferIdx = uncompressedPixels * 4; + this.currentStream.Read(pixel, 0, bytesPerPixel); + int bufferIdx = uncompressedPixels * bytesPerPixel; for (int i = 0; i < runLength + 1; i++, uncompressedPixels++) { pixel.AsSpan().CopyTo(buffer.Slice(bufferIdx)); - bufferIdx += 4; + bufferIdx += bytesPerPixel; } } else { // Non-run-length encoded packet. int runLength = runLengthByte; - int bufferIdx = uncompressedPixels * 4; + int bufferIdx = uncompressedPixels * bytesPerPixel; for (int i = 0; i < runLength + 1; i++, uncompressedPixels++) { - this.currentStream.Read(pixel, 0, 4); + this.currentStream.Read(pixel, 0, bytesPerPixel); pixel.AsSpan().CopyTo(buffer.Slice(bufferIdx)); - bufferIdx += 4; + bufferIdx += bytesPerPixel; } } }