How do I rotate a warping grid without changing the warped image's orientation? I have a warping grid, that has to be applied to an image. When I apply the grid, I get the results I expect.

Now, I want to apply the same grid to the same image, but the image is modified in the following way: it is rotated 270 degrees counterclockwise. 

The grid is stored in memory as an array of points. It is [-1, -1] - [1, 1] normalized. I am trying to rotate the whole thing around the origin, that is [0, 0] id est the center of the image. Also, I am trying to rotate in discrete steps of 90 degrees.

P1 P2 P3 

P4 P5 P6

P7 P8 P9


These are destination coordinates of the points in the image. For example, the lower right corner of the input image has coordinates of [1,1]. In the output image, this point is moved to [P9.x, P9.y] (a.k.a. forward mapping).

I transpose the matrix:

P1 P4 P7

P2 P5 P8

P3 P6 P9


Then I apply to every point the transformation

x = y'

y = -x',


where ' denotes old coordinate and lack of prime denotes new coordinate. I apply the derived grid to the rotate

However, I do not get the expected warp. This task seemed incredibly simple to me, but I find myself struggling with the strange behaviour for a third day. Could someone point out my mistake or a procedure to get things right?
After three and a half days of cursing, a couple of beers with a friend helped solve the problem. My mistake was that the reordering of points needed to be not exactly transpose, but close. So:

To rotate a grid 90 degrees counter-clockwise:


transpose the grid and reflect it along the horizontal central line
apply the transformation 


x = y'

y = -x',

where primes denote old coordinates. 

To rotate a grid 180 degrees


reflect the grid around its horizontal and around it's vertical axis
apply the transformation


x = - x'

y = - y'

To rotate a grid 270 degrees:


transpose the grid and reflect it along the vertical central line
apply the transformation 


x = - y'

y = x'