diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index e3a09601aa..e1850a32b6 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -141,7 +141,6 @@ namespace SixLabors.ImageSharp.Formats.Tga case 16: if (this.fileHeader.ImageType.IsRunLengthEncoded()) { - long currentPosition = this.currentStream.Position; this.ReadRle(this.fileHeader.Width, this.fileHeader.Height, pixels, 2, inverted); } else @@ -218,7 +217,7 @@ namespace SixLabors.ImageSharp.Formats.Tga { int colorIndex = rowSpan[x]; - // Set bit 16 to 1, to treat it as opaque for Bgra5551. + // Set alpha value to 1, to treat it as opaque for Bgra5551. Bgra5551 bgra = Unsafe.As(ref palette[colorIndex * colorMapPixelSizeInBytes]); bgra.PackedValue = (ushort)(bgra.PackedValue | 0x8000); color.FromBgra5551(bgra); @@ -285,7 +284,7 @@ namespace SixLabors.ImageSharp.Formats.Tga color.FromGray8(Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes])); break; case 2: - // Set bit 16 to 1, to treat it as opaque for Bgra5551. + // Set alpha value to 1, to treat it as opaque for Bgra5551. Bgra5551 bgra = Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]); bgra.PackedValue = (ushort)(bgra.PackedValue | 0x8000); color.FromBgra5551(bgra); @@ -449,7 +448,7 @@ namespace SixLabors.ImageSharp.Formats.Tga color.FromGray8(Unsafe.As(ref bufferSpan[idx])); break; case 2: - // Set bit 16 to 1, to treat it as opaque for Bgra5551. + // Set alpha value to 1, to treat it as opaque for Bgra5551. bufferSpan[idx + 1] = (byte)(bufferSpan[idx + 1] | 128); color.FromBgra5551(Unsafe.As(ref bufferSpan[idx])); break; @@ -590,7 +589,7 @@ namespace SixLabors.ImageSharp.Formats.Tga this.tgaMetadata = this.metadata.GetFormatMetadata(TgaFormat.Instance); this.tgaMetadata.BitsPerPixel = (TgaBitsPerPixel)this.fileHeader.PixelDepth; - // Bit at position 3 of the descriptor indicates, that the origin is top left instead of bottom right. + // Bit at position 5 of the descriptor indicates, that the origin is top left instead of bottom right. if ((this.fileHeader.ImageDescriptor & (1 << 5)) != 0) { return true; diff --git a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs index a441a40d82..0b0e0b1935 100644 --- a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Buffers.Binary; using System.IO; using System.Numerics; using System.Runtime.CompilerServices; @@ -29,6 +30,11 @@ namespace SixLabors.ImageSharp.Formats.Tga /// private Configuration configuration; + /// + /// Reusable buffer for writing data. + /// + private readonly byte[] buffer = new byte[2]; + /// /// The color depth, in number of bits per pixel. /// @@ -74,7 +80,7 @@ namespace SixLabors.ImageSharp.Formats.Tga imageType = this.useCompression ? TgaImageType.RleBlackAndWhite : TgaImageType.BlackAndWhite; } - // If compression is used, set byte 3 of the image descriptor to indicate an left top origin. + // If compression is used, set bit 5 of the image descriptor to indicate an left top origin. byte imageDescriptor = (byte)(this.useCompression ? 32 : 0); var fileHeader = new TgaFileHeader( @@ -89,7 +95,7 @@ namespace SixLabors.ImageSharp.Formats.Tga width: (short)image.Width, height: (short)image.Height, pixelDepth: (byte)this.bitsPerPixel.Value, - imageDescriptor: (byte)(this.useCompression ? 32 : 0)); + imageDescriptor: imageDescriptor); #if NETCOREAPP2_1 Span buffer = stackalloc byte[TgaFileHeader.Size]; @@ -174,10 +180,10 @@ namespace SixLabors.ImageSharp.Formats.Tga break; case TgaBitsPerPixel.Pixel16: - // TODO: this seems to be wrong var bgra5551 = new Bgra5551(color.ToVector4()); - stream.WriteByte((byte)(bgra5551.PackedValue & 0xFF)); - stream.WriteByte((byte)(bgra5551.PackedValue & 0xFF00)); + BinaryPrimitives.TryWriteInt16LittleEndian(this.buffer, (short)bgra5551.PackedValue); + stream.WriteByte(this.buffer[0]); + stream.WriteByte(this.buffer[1]); break;