简体   繁体   中英

Convert C-Source image dump from RGB565 into RGB888

I have created with GIMP a C-Source image dump like the following:

/* 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..... }

Is there a way to convert this image from RG565 to RGB888?

I mean, I have found a way to covert pixel by pixel:

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

the problem is that using this logic I get numbers that are not represented as the one used n the original image:

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

Did I miss something?

EDIT: here you can find the original image: https://drive.google.com/file/d/1YBphg5_V6M2FA3HWcaFZT4fHqD6yeEOl/view

There are two things you need to do to create a C file similar to the original.

  1. Increase the size of the pixel buffer, because you are creating three bytes per pixel from the original's two bytes.
  2. Write strings that represent the new pixel data

The first part means simply changing the 2 to 3 , so you get:

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

In the second part the simplest method would be to print ALL characters in octal representation. (The original code has the "printable" characters visible, but the non-printable as octal escape sequences.)

To print ALL the characters in octal representation, do similar to

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 

This prints three bytes in octal representation \123\123\123 and after 16 pixels, prints end of string and newline.

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