If you'd use lite-c it were easy.... I think there's something about that on the wiki, look for "postprocessing helpers" in the "code" section of the wiki.