Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. public static byte[] rotateNV21(final byte[] yuv,
  2.                                             final int width,
  3.                                             final int height,
  4.                                             final int rotation)
  5.     {
  6.         if (rotation == 0) return yuv;
  7.         if (rotation % 90 != 0 || rotation < 0 || rotation > 270) {
  8.             throw new IllegalArgumentException("0 <= rotation < 360, rotation % 90 == 0");
  9.         }
  10.  
  11.         final byte[]  output    = new byte[yuv.length];
  12.         final int     frameSize = width * height;
  13.         final boolean swap      = rotation % 180 != 0;
  14.         final boolean xflip     = rotation % 270 != 0;
  15.         final boolean yflip     = rotation >= 180;
  16.  
  17.         for (int j = 0; j < height; j++) {
  18.             for (int i = 0; i < width; i++) {
  19.                 final int yIn = j * width + i;
  20.                 final int uIn = frameSize + (j >> 1) * width + (i & ~1);
  21.                 final int vIn = uIn       + 1;
  22.  
  23.                 final int wOut     = swap  ? height              : width;
  24.                 final int hOut     = swap  ? width               : height;
  25.                 final int iSwapped = swap  ? j                   : i;
  26.                 final int jSwapped = swap  ? i                   : j;
  27.                 final int iOut     = xflip ? wOut - iSwapped - 1 : iSwapped;
  28.                 final int jOut     = yflip ? hOut - jSwapped - 1 : jSwapped;
  29.  
  30.                 final int yOut = jOut * wOut + iOut;
  31.                 final int uOut = frameSize + (jOut >> 1) * wOut + (iOut & ~1);
  32.                 final int vOut = uOut + 1;
  33.  
  34.                 output[yOut] = (byte)(0xff & yuv[yIn]);
  35.                 output[uOut] = (byte)(0xff & yuv[uIn]);
  36.                 output[vOut] = (byte)(0xff & yuv[vIn]);
  37.             }
  38.         }
  39.         return output;
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement