Not going to lie. I really didn't understand the algorithm this was coding and optimizing. depending on the bit depth you basically do a 1-d bressenham mapping x pixels into y pixels and the same for the y direction either duplicating scaled lines or dropping lines in the source image.