YUV转换为RGB

void yuv2rgb_nv21(const unsigned char *src, unsigned char *dst, int src_width, int src_height, int src_step, int dst_width, int dst_height, int dst_step) {
    
    
	typedef unsigned char uint8;
	const uint8* pY;
	const uint8* pU;
	const uint8* pV;
	int Y00, Y01, U, V;
	int Y10, Y11;
	int i, j;
	int ruv, guv, buv;
	int R, G, B;

#define SET_COLOR(out, index) \
    {
      
                        \
        out[index++] = B;     \
        out[index++] = G;     \
        out[index++] = R;     \
    }

	int width = dst_width;
	int height = dst_height;
	pY = src;
	{
    
    
		pV = src + height*src_step;
		pU = src + (height + height / 4)*src_step;
	}
	for (i = 0; i < height; i += 2, pY += src_step * 2) {
    
    
		size_t index = 0;
		size_t index1 = 0;
		uint8* out = dst + i*dst_step;
		uint8* out1 = dst + (i + 1)*dst_step;
		int jV = 0;
		for (j = 0; j < width; j += 2) {
    
    
			Y00 = *((pY)+j);
			Y01 = *((pY)+j + 1);
			Y10 = *((pY)+src_step + j);
			Y11 = *((pY)+src_step + j + 1);
			{
    
    
				{
    
    
					V = *(pV + j);
					U = *(pV + j + 1);
				}
			}

			ruv = ((359 * (V - 128)) >> 8);
			guv = -1 * ((88 * (U - 128) + 183 * (V - 128)) >> 8);
			buv = ((454 * (U - 128)) >> 8);

			R = Y00 + ruv;
			G = Y00 + guv;
			B = Y00 + buv;
			R = (R > 255) ? 255 : ((R < 0) ? 0 : R);
			G = (G > 255) ? 255 : ((G < 0) ? 0 : G);
			B = (B > 255) ? 255 : ((B < 0) ? 0 : B);

			SET_COLOR(out, index)

				R = Y01 + ruv;
			G = Y01 + guv;
			B = Y01 + buv;
			R = (R > 255) ? 255 : ((R < 0) ? 0 : R);
			G = (G > 255) ? 255 : ((G < 0) ? 0 : G);
			B = (B > 255) ? 255 : ((B < 0) ? 0 : B);

			SET_COLOR(out, index)

				ruv = ((359 * (V - 128)) >> 8);
			guv = -1 * ((88 * (U - 128) + 183 * (V - 128)) >> 8);
			buv = ((454 * (U - 128)) >> 8);
			R = Y10 + ruv;
			G = Y10 + guv;
			B = Y10 + buv;
			R = (R > 255) ? 255 : ((R < 0) ? 0 : R);
			G = (G > 255) ? 255 : ((G < 0) ? 0 : G);
			B = (B > 255) ? 255 : ((B < 0) ? 0 : B);

			SET_COLOR(out1, index1)

				R = Y11 + ruv;
			G = Y11 + guv;
			B = Y11 + buv;
			R = (R > 255) ? 255 : ((R < 0) ? 0 : R);
			G = (G > 255) ? 255 : ((G < 0) ? 0 : G);
			B = (B > 255) ? 255 : ((B < 0) ? 0 : B);

			SET_COLOR(out1, index1)
		}
		{
    
    
			{
    
    
				pV += src_step;
			}
		}
	}
#undef SET_COLOR
}

猜你喜欢

转载自blog.csdn.net/idream68/article/details/118608991