Browse Source

Fix orientation of RLE images

af/merge-core
Brian Popow 6 years ago
parent
commit
da6ff8b3d6
  1. 3
      src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
  2. 9
      src/ImageSharp/Formats/Tga/TgaEncoderCore.cs

3
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;
}

9
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<byte> buffer = stackalloc byte[TgaFileHeader.Size];
@ -327,7 +330,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
/// <summary>
/// Convert the pixel values to grayscale using ITU-R Recommendation BT.709.
/// </summary>
/// <param name="sourcePixel">The pixel to get the luminance from</param>
/// <param name="sourcePixel">The pixel to get the luminance from.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public static int GetLuminance<TPixel>(TPixel sourcePixel)
where TPixel : struct, IPixel<TPixel>
@ -339,7 +342,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
/// <summary>
/// Convert the pixel values to grayscale using ITU-R Recommendation BT.709.
/// </summary>
/// <param name="vector">The vector to get the luminance from</param>
/// <param name="vector">The vector to get the luminance from.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public static int GetLuminance(ref Vector4 vector)
=> (int)MathF.Round(((.2126F * vector.X) + (.7152F * vector.Y) + (.0722F * vector.Y)) * (256 - 1));

Loading…
Cancel
Save