OpenCL Hard at Work: The Color Substitution Filter

I’ve been asked by a few people “Why do you require OpenCL? Photoshop doesn’t need it and they sell a hojillion copies!”. My response is twofold:

1) I want to push the envelope and incorporate the latest technologies.
2) I don’t need to sell a hojillion copies…just enough to pay the mortgage!

Before continuing, here’s a quick video of the color substitution filter in action (pop the player out to 1080p for the best viewing).

The concept of image filtering is simple: given a source image, apply some type of algorithm to the image which yields a modified version. The filter can be as simple as inverting the colors or something a little more taxing such as a multi-pass convolution filter. Regardless of how simple or complex the filter is, it must be run on every single pixel of the image.

Given a 3000×2000 image (common resolution from a compact digital camera) that means our filter must be run 6 million times to produce the output image.

Some filters have parameters that the user can tweak to vary the intensity of the effect. Using PhotoMonkee’s Color Substitution Filter as an example, the user can spin the color wheel to change the mapping of source and destination hues. Each time the user modifies a parameter we’ve got to reapply the filter to all 6 million of our pixels.

Here’s where things get sticky.

The computer is going to be awfully busy trying to update 6 million pixels when the application asks to redraw the UI because the user moved a slider or clicked a button. Best case? The application spins the filter off to a separate thread so the UI doesn’t hang but you’re still going to end up with a very slow rendering of your image.

What about a preview?

Great idea! Instead of trying to update 6 million pixels we can scale down the image to 256×256 and just update that scaled down preview until the user clicks “accept”. At that point we can give it the full monty. That sucks! I want to see my beautiful 3000×2000 image updated on my 24″ monitor as I tweak the filter. Unfortunately any application that only makes use of the CPU is bound to this slow fate.

How does OpenCL fix this?

OpenCL leverages a concept called heterogeneous computing. In short, it can farm out work to all of your CPU cores and all of the processing units on your graphics card (GPU). In particular, those GPU processing units are really good at crunching image data. An HD resolution image (1920×1080) contains 2,073,600 pixels and is a walk in the park, even on older GPU’s.

OpenCL compatibility issues

OpenCL is still a relatively young API at only 3.5 years old. The initial driver implementations by nVidia and ATI were clunky and are only now getting stable. In addition,  Intel is relatively new to the game with their driver offering for CPU-only acceleration. Windows doesn’t yet ship with a driver which, until that happens, means OpenCL isn’t quite mainstream. Think back to the days when OpenGL didn’t come stock with your OS.

Going Forward

Even given the compatibility issues I think that OpenCL is here to stay and is laying a solid foundation for high performance applications in the future. Just this week I started coding up the image filter plugins so 3rd party developers can add filter to PhotoMonkee. Those plugins will all have the ability to leverage OpenCL for accelerated processing.

Interested in getting involved with PhotoMonkee plugin development? Send an email to “support@photomonkee.awesome” =~ s/awesome/com/; and we’ll let you know when the API becomes available. Thanks for your interest!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>