TweetFollow Us on Twitter

Mar 94 Challenge
Volume Number:10
Issue Number:3
Column Tag:Programmers’ Challenge

Related Info: Color Quickdraw

Programmers’ Challenge

By Mike Scanlin, MacTech Magazine Regular Contributing Author

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

The rules

Here’s how it works: Each month there will be a different programming challenge presented here. First, you must write some code that solves the challenge. Second, you must optimize your code (a lot). Then, submit your solution to MacTech Magazine (formerly MacTutor). A winner will be chosen based on code correctness, speed, size and elegance (in that order of importance) as well as the postmark of the answer. In the event of multiple equally desirable solutions, one winner will be chosen at random (with honorable mention, but no prize, given to the runners up). The prize for the best solution each month is $50 and a limited edition “The Winner! MacTech Magazine Programming Challenge” T-shirt (not to be found in stores).

In order to make fair comparisons between solutions, all solutions must be in ANSI compatible C (i.e., don’t use Think’s Object extensions). Only pure C code can be used. Any entries with any assembly in them will be disqualified (except for those challenges specifically stated to be in assembly). However, you may call any routine in the Macintosh toolbox you want (i.e., it doesn’t matter if you use NewPtr instead of malloc). All entries will be tested with the FPU and 68020 flags turned off in THINK C. When timing routines, the latest version of THINK C will be used (with ANSI Settings plus “Honor ‘register’ first” and “Use Global Optimizer” turned on) so beware if you optimize for a different C compiler. All code should be limited to 60 characters wide. This will aid us in dealing with e-mail gateways and page layout.

The solution and winners for this month’s Programmers’ Challenge will be published in the issue two months later. All submissions must be received by the 10th day of the month printed on the front of this issue.

All solutions should be marked “Attn: Programmers’ Challenge Solution” and sent to Xplain Corporation (the publishers of MacTech Magazine) via “snail mail” or preferably, e-mail - AppleLink: MT.PROGCHAL, Internet: progchallenge@xplain.com, CompuServe: 71552,174 and America Online: MT PRGCHAL. If you send via snail mail, please include a disk with the solution and all related files (including contact information). See page 2 for information on “How to Contact Xplain Corporation.”

MacTech Magazine reserves the right to publish any solution entered in the Programming Challenge of the Month and all entries are the property of MacTech Magazine upon submission. The submission falls under all the same conventions of an article submission.

BITMAP TO TEXT

Have you ever seen one of those text files where if you print it out, tack it on a wall and step back it looks like a graphic? I’ve seen dragons, islands, Star Trek images, etc. done this way. This month’s challenge is to write the routine that converts a bitmap into a text equivalent.

The prototype of the function you write is:


/* 1 */
short BitMapToText(bitMapPtr, fontName,
  fontSize, outputFile)
BitMap  *bitMapPtr;
Str255  fontName;
unsigned short fontSize;
FILE    *outputFile;

BitMapPtr points to the input bits. The max size is 1000 pixels square. fontname is the name of the monospaced font to use (Monaco, Courier, for example) and fontSize is the size of that font that should be used (6pt to 24pt). outputFile is a standard C output stream that you should write your text output to, each line separated by a 0x0D byte. The return value of the function is an error code: zero if nothing went wrong or non-zero if an error occurred. Do not close outputFile when you are finished.

Your basic strategy will be to split the bitMap into character size pieces and then find the best character match for each piece. You should only use the printable ASCII characters (charCodes from 32 to 127, inclusive). The closeness-of-match algorithm is not given. It’s up to you to pick something that works reasonably well and doesn’t take 50 years to compute. This contest will be judged primarily on speed but routines that produce unrecognizable output will be disqualified (no matter how fast they are). Recognizability will be judged on the screen, at 72dpi.

Note that in order to have recognizable output the size of the smallest detail in the input image needs to be roughly equal to or larger than a single character of the given font and font size. This will be true for the test images I use (so don’t stress too much over the problem of how to represent a very small image using only 24pt glyphs).

TWO MONTHS AGO WINNER

Of the eight entries I received for the Connect The Dots challenge, six worked correctly. Bill Karsh (Chicago, IL) joins the ranks of Challenge superstars for coming in first place for the second time. Bill previously won the Who Plays Who challenge and is now tied in a 4-way tie for the most number of Challenge wins. Bill’s line drawing routine is about 3x faster than Color QuickDraw for long lines and about 30x faster for very short lines (for the special cases given in the challenge: no clipping, pen size (1, 1), patCopy, no bitMaps). If you have intensive line-drawing routines in your code you ought to consider casing out those cases that Bill’s code handles and using it instead of many calls to Line or LineTo.

Here are the code sizes and average times (for medium to long line-length tests) of each entry. Numbers in parens after a person’s name indicate how many times that person has finished in the top 5 places of all previous Programmer Challenges, not including this one:

Name Time Code

Bill Karsh (2) 1114 1314

Kevin Cutts (2) 1370 1802

Bob Boonstra (5) 1434 750

Allen Stenger (2) 1623 1664

John Heaney 1711 710

Stefan Pantke 2500 876

Color Quickdraw 3219 ?

There were three cases that had to be dealt with: 8-bit, 16-bit and 32-bit deep pixMaps. Once the appropriate pixel value to stuff has been figured out, all three cases are the same (as far as determining which pixels are part of the line). Bill solved this redundant code problem by having the guts of each case #included in three different places. This makes it easy to update the line generating code for all three cases at the same time. And as did nearly everyone else, Bill handles the common cases of horizontal and vertical lines separately (which is a big win for those cases).

For the 8-bit case he uses his own RGB2Index routine instead of the ROM’s Color2Index, which would be fine if his routine worked all the time, but it doesn’t. It only works if the RGB value you’re trying to convert is an exact match with one of the index values. However, the main point of this challenge was about drawing lines fast, not inverse color table lookups.

Kevin Cutts (Schaumburg, IL) and Bob Boonstra (Westford, MA) deserve a mention here because in some of the very short line cases their code was faster than Bill’s. But I think the average line drawn by QuickDraw is longer than a few pixels and Bill’s code is faster for those cases so he wins.

I’d also like to apologize to Alan Hughes (Ames, IA) for the mixup last month that caused his on-time and correct entry to the Present Packing Challenge to get to me after I had sent in the column. His 94.2 average puts him in 3rd place and knocks Dave Darrah out of the top 5.

As readers of this column know, I have been stressing 680x0 optimizations in this column for over a year (and C code that generates better 680x0 code in Think C). Now that the PowerPC is coming out I am faced with a choice: Which platform should I run the challenge on, 680x0 or 601? Obviously, if there is a switch to 601 it would not happen for at least a couple of months after they are made generally available. But are readers interested in 601 tricks or should we stick to the installed base of 680x0s for many more months? And if and when we switch to the 601, what PPC compiler should I use to test challenge entries? Send me e-mail at the progchal addresses in the front of the magazine and let me know what you think. Thanks.

Here’s Bill’s winning solution:

ConnectTheDots

Response to Jan 94 MacTech Programmer's Challenge.

Object: Go around QD to draw color lines as fast as possible.

Specs:

• nDots >= 2,

• handle only cases {(pixelSize,cmpSize,cmpCount) = (8,8,1), (16,5,3), (32,8,3)},

• arbitrary alpha-bits,

• don't bother clipping,

• penSize = 1,1,

• patCopy mode.

Notes on method: Specify segment by two endpoints {(x,y)=(a,b),(A,B)}. Form of line is {(x,y): (y-b)/(x-a) = m}, where slope m = (B-b)/(A-a). Then, y = m*(x-a)+b.

Two successive values of y are: y2 = m*(x2-a)+b; y1 = m*(x1-a)+b, and the diff is, y2-y1 = m, since x2-x1 will always = 1 (pixel).

