Hi sorry for the late reply.
Yes you find the good lines, you can edit it like this :
if r == 0 w, h = w_max, w_max * height / width h, w = h_max, h_max * width / height if h > h_max elsif r == 1 w, h = w_max, h_max else w, h = width * (r-1), height * (r-1) ...