各種格式圖片文件頭標識分析
圖片的格式很多,一個圖片文件的後綴名並不能說明這個圖片的真正格式什麼,那麼如何獲取圖片的格式呢?我想到了幾個簡單但有效的方法,那就是讀取圖片文件的文件頭標識。我們知道各種格式的圖片的文件頭標識識不同的,因此我們可以通過判斷文件頭的標識來識別圖片格式。
我對各種格式的圖片文件頭標識進行了分析,不僅查找資料,也用十六進制編輯器察看過圖片的文件頭,以下是我收集、分析的結果,供大家參考。
1.JPEG
- 文件頭標識 (2 bytes): $ff, $d8 (SOI) (JPEG 文件標識)
- 文件結束標識 (2 bytes): $ff, $d9 (EOI)
2.TGA
- 未壓縮的前5字節 00 00 02 00 00
- RLE壓縮的前5字節 00 00 10 00 00
3.PNG
- 文件頭標識 (8 bytes) 89 50 4E 47 0D 0A 1A 0A
4.GIF
- 文件頭標識 (6 bytes) 47 49 46 38 39(37) 61
G I F 8 9 (7) a
5.BMP
- 文件頭標識 (2 bytes) 42 4D
B M
6.PCX
- 文件頭標識 (1 bytes) 0A
7.TIFF
- 文件頭標識 (2 bytes) 4D 4D 或 49 49
8.ICO
- 文件頭標識 (8 bytes) 00 00 01 00 01 00 20 20
9.CUR
- 文件頭標識 (8 bytes) 00 00 02 00 01 00 20 20
10.IFF
- 文件頭標識 (4 bytes) 46 4F 52 4D
F O R M
11.ANI
- 文件頭標識 (4 bytes) 52 49 46 46
R I F F
根據這些文件頭標識的收集,我可以寫一個識別圖像格式的模塊了。但是在寫這個模塊之前可以對收集到的文件頭標識進行優化,使得程序中字符串比對次數儘量的少。
1.JPEG我們知需要比對文件頭的$ff, $d8這兩個字符,而不用讀取最後的兩個結束標識了。
2.TGA,ICO,CUR只需比對第三個與第五個字符即可。
3.PNG比對[89][50]這兩個字符。
4.GIF比對[47][49][46]與第五個字符。
到這裏,我想代碼是不難寫的,但是爲了方便大家我還是把代碼貼出來了,如果這代碼寫的不好,可以與我討論。您可採用下面的代碼,但請保留版權,謝謝!
模塊代碼如下:
'枚舉圖片格式種類
Public Enum ImageForm
[BMP] = 0
[JPEG] = 1
[GIF87] = 2
[GIF89] = 3
[PNG] = 4
[TGA Normal] = 5 'TGA未壓縮
[TGA RLE] = 6 'TGA經過RLE壓縮後的
[PCX] = 7
[TIFF] = 8
[ICO] = 9
[CUR] = 10
[IFF] = 11
[ANI] = 12
[Other] = 13
[FileError] = 14
End Enum
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
'-- 標題:獲取圖片的格式
'-- 作者:BEAR-BEN
'-- 製作日期:2007-8-5
'-- 支持的格式:BMP,JPEG,GIF,PNG,TGA,PCX,TIFF,
' ICO,CUR,IFF,ANI 共11種格式
'-- 版本:1.0
'-- 使用者請保留版權,謝謝!
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
Public Function GetImageFileForm(ImageFilePath As String) As ImageForm
Dim FileHeader(5) As Byte, FileNumber As Integer
GetImageFileForm = FileError
If Dir(ImageFilePath) <> "" Then '判斷圖片文件是否存在
FileNumber = FreeFile
Open ImageFilePath For Binary As #FileNumber
Get FileNumber, , FileHeader() '二進制流讀取圖片前5個字符
Close FileNumber
GetImageFileForm = Other
'文件頭標識識別
If (FileHeader(0) = 66) And (FileHeader(1) = 77) Then
GetImageFileForm = BMP
Exit Function
End If
If (FileHeader(0) = 255) And (FileHeader(1) = 216) Then
GetImageFileForm = JPEG
Exit Function
End If
If (FileHeader(0) = 71) And (FileHeader(1) = 73) And (FileHeader(2) = 70) And (FileHeader(4) = 57) Then
GetImageFileForm = GIF89
Exit Function
End If
If (FileHeader(0) = 71) And (FileHeader(1) = 73) And (FileHeader(2) = 70) And (FileHeader(4) = 55) Then
GetImageFileForm = GIF87
Exit Function
End If
If (FileHeader(0) = 137) And (FileHeader(1) = 80) Then
GetImageFileForm = PNG
Exit Function
End If
If (FileHeader(0) = 73) And (FileHeader(1) = 73) Then
GetImageFileForm = TIFF 'TIFF 摩托羅拉
Exit Function
End If
If (FileHeader(0) = 77) And (FileHeader(1) = 77) Then
GetImageFileForm = TIFF 'TIFF Intel
Exit Function
End If
If (FileHeader(2) = 1) And (FileHeader(4) = 1) Then
GetImageFileForm = ICO
Exit Function
End If
If (FileHeader(2) = 2) And (FileHeader(4) = 1) Then
GetImageFileForm = CUR
Exit Function
End If
If (FileHeader(0) = 82) And (FileHeader(1) = 73) And (FileHeader(2) = 70) And (FileHeader(3) = 70) Then
GetImageFileForm = ANI
Exit Function
End If
If (FileHeader(2) = 2) And (FileHeader(4) = 0) Then
GetImageFileForm = [TGA Normal]
Exit Function
End If
If (FileHeader(2) = 16) And (FileHeader(4) = 0) Then
GetImageFileForm = [TGA RLE]
Exit Function
End If
If (FileHeader(0) = 10) Then
GetImageFileForm = PCX
Exit Function
End If
End If
End Function
我對各種格式的圖片文件頭標識進行了分析,不僅查找資料,也用十六進制編輯器察看過圖片的文件頭,以下是我收集、分析的結果,供大家參考。
1.JPEG
- 文件頭標識 (2 bytes): $ff, $d8 (SOI) (JPEG 文件標識)
- 文件結束標識 (2 bytes): $ff, $d9 (EOI)
2.TGA
- 未壓縮的前5字節 00 00 02 00 00
- RLE壓縮的前5字節 00 00 10 00 00
3.PNG
- 文件頭標識 (8 bytes) 89 50 4E 47 0D 0A 1A 0A
4.GIF
- 文件頭標識 (6 bytes) 47 49 46 38 39(37) 61
G I F 8 9 (7) a
5.BMP
- 文件頭標識 (2 bytes) 42 4D
B M
6.PCX
- 文件頭標識 (1 bytes) 0A
7.TIFF
- 文件頭標識 (2 bytes) 4D 4D 或 49 49
8.ICO
- 文件頭標識 (8 bytes) 00 00 01 00 01 00 20 20
9.CUR
- 文件頭標識 (8 bytes) 00 00 02 00 01 00 20 20
10.IFF
- 文件頭標識 (4 bytes) 46 4F 52 4D
F O R M
11.ANI
- 文件頭標識 (4 bytes) 52 49 46 46
R I F F
根據這些文件頭標識的收集,我可以寫一個識別圖像格式的模塊了。但是在寫這個模塊之前可以對收集到的文件頭標識進行優化,使得程序中字符串比對次數儘量的少。
1.JPEG我們知需要比對文件頭的$ff, $d8這兩個字符,而不用讀取最後的兩個結束標識了。
2.TGA,ICO,CUR只需比對第三個與第五個字符即可。
3.PNG比對[89][50]這兩個字符。
4.GIF比對[47][49][46]與第五個字符。
到這裏,我想代碼是不難寫的,但是爲了方便大家我還是把代碼貼出來了,如果這代碼寫的不好,可以與我討論。您可採用下面的代碼,但請保留版權,謝謝!
模塊代碼如下:
'枚舉圖片格式種類
Public Enum ImageForm
[BMP] = 0
[JPEG] = 1
[GIF87] = 2
[GIF89] = 3
[PNG] = 4
[TGA Normal] = 5 'TGA未壓縮
[TGA RLE] = 6 'TGA經過RLE壓縮後的
[PCX] = 7
[TIFF] = 8
[ICO] = 9
[CUR] = 10
[IFF] = 11
[ANI] = 12
[Other] = 13
[FileError] = 14
End Enum
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
'-- 標題:獲取圖片的格式
'-- 作者:BEAR-BEN
'-- 製作日期:2007-8-5
'-- 支持的格式:BMP,JPEG,GIF,PNG,TGA,PCX,TIFF,
' ICO,CUR,IFF,ANI 共11種格式
'-- 版本:1.0
'-- 使用者請保留版權,謝謝!
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
Public Function GetImageFileForm(ImageFilePath As String) As ImageForm
Dim FileHeader(5) As Byte, FileNumber As Integer
GetImageFileForm = FileError
If Dir(ImageFilePath) <> "" Then '判斷圖片文件是否存在
FileNumber = FreeFile
Open ImageFilePath For Binary As #FileNumber
Get FileNumber, , FileHeader() '二進制流讀取圖片前5個字符
Close FileNumber
GetImageFileForm = Other
'文件頭標識識別
If (FileHeader(0) = 66) And (FileHeader(1) = 77) Then
GetImageFileForm = BMP
Exit Function
End If
If (FileHeader(0) = 255) And (FileHeader(1) = 216) Then
GetImageFileForm = JPEG
Exit Function
End If
If (FileHeader(0) = 71) And (FileHeader(1) = 73) And (FileHeader(2) = 70) And (FileHeader(4) = 57) Then
GetImageFileForm = GIF89
Exit Function
End If
If (FileHeader(0) = 71) And (FileHeader(1) = 73) And (FileHeader(2) = 70) And (FileHeader(4) = 55) Then
GetImageFileForm = GIF87
Exit Function
End If
If (FileHeader(0) = 137) And (FileHeader(1) = 80) Then
GetImageFileForm = PNG
Exit Function
End If
If (FileHeader(0) = 73) And (FileHeader(1) = 73) Then
GetImageFileForm = TIFF 'TIFF 摩托羅拉
Exit Function
End If
If (FileHeader(0) = 77) And (FileHeader(1) = 77) Then
GetImageFileForm = TIFF 'TIFF Intel
Exit Function
End If
If (FileHeader(2) = 1) And (FileHeader(4) = 1) Then
GetImageFileForm = ICO
Exit Function
End If
If (FileHeader(2) = 2) And (FileHeader(4) = 1) Then
GetImageFileForm = CUR
Exit Function
End If
If (FileHeader(0) = 82) And (FileHeader(1) = 73) And (FileHeader(2) = 70) And (FileHeader(3) = 70) Then
GetImageFileForm = ANI
Exit Function
End If
If (FileHeader(2) = 2) And (FileHeader(4) = 0) Then
GetImageFileForm = [TGA Normal]
Exit Function
End If
If (FileHeader(2) = 16) And (FileHeader(4) = 0) Then
GetImageFileForm = [TGA RLE]
Exit Function
End If
If (FileHeader(0) = 10) Then
GetImageFileForm = PCX
Exit Function
End If
End If
End Function
轉自: http://hi.baidu.com/xovqbgpcznbovxq/item/ac7f14ec8b9df4285a7cfb3f