Affine transform for rotating in place an A4 ratio (A3, A5, etc.) image for e.g. converting landscape to portrait

I'm sure there's a better way of doing this with only one scale operation, but I don't know the maths. This took hours to find, and so hopefully it will save someone else the time. The Java code I'm sure can be translated to other languages relatively easily.

AffineTransform r = new AffineTransform();
r.rotate(Math.toRadians(90), width/2, height/2);
r.scale(width, height);
 * -(Math.sqrt(2d)-1d)/2d
 * = half the distance as % left between
 * the ratios of Ax paper;

r.translate(-(Math.sqrt(2d)-1d)/2d, (Math.sqrt(2d)-1d)/2d * (1/Math.sqrt(2d)));
r.scale(Math.sqrt(2d), Math.sqrt(2d)/2d);