Colors and Color Reduction

JPEG-compressed images, as well many PICT, TIFF, and BMP images, can contain a lot of color information Ñ 16.7 million colorsÕ worth, to be exact.  This is just fine if you have 24-bit display hardware, capable of displaying in AppleÕs ÒMillionsÓ mode, but unfortunately we arenÕt all so lucky.  The fact is that the single most popular color setup on the Macintosh is the standard 8-bit, or 256 color display.  It may not be immediately obvious that you can, in fact, view 24-bit color images with rather high quality on a 256 color display.  After all, how can you possibly go from 16.7 million colors down to 256 colors without losing something significant?


Video modes on the Macintosh

To understand how colors and color reduction work, it is necessary to understand something about the way the Macintosh handles its monitors.  The current version of 32-bit Color QuickDraw, which is responsible for all drawing on your Macintosh, supports six different screen modes, depending on how much video memory (sometimes called VRAM) your Macintosh has available for the screen.  These modes are identified by the number of colors that each pixel, or dot, on your screen can represent.  There are the direct video modes (Millions and Thousands in the Monitors control panel), the CLUT (or indirect) video modes (256 and 16 colors), and the monochrome modes (4-color and Black & White).

The direct video modes are so named because you can directly control the exact color of each pixel on the screen independently.  When you look at a pixel on your color screen, what you are actually looking at are three dots of light superimposed on top of one other: one red, one blue, and one green.  By varying the brightness of each of these three components, you can ÒcreateÓ virtually any color imaginable.  Thus, you can say that red, green, and blue are the three ÒprimaryÓ colors of your monitor, in much the same way that red, yellow, and blue are the three primary colors of your watercolor set; if you combine them in just the right proportions, you can get any other color you want.

In direct video modes, you specify for each pixel exactly how much red, blue, and green it should display on your screen.  This gives you complete control over the final result.  What then becomes important is how finely you can adjust the brightness levels of each of these components.  In AppleÕs Thousands mode, you can specify one of 32 possible brightness levels for each component, red, green, and blue, giving you 32 x 32 x 32 = 32,768 colors total.  In Millions mode, you get to choose one of 256 possible brightness levels for each component, or 256 x 256 x 256 = 16,777,216 colors.

When you scale back to the 256-color and 16-color video modes, this direct control over the color components isnÕt nearly as effective, since you would only be allowed at most 6 or 7 possible brightness levels in each component.  The MacintoshÕs solution to this problem lies in its use of color lookup tables (CLUTs).  A CLUT is a small table of colors (with 256 or 16 entries, in this case) chosen from any of the 16.7 million colors available in the Millions mode.  To display one of these colors, the Macintosh gives the display hardware a color index, say, from 0 to 255, which is then used to pick a corresponding color from the CLUT to display on your screen.

This way of doing things gives you tremendous flexibility in choosing your colors.  For example, on a 256-color system, you can design a color set with 256 levels of grayscales for your display just as easily as you can design a color set with 256 colors chosen at random from the 16.7 million available.  Of course, what you would obviously prefer is the ability to choose the optimal set of 256 colors for displaying a given image, and this is exactly what JPEGViewÕs two-pass color reduction feature does for you.

Finally, I should admit that the 4-color mode is also technically a CLUT mode, even though I called it a monochrome mode before.  The reason I grouped it in with the black & white mode is that your Macintosh requires every display to be capable of showing black and white, which leaves only 2 colors that are truly free to be chosen.  Since you canÕt even get the essential red, green, and blue with those 2 colors, it is best simply to choose two intermediate grayscales to give you the highest-quality image, leaving you with a monochrome display.


JPEGView and color sets

If at least one monitor in your system setup is operating in one of the CLUT modes Ñ either with 16 colors or with 256 colors Ñ then you can use JPEGViewÕs Colors menu to select a new color set for the current image.  There are currently four different color sets available: the System Colors (Y), the Grayscales (G), the Image Colors (J), and the color set produced with Two-Pass Color Reduction (T).

The first two options Ñ the System Colors and the Grayscales Ñ are color sets that youÕre probably familiar with already, though you may not have known it.  The former is simply the set of colors that youÕre used to seeing when youÕre in the Finder, while the latter is what you see when you switch your monitor into Grayscales mode in the Monitors control panel.  To see what sort of color selection is provided by these or any other sets of colors in JPEGView, you can make the Colors floating window visible.  As you switch color sets within JPEGView, the colors displayed in the floating window will change to reflect the new set of colors.

Some images that you load with JPEGView, in particular GIF images, contain a color set internally, defining which colors should be used to display the image.  If JPEGView finds such a color set within an image, it will allow you to choose those colors via the Image Colors item in the Colors menu.  If no color set information is provided with the image, this menu item will not be available.

Finally, for images that contain Thousands and Millions of colors, JPEGView provides the ability to generate a custom color set for the image, produced by examining the distribution of colors within the image.  This is known as two-pass color reduction (sometimes called color quantization), for the simple reason that JPEGView must first scan through the entire image once to determine the proper color set, and then the image must be scanned a second time during the actual display process.  For the other color sets available, the display process only requires one pass through the image.

Two-pass color reduction will very nearly always give you far better results than any of the other color options.  The main downside to this feature is that it simply requires more time to display an image.  JPEGView leaves the default choice up to you; see the Preferences Settings chapter for more details on how you can control this behavior.  In addition, for JFIF and PICT images, once you have performed two-pass color reduction, JPEGView allows you to save the calculated color set information with the image, so that you donÕt need to perform the calculation again.  The chapter Saving Images explains how you can do this.


