Finding an image in a mosaic

Browsing the Ludum Dare (see previous post) website I found this post from a friend Will. He made the following mosaic:


Pretty interesting to see, I’ve already worked with him on improving the algorithm to generate these mosaics in the past. But next he set me a challenge: Find your own game thumbnail, it is in there somewhere!

This is the screenshot of my game, used as thumbnail:


So I went though the thumbnails, one time.. and a second time… then I decided to solve it like a real programmer:



How does it work? Well it is pretty simple:

  1. Input #1: mosaic.jpg
  2. Input #2: Amount of thumbnails width and height
  3. Input #3: screenshot.png
  4. The program resizes my game screenshot to the thumbnail size.
  5. Next it loops over all sub-images of the mosaic.
  6. For every sub-thumbnail: Calculate the error  (+=Math.abs(mosaicPixelValue - screenshotPixelValue) for each color, for each pixel)
  7. Store the location of the thumbnail with the smallest error!

That is it, solved it in 10 minutes of coding!
(and another 10 minutes to make the visual conformation and make the animated gif).