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.

 
AAPL
$119.00
Apple Inc.
+1.40
MSFT
$47.75
Microsoft Corpora
+0.28
GOOG
$540.37
Google Inc.
-0.71

MacTech Search:
Community Search:

Software Updates via MacUpdate

Skype 7.2.0.412 - Voice-over-internet ph...
Skype allows you to talk to friends, family and co-workers across the Internet without the inconvenience of long distance telephone charges. Using peer-to-peer data transmission technology, Skype... Read more
HoudahSpot 3.9.6 - Advanced file search...
HoudahSpot is a powerful file search tool built upon MacOS X Spotlight. Spotlight unleashed Create detailed queries to locate the exact file you need Narrow down searches. Zero in on files Save... Read more
RapidWeaver 6.0.3 - Create template-base...
RapidWeaver is a next-generation Web design application to help you easily create professional-looking Web sites in minutes. No knowledge of complex code is required, RapidWeaver will take care of... Read more
iPhoto Library Manager 4.1.10 - Manage m...
iPhoto Library Manager lets you organize your photos into multiple iPhoto libraries. Separate your high school and college photos from your latest summer vacation pictures. Or keep some photo... Read more
iExplorer 3.5.1.9 - View and transfer al...
iExplorer is an iPhone browser for Mac lets you view the files on your iOS device. By using a drag and drop interface, you can quickly copy files and folders between your Mac and your iPhone or... Read more
MacUpdate Desktop 6.0.3 - Discover and i...
MacUpdate Desktop 6 brings seamless 1-click installs and version updates to your Mac. With a free MacUpdate account and MacUpdate Desktop 6, Mac users can now install almost any Mac app on macupdate.... Read more
SteerMouse 4.2.2 - Powerful third-party...
SteerMouse is an advanced driver for USB and Bluetooth mice. It also supports Apple Mighty Mouse very well. SteerMouse can assign various functions to buttons that Apple's software does not allow,... Read more
iMazing 1.1 - Complete iOS device manage...
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
PopChar X 7.0 - Floating window shows av...
PopChar X helps you get the most out of your font collection. With its crystal-clear interface, PopChar X provides a frustration-free way to access any font's special characters. Expanded... Read more
OneNote 15.4 - Free digital notebook fro...
OneNote is your very own digital notebook. With OneNote, you can capture that flash of genius, that moment of inspiration, or that list of errands that's too important to forget. Whether you're at... Read more

Latest Forum Discussions

See All

Lucha Amigos (Games)
Lucha Amigos 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Forget Ninja Turtles, and meet Wrestlers Turtles! Crazier, Spicier and…Bouncier! Sling carapaces of 7 Luchadores to knock all... | Read more »
Raby (Games)
Raby 1.0.3 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0.3 (iTunes) Description: ***WARNING - Raby runs on: iPhone 5, iPhone 5C, iPhone 5S, iPhone 6, iPhone 6 Plus, iPad Mini Retina, iPad Mini 3, iPad 4, iPad Air,... | Read more »
Oddworld: Stranger's Wrath (Games)
Oddworld: Stranger's Wrath 1.0 Device: iOS Universal Category: Games Price: $5.99, Version: 1.0 (iTunes) Description: ** PLEASE NOTE: Oddworld Stranger's Wrath requires at least an iPhone 4S, iPad 2, iPad Mini or iPod Touch 5th gen... | Read more »
Bounce On Back (Games)
Bounce On Back 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: | Read more »
Dwelp (Games)
Dwelp 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: === 50% off for a limited time, to celebrate release === Dwelp is an elegant little puzzler with a brand new game mechanic. To complete a... | Read more »
Make Way for Fat Chicken, from the Maker...
Make Way for Fat Chicken, from the Makers of Scrap Squad Posted by Jessica Fisher on November 26th, 2014 [ permalink ] Relevant Games has announced they will be releasing their reverse tower defense game, | Read more »
Tripnary Review
Tripnary Review By Jennifer Allen on November 26th, 2014 Our Rating: :: TRAVEL BUCKET LISTiPhone App - Designed for the iPhone, compatible with the iPad Want to create a travel bucket list? Tripnary is a fun way to do exactly that... | Read more »
Ossian Studios’ RPG, The Shadow Sun, is...
Ossian Studios’ RPG, The Shadow Sun, is Now Available for $4.99 Posted by Jessica Fisher on November 26th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Mmmm, Tasty – Having the Angry Birds for...
The very first Angry Birds debuted on iOS back in 2009. When you sit back and tally up the number of Angry Birds games out there and the impact they’ve had on pop culture as a whole, you just need to ask yourself: “How would the birds taste... | Read more »
Rescue Quest Review
Rescue Quest Review By Jennifer Allen on November 26th, 2014 Our Rating: :: PATH BASED MATCH-3Universal App - Designed for iPhone and iPad Guide a wizard to safety by matching gems. Rescue Quest might not be an entirely original... | Read more »

Price Scanner via MacPrices.net

Black Friday: 15% off iTunes Gift Cards
Staples is offering 15% off $50 and $100 iTunes Gift Cards on their online store as part of their Black Friday sale. Click here for more information. Shipping is free. Best Buy is offering $100... Read more
BEVL Releases Dock Tailored for iPhone 6 and...
Seattle based BEVL has released their first product: an iPhone dock that is divergent in build quality, rock-solid function and visual simplicity to complement the iPhone. BEVL is now accepting... Read more
Black Friday: $150 off 13-inch Retina MacBook...
 Best Buy has 13-inch 2.6GHz Retina MacBook Pros on sale for $150 off MSRP on their online store as part of their Black Friday sale. Choose free shipping or free local store pickup (if available).... Read more
Black Friday: $300 off 15-inch Retina MacBook...
 B&H Photo has the new 2014 15″ Retina MacBook Pros on sale for $300 off MSRP as part of their Black Friday sale. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.2GHz Retina... Read more
Black Friday: Up to $140 off MacBook Airs, fr...
 B&H Photo has 2014 MacBook Airs on sale for up to $140 off MSRP as part of their Black Friday sale. Shipping is free, and B&H charges NY sales tax only: - 11″ 128GB MacBook Air: $799 $100... Read more
Black Friday: 13-inch 2.5GHz MacBook Pro on s...
 Best Buy has the 13″ 2.5GHz MacBook Pro on sale for $899.99 on their online store as part of their Black Friday sale. Choose free shipping or free instant local store pickup (if available). Their... Read more
Black Friday: 21-inch 1.4GHz iMac on sale for...
 Best Buy has the 21″ 1.4GHz iMac on sale for $899.99 on their online store as part of their Black Friday sale. Their price is $200 off MSRP. Choose free shipping or free local store pick up. Price... Read more
Black Friday iPad Air 2 sale prices, $100 off...
 Best Buy has iPad Air 2s on sale for $100 off MSRP on their online store for Black Friday. Choose free shipping or free local store pickup (if available). Sale prices available for online orders... Read more
2014 1.4GHz Mac mini on sale for $449, save $...
 B&H Photo has the new 1.4GHz Mac mini on sale for $449.99 including free shipping plus NY tax only. Their price is $50 off MSRP, and it’s the lowest price available for this new model. Adorama... Read more
Early Black Friday pricing on 27-inch 5K iMac...
 B&H Photo continues to offer Black Friday sale prices on the 27″ 3.5GHz 5K iMac, in stock today and on sale for $2299 including free shipping plus NY sales tax only. Their price is $200 off MSRP... Read more

Jobs Board

*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Senior Event Manager, *Apple* Retail Market...
…This senior level position is responsible for leading and imagining the Apple Retail Team's global event strategy. Delivering an overarching brand story; in-store, Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.