Skip to content

Commit 84a7988

Browse files
committed
TrueColor images with 32 bits per pixel are assumed to always have 8 bit alpha channel
1 parent efd0c8e commit 84a7988

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/ImageSharp/Formats/Tga/TgaDecoderCore.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,9 @@ private TgaImageOrigin ReadFileHeader(BufferedReadStream stream)
937937
this.tgaMetadata = this.metadata.GetTgaMetadata();
938938
this.tgaMetadata.BitsPerPixel = (TgaBitsPerPixel)this.fileHeader.PixelDepth;
939939

940-
int alphaBits = this.fileHeader.ImageDescriptor & 0xf;
940+
// TrueColor images with 32 bits per pixel are assumed to always have 8 bit alpha channel,
941+
// because some encoders do not set correctly the alpha bits in the image descriptor.
942+
int alphaBits = this.IsTrueColor32BitPerPixel(this.tgaMetadata.BitsPerPixel) ? 8 : this.fileHeader.ImageDescriptor & 0xf;
941943
if (alphaBits is not 0 and not 1 and not 8)
942944
{
943945
TgaThrowHelper.ThrowInvalidImageContentException("Invalid alpha channel bits");
@@ -949,4 +951,8 @@ private TgaImageOrigin ReadFileHeader(BufferedReadStream stream)
949951
// Bits 4 and 5 describe the image origin.
950952
return (TgaImageOrigin)((this.fileHeader.ImageDescriptor & 0x30) >> 4);
951953
}
954+
955+
private bool IsTrueColor32BitPerPixel(TgaBitsPerPixel bitsPerPixel) => bitsPerPixel == TgaBitsPerPixel.Pixel32 &&
956+
(this.fileHeader.ImageType == TgaImageType.TrueColor ||
957+
this.fileHeader.ImageType == TgaImageType.RleTrueColor);
952958
}

src/ImageSharp/Formats/Tga/TgaFileHeader.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ public TgaFileHeader(
131131
/// </summary>
132132
public byte ImageDescriptor { get; }
133133

134-
public static TgaFileHeader Parse(Span<byte> data)
135-
{
136-
return MemoryMarshal.Cast<byte, TgaFileHeader>(data)[0];
137-
}
134+
public static TgaFileHeader Parse(Span<byte> data) => MemoryMarshal.Cast<byte, TgaFileHeader>(data)[0];
138135

139136
public void WriteTo(Span<byte> buffer)
140137
{

0 commit comments

Comments
 (0)