Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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

Background

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.

Input

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; 
            nNumberOfFileProcessed++)
        {
            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"

Customization

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;
            break;
        case 0xE1:
            return JPEGEXIF;
            break;
        case 0xE2:
            return JPEGAPP2;
            break;
        case 0xE3:
            return JPEGAPP3;
            break;
        case 0xE4:
            return JPEGAPP4;
            break;
        case 0xE5:
            return JPEGAPP5;
            break;
        case 0xE6:
            return JPEGAPP6;
            break;
        case 0xE7:
            return JPEGAPP7;
            break;
        case 0xE8:
            return JPEGAPP8;
            break;
        case 0xE9:
            return JPEGAPP9;
            break;
        case 0xEA:
            return JPEGAPPA;
            break;
        case 0xEB:
            return JPEGAPPB;
            break;
        case 0xEC:
            return JPEGAPPC;
            break;
        case 0xED:
            return JPEGAPPD;
            break;
        case 0xEE:
            return JPEGAPPE;
            break;
        case 0xEF:
            return JPEGAPPF;
            break;
        default:
            break;
        }
    }
}

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";

Limitations

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.

License

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

Share

About the Author

Ali Rizvi
Product Manager Sivia Technologies
Germany Germany
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMember 874252317-Oct-12 0:42 
GeneralImage Segmentation Pinmemberkarthicinbaraj14-May-08 23:05 
Question[Message Deleted] Pinmemberkarthicinbaraj28-Apr-08 22:26 
AnswerRe: Image Clasiification PinmemberAli Rizvi28-Apr-08 23:06 
General[Message Deleted] Pinmemberkarthicinbaraj28-Apr-08 23:48 
GeneralRe: Image Clasiification PinmemberAli Rizvi29-Apr-08 1:08 
GeneralIs there anyway I could get this in .NET code Pinmemberbilleyhollidy1-Feb-07 11:53 
First off this article is COOLBig Grin | :-D as it is one of the VERY FEW that enables this functionality,, kudos to you.
 
I have been trying to convert this for 3 Days without success. Can someone help me to do so or alternatively could a DL be available to reference.
 
This would be very much appreciated.
 
Thank you Smile | :) Big Grin | :-D
GeneralNice idea Pinmember.rich.w15-May-06 10:21 
GeneralUseful Pinmembergxdata15-May-06 5:08 
GeneralBugs PinmemberDarka15-May-06 3:17 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 12 May 2006
Article Copyright 2006 by Ali Rizvi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid