102

I have just seen this within the past few days and cannot figure out how it works. The video I talk about is here:

It's the top rated answer from this Stack Overflow question: Why was this program rejected by three compilers?

How is this bitmap able to show a C++ program for "Hello World"?

Community
  • 1
  • 1
Eamonn O'Brien
  • 1,085
  • 2
  • 8
  • 3
  • @Close voter: How is this not a real question? It's got an objective answer. – Andrew Grimm Apr 07 '11 at 23:09
  • 7
    bitmap values are just bits in a file. If you interpret those bits as ASCii then it will show something. Careful selection of bits and you can write a letter, or the constitution, in a bitmap (modulo the file header). What isn't to understand? It's just a silly thing, like the worlds worst editor. – Thomas M. DuBuisson Apr 07 '11 at 23:11
  • 3
    For what it's worth, there *is* at least [one programming language](http://en.wikipedia.org/wiki/Piet_(programming_language)) where the source code is expressed as an image. Not that this particular image would work with it, but... – Michael Madsen Apr 07 '11 at 23:44
  • 1
    The thread you were looking for http://stackoverflow.com/questions/5508110/why-is-this-program-erroneously-rejected-by-three-c-compilers/5509143 – phwd Apr 09 '11 at 02:33
  • 6
    That animated GIF is killing my browser (IE and Chrome) - there are better ways to post videos than an animated GIF. – MusiGenesis Apr 10 '11 at 12:47
  • I made several one of these, you just need to fill create an image big enough to hold all characters, then just use a hex editor to insert them. –  Apr 11 '11 at 18:01
  • Obviously, this person is just using an enhancement of the [Whitespace](http://en.wikipedia.org/wiki/Whitespace_%28programming_language%29) programming language. – MusiGenesis Apr 07 '11 at 23:32
  • 3
    @MusiGenesis Oh, how I remember the problems of 2011. – Charles Clayton Nov 20 '15 at 18:27
  • 1
    If the program is C++ why did you add the C tag? (C is a different language) – Toby Aug 11 '17 at 10:54

3 Answers3

58

A BMP (DIB) image is composed by a header followed by uncompressed1 color data (for 24 bpp images it's 3 bytes per pixel, stored in reverse row order and with 4 bytes row stride).

The bytes for color data are used to represent colors (i.e. none of them are "mandated" by the file format2, they all come from the color of each pixel), and there's a perfect 1:1 correspondence between pixel colors and bytes written in the file; thus, using perfectly chosen colors you can actually write anything you want in the file (with the exception of the header).

When you open the generated file in notepad, the color data will be shown as text; you can still clearly see from the header (the part from BM to the start of the text), that is mandated by the file format.

In my opinion this video was done this way: first the author calculated the size needed for the bitmap, and created a DIB file of the correct size filled with a color that expands to a simple pattern (e.g. all bytes 65 => 'A'); then replaced such pattern with the "payload" code, as shown in the video.

Notice however that it's not impossible to hand-craft the whole thing with notepad - with the color chooser dialog, an ASCII table and a basic knowledge of the DIB format it can be done, but it would be much much slower and error-prone.

More info about the DIB format


  1. There are RLE compressed DIBs, but in this case uncompressed bitmaps are used (and they are used really rarely anyway).
  2. With the exception of the stride, that was avoided using rows multiple of 4 bytes.
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
20

I assume you're referring to the answer to one of the April Fools questions.

My guess is that each pixel has a binary representation for it. And that each character in source code has a binary representation for it.

The person who created the program must have worked out the color for each pixel that'd have a binary representation that'd correspond to each character.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
  • 7
    +1: in the end, bytes are bytes. Notepad interprets them as characters of text while mspaint interprets them as pixels in a bitmap. The leading "junk" text is additional image information (possibly things like resolution, version, etc.). – Chris Schmich Apr 07 '11 at 23:12
6

From a theoretical computer science point of view, it would be interesting to ask, if every program can be written in such a way so that, viewed as a bitmap, you actually saw the source code that does the same thing. If you are seriously interested in such results, read e.g. about the Kleene's fixed point theorem.

Program-as-an-image can also be viewed as a form of code obfuscation. Not that it were particularly practical...

Sergey Orshanskiy
  • 6,794
  • 1
  • 46
  • 50