像I420一样对NV12应用转换

时间:2013-10-13 03:13:08

标签: image image-processing rgb vlc yuv

我有一个内射函数可以在图像中的某些像素周围移动:

pixel (x, y) ===func===> pixel (X, Y)
X = funcX(x, y)
Y = funcY(y, x)

我想使用此功能在RGB,I420和NV12模式下转换整个图像。

* RGB * :如果图像处于RGB模式,则非常明显:

strideR = strideG = strideB = width;

//Temporary table for the destination
for (j = 0; j < height; j++)
    for (i = 0; i < width; i++) {
        toR[i][j] = j * strideR + i;
        toG[i][j] = j * strideG + i;
        toB[i][j] = j * strideB + i;
    }

//Temporary table for the source
for (j = 0; j < height; j++)
    for (i = 0; i < width; i++) {
        fromR[i][j] = funcY(i, j) * strideR + funcX(i, j);
        fromG[i][j] = funcY(i, j) * strideG + funcX(i, j);
        fromB[i][j] = funcY(i, j) * strideB + funcX(i, j);
    }

for (j = 0; j < height; j++)
    for (i = 0; i < width; i++) {
        destR[ toR[i][j] ] = srcR[ fromR[i][j] ];
        destG[ toG[i][j] ] = srcG[ fromG[i][j] ];
        destb[ toB[i][j] ] = srcB[ fromB[i][j] ];
    }

* I420 * :如果图像处于I420模式(YYYYYYYY UU VV),则以下工作正常:

strideY = width;
strideU = strideV = width / 2;

//Temporary table for the destination
for (j = 0; j < height; j++)
    for (i = 0; i < width; i++) {
        toY[i][j] = j * strideY + i;
        toU[i][j] = j / 2 * strideU + i / 2;
        toV[i][j] = j / 2 * strideV + i / 2;
    }

//Temporary table for the source
for (j = 0; j < height; j++)
    for (i = 0; i < width; i++) {
        fromY[i][j] = funcY(i, j) * strideY + funcX(i, j);
        fromU[i][j] = funcY(i, j) / 2 * strideU + funcX(i, j) / 2;
        fromV[i][j] = funcY(i, j) / 2 * strideV + funcX(i, j) / 2;
    }

    for (j = 0; j < height; j++)
        for (i = 0; i < width; i++) {
            destY[ toY[i][j] ] = srcY[ fromY[i][j] ];
            if ((i % 2 == 0) && (j % 2 == 0)) {
                destU[ toU[i][j] ] = srcU[ fromU[i][j] ];
                destV[ toV[i][j] ] = srcV[ fromV[i][j] ];
            }
        }

* NV12 * :如果图像处于NV12模式(YYYYYYYY UVUV),则以下 NOT 正在工作:

strideY = strideUV = width;

//Temporary table for the destination
for (j = 0; j < height; j++)
    for (i = 0; i < width; i++) {
        toY[i][j] = j * strideY + i;
        toUV[i][j] = j / 2 * strideUV + i;
    }

//Temporary table for the source
for (j = 0; j < height; j++)
    for (i = 0; i < width; i++) {
        fromY[i][j] = funcY(i, j) * strideY + funcX(i, j);
        fromUV[i][j] = funcY(i, j) / 2 * strideUV + funcX(i, j);
    }

for (j = 0; j < height; j++)
    for (i = 0; i < width; i++) {
        destY[ toY[i][j] ] = srcY[ fromY[i][j] ];
        if ((i % 2 == 0) && (j % 2 == 0)) {
            destUV[ toUV[i][j] ] = srcUV[ fromUV[i][j] ];
            destUV[ toUV[i][j] + 1 ] = srcUV[ fromUV[i][j] + 1 ];
        }
    }

我得到的图片但颜色错误。黑色和白色部分(也称为Y部分)是正确的,但颜色部分(也称为UV部分)是改变的。我做错了什么?

1 个答案:

答案 0 :(得分:1)

发现问题了!解决方案是:

fromUV[i][j] = funcY(i, j) / 2 * strideUV + ((int)(funcX(i, j) / 2)) * 2;

我需要将X / 2放在一起以获得UV字节的开始。