diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index aa830057e8..d861450e04 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -362,24 +362,26 @@ namespace SixLabors.ImageSharp.Formats.Tga where TPixel : struct, IPixel { using (IManagedByteBuffer row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 2, 0)) - using (IMemoryOwner bgraRow = this.memoryAllocator.Allocate(width)) { - Span bgraRowSpan = bgraRow.GetSpan(); - long currentPosition = this.currentStream.Position; for (int y = 0; y < height; y++) { this.currentStream.Read(row); + Span rowSpan = row.GetSpan(); + + // We need to set each alpha component value to fully opaque. + for (int x = 1; x < rowSpan.Length; x += 2) + { + rowSpan[x] = (byte)(rowSpan[x] | (1 << 7)); + } + int newY = Invert(y, height, inverted); Span pixelSpan = pixels.GetRowSpan(newY); PixelOperations.Instance.FromBgra5551Bytes( this.configuration, - row.GetSpan(), + rowSpan, pixelSpan, width); } - - // We need to set each alpha component value to fully opaque. - this.MakeOpaque(pixels, width, height, currentPosition, row, bgraRowSpan); } } @@ -544,43 +546,6 @@ namespace SixLabors.ImageSharp.Formats.Tga } } - /// - /// Helper method for decoding BGRA5551 images. Makes the pixels opaque, because the high bit does not - /// represent an alpha channel. - /// TODO: maybe there is a better/faster way to achieve this. - /// - /// The pixel type. - /// The destination pixel buffer. - /// The width of the image. - /// The height of the image. - /// The start position of pixel data. - /// A byte array to store the read pixel data. - /// Bgra pixel row span. - private void MakeOpaque(Buffer2D pixels, int width, int height, long pixelDataStart, IManagedByteBuffer row, Span bgraRowSpan) - where TPixel : struct, IPixel - { - // Reset our stream for a second pass. - this.currentStream.Position = pixelDataStart; - for (int y = 0; y < width; y++) - { - this.currentStream.Read(row); - PixelOperations.Instance.FromBgra5551Bytes( - this.configuration, - row.GetSpan(), - bgraRowSpan, - this.fileHeader.Width); - Span pixelSpan = pixels.GetRowSpan(height - y - 1); - - for (int x = 0; x < width; x++) - { - Bgra5551 bgra = bgraRowSpan[x]; - bgra.PackedValue = (ushort)(bgra.PackedValue | (1 << 15)); - ref TPixel pixel = ref pixelSpan[x]; - pixel.FromBgra5551(bgra); - } - } - } - /// /// Returns the y- value based on the given height. ///