One of the first sample Mac programs was Cary Clark's "File". The "About File" menu item did a nifty piece of animation with text which grew and seemed to "zoom out" toward the viewer. Clark calls this a "bit of fluff". I disagree: effects like this are part of the allure of the Mac. Here's a description of another visual effect which "File" inspired: a "dissolve" routine called DissBits, which fades from one image to another on the screen.

The routine is patterned after Quickdraw's CopyBits, which copies a rectangle in one bitmap to any other rectangle in any other bitmap. Along the way, it can clip to any region, stretch the image if the rectangles aren't the same size, and use any transfer mode. DissBits performs only some of these functions: it will work with any two bitmaps, but the rectangles must be of the same size, no clipping is done, and the bits are always copied directly, as the "srcCopy" mode does.

How does DissBits work? The idea is simple: pixels are copied from one rect to another in a random sequence which copies each pixel once. There are two tricky parts to this: finding a way to copy each bit only once and coding it to be fast. This article covers both issues.

The random sequence which hits all the bits is simple to implement. Let's reduce it to a smaller problem first: Suppose we want to copy all bits in a rect H bits wide and V bits high. Then we want a sequence which produces all (v, h) combinations in the rect. Suppose instead we have a random sequence of numbers n, 0 through V*H. If we take each number n and compute h=(n / V) and v=(n mod V), that gives us all (v, h) pairs in the rect. So we've changed the problem of hitting every point to the problem of generating the first V*H integers in a random sequence. If we can find a "1-dimensional" sequence, that gives us the "2-dimensional" sequence.

A way to generate the linear sequence without huge arrays is this: start with any number between 1 and N. To get the next sequence element, shift the number right by one bit. If a bit fell off in the shift, exclusive-or in a magic value (more on this value later). Repeat this to get more sequence elements. The resulting sequence will be all the numbers from 1 through 2k-1, where "k" depends on the magic value (don't worry, I really will explain about the magic values). To get numbers between 0 and N, choose a magic value such that 2k-1 is greater than or equal to N, and throw away elements which are too big. This may seem like a waste, but generating the extra elements is very fast. (Note: the sequence doesn't produce the value zero; this has to be done with special-case code.)

An example: if you want to cover an 80-by-100 bit rect, you must produce the numbers 0 through 7999 and map those to the points in the rect. To do this, use a magic value of $1D00, which will produce a sequence from 1 through 213-1; discard all the ones above 7999. Add special-case code to throw in the value 0.

How does one know which magic value to use? If you're into abstract algebra, you'll know that the magic values are related to prime polynomials. If you'd rather take the empirical approach, you can write a program to find the values. This took two weeks of CPU time on a Lisa. You'll probably want to use the table in the DissBits source. It gives the magic values to use for all "k"s from 2 to 32. (Be careful: the table is compressed; see the code which expands it.)

To sum up the sequence problem: to copy the pixels of one rect to another requires these steps: Find the total number of pixels in the rect. Find "k" which will produce a sequence including that number of elements (and a few more, which will be discarded). Cycle once through the sequence, taking each element and checking if it's in range. If so, map the integer to coordinates inside the rect and copy that point. When the sequence returns to the first element ( it always will), copy the point (0, 0), which the sequence doesn't produce.

The actual implementation has one difference: instead of using "modulo" and division to map a sequence element to two coordinates, the number is broken bit-wise, extracting the low bits to get one coordinate, and the high bits to get the other. This means that the sequence is slightly longer, and that both coordinates must be checked to see if they're in range, but the time saved by avoiding a 68000 DIV instruction is worth it.

Now for the graphics part. To copy a pixel from one rect to another, we want to know the state of the source pixel. We could use Quickdraw's "GetPixel", and then plot a black or white pixel depending on the source pixel. This would be too slow. Instead, DissBits works directly with the graphical data.

All graphics work is done using bitmaps: arrays of pixels in memory. A bitmap has a "base address", which points to the data. It has a "bounds" rect, which describes the coordinate system of the bitmap: the range of v and h coordinates. And it has a field called "rowbytes", which describes how many bytes are used to store each row of graphical data. The "rowbytes" field is important: to move "down" one pixel in the bitmap, you skip "ahead" by one row of bytes in memory -- "rowbytes" tells you how much to skip.

The link between a coordinate pair (v,h) and a bit in memory is through a bitmap. The point is (v-bounds.top) rows down into the bitmap and (h-bounds.left) columns across into it. Call these offsets "dv" and "dh". The base address of the bitmap is where you start. Then to skip "dv" rows, each of "rowbytes" bytes, skip (dv*rowbytes) bytes. The address of the start of the row is:

baseaddr + dv*rowbytes

To index by "dh" bits into the row, remember that each byte is eight bits. To get to the correct byte, the above address needs (dh/8) more bytes, so the final byte address is:

pixaddr = baseaddr + dv*rowbytes + (dh/8)

To select the correct bit number within this byte, use the remainder from computing (dh/8) -- this is (dh mod 8). Unfortunately, the 68000's method of bit numbering is backwards from the way Quickdraw numbers bits, so the 68000 bit number in the byte is:

