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

Latest Forum Discussions

See All

Whitethorn Games combines two completely...
If you have ever gone fishing then you know that it is a lesson in patience, sitting around waiting for a bite that may never come. Well, that's because you have been doing it wrong, since as Whitehorn Games now demonstrates in new release Skate... | Read more »
Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »

Price Scanner via MacPrices.net

B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for up to $150 off Apple’s new MSRP, starting at only $849. Free 1-2 day delivery is available to most US... Read more
M2 Mac minis on sale for $100-$200 off MSRP,...
B&H Photo has Apple’s M2-powered Mac minis back in stock and on sale today for $100-$200 off MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $... Read more
Mac Studios with M2 Max and M2 Ultra CPUs on...
B&H Photo has standard-configuration Mac Studios with Apple’s M2 Max & Ultra CPUs in stock today and on Easter sale for $200 off MSRP. Their prices are the lowest available for these models... Read more
Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.