Autostereogram Creator

By Matthew Ford

Originally published in EUG #18

An autostereogram is a repeating pattern of dots which reveals a convincing 3-dimonsional illusion if viewed correctly. They have become popularly known as 'Magic Eye' pictures after the recent series of books and can also be found on posters in art shops. They are generated using computer programs, so I set about writing a program to produce them on the Electron.

I found a simple routine to do this written in the C programming language in an issue of last year's New Scientist magazine and, as I understood enough of it to write a version in BASIC, I did so. Unfortunately my version took over four hours to run (and was only capable of producing images of very simple shapes) so was quickly evident that a machine code version was needed. I have now written a version which can produce virtually any image and never takes more than 45 minutes to run. In fact, it often runs in under ten! (I admit that this is still slow - the program is held up by needing to use BASIC's RND function to generate random numbers.

To produce an autostereogram, you will need a picture to supply the 3D image that you will see. This can be a screen file in any graphics mode (0,1,2,4,5) but it must have a very clear outline, particularly on the first one you try. The colours of the picture are used to tell the program how far in front of the page each part of the image will appear - so don't use them to make the picture look pretty or realistic. There is no concept of colour in the illusions themselves.

If the picture is fairly simple, you can draw it just using BASIC's graphics commands (and PLOT 85 to fill areas with colour) then save it using *SAVE filename 3000 8000 for a Mode 0, 1 or 2 screen or *SAVE filename 5800 8000 for a Mode 4 or 5 screen. However, it is probably easier to use an art program such as ELKPAINT (Electron User, October 1989). An improved version of this is on this disk with a couple of changes made to the load/save routines. The keys are as follows:

Z,X,*,? Move cursor
S Store screen in memory
0-3 Choose colour
U Swap to stored screen
SHIFT+1-6Choose speed
T Text
B Rubberband
Q and W Clear screen
C Circle
K Save screen
E Ellipse
L Load screen
R Rectangle
M Magnify (cursors scroll view)
A then 1-5Brush mode
F Fill
P Cut and paste

(Use SPACE bar to fix area to cut, drag box to new position, press RETURN then Space for a normal copy)

H Horizontal flip
V Vertical flip
R Rotate

(Z and X rotate about bottom left corner, then Space to fix)

SPACEConfirm/fix/draw in brush and magnify modes
RETURNExit brush/magnify modes

When you have a picture ready, run the autostereogram generator. You will first need to enter the filename of the source picture and it's mode (which will be 5 if you used ELKPAINT). Next enter the filename you wish to save the autostereogram under and its mode (0 or 4). The dot resolution in Mode 0 is twice that in Mode 4 but my opinion is that the images are easier to see in Mode 4. They are also much quicker to produce in Mode 4.

Next you must enter the 'eye separation'. This will be the distance between the repeating patterns on the page, and hence relates to the amount you have to cross your eyes to see the illustion. (If you use my small screen dump, this will be in millimetres). I find it easiest to see the images if I use an eye separation of about 20, though this is much less than in most commercial autostereograms.

You will then be asked to enter an 'elevation number' for each logical colour used in the source picture. This is a measure of how far in front of the page each part of the image will appear. The higher the number, the more the illusion stands out, so you should enter 0 for the background colour. I use a value of 4 for most images, though you can vary this from about 2 to 10 to get a good 3-D effect. It's important that you remember which logical colours you used for which parts of the picture. If you use Mode 0, you should double the values mentioned above (as these refer to Mode 4). Similarly, if you alter the eye separation, you should change the elevation numbers in approximately the same ratio.

Finally you will be asked if you want a printed copy. I have written my own single-shade screen dump for this purpose. This must be saved in the same directory as the autostereogram generator, under the filename ScrDump (Sorry about the total lack of meaningful labels and variable names in this program!). You may prefer to use the dump from Diagram which I have also improved. This is twice as fast as mine, and the same shape but is 50% wider, filling the full length of the carriage. This machine code program is saved as "UCODE". There is an option to select between these two dumps (small and large respectively).

Now just sit back and wait for the result. This could take up to 45 minutes if you are using Mode 0 so be patient if the computer appears to be doing nothing for a long period.

If you've never actually seen an autostereogram before, all these parameters will sound rather complicated. But don't worry - just use my suggested values to get you started. You can try varying them later, one at a time, to see the effects.

You may be able to see the effect on your TV/Monitor screen if it is not too flickery though it is generally easier on paper. My preferred method is to stare blankly at the page and slowly refocus my eyes until two columns of the repeating pattern appear to merge into one. Then, if you can hold your focus long enough on the merged pattern, the image should lift out.

Other people prefer the following method: Hold the paper against your face and stare through it blankly. Slowly move the paper away from your face without altering your focus until the image appears then wait until it becomes clear. You will have to concentrate very hard to prevent yourself from refocusing on the image as it appears.

Another method is to place a reflective surface over the page, and focus your reflection (a TV/Monitor screen might also work). I have never found this method useful myself.

You will probably find that you have to stare at your first autostereogram a long time before you see the effect for the first time. It took me several hours! But it's well worth the wait. If you still can't see them, don't worry - apparently an estimated 10% of the population never can see them.

The autostereograms produced by this program are not as effective as commerical ones (though they may be easier to see). This is partly because they are black and white and the patterns are not attractive in themselves, as they are generated randomly. However, another serious limitation is that the images you see are always parallel to the paper - they can never slope towards or away from you. I have tried to make sloping surfaces using a 'staircase' effect - several narrow bands of Mode 2 colours, with consecutive elevation numbers. However, the resolution of Mode 0 graphics was not really enough to make this successful.

You may wish to produce an autostereogram of a word or a short phrase. If so, I have sent in a program to print large text on the screen. This is based on the Banner Printing program from Electron User (June 1989) and is described elsewhere on the disk.

If you do produce an impressive image, send it in! Perhaps this could be the basis of a future competition (i.e. "What object is shown in this autostereogram?" with the source picture included on the following EUG disk)?

I have included an example of an autostereogram so you can test yourself on your ability to see it. The source picture it came from will be published next issue so you have a whole two months to work at it. Good luck!

Matthew Ford, EUG #18

The example picture is at the top of the Utilities Menu A and the effect of the autostereogram is really quite amazing. As he says, although he has sent EUG the original picture, it will not be revealed until next issue. Don't you just love a mystery?

I personally like these. I was showing one to my wife. She said that they were stupid and mocked the way I looked at them saying I looked like I was mad. It turned out that she is able to see the hidden shape without trying.

Gus Donnachaidh, EUG #18