繁体   English   中英

将 C 源图像转储从 RGB565 转换为 RGB888

[英]Convert C-Source image dump from RGB565 into RGB888

我用 GIMP 创建了一个 C 源图像转储,如下所示:

/* GIMP RGBA C-Source image dump (example.c) */

static const struct {
  guint      width;
  guint      height;
  guint      bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */ 
  guint8     pixel_data[304 * 98 * 2 + 1];
} example= {
  304, 98, 2,
  "\206\061\206\061..... }

有没有办法将此图像从 RG565 转换为 RGB888?

我的意思是,我找到了一种逐像素转换的方法:

 for (i = 0; i < w * h; i++)
   {
      uint16_t color = *RGB565p++;
      uint8_t r = ((color >> 11) & 0x1F);
      uint8_t g = ((color >> 5) & 0x3F);
      uint8_t b = (color & 0x1F);

      r = ((((color >> 11) & 0x1F) * 527) + 23) >> 6;
      g = ((((color >> 5) & 0x3F) * 259) + 33) >> 6;
      b = (((color & 0x1F) * 527) + 23) >> 6;

      uint32_t RGB888 = r << 16 | g << 8 | b;
      printf("%d \n", RGB888);
   }

问题是使用此逻辑我得到的数字未表示为原始图像中使用的数字:

P3
304 98
255
3223857
3223857
3223857
3223857
3223857
3223857
3223857
3223857

我错过了什么?

编辑:在这里你可以找到原始图片: https://drive.google.com/file/d/1YBphg5_V6M2FA3HWcaFZT4fHqD6yeEOl/view

要创建类似于原始文件的 C 文件,您需要做两件事。

  1. 增加像素缓冲区的大小,因为您要从原始像素的两个字节创建每个像素三个字节。
  2. 写入表示新像素数据的字符串

第一部分意味着简单地将2更改为3 ,所以你得到:

    guint8     pixel_data[304 * 98 * 3 + 1];
} example= {
    304, 98, 3,

在第二部分中,最简单的方法是以八进制表示形式打印所有字符。 (原始代码具有可见的“可打印”字符,但不可打印为八进制转义序列。)

要以八进制表示形式打印所有字符,请执行类似于

for (i = 0; i < w * h; i++)
{
    ...
    R, G and B calculation goes here
    ...

    // Print start of line and string (every 16 pixels)
    if (i % 16 == 0)
        printf("\n\"");

    printf("\\%3o\\%3o\\%3o", r, g, b);

    // Print end of string and line (every 16 pixels)
    if ((i+1) % 16 == 0)
        printf("\"\n");
}
printf("\"\n");  // Termination of last line 

这将以八进制表示形式打印三个字节\123\123\123并在 16 像素后打印字符串结尾和换行符。

暂无
暂无

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

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