11namespace MiniExcelLibs . Utils
22{
3+ using System ;
34 using System . Linq ;
45 using System . Text ;
56
@@ -15,36 +16,35 @@ public enum ImageFormat
1516 unknown
1617 }
1718
18- public static ImageFormat GetImageFormat ( byte [ ] bytes )
19+ public static ImageFormat GetImageFormat ( ReadOnlySpan < byte > bytes )
1920 {
20- // see http://www.mikekunz.com/image_file_header.html
21- var bmp = Encoding . ASCII . GetBytes ( "BM" ) ; // BMP
22- var gif = Encoding . ASCII . GetBytes ( "GIF" ) ; // GIF
23- var png = new byte [ ] { 137 , 80 , 78 , 71 } ; // PNG
24- var tiff = new byte [ ] { 73 , 73 , 42 } ; // TIFF
25- var tiff2 = new byte [ ] { 77 , 77 , 42 } ; // TIFF
26- var jpeg = new byte [ ] { 255 , 216 , 255 , 224 } ; // jpeg
27- var jpeg2 = new byte [ ] { 255 , 216 , 255 , 225 } ; // jpeg canon
28-
29- if ( bmp . SequenceEqual ( bytes . Take ( bmp . Length ) ) )
21+ ReadOnlySpan < byte > bmp = stackalloc byte [ ] { ( byte ) 'B' , ( byte ) 'M' } ; // BMP
22+ ReadOnlySpan < byte > gif = stackalloc byte [ ] { ( byte ) 'G' , ( byte ) 'I' , ( byte ) 'F' } ; // GIF
23+ ReadOnlySpan < byte > png = stackalloc byte [ ] { 137 , 80 , 78 , 71 } ; // PNG
24+ ReadOnlySpan < byte > tiff = stackalloc byte [ ] { 73 , 73 , 42 } ; // TIFF
25+ ReadOnlySpan < byte > tiff2 = stackalloc byte [ ] { 77 , 77 , 42 } ; // TIFF
26+ ReadOnlySpan < byte > jpeg = stackalloc byte [ ] { 255 , 216 , 255 , 224 } ; // jpeg
27+ ReadOnlySpan < byte > jpeg2 = stackalloc byte [ ] { 255 , 216 , 255 , 225 } ; // jpeg canon
28+
29+ if ( bytes . StartsWith ( bmp ) )
3030 return ImageFormat . bmp ;
3131
32- if ( gif . SequenceEqual ( bytes . Take ( gif . Length ) ) )
32+ if ( bytes . StartsWith ( gif ) )
3333 return ImageFormat . gif ;
3434
35- if ( png . SequenceEqual ( bytes . Take ( png . Length ) ) )
35+ if ( bytes . StartsWith ( png ) )
3636 return ImageFormat . png ;
3737
38- if ( tiff . SequenceEqual ( bytes . Take ( tiff . Length ) ) )
38+ if ( bytes . StartsWith ( tiff ) )
3939 return ImageFormat . tiff ;
4040
41- if ( tiff2 . SequenceEqual ( bytes . Take ( tiff2 . Length ) ) )
41+ if ( bytes . StartsWith ( tiff2 ) )
4242 return ImageFormat . tiff ;
4343
44- if ( jpeg . SequenceEqual ( bytes . Take ( jpeg . Length ) ) )
44+ if ( bytes . StartsWith ( jpeg ) )
4545 return ImageFormat . jpg ;
4646
47- if ( jpeg2 . SequenceEqual ( bytes . Take ( jpeg2 . Length ) ) )
47+ if ( bytes . StartsWith ( jpeg2 ) )
4848 return ImageFormat . jpg ;
4949
5050 return ImageFormat . unknown ;
0 commit comments