Exporting GIMP Images as C Source Code Arrays

GIMP provides native support for converting graphics directly into C language source code arrays, making it an essential tool for embedded systems programming, microcontrollers, and low-level graphical interfaces. Through standard export dialogues, users can output pixel data directly into .c, .h, .xbm, and .xpm formats. This guide outlines the available export options in GIMP, detailing how each format structures array data, handles color profiles and transparency, and allows customization of variable identifiers and memory storage.

1. The Native C Source Code (.c / .h) Exporter

When exporting an image with a .c extension, GIMP opens the "Export Image as C-Source" dialog (file-c-source plug-in). This tool translates raw pixel values directly into byte arrays.

  • Prefixed Name: Specifies the identifier prefix used for the generated variables, types, and constants. For example, setting the prefix to icon produces declarations such as icon_width, icon_height, icon_bytes_per_pixel, and icon_pixel_data.
  • Save as RGB565 (16-bit): Allows reducing the color depth down to 16 bits per pixel (5 bits red, 6 bits green, 5 bits blue), which is standard for small TFT displays and memory-constrained microcontrollers.
  • Save Alpha Channel (RGBA/RGB): If the image contains an alpha channel, GIMP can either preserve it (outputting 4 bytes per pixel) or discard it (outputting 3 bytes per pixel in standard 24-bit RGB).
  • Use Run-Length Encoding (RLE): Compresses the byte array using RLE. While this reduces the final flash/binary footprint, your C application must decompress the stream before rendering it.
  • Use Macros Instead of Struct: Controls whether image data is encapsulated within a structured definition or laid out as independent preprocessor definitions and byte arrays.
  • Use GLib Types (gint, guint8): Toggles between standard GLib data types and traditional C standard types (such as unsigned int and unsigned char).
  • Save as Header File: By naming the file with a .h extension during export, GIMP formats the output as an include file suitable for direct inclusion in C/C++ projects.
  • Comment Field: Inserts custom string documentation or licensing details directly into the comment block at the top of the generated C file.

2. X BitMap (.xbm) Export

For monochrome displays (such as 1-bit OLED or LCD panels), GIMP offers the X BitMap format.

  • Output Structure: Produces plain C code containing #define macros for image width and height, along with a static unsigned char <name>_bits[] array.
  • Data Organization: Each byte represents 8 individual black-and-white pixels.
  • Export Options: GIMP provides options to set the identifier prefix and optionally write a separate XBM header file. Images must be converted to Indexed mode (1-bit) before export to ensure correct bit packing.

3. X PixMap (.xpm) Export

The X PixMap format stores color graphics as human-readable C source code.

  • Output Structure: Generates a static array of strings (static char *<name>_xpm[]) where each character maps to an entry in a color palette table defined at the top of the array.
  • Export Options: The exporter allows setting transparency thresholds and color quantization parameters. This format is widely used in legacy X11 applications, toolkits like GTK or Qt for embedded icons, and applications where visual debugging of source arrays is necessary.