Understanding And Getting info of Bitmap in C -


i having hard time understanding , parsing info data present in bitmap image. better understand read following tutorial, raster data.

now, code present there follows, (greyscale 8bit color value)

#include <stdio.h> #include <stdlib.h> #include <math.h>  /*-------structures---------*/ typedef struct {int rows; int cols; unsigned char* data;} simage;  /*-------prototypes---------*/ long getimageinfo(file*, long, int);  int main(int argc, char* argv[]) {   file          *bmpinput, *rasteroutput;   simage        originalimage;   unsigned char     somechar;   unsigned char*    pchar;   int           ncolors;  /* bmp number of colors */   long          filesize; /* bmp file size */   int           vectorsize; /* bmp vector size */   int           r, c;       /* r = rows, c = cols */     /* initialize pointer */   somechar = '0';   pchar = &somechar;    if(argc < 2)   {     printf("usage: %s bmpinput.bmp\n", argv[0]);     //end execution     exit(0);   }   printf("reading filename %s\n", argv[1]);    /*--------read input file------------*/   bmpinput = fopen(argv[1], "rb");   //fseek(bmpinput, 0l, seek_end);    /*--------declare output text file--------*/   rasteroutput = fopen("data.txt", "w");    /*--------get bmp data---------------*/   originalimage.cols = (int)getimageinfo(bmpinput, 18, 4);   originalimage.rows = (int)getimageinfo(bmpinput, 22, 4);   filesize = getimageinfo(bmpinput, 2, 4);   ncolors = getimageinfo(bmpinput, 46, 4);   vectorsize = filesize - (14 + 40 + 4*ncolors);    /*-------print data screen-------------*/   printf("width: %d\n", originalimage.cols);   printf("height: %d\n", originalimage.rows);   printf("file size: %ld\n", filesize);   printf("# colors: %d\n", ncolors);   printf("vector size: %d\n", vectorsize);    /*----start @ beginning of raster data-----*/   fseek(bmpinput, (54 + 4*ncolors), seek_set);    /*----------read raster data----------*/   for(r=0; r<=originalimage.rows - 1; r++)   {     for(c=0; c<=originalimage.cols - 1; c++)     {       /*-----read data , print in (row,column) form----*/       fread(pchar, sizeof(char), 1, bmpinput);       fprintf(rasteroutput, "(%d, %d) = %d\n", r, c, *pchar);     }   }    fclose(bmpinput);   fclose(rasteroutput);   }   /*----------get image info subprogram--------------*/ long getimageinfo(file* inputfile, long offset, int numberofchars) {   unsigned char     *ptrc;   long          value = 0l;   unsigned char     dummy;   int           i;    dummy = '0';   ptrc = &dummy;    fseek(inputfile, offset, seek_set);    for(i=1; i<=numberofchars; i++)   {     fread(ptrc, sizeof(char), 1, inputfile);     /* calculate value based on adding bytes */     value = (long)(value + (*ptrc)*(pow(256, (i-1))));   }   return(value);  } /* end of getimageinfo */ 

what not understanding:-

  1. i unable understand 'get image intosubprogram' part code trying image infos no of rows,columns, etc. why these infos stored on 4 bytes , use of value = (long)(value + (*ptrc)*(pow(256, (i-1)))); instruction.

  2. why there unsigned char dummy ='0' created , ptrc =&dummy assigned?

  3. why can't no of rows in image reading 1 byte of data getting greyscale value @ particular row , column.

  4. why using unsigned char store byte, isn't there other data type or int or long can use here?

please me understand these doubts(confusions!!?) having , forgive me if sound noobish.

thank you.

i tutorial quite bad in ways , problems understand not due being beginner.

i unable understand 'get image intosubprogram' part code trying image infos no of rows,columns, etc. why these infos stored on 4 bytes , use of value = (long)(value + (ptrc)(pow(256, (i-1)))); instruction.

the reason store on 4 bytes allow image sized between 0 , 2^32-1 high , wide. if used 1 byte, have images sized 0..255 , 2 bytes 0..65535.

the strange value = (long)(value + (*ptrc)*(pow(256, (i-1)))); i've never seen before. it's used convert bytes long work endianness. idea use powers of 256 set *ptrc value, i.e. multiplying first byte 1, next 256, next 65536 etc.

a more readable way use shifts, e.g. value = value + ((long)(*ptrc) << 8*(i-1));. or better read bytes highest 1 lower , use value = value << 8 + *ptrc;. in eyes lot better, when bytes come in different order, not simple.

a simple rewrite easier understand be

long getimageinfo(file* inputfile, long offset, int numberofchars) {   unsigned char     ptrc;   long          value = 0l;   int           i;    fseek(inputfile, offset, seek_set);    for(i=0; i<numberofchars; i++)  // start 0 make code simpler   {     fread(&ptrc, 1, 1, inputfile); // sizeof(char) 1, no need use     value = value + ((long)ptrc << 8*i); // shifts lot simpler @ , understand what's meaning   }   return value; // parentheses make function } 

why there unsigned char dummy ='0' created , ptrc =&dummy assigned?

this pointless. could've used unsigned char ptrc , used &ptrc instead of ptrc , ptrc instead of *ptrc. would've shown normal static variable.

why can't no of rows in image reading 1 byte of data getting greyscale value @ particular row , column.

what if image 3475 rows high? 1 byte isn't enough. needs more bytes. way of reading bit complicated.

why using unsigned char store byte, isn't there other data type or int or long can use here?

unsigned char 1 byte long. why use other type storing byte then?


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -