@@ -168,7 +168,9 @@ public Image<TPixel> Decode<TPixel>(Stream stream)
168168 break ;
169169
170170 default :
171- throw new NotSupportedException ( "Does not support this kind of bitmap files." ) ;
171+ BmpThrowHelper . ThrowNotSupportedException ( "Does not support this kind of bitmap files." ) ;
172+
173+ break ;
172174 }
173175
174176 return image ;
@@ -319,7 +321,7 @@ private void UncompressRle4(int w, Span<byte> buffer)
319321 {
320322 if ( this . stream . Read ( cmd , 0 , cmd . Length ) != 2 )
321323 {
322- throw new Exception ( "Failed to read 2 bytes from the stream" ) ;
324+ BmpThrowHelper . ThrowImageFormatException ( "Failed to read 2 bytes from the stream while uncompressing RLE4 bitmap. " ) ;
323325 }
324326
325327 if ( cmd [ 0 ] == RleCommand )
@@ -429,7 +431,7 @@ private void UncompressRle8(int w, Span<byte> buffer)
429431 {
430432 if ( this . stream . Read ( cmd , 0 , cmd . Length ) != 2 )
431433 {
432- throw new Exception ( "Failed to read 2 bytes from stream" ) ;
434+ BmpThrowHelper . ThrowImageFormatException ( "Failed to read 2 bytes from stream while uncompressing RLE8 bitmap. " ) ;
433435 }
434436
435437 if ( cmd [ 0 ] == RleCommand )
@@ -913,7 +915,7 @@ private void ReadInfoHeader()
913915 int headerSize = BinaryPrimitives . ReadInt32LittleEndian ( buffer ) ;
914916 if ( headerSize < BmpInfoHeader . CoreSize )
915917 {
916- throw new NotSupportedException ( $ "ImageSharp does not support this BMP file. HeaderSize: { headerSize } .") ;
918+ BmpThrowHelper . ThrowNotSupportedException ( $ "ImageSharp does not support this BMP file. HeaderSize is ' { headerSize } ' .") ;
917919 }
918920
919921 int skipAmount = 0 ;
@@ -926,23 +928,23 @@ private void ReadInfoHeader()
926928 // read the rest of the header
927929 this . stream . Read ( buffer , BmpInfoHeader . HeaderSizeSize , headerSize - BmpInfoHeader . HeaderSizeSize ) ;
928930
929- BmpInfoHeaderType inofHeaderType = BmpInfoHeaderType . WinVersion2 ;
931+ BmpInfoHeaderType infoHeaderType = BmpInfoHeaderType . WinVersion2 ;
930932 if ( headerSize == BmpInfoHeader . CoreSize )
931933 {
932934 // 12 bytes
933- inofHeaderType = BmpInfoHeaderType . WinVersion2 ;
935+ infoHeaderType = BmpInfoHeaderType . WinVersion2 ;
934936 this . infoHeader = BmpInfoHeader . ParseCore ( buffer ) ;
935937 }
936938 else if ( headerSize == BmpInfoHeader . Os22ShortSize )
937939 {
938940 // 16 bytes
939- inofHeaderType = BmpInfoHeaderType . Os2Version2Short ;
941+ infoHeaderType = BmpInfoHeaderType . Os2Version2Short ;
940942 this . infoHeader = BmpInfoHeader . ParseOs22Short ( buffer ) ;
941943 }
942944 else if ( headerSize == BmpInfoHeader . SizeV3 )
943945 {
944946 // == 40 bytes
945- inofHeaderType = BmpInfoHeaderType . WinVersion3 ;
947+ infoHeaderType = BmpInfoHeaderType . WinVersion3 ;
946948 this . infoHeader = BmpInfoHeader . ParseV3 ( buffer ) ;
947949
948950 // if the info header is BMP version 3 and the compression type is BITFIELDS,
@@ -960,24 +962,30 @@ private void ReadInfoHeader()
960962 else if ( headerSize == BmpInfoHeader . AdobeV3Size )
961963 {
962964 // == 52 bytes
963- inofHeaderType = BmpInfoHeaderType . AdobeVersion3 ;
965+ infoHeaderType = BmpInfoHeaderType . AdobeVersion3 ;
964966 this . infoHeader = BmpInfoHeader . ParseAdobeV3 ( buffer , withAlpha : false ) ;
965967 }
966968 else if ( headerSize == BmpInfoHeader . AdobeV3WithAlphaSize )
967969 {
968970 // == 56 bytes
969- inofHeaderType = BmpInfoHeaderType . AdobeVersion3WithAlpha ;
971+ infoHeaderType = BmpInfoHeaderType . AdobeVersion3WithAlpha ;
970972 this . infoHeader = BmpInfoHeader . ParseAdobeV3 ( buffer , withAlpha : true ) ;
971973 }
974+ else if ( headerSize == BmpInfoHeader . Os2v2Size )
975+ {
976+ // == 64 bytes
977+ infoHeaderType = BmpInfoHeaderType . Os2Version2 ;
978+ this . infoHeader = BmpInfoHeader . ParseOs2Version2 ( buffer ) ;
979+ }
972980 else if ( headerSize >= BmpInfoHeader . SizeV4 )
973981 {
974982 // >= 108 bytes
975- inofHeaderType = headerSize == BmpInfoHeader . SizeV4 ? BmpInfoHeaderType . WinVersion4 : BmpInfoHeaderType . WinVersion5 ;
983+ infoHeaderType = headerSize == BmpInfoHeader . SizeV4 ? BmpInfoHeaderType . WinVersion4 : BmpInfoHeaderType . WinVersion5 ;
976984 this . infoHeader = BmpInfoHeader . ParseV4 ( buffer ) ;
977985 }
978986 else
979987 {
980- throw new NotSupportedException ( $ "ImageSharp does not support this BMP file. HeaderSize: { headerSize } .") ;
988+ BmpThrowHelper . ThrowNotSupportedException ( $ "ImageSharp does not support this BMP file. HeaderSize ' { headerSize } ' .") ;
981989 }
982990
983991 // Resolution is stored in PPM.
@@ -1001,7 +1009,7 @@ private void ReadInfoHeader()
10011009
10021010 short bitsPerPixel = this . infoHeader . BitsPerPixel ;
10031011 this . bmpMetaData = this . metaData . GetFormatMetaData ( BmpFormat . Instance ) ;
1004- this . bmpMetaData . InfoHeaderType = inofHeaderType ;
1012+ this . bmpMetaData . InfoHeaderType = infoHeaderType ;
10051013
10061014 // We can only encode at these bit rates so far.
10071015 if ( bitsPerPixel . Equals ( ( short ) BmpBitsPerPixel . Pixel24 )
@@ -1027,6 +1035,11 @@ private void ReadFileHeader()
10271035 this . stream . Read ( buffer , 0 , BmpFileHeader . Size ) ;
10281036
10291037 this . fileHeader = BmpFileHeader . Parse ( buffer ) ;
1038+
1039+ if ( this . fileHeader . Type != BmpConstants . TypeMarkers . Bitmap )
1040+ {
1041+ BmpThrowHelper . ThrowNotSupportedException ( $ "ImageSharp does not support this BMP file. File header bitmap type marker '{ this . fileHeader . Type } '.") ;
1042+ }
10301043 }
10311044
10321045 /// <summary>
@@ -1080,7 +1093,7 @@ private int ReadImageHeaders(Stream stream, out bool inverted, out byte[] palett
10801093 // 256 * 4
10811094 if ( colorMapSize > 1024 )
10821095 {
1083- throw new ImageFormatException ( $ "Invalid bmp colormap size '{ colorMapSize } '") ;
1096+ BmpThrowHelper . ThrowImageFormatException ( $ "Invalid bmp colormap size '{ colorMapSize } '") ;
10841097 }
10851098
10861099 palette = new byte [ colorMapSize ] ;
0 commit comments