www.pudn.com > UYVY2RGB.rar > YUV2RGB1.CPP


static   long   int   crv_tab[256];    
static   long   int   cbu_tab[256];    
static   long   int   cgu_tab[256];    
static   long   int   cgv_tab[256];    
static   long   int   tab_76309[256];    
static   unsigned   char   clp[1024]; //for   clip   in   CCIR601    
 
 
 
#include       
#include       
using   namespace   std;    
 
#define   FRAME   0    
#define   FILED   1    
#define   YUV422   0    
#define   YUV420   1    
#define   RGB   2    
#define   TRUE   0    
#define   FALSE   1    
 
static   string   str;    
 
void   InitConvtTbl();    
void   YUV2RGB420(unsigned   char   *src,   unsigned   char   *dst_ori,    
				  int   width,int   height);    
void   YUV2RGB(unsigned   char   *src,   unsigned   char   *dst_ori,    
			   int   width,int   height);    
void   RGB2YUV(unsigned   char   *,   unsigned   char   *,   int,int);    
 
 
int   main(int   argc,char   *argv[])    
{    
	int   i,j,k,m,n;    
	int   fileback;    
	int   frameWidth=160;      
	int   frameHeight=120;    
	int   frameSize;    
	int   DocType=YUV422;    
	int   frameRate=30;    
	int   YframeStored=2;    
	int   HistStored=16;    
	int   slomoNum=0;      
	int   slomoPreNum=0;    
	int   length=5600;      
	int   roundCount=0;    
	int*   slomoPreST;    
	int*   slomoPreND;    
	int*   slomoPreRD;    
	int*   slomoScore; //record   the   zc   of   the   detected   slom   motion    
	int*   fileSize;    
	int   Start[300];    
	int   End[300];    
	int   SegNum;    
	unsigned   char*   bufferYUV;    
	unsigned   char*   bufferRGB;    
	int   segcount;    
	int   startpoint;    
	int   flag1=0;    
	int   startpnt,   endpnt;    
	int   totalframenum=0;    
	int   modifiedframenum=0;    
	float   histthrldH=2;    
	float   histthrldL=2;    
	float   tmthrld=5; //set   the   time   period   bigger   5   seconds      
     
     
	char   strtmp[20];    
	int   fileNum=3;    
     
	FILE   *filedata;    
	FILE   *filestnd;    
	FILE   *fid;    
	string   str,   str0,   str1,str2;    
     
	if(argc<5){    
		fprintf(stderr,   "yuv2rgb   bsb0.yuv   160   120   YUV420\n");    
		return   1;    
	}    
     
	str=argv[1];    
	frameWidth=atoi(argv[2]);    
	frameHeight=atoi(argv[3]);    
	if(strcmp(argv[4],"YUV422")==0){    
		DocType=YUV422;    
		frameSize=frameWidth*frameHeight*2;    
	}    
	else   if(strcmp(argv[4],"YUV420")==0){    
		DocType=YUV420;    
		frameSize=frameWidth*frameHeight*3/2;    
	}    
	else   if(strcmp(argv[4],"RGB")==0){    
		DocType=RGB;    
		frameSize=frameWidth*frameHeight*3;    
	}    
	else{    
		printf("wrong   file   type.   Must   be   YUV422,   YUV420,   or   RGB\n");    
		return   1;    
	}    
     
	bufferYUV =   new   unsigned   char[frameSize];    
	bufferRGB =   new   unsigned   char[frameWidth*frameHeight*3];    
     
	str2=str;    
	filedata=fopen(str2.c_str(),"rb");    
	if(!filedata){    
		fprintf(stderr,   "couldn't   open   the   source   file   %s\n",str2.c_str());    
		return   1;    
	}    
	fileback=fread(bufferYUV,1,   frameSize,   filedata);    
	if(fileback!=frameSize){    
		printf("error:   the   resolution   doesn't   agree   with   file   size\n");    
		return   1;    
	}    
	fclose(filedata);    
     
	sprintf(strtmp,"rgb",m);    
	n=str2.rfind('.');    
	str2.replace(n+1,3,strtmp);    
	filedata=fopen(str2.c_str(),"wb");    
	if(!filedata){    
		fprintf(stderr,   "couldn't   open   the   source   file   %s\n",str2.c_str());    
		return   1;    
	}    
     
	InitConvtTbl();    
	if(DocType==YUV422)    
		YUV2RGB(bufferYUV,   bufferRGB,   frameWidth,   frameHeight);    
	else   if   (DocType==YUV420)    
		YUV2RGB420(bufferYUV,   bufferRGB,   frameWidth,   frameHeight);    
     
	fwrite(bufferRGB,1,frameWidth*frameHeight*3,filedata);    
	fclose(filedata);    
     
	//*******   initialization   of   the   loop    
     
	//house   keeping      
	delete   []   bufferYUV;    
	delete   []   bufferRGB;    
     
	return   TRUE;    
  }    
   
   
   
  /****************************************************/    
  /* Sum   the   input */    
  /* Input:   input,   len */    
  /* Output:   input */    
  /* Algorithm:   add */    
  /****************************************************/    
  void   YUV2RGB(unsigned   char   *src,   unsigned   char   *dst_ori,    
      int   width,   int   height)    
  {                
	  unsigned   char   *src0;    
	  unsigned   char   *src1;    
	  unsigned   char   *src2;    
	  unsigned   char   *dst;    
	  unsigned   char   *py1,*py2;    
	  unsigned   char   *d1,   *d2,   *d3;    
	  int   y1,u,v;      
	  int   i,j,   c1,   c2,   c3,   c4;    
	   
	   
	  //Initialization    
	  src0=src;    
	  src1=src+width*height;    
	  src2=src+width*height+width*height/2;    
	   
	  dst=dst_ori;    
	   
       
	  for   (j   =   0;   j   <   height;   j   +=   1)   {      
		  for   (i   =   0;   i   <   width;   i   +=   2)   {    
			   
			  u   =   *src1++;    
			  v   =   *src2++;    
			   
			  c1   =   crv_tab[v];    
			  c2   =   cgu_tab[u];    
			  c3   =   cgv_tab[v];    
			  c4   =   cbu_tab[u];    
			   
			  y1   =   tab_76309[*src0++];    
			  *dst++   =   clp[384+((y1   +   c1)>>16)];        
			  *dst++   =   clp[384+((y1   -   c2   -   c3)>>16)];    
			  *dst++   =   clp[384+((y1   +   c4)>>16)];    
			   
			  y1   =   tab_76309[*src0++];    
			  *dst++   =   clp[384+((y1   +   c1)>>16)];        
			  *dst++   =   clp[384+((y1   -   c2   -   c3)>>16)];    
			  *dst++   =   clp[384+((y1   +   c4)>>16)];    
		  }    
	  }    
	   
  }        
   
   
  void   YUV2RGB420(unsigned   char   *src,   unsigned   char   *dst_ori,    
	  int   width,int   height)    
  {    
	  unsigned   char   *src0;    
	  unsigned   char   *src1;    
	  unsigned   char   *src2;    
	  int   y1,y2,u,v;      
	  unsigned   char   *py1,*py2;    
	  int   i,j,   c1,   c2,   c3,   c4;    
	  unsigned   char   *d1,   *d2,   *d3;    
	   
	  //Initialization    
	  src0=src;    
	  src1=src+width*height;    
	  src2=src+width*height+width*height/4;    
	   
	  py1=src0;    
	  py2=py1+width;    
	  d1=dst_ori;    
	  d2=d1+3*width;    
	  for   (j   =   0;   j   <   height;   j   +=   2)   {      
		  for   (i   =   0;   i   <   width;   i   +=   2)   {    
			   
			  u   =   *src1++;    
			  v   =   *src2++;    
			   
			  c1   =   crv_tab[v];    
			  c2   =   cgu_tab[u];    
			  c3   =   cgv_tab[v];    
			  c4   =   cbu_tab[u];    
			   
			  //up-left    
			  y1   =   tab_76309[*py1++];    
			  *d1++   =   clp[384+((y1   +   c1)>>16)];        
			  *d1++   =   clp[384+((y1   -   c2   -   c3)>>16)];    
			  *d1++   =   clp[384+((y1   +   c4)>>16)];    
			   
			  //down-left    
			  y2   =   tab_76309[*py2++];    
			  *d2++   =   clp[384+((y2   +   c1)>>16)];        
			  *d2++   =   clp[384+((y2   -   c2   -   c3)>>16)];    
			  *d2++   =   clp[384+((y2   +   c4)>>16)];    
			   
			  //up-right    
			  y1   =   tab_76309[*py1++];    
			  *d1++   =   clp[384+((y1   +   c1)>>16)];        
			  *d1++   =   clp[384+((y1   -   c2   -   c3)>>16)];    
			  *d1++   =   clp[384+((y1   +   c4)>>16)];    
			   
			  //down-right    
			  y2   =   tab_76309[*py2++];    
			  *d2++   =   clp[384+((y2   +   c1)>>16)];        
			  *d2++   =   clp[384+((y2   -   c2   -   c3)>>16)];    
			  *d2++   =   clp[384+((y2   +   c4)>>16)];    
		  }    
		  d1   +=   3*width;    
		  d2   +=   3*width;    
		  py1+=       width;    
		  py2+=       width;    
	  }                  
	   
	   
  }    
   
  /****************************************************/    
  /* Sum   the   input */    
  /* Input:   input,   len */    
  /* Output:   input */    
  /* Algorithm:   add */    
  /****************************************************/    
  void   InitConvtTbl()    
  {    
	  long   int   crv,cbu,cgu,cgv;    
	  int   i,ind;          
	   
	  crv   =   104597;   cbu   =   132201;     /*   fra   matrise   i   global.h   */    
	  cgu   =   25675;     cgv   =   53279;    
	   
	  for   (i   =   0;   i   <   256;   i++)   {    
		  crv_tab[i]   =   (i-128)   *   crv;    
		  cbu_tab[i]   =   (i-128)   *   cbu;    
		  cgu_tab[i]   =   (i-128)   *   cgu;    
		  cgv_tab[i]   =   (i-128)   *   cgv;    
		  tab_76309[i]   =   76309*(i-16);    
	  }    
       
	  for   (i=0;   i<384;   i++)    
		  clp[i]   =0;    
	  ind=384;    
	  for   (i=0;i<256;   i++)    
		  clp[ind++]=i;    
	  ind=640;    
	  for   (i=0;i<384;i++)    
		  clp[ind++]=255;    
  }