#define MIN(x,y) (x)<(y) ? (x) : (y);
void YUY2toRGB24(BYTE *pDst, BYTE *pSrc)
{
/*for(long lSrc=0,lDst=0; lSrc<320*240*2; lSrc=lSrc+4,lDst=lDst+6)
{
int C,D,E;
C = *(pSrc+lSrc+0)-16;
D = *(pSrc+lSrc+1)-128;
E = *(pSrc+lSrc+3)-128;
*(pDst+lDst+2) = clip(( 298 * C + 409 * E + 128) >> 8);
*(pDst+lDst+1) = clip(( 298 * C - 100 * D - 208 * E + 128) >> 8);
*(pDst+lDst+0) = clip(( 298 * C + 516 * D + 128) >> 8);
C = *(pSrc+lSrc+2)-16;
*(pDst+lDst+5) = clip(( 298 * C + 409 * E + 128) >> 8);
*(pDst+lDst+4) = clip(( 298 * C - 100 * D - 208 * E + 128) >> 8);
*(pDst+lDst+3) = clip(( 298 * C + 516 * D + 128) >> 8);
}*/
for(long lSrc=0,lDst=0; lSrc<320*240*2; lSrc=lSrc+4,lDst=lDst+6)
{
int y,u,v;
y = *(pSrc+lSrc+0);
u = *(pSrc+lSrc+1);
v = *(pSrc+lSrc+3);
*(pDst+lDst+2) = MIN((9535 * (y - 16) + 13074 * (v - 128)) >> 13, 255);
*(pDst+lDst+1) = MIN((9535 * (y - 16) - 6660 * (v - 128) - 3203 * (u - 128)) >> 13, 255);
*(pDst+lDst+0) = MIN((9535 * (y - 16) + 16531 * (u - 128)) >> 13, 255);
y = *(pSrc+lSrc+2);
*(pDst+lDst+5) = MIN((9535 * (y - 16) + 13074 * (v - 128)) >> 13, 255);
*(pDst+lDst+4) = MIN((9535 * (y - 16) - 6660 * (v - 128) - 3203 * (u - 128)) >> 13, 255);
*(pDst+lDst+3) = MIN((9535 * (y - 16) + 16531 * (u - 128)) >> 13, 255);
}
}
void YUY2toYV12(BYTE * pDst, BYTE * pSrc)
{
for(long lSrc=0; lSrc<320*240*2; lSrc=lSrc+2)
{
*(pDst++) = *(pSrc+lSrc);
}
long lDistance = 160*120;
for(int y=0; y<240; ++y)
{
if(y%2==0)
{
for(int x=0; x<320; x=x+4)
{
*(pDst++) = *(pSrc+x+1);
*(pDst+lDistance) = *(pSrc+x+3);
}
}
}
}
void RGB24toYUY2(CString str, BYTE *pBuf)
{
HANDLE hFile = NULL;
BOOL bResult = FALSE;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
memset(&bfh,0,sizeof(BITMAPFILEHEADER));
memset(&bih,0,sizeof(BITMAPINFOHEADER));
hFile = CreateFile(str, // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open file (error %d)/n", GetLastError());
return ;
}
DWORD dwBytes;
bResult = ReadFile(hFile,&bfh,sizeof(BITMAPFILEHEADER),&dwBytes,NULL);
if(!bResult)
{
printf("Could not read file (error %d)/n",GetLastError());
return ;
}
bResult = ReadFile(hFile,&bih,sizeof(BITMAPINFOHEADER),&dwBytes,NULL);
if(!bResult)
{
printf("Could not read file (error %d)/n",GetLastError());
return ;
}
BYTE *pYUV = NULL;
pYUV = new BYTE[320*240*3];
if(pYUV==NULL)
return;
bResult = ReadFile(hFile,pYUV,320*240*3,&dwBytes,NULL);
if(!bResult)
{
//printf("Could not read file (error %d)/n",GetLastError());
DWORD dwResult = GetLastError();
return ;
}
/*for(long lSrc=0,lDst=0; lSrc<320*240*3; lSrc=lSrc+6,lDst=lDst+4)
{
*(pBuf+lDst+0) = ( ( 66 * (*(pYUV+lSrc+2)) + 129 * (*(pYUV+lSrc+1)) + 25 * (*(pYUV+lSrc+0)) + 128) >> 8) + 16;
*(pBuf+lDst+1) = ( ( -38 * (*(pYUV+lSrc+2)) - 74 * (*(pYUV+lSrc+1)) + 112 * (*(pYUV+lSrc+0)) + 128) >> 8) + 128;
*(pBuf+lDst+3) = ( ( 112 * (*(pYUV+lSrc+2)) - 94 * (*(pYUV+lSrc+1)) - 18 * (*(pYUV+lSrc+0)) + 128) >> 8) + 128;
*(pBuf+lDst+2) = ( ( 66 * (*(pYUV+lSrc+5)) + 129 * (*(pYUV+lSrc+4)) + 25 * (*(pYUV+lSrc+3)) + 128) >> 8) + 16;
}*/
long lDst = 0;
for(int y=239; y>=0; --y)
{
for(int x=0; x<320/2; ++x)
{
*(pBuf+lDst+0) = ( ( 66 * (*(pYUV+y*320*3+x*6+2)) + 129 * (*(pYUV+y*320*3+x*6+1)) + 25 * (*(pYUV+y*320*3+x*6+0)) + 128) >> 8) + 16;
*(pBuf+lDst+1) = ( ( -38 * (*(pYUV+y*320*3+x*6+2)) - 74 * (*(pYUV+y*320*3+x*6+1)) + 112 * (*(pYUV+y*320*3+x*6+0)) + 128) >> 8) + 128;
*(pBuf+lDst+3) = ( ( 112 * (*(pYUV+y*320*3+x*6+2)) - 94 * (*(pYUV+y*320*3+x*6+1)) - 18 * (*(pYUV+y*320*3+x*6+0)) + 128) >> 8) + 128;
*(pBuf+lDst+2) = ( ( 66 * (*(pYUV+y*320*3+x*6+5)) + 129 * (*(pYUV+y*320*3+x*6+4)) + 25 * (*(pYUV+y*320*3+x*6+3)) + 128) >> 8) + 16;
lDst = lDst+4;
}
}
delete [] pYUV;
pYUV = NULL;
CloseHandle(hFile);
return ;
}