public static byte[] RotateImage(byte[] pixels, int w, int h, int rotation)
{
//rotation is in multiples of 90 degrees CW.
byte[] newPixels = new byte[3*w * h];
switch (rotation)
{
case 0:
return pixels;
case 2:
//simply reverse direction
for (int i = 0; i < w * h; i++)
{
int newIndex = w * h - 1 - i;
newPixels[3 * i] = pixels[3 * newIndex];
newPixels[3 * i + 1] = pixels[3 * newIndex + 1];
newPixels[3 * i + 2] = pixels[3 * newIndex + 2];
}
return newPixels;
case 1:
for (int i = 0; i < w * h; i++)
{
int newIndex = (h - 1 - i / w) + h * (i % w);
newPixels[3 * newIndex] = pixels[3 * i];
newPixels[3 * newIndex + 1] = pixels[3 * i + 1];
newPixels[3 * newIndex + 2] = pixels[3 * i + 2];
}
return newPixels;
case 3:
//the below makes me a terrible person.
newPixels = RotateImage(pixels,w, h, 2);
newPixels = RotateImage(newPixels,w, h, 1);
//for (int i = 0; i < w * h; i++)
//{
// int newIndex = (w * (h - 1) + 1 + i/w) - h * (i % w);
// newPixels[3 * newIndex] = pixels[3 * i];
// newPixels[3 * newIndex + 1] = pixels[3 * i + 1];
// newPixels[3 * newIndex + 2] = pixels[3 * i + 2];
//}
return newPixels;
}
//it should never come to this.
return newPixels;
}