Getting the pixel value of BMP file
Asked Answered
O

3

8

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?

Overripe answered 28/12, 2009 at 8:35 Comment(0)
C
8

The easy way would be to find a good image manipulation library for your chosen platform and use that.

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.

Cherri answered 28/12, 2009 at 8:52 Comment(0)
C
15

Note: you may need to grab an extra byte for the alpha values if your BMP has alpha channel. In that case image would be image[pixelcount][4], and you would add another getc(streamIn) line to hold that fourth index. My BMP turned out to not need that.

 // super-simplified BMP read algorithm to pull out RGB data
 // read image for coloring scheme
 int image[1024][3]; // first number here is 1024 pixels in my image, 3 is for RGB values
 FILE *streamIn;
 streamIn = fopen("./mybitmap.bmp", "r");
 if (streamIn == (FILE *)0){
   printf("File opening error ocurred. Exiting program.\n");
   exit(0);
 }

 int byte;
 int count = 0;
 for(i=0;i<54;i++) byte = getc(streamIn);  // strip out BMP header

 for(i=0;i<1024;i++){    // foreach pixel
    image[i][2] = getc(streamIn);  // use BMP 24bit with no alpha channel
    image[i][1] = getc(streamIn);  // BMP uses BGR but we want RGB, grab byte-by-byte
    image[i][0] = getc(streamIn);  // reverse-order array indexing fixes RGB issue...
    printf("pixel %d : [%d,%d,%d]\n",i+1,image[i][0],image[i][1],image[i][2]);
 }

 fclose(streamIn);

~Locutus

Caduceus answered 30/8, 2011 at 4:42 Comment(2)
Isn't there a header chunk you need to get past before you hit the image data?Allargando
@Caduceus What if my file is jpeg?Closelipped
C
8

The easy way would be to find a good image manipulation library for your chosen platform and use that.

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.

Cherri answered 28/12, 2009 at 8:52 Comment(0)
O
7

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_file_format. Follow the below steps.

  1. Create the 2x2 BMP image shown on Wikipedia.
  2. Open the file in binary mode using your C program.
  3. Seek to byte position 54.
  4. 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.

Olid answered 28/12, 2009 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.