Therefore, as we move from x to x, we add or sub m to the previous value of y.

Speed: The cases that arise for combinations of {dy,dx} fall generally into 8 octants that cover the plane. Diagonally opposite octants are treated together, so there are 4 main cases to worry about. We first weed out 3 special cases: exactly horizontal, vertical, and diagonal segs. These are the simplest, most common, and fastest.

In a given octant, one of |dx|, |dy| is strictly larger than the other. Our loop over pixels will always be over the larger magnitude for higher resolution drawing. The slope is formed then by smallNum/largeNum which must have quotient == 0, and remainder == smallNum. Adding the slope is a matter of accumulating remainders. If this sum exceeds largeNum, we move to next pixel.


/* 2 */
#pragma options( honor_register, !assign_registers )
#pragma options( !check_ptrs )

#include"ConnectTheDots.h"

#define HiFiveMask 0xF800
#define Abs( a ) (a > 0 ? a : -a)

/* RGB2Index
 *
 * Expects rgb color is an exact member of table, to avoid time spent 
close
 * matching. Index is just position in table.
 */
static Byte RGB2Index( ColorSpec *cSpec, RGBColor *rgb )
{
 register ColorSpec*cs = cSpec;
 register short  entries = ((short*)cs)[-1]+1;
 register short  red = rgb->red,
 green = rgb->green,
 blue = rgb->blue;
 do {
 if( red   == cs->rgb.red  &&
 blue  == cs->rgb.blue &&
 green == cs->rgb.green )
 return cs-cSpec;
 ++cs;
 } while( --entries );
}
/* Lines8
 *
 * Depth == 8 case.
 *
 * To maximize register usage, chose to put rowBytes in address reg. 
 
 * Also, some vars like v_Cnt are dual purpose.
 */
static void Lines8(
 PixMapPtrpm,
 Point  dot[],
 unsigned short  nDots,
 register Byte   pixel )
{
 register Ptr  at;
 #include "ConnectTheDots.com"
}

/* Lines16
 */
static void Lines16(
 PixMapPtrpm,
 Point  dot[],
 unsigned short  nDots,
 register short  pixel )
{
 register short  *at;
 #include "ConnectTheDots.com"
}

/* Lines32
 *
 * align ensures 4-byte stack alignment for better speed.
 */
static void Lines32(
 PixMapPtrpm,
 Point  dot[],
 unsigned short  nDots,
 short  align,
 register long   pixel )
{
 register long   *at;
 #include "ConnectTheDots.com"
}

/* ConnectTheDots */
void ConnectTheDots(
 unsigned short  nDots,
 Point  dot[],
 PixMapHandle    pmH,
 RGBColor color )
{
 register PixMapPtrpm = *pmH;
 register unsigned short  pix16;
 register Ptr    p32;
 long   pix32;
 
 if( pm->pixelSize == 8 ) {
 
 Lines8( pm, dot, nDots,
 RGB2Index(&(**pm->pmTable).ctTable, &color) );
 }
 if( pm->pixelSize == 16 ) {

 pix16  = (color.red   & HiFiveMask) >> 1;
 pix16 |= (color.green & HiFiveMask) >> 6;
 pix16 |= (color.blue  & HiFiveMask) >> 11;
 
 Lines16( pm, dot, nDots, pix16 );
 }
 if( pm->pixelSize == 32 ) {
 
 p32 = ((Byte*)&pix32) + 1;
 *p32++ = *(Byte*)&color.red;
 *p32++ = *(Byte*)&color.green;
 *p32++ = *(Byte*)&color.blue;
 
 Lines32( pm, dot, nDots, 0, pix32 );
 }
}

This is the part of the line drawing algorithm common to all three depths, and it’s in its own separate file called ConnectTheDots.com. This is an unusual, but very useful way to use #include directive. Treat this file like a .h file, though it contains code instead of interface info. That means, like a .h file, you do not directly compile or link this file. If using Think C, don't put it in your project. It automatically becomes part of the .c file at compile time.

/* 3 */
/* ConnectTheDots.com
*/

// start
 register Ptr    rowBytes;
 register short  *pnt;
 register short  dh, dv, h_Sum, v_Cnt;
 Ptr    savedRowBytes;
 short  *savedPnt;
 short  pad;
 
 --nDots;
 
 pnt = (short*)dot;
 savedRowBytes = (Ptr)(pm->rowBytes & 0x7fff);

 do {

 // find this seg's dimensions {dv,dh} and endpoints in bounds coordinate
 // system.  ends are (v,h) and (v+dv,h+dh).
 // point to pixels, and restore rowBytes, which are altered in loop.

 dv       = *pnt++;
 dh       = *pnt++;
 v_Cnt    = *pnt;
 h_Sum    = pnt[1];
 dv      -= v_Cnt;
 dh      -= h_Sum;
 v_Cnt   -= pm->bounds.top;
 h_Sum   -= pm->bounds.left;
 at       = pm->baseAddr;
 rowBytes = savedRowBytes;
 
 if( !dh ) {
 // do vertical line
 if( dv < 0 ) {
 v_Cnt += dv;
 dv = -dv;
 }

 at = (Ptr)at + (long)v_Cnt*(short)rowBytes;
 at += h_Sum;
 
 v_Cnt = dv + 1;
 
doVert:
 do {
 *at = pixel;
 at = (Ptr)at + (long)rowBytes;
 } while( --v_Cnt );
 }
 else if( !dv ) {
 
 // do horizontal line
 
 if( dh < 0 ) {
 h_Sum += dh;
 dh = -dh;
 }
 
 at = (Ptr)at + (long)v_Cnt*(short)rowBytes;
 at += h_Sum;
 
 ++dh;
 
 do {
 *at++ = pixel;
 } while( --dh );
 }
 else if( Abs( dv ) >= Abs( dh ) ) {
 
 // more vertical or diagonal
 
 if( dv < 0 ) {
 v_Cnt += dv;
 h_Sum += dh;
 dv = -dv;
 dh = -dh;
 }
 
 at = (Ptr)at + (long)v_Cnt*(short)rowBytes;
 at += h_Sum;
 
 v_Cnt = dv + 1;
 
 if( dh == dv ) {
 rowBytes += sizeof(pixel);
 goto doVert;
 }
 else if( -dh == dv ) {
 rowBytes -= sizeof(pixel);
 goto doVert;
 }
 else {

 h_Sum = 0;
 
 savedPnt = pnt;
 pnt = (short*)sizeof(pixel);
 
 if( dh < 0 ) {
 dh = -dh;
 pnt = (short*)-sizeof(pixel);
 }

 do {
 *at = pixel;
 at = (Ptr)at + (long)rowBytes;
 
 h_Sum += dh;
 
 if( h_Sum >= dv ) {
 h_Sum -= dv;
 at = (Ptr)at + (long)pnt;
 }
 } while( --v_Cnt );
 
 pnt = savedPnt;
 }
 }
 else {
 
 // more horizontal
 
 if( dh < 0 ) {
 v_Cnt += dv;
 h_Sum += dh;
 dv = -dv;
 dh = -dh;
 }
 
 at = (Ptr)at + (long)v_Cnt*(short)rowBytes;
 at += h_Sum;
 
 v_Cnt = dh + 1;
 h_Sum = 0;

 if( dv < 0 ) {
 dv = -dv;
 rowBytes = (Ptr)(-(short)rowBytes);
 }
 
 do {
 *at++ = pixel;
 
 h_Sum += dv;
 
 if( h_Sum >= dh ) {
 h_Sum -= dh;
 at = (Ptr)at + (long)rowBytes;
 }
 } while( --v_Cnt );
 }
 } while( --nDots );
 
// end







  
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
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
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and 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
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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.