From da6ff8b3d6a3e09fac10b62a232f05008fb778e2 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 13 Oct 2019 19:16:40 +0200 Subject: [PATCH] Fix orientation of RLE images --- src/ImageSharp/Formats/Tga/TgaDecoderCore.cs | 3 ++- src/ImageSharp/Formats/Tga/TgaEncoderCore.cs | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index 8f36a7626..e3a09601a 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -590,7 +590,8 @@ namespace SixLabors.ImageSharp.Formats.Tga this.tgaMetadata = this.metadata.GetFormatMetadata(TgaFormat.Instance); this.tgaMetadata.BitsPerPixel = (TgaBitsPerPixel)this.fileHeader.PixelDepth; - if (this.fileHeader.YOffset > 0) + // Bit at position 3 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 1bde05c93..a441a40d8 100644 --- a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs @@ -74,6 +74,9 @@ 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. + byte imageDescriptor = (byte)(this.useCompression ? 32 : 0); + var fileHeader = new TgaFileHeader( idLength: 0, colorMapType: 0, @@ -86,7 +89,7 @@ namespace SixLabors.ImageSharp.Formats.Tga width: (short)image.Width, height: (short)image.Height, pixelDepth: (byte)this.bitsPerPixel.Value, - imageDescriptor: 0); + imageDescriptor: (byte)(this.useCompression ? 32 : 0)); #if NETCOREAPP2_1 Span buffer = stackalloc byte[TgaFileHeader.Size]; @@ -327,7 +330,7 @@ namespace SixLabors.ImageSharp.Formats.Tga /// /// Convert the pixel values to grayscale using ITU-R Recommendation BT.709. /// - /// The pixel to get the luminance from + /// The pixel to get the luminance from. [MethodImpl(InliningOptions.ShortMethod)] public static int GetLuminance(TPixel sourcePixel) where TPixel : struct, IPixel @@ -339,7 +342,7 @@ namespace SixLabors.ImageSharp.Formats.Tga /// /// Convert the pixel values to grayscale using ITU-R Recommendation BT.709. /// - /// The vector to get the luminance from + /// The vector to get the luminance from. [MethodImpl(InliningOptions.ShortMethod)] public static int GetLuminance(ref Vector4 vector) => (int)MathF.Round(((.2126F * vector.X) + (.7152F * vector.Y) + (.0722F * vector.Y)) * (256 - 1));