简体   繁体   English

BMP 16位图像转换为数组

[英]BMP 16-bit Image converting into array

I have a BMP format image that is archived in the following manner 我有一个BMP格式的图片,该图片以以下方式存档

  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 LCD_ReadRam函数一次从LCD屏幕读取一个像素

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 以及如何将每个像素的值保存在[320] [240]矩阵中
Any help would be appreciated, Thanks. 任何帮助,将不胜感激,谢谢。

A BMP file reader does what you want. BMP文件阅读器可以满足您的需求。 You can get any good BMP file reader and tweak it to your purposes. 您可以获取任何优质的BMP文件阅读器,并对其进行调整。 For example: this question and answer gives a BMP file reader that assumes 24-bit BMP format. 例如: 此问题和答案为BMP文件阅读器提供了24位BMP格式。 Your format is 16-bit, so it requires some tweaking. 您的格式为16位,因此需要进行一些调整。

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. 注意:这是假定您的LCD_ReadRAM函数(大概是从LCD内存中读取内容)以标准5-6-5格式给出像素。

The name 5-6-5 signifies the number of bits in each 16-bit number allocated for each colour component (red, green, blue). 名称5-6-5表示分配给每个颜色分量(红色,绿色,蓝色)的每个16位数字中的位数。 There exist other allocations like 5-5-5 , but i have never seen them in practice. 还有其他分配,例如5-5-5 ,但我从未在实践中见过。

If you are talking about BMP image, then there is existing BMP format . 如果您正在谈论BMP图像,则存在现有的BMP格式

In BMP image all pixels are written in the reversed order (starting from the last line in image) sequentially. BMP图像中,所有像素都以相反的顺序(从图像的最后一行开始)顺序写入。 Size is defined in the BMP header, so you will have to read it. 大小是在BMP标头中定义的,因此您必须阅读它。

One more point is that each line in image has padding in order to make it multiply of 4. 还有一点是,图像中的每一行都有填充以使其乘以4。

You can use gimp. 您可以使用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 :-). 在gimp中打开图像,使用插件以16位模式导出C代码,并将其放入导出C代码的数组中:-)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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