简体   繁体   中英

BMP 16-bit Image converting into array

I have a BMP format image that is archived in the following manner

  for (j = 0; j < 240; j++) {
    for(i=0;i<320;i++) { 
      data_temp = LCD_ReadRAM();
      image_buf[i*2+1] = (data_temp&0xff00) >> 8;
      image_buf[i*2+0] = data_temp & 0x00ff;

    }
    ret = f_write(&file, image_buf, 640, &bw);

where LCD_ReadRam function reads a pixel at a time from the LCD screen

I want to know, How could I get the pixel positions of this image file. And how to save the values of each pixel in an [320][240] matrix
Any help would be appreciated, Thanks.

A BMP file reader does what you want. You can get any good BMP file reader and tweak it to your purposes. For example: this question and answer gives a BMP file reader that assumes 24-bit BMP format. Your format is 16-bit, so it requires some tweaking.

Here is my attempt at doing that (didn't test, so you should take the hard-coded details with a grain of salt).

int i;
FILE* f = fopen(filename, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header

int width = 320, height = 240; // might want to extract that info from BMP header instead

int size_in_file = 2 * width * height;
unsigned char* data_from_file = new unsigned char[size_in_file];
fread(data_from_file, sizeof(unsigned char), size_in_file, f); // read the rest
fclose(f);

unsigned char pixels[240 * 320][3];

for(i = 0; i < width * height; ++i)
{
    unsigned char temp0 = data_from_file[i * 2 + 0];
    unsigned char temp1 = data_from_file[i * 2 + 1];
    unsigned pixel_data = temp1 << 8 | temp0;

    // Extract red, green and blue components from the 16 bits
    pixels[i][0] = pixel_data >> 11;
    pixels[i][1] = (pixel_data >> 5) & 0x3f;
    pixels[i][2] = pixel_data & 0x1f;
}

Note: this assumes that your LCD_ReadRAM function (presumably, reading stuff from your LCD memory) gives the pixels in the standard 5-6-5 format.

The name 5-6-5 signifies the number of bits in each 16-bit number allocated for each colour component (red, green, blue). There exist other allocations like 5-5-5 , but i have never seen them in practice.

If you are talking about BMP image, then there is existing BMP format .

In BMP image all pixels are written in the reversed order (starting from the last line in image) sequentially. Size is defined in the BMP header, so you will have to read it.

One more point is that each line in image has padding in order to make it multiply of 4.

You can use gimp. Open the image in gimp, use a plugin to export C code using the 16-bit mode, and its put into an array in exported C code :-).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM