Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read pixel value in bmp file [closed]

How can I read the color value of 24bit BMP images at all the pixel [h*w] in C or C++ on Windows [better without any 3rd party library]. I got Dev-C++
A working code will be really appreciated as I've never worked on Image reading & have come to SO after Googling [if you can google better than me, plz provide a link].

like image 264
Sourav Avatar asked Feb 15 '12 15:02

Sourav


People also ask

How do you unlock a BMP file?

You can open BMP files on either PC or Mac with external software, such as Adobe Creative Cloud. If you use a PC or Mac, start by opening the folder with the BMP file you want to use. Right-click on the file name and then hover over the Open With option.

How is a pixel stored in BMP file?

The pixel values are stored in each bit, with the first (left-most) pixel in the most-significant bit of the first byte. Each bit is an index into a table of 2 colors. An unset bit will refer to the first color table entry, and a set bit will refer to the last (second) color table entry.


1 Answers

The following code snippet is not complete, and contains lots of hidden assumptions and bugs. I wrote it from scratch for a university course project from mere observation, where it minimally fulfilled all the requirements. I didn't work on it any more, because there must be libraries that would do the job way better.

Here are the conditions where it worked okay (some assumptions are pointed out in the comments):

  1. It ran on Windows, I'm not sure about other platforms
  2. It works for 24-bit color BMP images
  3. It assumes that the width of the image is a multiple of 4, so it doesn't handle the padding bytes in case it's not
  4. It decodes the image width and height as 32-bit little endian integers
  5. It returns a pointer to dynamically allocated memory, it may cause memory leak if it's not released by the caller

Other answers have covered some of these issues.


You can try this one:

unsigned char* readBMP(char* filename)
{
    int i;
    FILE* f = fopen(filename, "rb");
    unsigned char info[54];

    // read the 54-byte header
    fread(info, sizeof(unsigned char), 54, f); 

    // extract image height and width from header
    int width = *(int*)&info[18];
    int height = *(int*)&info[22];

    // allocate 3 bytes per pixel
    int size = 3 * width * height;
    unsigned char* data = new unsigned char[size];

    // read the rest of the data at once
    fread(data, sizeof(unsigned char), size, f); 
    fclose(f);

    for(i = 0; i < size; i += 3)
    {
            // flip the order of every 3 bytes
            unsigned char tmp = data[i];
            data[i] = data[i+2];
            data[i+2] = tmp;
    }

    return data;
}

Now data should contain the (R, G, B) values of the pixels. The color of pixel (i, j) is stored at data[3 * (i * width + j)], data[3 * (i * width + j) + 1] and data[3 * (i * width + j) + 2].

In the last part, the swap between every first and third pixel is done because I found that the color values are stored as (B, G, R) triples, not (R, G, B).

like image 147
Sufian Latif Avatar answered Sep 21 '22 20:09

Sufian Latif