Hello, i got a question for reading an bmp image. How can i get the pixel value(R, G, B values) in an bmp image? Can anyone help me using the C programming language?
The easy way would be to find a good image manipulation library for your chosen platform and use that.
- Linux ImLib / GDK-Pixbuf (Gnome/GTK) / QT Image (KDE/Qt) should be able to do what you need.
- Windows I'm not familiar with the appropriate system library, but an MSDN Search for "Bitmap" is probably a good place to start.
- Mac OSX Cocoa has some image manipulation capabilities, see this article.
The hard way would be to open the file and actually interpret the binary data within. To do that you'll need the BMP File Specification. I'd recommend trying the easy way first.
You need to study the BMP file format. It is easier to read uncompressed 24-bit BMP files. They just contain a header at the beginning and RGB values of each pixel.
To start with this, check the example of 2x2 bitmap image at http://en.wikipedia.org/wiki/BMP%5Ffile%5Fformat. Follow the below steps.
- Create the 2x2 BMP image shown on Wikipedia.
- Open the file in binary mode using your C program.
- Seek to byte position 54.
- Read 3 bytes.
The bytes would be 0, 0 and 255 respectively. (Not sure whether the order is RGB. I had done this long back and I think the order is not RGB. Just verify this.)
As simple as that! Study the header of the BMP to understand more about the format.