@@ -126,7 +126,10 @@ protected override Image<TPixel> Decode<TPixel>(BufferedReadStream stream, Cance
126126 switch ( this . infoHeader . Compression )
127127 {
128128 case BmpCompression . RGB :
129- if ( this . infoHeader . BitsPerPixel == 32 )
129+
130+ ushort bitsPerPixel = this . infoHeader . BitsPerPixel ;
131+
132+ if ( bitsPerPixel == 32 )
130133 {
131134 if ( this . bmpMetadata . InfoHeaderType == BmpInfoHeaderType . WinVersion3 )
132135 {
@@ -137,26 +140,30 @@ protected override Image<TPixel> Decode<TPixel>(BufferedReadStream stream, Cance
137140 this . ReadRgb32Fast ( stream , pixels , this . infoHeader . Width , this . infoHeader . Height , inverted ) ;
138141 }
139142 }
140- else if ( this . infoHeader . BitsPerPixel == 24 )
143+ else if ( bitsPerPixel == 24 )
141144 {
142145 this . ReadRgb24 ( stream , pixels , this . infoHeader . Width , this . infoHeader . Height , inverted ) ;
143146 }
144- else if ( this . infoHeader . BitsPerPixel == 16 )
147+ else if ( bitsPerPixel == 16 )
145148 {
146149 this . ReadRgb16 ( stream , pixels , this . infoHeader . Width , this . infoHeader . Height , inverted ) ;
147150 }
148- else if ( this . infoHeader . BitsPerPixel <= 8 )
151+ else if ( bitsPerPixel is > 0 and <= 8 )
149152 {
150153 this . ReadRgbPalette (
151154 stream ,
152155 pixels ,
153156 palette ,
154157 this . infoHeader . Width ,
155158 this . infoHeader . Height ,
156- this . infoHeader . BitsPerPixel ,
159+ bitsPerPixel ,
157160 bytesPerColorMapEntry ,
158161 inverted ) ;
159162 }
163+ else
164+ {
165+ BmpThrowHelper . ThrowInvalidImageContentException ( $ "Invalid bits per pixel: { bitsPerPixel } ") ;
166+ }
160167
161168 break ;
162169
0 commit comments