pixbit = 7 - (dh mod 8)

These formulas let us take a point (v, h) in a bitmap and find the bit in memory, so we can quickly test the point with the instruction

BTST pixbit,pixaddr

If we also find bit and byte addresses for the pixel we want to copy to, we can branch after the BTST and do either a BSET or BCLR to set the destination pixel to match the source.

Putting all this together, we can take two rectangles of equal size, each in a bitmap, and produce a sequence of integers which (discarding some elements) takes us through all coordinate pairs in the rect. For each point, we use the bitmaps to find the actual bits in memory, test the source bit and set the destination bit appropriately.

Most of the algorithm is described in the code. The code has become obscure as successive optimizations have been added. One very important optimization should be explained in detail: a lot of the time spent in the loop is calculating (dv*rowbytes). If "rowbytes" is a power of two (as it is for the Mac screen), DissBits notices this and shifts into "middle gear" using a different loop and using shifts to avoid the multiply. If the rects being copied are the full width of the bitmaps, some more tricks can be done, and a third loop is used. At top speed, DissBits can copy an image onto the full screen in under 3.5 seconds.

Low Level Tricks

Most of the high-level optimizations are too detailed to cover here; the comments describe them in detail. Some low-level tricks worth mentioning are:

 When changing Quickdraw's numbering of bits to the 68000 conventions, it's actually not necessary to map x to (7-x). Since only the low 3 bits of the bit index are used in bit instructions, a NOT will convert the bit offset. (See the code after LOOPROW.)

 The first part of getting a new sequence element is shifting it to the right. If no magic value needs to be XOR-ed in, then the row number (in the high bits of the sequence element) is in range. (See the code after NEXT.)

 If the first sequence element is the magic value, the next to last element before the cycle loops is one (this is easy to prove). When 1 is shifted right, it becomes 0, so the end of the sequence is easy to detect. (See the code after NEXT.)

 Since the code to find the next sequence element wants to detect the case where no carry has occurred, and the result of the shift isn't zero, it combines these two conditions with a BHI instruction.

 When the high bits of the sequence element are shifted down to get the row number, they're soon multiplied by the "rowbytes" value. If "rowbytes" is even, it can be halved and the shift-down can be reduced by one. The code in HALFLOOP does this repeatedly, until "rowbytes" is odd.

 In the fastest case (copying the whole screen), it doesn't matter if the bit-numbering is reversed or not, since every bit will be copied anyway. This step is skipped in the "high-gear" loop.

The Mondrian Demo

"Mondrian", a short demo program for the dissolve effect is given here. Be sure to pull out "About Mondrian" from the apple menu to see the high-speed, full-screen dissolve.

DissBits is a good example of how the Mac's processing power can animate an effect which few other microcomputers can do in real time. Even machines with faster 68000 processors, such as the Amiga, can't get the speed the Macintosh can in copying the full screen, because of Apple's decision to make the screen's width a power of two pixels, allowing shifts instead of multiplies.

DissBits is a favorite effect of mine. I use it in nearly everything I write. I hope you'll use it in your applications, and perhaps be inspired to exploit the Mac's visual technology in new ways, just as Cary Clark's "bit of fluff" brought about DissBits.

Now comes the problem of putting all the pieces together. The Pascal source code for Mondrian was written first in Lisa Pascal and has been converted here to TML Pascal on the Macintosh. Actually, very little was required to make this conversion; only the TML libraries were substituted for the Lisa compiler versions. Nearly all the source code compiled without change. The TML product has been covered extensively by Alan Wootton in the Pascal column and is highly recommended as an inexpensive full-blown Pascal compiler that is MDS ".REL" file compatible. This source code is compiled to MDS assembly source as shown graphically in figure 3. That assembly code is then run through the MDS assembler to produce the ".REL" file for the MDS linker. In this respect, the product works much like the Absoft Fortran package, only better because there is no "run time" package to fool around with and no incompatibility problems with MDS as we have noted in our Fortran columns. [MICROSOFT please take note!!] Only one problem was encountered. It seems TML does not handle the Writeln statement yet. That was replaced with DrawString, which worked fine.

Even though the TML Pascal produces an MDS link file for you, it is best to either edit that file or use your own. In this case, I used my own, shown below. The DissBits subroutine [see next article] is assembled and linked with the Mondrian source and the resource file. As is my custom, I chose to do the resources in assembly so they could be assembled with MDS and linked directly to the two ".REL" files, thus avoiding the RMaker loop. One thing to note in the link file produced by the TML compiler is the names of the Pascal support routines that must be linked to your source code also. The result is a stand-alone, fully machine code program!

Software Updates via MacUpdate

Microsoft OneNote 15.29 - Free digital n...

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

Spotify 1.0.44.100. - Stream music, crea...

Spotify is a streaming music service that gives you on-demand access to millions of songs. Whether you like driving rock, silky R&B, or grandiose classical music, Spotify's massive catalogue puts... Read more

SpamSieve 2.9.27 - Robust spam filter fo...

SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering.
SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more

