Click here to Skip to main content
13,800,882 members
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 12 May 2006

Image Classification Using File Header Information

, 12 May 2006
Rate this:
Please Sign up or sign in to vote.
How to do image classification using file header information.

Sample Image


This is a program in ANSII C that reads through image file headers and classify the images based on their type.


I had a lot of images of different types with lost file extensions and no other way of classifying them. So I wrote this small program in ANSII C to classify the images by reading their header information. The code runs on several platforms like Windows and Linux.


The program takes multiple file-names (with file path) as command line arguments. It processes each file one by one.

int _tmain(int argc, _TCHAR* argv[])
    int File; // file descriptor
    char pbData[10]; // buffer to reader file headers
    int nImageType = 0;

    if (argc > 1)
        // start from 1 because 0 index point to exe it self
        for(int nNumberOfFileProcessed=1; 
            nNumberOfFileProcessed < argc; 
            File = _open(argv[nNumberOfFileProcessed],_O_BINARY);
            // read 10 bytes
            _read(File, pbData,10);

The first 10 bytes of each file are read and compared with the predefined header information. The results are classified for the following image types:

TIFFINTEL:    "Tiff image for Intel processor"
TIFFMOTOROLA:    "Tiff image for Motorola processor"
GIF87a:        "GIF87a Image"
GIF89a:        "GIF89a Image"
PNG:        "PNG Image"
JPEGJFIF:    "JPEG JFIF compliant image"
JPEGEXIF:    "JPEG EXIF compliant image"
JPEGAPP2:    "JPEG with APP2 marker"
JPEGAPP3:    "JPEG with APP3 marker"
JPEGAPP4:    "JPEG with APP4 marker"
JPEGAPP5:    "JPEG with APP5 marker"
JPEGAPP6:    "JPEG with APP6 marker"
JPEGAPP7:    "JPEG with APP7 marker"
JPEGAPP8:    "JPEG with APP8 marker"
JPEGAPP9:    "JPEG with APP9 marker"
JPEGAPPA:    "JPEG with APPA marker"
JPEGAPPB:    "JPEG with APPB marker"
JPEGAPPC:    "JPEG with APPC marker"
JPEGAPPD:    "JPEG with APPD marker"
JPEGAPPE:    "JPEG with APPE marker"
JPEGAPPF:    "JPEG with APPF marker"
BITMAP:        "Bitmap file"


The code may be customized and reproduced according to your requirements. All you need to do is use the core function in your own program.

int DisplayImageType(char *pData)
    // compare file headers to determine the file type
    if (!memcmp(pData, szTiffHeaderForIntel,3))
        return TIFFINTEL;
    else if (!memcmp(pData, szTiffHeaderForMotorola,3))
        return TIFFMOTOROLA;
    else if (!memcmp(pData, szPNGHeader,8))
        return PNG;
    else if (!memcmp(pData, szGIF87aHeader,6))
        return GIF87a;
    else if (!memcmp(pData, szGIF89aHeader,6))
        return GIF89a;
    else if (!memcmp(pData, szBMPHeader,2))
        // 7 to ten byte must be zero
        // 3 to 6 is size of image
        char szNull[] = "\x0\x0\x0\x0";
        if (!memcmp(pData+6, szNull,4))
            return BITMAP;
    else if (!memcmp(pData, szJPEGCommonHeader,3))
        switch (((long)*(pData+3)) & 0xFF)
        case 0xE0:
            return JPEGJFIF;
        case 0xE1:
            return JPEGEXIF;
        case 0xE2:
            return JPEGAPP2;
        case 0xE3:
            return JPEGAPP3;
        case 0xE4:
            return JPEGAPP4;
        case 0xE5:
            return JPEGAPP5;
        case 0xE6:
            return JPEGAPP6;
        case 0xE7:
            return JPEGAPP7;
        case 0xE8:
            return JPEGAPP8;
        case 0xE9:
            return JPEGAPP9;
        case 0xEA:
            return JPEGAPPA;
        case 0xEB:
            return JPEGAPPB;
        case 0xEC:
            return JPEGAPPC;
        case 0xED:
            return JPEGAPPD;
        case 0xEE:
            return JPEGAPPE;
        case 0xEF:
            return JPEGAPPF;

You will also need to define the header files.

char szTiffHeaderForMotorola[]= "MM*";
Char szTiffHeaderForIntel[]    = "II*";
Char szPNGHeader[]        = "\x89PNG\r\n\x1a\n";
char szGIF87aHeader[]    = "GIF87a";
char szGIF89aHeader[]    = "GIF89a";
// this part of the header 
char szJPEGCommonHeader[]    = "\xFF\xD8\xFF";
// for future use
char szJPEGCommonEOI[]    = "\xFF\xD9";
// followinf 4 bytes will be size and the 4 will be
char szBMPHeader[]        = "\x42\x4D";


For now, the code works for the above mentioned image types only, but hopefully, I will be updating and expanding the types of images it can handle. The future versions will also include further details about the images, such as image resolution, size, etc.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Ali Rizvi
Product Manager Sivia Technologies
Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Member 874252317-Oct-12 1:42
memberMember 874252317-Oct-12 1:42 
GeneralImage Segmentation Pin
karthicinbaraj15-May-08 0:05
memberkarthicinbaraj15-May-08 0:05 
Question[Message Deleted] Pin
karthicinbaraj28-Apr-08 23:26
memberkarthicinbaraj28-Apr-08 23:26 
AnswerRe: Image Clasiification Pin
Ali Rizvi29-Apr-08 0:06
memberAli Rizvi29-Apr-08 0:06 
General[Message Deleted] Pin
karthicinbaraj29-Apr-08 0:48
memberkarthicinbaraj29-Apr-08 0:48 
GeneralRe: Image Clasiification Pin
Ali Rizvi29-Apr-08 2:08
memberAli Rizvi29-Apr-08 2:08 
GeneralIs there anyway I could get this in .NET code Pin
billeyhollidy1-Feb-07 12:53
memberbilleyhollidy1-Feb-07 12:53 
GeneralNice idea Pin
.rich.w15-May-06 11:21 11:21 
GeneralUseful Pin
gxdata15-May-06 6:08
membergxdata15-May-06 6:08 
GeneralBugs Pin
Jonathan [Darka]15-May-06 4:17
mentorJonathan [Darka]15-May-06 4:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.181215.1 | Last Updated 12 May 2006
Article Copyright 2006 by Ali Rizvi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid