Turn from: http://www.jb51.net/article/41560.htm
I want to do all kinds of thinking. Today, I finally got the picture judged.
Here, I write my thoughts. Want to be helpful to friends who don't want to read the code.
The picture format of the constant wind has: Bmp,png,jpg,gif and so on picture format.
The method I use is to read the identifier in the header file of the picture:
Copy code code as follows:
unsigned short bmp=0x4d42,
JPG=0XD8FF,
PNG[4]={0X5089,0X474E,0X0A0D,0X0A1A},
GIF[3]={0X4947,0X3846,0X6139};
You will find that the order of n bytes is read at a time, in a different order than the "Okhit" open.
You don't even care about that.
With this judging basis, the next step is to judge. This can not be used in Chinese, please see the original code.
The IF else structure is used.
Maybe, when you compile, you'll find that you can't open the file. Note: The file name that you open
Copy code code as follows:
int main ()
{
FILE *fis;
Short int i=0;
unsigned short pis[5];
if ((Fis=fopen ("1.bmp", "R")) ==null)
printf ("Can not read%s\n", "1.bmp");
fread (Pis,8,1,fis);
for (i=0;i<4;++i)
printf ("%0x..", Pis[i]);
printf ("\ n");
if (pis[0]==bmp)
printf ("It is a bmp\n");
else if (pis[0]==jpg)
printf ("It is a jpg\n");
else if (png[0]==pis[0]&&png[1]==pis[1]&&png[2]==pis[2]&&png[3]==pis[3])
printf ("It is a png\n");
else if (gif[0]==pis[0]&&gif[1]==pis[1]&&gif[2]==pis[2])
printf ("It is a gif\n");
return 0;
}
For convenience of invocation, a function is specially made:
Copy code code as follows:
int Istupian (char entry[])
/* return value is
Bmp
jpg
png
gif
*/
{
FILE *fis;
short int i=0;
unsigned short pis[5];
int flag=0;//initialization Every time the call is made
if ((Fis=fopen (entry, "R")) ==null)
printf ("Can not read%s\n", entry);
Fread (Pis,8,1,fis);
if (pis[0]==bmp)
{
flag=1;
printf ("It is a bmp\n");
}
else if (pis[0]==jpg)
{
flag=2;
printf ("It is a jpg\n");
}
else if (png[0]==pis[0]&&png[1]==pis[1]&&png[2]==pis[2]&&png[3]==pis[3])
{
flag=3;
printf ("It is a png\n");
}
else if (gif[0]==pis[0]&&gif[1]==pis[1]&&gif[2]==pis[2])
{
flag=4;
printf ("It is a gif\n");
}
return flag;
}