VueScan 9.5.62 - Scanner software with a...

VueScan is a scanning program that works with most high-quality flatbed and film scanners to produce scans that have excellent color fidelity and color balance. VueScan is easy to use, and has... Read more

Fantastical 2.3.2 - Create calendar even...

Fantastical 2 is the Mac calendar you'll actually enjoy using. Creating an event with Fantastical is quick, easy, and fun:
Open Fantastical with a single click or keystroke
Type in your event... Read more

PCalc 4.4.4 - Full-featured scientific c...

PCalc is a full-featured, scriptable scientific calculator with support for hexadecimal, octal, and binary calculations, as well as an RPN mode, programmable functions, and an extensive set of unit... Read more

Alfred 3.2.1 - Quick launcher for apps a...

Alfred is an award-winning productivity application for OS X. Alfred saves you time when you search for files online or on your Mac. Be more productive with hotkeys, keywords, and file actions at... Read more

OmniPlan 3.6 - Robust project management...

With OmniPlan, you can create logical, manageable project plans with Gantt charts, schedules, summaries, milestones, and critical paths. Break down the tasks needed to make your project a success,... Read more

Backblaze 4.2.0.990 - Online backup serv...

Backblaze is an online backup service designed from the ground-up for the Mac. With unlimited storage available for $5 per month, as well as a free 15-day trial, peace of mind is within reach with... Read more

AppDelete 4.3.1 - $7.99

AppDelete is an uninstaller that will remove not only applications but also widgets, preference panes, plugins, and screensavers along with their associated files. Without AppDelete these associated... Read more

Latest Forum Discussions

Galaxy on Fire 3 - Manticore brings the series back for another round of daring space battles. It's familiar territory for folks who are familiar with the franchise. If you've beaten the game and are looking to broaden your horizons, might we... | Read more »

The best apps for your holiday gift exch...

What's that, you say? You still haven't started your holiday shopping? Don't beat yourself up over it -- a lot of people have been putting it off, too. It's become easier and easier to procrastinate gift shopping thanks to a number of apps that... | Read more »

MyTona, based in the chilly Siberian city of Yakutsk, has brought a little festive fun to its hidden object game Seekers Notes: Hidden Mystery. The Christmas update introduces some new inhabitants to players, and with them a chance to win plenty of... | Read more »

PINE GROVE 1.0
Device: iOS Universal
Category: Games
Price: $1.99, Version: 1.0 (iTunes)
Description:
A pine grove where there are no footsteps of people due to continuous missing cases. The case is still unsolved and nothing has... | Read more »

Niantic teases new Pokémon announcement...

After rumors started swirling yesterday, it turns out there is an official Pokémon GO update on its way. We’ll find out what’s in store for us and our growing Pokémon collections tomorrow during the Starbucks event, but Niantic will be revealing... | Read more »

3 reasons why Nicki Minaj: The Empire is...

Nicki Minaj is as business-savvy as she is musically talented and she’s proved that by launching her own game. Designed by Glu, purveyors of other fine celebrity games like cult favorite Kim Kardashian: Hollywood, Nicki Minaj: The Empire launched... | Read more »

Clash of Clans is getting its own animat...

Riding on its unending wave of fame and success, Clash of Clans is getting an animated web series based on its Clash-A-Rama animated shorts.As opposed to the current shorts' 60 second run time, the new and improved Clash-A-Rama will be comprised of... | Read more »

Leaks hint at Pokémon GO and Starbucks C...

Leaked images from a hub for Starbucks employees suggests that a big Pokémon GO event with the coffee giant could begin this very week. The images appeared on Reddit and hint at some exciting new things to come for Niantic's smash hit game.
| Read more »

DietSensor, Inc., a developer of smart food and nutrition applications designed to fight diabetes and obesity and help improve overall fitness, has announced the launch of its DietSensor app for... Read more

Best Buy has dropped their price on the 64GB Apple TV to $159.99 including free shipping. That’s $40 off MSRP.
32GB Apple TVs are on sale right now for $98 on Sams Club’s online store. That’s $51 off... Read more

12-inch Retina MacBooks, Apple refurbished, n...

Apple has restocked a full line of Certified Refurbished 2016 12″ Retina MacBooks, now available for $200-$260 off MSRP. Refurbished 2015 models are available starting at $929. Apple will include a... Read more

Apple has Certified Refurbished 13″ MacBook Airs available starting at $849. An Apple one-year warranty is included with each MacBook, and shipping is free:
- 13″ 1.6GHz/8GB/128GB MacBook Air: $849 $... Read more

Apple refurbished iMacs available for up to $...

Apple has Certified Refurbished 2015 21″ & 27″ iMacs available for up to $350 off MSRP. Apple’s one-year warranty is standard, and shipping is free. The following models are available:
- 21″ 3.... Read more

Apple’s Education discount saves up to $300 o...

Purchase a new Mac or iPad using Apple’s Education Store and take up to $300 off MSRP. All teachers, students, and staff of any educational institution qualify for the discount. Shipping is free:
-... Read more

MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders. Not responsible for typographical errors.

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.