restructured the tone-mapping code