TweetFollow Us on Twitter

QD Antialiasing Techniques

Volume Number: 13 (1997)
Issue Number: 1
Column Tag: Tips & Graphics Workshop

Antialiasing with Color QuickDraw

By Martin James Murrett II, MacPants Software

Techniques used in AntiAliasMan to smooth the edges of Text, Line, Ovals and other shapes

I have been interested in antialiasing for a long time. It is a process in which the edges of a graphic are dithered with the background to produce a smoothing effect. My first experiment took about a week to render on screen, and showed a very poorly antialiased black circle over a white background. Since then, I have not only drawn that circle even slower, but have also released two versions of AntiAliasMan, a C library with a wide array of antialiasing functions, ranging from the basic circle to antialiased text, rounded rectangles, and lines, in any color, with or without disturbing the background. This article describes how the newest version of AntiAliasMan (2.0) works.

The first and most important area that this article will cover, before the code, is the area of antialiasing theory used in this code. An antialiasing effect can be achieved by drawing a large image off-screen, then dithering it down to a small image. My first two efforts used a method of enlarging the background, drawing a large image on it, and dithering down the whole mess.

One day it occurred to me that I could write a fast dithering mechanism by taking four char pointers, four rows at a time, and using them to get an index for a four-bit off-screen GWorld. I finally decided to implement my theoretical dithering routine in AntiAliasMan. Upon trying it out (once the bugs were gone), I realized that I was getting a 0-16, not 0-15 index. This lingered in my head for a little while. Then, one night (morning) at about 3 A.M., it occurred to me that I could mask the index with 0x08, shift the result three bits right, and subtract it from the original index to get a 0-15 index. This was incorrect. A value of 16 (0x10) has only one bit set, the fifth. I realized this the next day, and I had a working dithering routine.

Because Apple likes you, they made a routine called CopyBits that will change a grayscale image into a color image, complete with a mode parameter and some other goodies. Without CopyBits, AntiAliasMan would not exist. This is how the four-bit grayscale image is changed to text or ovals or whatever else you are antialiasing.

The uses for AntiAliasMan are limitless. Use it in all of your PowerMac applications, regardless of what they do. Write your own LDEFs and MDEFs for antialiased lists and menus. Write system extensions that give Word 6.0 a reason to go slowly, by patching DrawString to antialias text when Word 6.0 is the active process. With its new DitherMan dithering engine, AntiAliasMan is even practical for 680x0 series Macs.

General Stuff

