c - How to get the width/height of jpeg file without using library? -


firstly want tried many times find answer using google search, , found many results did not understand, because don't know idea of reading binary file, , convert value obtained readable value.

what tried doing it.

unsigned char fbuff[16]; file *file; file = fopen("c:\\loser.jpg", "rb"); if(file != null){    fseek(file, 0, seek_set);    fread(fbuff, 1, 16, file);    printf("%d\n", fbuff[1]);    fclose(file); }else{    printf("file not exists."); } 

i want simple explanation example shows, how width/height of jpeg file header, , convert value readable value.

unfortunately, doesn't seem simple jpeg. should @ source jhead command line tool. provides information. when going through source, see function readjpegsections. function scans through segments contained within jpeg file extract desired information. image width , height obtained when processing frames have sofn marker.

i see source in public domain, i'll show snippet gets image info:

static int get16m(const void * short) {     return (((uchar *)short)[0] << 8) | ((uchar *)short)[1]; }  static void process_sofn (const uchar * data, int marker) {     int data_precision, num_components;      data_precision = data[2];     imageinfo.height = get16m(data+3);     imageinfo.width = get16m(data+5); 

from source code, clear me there no single "header" information. have scan through jpeg file, parsing each segment, until find segment information in want. described in wikipedia article:

a jpeg image consists of sequence of segments, each beginning marker, each of begins 0xff byte followed byte indicating kind of marker is. markers consist of 2 bytes; others followed 2 bytes indicating length of marker-specific payload data follows.


a jpeg file consists of sequence of segments:

segment_0 segment_1 segment_2 ... 

each segment begins 2-byte marker. first byte 0xff, second byte determines type of segment. followed encoding of length of segment. within segment data specific segment type.

the image width , height found in segment of type sofn, or "start of frame [n]", "n" number means special jpeg decoder. should enough sof0, , byte designation 0xc0. once find frame, can decode find image height , width.

so structure of program want like:

file_data = data in file data = &file_data[0] while (data not @ end of file_data)     segment_type = decoded jpeg segment type @ data     if (type != sof0)         data += byte length segment_type         continue     else         image height , width segment         return 

this structure found in michael petrov's get_jpeg_size() implementation.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -