# Softwire Speed Coding Challenge 2013: Hidden Images – Solution

17 October 2013, by Chris Arnott

Hello everyone,

I am sure you have been eagerly awaiting the solution to our previous blog post on speed coding so that you can see how you compared to Softwire’s elite coders.

The fastest solution to the hiding images question was Rupert Wood who had puzzled out a solution in an impressive 8 minutes. He discovered the picture hidden in this image.

Here is his solution (written in C#), which you are welcome to use to find the picture yourself:

using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SSCThreeThree { class Program { static void Main(string[] args) { var bitmap = (Bitmap) Image.FromFile("ImageWithHiddenContent.bmp"); int width = bitmap.Width/3; int height = bitmap.Height/3; var output = new Bitmap(width, height); for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { int r = 0; int g = 0; int b = 0; int power = 1; for (int y0 = 0; y0 < 3; ++y0) { int m = (y0 < 2) ? 3 : 2; for (int x0 = 0; x0 < m; ++x0) { var c = bitmap.GetPixel((x * 3) + x0, (y * 3) + y0); if ((c.R && 1) != 0) r += power; if ((c.B && 1) != 0) b += power; if ((c.G && 1) != 0) g += power; power <<= 1; } } output.SetPixel(x, y, Color.FromArgb(r, g, b)); } } output.Save("Output.bmp", System.Drawing.Imaging.ImageFormat.Bmp); } } }

Solution designs

The solution to this problem required code that would do the following things:

- Looping through an image’s pixels.
- Extracting 8 bits of data from 9 pixels.
- Retrieving the least significant bit of each colour from each pixel.
- Assembling the bits in order to construct a new pixel.
- Storing the pixel and outputing the image.

**Steps 1 and 5** were pretty straight forward and everyone coded them in a similar way to Rupert above. The steps that people varied on slightly were 2,3 and 4.

**Step 2: Extracting 8 bits of data from 9 pixels.**

The majority of people looped through all 9 pixels in order, skipping the final pixel (which was redundant). One alternative to this method was to loop 8 times selecting the correct pixel each time:

var orig = bitmap.GetPixel(x * 3 + i % 3, y * 3 + i / 3);

**Step 3: Retrieving the least significant bit of each colour from each pixel.**

Two methods were used to retrieve the least significant bit of the pixels colour. They were:

- To use a bitwise & operator with 1 (as in Rupert’s answer).
- To modulus the number by 2.

**Step 4: Assembling the bits in order to construct a new pixel.**

The majority of solutions kept track of which output pixel bit they were handling in a variable (called power in Rupert’s program). This variable needed to be doubled on each iteration, and this could be done by either bit shifting the variable by 1 or multiplying it by 2.

Rich Bradley came up with an interesting solution to steps 2, 3 and 4, which was to loop through all 9 pixels in reverse order, bit shifting the solution each iteration. This results in the storing of the final bit causing the redundant bit to be discarded (as shown below for red only):

int r = 0; for (int dy = 2; dy >= 0; dy--) { for (int dx = 2; dx >= 0; dx--) { var c = src.GetPixel(srcX + dx, srcY + dy); r = (byte)(r << 1) | (c.R && 1); } }

Conclusion

This was quite a simple question to solve, with the variation in answers arising from different approaches to basic operations on bytes. Hopefully you have discovered something interesting while answering this question and reading about coding techniques you may or may not have thought of. If you managed to solve the question using a different method to those outlined above, let us know in the comments below!