繁体   English   中英

BMP 16位图像转换为数组

[英]BMP 16-bit Image converting into array

我有一个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);

LCD_ReadRam函数一次从LCD屏幕读取一个像素

我想知道,如何获得此图像文件的像素位置 以及如何将每个像素的值保存在[320] [240]矩阵中
任何帮助,将不胜感激,谢谢。

BMP文件阅读器可以满足您的需求。 您可以获取任何优质的BMP文件阅读器,并对其进行调整。 例如: 此问题和答案为BMP文件阅读器提供了24位BMP格式。 您的格式为16位,因此需要进行一些调整。

这是我尝试进行的操作(未经测试,因此您应该将一些硬编码的细节放在一粒盐上)。

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;
}

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

名称5-6-5表示分配给每个颜色分量(红色,绿色,蓝色)的每个16位数字中的位数。 还有其他分配,例如5-5-5 ,但我从未在实践中见过。

如果您正在谈论BMP图像,则存在现有的BMP格式

BMP图像中,所有像素都以相反的顺序(从图像的最后一行开始)顺序写入。 大小是在BMP标头中定义的,因此您必须阅读它。

还有一点是,图像中的每一行都有填充以使其乘以4。

您可以使用gimp。 在gimp中打开图像,使用插件以16位模式导出C代码,并将其放入导出C代码的数组中:-)。

暂无
暂无

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

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