RGB转YCbCr
从RGB转换成YCbCr
// Purpose:
// Save RGB->YCC colorspace conversion for reuse, only computing once
// so dont need multiply in color conversion later
/* Notes:
*
* YCbCr is defined per CCIR 601-1, except that Cb and Cr are
* normalized to the range 0 .. 255 rather than -0.5 .. 0.5.
* The conversion equations to be implemented are therefore
*
* Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
* Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + 128
* Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + 128
*
* (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
* To avoid floating-point arithmetic, we represent the fractional constants
* as integers scaled up by 2^16 (about 4 digits precision); we have to divide
* the products by 2^16, with appropriate rounding, to get the correct answer.
*/
void CMainFrame::InitColorTable( void )
{
int i;
int nScale = 1L << 16; //(1为长整形,左移16位 =2^16)
int CBCR_OFFSET = 128<<16;
/*
* nHalf is for (y, cb, cr) rounding, equal to (1L<<16)*0.5
* If (R,G,B)=(0,0,1), then Cb = 128.5, should round to 129
* Using these tables will produce 129 too:
* Cb = (int)((RToCb[0] + GToCb[0] + BToCb[1]) >> 16)
* = (int)(( 0 + 0 + 1L<<15 + 1L<<15 + 128 * 1L<<16 ) >> 16)
* = (int)(( 1L<<16 + 128 * 1L<<16 ) >> 16 )
* = 129
*/
/*
* Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
* Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + 128
* Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + 128
*/
int nHalf = nScale >> 1;
for( i=0; i<256; i++ )
{
m_RToY[ i ] = (int)( 0.29900 * nScale + 0.5 ) * i;
m_GToY[ i ] = (int)( 0.58700 * nScale + 0.5 ) * i;
m_BToY[ i ] = (int)( 0.11400 * nScale + 0.5 ) * i + nHalf;
m_RToCb[ i ] = (int)( 0.16874 * nScale + 0.5 ) * (-i);
m_GToCb[ i ] = (int)( 0.33126 * nScale + 0.5 ) * (-i);
m_BToCb[ i ] = (int)( 0.50000 * nScale + 0.5 ) * i +
CBCR_OFFSET + nHalf - 1;
m_RToCr[ i ] = m_BToCb[ i ];
m_GToCr[ i ] = (int)( 0.41869 * nScale + 0.5 ) * (-i);
m_BToCr[ i ] = (int)( 0.08131 * nScale + 0.5 ) * (-i);
}
}
////////////////////////////////////////////////////////////////////////////////
// 颜色转换
// Color convertion from bgr to ycbcr for one tile, 16*16 pixels
// Actually being not used for efficiency !!!!! Please use BGRToYCbCrEx()
void CMainFrame::BGRToYCbCr(BYTE * pBgr,BYTE * pY,BYTE * pCb,BYTE * pCr)
{
int i;
BYTE r, g, b, *pByte = pBgr, *py = pY, *pcb = pCb, *pcr = pCr;
for( i=0; i<256; i++ )
{
b = *(pByte ++);
g = *(pByte ++);
r = *(pByte ++);
*(py++) = (unsigned char)((m_RToY[r] + m_GToY[g] + m_BToY[b] )>>16);
*(pcb++) = (unsigned char)((m_RToCb[r] + m_GToCb[g] + m_BToCb[b])>>16);
*(pcr++) = (unsigned char)((m_RToCr[r] + m_GToCr[g] + m_BToCr[b])>>16);
}
}
//* pBlock
//输出, Y: 256; Cb: 64; Cr: 64 , 指向一块长度为384的内存区域
// [0,63]存储亮度信号block0
// [64,127]存储亮度信号block1
// [128,191]存储亮度信号block2
// [192,255]存储亮度信号block3
// [256,319]存储色度信号 Cb
// [320,383]存储色度信号 Cr
void CMainFrame::BGRToYCbCrEx(unsigned char * pBgr, int * pBlock)
{
int x, y, *py[4], *pcb, *pcr;
unsigned char r, g, b, *pByte;
pByte = pBgr;
for( x = 0; x < 4; x++ )
py[ x ] = pBlock + 64 * x;
pcb = pBlock + 256;
pcr = pBlock + 320;
for( y=0; y<16; y++ )
{
for( x=0; x<16; x++ )
{
b = *(pByte ++);
g = *(pByte ++);
r = *(pByte ++); // 取出连续的R G B的值
// block number is ((y/8) * 2 + x/8): 0, 1, 2, 3
// 计算出各block(8*8)中亮度信号的值,取值范围 [-127,127]
*( py[((y>>3)<<1) + (x>>3)] ++ ) =
((m_RToY[ r ] + m_GToY[ g ] + m_BToY[ b ] )>>16) -128;
// Equal to: (( x%2 == 0 )&&( y%2 == 0 ))
// 计算出两个色度信号的值,取值范围[-127,127]
if( (!(y & 1L)) && (!(x & 1L)) )
{
*(pcb++) =
((m_RToCb[ r ] + m_GToCb[ g ] + m_BToCb[ b ])>>16) -128;
*(pcr++) =
((m_RToCr[ r ] + m_GToCr[ g ] + m_BToCr[ b ])>>16) -128;
}
}
}
}