Good day, dear habrachelovek. On Habré already
thought about how to hide the text in the bitmap-image. Unfortunately, I did not find topics on this topic and decided to fill this gap. Under the cat you will find a way to hide the text in a bitmap, as well as the implementation in C #.
Formulation of the problem
Hide any text in windows-1251 encoding into a 24-bit bitmap image and paste it back without distortion.
Bmp file structure
First let me remind you what a bitmap file is. I am sure that you all know this perfectly well, it will just be clearer to describe an algorithm for hiding text based on the material presented. So. Any bmp file consists of four parts:
- File header
- Image title (may be absent)
- Palette (may be missing)
- Image itself
The header of the file contains service information, including the bit pattern. By the way, for the 24-bit pattern palette is not used. Since we in the task clearly indicated that we will work only with 24-bit images, ideally, we can check the input image for compliance.
We now turn directly to the image itself. As you know, the bmp format does not provide compression by default (although there is support for
RLE compression). Thus, each pixel in our case is encoded with 24 bits, one byte for each color component. Therefore, we can encode no more no less, but exactly 16777216 colors. For clarity, here is a picture:
The idea of the text hiding algorithm
Perhaps you already guessed what the idea. The thing is that the eye of an average person (not a professional artist or photographer) distinguishes much less colors than was indicated above. Not a single book has a clear answer to the question of how many colors the eye can distinguish, but the largest figure I met is 10 million. It follows that several of the lower eight bits allocated to each component of color can be borrowed for our self-serving goals.
A few numbers: for example, take and brazenly take away from the RGB component of the two younger bits. That is, of 24 bits, we will have 18, which can encode exactly 262,144 colors. Now let's take the text in windows-1251 encoding, in which each character is represented by 8 bits. By simple mathematical calculations we get that 3 characters can be saved in 4 pixels. Thus, in the picture 1024x768, where 786432 pixels you can save 589824 characters. Not bad, huh? For clarity, here are two pictures. The first is the original image, and the second is the image, in which the lower two bits of each color component are filled with text. At once I will make a reservation that the images are converted to png in order to regret the traffic.
')
Original Image:Image containing textIf you look closely, then on the second image the colors appear dimmer. Yes it is. But we know with you that something is wrong in the presented image, and if you didn’t know, you wouldn’t have guessed that there is some text hidden in it. By the way, the phrase “Hello World !!!” is hidden in the image. =) "Multiplied 100 times.
That's all. As you can see, the idea is extremely simple. By the way, the presented method is called
LSB (thanks to
frol for the help). Finally, you can see the implementation in C #.
C # implementation
The presented implementation does not claim to be awarded in the “Perfect Code” style; it only demonstrates the described algorithm in practice. In this case, I was not chasing the beauty of the code, but for clarity.
Here you can download the archive and skiters (~ 20 kb)
Work examples
Original Image:Image with 2 bits color components reserved for textImage with 4 bits of color components reserved for text.Image with 6 bits of color components reserved for textThanks for attention!