Browse Source

TrueColor images with 32 bits per pixel are assumed to always have 8 bit alpha channel

pull/2643/head
Brian Popow 2 years ago
parent
commit
84a7988f5d
  1. 8
      src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
  2. 5
      src/ImageSharp/Formats/Tga/TgaFileHeader.cs

8
src/ImageSharp/Formats/Tga/TgaDecoderCore.cs

@ -937,7 +937,9 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
this.tgaMetadata = this.metadata.GetTgaMetadata(); this.tgaMetadata = this.metadata.GetTgaMetadata();
this.tgaMetadata.BitsPerPixel = (TgaBitsPerPixel)this.fileHeader.PixelDepth; this.tgaMetadata.BitsPerPixel = (TgaBitsPerPixel)this.fileHeader.PixelDepth;
int alphaBits = this.fileHeader.ImageDescriptor & 0xf; // TrueColor images with 32 bits per pixel are assumed to always have 8 bit alpha channel,
// because some encoders do not set correctly the alpha bits in the image descriptor.
int alphaBits = this.IsTrueColor32BitPerPixel(this.tgaMetadata.BitsPerPixel) ? 8 : this.fileHeader.ImageDescriptor & 0xf;
if (alphaBits is not 0 and not 1 and not 8) if (alphaBits is not 0 and not 1 and not 8)
{ {
TgaThrowHelper.ThrowInvalidImageContentException("Invalid alpha channel bits"); TgaThrowHelper.ThrowInvalidImageContentException("Invalid alpha channel bits");
@ -949,4 +951,8 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
// Bits 4 and 5 describe the image origin. // Bits 4 and 5 describe the image origin.
return (TgaImageOrigin)((this.fileHeader.ImageDescriptor & 0x30) >> 4); return (TgaImageOrigin)((this.fileHeader.ImageDescriptor & 0x30) >> 4);
} }
private bool IsTrueColor32BitPerPixel(TgaBitsPerPixel bitsPerPixel) => bitsPerPixel == TgaBitsPerPixel.Pixel32 &&
(this.fileHeader.ImageType == TgaImageType.TrueColor ||
this.fileHeader.ImageType == TgaImageType.RleTrueColor);
} }

5
src/ImageSharp/Formats/Tga/TgaFileHeader.cs

@ -131,10 +131,7 @@ internal readonly struct TgaFileHeader
/// </summary> /// </summary>
public byte ImageDescriptor { get; } public byte ImageDescriptor { get; }
public static TgaFileHeader Parse(Span<byte> data) public static TgaFileHeader Parse(Span<byte> data) => MemoryMarshal.Cast<byte, TgaFileHeader>(data)[0];
{
return MemoryMarshal.Cast<byte, TgaFileHeader>(data)[0];
}
public void WriteTo(Span<byte> buffer) public void WriteTo(Span<byte> buffer)
{ {

Loading…
Cancel
Save