File tree Expand file tree Collapse file tree
src/ImageSharp/Formats/Tga Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments