Given image file with no extension, how can I read the image file and identify the file format in C++?
+6
A:
You can check the source of Linux file
command (git://git.debian.net/git/debian/file.git
). It does exactly the same thing; and not just for image files.
Mehrdad Afshari
2009-07-29 20:01:23
+1
A:
Old school question ;) You need to check so called 'magic numbers' on that file. In other words, almost each binary file type has some constant code at the start of the file. First, you need hex viewer for that: www.hhdsoftware.com/Products/home/hex-editor-free.html
And then search here: www.garykessler.net/library/file_sigs.html
Andrejs Cainikovs
2009-07-29 20:51:34
well, this is in case if you're not proud owner of linux machine, or 'file' will not work for you (i actually doubt so).
Andrejs Cainikovs
2009-07-29 20:58:57
+2
A:
By reading the first few bytes you can get a guess but would need to try fully parsing to be sure. Here is some code from one of my image loading object you can use for reference:
if(Open()==true)
{
unsigned char testread[5];
if(Read(&testread,(unsigned long)4)==4)
{
testread[4]=0;
if(!strcmp((char *)testread,"GIF8"))
{
Close();
LoadGIFImage(justsize);
}
else if(testread[0]==0xff && testread[1]==0xd8)
{
Close();
LoadJPGImage(justsize);
}
else if(testread[0]==0x89 && testread[1]==0x50 && testread[2]==0x4e && testread[3]==0x47)
{
Close();
LoadPNGImage(justsize);
}
else if(testread[0]==0x00 && testread[1]==0x00 && testread[2]==0x01 && testread[3]==0x00)
{
Close();
LoadWINICOImage(justsize);
}
else if(testread[0]==0x42 && testread[1]==0x4d)
{
Close();
LoadBMPImage(justsize);
KPexEA
2009-07-29 21:02:46