if I got it correctly the affine matrix as found in nifti images casts the images to the RAS orientation if apllied, right?
Is there a nice way to calculate a matrix based on the stored affine that casts the image to an other orientation?

The affine is the matrix that transforms a voxel index to an RAS coordinate. I’m not sure what you mean by a matrix that casts the image to another orientation; could you rephrase?

Hi,
I would like to apply affine transformation to images, but not to align them to the standard RAS space but to SPL space (z,y,x with origin up left, if I’m not mistaken).

The NIfFTI SForm and QForm map voxels as stored on disk to MNI space (e.g. X+ = R, Y+ = A, Z+ = S). Often, MRI scans are not acquired orthogonal to the scanner bore, therefore there is a rotation component. Therefore, if one tries to reslice voxels to a spatially-aligned grid, one will need to interpolate values, which is lossy. An alternative is to reorder the dimensions to get them as close to the desired orientation as possible, storing the residual value in your QForm/SForm. I suspect this is what you want to do. I would look at the elegant code in nii_viewer. At the moment is will reorder voxels to RAS or LAS depending on the value leftHand. You could adjust this for any layout you want (there are a total of 48 possible orthogonal permutations: six of the 3 spatial dimensions without replacement [1, 2, 3], multiplied by the eight combinations for the sign with replacement [+/-, +,-, +,-].