Todays tutorial is brought to you courtesy of Dermot Balson from the lovely city of Perth (the most remote city on Earth according to Bill Bryson). Dermot noted that Spritely (the image editor which comes with Codea) produces some fairly large and unwieldy image files. Given Dermot's background in compression he immediately worked out a way to dramatically reduce these files in size.
Essentially, Dermot creates an array of unique colours, and builds a string for each of them which consist of two-digit cell addresses.
Let me hand over to Dermot (@Ignatz on the Codea Forum) to explain how it all works.
Run Length Encoding (RLE) is a well known compression technique. Essentially, you run through the cells, and every time the color changes, you store the last color and the number of cells it applied to.
The RLE code can be downloaded below and has several classes.
The RLE compression code can turn any image (with reason - see below) into code strings, and the Decode function can reassemble the image from these strings.
It can take images from the libraries, or Spritely code, and convert to compressed code.
The ImageCoder:code function takes the image as input and prints the code to recreate it.
The Test tab shows how this code is used. NB this function also returns the two strings required to decode the image. This is only for testing purposes so the Main tab can show how it works - in practice, users will use the printed code.
The Decoder:Decode function takes two strings as input and returns an image
The Main function carries out several tests:
All three images above are drawn on screen. The first two images have their originals drawn above them for comparison purposes.
The ImageCoder tab is needed to encode images.
The Decoder tab needs to be included in any project where you want to decode the RLE images.
The level of compression depends on many factors, but as one indication, the size of the Spritely code in this example is about 28,000 chars, compared to 1,000 chars for the RLE version.
To be fair, this is largely because the Spritely image has a background color, meaning all 1,024 cells have to be coded. An image without a background would be much smaller.
The practical limits on image size and complexity are processing speed and storage, because the greater the number of colors and cells, the more space we have to allow for each item. Currently, it can allow for up to 4,096 unique r,g,b,a combinations, and up to 4,096 characters of the same color in a sequence. Increasing storage from 3 to 4 chars per item would increase capacity by a factor of 16, but would blow out the data by 1/3.
There are all sorts of ways to tweak for speed, but this may be good enough. I will tweak if there is demand for it.
Dermot has been kind enough to share his RLE coding and decoding classes. It is very well commented, so you should have no problems in following what he has done. You can download the complete project (useful for pasting directly into Codea) or the individual classes using the links below: