Palette Images

Palette images reduce the storage space required for images by reducing the number of colors used in the image to 256 colors and then using one number per pixel (one channel) to specify the color for each pixel. Each color number corresponds to a color in a palette of 256 colors. Each color in the palette is an RGB color out of a possible range of millions of colors.


Palette images are known as Indexed Color mode images in Adobe PhotoShop and palettes are called color tables or color maps in some applications.


Using palette images to save storage space has become less important for images in modern times because the cost of disk drive space has plummeted. It is no longer as important as it once was to reduce the space consumed by images. Preserving high visual quality is usually now more important with most images than saving space.


But there are other reasons to use palette images besides reduction of storage space.  For example, an important raster data usage of palettes is to force pixels into a pre-defined set of color values. Such values can represent various classification schemes for the physical regions represented by the pixels or other data values.


Another reason to use palette images is to reduce the size of images that will be published on Internet. Even with fast connections, Internet is so slow that it is still  important to reduce the size of images used on web sites. Reducing the number of colors in an image allows web graphics formats the best possibilities of compressing the image to a small size, which in turn allows faster loading.  


Palette images therefore remain popular and are frequently encountered in GIS work.   Manifold links to, imports from, and  exports to palette images in a variety of formats.  Manifold also provides fine control over palette images and the palettes they use to allow maximum exploitation of such images.

About Palette Images

RGB images use three numbers per pixel to specify the colors in an image, with four numbers per pixel used in the most common implementation of RGB images, RGBA, which adds a fourth, alpha channel to specify per-pixel transparency. . Using three numbers per pixel allows about 16 million color combinations in most installations but it requires enough storage space for each image to store three numbers for each pixel in the image. As mentioned in the Images and Channels topic there have been many methods invented for reducing the size of RGB images.


One popular method for reducing the size of RGB images is to use a palette. This method will preserve good color appearance in many cases while dramatically reducing the size of the image.  Because converting an RGB image into a palette image alters the data, such conversions are most frequently used either to force pixel values into a particular subset or when the visual appearance of the image is what counts and not the precise preservation of original data values within each pixel.


When used to preserve perceived visual appearance, the palette method depends on the observation that most RGB images use only a small number of the 16 million possible colors available for use. There are two reasons why this is so. The first reason is that most images have far fewer than 16 million pixels in them. A 1024 x 768 image, for example, has less than a million pixels. Even if each pixel were a different color such an image would require much less than 16 million different colors to draw.




The second reason, specific to uses where human perception of the image is what counts, is that most colors used in computer images are so similar to each other that they are indistinguishable to the human eye.   Consider an example using the image seen above, a photo of the bronze monument to Henry of Navarre which stands at the Pont Neuf in Paris.




If we zoom far into our example image near the head and chin of the monument we can see that the blue sky is composed of many blue pixels that are slightly different shades of blue. However, most of the blue shades are so similar to each other that it is very difficult to tell them apart. We could easily replace the hundreds of different, but virtually identical, shades of blue with two or three "blues" without significantly changing how the image looks to the human eye.


That's exactly what the palette method does: to convert an RGB image into a palette image the system analyzes all RGB colors in the image and groups them into similar shades. The palette method then takes all the very similar colors in a group and replaces them with one color that is a close match to all of the colors in that group. For many images, it is possible to choose only 256 colors that are so reasonably close to the various similar colors out of all 16 million possible colors that the image appears to the human eye the same as the original.


i_palettes01_01.png    i_palettes01_02.png


To see this in action, consider the above two images. Both images are zoomed in views of the bronze sample image showing the head and chin of the bronze monument. The image at left uses three RGB numbers per pixel so that each pixel can be any one of 16 million possible colors. Note how the blue sky is composed of many similar shades of blue, most of which are indistinguishable from each other.


The image on the right uses only 32 colors throughout the entire image. Each of these 32 colors is an RGB color chosen from the overall range of 16 million colors to be the best match possible to the group of similar colors it replaces. Only two shades of blue, for example, appear in the blue sky. We use 32 colors in this example because using 256 colors as is normally done with palette images results in such a close match to the original image that it is difficult to tell the difference. Using 32 colors makes differences more apparent for the sake of an example.


The palette method saves the colors that are used in a palette (also known as a color table or color map) which is to be used for that image. Each color in the palette is listed as a three-number RGB color. One possible 32-color palette for the sample bronze image converted to 32 colors is:




Using only the above 32 colors results in an image that is remarkably close to the original.  


i_palettes01_04.png   i_palettes01_05.png


