Image orientation is indicated using a three-character string, with each
character indicating the approximate world-space direction of the positive
axes in the first, second and third dimensions, in order. Each character may
be `R' for left-to-right, `L' for right-to-left, `A' for posterior-to-
anterior, `P' for anterior-to-posterior, `S' for inferior-to-superior, or
`I' for superior-to-inferior. The default for NIfTI is RAS, meaning that the
first dimension points towards the right, the second towards the front and
the third towards the top. An xform matrix is an affine transform relative
to that default.
The upper-left 3x3 matrix in a 3D affine transform governs scale, rotation
and skew, while the last column is a translation. (The rotation
function extracts the rotation part alone.) The final row is always
(0,0,0,1). Reorienting an image involves permuting and possibly reversing
some of the axes, both in the data and the metadata. The sense of the
translation may also need to be reversed, but this is only possible if the
image dimensions are known.