First proper working version. For scales that are integer factors it works satisfactorily fast. For other scales it works slower, with significant time proportional to the number of pixels in the final image.
There is also likely a problem with the alignment of pixels (something like half-pixels stretching or shifting).