GRAPHICAL TRUFFLES

FOUR COMMON GRAPHICS ANSWERS

BILL GUSCHWAN

QuickDraw regions were almost lost when Bill Atkinson crashed his car and nearly killed himself.
Considering that Steve Wozniak also crashed his airplane, crashing must be a hallmark of Apple
genius. I'm no Macintosh wizard, though I did crash my car once. To aid and abet other non-
wizards, I'll divulge four cool answers that apply to nearly any QuickDraw question:

Check the graphics state.

Check the QuickDraw version.

Do it off-screen.

Use the bottlenecks.

They may not answer your questions completely, but they'll probably get you partway there.

CHECK THE GRAPHICS STATE
Whenever you call a QuickDraw routine, its behavior depends heavily on the state of the machine at
the time of the call: things like the pen size, transfer mode, and color environment all affect the
drawing. Most of the state information QuickDraw depends on can be found in two handy locations
-- the current grafPort and the current GDevice.

The grafPort maintains state information for the pen, the text, and the bitmap (or pixel map) to draw
into. The GDevice defines the color environment, among other things. This information is accessed
by many QuickDraw routines. For example, the LineTo routine draws a line from the current pen
location to the point you pass to the routine, using the current pen size, pattern, and transfer mode;
all these values are fields of the current grafPort. Because most QuickDraw routines use these
"implied" parameters, you can't fully understand the behavior of a QuickDraw routine without
knowing about them.