The image on the left is the original, while the image on the right is the 32-color image.  We've used only 32 colors in this example because using a full range of 256 color in the palette as allowed by Manifold results in a palette image so visually similar to a full RGB image that we could not tell the two apart.  Using only 32 colors results in some visible pixilation in the white cloud under the belly of the horse but a 256-color palette image would show no such visible pixilation.  


The size in bytes of the palette image is about one-third of the size of the full RGB image since only one number per pixel is required for the palette image and not three numbers per pixel as are required by the RGB image.  That may not make a big difference for a single, small image containing a few thousand bytes but it would make a big difference for an image that is ten gigabytes in size.  

How Palettes Work

When we import an image that uses palettes into Manifold we get two components, an image and the image's table:




The image's table contains the data as tiles ...




 ... and an image component which serves as a display window to visualize that data.




The image knows to display a palette image based on how the original data was imported into Manifold, as a palette image.   We can see that Manifold understands this data is for a palette image by opening the Properties for the image.




The StylePixel property exposes the style values applied to each pixel, including the palette color values within the palette that is used to render the image based on the data in the table.    We can see the palette by opening the Style dialog for the image:




Palette images in Manifold use a palette table of 256 colors per image.   Palette images then require only one byte per pixel, since each byte can represent 256 colors (bytes range in value from 0 to 255 for 256 positions in all).  The palette is just an internal look-up table that says what RGB value is to be used for that pixel. That is why Adobe PhotoShop refers to such images as "Indexed Color" images.


How are Palette Images Created?


Manifold queries which convert RGB images into palette images do the following automatically: The first step in converting an RGB image into a palette image is reducing the number of colors it uses to 256 colors or less. This requires picking out a palette that is a "best match" to the many colors in the RGB image and then assigning a color from the palette to each pixel in the image. Each pixel then is represented with one number that specifies which color in the palette is to be used for that pixel.


Let's take a look at how the example 32-color bronze image might be saved as a palette image.




The pixels in the region enclosed in red in the illustration would be coded as one number per pixel:




The number for each pixel says which color in the palette to use for that number. To figure out what color the number 16 in red shown above is supposed to be, we look at the palette used for this image:




All the colors used in this image are listed in the palette.




The look-up process is simple: the number 16 takes us to the sixteenth color in the palette, which is a shade of blue specified by the RGB triplet of numbers 91, 143, 207, whch are listed in the palette table in Windows standard BGR order of 207, 143, 91.   These three numbers will be used to control the RGB pixel elements in the computer monitor for that specific pixel.


Note that the pixel to the right of the one shown in red has the value 18, which is a lighter shade of blue, the eighteenth color in the palette, two slots over from 16.

Tutorial Examples of Palettes in Action

An easy way to understand how palettes work is to consider an analogy, the hobby activity of painting by numbers.   That hobby is derided by some art snobs but painting by numbers has been a well-loved pastime of many, including Winston Churchill, Albert Einstein and Mahatma Gandhi.*




Suppose we would like to create a hobby painting similar to one of the famous "Sunflowers" paintings by Vincent van Gogh.




Vendors of paint by numbers worksheets start with a simplified image of the original painting, like the above, which uses fewer colors and is easier to trace into regions.




A worksheet of outlines is created with numbers for each region where a different color of paint is to be applied.




The numbers match paint colors provided in numbered bins on a palette or in numbered tubes that an artist can squeeze out onto a palette.  The numbers on the worksheet indicate which number of paint to use in that region.  Numbers are used to save space, since it is more economical to print a single number than to write out "Ocher Brown" at one spot and "Goldenrod Yellow" at another.  




For example, we might color all regions in the worksheet marked with the number 3 using paint number 3 on the palette.




We color all regions marked with a 2 using paint number 2.




A dark green paint number 4 goes into all regions marked with the number 4.




If we continue this way and do a good job of coloring within the lines (Einstein was particularly good at this...) and we do not make any mistakes, such as using paint number 3 to color regions marked 13, the result can be surprisingly pleasing.   The illustrations show simplified worksheets: in reality most paint by numbers worksheets for adults are much more detailed to better approximate the original.


In the paint by numbers method there is no particular meaning as to how colors are assigned.   All that matters is that each region to be colored on a worksheet has a number and that we use whatever number of paint matches that number.   If we make a mistake creating our palette or if we make a mistake in reading which number is to be used the results will not be correct.


For example, suppose instead of grabbing a tube of yellow paint marked number 3 we accidentally grab a tube of bright green paint marked number 30 and squeeze a big dollop of bright green paint onto our palette in position number 3.   


If we then merrily go on to fill in all regions marked 3 in the worksheet with the (wrong) paint color poured into the number 3 position of our palette the results will be incorrect.




