我有一个单射函数,可以围绕图像中的一些像素移动:
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),则以下内容不起作用:
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 部分)已更改。我究竟做错了什么?