How to convert image to Uint16 array of hex codes?

Hello. My goal is to load a uint array of hex and I found this code somewhere on the internet

Uint16 pixels[16*16] = { // …or with raw pixel data:
0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
0x0fff, 0x0aab, 0x0789, 0x0bcc, 0x0eee, 0x09aa, 0x099a, 0x0ddd,
0x0fff, 0x0eee, 0x0899, 0x0fff, 0x0fff, 0x1fff, 0x0dde, 0x0dee,
0x0fff, 0xabbc, 0xf779, 0x8cdd, 0x3fff, 0x9bbc, 0xaaab, 0x6fff,
0x0fff, 0x3fff, 0xbaab, 0x0fff, 0x0fff, 0x6689, 0x6fff, 0x0dee,
0xe678, 0xf134, 0x8abb, 0xf235, 0xf678, 0xf013, 0xf568, 0xf001,
0xd889, 0x7abc, 0xf001, 0x0fff, 0x0fff, 0x0bcc, 0x9124, 0x5fff,
0xf124, 0xf356, 0x3eee, 0x0fff, 0x7bbc, 0xf124, 0x0789, 0x2fff,
0xf002, 0xd789, 0xf024, 0x0fff, 0x0fff, 0x0002, 0x0134, 0xd79a,
0x1fff, 0xf023, 0xf000, 0xf124, 0xc99a, 0xf024, 0x0567, 0x0fff,
0xf002, 0xe678, 0xf013, 0x0fff, 0x0ddd, 0x0fff, 0x0fff, 0xb689,
0x8abb, 0x0fff, 0x0fff, 0xf001, 0xf235, 0xf013, 0x0fff, 0xd789,
0xf002, 0x9899, 0xf001, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0xe789,
0xf023, 0xf000, 0xf001, 0xe456, 0x8bcc, 0xf013, 0xf002, 0xf012,
0x1767, 0x5aaa, 0xf013, 0xf001, 0xf000, 0x0fff, 0x7fff, 0xf124,
0x0fff, 0x089a, 0x0578, 0x0fff, 0x089a, 0x0013, 0x0245, 0x0eff,
0x0223, 0x0dde, 0x0135, 0x0789, 0x0ddd, 0xbbbc, 0xf346, 0x0467,
0x0fff, 0x4eee, 0x3ddd, 0x0edd, 0x0dee, 0x0fff, 0x0fff, 0x0dee,
0x0def, 0x08ab, 0x0fff, 0x7fff, 0xfabc, 0xf356, 0x0457, 0x0467,
0x0fff, 0x0bcd, 0x4bde, 0x9bcc, 0x8dee, 0x8eff, 0x8fff, 0x9fff,
0xadee, 0xeccd, 0xf689, 0xc357, 0x2356, 0x0356, 0x0467, 0x0467,
0x0fff, 0x0ccd, 0x0bdd, 0x0cdd, 0x0aaa, 0x2234, 0x4135, 0x4346,
0x5356, 0x2246, 0x0346, 0x0356, 0x0467, 0x0356, 0x0467, 0x0467,
0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff
};

That code is a array of hex of the 16x16 sdl logo. So I wanted to convert my image to hex array like that but there is no converter for that. How do you guys do it? Thanks :).

GIMP can export images as .c file, supporting 16, 24 and 32bit per pixel
(you apparently want 16bit per pixel?)
The result will look like

static const struct {
   unsigned int   width;
   unsigned int   height;
   unsigned int   bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */
   unsigned char  pixel_data[68 * 63 * 2 + 1];
} gimp_image = {
   68, 63, 2,
 
"\11BF)\344\30\344\40\344\30\304\30\304\30\243\20\304\30\303\30\303\30\303"
  // .. and lots of more lines like that ..
};

unfortunately, even though this is 16bit (RGB565) data, it’s in a char
array and I’m not sure whether it’s in little or big endian order (i.e.
if you could just cast pixel_data to Uint16* or if you’d have to swap
all the bytes on little endian platforms, assuming big endian platforms
are irrelevant for you).

However, unless you insist on using 16bit per pixel, you can use this to
save 24bit RGB or 32bit RGBA data, which is easier to handle as each
color channel uses one byte (char).

I wrote an article on using this format to integrate 32bit RGBA icons
into SDL applications:

By the way, in SDL2 2.0.5 and newer loading the pixeldata into an
SDL_Surface* is even easier, it can be done like:

int depth = my_icon.bytes_per_pixel*8; // bits instead of bytes per pixels
int pitch = my_icon.bytes_per_pixel*my_icon.width;
Uint32 pixelFormat = SDL_PIXELFORMAT_RGB24;
if(my_icon.bytes_per_pixel == 4)
     pixelFormat = SDL_PIXELFORMAT_RGBA32;

SDL_Surface* surf =
   SDL_CreateRGBSurfaceWithFormatFrom((void*)my_icon.pixel_data,
       my_icon.width, my_icon.height, depth, pitch, pixelFormat);

without all the messing around with colormasks.
See also https://wiki.libsdl.org/SDL_CreateRGBSurfaceWithFormatFrom

Cheers,
Daniel

1 Like