Just For Fun: Autostereograms in Haskell

I’ve added some code here, which I wrote last night, to generate autostereograms from depth maps. It’s written in Haskell, but that doesn’t mean you should blame Haskell for its shortcomings.

To use:

Install GHC and gtk2hs. Any recent version should do.

Copy this code into a file and build with ghc --make -O2 TheFile.hs.

Generate a tiling background image of your choice. GIMP’s Filter -> Map -> Make Seamless helps a lot with this, and it has some nice options under Filter -> Render to generate nice-looking images. The important thing is for your image to be seamlessly tileable, and have lots of easily identifiable points. Save it as a PNG file.

Generate a depth map, on the order of about 100 x 100 pixels. (Too much larger, and you’ll have to profile and optimize my code for me.) Save it as a PNG file again.

Run the program with the depth map and the background image (in that order) as parameters.

This code basically works, and realistically that’s about all I am going to do right now. There are lots of things that could be improved about it. The big three are: lots of performance issues; should be able to write out to a file instead of just the screen; and there’s something funky in the drawing that causes the depth to slope outward rather gently to each side no matter what the depth map looks like. As far as performance, the problem is with a lot of fiddling with lists for control flow; I was able to do considerably better with Cairo; but unfortunately, Cairo’s matrices only allow for affine transformations, which doesn’t seem to be quite enough to scale between arbitrary trapezoids, so the result didn’t look great and I reverted to drawing pixel by pixel. The last problem may be a rather fundamental issue in how I’m approaching the problem; but the current code works well enough to get the point.