Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting actual image resolution from JFIF/EXIF (JPEG) header?

I've created a C++ structure for reading and verifying header of JFIF file-format. The structure is as follows...

#pragma pack(1)
typedef struct _JFIF_Header
{
    WORD SOIMarker;         // start of image marker
    WORD APP0_Marker;       // Application use marker
    WORD APP0_Length;       // Length of APP0 field
    BYTE Identifier[5];     // zero terminating string "JFIF",X'4A', X'46', X'49', X'46', X'00'
    BYTE Version[2];        // X'01',X'02'
    BYTE Units;             // units = 0:no units ;; units = 1:X and Y are dots per inch ;; units = 2:X and Y are dots per cm
    WORD XDensity;          // horizontal pixel density
    WORD YDensity;          // vertical pixel density
    BYTE XThumbnail;        // Thumbnail horizontal pixel density
    BYTE YThumbnail;        // Thumbnail vertical pixel density
}JFIFHeader;

Reading from raw disk clusters and copying these bytes in JFIFHeader Structure has done, and everything works fine. Now, the problem is, values saved in XDensity and YDensity variables are very high (X:24576,Y:24576 I read an image in Hex-Editor). This value cannot be the actual width and height of the image. The value of Units is 0x01 , i.e. unit is in "pixels per inch".

How can I get the actual image resolution 1024x826 from these value ? If this is not the right method, please give me any other alternative.

like image 503
Hemendra Sharma Avatar asked Feb 17 '23 19:02

Hemendra Sharma


1 Answers

You can locate JPEG's SOF0 marker (0xC0) and it is followed by 16-bit length, and then bits per pixel (8 bit), height (16 bit), width (16-bit) and component count (8 bit). All values are network byte order.

enter image description here

Image is 0x1C2 by 0x1C2 pixels (450x450).

like image 159
Roman R. Avatar answered Feb 20 '23 11:02

Roman R.