Real-time Video Gamma Correction Possible

Someone asked Robert Reinhardt if it was possible to do gamma correction on video. This is a great detail you can pay attention to, if you want to be nice to everyone. For instance, say you published a video to look good on a Mac, with a typical gamma of 2.2. You could easily inspect flash.system.Capabilities.os and modify the gamma of the video on the fly if your viewer is using Windows, with a typical gamma of 1.8.

My first impression was to use a ColorTransform matrix, but this is only able to transform colors in a linear equation:
output = a * input + b
Gamma is a simple power curve, so this wasn’t going to cut it. Then I found that BitmapData.paletteMap was the solution. With paletteMap, you can perform any function on the colors in an image, or rather 4 functions on each channel RGBA. You simply precalculate 256 values for all possible 8 bit color values in each channel, and paletteMap uses this as a lookup table. This made the power curve of a gamma correction trivial to calculate and apply, and the performance even on large videos is more than acceptable.

Here’s the example. Just right-click to view source.
Real-time Gamma Correction [AS3]
Or just view the super-simple Gamma classs.

This entry was posted in AS3, Flash, Flex, Programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

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>