Frank's Random Wanderings

Fast RGB332 to RGB565 Colorspace Conversion


I’ve recently been working on a project with a small 320×240 TFT display connected to a microcontroller. In this case an ARM Cortex-M0+. Being able to display images is a really nice feature, however images can consume a lot of space in the limited flash memory of the microcontroller.

The controller chip built into the display accepts RGB565 data, which is to say, 16 bits per pixel. One idea to reduce the size of the images is to use RGB332, which is 8 bits per pixel. For displaying photographs this drop in color depth will be visually noticeable, however for icons and similar things RGB332 would be just fine.

Images can be converted to RGB332 using the free download program “Image2LCD” found here:

Now the microcontroller needs a fast way of converting the 8-bit RGB332 “pixel data” of the saved image file back into the 16-bit RGB565 “pixel data” that the display needs to receive. The obvious answer is a lookup table. I couldn’t quickly find such a table on the web so I spent a little time on the weekend writing a program to generate a table. Here’s my resulting RGB332 to RGB565 lookup table:

The microcontroller code then simply has to:

  • Read a byte from the image
  • Use the byte as an index into the table to read a word from the table
  • Write the word to the display

And repeat for every pixel data byte in the image.

For those who want to generate the table themselves, here’s my little C program:

Notice this conversion table is not optimised for any particular purpose. The resulting lookup table is a “palette conversion” – it’s mapping an 8-bit color palette to a 16-bit color palette. Obviously with an 8-bit starting point, only 256 colors from the 65536 possible 16-bit colors can be selected. For a general purpose conversion there is no perfect answer – something like the above is about as good as you can do. The above lookup table attempts to cover the 16-bit RGB565 color space as evenly as possible.

However, if you’re doing this conversion for a specific image it can be possible to optimise the palette for it. For example, if your image was mostly shades of red, you could place a lot more “shades of red” 16-bit RGB565 entries into your lookup table (at the expense of green and blue entries). Some early computer games, like Doom, used 8-bit color, and optimised their palettes for the types of images they were displaying. Those who have played Doom know it’s lots of shades of brown. There’s no end to the tweaking that can be done for specific applications.

Leave a Reply

Your email address will not be published. Required fields are marked *