this is a good algorithm. I'll steal it for the blitbuffer code (which can do this conversion).