Skip to content

Instantly share code, notes, and snippets.

@sbaar
Created January 3, 2016 11:12
Show Gist options
  • Save sbaar/0a85727d599fcbd5acd1 to your computer and use it in GitHub Desktop.
Save sbaar/0a85727d599fcbd5acd1 to your computer and use it in GitHub Desktop.
nv21 180 rotate
/**
* Borrowed from http://stackoverflow.com/questions/23107057/rotate-yuv420-nv21-image-in-android
* Note, this will distort images if rotating 90 or 270 because it does not work well with rectangles.
*/
public static void rotateNV21(byte[] input, byte[] output, int width, int height, int rotation) {
if (rotation==0){
System.arraycopy(input, 0, output, 0, input.length);
return;
}
boolean swap = (rotation == 90 || rotation == 270);
boolean yflip = (rotation == 90 || rotation == 180);
boolean xflip = (rotation == 270 || rotation == 180);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int xo = x, yo = y;
int w = width, h = height;
int xi = xo, yi = yo;
if (swap) {
xi = w * yo / h;
yi = h * xo / w;
}
if (yflip) {
yi = h - yi - 1;
}
if (xflip) {
xi = w - xi - 1;
}
output[w * yo + xo] = input[w * yi + xi];
int fs = w * h;
int qs = (fs >> 2);
xi = (xi >> 1);
yi = (yi >> 1);
xo = (xo >> 1);
yo = (yo >> 1);
w = (w >> 1);
h = (h >> 1);
// adjust for interleave here
int ui = fs + (w * yi + xi) * 2;
int uo = fs + (w * yo + xo) * 2;
// and here
int vi = ui + 1;
int vo = uo + 1;
output[uo] = input[ui];
output[vo] = input[vi];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment