RGB2YUV420SP
/**
* @name: RGB2YUV420SP
* @msg: 将RGB格式的图像转换为YUV420sp格式
* @param {*}
* @return {*}
*/
extern "C" void RGB2YUV420SP(unsigned char *rgb, unsigned char *yuv420sp, int width, int height)
{
if (yuv420sp == NULL || rgb == NULL)
return;
int frameSize = width*height;
int yIndex = 0;
int uvIndex = frameSize;
int R, G, B, Y, U, V;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
B = rgb[(i * width + j) * 3 + 0];
G = rgb[(i * width + j) * 3 + 1];
R = rgb[(i * width + j) * 3 + 2];
//RGB to YUV
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
yuv420sp[yIndex++] = (unsigned char)((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (i % 2 == 0 && j % 2 == 0)
{
yuv420sp[uvIndex++] = (unsigned char)((V < 0) ? 0 : ((V > 255) ? 255 : V));//UV交替排列
yuv420sp[uvIndex++] = (unsigned char)((U < 0) ? 0 : ((U > 255) ? 255 : U));
}
}
}
}
RGB2YUV420P
/**
* @name: RGB2YUV420P
* @msg: 将RGB格式的图像转换为YUV420p格式
* @param {*}
* @return {*}
*/
unsigned char ClipValue(unsigned char x, unsigned char min_val, unsigned char max_val) {
if (x > max_val) {
return max_val;
} else if (x < min_val) {
return min_val;
} else {
return x;
}
}
void RGB2YUV420P(unsigned char *rgb24, int width, int height, unsigned char *yuv420p) {
unsigned char *ptrY, *ptrU, *ptrV;
memset(yuv420p, 0, width * height * 3 / 2);
ptrY = yuv420p;
ptrU = yuv420p + width * height;
ptrV = ptrU + (width * height * 1 / 4);
unsigned char y, u, v, r, g, b;
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
index = width * j * 3 + i * 3;
r = rgb24[index];
g = rgb24[index + 1];
b = rgb24[index + 2];
y = (unsigned char) ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
u = (unsigned char) ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v = (unsigned char) ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
*(ptrY++) = ClipValue(y, 0, 255);
if (j % 2 == 0 && i % 2 == 0) {
*(ptrU++) = ClipValue(u, 0, 255);
} else if (i % 2 == 0) {
*(ptrV++) = ClipValue(v, 0, 255);
}
}
}
}
YUV420sp_to_YUV422p
/**
* @name: YUV420sp_to_YUV422p
* @msg: 将YUV420sp格式的图像转换为YUV422格式
* @param {*}
* @return {*}
*/
extern "C" void YUV420sp_to_YUV422p(unsigned char *yuv420,unsigned char *yuv422, int imgW, int imgH)
{
int imgW_cnt = imgW / 2;
// int yuv420Uoffset = imgH * imgW;
// int yuv420Voffset = imgH * imgW + imgH * imgW/4;
unsigned char * yuv420Y = yuv420;
unsigned char * yuv420VU = yuv420 + imgH * imgW;
int yuv422rowoffset = 0;
int yuv420Yoffset = 0;
int yuv420UVoffset = 0;
for (int h = 0; h < imgH; h++)
{
int yuv422coloffset = 0;
int yuv420coloffset = 0;
for (int w = 0; w < imgW_cnt; w++, yuv422coloffset+=4 , yuv420coloffset +=2)
{
yuv422[yuv422coloffset + yuv422rowoffset] = yuv420Y[yuv420coloffset + yuv420Yoffset];
yuv422[yuv422coloffset + yuv422rowoffset+1] =yuv420VU[yuv420coloffset+1 + yuv420UVoffset];
yuv422[yuv422coloffset + yuv422rowoffset+2] = yuv420Y[yuv420coloffset + yuv420Yoffset+1];
yuv422[yuv422coloffset + yuv422rowoffset + 3] = yuv420VU[yuv420coloffset + yuv420UVoffset];
}
yuv420Yoffset += imgW;
yuv420UVoffset += (h %2==1)?imgW:0;
yuv422rowoffset += imgW * 2;
}
}