Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding a String as a picture causes compression

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:

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:

  1. The first character of the string is popped off

  2. 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]).

  3. Each digit is treated as red, green and blue channels, which are given to BufferedImage's setRGB method.

  4. 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.

like image 936
Carcigenicate Avatar asked Jan 22 '17 22:01

Carcigenicate


1 Answers

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.

like image 149
thebjorn Avatar answered Sep 19 '22 23:09

thebjorn