Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How did this person code "Hello World" with Microsoft Paint?

Tags:

c++

c

paint

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"?

like image 322
Eamonn O'Brien Avatar asked Apr 07 '11 23:04

Eamonn O'Brien


People also ask

Can you code in MS Paint?

Well, this application gives MS Paint a boost, and lets MS Paint highlight, compile, and execute code, with just a few clicks of a button, and only text coming from MS Paint. It is now much more practical than things like Word, Notepad, and obviously Eclipse.

What did Microsoft replace paint with?

Microsoft has retired MS Paint in favor of Paint 3D.


2 Answers

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.
like image 124
Matteo Italia Avatar answered Sep 28 '22 15:09

Matteo Italia


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.

like image 38
Andrew Grimm Avatar answered Sep 28 '22 15:09

Andrew Grimm