November 27th, 2006
The other day there was a thread on a mailing list related to getting large amounts of text in to a Shockwave movie and keeping the bandwidth small. Since you can’t use the UnZip Xtra in Shockwave someone mentioned the idea of having data that represents text placed inside an image, and then convert the image to the text in the dcr.
For some reason I suddenly thought of when I was a kid trying to write top secret notes that only my buddy would be able to read if he knew “the code,” so I figured I’d play with this for a few minutes after eating a bit too much turkey the other day.
In terms of a basic image, each pixel of the image consists of red, green, and blue (when working in the RGB color model). Since each pixel has three colors (and each color ranges from 0 to 255) I decided to use a very simple method where each pixel will represent three letters of the text. Using a 100 x 100 pixel image I can get 30,000 characters of text. A typical png of that size is around 6k, so the png is very small compared to the amount of data you can extract.
When converting from text to image I embed the total number of characters in the first pixel (see code for method used), and then use the remaining pixels to store the characters (three characters/pixel). Each character is turned in to a number using the
charToNum function, and that set of three characters is then turned in to the pixel using
When going from image to text I basically do the opposite. I read the first pixel (using
getPixel) to see how many characters I have to deal with, and then start reading all the rest of the pixels and using
numToChar to translate the red, green, and blue colors in to characters. The reason I put the total number of characters in that first pixel was so that I wouldn’t have to read all 10,000 pixels every time (only as many as needed to save time).
This was a very simple way to do this and you could add a lot of features that would enable more interesting encryption methods, as well as other ways to encode the data that would lead to far more compression.
Note: I have not added any error checking to the source code, so many thing could cause an error (as well as the shockwave sample).