File tree Expand file tree Collapse file tree 2 files changed +8
-5
lines changed
src/ImageSharp/Formats/Tga Expand file tree Collapse file tree 2 files changed +8
-5
lines changed Original file line number Diff line number Diff line change @@ -937,7 +937,9 @@ private TgaImageOrigin ReadFileHeader(BufferedReadStream stream)
937
937
this . tgaMetadata = this . metadata . GetTgaMetadata ( ) ;
938
938
this . tgaMetadata . BitsPerPixel = ( TgaBitsPerPixel ) this . fileHeader . PixelDepth ;
939
939
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 ;
941
943
if ( alphaBits is not 0 and not 1 and not 8 )
942
944
{
943
945
TgaThrowHelper . ThrowInvalidImageContentException ( "Invalid alpha channel bits" ) ;
@@ -949,4 +951,8 @@ private TgaImageOrigin ReadFileHeader(BufferedReadStream stream)
949
951
// Bits 4 and 5 describe the image origin.
950
952
return ( TgaImageOrigin ) ( ( this . fileHeader . ImageDescriptor & 0x30 ) >> 4 ) ;
951
953
}
954
+
955
+ private bool IsTrueColor32BitPerPixel ( TgaBitsPerPixel bitsPerPixel ) => bitsPerPixel == TgaBitsPerPixel . Pixel32 &&
956
+ ( this . fileHeader . ImageType == TgaImageType . TrueColor ||
957
+ this . fileHeader . ImageType == TgaImageType . RleTrueColor ) ;
952
958
}
Original file line number Diff line number Diff line change @@ -131,10 +131,7 @@ public TgaFileHeader(
131
131
/// </summary>
132
132
public byte ImageDescriptor { get ; }
133
133
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 ] ;
138
135
139
136
public void WriteTo ( Span < byte > buffer )
140
137
{
You can’t perform that action at this time.
0 commit comments