I have only three rules: use CopyBits if at all practical, don't call SetRect, and don't wear your pants inside out. Of these three, only the first two are enforced in the code. I like CopyBits because it is ridiculously fast on a PowerMac, compared to any custom pixel copying routine (if you don't have a PowerPC compiler). It is guaranteed to work in the future, and your code automatically benefits from graphic accelerators. I hate SetRect because it adds to code size while decreasing code speed. Worse yet, is occupies a valuable A-trap spot (there are only 4,096 available). I am currently working on a patch that will make SetRect useful.

The code that follows presents the sore of AntiAliasMan two file segments: all of AntiAliasMan.h and AntiAliasManInit() from AntiAliasMan.c.

Listing 1: AntiAliasMan.h

#ifndef __ANTIALIASMAN__
#define __ANTIALIASMAN__

#ifndef __QDOFFSCREEN__
#include <QDOffscreen.h>
#endif

#ifndef __QUICKDRAW__
#include <Quickdraw.h>
#endif

pascal void AntiAliasManInit( void );

pascal OSErrDrawAntiAliasManString( ConstStr255Param s );
pascal OSErrDrawAntiAliasManChar( short ch );

pascal OSErrStdAntiAliasManRRect( GrafVerb verb, Rect *r,
 short ovalWidth, short ovalHeight );
pascal OSErrStdAntiAliasManOval( GrafVerb verb, Rect *r );
pascal OSErrStdAntiAliasManArc( GrafVerb verb, Rect *r,
 short startAngle, short arcAngle );

pascal OSErrAntiAliasManLineTo( short h, short v );
pascal OSErrAntiAliasManLine( short dh, short dv );

/* these macros make it easier to call the various AntiAliasMan functions, requiring less typing, and more 
familiar names */

#define DrawAAString( s ) DrawAntiAliasManString( s )

#define StdAARRect( v, r, ow, oh ) \
 StdAntiAliasManRRect( v, r, ow, oh )
#define StdAAOval( v, r ) StdAntiAliasManOval( v, r )
#define StdAAArc( v, r, sa, aa ) \
 StdAntiAliasManArc( v, r, sa, aa )

#define FrameAntiAliasManOval( r ) \
 StdAntiAliasManOval( frame, r )
#define PaintAntiAliasManOval( r ) \
 StdAntiAliasManOval( paint, r )
#define EraseAntiAliasManOval( r ) \
 StdAntiAliasManOval( erase, r )
#define InvertAntiAliasManOval( r )\
 StdAntiAliasManOval( invert, r )
#define FillAntiAliasManOval( r, p ) \
 StdAntiAliasManOval( fill, r )

#define FrameAAOval( r )  \
 StdAntiAliasManOval( frame, r )
#define PaintAAOval( r )  \
 StdAntiAliasManOval( paint, r )
#define EraseAAOval( r )  \
 StdAntiAliasManOval( erase, r )
#define InvertAAOval( r ) \
 StdAntiAliasManOval( invert, r )
#define FillAAOval( r, p )\
 StdAntiAliasManOval( fill, r )

#define FrameAntiAliasManRoundRect( r, ow, oh )          \
 StdAntiAliasManRRect( frame, r, ow, oh )
#define PaintAntiAliasManRoundRect( r, ow, oh )          \
 StdAntiAliasManRRect( paint, r, ow, oh )
#define EraseAntiAliasManRoundRect( r, ow, oh )          \
 StdAntiAliasManRRect( erase, r, ow, oh )
#define InvertAntiAliasManRoundRect( r, ow, oh )   \
 StdAntiAliasManRRect( invert, r, ow, oh )
#define FillAntiAliasManRoundRect( r, ow, oh, p )  \
 StdAntiAliasManRRect( fill, r, ow, oh )

#define FrameAARoundRect( r, ow, oh )\
 StdAntiAliasManRRect( frame, r, ow, oh )
#define PaintAARoundRect( r, ow, oh )\
 StdAntiAliasManRRect( paint, r, ow, oh )
#define EraseAARoundRect( r, ow, oh )\
 StdAntiAliasManRRect( erase, r, ow, oh )
#define InvertAARoundRect( r, ow, oh ) \
 StdAntiAliasManRRect( invert, r, ow, oh )
#define FillAARoundRect( r, ow, oh, p )\
 StdAntiAliasManRRect( fill, r, ow, oh )

#define FrameAAArc( r, sa, aa )    \
 StdAntiAliasManArc( frame, r, sa, aa )
#define PaintAAArc( r, sa, aa )    \
 StdAntiAliasManArc( paint, r, sa, aa )
#define EraseAAArc( r, sa, aa )    \
 StdAntiAliasManArc( erase, r, sa, aa )
#define InvertAAArc( r, sa, aa )   \
 StdAntiAliasManArc( invert, r, sa, aa )
#define FillAAArc( r, sa, aa, p )  \
 StdAntiAliasManArc( fill, r, sa, aa )

#define FrameAntiAliasManArc( r, sa, aa )                \
 StdAntiAliasManArc( frame, r, sa, aa )
#define PaintAntiAliasManArc( r, sa, aa )                \
 StdAntiAliasManArc( paint, r, sa, aa )
#define EraseAntiAliasManArc( r, sa, aa )                \
 StdAntiAliasManArc( erase, r, sa, aa )
#define InvertAntiAliasManArc( r, sa, aa )         \
 StdAntiAliasManArc( invert, r, sa, aa )
#define FillAntiAliasManArc( r, sa, aa, p )        \
 StdAntiAliasManArc( fill, r, sa, aa )

#define AALineTo( h, v )  AntiAliasManLineTo( h, v )
#define AALine( dh, dv )  AntiAliasManLine( dh, dv )

#endif
Listing 2: AntiAliasMan.c: Macros, AntiAliasManInit, and DitherMan

#include "AntiAliasMan2.h"

#ifndef nil
#define nil 0L
#endif

/* Low returns the lower of s1 and s2 */
#define Low( s1, s2 )( ( s1 > s2 ) ? s2 : s1 )
/* High returns the higher of s1 and s2 */
#define High( s1, s2 )  ( ( s1 > s2 ) ? s1 : s2 )
/* Abs returns the absolute value of s */
#define Abs( s ) ( s > 0 ? s : -( s ) )

#define uchar  unsigned char
#define ushort unsigned short
#define ulong  unsigned long

MSetRect 
/* MSetRect sets a rectangle in less code and fewer cycles than a call to SetRect */
#define MSetRect( r, lt, tp,\
 rt, bm ) \
{\
 r.top = tp;\
 r.left = lt;    \
 r.bottom = bm;  \
 r.right = rt;   \
}

CreateGWorlds 
/* CreateGWorlds creates the two GWorlds: the one-bit GWorld, x4GW, and the four-bit GWorld, x1GW. 
if an error occurs, CreateGWorlds cleans up, calls the routine specified in r, and returns the error. */
#define CreateGWorlds( r )\
{\
 err = NewGWorld( &x4GW, 1, \
 &x4Rect, nil, nil, 0 );  \
 if( err || x4GW == nil ) \
 { \
 r;\
 if( err != noErr )\
 return( err );  \
 return( memFullErr );    \
 } \
 \
/* GetCTable( 32 + x ) yeilds a gray scale gradient color table of depth x. since we're using four-bit color, 
we pass 32 + 4, or 36 */
 x1Tab = GetCTable( 36 ); \
 if( x1Tab == nil )\
 { \
 DisposeGWorld( x4GW );   \
 r;\
 return( memFullErr );    \
 } \
 err = NewGWorld( &x1GW, 4, \
 &x1Rect, x1Tab, nil, 0 );\
 if( err || x1GW == nil ) \
 { \
 DisposeGWorld( x4GW );   \
 DisposeCTable( x1Tab );  \
 r;\
 if( err != noErr )\
 return( err );  \
 return( memFullErr );    \
 } \
}

LockGWorlds
/* LockGWorlds saves and locks the two GWorlds' pixMaps, storing x4GW's pixMap in x4Map, and x1GW's 
pixMap in x1Map. if, for some reason, the GWorlds cannot be locked, LockGWorlds cleans up, calls the 
routine specified in r, and returns notLockedErr. LockGWorlds also puts dstGW's pixMap into dstMap. */
#define LockGWorlds( r )  \
{\
 x4Map = x4GW->portPixMap;\
 x1Map = x1GW->portPixMap;\
 dstMap = dstGW->portPixMap;\
 \
 locked = \
 LockPixels( x4Map );\
 if( !locked )   \
 { \
 DisposeGWorld( x4GW );   \
 DisposeGWorld( x1GW );   \
 \
 r;\
 \
 return( notLockedErr );  \
 } \
 locked = \
 LockPixels( x1Map );\
 if( !locked )   \
 { \
 DisposeGWorld( x4GW );   \
 DisposeGWorld( x1GW );   \
 \
 r;\
 \
 return( notLockedErr );  \
 } \
}



