January 90 - REALISTIC COLOR FOR REAL-WORLD APPLICATIONS
REALISTIC COLOR FOR REAL-WORLD APPLICATIONS
BRUCE LEAK
32-Bit QuickDraw the new extension to the Macintosh graphics system software,
enables the manipulation of 16- and 32-bit color data. This article gives details on
the new offscreen pixMap support, the improved Palette Manager, color to grayscale
conversion through luminosity mapping, and advanced dithering from 16 and 32 bits
per pixel to lower bit depths.
On the Macintosh, Apple's graphics software (QD) lets a programmer work with a
high-level graphics model that is independent of the physical display device. This gives
Apple the option to take advantage of new features and technologies without requiring
application developers to rewrite their code.
32-Bit QuickDraw extends QuickDraw to encompass the full range of displayable
color. Although today's monitors are typically capable of showing a full spectrum of
color or grays, it is the video card and system software of a computer that control the
number of colors or shades of gray on the screen. A 1-bit video card allows 2 colors
or shades (typically black and white), 2 bits gives 4, 4 bits gives 16, 8 bits gives
256, and 24 bits gives over 16 million colors.
Color QuickDraw always supported the description of nearly unlimited colors (248)
but constrained images and screens to any 256 of the expressible colors. 32-Bit
QuickDraw significantly increases the standard number of colors available for an
application while maintaining speed and affordability.
32-Bit QuickDraw--which carries 24 bits of color information--consists of three
files:
• a General cdev that works more effectively with the system, whether the
other pieces are installed or not
• a Monitors cdev extended for 32-bit addressed video cards
• a 32-Bit QuickDraw Init file, which contains the new QuickDraw
software
NEW FUNCTIONALITY IN 32-BIT QUICKDRAW
This is the laundry list of 32-Bit QuickDraw features. They are listed in relative
order of importance, but some may be more important to you, depending on whether
you've thought about them before.
Support for 32-bit-per-pixel graphics The pixel data has 8 bits each in red,
green, and blue and an 8-bit alpha channel. Only 24 of the 32 bits per pixel are used
by QuickDraw. 24 bits per pixel--over 16 million colors--gives you pretty much
the maximum number of colors the eye can distinguish. When the display mode of the
graphics system supports this much color, you don't have to restrict the application or
user to a particular set or palette.
Support for 16-bit-per-pixel graphics (1 alpha-5-5-5)The pixel data
has 5 bits each in red, green, and blue and a 1-bit alpha channel. 16 bits per pixel--
or over 32,000 colors--is considered by many to be sufficient for high-quality
graphics. Because 16 bits per pixel requires less memory than 32 bits per
pixel,displays can use less expensive hardware.
Dithering Dithering of 32- and 16-bit images to lower color
resolutions --1, 2, 4, and 8 Color dithering is similar to creating a color that looks
like orange by placing red and yellow pixels close together. If the display is limited to
a certain number of colors, the Macintosh can take images rich in color information
and produce a close match at lower color resolutions, giving the appearance of more
color by combining the limited number of colors in an effective manner.
Gray-level representation with luminosity In grayscale mode, QuickDraw
will map a color to its nearest luminance value or lightness. This produces superior
images, even with 16 gray levels. It's hard to imagine how good this can be without
seeing it. so we'll show you just how great it is in these next two pictures
An improved Palette Manager The Palette Manager now allows application
developers to have more control over their color environment by improving multiple
monitors support, supporting a highlight color as one of four colors in 2- bit mode,
setting up a true grayscale look-up table on monochrome devices, and restoring the
color environment when an application quits. The new Palette Manager also directly
supports color table animation of pixel images in a device-independent manner (see
"All About the Palette Manager" in this issue for details).
New offscreen support These new routines can isolate developers from device
dependencies by extending offscreen pixMap support. Developers typically use
offscreen support to buffer drawing for fast, seamless updates to the screen. Offscreen
imaging is also convenient for custom rendering algorithms that assume a particular
frame buffer configuration--since requiring a particular video display mode is not a
user-friendly solution.
Improved graphics performance in all bit modes 32-Bit QuickDraw provides
performance improvements in region-clipped pattern fills and bit blits, such as
updating the desktop pattern or resizing a window. Improved rescaling of images to
smaller sizes When 32-bit-per-pixel images are resized to smaller sizes, pixels are
combined using an averaging technique to yield recognizable thumbnail-sized
images--an important improvement for the postage stamp market.
New routine to get regions from bitmaps Getting a bitmap from a region is
easy--just paint the region. The new routine BitmapToRgn provides the inverse
transformation.
Alpha channel movement 32-Bit QuickDraw supports routines that use up to 24
bits of color. The additional 8 bits of information are typically used by application
developers as an alpha channel or transparency mask. QuickDraw now moves the extra
8 bits around without adding functionality. Note that alpha channel memory may not
actually be present on the video card. The new Monitors cdev The new cdev improves
the Monitors interface and adds advanced features. Now developers can easily modify
the cdev through astandard programming interface.
Support for color PostScript printing Apple's new LaserWriter 6.0 driver
now offers a standard mechanism for printing the high-quality color that 32-Bit
QuickDraw provides.
Color Picker changes Most of the changes to the Color Picker are related to the
user interface. In the former version of the Color Picker, if the where parameter was
set to (0, 0), then the dialog was centered on the main device. For obvious reasons,
this method did not work well if the main device was not a color device. In the new
Color Picker, if you want the dialog centered on the best device--that is, the device
with the highest bit depth, regardless of color support--then you must pass (-1, -1)
as the where parameter.
Compression for file formats 32-bit and 16-bit data are normally compressed
when they are in PICT files so that storage on disk takes significantly less space than
the original data.
Support for very large frame buffers Video cards that require more than 1 MB
of memory, such as a 1280 x 1024 x 8-bit deep card, now work in a standard way
with the system.
Changes in rowbyte restrictions 32-Bit QuickDraw relaxes the restriction that
rowbytes be less than $2000. However, the new limit of $3FFE may still be a
problem with large pixel maps at 32 bits per pixel. Early releases of the 32-Bit
QuickDraw documentation incorrectly reported that rowbytes had a limit of $8000.
Not to mention, as you always expected... 32-Bit QuickDraw has implications
for several familiar aspects of working with the Macintosh. PICT, the Macintosh
graphics resource and file format, has been extended to support 16-bit and 32- bit
data. The transfer modes that allow QuickDraw to blend different bitmaps and combine
their colors interactively in different ways have been extended so that they work at,
and between, all bit depths. Because images of any depth can be displayed at any depth,
you have ultimate flexibility to respond to the requirements of the task at hand. Colors
are mapped to an appropriate match when increasing or decreasing color depth of the
display.
WHAT CAN YOU DO WITH IT
To begin with, you can't use 32-Bit QuickDraw features if you don't know they're
installed. To check for 32-Bit QuickDraw, your application should ensure that Color
QuickDraw is present on the machine by calling SysEnvirons. Then, since 32-Bit
QuickDraw internally uses trap number $AB03, check to see if this trap is available
by comparing its trap address with that of the standard unimplemented trap $A89F. If
the two are the same, trap $AB03 is unavailable, and 32-Bit QuickDraw is not present.
You can use the following MPW C code fragment to test for Color QuickDraw and
32-Bit QuickDraw. If either test fails, tell the user.
#define QD32Trap 0xAB03
#define UnImplTrap 0xA89F
#define False 0
#define True 1
PutUpInformativeMessage()
{
printf("\n 32-Bit QuickDraw is not implemented.");
}
QD32Exists()
{
short error;
Boolean result = False; /* Assume not there */
SysEnvRec theWorld;

error = SysEnvirons (2, &theWorld);

if (theWorld.hasColorQD)
result = (NGetTrapAddress (QD32Trap, ToolTrap) !=
NGetTrapAddress (UnImplTrap, ToolTrap));

return result;
}
main()
{
Boolean QD32IsImplemented;
QD32IsImplemented = QD32Exists();
if (!QD32IsImplemented)
PutUpInformativeMessage();
}
Direct pixMaps 32-Bit QuickDraw supports two new pixel formats, corresponding
to 32-bit pixels and 16-bit pixels. In each case, the pixel's color is specified by the
pixel value directly. The pixel value is not an index into a color look-up table. In a
pixMap, this is specified by setting the pixelType field to RGBDirect = 16.
Before 32-Bit QuickDraw, when each pixel was a single value representing an index
into a color table, the cmpCount field was always equal to 1. With RGBDirect pixels,
each pixel contains three components, one each for the intensities of red, green, and
blue therefore, cmpCount should be set to 3.
pixelType = 16; {RGBDirect}
pixelSize = 32; {Must be a power of 2}
cmpCount = 3; {Three components: Red, Green, Blue}
cmpSize = 8; {8 bits for each component}
pmVersion = 0; {Must be set for future compatibility}
pmTable {Handle to color table with 1 entry}
ctSeed = 24; {CmpCount * CmpSize}
ctFlags = 0; {No special flags}
ctSize = 0; {Zero based count of entries}
ctTable = {Space for one color spec}
pixelType = 16; {RGBDirect}
pixelSize = 16; {Must be a power of 2}
cmpCount = 3; {Red, Green, Blue
cmpSize = 5; {5 bits for each component}
pmVersion = 0; {Must be set for future compatibility}
pmTable {Handle to color table with 1 entry}
ctSeed = 15; {CmpCount * CmpSize}
ctFlags = 0; {No special flags}
ctSize = 0; {Zero based count of entries}
Since the Window Manager, as well as many applications, examines the ctSeed of
screen pixMaps, the pmTable field for a direct device pixel map should always contain
a valid handle with a color table header. For consistency, the ctSeed should be equal to
cmpCount * cmpSize--although you can have the seed equal a unique value. If the seed
value is the same across devices, then window moves will copy an image with CopyBits
otherwise, the Window Manager will generate an update event. The ctFlags and ctSize
values should be set to zero.
PixPats In addition to the standard 8 x 8 foreground/background QuickDraw pattern
filling we have all come to know and love, Color QuickDraw supports drawing objects
with tiled pixel images or pixPats (short for pixel patterns). Although pixPats
actually contain a pixMap that is capable of describing a pixel image of any size and
depth, only bit depths 1 through 8 and dimensions that are a power of two are
supported. Since pixPats contain color information, the grafPort's foreground and
background colors are ignored when drawing with pixel patterns. Future versions of
Color QuickDraw will support 16- and 32-bit pixel patterns.
Color QuickDraw expands a pixPat to the depth of the target device before drawing.
Make sure that enough memory is available for the expanded pattern. The current
32-Bit QuickDraw limits the volume (area times depth) of an expanded pixPat to less
than 64K. Hence a 128 x 128 pixPat (16K @ 8 bits deep) will work at 8 bits per
pixel but draw incorrectly when rendered at 32 bits per pixel. A 64 x 128 pixPat
(32K @ 32 bits deep) will work at any depth. Another type of pixPat, the RGB
pattern, is completely described by a single RGB Color. When drawing an object with
an RGB pattern, Color QuickDraw computes an ordered dither matrix that most
accurately represents the RGB Color on each screen it intersects. Since dithering is