关闭

c++判断图片格式

标签: c++
379人阅读 评论(0) 收藏 举报
分类:
<strong>转自:http://blog.csdn.net/zxlstudio/article/details/10362187</strong>
#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
 #include <errno.h>

 #define GETBYTELEN   2
 #define IMAGE_YES    1
 #define IMAGE_NO     0

 /*---JPEG/JPG(1)---*/
 #define IMAGE_JPEG_TYPE  0x01
 #define IMAGE_JPEG_JPG "JPEG/JPG"
 #define JPEG_FIRSTBIT  "FF"
 #define JPEG_SECONDBIT "D8"

 /*---BMP(2)---*/
 #define IMAGE_BMP_TYPE  0x02
 #define IMAGE_BMP     "BMP"
 #define BMP_FIRSTBIT  "42"
 #define BMP_SECONDBIT "4D"
 /*---PNG(3)---*/
 #define IMAGE_PNG_TYPE  0x04
 #define IMAGE_PNG     "PNG"
 #define PNG_FIRSTBIT  "89"
 #define PNG_SECONDBIT "50"

 /*---GIF(4)---*/
 #define IMAGE_GIF_TYPE 0x08
 #define IMAGE_GIF     "GIF"
 #define GIF_FIRSTBIT  "47"
 #define GIF_SECONDBIT "49"
 #define GIF_THIRDBIT  "46"
 #define GIF_FIFTHBIT1 "39"   // 可能存在是37
 #define GIF_FIFTHBIT2 "37"
 /*---TIFF(5)---*/
 #define IMAGE_TIFF_TYPE  0x10
 #define IMAGE_TIFF      "TIFF"
 #define TIFF_FIRSTBIT   "4D"
 #define TIFF_SECONDBIT  "4D"
 #define TIFF_FIRSTBIT2  "49"  // 或者
  #define TIFF_SECONDBIT2 "49"

 /*---ICO(6)---*/
 #define IMAGE_ICO    "ICO"
 #define ICO_THIRDBIT "1"  // 01
 #define ICO_FIFTHBIT "1"  // 01
 /*---TGA(7)---*/
 #define IMAGE_TGA    "TGA"
 #define TGA_THIRDBIT "2"  // 02
 #define TGA_FIFTHBIT "0"  // 00
 /*---CUR(8)---*/
 #define IMAGE_CUR    "CUR"
 #define CUR_THIRDBIT "2"  // 02
 #define CUR_FIFTHBIT "1"  // 01
 /*---PCX(9)---*/
 #define IMAGE_PCX    "PCX"
 #define PCX_FIRSTBIT "A"  // 0A
 /*---IFF(10)---*/
 #define IMAGE_IFF     "IFF"
 #define IFF_FIRSTBIT  "46"
 #define IFF_SECONDBIT "4F"
 #define IFF_THIRDBIT  "52"
 #define IFF_FORTHBIT  "4D"

 /*---ANI(11)---*/
 #define IMAGE_ANI     "ANI"
 #define ANI_FIRSTBIT  "52"
 #define ANI_SECONDBIT "49"
 #define ANI_THIRDBIT  "46"
 #define ANI_FORTHBIT  "46"
 /* 根据图片文件头部数据判断*/
