Spring 91 - QUICKDRAW'S COPYBITS PROCEDURE: BETTER THAN EVER IN SYSTEM 7.0
QUICKDRAW'S COPYBITS PROCEDURE: BETTER THAN EVER IN
SYSTEM 7.0
KONSTANTIN OTHMER
With System 7.0 comes a major revision of QuickDraw. The CopyBits procedure,
QuickDraw's image-processing workhorse, has had some bugs fixed and some features
added. This article gives a brief overview of changes to QuickDraw and then brings you
up to speed on changes to CopyBits.
We're seeing some impressive examples of computer image processing at the movies
these days. Special effects in movies such as Star Trek III , Willow , Back to the
Future , Arachnophobia , Ghost , andThe Abyss were either assisted or completely
generated by computer. While QuickDraw TM does not have the built-in ability to
perform the highly specialized image processing necessary to produce the types of
effects seen in those movies, producing such effects is not beyond the capability of the
Macintosh. You can write custom routines to perform such operations as rotation,
warping, and advanced filtering.
Before you get to that advanced stuff, though, you need to be familiar with QuickDraw's
basic image-processing capabilities, which provide the starting point for an effects
toolbox. With QuickDraw's CopyBits procedure, you can perform several standard
image-processing operations, such as resizing (by stretching, shrinking, or clipping
the image), colorizing, or changing the pixel depth.
CopyBits is better than ever in System 7.0. Improvements to transfer operations and
colorizing mean enhanced results. And it's now easier to use a search procedure to
alter colors. We'll look at these improvements in detail and will see samples of
CopyBits in action after a brief overview of how QuickDraw has evolved.
A BRIEF HISTORY OF QUICKDRAW
There have been a number of QuickDraw versions since the introduction of the
Macintosh in 1984. Table 1 summarizes the major QuickDraw versions. Many minor
revisions and bug fixes have also occurred along the way, of course.
Table 1 A Summary of Major QuickDraw Versions
Date Version Where Documented
January 1984 Original B&W QuickDraw Inside Macintosh
(Macintosh 128K) Volume I
January 1986 B&W QuickDraw Inside Macintosh
(Macintosh Plus) Volume IV
March 1987 Color QuickDraw Inside Macintosh
B&W QuickDraw Volume V
(Macintosh II)
May 1989 32-Bit QuickDraw v. 1.0 Inside Macintosh
Volume VI
September 1989 32-Bit QuickDraw v. 1.1 Inside Macintosh
(System 6.0.4, Macintosh Volume VI
IIci, IIfx, IIsi, and LC)
March 1990 32-Bit QuickDraw v. 1.2 Inside Macintosh
(System 6.0.5) Volume VI
April 1991 Color QuickDraw Inside Macintosh
B&W QuickDraw QuickDraw Volume VI
(System 7.0)
Note: QuickDraw is revised for system releases and, in the past, major revisions have
coincided with hardware releases. In the future, it's likely that major system releases
will be independent of hardware releases.
The version of black-and-white QuickDraw that accompanied the Macintosh Plus
system added the SeedFill, CalcMask, and CopyMask calls. The Macintosh II revision
introduced Color QuickDraw (which supported indexed devices only) and revised the
existing black-and-white QuickDraw (which is still used on 68000-based machines)
to display pictures (data of type 'PICT') created in the color version.
Version 1.0 of 32-Bit QuickDraw, released as an INIT at the Developers Conference in
1989, added direct-color capability to QuickDraw. No black-and-white QuickDraw
update was provided. Version 1.1 of 32-Bit QuickDraw is in ROM on the Macintosh
IIci, IIfx, IIsi, and LC. Version 1.2 of 32-Bit QuickDraw, released as an INIT with
System 6.0.5 and patched by the system on machines that have version 1.1 in ROM,
added the OpenCPicture call and the capability of recording font names into pictures.
The System 7.0 version of Color QuickDraw integrates the functionality of 32-Bit
QuickDraw into all Color QuickDraw machines and adds a variety of new features and
bug fixes. In addition, System 7.0 has a new version of black-and-white QuickDraw
that includes some of Color QuickDraw's functionality. (See "QuickDraw Features New
in System 7.0" on the next page for more information.)
ABOUT COPYBITS
The CopyBits procedure, along with the CopyMask and CopyDeepMask calls, is the core
of QuickDraw's image-processing capability. CopyBits transfers a bit image from one
bitmap to another and clips the result to a specified area. With CopyBits you can
perform such image- processing operations as resizing (by stretching, shrinking, or
clipping the image), colorizing, and changing the pixel depth. You can use it to display
on-screen the contents of an off-screen buffer.
In the System 7.0 version of QuickDraw, as in previous versions, the CopyBits
procedure is defined as
PROCEDURE CopyBits (srcBits,dstBits: BitMap;srcRect,dstRect: Rect;
mode: INTEGER; maskRgn: RgnHandle);
In the original black-and-white QuickDraw, CopyBits used six explicit parameters
(srcBits, dstBits,srcRect, dstRect, mode, and maskRgn) and one global variable
(thePort). The introduction of Color QuickDraw required an additional global variable,
theGDevice, which is used to determine color information for the destination.
Although the number of variables used by CopyBits hasn't changed from earlier
QuickDraw versions, several things have changed:
• The way transfer operations specified by the mode parameter are
performed has changed to make their results predictable regardless of
whether the destination device uses indexed or direct color.
• The way the notCopy transfer operation is performed has changed to
improve the quality of color inversions.
• Dithering has been extended to improve the quality of images resulting
from depth conversion, color mapping, or resizing.
• The way colorizing is performed has changed to make the results
predictable for all pixel depths.
• The use of search procedures has been extended and now provides an
easier mechanism for altering colors.
In the following sections we'll take a closer look at each of these improvements. We'll
then watch CopyBits in action as we stretch and colorize a gray ramp, and perform
RGB and CMY color separations.
IMPROVEMENTS TO TRANSFER OPERATIONS
The appearance of the result of the CopyBits procedure is determined by the mode
parameter. This parameter specifies which source transfer mode is to be used and
whether or not dithering should occur during transfer operations. Improvements to
CopyBits in System 7.0 make the results of transfer operations independent of
whether the destination device uses indexed or direct color. The new CopyBits also
improves the results of color inversions and extends the use of dithering.
RESULTS INDEPENDENT OF DESTINATION DEVICE
Before System 7.0, the transfer mode specified in CopyBits' mode parameter was
implemented directly by one of eight transfer operations: Copy, Or, Xor, Bic, notCopy,
notOr, notXor, and notBic. For each bit in the source bitmap to be drawn, QuickDraw
found the corresponding bit in the destination bitmap, performed the transfer
operation on the pair of bits, and stored the resulting bit into the bit image.
This method extended naturally to the use of indexed devices in Color QuickDraw. But
with the introduction of 32-Bit QuickDraw, which supported both indexed and
direct-color devices, the results of the Or, Bic, and Xor transfer operations became
dependent on the type of destination device. Using the Or operation with direct
color--where 0 represents black and $FF represents white--resulted in pixels that
went toward white, while using the Or operation on indexed pixels-- where indexes
typically range from 0 (white) to $FF (black)--had a result that went toward black.
Bic and Xor had similar problems.
For example, many applications use the srcXor transfer mode--defined in Inside
Macintosh Volume I as inverting destination pixels that are black in the
source--when dragging a selection. In the original Color QuickDraw, this operation
was performed correctly. In 32-Bit QuickDraw, on the other hand, destination pixels
that were white in the source were inverted on direct-color devices.
In the new Color QuickDraw, the transfer modes srcOr, srcBic, and srcXor are still
undefined for color pixel values, but behave correctly--that is, as documented in
Inside Macintosh Volume I--with respect to black and white regardless of whether the
destination device uses indexed or direct color. The way these modes work now as
compared to the way they worked in 32-Bit QuickDraw version 1.0 for direct sources
copied to a direct-color device is shown in Figure 1.
Figure 1 Results of Transfer Modes for Direct Source to Direct Destination
For all devices now, the srcOr transfer mode produces a black result where the source
is black. The srcXor transfer mode inverts destination pixels where the source is
black. And srcBic (which stands for "bit clear" but may be easier to remember as
"black is changed") produces a white result where the source is black. All three modes
leave the destination pixels under the white part of the source unchanged. (Note that
using these transfer modes for colored sources, while legal, does not always produce
well-defined results.)
INVERSIONS IN COLOR SPACE
Before System 7.0, notCopy was performed by inverting source index values. In
System 7.0, the inversion takes place in color space, giving a much more pleasing
result. Note that the trade-off for higher quality in this case is reduced speed: this
operation is somewhat slower than in previous versions.
Using notSrcCopy mode to highlight items when they've been selected produces good
results on screens of all depths, although it suffers from gray mapping to gray.