Monday, January 23, 2012

Processing pixels in a Bitmap Image file

I was meddling with some image processing, I had to process some pixels from a BMP image, if you're looking for the same then this is the blog post for you. The Algorithm is pretty simple, I've used the Wikipedia article BMP File format as my reference, the source is available in a GitHub repository.

//Program to analyze a Bitmap File.
//Please read the Wikipedia article follow this algorithm
//even more clearly.

//Moderate familiarity with C required to understand the
//the functions in the source code, but not the algorithm.

Start Program
     Openthe BMP file
          Get the BMP Magic
          Print the BMP Magic
              **Nothing of Importance, just
                the letters 'B' and 'M'
          Get the BMP Header
          Print the BMP Header
              **Contains the Pixel Data Offset (address)
              **Other Information (Not important):
                   >File Size
                   >Creator 1
                   >Creator 2
          Get the DIB` Header
          Print the DIB Header
              **Contains Width in Pixels  (in pixels)
              **Contains Height in Pixels (in pixels)
              **Contains Pixel Depth (in bits)
               **Other Information (not important):
                   >Header Size (in bytes)
                   >Compression Type (ENUM)
                   >Horizontal, Vertical (in pixels/m)
                   >No of color planes (usually 1)
                   >Pixel Data size (don't confuse with filesize)
                   >No of colors in palette (0 - default)
                   >No of important colors (0 - all are important)
          No. of bytes per pixel = Pixel Depth / 8
          Go the Pixel Data Offset
          foreach x in Height (No. of rows)
              foreach y in Width (No. of columns)
                   foreach b in [No. of bytes per pixel]
                        print [BYTE DATA]
                        //You can perform any other operation
                        //once you can print the information.
              print NEWLINE
     Close the BMP File
End Program

DIB: Device Independent Bitmap
NOTE: I've printed out all the information in the bitmap file though

The source code is available here in my GitHub repository.