GIMP Rearrange Colormap plug-in

on 16 Jun 2006 by Mukund (@muks)

A couple of days ago, an Ubuntu user stopped by with a problem. He wanted to create an Ubuntu splash screen which was an indexed image with certain special colormap (palette) positions. These colormap positions contained specific colours which were used by applications for displaying text. His problem was that these indices in the colormap were already taken by the image he had. In order to clear up those positions, the image data needed to be modified so that these positions were unused by the image.

Such a facility did not exist in GIMP yet, so it was difficult to do. I wrote a quick C plug-in prototype for him which shifted the colormap by one slot so that he could run it multiple times and get on with business. But the prototype evolved into a full-fledged plug-in today. It's called Rearrange Colormap and also comes with two PDB procedures plug-in-colormap-remap() (which accepts an array map) and plug-in-colormap-swap() (the simpler form which just swaps two entries in the colormap).

The core algorithm takes a map array which is of the same length as the colormap and depicts how the new colormap would look. Each slot contains the original index of a color which should be at that slot in the target image. Then the new colormap is simply:

    /* Data types are provided for reference */
    gint    i, j, num_entries;
    guchar  *map;
    guchar  *original_cmap, *new_cmap;

    for (i = 0; i < num_entries; i++)
        j = map[i] * 3;

        *new_cmap++ = original_cmap[j];
        *new_cmap++ = original_cmap[j + 1];
        *new_cmap++ = original_cmap[j + 2];

Changing the image data is a bit tricky, because map[] cannot be used directly as a fast lookup table. So we first create another map called pixel_map[] which can be used as a lookup table:

    /* Data types are provided for reference */
    gint    i, ncols;
    guchar  *map, *pixel_map;

    for (i = 0; i < ncols; i++)
      pixel_map[map[i]] = i;

pixel_map[] is then used as follows for translating the large amount of pixel data (the real code is more complicated than this as it needs to handle alpha-transparency data, etc.):

    /* Data types are provided for reference */
    const guchar *src_row =;
    guchar       *dest_row =;
    gint         i, j;

    for (i = 0; i < src_rgn.h; i++)
        const guchar *src  = src_row;
        guchar       *dest = dest_row;

        for (j = 0; j < src_rgn.w; j++)
          *dest++ = pixel_map[*src++];

        src_row += src_rgn.rowstride;
        dest_row += dest_rgn.rowstride;

All this is of course a simple abstraction. The plug-in has to do a lot more like input validation, user interaction, etc. It is available in the CVS version of GIMP now for anyone who wants to try it. You can find it in the image menu at Colors→Map→Rearrange Colormap. As usual, pizzas are queued for Michael Natterer and Sven Neumann. They refactored the plug-in code and made it better.

Screenshot of GIMP Rearrange Colormap plug-in