@@ -131,30 +131,35 @@ protected override Image<TPixel> Decode<TPixel>(BufferedReadStream stream, Cance
131131 try
132132 {
133133 int bytesPerColorMapEntry = this . ReadImageHeaders ( stream , out bool inverted , out byte [ ] palette ) ;
134+ ushort bitsPerPixel = this . infoHeader . BitsPerPixel ;
134135
135136 image = new Image < TPixel > ( this . configuration , this . infoHeader . Width , this . infoHeader . Height , this . metadata ) ;
136137
137138 Buffer2D < TPixel > pixels = image . GetRootFramePixelBuffer ( ) ;
138139
139140 switch ( this . infoHeader . Compression )
140141 {
141- case BmpCompression . RGB when this . infoHeader . BitsPerPixel is 32 && this . bmpMetadata . InfoHeaderType is BmpInfoHeaderType . WinVersion3 :
142+ case BmpCompression . RGB when bitsPerPixel is 32 && this . bmpMetadata . InfoHeaderType is BmpInfoHeaderType . WinVersion3 :
142143 this . ReadRgb32Slow ( stream , pixels , this . infoHeader . Width , this . infoHeader . Height , inverted ) ;
143144
144145 break ;
145- case BmpCompression . RGB when this . infoHeader . BitsPerPixel is 32 :
146+
147+ case BmpCompression . RGB when bitsPerPixel is 32 :
146148 this . ReadRgb32Fast ( stream , pixels , this . infoHeader . Width , this . infoHeader . Height , inverted ) ;
147149
148150 break ;
149- case BmpCompression . RGB when this . infoHeader . BitsPerPixel is 24 :
151+
152+ case BmpCompression . RGB when bitsPerPixel is 24 :
150153 this . ReadRgb24 ( stream , pixels , this . infoHeader . Width , this . infoHeader . Height , inverted ) ;
151154
152155 break ;
153- case BmpCompression . RGB when this . infoHeader . BitsPerPixel is 16 :
156+
157+ case BmpCompression . RGB when bitsPerPixel is 16 :
154158 this . ReadRgb16 ( stream , pixels , this . infoHeader . Width , this . infoHeader . Height , inverted ) ;
155159
156160 break ;
157- case BmpCompression . RGB when this . infoHeader . BitsPerPixel is <= 8 && this . processedAlphaMask :
161+
162+ case BmpCompression . RGB when bitsPerPixel is > 0 and <= 8 && this . processedAlphaMask :
158163 this . ReadRgbPaletteWithAlphaMask (
159164 stream ,
160165 pixels ,
@@ -166,7 +171,8 @@ protected override Image<TPixel> Decode<TPixel>(BufferedReadStream stream, Cance
166171 inverted ) ;
167172
168173 break ;
169- case BmpCompression . RGB when this . infoHeader . BitsPerPixel is <= 8 :
174+
175+ case BmpCompression . RGB when bitsPerPixel is > 0 and <= 8 :
170176 this . ReadRgbPalette (
171177 stream ,
172178 pixels ,
@@ -179,6 +185,10 @@ protected override Image<TPixel> Decode<TPixel>(BufferedReadStream stream, Cance
179185
180186 break ;
181187
188+ case BmpCompression . RGB when bitsPerPixel is <= 0 or > 32 :
189+ BmpThrowHelper . ThrowInvalidImageContentException ( $ "Invalid bits per pixel: { bitsPerPixel } ") ;
190+ break ;
191+
182192 case BmpCompression . RLE24 :
183193 this . ReadRle24 ( stream , pixels , this . infoHeader . Width , this . infoHeader . Height , inverted ) ;
184194
0 commit comments