def scaling_transform(w1, h1, w2, h2):
    """Rescale rectangle (w1, h1) to fit in rectangle (w2, h2) without changing the ratio."""
    r1 = w1 / h1
    r2 = w2 / h2
    if r1 <= r2:
        h = h2
        w = r1 * h2
    else:
        w = w2
        h = w2 / r1
    return w, h