DisposeGWorlds
/* DisposeGWorlds unlocks both pixMaps, then disposes of the two GWorlds */
#define DisposeGWorlds()  \
{\
 UnlockPixels( x4Map );   \
 UnlockPixels( x1Map );   \
 \
 DisposeGWorld( x4GW );   \
 DisposeGWorld( x1GW );   \
}

void  DitherMan( PixMapHandle srcMap, PixMapHandle dstMap,
 ushort width );
/* truebits is a 16-ushort array, with each item set to the number of true (1) bits in its index. */
static ushort  truebits[ 16 ] =
 { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
/* fulltruebits will be initialized by AntiAliasManInit to a format specified in the AntiAliasManInit routine description. 
*/
ushort  fulltruebits[ 256 ];
/* inited is true if AntiAliasManInit has been called. */
Boolean inited = false;

AntiAliasManInit
/* AntiAliasManInit initializes a global array to the following:
    high half-byte of index i: number of bits set to true (1) in the high half-byte of i
    low half-byte of index i: number of bits set to true (1) in the low half-byte of i
    then the routine sets a global variable to tell itslef that it has been initialized. */
pascal void AntiAliasManInit( void )
{
 ushort one, two;
 for( one = 0; one <= 0x000f; one++ )
 {
 for( two = 0; two <= 0x000f; two++ )
 {
 fulltruebits[ ( one << 4 ) + two ] =
 ( truebits[ one ] << 8 ) + truebits[ two ];
 }
 }

 inited = true;
}

DitherMan
/* DitherMan takes eight bits from four rows at a time from srcMap (a one-bit PixMapHandle), adds them up, 
and decrements either half-byte if it exceeds 0xf. it then uses this value for two indexes in dstMap, a four-byte 
PixMapHandle. */
void    DitherMan( PixMapHandle srcMap, PixMapHandle dstMap,
 ushort width )
{
 ushort y, x, height, dstval;
 ulong  srcRowBytes, dstRowBytes;
 uchar  *src, *dst;
 uchar  *rowone, *rowtwo, *rowthree, *rowfour;
 uchar  *dstrow;
 height = ( *dstMap )->bounds.bottom;
 src = ( *srcMap )->baseAddr;
 dst = ( *dstMap )->baseAddr;
 srcRowBytes = ( *srcMap )->rowBytes & 0x7fff;
 dstRowBytes = ( *dstMap )->rowBytes & 0x7fff;

 for ( y = 0; y < height; y++ )
 {
 rowone = src;
 rowtwo = rowone + srcRowBytes;
 rowthree = rowtwo + srcRowBytes;
 rowfour = rowthree + srcRowBytes;
 dstrow = dst;
 for ( x = 0; x < width; x += 2 )
 {
 dstval = fulltruebits[ *( rowone++ ) ];
 dstval += fulltruebits[ *( rowtwo++ ) ];
 dstval += fulltruebits[ *( rowthree++ ) ];
 dstval += fulltruebits[ *( rowfour++ ) ];

/* the following line changes a 0x00-0x10 value to a 0x00-0x0f value. it works like this:
    all byte values over 0x0f have the fifth bit set. a mask of 0x10 strips all but this bit, so we now have a value 
of 0x10 or 0x00. shifting right four bits gives us a value of 0x01 or 0x00. so we subtract this value, and, if 
the index is over 0x0f, (like 0x10) it will be decremented. otherwise, nothing happens. since we are working 
with two bytes at a time, our mask changes to 0x1010, giving us values of 0x0000, 0x0010, 0x1000, or 0x1010 
when the bits are stripped, and subsequent values of 0x0000, 0x0001, 0x0100, and 0x0101, respectively, 
when the result is shifted right four bits. */
 dstval -= ( ( dstval & 0x1010 ) >> 4 );
/* to get a valid value for our dstMap, we must compress our word into a byte, so what we do is add dstval 
to dstval >> 4, squishing the low half-byte of our high byte into the high half-byte of our low byte.

    Example:
    1.   0x0X0Y
    2.   ( char )0x0X0Y + ( char )( 0x0X0Y >> 4 )
    3.   0x0Y + 0xX0
    4.   0xXY */
 *( dstrow++ ) = dstval + ( dstval >> 4 );
 }
 src += srcRowBytes * 4;
 dst += dstRowBytes;
 }
}

Antialiased Text

The most exciting area of AntiAliasMan is its ability to draw antialiased text. First, DrawAntiAliasManString() creates two off-screen GWorlds: one at one bit per pixel, at the maximum size of a character of four times the current font size; the other at four bits per pixel, with a four-bit grayscale gradient for a ColorTable, the size of the largest possible character in the current font and size. Next, DrawAntiAliasManString() drops into a loop, during which the one-bit off-screen is erased, filled with a character, dithered to the four-bit off-screen, and copied to the current port. When all of this has been completed, the off-screens are disposed, and the function returns noErr.

Listing 3: AntiAliasMan.c: DrawAntiAliasManString

DrawAntiAliasManString
/* DrawAntiAliasManString draws a string of antialiased text, using all of the standard Quickdraw globals 
*/
pascal OSErrDrawAntiAliasManString( ConstStr255Param s )
{
 CTabHandle x1Tab;
 FontInfo x4Font,x1Font;
 Rect   x4Rect, x1Rect, dstRect;
 GWorldPtrx4GW,  x1GW,  dstGW;
 PixMapHandle  x4Map,x1Map, dstMap;
 GDHandle dstGD;
 OSErr  err;
 Booleanlocked;
 short  i, charwidth;
 short  txFont, txSize, txFace, txMode;
 Point  pnLoc;
/* initialize AntiAliasMan if it hasn't been done already. */
 if( !inited ) AntiAliasManInit();

 GetPen( &pnLoc );
 GetGWorld( &dstGW, &dstGD );


/* save all text information for use in out GWorld */
 txFont = dstGW->txFont;
 txSize = dstGW->txSize;
 txFace = ( short )dstGW->txFace;
 txMode = dstGW->txMode;

 GetFontInfo( &x1Font );
 TextSize( txSize * 4 );
 GetFontInfo( &x4Font );
 TextSize( txSize );

/* make our GWorlds' rectangles the size of the largest possible character */
 MSetRect( x4Rect, 0, 0, x4Font.widMax,
 x4Font.ascent + x4Font.descent );
 MSetRect( x1Rect, 0, 0, x1Font.widMax,
 x1Font.ascent + x1Font.descent );

/* make and lock our GWorlds */
 CreateGWorlds( DrawString( s ) );
 LockGWorlds( DrawString( s ) );

 SetGWorld( x4GW, nil );

/* set up for text drawing:
    use the same font as dstGW.
    use the same style as dstGW.
    use srcCopy unless the text mode is grayishTextOr; then use grayishTextOr and set txMode (used in 
CopyBits) to srcOr.
    use a font size four times that of dstGW; out text will be shrunk by a factor of four. */
 TextFont( txFont );
 TextFace( txFace );
 if( txMode == grayishTextOr )
 {
 TextMode( grayishTextOr );
 txMode = srcOr;
 }
 else
 {
 TextMode( srcCopy );
 }
 TextSize( txSize * 4 );

 SetGWorld( dstGW, dstGD );

 for( i = 1; i <= s[ 0 ]; i++ )
 {
 charwidth = CharWidth( s[ i ] );

/* make a rectangle the size of character #i */
 MSetRect( dstRect, pnLoc.h, pnLoc.v - x1Font.ascent,
 pnLoc.h + charwidth, pnLoc.v + x1Font.descent );
 MSetRect( x1Rect, 0, 0, charwidth,
 x1Font.ascent + x1Font.descent - 1 );

/* clear the large gworld of gibberish and previous characters */
 SetGWorld( x4GW, nil );
 EraseRect( &x4Rect );

 MoveTo( 0, x4Font.ascent );
 DrawChar( s[ i ] );

/* dither the large GWorld to the small GWorld */
 DitherMan( x4Map, x1Map, charwidth );

 SetGWorld( dstGW, dstGD );
/* copy the four-bit image from the offscreen to dstGW, coloring it and styling it along the way. */
 CopyBits( ( BitMapPtr )*x1Map, ( BitMapPtr )*dstMap, &x1Rect,
 &dstRect, txMode, nil );
 pnLoc.h += charwidth;
 }
/* Move the Quickdraw pen to where it should be, now that more text has been printed */
 MoveTo( pnLoc.h, pnLoc.v );

/* clean up */
 DisposeGWorlds();
 return( noErr );
}

Antialiased Ovals (and Arcs)

Ovals are by far the easiest things to antialias, as long as you are reasonable about the method you use. The best way to antialias an oval is the same way that text is done. Draw an oval in a big, one-bit off-screen, dither it to a small, four-bit off-screen, and copy it to the screen. Here's the code that will do just that.

Note that by changing all calls to StdOval, FrameOval, PaintOval, EraseOval, and InvertOval or calls to StdArc, FrameArc, PaintArc, EraseArc, and InvertArc, respectively, and adding on the extra arguments (startAngle and arcAngle), this routine can be used to make antialiased arcs. However, to the best of my knowledge there have been no more than seven useful, practical calls to StdArc or any of its counterparts since its introduction in 1984, so I have left out source for that separate routine.

Listing 4: AntiAliasMan.c: StdAntiAliasManOval

StdAntiAliasManOval
/* StdAntiAliasManOval draws an antialiased oval in dstRect, of type verb (fill is unsupported at this time 
*/
pascal OSErrStdAntiAliasManOval( GrafVerb verb,
 Rect *dstRect )
{
 CTabHandle x1Tab;
 Rect   x4Rect, x1Rect;
 GWorldPtrx4GW, x1GW,dstGW;
 PixMapHandle  x4Map, x1Map, dstMap;
 GDHandle dstGD;
 OSErr  err;
 Booleanlocked;
 PenState pen;
 short  mode;

 GetGWorld( &dstGW, &dstGD );
 GetPenState( &pen );

/* if AntiAliasManInit has not been called, call it now. */
 if( !inited ) AntiAliasManInit();

/* set up our rectangles for CreateGWorlds */
 MSetRect( x4Rect, 0, 0, ( dstRect->right - dstRect->left )
 * 4,
 ( dstRect->bottom - dstRect->top ) * 4 );
 MSetRect( x1Rect, 0, 0, ( dstRect->right - dstRect->left ),
 ( dstRect->bottom - dstRect->top ) );

/* make and lock our GWorlds */
 CreateGWorlds(  StdOval( verb, dstRect ) );
 LockGWorlds(  StdOval( verb, dstRect ) );

 SetGWorld( x4GW, nil );

/* Set up for and draw the oval:
    the pnSize (pen size) should be four times that of the destination port, because the image drawn will be 
shrunk by an x and y factor of four.
    the background of the port must be erased to prevent gibberish from being copied to the screen. */
 pen.pnSize.v *= 4;
 pen.pnSize.h *= 4;
 SetPenState( &pen );
 EraseRect( &x4Rect );
 switch( verb )
 {
 case frame:
/* if the verb is frame, the mode should be srcOr, so that the black pixels colored by FrameOval are replaced 
with the forecolor of the destination port. */

 FrameOval( &x4Rect );
 mode = srcOr;
 break;
 case paint:
/* if the verb is paint, the mode should be srcOr, so that the black pixels colored by PaintOval are replaced 
with the forecolor of the destination port. */
 PaintOval( &x4Rect );
 mode = srcOr;
 break;
 case erase:
/* if the verb is erase, the mode should be srcBic, so that the black pixels colored by PaintOval are replaced 
with the backcolor of the destination port. */
 PaintOval( &x4Rect );
 mode = srcBic;
 break;
 case invert:
/* if the verb is invert, the mode should be srcXor, so that the black pixels colored by PaintOval are inverted 
in the destination port. */
 PaintOval( &x4Rect );
 mode = srcXor;
 break;
 case fill:
 default:
/* if the verb is fill or something else unsupported, the mode should be srcOr, so as to emulate PaintAAOval, 
a weak substitute, but better than nothing. */
 PaintOval( &x4Rect );
 mode = srcOr;
 break;
 }

/* DitherMan dithers a one-bit offscreen to a four-bit, 1/4th size offscreen */
 DitherMan( x4Map, x1Map, x1Rect.right );

 SetGWorld( dstGW, dstGD );
/* setting the GWorld to dstGW will set up the forecolor and backcolor and whatever else; passing mode 
to CopyBits will draw the shape as intended. */
 CopyBits( ( BitMapPtr )*x1Map, ( BitMapPtr )*dstMap, &x1Rect,
 dstRect, mode, nil );
/* clean up */
 DisposeGWorlds();

 return( noErr );
}

Antialiased Rounded Rectangles

An oval is pretty straightforward. On the other hand, a rounded rectangle (roundrect) is not. As previously mentioned, roundrects are part round and part straight. Therefore, the best way to antialias them is to make a rectangle, and place antialiased corners in place of its corners. The following code does that very well, making it one of AntiAliasMan's fastest functions. This code makes an antialiased oval in much the same fashion as StdAntiAliasManOval(). It draws the rectangle (sans corners) directly into the port, and then copies the corners from the four-bit off-screen to the port.

Listing 5: AntiAliasMan.c: StdAntiAliasManRRect

StdAntiAliasManRRect
/* StdAntiAliasManRRect acts just as StdRRect does: it draws a rounded rectangle in dstRect, with curves 
of ovalWidth and ovalHeight, of type verb. */
pascal OSErrStdAntiAliasManRRect( GrafVerb verb, Rect *dstRect,
 short ovalWidth, short ovalHeight )
{
 CTabHandle x1Tab;
 Rect   x4Rect, x1Rect;
 GWorldPtrx4GW, x1GW, dstGW;
 PixMapHandle  x4Map, x1Map, dstMap;
 GDHandle dstGD;
 OSErr  err;
 Booleanlocked;

 Rect   upperLeftQuad, upperRightQuad, lowerLeftQuad,
 lowerRightQuad, upperLeftCorner, upperRightCorner,
 lowerLeftCorner, lowerRightCorner,
 left, center, right;
 short  bigOvalWidth, bigOvalHeight;
 PenState pen;
 short  mode;

/* set up our global array if it's not set up already */
 if( !inited ) AntiAliasManInit();

 GetGWorld( &dstGW, &dstGD );
 GetPenState( &pen );

 bigOvalWidth = ovalWidth * 4;
 bigOvalHeight = ovalHeight * 4;

 MSetRect( x4Rect, 0, 0, bigOvalWidth, bigOvalHeight );
 MSetRect( x1Rect, 0, 0, ovalWidth, ovalHeight );

/* make and lock our GWorlds */
 CreateGWorlds(
 StdRRect( verb, dstRect, ovalWidth, ovalHeight ) );
 LockGWorlds(
 StdRRect( verb, dstRect, ovalWidth, ovalHeight ) );

 ovalWidth /= 2;
 ovalHeight /= 2;
 bigOvalWidth /= 2;
 bigOvalHeight /= 2;

/* the following eight lines set up the source and destination rectangles for the corners of the roundrect. 
the source (Quad) rectangles are each one-fourth of x1GW; the destination (Corner) rectangles are the four 
corners of the roundrect in dstGW. */
 MSetRect( upperLeftQuad, 0, 0, ovalWidth, ovalHeight );
 MSetRect( upperRightQuad, ovalWidth, 0, ovalWidth * 2,
 ovalHeight );
 MSetRect( lowerLeftQuad, 0, ovalHeight, ovalWidth,
 ovalHeight * 2 );
 MSetRect( lowerRightQuad, ovalWidth, ovalHeight, ovalWidth * 2,
 ovalHeight * 2 );

 MSetRect( upperLeftCorner, dstRect->left, dstRect->top,
 dstRect->left + ovalWidth, dstRect->top + ovalHeight );
 MSetRect( upperRightCorner, dstRect->right - ovalWidth,
 dstRect->top, dstRect->right, dstRect->top + ovalHeight );
 MSetRect( lowerLeftCorner, dstRect->left,
 dstRect->bottom - ovalHeight, dstRect->left + ovalWidth,
 dstRect->bottom );
 MSetRect( lowerRightCorner, dstRect->right - ovalWidth,
 dstRect->bottom - ovalHeight, dstRect->right,
 dstRect->bottom );

 SetGWorld( x4GW, nil );
 pen.pnSize.h *= 4;
 pen.pnSize.v *= 4;
 SetPenState( &pen );
 EraseRect( &x4Rect );
 switch( verb )
 {
 case frame:
/* if the verb is frame, the mode should be srcOr, so that the black pixels colored by FrameOval are replaced 
with the forecolor of the destination port. */
 FrameOval( &x4Rect );
 mode = srcOr;
 break;
 case paint:
/* if the verb is paint, the mode should be srcOr, so that the black pixels colored by PaintOval are replaced 
with the forecolor of the destination port. */
 PaintOval( &x4Rect );
 mode = srcOr;
 break;
 case erase:

/* if the verb is erase, the mode should be srcBic, so that the black pixels colored by PaintOval are replaced 
with the backcolor of the destination port. */
 PaintOval( &x4Rect );
 mode = srcBic;
 break;
 case invert:
/* if the verb is invert, the mode should be srcXor, so that the black pixels colored by PaintOval are inverted 
in the destination port. */
 PaintOval( &x4Rect );
 mode = srcXor;
 break;
 case fill:
 default:
/* if the verb is fill or something else unsupported, the mode should be srcOr, so as to emulate PaintAARRect, 
a weak substitute, but better than nothing. */
 PaintOval( &x4Rect );
 mode = srcOr;
 verb = paint;
 break;
 }

/* dither all four corners */
 DitherMan( x4Map, x1Map, x1Rect.right );
 SetGWorld( dstGW, dstGD );
 switch( verb )
 {
 case frame:
/* if the verb is frame, draw the four walls of the rectangle with MoveTo and LineTo */
// top
 MoveTo( dstRect->left + ovalWidth, dstRect->top );
 LineTo( dstRect->right - ovalWidth, dstRect->top );
// left
 MoveTo( dstRect->left, dstRect->top + ovalHeight );
 LineTo( dstRect->left, dstRect->bottom - ovalHeight );
// bottom
 MoveTo( dstRect->left + ovalWidth, dstRect->bottom - 1 );
 LineTo( dstRect->right - ovalWidth, dstRect->bottom - 1 );
// right
 MoveTo( dstRect->right - 1, dstRect->top + ovalHeight );
 LineTo( dstRect->right - 1, dstRect->bottom - ovalHeight );
 break;


 case paint:
 case erase:
 case invert:
 case fill:
/* otherwise, use three rectengles to do the trick: left, right, and bottom. left occupies the area from the left 
border to the right edge of the left corner ovals, from the bottom of the top ovals to the top of the bottom 
ones; center occupies the area bordered by left.right dstRect->top, right.left, and dstRect->bottom; right 
occupies the area beginning et the left border of the right corners and the bottom of the top corners, going 
until the right border of dstRect, and the top of the bottom corners. */
 MSetRect( left, dstRect->left, dstRect->top + ovalHeight,
 dstRect->left + ovalWidth, dstRect->bottom - ovalHeight );
 MSetRect( center, dstRect->left + ovalWidth, dstRect->top,
 dstRect->right - ovalWidth, dstRect->bottom );
 MSetRect( right, dstRect->right - ovalWidth,
 dstRect->top + ovalHeight, dstRect->right,
 dstRect->bottom - ovalHeight );
 StdRect( verb, &left );
 StdRect( verb, &center );
 StdRect( verb, &right );
 break;
 }

/* the following four lines copy the corners from x1GW to dstGW. */
 CopyBits( ( BitMapPtr )*x1Map, ( BitMapPtr )*dstMap,
 &upperLeftQuad, &upperLeftCorner, mode, nil );
 CopyBits( ( BitMapPtr )*x1Map, ( BitMapPtr )*dstMap,
 &upperRightQuad, &upperRightCorner, mode, nil );
 CopyBits( ( BitMapPtr )*x1Map, ( BitMapPtr )*dstMap,
 &lowerLeftQuad, &lowerLeftCorner, mode, nil );
 CopyBits( ( BitMapPtr )*x1Map, ( BitMapPtr )*dstMap,
 &lowerRightQuad, &lowerRightCorner, mode, nil );

/* clean up */
 DisposeGWorlds();

 return( noErr );
}

Antialiased Lines

This section is hiding at the very end of the article because it has ugly code associated with it, and I am sure that there are faster ways of achieving its purpose. However, it is the only easy-to-use routine that I have seen, so I am including it. The routine operates just as the rest of the routines included here - by making a large, one-bit off-screen, dithering it to a four-bit off-screen, and copying what is there to the screen.

Listing 6: AntiAliasMan.c: AntiAliasManLineTo

AntiAliasManLineTo
/* AntiAliasManLineTo draws an antialiased line from the current pen location to h, v. */
pascal OSErrAntiAliasManLineTo( short h, short v )
{
 CTabHandle x1Tab;
 Rect   x4Rect, x1Rect, dstRect;
 GWorldPtrx4GW, x1GW, dstGW;
 PixMapHandle  x4Map, x1Map, dstMap;
 GDHandle dstGD;

 OSErr  err;
 Booleanlocked;

 short  dh, dv;
 PenState pen;
 short  mode;

 GetPenState( &pen );
 dh = Abs( h - pen.pnLoc.h );
 dv = Abs( v - pen.pnLoc.v );
/* if the line it straight along the x or y axis, there is no reason to antialias it. */
 if( dh == 0 || dv == 0 ) LineTo( h, v );
 else
 {
/* make sure AntiAliasManInit gets called */
 if( !inited ) AntiAliasManInit();

 GetGWorld( &dstGW, &dstGD );

/* set up the rectangles for CreateGWorlds */
 MSetRect( x4Rect, 0, 0, ( dh + pen.pnSize.h * 2 ) * 4,
 ( dv + pen.pnSize.v * 2 ) * 4 );
 MSetRect( x1Rect, 0, 0, dh + pen.pnSize.h * 2,
 dv + pen.pnSize.v * 2 );
 MSetRect( dstRect, Low( h, pen.pnLoc.h ) - pen.pnSize.h,
 Low( v, pen.pnLoc.v ) - pen.pnSize.v,
 High( h, pen.pnLoc.h ) + pen.pnSize.h,
 High( v, pen.pnLoc.v ) + pen.pnSize.v );

/* create and lock our GWorlds */
 CreateGWorlds(  LineTo( h, v ) );
 LockGWorlds(  LineTo( h, v ) );

 switch( pen.pnMode )
 {
 case srcCopy:
 case srcOr:
 case blend:
 case subPin:
 case transparent:
 case adMin:
/* all of these modes essentially draw lines in the foreground color, so we use srcOr */
 mode = srcOr;
 break;
 case notSrcCopy:
 case srcBic:
 case addPin:
 case addMax:
/* all of these modes essentially draw lines in the background color, so we use srcBic */
 mode = srcBic;
 break;
 case srcXor:
 case addOver:
 case subOver:
/* all of these modes essentially invert lines, so we use srcXor */
 mode = srcXor;
 break;
 default:
/* otherwise, default to srcOr */
 mode = srcOr;
 }

 SetGWorld( x4GW, nil );
/* make our pnSize four times the original for a correct size when shrunk */
 pen.pnSize.v *= 4;
 pen.pnSize.h *= 4;
 pen.pnMode = srcCopy;
 SetPenState( &pen );
/* clear the GWorld of gibberish */
 EraseRect( &x4Rect );
/* aargh! I can't look! it's too hideous
    the following two lines (yes, there are only two lines there) figure out how the line
    is aligned in the offscreen, then draw it. */
 MoveTo( ( pen.pnLoc.h - dstRect.left + ( pen.pnLoc.h == dstRect.left 
+ pen.pnSize.h ? pen.pnSize.h * 2 : 0 ) ) * 4,
 ( pen.pnLoc.v - dstRect.top + ( pen.pnLoc.v == dstRect.top + pen.pnSize.v 
? pen.pnSize.v * 2 : 0 ) ) * 4 );
 LineTo( ( h - dstRect.left + ( h == dstRect.left + pen.pnSize.h ? pen.pnSize.h 
* 2 : 0 ) ) * 4,
 ( v - dstRect.top + ( v == dstRect.top + pen.pnSize.v ? pen.pnSize.v 
* 2 : 0 ) ) * 4 );
/* call DitherMan to dither our big line to a little one */
 DitherMan( x4Map, x1Map, x1Rect.right );
 SetGWorld( dstGW, dstGD );
/* CopyBits will colorize and stylize our line for us */
 CopyBits( ( BitMapPtr )*x1Map, ( BitMapPtr )*dstMap, &x1Rect,
 &dstRect, mode, nil );
/* relocate the pen and clean up */
 MoveTo( h, v );
 DisposeGWorlds();
 }

 return( noErr );
}

Conclusion

I hope this article helps you write good code and good applications that have antialiased text, ovals, arcs, rounded rectangles, and lines, without using huge pictures that have the images pre-antialiased. As far as other resources go, I can refer you to very little - almost all of the code presented here is completely of my own creation. However, I was told that Graphics Gems, volume I, has some good line antialiasing code, but when I looked at it, I decided that it was definitely not worth the effort to decipher its graphic format, etc., so I forgot about it. That is my only reference.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Minecraft 1.20.2 - Popular sandbox build...
Minecraft allows players to build constructions out of textured cubes in a 3D procedurally generated world. Other activities in the game include exploration, gathering resources, crafting, and combat... Read more
HoudahSpot 6.4.1 - Advanced file-search...
HoudahSpot is a versatile desktop search tool. Use HoudahSpot to locate hard-to-find files and keep frequently used files within reach. HoudahSpot is a productivity tool. It is the hub where all the... Read more
coconutBattery 3.9.14 - Displays info ab...
With coconutBattery you're always aware of your current battery health. It shows you live information about your battery such as how often it was charged and how is the current maximum capacity in... Read more
Keynote 13.2 - Apple's presentation...
Easily create gorgeous presentations with the all-new Keynote, featuring powerful yet easy-to-use tools and dazzling effects that will make you a very hard act to follow. The Theme Chooser lets you... Read more
Apple Pages 13.2 - Apple's word pro...
Apple Pages is a powerful word processor that gives you everything you need to create documents that look beautiful. And read beautifully. It lets you work seamlessly between Mac and iOS devices, and... Read more
Numbers 13.2 - Apple's spreadsheet...
With Apple Numbers, sophisticated spreadsheets are just the start. The whole sheet is your canvas. Just add dramatic interactive charts, tables, and images that paint a revealing picture of your data... Read more
Ableton Live 11.3.11 - Record music usin...
Ableton Live lets you create and record music on your Mac. Use digital instruments, pre-recorded sounds, and sampled loops to arrange, produce, and perform your music like never before. Ableton Live... Read more
Affinity Photo 2.2.0 - Digital editing f...
Affinity Photo - redefines the boundaries for professional photo editing software for the Mac. With a meticulous focus on workflow it offers sophisticated tools for enhancing, editing and retouching... Read more
SpamSieve 3.0 - Robust spam filter for m...
SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering. SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more
WhatsApp 2.2338.12 - Desktop client for...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more

Latest Forum Discussions

See All

‘Resident Evil 4’ Remake Pre-Orders Are...
Over the weekend, Capcom revealed the Japanese price points for both upcoming iOS and iPadOS ports of Resident Evil Village and Resident Evil 4 Remake , in addition to confirming the release date for Resident Evil Village. Since then, pre-orders... | Read more »
Square Enix commemorates one of its grea...
One of the most criminally underused properties in the Square Enix roster is undoubtedly Parasite Eve, a fantastic fusion of Resident Evil and Final Fantasy that deserved far more than two PlayStation One Games and a PSP follow-up. Now, however,... | Read more »
Resident Evil Village for iPhone 15 Pro...
During its TGS 2023 stream, Capcom showcased the Following upcoming ports revealed during the Apple iPhone 15 event. Capcom also announced pricing for the mobile (and macOS in the case of the former) ports of Resident Evil 4 Remake and Resident Evil... | Read more »
The iPhone 15 Episode – The TouchArcade...
After a 3 week hiatus The TouchArcade Show returns with another action-packed episode! Well, maybe not so much “action-packed" as it is “packed with talk about the iPhone 15 Pro". Eli, being in a time zone 3 hours ahead of me, as well as being smart... | Read more »
TouchArcade Game of the Week: ‘DERE Veng...
Developer Appsir Games have been putting out genre-defying titles on mobile (and other platforms) for a number of years now, and this week marks the release of their magnum opus DERE Vengeance which has been many years in the making. In fact, if the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for September 22nd, 2023. I’ve had a good night’s sleep, and though my body aches down to the last bit of sinew and meat, I’m at least thinking straight again. We’ve got a lot to look at... | Read more »
TGS 2023: Level-5 Celebrates 25 Years Wi...
Back when I first started covering the Tokyo Game Show for TouchArcade, prolific RPG producer Level-5 could always be counted on for a fairly big booth with a blend of mobile and console games on offer. At recent shows, the company’s presence has... | Read more »
TGS 2023: ‘Final Fantasy’ & ‘Dragon...
Square Enix usually has one of the bigger, more attention-grabbing booths at the Tokyo Game Show, and this year was no different in that sense. The line-ups to play pretty much anything there were among the lengthiest of the show, and there were... | Read more »
Valve Says To Not Expect a Faster Steam...
With the big 20% off discount for the Steam Deck available to celebrate Steam’s 20th anniversary, Valve had a good presence at TGS 2023 with interviews and more. | Read more »
‘Honkai Impact 3rd Part 2’ Revealed at T...
At TGS 2023, HoYoverse had a big presence with new trailers for the usual suspects, but I didn’t expect a big announcement for Honkai Impact 3rd (Free). | Read more »

Price Scanner via MacPrices.net

New low price: 13″ M2 MacBook Pro for $1049,...
Amazon has the Space Gray 13″ MacBook Pro with an Apple M2 CPU and 256GB of storage in stock and on sale today for $250 off MSRP. Their price is the lowest we’ve seen for this configuration from any... Read more
Apple AirPods 2 with USB-C now in stock and o...
Amazon has Apple’s 2023 AirPods Pro with USB-C now in stock and on sale for $199.99 including free shipping. Their price is $50 off MSRP, and it’s currently the lowest price available for new AirPods... Read more
New low prices: Apple’s 15″ M2 MacBook Airs w...
Amazon has 15″ MacBook Airs with M2 CPUs and 512GB of storage in stock and on sale for $1249 shipped. That’s $250 off Apple’s MSRP, and it’s the lowest price available for these M2-powered MacBook... Read more
New low price: Clearance 16″ Apple MacBook Pr...
B&H Photo has clearance 16″ M1 Max MacBook Pros, 10-core CPU/32-core GPU/1TB SSD/Space Gray or Silver, in stock today for $2399 including free 1-2 day delivery to most US addresses. Their price... Read more
Switch to Red Pocket Mobile and get a new iPh...
Red Pocket Mobile has new Apple iPhone 15 and 15 Pro models on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide service using all the major... Read more
Apple continues to offer a $350 discount on 2...
Apple has Studio Display models available in their Certified Refurbished store for up to $350 off MSRP. Each display comes with Apple’s one-year warranty, with new glass and a case, and ships free.... Read more
Apple’s 16-inch MacBook Pros with M2 Pro CPUs...
Amazon is offering a $250 discount on new Apple 16-inch M2 Pro MacBook Pros for a limited time. Their prices are currently the lowest available for these models from any Apple retailer: – 16″ MacBook... Read more
Closeout Sale: Apple Watch Ultra with Green A...
Adorama haș the Apple Watch Ultra with a Green Alpine Loop on clearance sale for $699 including free shipping. Their price is $100 off original MSRP, and it’s the lowest price we’ve seen for an Apple... Read more
Use this promo code at Verizon to take $150 o...
Verizon is offering a $150 discount on cellular-capable Apple Watch Series 9 and Ultra 2 models for a limited time. Use code WATCH150 at checkout to take advantage of this offer. The fine print: “Up... Read more
New low price: Apple’s 10th generation iPads...
B&H Photo has the 10th generation 64GB WiFi iPad (Blue and Silver colors) in stock and on sale for $379 for a limited time. B&H’s price is $70 off Apple’s MSRP, and it’s the lowest price... Read more

Jobs Board

Housekeeper, *Apple* Valley Villa - Cassia...
Apple Valley Villa, part of a 4-star senior living community, is hiring entry-level Full-Time Housekeepers to join our team! We will train you for this position and Read more
Housekeeper, *Apple* Valley Village - Cassi...
Apple Valley Village Health Care Center, a 4-star rated senior care campus, is hiring a Part-Time Housekeeper to join our team! We will train you for this position! Read more
Optometrist- *Apple* Valley, CA- Target Opt...
Optometrist- Apple Valley, CA- Target Optical Date: Sep 23, 2023 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 796045 At Target Read more
Senior *Apple* iOS CNO Developer (Onsite) -...
…Offense and Defense Experts (CODEX) is in need of smart, motivated and self-driven Apple iOS CNO Developers to join our team to solve real-time cyber challenges. Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.