⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 avstorgb.cpp

📁 网上关于YUV转RGB的还真不少,我这个还是不错的,查表形,很快的,一个简单的VC程序但还不错
💻 CPP
字号:
#include <stdlib.h>
#include <stdio.h>
//#include <memory.h>
#define N 10
int width = 352;
int height = 288;
unsigned char *clp;

/* Data for ConvertYUVtoRGB*/
long int *crv_tab;
long int *cbu_tab;
long int *cgu_tab;
long int *cgv_tab;
long int *tab_76309;
FILE *fyuv;
void getyuvtorgbtable();
int get_one_frame(unsigned char * yuvin);
void ConvertYUVtoRGB(unsigned char *yuv,unsigned char *dst_ori,int width, int height);

int main()
{  	
	unsigned char * yuvin;
	unsigned char * rgbout;
	int length = 352*288*3;
	int i;

	FILE *frgb;

	if((yuvin=(unsigned char *)malloc(length*sizeof(unsigned char)/2))==NULL)
	{
		return -1;
	}
	if((rgbout=(unsigned char *)malloc(length*sizeof(unsigned char)))==NULL)
	{
		return -1;
	}
	///////////////////init 6 buf//////////
	if((crv_tab=(long int *)malloc(256*sizeof(long int)))==NULL)
	{
		return -1;
	}
	if((cbu_tab=(long int *)malloc(256*sizeof(long int)))==NULL)
	{
		return -1;
	}
	if((cgu_tab=(long int *)malloc(256*sizeof(long int)))==NULL)
	{
		return -1;
	}
	if((cgv_tab=(long int *)malloc(256*sizeof(long int)))==NULL)
	{
		return -1;
	}
	if((tab_76309=(long int *)malloc(256*sizeof(long int)))==NULL)
	{
		return -1;
	}
	if((clp=(unsigned char *)malloc(1024*sizeof(unsigned char)))==NULL)
	{
		return -1;
	}
	/////////////////////////////////////////////
	if((fyuv=fopen("hall.cif","rb"))==NULL)
	{
		printf("can't open file.yuv!")	;
		exit(0);
	}
	if((frgb=fopen("rgbout.avi","wb"))==NULL)
	{	
		printf("can't open rgbout.avi!")	;
		exit(0);
	}
	getyuvtorgbtable();
	for(i=0;i<N;i++)
	{
		get_one_frame(yuvin); 
		ConvertYUVtoRGB(yuvin,rgbout,width,height);		
		fwrite(rgbout,1,length,frgb);			
	}
	clp -= 384;
	free(clp);
	clp = NULL;		
	free(crv_tab);
	crv_tab = NULL;
	free(cbu_tab);
	cbu_tab = NULL;
	free(cgu_tab);
	cgu_tab = NULL;
	free(cgv_tab);
	cgv_tab = NULL;
    free(tab_76309);
	tab_76309 = NULL;

	free(yuvin);
	yuvin = NULL;
	free(rgbout);
	rgbout = NULL;
	fclose(fyuv);
	fclose(frgb);
	return 0;
}

int get_one_frame(unsigned char * yuvin)       //输入一帧图像
{
	int k;
	k=fread(yuvin,1,width*height*3/2,fyuv);
	if(k!=352*288*3/2)
		return 0;
	return k;
}
void getyuvtorgbtable()
{	
	/* Data for ConvertYUVtoRGB*/
	long int crv,cbu,cgu,cgv;
	int i;   
	clp += 384;
	

	for (i=-384; i<640; i++){
		clp[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
	}
	/*memset(clp,0,385);
	for(i=1;i<255;i++)
		clp[i] = i;
	memset(&clp[256],255,385);*/
	
	crv = 104597; cbu = 132201;
	cgu = 25675;  cgv = 53279;
//	clp -=384;
//	free(clp);
	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);
	}
}
void ConvertYUVtoRGB(unsigned char *yuv,unsigned char *dst_ori,int width, int height)
{
   int y11,y21;
   int y12,y22;
   int y13,y23;
   int y14,y24;
   int u,v; 
   int i,j;
   int c11, c21, c31, c41;
   int c12, c22, c32, c42;
   unsigned int DW;
   unsigned int *id1, *id2;
   unsigned char *py1,*py2,*pu,*pv;
   unsigned char *d1, *d2;
/*    memset(dst_ori,200,3*width*height);
*/  
   d1 = dst_ori;
   d1 += width*height*3 - width*3;
   d2 = d1 - width*3;
   
   py1 = yuv; 
   pu  = yuv+width*height; 
   pv  = yuv + (width*height+(width*height)/4);//lbx
   
   py2 = py1 + width;
   
   id1 = (unsigned int *)d1;
   id2 = (unsigned int *)d2;

   for (j = 0; j < height; j += 2) { 
      // line j + 0 
      for (i = 0; i < width; i += 4) {
         u = *pu++;
         v = *pv++;
         c11 = crv_tab[v];
         c21 = cgu_tab[u];
         c31 = cgv_tab[v];
         c41 = cbu_tab[u];
         u = *pu++;
         v = *pv++;
         c12 = crv_tab[v];
         c22 = cgu_tab[u];
         c32 = cgv_tab[v];
         c42 = cbu_tab[u];

         y11 = tab_76309[*py1++]; // (255/219)*65536 
         y12 = tab_76309[*py1++];
         y13 = tab_76309[*py1++]; // (255/219)*65536 
         y14 = tab_76309[*py1++];

         y21 = tab_76309[*py2++];
         y22 = tab_76309[*py2++];
         y23 = tab_76309[*py2++];
         y24 = tab_76309[*py2++];

         // RGBR
         DW = ((clp[(y11 + c41)>>16])) |
            ((clp[(y11 - c21 - c31)>>16])<<8) |
            ((clp[(y11 + c11)>>16])<<16) |  
            ((clp[(y12 + c41)>>16])<<24);
         *id1++ = DW;

         // GBRG
         DW = ((clp[(y12 - c21 - c31)>>16])) |
            ((clp[(y12 + c11)>>16])<<8) |  
            ((clp[(y13 + c42)>>16])<<16) |
            ((clp[(y13 - c22 - c32)>>16])<<24);
         *id1++ = DW;

         // BRGB
         DW = ((clp[(y13 + c12)>>16])) |  
            ((clp[(y14 + c42)>>16])<<8) |
            ((clp[(y14 - c22 - c32)>>16])<<16) |
            ((clp[(y14 + c12)>>16])<<24);  
         *id1++ = DW;

         // RGBR
         DW = ((clp[(y21 + c41)>>16])) |
            ((clp[(y21 - c21 - c31)>>16])<<8) |
            ((clp[(y21 + c11)>>16])<<16) |  
            ((clp[(y22 + c41)>>16])<<24);
         *id2++ = DW;

         // GBRG
         DW = ((clp[(y22 - c21 - c31)>>16])) |
            ((clp[(y22 + c11)>>16])<<8) |  
            ((clp[(y23 + c42)>>16])<<16) |
            ((clp[(y23 - c22 - c32)>>16])<<24);
         *id2++ = DW;

         // BRGB
         DW = ((clp[(y23 + c12)>>16])) |  
            ((clp[(y24 + c42)>>16])<<8) |
            ((clp[(y24 - c22 - c32)>>16])<<16) |
            ((clp[(y24 + c12)>>16])<<24);  
         *id2++ = DW;
      }
      id1 -= (9 * width)>>2;
      id2 -= (9 * width)>>2;
      py1 += width;
      py2 += width;
   }
   
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -