From aa110740e057bab1c1b17d2c4697e681c1165fc1 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 6 Oct 2019 20:55:03 +0200 Subject: [PATCH] Fix decoding 16 bit tga files by making them opaque --- src/ImageSharp/Formats/Tga/TgaDecoderCore.cs | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index abad2c5e77..92811fa46d 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Buffers; using System.IO; using System.Runtime.CompilerServices; @@ -123,7 +124,10 @@ namespace SixLabors.ImageSharp.Formats.Tga where TPixel : struct, IPixel { using (IManagedByteBuffer row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(this.fileHeader.Width, 2, 0)) + using (IMemoryOwner bgraRow = this.memoryAllocator.Allocate(this.fileHeader.Width)) { + Span bgraRowSpan = bgraRow.GetSpan(); + long currentPosition = this.currentStream.Position; for (int y = 0; y < this.fileHeader.Height; y++) { this.currentStream.Read(row); @@ -134,6 +138,28 @@ namespace SixLabors.ImageSharp.Formats.Tga pixelSpan, this.fileHeader.Width); } + + // We need to set each alpha component value to fully opaque. + // Reset our stream for a second pass. + this.currentStream.Position = currentPosition; + for (int y = 0; y < this.fileHeader.Height; y++) + { + this.currentStream.Read(row); + PixelOperations.Instance.FromBgra5551Bytes( + this.configuration, + row.GetSpan(), + bgraRowSpan, + this.fileHeader.Width); + Span pixelSpan = pixels.GetRowSpan(this.fileHeader.Height - y - 1); + + for (int x = 0; x < this.fileHeader.Width; x++) + { + Bgra5551 bgra = bgraRowSpan[x]; + bgra.PackedValue = (ushort)(bgra.PackedValue | (1 << 15)); + ref TPixel pixel = ref pixelSpan[x]; + pixel.FromBgra5551(bgra); + } + } } }