13. How To Tutorial # 1: Outputting good quality 8-bit images
NuGraf has several built-in facilities that compress 24-bit rendered
output to 256 or less colors. The most versatile and accurate of these is the Wu
Quantization Algorithm which chooses a reduced color representation of an image given
a fixed number of colors; the resulting 8-bit image usually looks as good as the original
24-bit image.
As an example, the following C code shows how to output an 8-bit
colormapped TIFF image which was reduced with the Wu quantization algorithm:
The "colormaplength=256'' option tells the TIFF driver
to output the image as a 256-color colormapped image rather than a 24-bit image; this
statement is not required by implicit colormapped output drivers such as the GIF file
driver or the VGA frame buffer driver. The Wu quantization algorithm reduces the 24-bit
rendered output down to the number of colors specified by the Nt_NUMCOLORS option,
or if that value is set to -1L then it reduces the output to the maximum number of colors
that the current colormapped output device can handle (which is 256 for the TIFF driver
statement shown above). The quantization algorithm can compress a 24-bit image down to a
minimum of 2 colors and a maximum of 256 colors.
The second method that can be used to compress output to 256-or-less
colors is to map the 24-bit image's colors directly into a predefined colormap. This is
the method used if the renderer determines that an output driver can only handle
colormapped data (the Wu Image Quantization algorithm must also be disabled). The colormap
is segmented by the renderer into a specific number of shades of red, green and blue. For
example, if only 252 output colors are available then the renderer will choose 6 shades of
red, 7 shades of green and 6 shades of blue (you can override the renderer's choice by
specifying the Nt_RGBSHADES option to the command). To improve the appearance of
the resulting 8-bit image you should also enable the Nt_DITHER option.
As an example, the following C code shows how to configure the
output driver mechanism so that it reduces a 24-bit image down to 64 colors using 4 shades
each of red, green and blue (as chosen internally by the renderer). Dithering is also
enabled to increase the apparent number of colors in the image. The output is sent to a
colormapped TIFF file.