Convert TIF Images to Apple II BASICSun, Apr 19, 2015 at 11:07AM
Over the past few months, I’ve been developing a piece of software with two other guys, and last month was one of their birthdays. Like me, they both are huge into retrocomputing (we’ve got some of our collection in the office).
Part of the birthday gift I came up with required me showing a bitmap graphic on an Apple II display. However, you can’t just throw an image on a floppy disk and do
open -A Preview. There’s two methods for getting an image on-screen:
- Package a program that can read the image, alongside the image file.
- Compress and encode the image data into a BASIC program, and print the data to screen.
For what I wanted to do (the birthday boy may be reading this, and I don’t want to spoil any details – he hasn’t figured his gift out yet), option number two was much more feasible.
The Apple II has a hi-res graphics mode of 280x192px. Broker encoded his images as
DATA statements, one statement per line. Each line is composed of 18 chunks – 17 chunks of 16 pixels (272 pixels), and one chunk of 8 pixels (+8 pixels = 280px). Each ‘chunk’ is stored as an integer. To read the pixel data, we divide this number by 2 – the pixel’s color (black or white) depends on whether this result is odd of even. If it’s odd, we round it down, and keep on dividing until that chunk’s pixels have been read.
Now that I knew how to decode the images, making the encoder is just doing everything backwards. I wrote it in C, and it’s available on Github.
I chose TIF images, because they’re so easy to work with. Plus, Photoshop gives me the option of how I want to encode the pixel data, so this made getting the bytes in a sensible order a snap.
The best way to encode images in Photoshop (I use CS4) is to select a two-color pallate (indexed colors). Saving the image as 8-bit and two-color means that each byte will correspond to one pixel: perfect for easy reading! All I needed to do is iterate over the bytes.
There’s some other stuff with TIFs that aren’t clean (lots of image editors save them with a huge XML header), so to grab the image data I’m looking for a series of bytes that I’ve always found to be consistently behind the image data:
00 03 A0. We have 53,760 bytes of image data (280x192px), so we search for those three bytes and grab the 53,760 bytes before it. Presto! I know this isn’t the ‘clean’ way of doing it, but hey, it only needed to work for me and this one-shot birthday gift.
If you’d like to convert your own images, you can grab my tif2basic utility on Github.
PS: Here’s a sample of the BASIC generated.