Paint Tools
Volume Number: 5
Issue Number: 6
Column Tag: Programmer's Workshop
Related Info: Quickdraw
Spray Can and Paint Bucket Tools
By Kevin Parichan, Reedley, CA
Note: Source code files accompanying article are located on MacTech CD-ROM or
source code disks.
[Kevin Parichan is a senior, computer science major attending Cal Poly in San
Luis Obispo. He has been programming on the Mac since early 1985 when he got his
first computer, the 128K Mac. Right now he is doing some work using 4th
Dimension.]
Painting Tools
When the Macintosh was first introduced, along with it came MacPaint and all
those nifty painting tools that first awed us.
I’m the kind of person who likes to take things apart, see how they work, and see
if I can figure them out. So it has been with MacPaint’s tools.
We can all certainly create cheap imitation tools that expand ovals and rectangles
that flash every time we move them. And since there are other, and far superior,
painting programs around these days, certainly other people have figured out the
mysteries of MacPaint. But I wanted to figure them out for myself.
I thought what better way than to create a Pascal unit that can be used by anybody
whose program’s purpose does not emphasize graphics, but would still like to supply
the standard tools to their users.
Since completing this unit (right now only in a Turbo Pascal version, and
available on GEnie) I thought that I might share some of my knowledge with the rest of
the people out there who are still scratching their heads.
Of course, I won’t show you everything. That would be too easy. I’ll just show
you how you can create both the Spray Can and Paint Bucket tools using some simple
logic and those new Quickdraw routines that surfaced with the MacPlus.
Along the way I did get some help
Technical Note #163 gives a very short code fragment on how to do a color fill
routine. The problem is that SeedCFill and SeedFill want different parameters. Plus,
SeedCFill uses Color Quickdraw which of course doesn’t much help those of us who
can’t afford a Mac II.
You may also notice the function, NewBitMap. I’ve been using this since I first
saw it in Scott Boyd’s article on animated bitmaps. It’s a handy little function.
I’ve tried to set up the routines as independent from the program as possible and
to do this I use the current grafport to pass information to the routines. For both tools
we use the portBits, portRect, and pnPat fields.
SeedFill and CopyMask were added when the MacPlus was introduced. They are
both described in Inside Mac Vol. IV. Both are routines that operate on a portion of a
bitmap, but I’ll be using them on whole bitmaps which has yet to cause any problems.
The spray can tool uses only CopyMask, so let’s start with that.
The Spray Can
{1}
PROCEDURE CopyMask (srcBits, maskBits, dstBits: BitMap; srcRect,
maskRect, dstRect: Rect);
As Inside Mac states: “ . . . it transfers a bit image from the source bitmap to the
destination bitmap only where the corresponding bit of the mask rectangle is a 1.” As
you can see in the example below.
Figure 1
So for the spray can tool what we’re going to do is create a source bitmap that
contains the pattern we want to spray with, and a mask bitmap which has 1’s
corresponding to where we want to paint. In our case, a spray pattern.
In both examples, it’s the call to CopyMask which is used to apply a pattern.
First thing we need to do is get the information we need from the current
grafport.
{2}
GetPort(workPort);
workBits:= workPort^.portBits;
workRect:= workPort^.portRect;
workPat:= workPort^.pnPat;
Then set up the mask bitmap representing an exact duplicate of the spray can