title: Understanding And Getting info of Bitmap in C
body: <p>I am having a hard time understanding and parsing the info data present in a bitmap image. To better understand I read the following tutorial, <a href="http://dasl.mem.drexel.edu/alumni/bGreen/www.pages.drexel.edu/_weg22/raster.html" rel="nofollow">Raster Data.</a></p>
  
  <p>Now, The code present there is as follows, (Greyscale 8bit color value)</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  
  /*-------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 = &amp;someChar;
  
    if(argc &lt; 2)
    {
      printf("Usage: %s bmpInput.bmp\n", argv[0]);
      //end the 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 TO 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 AT BEGINNING OF RASTER DATA-----*/
    fseek(bmpInput, (54 + 4*nColors), SEEK_SET);
  
    /*----------READ RASTER DATA----------*/
    for(r=0; r&lt;=originalImage.rows - 1; r++)
    {
      for(c=0; c&lt;=originalImage.cols - 1; c++)
      {
        /*-----read data and 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 = &amp;dummy;
  
    fseek(inputFile, offset, SEEK_SET);
  
    for(i=1; i&lt;=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 */
  </code></pre>
  
  <p><strong>What I am not understanding:-</strong></p>
  
  <ol>
  <li><p>I am unable the understand the <strong>'GET IMAGE INTOSUBPROGRAM'</strong> part where the code is trying to get the image infos like no of rows,columns, etc. Why are these infos stored over 4 bytes and what is the use of the  <code>value = (long)(value + (*ptrC)*(pow(256, (i-1))));</code> instruction.</p></li>
  <li><p>Why there  <code>unsigned char dummy ='0'</code> is created and then  <code>ptrC =&amp;dummy</code> is assigned?</p></li>
  <li><p>Why can't we just get the no of rows in an image by just reading 1 byte of data like getting the Greyscale value at a particular row and column.</p></li>
  <li><p>Why are we using <strong>unsigned char</strong> to store the byte, isn't there some other data type or int or long we can use effectively here?</p></li>
  </ol>
  
  <p>Please help me understand these doubts(confusions!!?) I am having and forgive me if they sound noobish.</p>
  
  <p>Thank you.</p>