int Check_Image_Header(void *FileData)
 {
 unsigned char *imagebuf = (unsigned char *)FileData;
 // unsigned char firstBit[3];
 // unsigned char secondBit[3];
 // unsigned char thirdBit[3];
 // unsigned char forthBit[3];
 // unsigned char fifthBit[3];
 unsigned char checkBit[5][3];

 if(NULL == imagebuf){
 return IMAGE_NO;
 }
 snprintf(checkBit[0], sizeof(checkBit[0]), "%X", imagebuf[0]);
 snprintf(checkBit[1], sizeof(checkBit[1]), "%X", imagebuf[1]);
 snprintf(checkBit[2], sizeof(checkBit[2]), "%X", imagebuf[2]);
 snprintf(checkBit[3], sizeof(checkBit[3]), "%X", imagebuf[3]);
 snprintf(checkBit[4], sizeof(checkBit[4]), "%X", imagebuf[4]);
 
 // snprintf(firstBit,  sizeof(firstBit),  "%X", imagebuf[0]);
 // snprintf(secondBit, sizeof(secondBit), "%X", imagebuf[1]);
 // snprintf(thirdBit,  sizeof(thirdBit),  "%X", imagebuf[2]);
 // snprintf(forthBit,  sizeof(forthBit),  "%X", imagebuf[3]);
 // snprintf(fifthBit,  sizeof(fifthBit),  "%X", imagebuf[4]);

 if( (0 == strncmp(checkBit[0], JPEG_FIRSTBIT,  GETBYTELEN)) && 
 (0 == strncmp(checkBit[1], JPEG_SECONDBIT, GETBYTELEN))) {
 return IMAGE_YES;
 }
 if( (0 == strncmp(checkBit[0], BMP_FIRSTBIT,  GETBYTELEN)) && 
 (0 == strncmp(checkBit[1], BMP_SECONDBIT, GETBYTELEN))) {
 return IMAGE_YES;
 }
 if( (0 == strncmp(checkBit[0], PNG_FIRSTBIT,   GETBYTELEN)) && 
 (0 == strncmp(checkBit[1], PNG_SECONDBIT, GETBYTELEN))) {
 return IMAGE_YES;
 }
 if( (0 == strncmp(checkBit[0], GIF_FIRSTBIT,   GETBYTELEN))   &&
 (0 == strncmp(checkBit[1], GIF_SECONDBIT, GETBYTELEN))   &&
 (0 == strncmp(checkBit[2], GIF_THIRDBIT,   GETBYTELEN))   &&
 ((0 == strncmp(checkBit[4], GIF_FIFTHBIT1, GETBYTELEN))   || 
 (0 == strncmp(checkBit[4], GIF_FIFTHBIT2, GETBYTELEN)))) {
 return IMAGE_YES;
 }
 if( ((0 == strncmp(checkBit[0], TIFF_FIRSTBIT,    GETBYTELEN))  && 
 (0 == strncmp(checkBit[1], TIFF_SECONDBIT,  GETBYTELEN))) ||
    ((0 == strncmp(checkBit[0], TIFF_FIRSTBIT2,   GETBYTELEN))  &&
     (0 == strncmp(checkBit[1], TIFF_SECONDBIT2, GETBYTELEN)))) {
 return IMAGE_YES;
 }
 if( (0 == strncmp(checkBit[2], ICO_THIRDBIT, GETBYTELEN)) && 
 (0 == strncmp(checkBit[4], ICO_FIFTHBIT, GETBYTELEN))){
 return IMAGE_YES;
 }


 if( (0 == strncmp(checkBit[2], TGA_THIRDBIT, GETBYTELEN)) && 
 (0 == strncmp(checkBit[4], TGA_FIFTHBIT, GETBYTELEN))){
 return IMAGE_YES;
 }
 if( (0 == strncmp(checkBit[2], CUR_THIRDBIT, GETBYTELEN)) && 
 (0 == strncmp(checkBit[4], CUR_FIFTHBIT, GETBYTELEN))){
 return IMAGE_YES;
 }
 if(0 == strncmp(checkBit[0], PCX_FIRSTBIT, GETBYTELEN)){
 return IMAGE_YES;
 }
 if( (0 == strncmp(checkBit[0], IFF_FIRSTBIT,  GETBYTELEN))   &&
 (0 == strncmp(checkBit[1], IFF_SECONDBIT, GETBYTELEN))   &&
 (0 == strncmp(checkBit[2], IFF_THIRDBIT,   GETBYTELEN))   &&
 (0 == strncmp(checkBit[3], IFF_FORTHBIT,   GETBYTELEN))) {
 return IMAGE_YES;
 }
 if( (0 == strncmp(checkBit[0], ANI_FIRSTBIT,   GETBYTELEN))   &&
 (0 == strncmp(checkBit[1], ANI_SECONDBIT, GETBYTELEN))   &&
 (0 == strncmp(checkBit[2], ANI_THIRDBIT,   GETBYTELEN))   &&
 (0 == strncmp(checkBit[3], ANI_FORTHBIT,   GETBYTELEN))) {
 return IMAGE_YES;
 }
 return IMAGE_NO;
 }

 int main(int argc, char *argv[])
 {
 int res = -1;
 char image_data[10];
 FILE *fd = NULL;
 if(argc < 2){
 printf("usage: %s <imagefilename>\n", argv[0]);
 exit(1);
 }
 assert(argv[1] != NULL);
 if( (fd = fopen(argv[1], "rb")) == NULL){
 perror("fopen error\n");
 exit(1);
 }
 fread(image_data, sizeof(unsigned char), sizeof(image_data), fd);
 printf("Image_head = %d\n", Check_Image_Header(image_data));
 printf("name = %d\n", res);
 fclose(fd);
 return 0;
 }

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4330次
    • 积分:114
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    文章分类