Algorithm to rotate an image 90 degrees in place? (No extra memory) Algorithm to rotate an image 90 degrees in place? (No extra memory) c c

Algorithm to rotate an image 90 degrees in place? (No extra memory)


This might help: In-place matrix transposition.

(You might also have to do some mirroring after the transposition, as rlbond mentions).


If you read the image from memory in "the wrong order", it's essentially the same as rotating it. This may or may not be suitable for whatever you're doing, but here goes:

image[y][x] /* assuming this is the original orientation */image[x][original_width - y] /* rotated 90 degrees ccw */image[original_height - x][y] /* 90 degrees cw */image[original_height - y][original_width - x] /* 180 degrees */


Not sure what processing you will do after the rotation, but you can leave it alone and use another function to read rotated pixel from the original memory.

uint16_t getPixel90(Image *img, int x, int y) {    return img->data[(img->height - x) * img->width + y];}

Where input parameter x and y has swapped dimension from original