欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

YUV转RGB的常见代码

最编程 2024-08-15 15:04:42
...
  • public class YuvToRGB {  
  •     private static int R = 0;  
  •     private static int G = 1;  
  •     private static int B = 2;  
  •     //I420是yuv420格式,是3个plane,排列方式为(Y)(U)(V)  
  •     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<00: rgb.r>255255 : rgb.r);  
  •         rgb.g =(rgb.g<00: rgb.g>255255 : rgb.g);  
  •         rgb.b =(rgb.b<00: rgb.b>255255 : rgb.b);  
  •         return rgb;  
  •     }  
  •       
  •     //YV16是yuv422格式,是三个plane,(Y)(U)(V)  
  •     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[index+R] = (int)((src[Y]&0xff) + 1.4075 * ((src[V]&0xff)-128));  
  •                 //rgb[index+G] = (int)((src[Y]&0xff) - 0.3455 * ((src[U]&0xff)-128) - 0.7169*((src[V]&0xff)-128));  
  •                 //rgb[index+B] = (int)((src[Y]&0xff) + 1.779 * ((src[U]&0xff)-128));  
  •                 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;  
  •     }  
  •       
  •     //YV12是yuv420格式,是3个plane,排列方式为(Y)(V)(U)  
  •     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;  
  • 上一篇: RGB

    下一篇: 总结YUV色彩格式的综述