/************************************************************************* * File Name : YUV_to_BGR.c * Description : Function to COnvert the output from YUV420 to BGR24 *************************************************************************/ /*To make sure that you are bounding your inputs in the range of 0 & 255*/ #define SATURATE8(x) ((unsigned int) x <= 255 ? x : (x < 0 ? 0: 255)) /************************************************************************* * Function Name : Convert_yuv420_to_bgr24 * Description : Converts YUV420 to BGR24 format * Return Value : none *************************************************************************/ Void Convert_yuv420_to_bgr24(char *videoBuffer, char * buffer_rgb, unsigned int Height, unsigned int Width) { char *buffer_rgb_1strow, *buffer_rgb_2ndrow; unsigned char y_start, u_start, v_start; unsigned int * videoBuffer_1strow,*videoBuffer_2ndrow; int r,g,b, r_prod, g_prod, b_prod; int y,u,v; unsigned int i_conv_ht =0,i_conv_wt =0; unsigned int y_size; char * cb_ptr; char * cr_ptr; /*Now do the conversion from YUV420 to BGR 888 */ /*So allocate that many amount of buffer*/ /*allocate memory to contain the one frame for RGB output:*/ /*Now do the conversion from YUV420 to BGR 888 */ /*So allocate that many amount of buffer*/ /*We are planning to convert an YUV file to RGB file*/ y_size = Width*Height; /*Calculate the Cb & Cr pointer*/ cb_ptr = videoBuffer + y_size; cr_ptr = cb_ptr + y_size/4; videoBuffer_1strow = videoBuffer; videoBuffer_2ndrow = videoBuffer+Width; buffer_rgb_1strow = buffer_rgb ; buffer_rgb_2ndrow = buffer_rgb+(3*Width); for (i_conv_ht =0; i_conv_ht>8; g =(y - g_prod)>>8; b =(y + b_prod)>>8; /*Now clip and store */ *buffer_rgb_1strow++ = SATURATE8(b); *buffer_rgb_1strow++ = SATURATE8(g); *buffer_rgb_1strow++ = SATURATE8(r); /*Extract the Y value*/ y_start = *videoBuffer_1strow++; y = (y_start -16)*298; /*!!! now it is time to do the conversion*/ r =(y + r_prod)>>8; g =(y - g_prod)>>8; b =(y + b_prod)>>8; /*Now clip and store */ *buffer_rgb_1strow++ = SATURATE8(b); *buffer_rgb_1strow++ = SATURATE8(g); *buffer_rgb_1strow++ = SATURATE8(r); /*Extract the Y value*/ y_start = *videoBuffer_2ndrow++; y = (y_start -16)*298; /*!!! now it is time to do the conversion*/ r =(y + r_prod)>>8; g =(y - g_prod)>>8; b =(y + b_prod)>>8; /*Now clip and store */ *buffer_rgb_2ndrow++ = SATURATE8(b); *buffer_rgb_2ndrow++ = SATURATE8(g); *buffer_rgb_2ndrow++ = SATURATE8(r); /*Extract the Y value*/ y_start = *videoBuffer_2ndrow++; y = (y_start -16)*298; /*!!! now it is time to do the conversion*/ r =(y + r_prod)>>8; g =(y - g_prod)>>8; b =(y + b_prod)>>8; /*Now clip and store */ *buffer_rgb_2ndrow++ = SATURATE8(b); *buffer_rgb_2ndrow++ = SATURATE8(g); *buffer_rgb_2ndrow++ = SATURATE8(r); } videoBuffer_1strow += Width; videoBuffer_2ndrow += Width; buffer_rgb_1strow += 3*Width; buffer_rgb_2ndrow += 3*Width; } }