What's the purpose of modifying the input image? This is a pretty nasty and unexpected side effect.