本文共 7577 字,大约阅读时间需要 25 分钟。
常用YUV转RGB
- public class YuvToRGB {
- private static int R = 0;
- private static int G = 1;
- private static int B = 2;
-
- public static int[] I420ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfV = numOfPixel;
- int positionOfU = numOfPixel/4 + numOfPixel;
- int[] rgb = new int[numOfPixel*3];
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = (i/2)*(width/2);
- int startU = positionOfV + step;
- int startV = positionOfU + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int U = startU + j/2;
- int V = startV + j/2;
- int index = Y*3;
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
-
- return rgb;
- }
-
- private static class RGB{
- public int r, g, b;
- }
-
- private static RGB yuvTorgb(byte Y, byte U, byte V){
- RGB rgb = new RGB();
- rgb.r = (int)((Y&0xff) + 1.4075 * ((V&0xff)-128));
- rgb.g = (int)((Y&0xff) - 0.3455 * ((U&0xff)-128) - 0.7169*((V&0xff)-128));
- rgb.b = (int)((Y&0xff) + 1.779 * ((U&0xff)-128));
- rgb.r =(rgb.r<0? 0: rgb.r>255? 255 : rgb.r);
- rgb.g =(rgb.g<0? 0: rgb.g>255? 255 : rgb.g);
- rgb.b =(rgb.b<0? 0: rgb.b>255? 255 : rgb.b);
- return rgb;
- }
-
-
- public static int[] YV16ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfU = numOfPixel;
- int positionOfV = numOfPixel/2 + numOfPixel;
- int[] rgb = new int[numOfPixel*3];
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = i*width/2;
- int startU = positionOfU + step;
- int startV = positionOfV + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int U = startU + j/2;
- int V = startV + j/2;
- int index = Y*3;
-
-
-
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
-
-
- public static int[] YV12ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfV = numOfPixel;
- int positionOfU = numOfPixel/4 + numOfPixel;
- int[] rgb = new int[numOfPixel*3];
-
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = (i/2)*(width/2);
- int startV = positionOfV + step;
- int startU = positionOfU + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int V = startV + j/2;
- int U = startU + j/2;
- int index = Y*3;
-
-
-
-
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
-
-
- public static int[] YUY2ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int[] rgb = new int[numOfPixel*3];
- int lineWidth = 2*width;
- for(int i=0; i<height; i++){
- int startY = i*lineWidth;
- for(int j = 0; j < lineWidth; j+=4){
- int Y1 = j + startY;
- int Y2 = Y1+2;
- int U = Y1+1;
- int V = Y1+3;
- int index = (Y1>>1)*3;
- RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- index += 3;
- tmp = yuvTorgb(src[Y2], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
-
-
- public static int[] UYVYToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int[] rgb = new int[numOfPixel*3];
- int lineWidth = 2*width;
- for(int i=0; i<height; i++){
- int startU = i*lineWidth;
- for(int j = 0; j < lineWidth; j+=4){
- int U = j + startU;
- int Y1 = U+1;
- int Y2 = U+3;
- int V = U+2;
- int index = (U>>1)*3;
- RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- index += 3;
- tmp = yuvTorgb(src[Y2], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
-
-
- public static int[] NV21ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfV = numOfPixel;
- int[] rgb = new int[numOfPixel*3];
-
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = i/2*width;
- int startV = positionOfV + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int V = startV + j/2;
- int U = V + 1;
- int index = Y*3;
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
-
-
- public static int[] NV12ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfU = numOfPixel;
- int[] rgb = new int[numOfPixel*3];
-
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = i/2*width;
- int startU = positionOfU + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int U = startU + j/2;
- int V = U + 1;
- int index = Y*3;
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
-
-
- public static int[] NV16ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfU = numOfPixel;
- int[] rgb = new int[numOfPixel*3];
-
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = i*width;
- int startU = positionOfU + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int U = startU + j/2;
- int V = U + 1;
- int index = Y*3;
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
-
-
- public static int[] NV61ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfV = numOfPixel;
- int[] rgb = new int[numOfPixel*3];
-
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = i*width;
- int startV = positionOfV + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int V = startV + j/2;
- int U = V + 1;
- int index = Y*3;
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
-
-
- public static int[] YVYUToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int[] rgb = new int[numOfPixel*3];
- int lineWidth = 2*width;
- for(int i=0; i<height; i++){
- int startY = i*lineWidth;
- for(int j = 0; j < lineWidth; j+=4){
- int Y1 = j + startY;
- int Y2 = Y1+2;
- int V = Y1+1;
- int U = Y1+3;
- int index = (Y1>>1)*3;
- RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- index += 3;
- tmp = yuvTorgb(src[Y2], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
-
-
- public static int[] VYUYToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int[] rgb = new int[numOfPixel*3];
- int lineWidth = 2*width;
- for(int i=0; i<height; i++){
- int startV = i*lineWidth;
- for(int j = 0; j < lineWidth; j+=4){
- int V = j + startV;
- int Y1 = V+1;
- int Y2 = V+3;
- int U = V+2;
- int index = (U>>1)*3;
- RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- index += 3;
- tmp = yuvTorgb(src[Y2], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
- }
转载地址:http://bubbl.baihongyu.com/