I'm working on a program that takes a string, turns each character of the string into a color, then draws the colors left-to-right, top-down across an image. The image can then be decoded using the same program to get the original message back. As an example, here's clojure.core
, encoded as an image:
I wrote this just as a toy, but I noticed an interesting property of the images it produces: they're smaller than the original messages were as text. For the clojure.core
, it's 259kb as text, but only 88.9kb as an image (above) (both values are "size on disk"). To ensure data wasn't being lost, I decoded the image, and got the original message back.
How is this possible? I'd think the image (png
format) would have headers and other extra information that would inflate the size.
The entire clojure.core
contains 265486 characters (according to Notepad++), which means that each character is basically taking up a byte.
From working with the BufferedImage
class (Java), it appears as though colors are stored as 4-byte integers, so shouldn't each pixel require ~4x the memory?
Here's how it's encoded:
The first character of the string is popped off
It's translated into a color by getting it's ASCII value, multiplying it by a large number (so it covers the range of possible colors better), then that number is converted into a 3 digit, base 256 number ([123 100 200]
).
Each digit is treated as red, green and blue channels, which are given to BufferedImage
's setRGB
method.
The position
indicator is advanced, the next character is popped, and the process repeats until the entire message is encoded.
The algorithm is a little convoluted right now. @Thumbnail suggested a far better way on Code Review, but I haven't implemented it yet. Since the results are the same though, that shouldn't make a difference for the question.
Portable Network Graphics (PNG) is a raster graphics file format that supports lossless data compression (from https://en.wikipedia.org/wiki/Portable_Network_Graphics), iow. the image data is compressed when stored as a .png file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With