Yesterday I was working with the ILI9341 library and came across the pictureEmbed example. I quite liked the idea of using custom images into my project, when I got to the image conversion part I discovered that GIMP provides the required functionality out of the box. I thought I'd share this; a screenshot that explains the steps:
In text:
1. File -> Export As
2. In the Export Image dialog, use 'C source code (*.c)' as filetype.
3. Press export to get the export options dialog.
4. Type the desired variable name into the 'prefixed name' box.
5. Uncheck 'GLIB types (guint8*)'
6. Check 'Save as RGB565 (16-bit)'
7. Press export to save your image.
This results in C file (attached as purple.c) that looks like this:
The advantage of using the GIMP to export the file this is that you get all the required information in this one file directly when you save the image. You can then read the width and height from the structure and use the image's data with the following code:
View attachment purple.c
In text:
1. File -> Export As
2. In the Export Image dialog, use 'C source code (*.c)' as filetype.
3. Press export to get the export options dialog.
4. Type the desired variable name into the 'prefixed name' box.
5. Uncheck 'GLIB types (guint8*)'
6. Check 'Save as RGB565 (16-bit)'
7. Press export to save your image.
This results in C file (attached as purple.c) that looks like this:
Code:
static const struct {
unsigned int width;
unsigned int height;
unsigned int bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */
unsigned char pixel_data[240 * 320 * 2 + 1];
} image_name = {
240, 320, 2,
"\236\233\274\202...", // a lot more data here...
};
The advantage of using the GIMP to export the file this is that you get all the required information in this one file directly when you save the image. You can then read the width and height from the structure and use the image's data with the following code:
Code:
// include the image, just as in the example.
#include "purple.c"
// Write the image, taking the width, height and data from the structure in the purple.c file
tft.writeRect(0, 0, image_name.width, image_name.height, (uint16_t*)(image_name.pixel_data));
View attachment purple.c