Friday, November 12, 2010

BBC on the BBC

Over the past few days I've been converting nitrofurano's image filters from stand-alone sdlBasic programs to Python-Fu image filters for The GIMP. But, so far, there has been a particularly noticeable absence - the BBC Micro.

Partly that was because the BBC Micro world has already been utterly spoilt in the image conversion department by Francis G. Loch's incredible BBC Micro Image Converter. It's a highly professional piece of software and does it all. I've posted about it many times here and the work I've done for Retro Software would have been utterly impossible without it.

BBC Micro Image Converter by Francis G. Loch

But partly it was because Francis' program had inspired me to try and find out more about the mind-boggling array of dither options he had included in his program. It boasted a host of exotic sounding names like "Floyd-Steinberg", "Sierra", "Jarvis, Judice and Ninke" and "Stucki".

Paulo had used a technique called Bayer ordered dither in his filters, which is similar to the traditional half-toning used in print. It's very powerful, very fast and gives you a lovely regular patterned effect on the images, which is sometimes just what you're after.

Naturally, Francis' BBC Micro Image Converter does this as well. But these exotic sounding names were the inventors of various flavours of another technique: error diffusion.

Error diffusion works by trying to compensate for the colour information lost by turning a pixel into a value from a restricted palette by sharing it out amongst the surrounding pixels.

After looking at the Wikipedia entry for Floyd-Steinberg, it looked like even I could understand how to program it and then after finding an excellent article here I realised that all the other filters did exactly the same thing. They just shared out the lost information (or quantisation error) to different pixels in different proportions.

And, after a couple of hours messing about in Python, I managed to get out a servicable MODE 2 image (click on the images to enlarge):

Floyd-Steinberg error diffusion, 100% strength

You can see just how effective error diffusion is when you compare the results to the same image processed with no error diffusion:

The same image with no error diffusion

Here's the original image for comparison:

I'm the one on the right

I excitedly added a range of different filters into my BBC Micro image filter:

Take your pick!

There was a problem though. Sierra3 was taking well over 70 seconds. This sluggishness was caused by the inefficient way in which I was checking that a pixel was within a certain range in Python.

Sierra3, 100% Strength - and very slow!

An error message I had been getting during development rather ironically proved to be the key to solving the speed problem. Instead of using time consuming range() functions to see if pixels were inside a particular range, I could use exception handling and check for an IndexError instead. This was very fast - it sped the filter up by a factor of at least four. Mind you, it still crawls along compared to Francis' version!

The next thing I needed to add to the filter was something called "Serpentine parsing". This means that instead of processing the image from left to right as it moves down, the computer processes the image backwards and forwards. This helps to stop all the error diffusion going in just one direction - smearing all the errors to the right.

Finally, pinching another one of Francis' excellent ideas, I added a strength control to the filter to allow you to control how strongly the error diffusion works.

Finished interface

Here is Test Card F with 50% Floyd-Steinberg error diffusion strength:

Floyd-Steinberg error diffusion, 50% strength

And here it is with 25% Floyd-Steinberg error diffusion strength:

Floyd-Steinberg error diffusion, 25% strength

So, a BBC Micro Mode 2 image filter for The GIMP, that can be downloaded from here. However there are numerous refinements that need to be added to it. But they will have to wait for another day.

Test Card F Copyright © 1967 BBC, ITA and BREMA.

No comments: