RGB转YUV

YUV常见格式是以下四种,其他的格式自行搜索,以下是转换方法
I420: YYYYYYYY UU VV =>YUV420P
YV12: YYYYYYYY VV UU =>YUV420P
NV12: YYYYYYYY UVUV =>YUV420SP
NV21: YYYYYYYY VUVU =>YUV420SP

以下转换方法是RGBA的像素格式,如果是ARGB,RGB,BGR等,自行去转换,
或者调整方法;

I420: YYYYYYYY UU VV =>YUV420P转换方法

char rgba[] = {
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0
    };
    
    NSLog(@"===y===%d",(int)(0.299*1 + 0.587*2 + 0.114*3));
    NSLog(@"===u===%d",(int)(-0.1687*1 - 0.3313*2 + 0.5*3 + 128));
    NSLog(@"===v===%d",(int)(0.5*1 - 0.4187*2 - 0.0813*3 + 128));
    
    int height = 4;
    int width = 4;
    
    int frameSize = width * height;
    int yIndex = 0;
    int vIndex = frameSize;
    int uIndex = frameSize*1.25;

    unsigned char *yuv_Y = (unsigned char *)malloc(frameSize+frameSize/4+frameSize/4);
    
    
    int r, g, b, y, u, v, a;
    int index = 0;
    size_t offset,p;
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {//RGBA
            
            offset = ((width * j) + i);
            p = offset*4;
            //
            r = rgba[p + 0];
            g = rgba[p + 1];
            b = rgba[p + 2];
            a = rgba[p + 3];
            
            //转换
            y = 0.299*r + 0.587*g + 0.114*b;
            u = -0.1687*r - 0.3313*g + 0.5*b + 128;
            v = 0.5*r - 0.4187*g - 0.0813*b + 128;
            
            yuv_Y[yIndex++] = y;
            if (j % 2 == 0 && i % 2 == 0) {//按2 * 2矩阵取
                yuv_Y[vIndex++] = u;
                yuv_Y[uIndex++] = v;
            }
            index ++;
        }
    }
    
    for (int i = 0; i<frameSize+frameSize/2; i++) {
        NSLog(@"===y===%d",yuv_Y[i]);
    }
    

I420: YYYYYYYY UU VV =>YUV420P转换方法(2)

char rgba[] = {
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0
    };
    
    NSLog(@"===y===%d",(int)(0.299*1 + 0.587*2 + 0.114*3));
    NSLog(@"===u===%d",(int)(-0.1687*1 - 0.3313*2 + 0.5*3 + 128));
    NSLog(@"===v===%d",(int)(0.5*1 - 0.4187*2 - 0.0813*3 + 128));
    
    int height = 4;
    int width = 4;
    
    int frameSize = width * height;
    int yIndex = 0;
    int vIndex = 0;
    int uIndex = 0;

    unsigned char *yuv_Y = (unsigned char *)malloc(frameSize);
    unsigned char *yuv_V = (unsigned char *)malloc(frameSize/4);
    unsigned char *yuv_U = (unsigned char *)malloc(frameSize/4);
    
    int r, g, b, y, u, v, a;
    int index = 0;
    size_t offset,p;
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {//RGBA
            
            offset = ((width * j) + i);
            p = offset*4;
            //
            r = rgba[p + 0];
            g = rgba[p + 1];
            b = rgba[p + 2];
            a = rgba[p + 3];
            
            //转换
            y = 0.299*r + 0.587*g + 0.114*b;
            u = -0.1687*r - 0.3313*g + 0.5*b + 128;
            v = 0.5*r - 0.4187*g - 0.0813*b + 128;
            
            yuv_Y[yIndex++] = y;
            if (j % 2 == 0 && i % 2 == 0) {//按2 * 2矩阵取
                yuv_V[vIndex++] = v;
                yuv_U[uIndex++] = u;
            }
            index ++;
        }
    }
    
    for (int i = 0; i<frameSize; i++) {
        NSLog(@"===y===%d",yuv_Y[i]);
    }
    for (int i = 0; i<frameSize/4; i++) {
        NSLog(@"===v===%d",yuv_V[i]);
    }
    for (int i = 0; i<frameSize/4; i++) {
        NSLog(@"===u===%d",yuv_U[i]);
    }

YV12: YYYYYYYY VV UU =>YUV420P转换方法

char rgba[] = {
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0
    };
    
    NSLog(@"===y===%d",(int)(0.299*1 + 0.587*2 + 0.114*3));
    NSLog(@"===u===%d",(int)(-0.1687*1 - 0.3313*2 + 0.5*3 + 128));
    NSLog(@"===v===%d",(int)(0.5*1 - 0.4187*2 - 0.0813*3 + 128));
    
    int height = 4;
    int width = 4;
    
    int frameSize = width * height;
    int yIndex = 0;
    int vIndex = frameSize;
    int uIndex = frameSize * 1.25;

    unsigned char *yuv = (unsigned char *)malloc(frameSize + frameSize/4 + frameSize/4);
    
    int r, g, b, y, u, v, a;
    int index = 0;
    size_t offset,p;
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {//RGBA
            
            offset = ((width * j) + i);
            p = offset*4;
            //
            r = rgba[p + 0];
            g = rgba[p + 1];
            b = rgba[p + 2];
            a = rgba[p + 3];
            
            //转换
            y = 0.299*r + 0.587*g + 0.114*b;
            u = -0.1687*r - 0.3313*g + 0.5*b + 128;
            v = 0.5*r - 0.4187*g - 0.0813*b + 128;
            
            yuv[yIndex++] = y;
            if (j % 2 == 0 && i % 2 == 0) {//按2 * 2矩阵取
                yuv[vIndex++] = v;
                yuv[uIndex++] = u;
            }
            index ++;
        }
    }
    
    for (int i = 0; i<frameSize + frameSize/2; i++) {
        NSLog(@"===y===%d",yuv[i]);
    }

NV21: YYYYYYYY VUVU =>YUV420SP转换方法

char rgba[] = {
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0
    };
    
    NSLog(@"===y===%d",(int)(0.299*1 + 0.587*2 + 0.114*3));
    NSLog(@"===u===%d",(int)(-0.1687*1 - 0.3313*2 + 0.5*3 + 128));
    NSLog(@"===v===%d",(int)(0.5*1 - 0.4187*2 - 0.0813*3 + 128));
    
    int height = 4;
    int width = 4;
    int wh = width * height;
    
    unsigned char *bufY = (unsigned char *)malloc(wh);
    unsigned char *bufUV = (unsigned char *)malloc(wh/2);

    size_t offset,p;

    int r,g,b,y,u,v;
    int a=255;
    for (int row = 0; row < height; ++row) {
      for (int col = 0; col < width; ++col) {
        //
        offset = ((width * row) + col);
        p = offset*4;
        //
        r = rgba[p + 0];
        g = rgba[p + 1];
        b = rgba[p + 2];
        a = rgba[p + 3];
        //
        y = 0.299*r + 0.587*g + 0.114*b;
        u = -0.1687*r - 0.3313*g + 0.5*b + 128;
        v = 0.5*r - 0.4187*g - 0.0813*b + 128;
        //
        bufY[offset] = y;
        bufUV[(row/2)*width + (col/2)*2] = v;
        bufUV[(row/2)*width + (col/2)*2 + 1] = u;
      }
    }
    
    for (int i = 0; i<wh; i++) {
        NSLog(@"===y===%d",bufY[i]);
    }
    
    for (int i = 0; i<wh/2; i++) {
        NSLog(@"===uv===%d",bufUV[i]);
    }

NV12: YYYYYYYY UVUV =>YUV420SP转换方法

char rgba[] = {
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
        1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0
    };
    
    NSLog(@"===y===%d",(int)(0.299*1 + 0.587*2 + 0.114*3));
    NSLog(@"===u===%d",(int)(-0.1687*1 - 0.3313*2 + 0.5*3 + 128));
    NSLog(@"===v===%d",(int)(0.5*1 - 0.4187*2 - 0.0813*3 + 128));
    
    int height = 4;
    int width = 4;
    int wh = width * height;
    
    unsigned char *bufY = (unsigned char *)malloc(wh);
    unsigned char *bufUV = (unsigned char *)malloc(wh/2);

    size_t offset,p;

    int r,g,b,y,u,v;
    int a=255;
    for (int row = 0; row < height; ++row) {
      for (int col = 0; col < width; ++col) {
        //
        offset = ((width * row) + col);
        p = offset*4;
        //
        r = rgba[p + 0];
        g = rgba[p + 1];
        b = rgba[p + 2];
        a = rgba[p + 3];
        //
        y = 0.299*r + 0.587*g + 0.114*b;
        u = -0.1687*r - 0.3313*g + 0.5*b + 128;
        v = 0.5*r - 0.4187*g - 0.0813*b + 128;
        //
        bufY[offset] = y;
        bufUV[(row/2)*width + (col/2)*2] = u;
        bufUV[(row/2)*width + (col/2)*2 + 1] = v;
      }
    }
    
    for (int i = 0; i<wh; i++) {
        NSLog(@"===y===%d",bufY[i]);
    }
    for (int i = 0; i<wh/2; i++) {
        NSLog(@"===uv===%d",bufUV[i]);
    }
    
上一篇:echarts属性的设置


下一篇:内置函数中的自定义属性,如 `rgba()` 抛出错误