Thanks snibgo!
That snippet is handy.
By the way, I have found a bug: when I adapt the image to the original AR, I should be checking whether there's enough space in both directions, rather than just adding half of the pixels to each direction.
I will fix it in the weekend