Dithering Ñ how and why it works

No matter which color set is selected, you also have control over whether or not to dither the resulting image via the Dithering item in the Colors menu (B).  Dithering is a technique that ÒsimulatesÓ colors that arenÕt available in the current color set by modifying the colors of the surrounding pixels.  As a simple example of how dithering accomplishes this feat, consider a random pixel in an image, whose color is supposed to be purple.  If the current color set contains a color that exactly matches that purple, that particular color is chosen, and there are no problems; dithering has no net effect.

But imagine a color set had no purples at all, but did have shades of red and blue.  Without dithering, that purple pixel would appear as a red pixel, since red is the closest match we can get to purple with the current (red and blue) set of colors; unfortunately, this wonÕt look very purple in the end.  Now turn dithering on.  Dithering finds the closest match Ñ red Ñ just as before, but then it looks at the difference between this closest match and the original color we wanted.  In this case, dithering would notice that there should have been more blue in this pixel, and, to compensate for this loss, it would add a little bit of blue to the neighboring pixels.

Now imagine that one of these neighboring pixels, whose blue component has just been increased, was originally the same purple as the original pixel.  With the adjustment, it is now a rather bluish-purple color.  So, when it comes time to find the closest match to this new color, we will choose blue rather than red, because blue is closer to bluish-purple than red is.  Again, we calculate the difference between the chosen blue color and the original color we wanted (bluish-purple), and see that some red was missing.  Dithering will thus add a little bit of red to pixels surrounding this one, and proceed on to the next pixel, continuing this for all the pixels in the image.

In the end, an area of solid purple Ñ which cannot be displayed as such with our example blue and red color set Ñ will appear as a pattern of alternating blue and red dots, which will actually give the appearance of purple if you donÕt look too closely.  In this way, dithering approximates colors by finding what was missing in the current pixel and distributing that difference to the surrounding pixel.  The advantage of dithering is that it effectively simulates more colors than your monitor can actually produce.  The disadvantage is that the image comes out a little less distinct, and can sometimes suffer from dithering artifacts.  The next section discusses this latter problem in more detail.


Scaling, dithering, and display quality

The MacintoshÕs 32-bit QuickDraw, which is the highly-optimized system responsible for all drawing operations that occur on your system, supports the ability to automatically perform dithering and scaling operations on an image.  This functionality makes it very easy to incorporate such features into an application program, and for most operations, it does an excellent job.  However, there are some limitations in QuickDrawÕs dithering that only become apparent when displaying images on 256-color screens using two-pass color reduction.

The problem is that the dithering algorithm used by QuickDraw works best when the available color set is not very well matched to the colors that are in an image, e.g., when an image is displayed with only 16 colors, or with 256 colors using the system colors.  When the available colors are a good match for the image colors, however, QuickDrawÕs dithering can produce dithering artifacts, or strangely discolored areas in the image.

In order to fix this problem of dithering artifacts, JPEGView implements its own dithering algorithm, which remedies the problem for most images.  Unfortunately, in order to support its own dithering, JPEGView must also support its own scaling, since the two operations are intricately coupled.  Although JPEGViewÕs high-quality dithering algorithm itself is fast Ñ almost as fast as QuickDrawÕs built in dithering Ñ JPEGView does not know how to scale an image as fast as QuickDraw.  Because of this, JPEGView supports two forms of scaling, in addition to QuickDrawÕs own routines.

Selecting which set of routines to use for drawing is accomplished through the Quality menu, available under the Colors menu.  Three options are available: Very High quality (Shift-V), High quality (Shift-H), and Normal quality (Shift-N).  These options are only useful (and only available) if the image is drawn on a screen with Thousands or more colors available, or on a screen with 256 colors if the image is drawn using either two-pass color reduction or the image colors.  In all other situations, the normal QuickDraw drawing is used.

What JPEGView calls Normal quality drawing is simply QuickDrawÕs standard routines, which give fast results with the possibility of the dithering artifacts mentioned above on 256-color screens.  Normal quality is also used when the current color set does not match the imageÕs colors very well, since QuickDrawÕs dithering does a fine job in this situation.

High quality drawing combines JPEGViewÕs improved dithering with a fast, but relatively coarse, scaling algorithm.  For most purposes, this fast scaling does a good job, and even gains you a small speed gain over the QuickDraw scaling youÕre used to.  High quality drawing is only available on 256 or Thousand-color screens; those with Millions of colors available wonÕt notice any dithering difference, and the scaling is visually a little worse than standard QuickDraw scaling, making High quality into a net loss over Normal quality for such users.

Very High quality drawing takes JPEGViewÕs improved dithering and adds a slower, but very accurate scaling routine.  For those with an eye for detail, the scaling used in Very High quality drawing is often better than standard QuickDraw scaling, making this quality level useful for those with Thousands and Millions of colors as well.  Note that when an image is not scaled, High quality and Very High quality are identical, since the difference lies only in the scaling algorithm used.

To help see the difference that JPEGViewÕs improved dithering can make, try opening the ÒParrots JPEGÓ image that was included with JPEGView.  View it using both Normal and High quality on a 256 color screen with two-pass color reduction turned on; the difference should be clear.  Note that ÒParrots JPEGÓ is an extreme example of the problems encountered with QuickDrawÕs dithering; donÕt expect such dramatic improvements on all images!