The current grafPort also defines where your drawing will occur. Even though you call a routine, it
may not draw, because QuickDraw applies a rectangle -- the portRect -- and two regions -- the
visRgn and clipRgn -- to your drawing. No drawing will occur outside the intersection of these
areas. QuickDraw places control of the clipRgn in your hands, first initializing it to be wide open (a
rectangular region that covers the entire QuickDraw coordinate space). If your grafPort is in a
window, control of the visRgn is placed in the hands of the Window Manager. (A region is a truly
marvelous concept, a compact description of strange shapes that can be extended and changed
dynamically. It's a good thing Bill Atkinson survived his crash.)

Let's try applying this first answer to a common QuickDraw question: Why can't you nest calls to
OpenPicture? Well, as you may know, when you call OpenPicture to begin saving picture data, a
handle is created to store the picture information until the corresponding ClosePicture call. This
handle is kept in the picSave field of the current grafPort. If you nest a second OpenPicture call,
where in the grafPort will you store the newly created handle? Answer: There is no place, so you
can't nest OpenPicture calls. Because so many other Managers rely on QuickDraw, this answer will help with questions about
other Managers as a bonus.

CHECK THE QUICKDRAW VERSION
There are currently seven versions of QuickDraw.
You can find out which version is available using Gestalt, and that's usually the most important thing
for your code to know about. But many developers also want to know which machine and system
software combinations produce which versions of QuickDraw. For example, some developers code
for 32-Bit QuickDraw and want to inform their users of the minimum Macintosh machine
requirement. The ROM version, extensions, and system software all combine to affect which version
of QuickDraw is available.

ROM versions of QuickDraw can be neatly categorized into three classes of machines: black and
white, Color QuickDraw, and "ci class." The original Macintosh and the Macintosh 512K, Plus,
Portable, SE, and PowerBook 100 are examples of the black-and-white class. The Macintosh II, IIx,
IIcx, and SE/30 fall into the Color QuickDraw (256K ROM) class. The Macintosh IIci, IIsi, LC II,
IIfx, and later models belong to the ci class (>256K ROM).

Black-and-white class. There are only two common versions of QuickDraw on black-and-white
machines today: original black-and-white QuickDraw and System 7 black-and-white QuickDraw.
(The uncommon ones are present only with pre-Macintosh Plus ROMs or system versions earlier
than 4.2.) When System 7 is installed on a machine of this class, it installs some new routines so that
a few Color QuickDraw routines can be used (you get 1-bit GWorlds, you can correctly display
pictures containing direct-color information, you can create version 2 pictures, and so on).

Black-and-white QuickDraw is documented inInside MacintoshVolumes I and IV. For a
comprehensive list of the routines that System 7 adds to black-and-white QuickDraw, see
"QuickDraw's CopyBits Procedure: Better Than Ever in System 7.0" in develop Issue 6.

Color QuickDraw class. This class of machines has 8-bit Color QuickDraw built into ROM, so it will
always be there regardless of the system version. When these machines are running system versions
earlier than System 7, they can be extended to handle direct color through the use of the 32-Bit
QuickDraw INIT. Finally, if they're running System 7, System 7 Color QuickDraw is available.

Inside MacintoshVolume V describes 8-bit Color QuickDraw. For documentation on the various 32-
Bit QuickDraw versions, including System 7 Color QuickDraw, the best place to look is Inside
Macintosh Volume VI. If you really need to know the differences in capabilities among the versions,
32-Bit QuickDraw v. 1.0 is covered in "Realistic Color for Real-World Applications" in develop Issue
1, and the features added in 32-Bit QuickDraw v. 1.2 are documented in the Tech Note "32-Bit
QuickDraw: Version 1.2 Features."

ci class. This class of machines has only three possible QuickDraw versions. The least common
version, 32-Bit QuickDraw v. 1.01, is found on a IIci running system software version 6.0.4. The
other machines in this class that can run System 6 need at least version 6.0.5, which will patch in 32-
Bit QuickDraw v. 1.2. Finally, System 7 provides its own version of Color QuickDraw.

Again, for documentation on the various 32-Bit QuickDraw versions, including System 7 Color
QuickDraw, seeInside MacintoshVolume VI.

In the GestaltEqu.h header file, you'll find Gestalt values for six QuickDraw versions:

One of these -- gestalt32BitQD11 -- will never be returned, so this list accounts for only five of the
total of seven versions. The sixth is 32-Bit QuickDraw
v. 1.01, mentioned above, which returns the Gestalt value 0x201 but doesn't have a gestalt constant
defined for it. The seventh is System 7 with a black-and-white machine: You'll need to check for
both black-and-white QuickDraw (gestaltOriginalQD) and System 7 (gestaltSystemVersion greater
than or equal to $0700). If both are true, you're running System 7 black-and-white QuickDraw.
That's the only way to tell.

Table 1 shows all the possible combinations, in one handy location.

Table 1Possible Combinations of ROM Versions and System Software Versions

ROM Class

System Version

Gestalt Value

Black-and-white class

Pre-7.0

gestaltOriginalQD

7.0 and later

gestaltOriginalQD and gestaltSystemVersion = $0700 or greater

Color QuickDraw class

Pre-7.0, no INITs

gestalt8BitQD

6.0.3 or 6.0.4, and

gestalt32BitQD

32-Bit QuickDraw INIT v. 1.0

System 6 from 6.0.5 on, and

gestalt32BitQD12

32-Bit QuickDraw INIT v. 1.2

7.0 and later

gestalt32BitQD13

ci class

6.0.4

gestalt32BitQD + 1

System 6 from 6.0.5 on

gestalt32BitQD12

7.0 and later

gestalt32BitQD13

Exactly which permutations you need to code for depends entirely on what you're doing, but
typically the major divisions are color versus black-and-white, direct color versus indexed color, and
GWorlds versus no GWorlds. Whenever possible, of course, you should make decisions in your code
based on the QuickDraw version rather than on the specific machine configuration.

DO IT OFF-SCREEN
Off-screen environments give you explicit and total control over an image. Since the image and its
associated data structures are no longer tied to a physical device, many of the complexities and
limitations of QuickDraw are reduced, and your hands -- previously tied tightly behind your back --
are now freed. You'll typically manipulate your image off-screen and then use CopyBits to move the
image to the screen. The FX snippet on this issue's CD provides a robust demonstration of some
snazzy CopyBits effects, and there's a nice overview of how to perform animation using off-screen
graphics environments in the Graphical Truffles column ("Animation at a Glance") in develop Issue
12.

Using CopyBits and off-screen environments for speed is covered eloquently in Konstantin Othmer
and Mike Reed's article "Drawing in GWorlds for Speed and Versatility" in develop Issue 10, so I
won't dwell on it here. Also, the Tech Note "Principia Offscreen Graphics Environments" gives
details for creating off-screen environments on machines without 32-Bit QuickDraw (see the
discussion above).

The point is this: when faced with a question in the "How do I . . ." category, try this answer on for
size first. That may be as far as you need to go.

USE THE BOTTLENECKSQuickDraw routines are easily customizable, which can be incredibly useful; however, this feature is
typically underused. (In fact, most of the Macintosh is customizable. There ought to be a whole
chapter in Inside Macintosh on customization; there are so many places in the OS that you can
intercept, you could probably patch out the whole OS if you were so inclined.) You can replace
QuickDraw's "guts" with viscera of your own design, completely (and reversibly) transforming
QuickDraw's functionality.

Here's one example of how this can be useful: Let's say we want to find out the exact colors used in a
picture that contains innumerable colors. We'll be drawing the picture to an 8-bit color monitor, and
we want to manually select the best 256 colors, replacing the default color table that DrawPicture
uses. There are two methods of getting the colors used in a PICT: use the System 7 Picture Utilities
Package, or do it yourself. The Picture Utilities Package is available only in System 7, so if we want
to run on earlier systems, our only choice is to do it ourselves. We do that by using the bottlenecks.

You can replace all the bottlenecks with no-ops except for a few carefully selected ones, then draw
the picture. Your replacement bottlenecks will be able to watch all the picture data go by and can
keep track, say, of the colors used in the picture. (Two sample programs on the CD,
CollectPictColors and GMundo, demonstrate this technique.) So, for instance, to draw our many-
colored picture with a custom-picked set of 256 colors, we actually have to draw the picture twice:
the first time, we replace the bottlenecks, allowing us to use -- collect, extract, or read -- the colors
in the picture. We can then set up the destination cGrafPort with the colors we want to show,
restore the bottlenecks, and draw the picture again, this time to actually image it into the destination
cGrafPort.

THAT'S IT FOR NOW When you're faced with a question about QuickDraw, try running through the answers in this
column first, to see if any of them fit. Is the state of the machine at the time of the call different than
you assumed? Did you check the QuickDraw features and version? Can you do it off-screen? Can
you intercept processing at the bottleneck level to customize QuickDraw's routines? It's likely that
one of these answers will help.

BILL GUSCHWAN (AppleLink ANGUS) asked Howard Roark to dialog with him: "So, Angus, you ditched med school to
become a protector of the dogcow? I love you, man." Angus: "Well, Howard, as Tori Amos would say, 'Sometimes I hear
my voice and it's been here silent all these years.'" Howard: "You know, I ditched architecture school, not unlike David
Byrne. Speaking of Talking Heads, you got kicked out of one of his concerts because you wanted to dance." Angus:
"Words are very unnecessary, they can only do harm, so I dance." Howard: "Even your idol Wittgenstein went back to
school. What about you?" Angus: "As you know, Howard, even Atlas shrugged." *

Software Updates via MacUpdate

Fantastical 2.3.2 - Create calendar even...

Fantastical 2 is the Mac calendar you'll actually enjoy using. Creating an event with Fantastical is quick, easy, and fun:
Open Fantastical with a single click or keystroke
Type in your event... Read more

PCalc 4.4.4 - Full-featured scientific c...

PCalc is a full-featured, scriptable scientific calculator with support for hexadecimal, octal, and binary calculations, as well as an RPN mode, programmable functions, and an extensive set of unit... Read more

Alfred 3.2.1 - Quick launcher for apps a...

Alfred is an award-winning productivity application for OS X. Alfred saves you time when you search for files online or on your Mac. Be more productive with hotkeys, keywords, and file actions at... Read more

OmniPlan 3.6 - Robust project management...

With OmniPlan, you can create logical, manageable project plans with Gantt charts, schedules, summaries, milestones, and critical paths. Break down the tasks needed to make your project a success,... Read more

Backblaze 4.2.0.990 - Online backup serv...

Backblaze is an online backup service designed from the ground-up for the Mac. With unlimited storage available for $5 per month, as well as a free 15-day trial, peace of mind is within reach with... Read more

AppDelete 4.3.1 - $7.99

AppDelete is an uninstaller that will remove not only applications but also widgets, preference panes, plugins, and screensavers along with their associated files. Without AppDelete these associated... Read more

Apple GarageBand 10.1.4 - Complete recor...

The new GarageBand is a whole music creation studio right inside your Mac -- complete with keyboard, synths, orchestral and percussion instruments, presets for guitar and voice, an entirely... Read more

Adobe Lightroom 6.8 - Import, develop, a...

Adobe Lightroom is available as part of Adobe Creative Cloud for as little as $9.99/month bundled with Photoshop CC as part of the photography package. Lightroom 6 is also available for purchase as a... Read more

iMazing 2.1.3 - Complete iOS device mana...

iMazing (was DiskAid) is the ultimate iOS device manager with capabilities far beyond what iTunes offers. With iMazing and your iOS device (iPhone, iPad, or iPod), you can:
Copy music to and from... Read more

Little Snitch 3.7.1 - Alerts you about o...

Little Snitch gives you control over your private outgoing data.
Track background activity
As soon as your computer connects to the Internet, applications often have permission to send any... Read more

Latest Forum Discussions

Galaxy on Fire 3 - Manticore brings the series back for another round of daring space battles. It's familiar territory for folks who are familiar with the franchise. If you've beaten the game and are looking to broaden your horizons, might we... | Read more »

The best apps for your holiday gift exch...

What's that, you say? You still haven't started your holiday shopping? Don't beat yourself up over it -- a lot of people have been putting it off, too. It's become easier and easier to procrastinate gift shopping thanks to a number of apps that... | Read more »

MyTona, based in the chilly Siberian city of Yakutsk, has brought a little festive fun to its hidden object game Seekers Notes: Hidden Mystery. The Christmas update introduces some new inhabitants to players, and with them a chance to win plenty of... | Read more »

PINE GROVE 1.0
Device: iOS Universal
Category: Games
Price: $1.99, Version: 1.0 (iTunes)
Description:
A pine grove where there are no footsteps of people due to continuous missing cases. The case is still unsolved and nothing has... | Read more »

Niantic teases new Pokémon announcement...

After rumors started swirling yesterday, it turns out there is an official Pokémon GO update on its way. We’ll find out what’s in store for us and our growing Pokémon collections tomorrow during the Starbucks event, but Niantic will be revealing... | Read more »

3 reasons why Nicki Minaj: The Empire is...

Nicki Minaj is as business-savvy as she is musically talented and she’s proved that by launching her own game. Designed by Glu, purveyors of other fine celebrity games like cult favorite Kim Kardashian: Hollywood, Nicki Minaj: The Empire launched... | Read more »

Clash of Clans is getting its own animat...

Riding on its unending wave of fame and success, Clash of Clans is getting an animated web series based on its Clash-A-Rama animated shorts.As opposed to the current shorts' 60 second run time, the new and improved Clash-A-Rama will be comprised of... | Read more »

Leaks hint at Pokémon GO and Starbucks C...

Leaked images from a hub for Starbucks employees suggests that a big Pokémon GO event with the coffee giant could begin this very week. The images appeared on Reddit and hint at some exciting new things to come for Niantic's smash hit game.
| Read more »

Price Scanner via MacPrices.net

12-inch Retina MacBooks, Apple refurbished, n...

Apple has restocked a full line of Certified Refurbished 2016 12″ Retina MacBooks, now available for $200-$260 off MSRP. Refurbished 2015 models are available starting at $929. Apple will include a... Read more

Apple has Certified Refurbished 13″ MacBook Airs available starting at $849. An Apple one-year warranty is included with each MacBook, and shipping is free:
- 13″ 1.6GHz/8GB/128GB MacBook Air: $849 $... Read more

Apple refurbished iMacs available for up to $...

Apple has Certified Refurbished 2015 21″ & 27″ iMacs available for up to $350 off MSRP. Apple’s one-year warranty is standard, and shipping is free. The following models are available:
- 21″ 3.... Read more

Apple’s Education discount saves up to $300 o...

Purchase a new Mac or iPad using Apple’s Education Store and take up to $300 off MSRP. All teachers, students, and staff of any educational institution qualify for the discount. Shipping is free:
-... Read more

Back in stock: Apple refurbished Mac minis fr...

Apple has Certified Refurbished Mac minis available starting at $419. Apple’s one-year warranty is included with each mini, and shipping is free:
- 1.4GHz Mac mini: $419 $80 off MSRP
- 2.6GHz Mac... Read more

Twenty-Five Years Of Apple Laptops – A person...

Among many other things, the often tumultuous 16th year of the new century marked the 25th anniversary of Apple laptop computers, not counting the optimistically named 16-pound Mac Portable of 1989.... Read more

Landlordy iOS App Adds Support For Appliances...

Riga, Latvia based E-protect SIA is releasing major update (version 1.8) to its Landlordy app for managing rental business financials on the go. Landlordy is iPhone and iPad app designed for self-... Read more

MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders. Not responsible for typographical errors.

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.