In every region of the painting where a soft yellow, the real number 3 color, should have been applied we instead would have applied a bright green color.  Whether or not van Gogh would have liked the effect might not be completely beside the point.   Manipulating palettes by substituting different colors for intended colors in a paint by numbers process is one way to create different versions of well-known paintings that are similar to the originals but, perhaps, use different colors.


The use of palettes for images in computers is a very similar process.   




The reduced-color (that is, created using a limited range of colors) example shown above utilizes only 32 different colors.   It achieves greater resemblance to the original because instead of assigning colors to large regions, each individual pixel can use one of those colors.




We can open the image in Manifold and then choose Edit - Style to open the Style dialog to see the palette used.  We can see from that palette that the colors used in our artist's palette example above are indeed the real colors used within this image on the computer.  


Those colors are applied to each pixel in a paint by numbers process directly analogous to the paint by numbers hobby process.    In a palette image, each pixel has a number that specifies the color for that pixel.  The number to use for a color in each pixel has no more meaning by itself than the number 3 printed for a region in a paint by numbers worksheet.   The numbers only have meaning in terms of what color is in the palette to be painted onto that number.  


That is different from RGB color values where the triplet of numbers that make up an RGB color value have meaning in and of themselves.  For example, an RGB color of 116, 235, 245 is the soft, yellow color that results when the red, green and blue pixel elements of a display are set to those respective intensities within a range of 0 to 255.


If we zoomed into the data, so to speak, to see how pixels might be arranged in the regular positions they have in an image we could imagine that each pixel position is marked with the number of the color that is supposed to be painted into that pixel, just like a rectangular set of boxes in a paint by numbers worksheet.




To fill each pixel with the right color we can imagine Manifold looking up the number in the palette and then filling the pixel with the color for that number.   For example, pixels with the number 4 in them would be filled with color number 4 from the palette, a forest green color.  Note that there is nothing special about whether a color is number 4 or number 3.  The only thing that matters is that the right number color from the palette is used for each number in the pixel.  Manifold, of course, does that automatically.




Above we see how the correspondence between color numbers in the pixels and color numbers in the palette are used to fill pixels with a color from the palette.




Just like painting by numbers, when all the pixels are filled in with the color their numbers choose from the palette, the image is filled in with all the right colors in all the right pixels.  Unlike painting by numbers, when Manifold fills pixels with the right color it does so automatically, it does so instantly and it never makes a mistake.




Looking at a series of zoomed out views in Manifold we can see how the cumulative effect of many pixels all being colored using the right colors from the palette builds up a recognizable image.   We will mark the location of the pixels with a magenta box.




We can see how what were just blobs of color start forming a green feature against a yellow background.




Zooming further ...




... and further out ...




We see that our example using a few pixels was part of the stem to a sunflower.


Just as in the case of hobby painting by numbers where changing the color assigned to a palette number can result in different visual effects the same is true for computer palettes as well.   We can see that by opening the Edit - Style dialog for the image and then double-clicking onto a color well to change the color assigned to a given palette number.  




Suppose, for example, we double-click into the color well for palette color number 5 and substitute a bright green color for the dark green color originally used in the palette for the number 5 color.  All pixels having a color value of 5 would now use that new bright green color.





Fewer than 256 colors - Manifold palette tables can hold 256 colors; however, many palette images that are imported may have been created using palettes of fewer colors, perhaps only 16 or 32 colors. In such cases the extra rows in the palette table will be black.


The meaning of the word - In classical artistic circles, the word palette means the board with colors on it that a painter holds in his or her hand. The painter chooses colors from the palette to load onto a brush, which he then uses to apply color to the canvas.


Some painters in art history had a preference for using a particular selection of colors on their palettes and so the word "palette" in art history has also come to mean a particular collection of colors favored by a painter. One can speak of the distinctive palette of Cézanne, for example, or one might compare the sepia palette used by Van Gogh in his early work with the more vivid palette he employed late in life in Arles.


Palettes may be  optimized for one image - A logical outcome of the palette method for images is that for photographic or visual rendering the palette used with each particular image is specific to that image. The particular set of 256 colors that does the best job of approximating colors in one RGB image will not be the same as the 256 colors used to best approximate a different image.


For example, the sample bronze image is mostly greens and blues so the 32-color palette for this image shown above has many different shades of green and blue.






The palette image of Tokyo airport shown above, on the other hand, would use an entirely different palette that has many shades of near-black color when reduced to 32 colors.


* Just kidding -  Gandhi preferred weaving and Einstein enjoyed sailing, while Churchill painted without numbers.


See Also