#include <stdio.h>
#include <Windows.h>
// --------------------------------------------------------------
#define RGB2YCbCr(R, G, B, Y, Cb, Cr) \
Y = ( 1868*B + 9617*G + 4899*R ) >> 14; \
Cb = ( 8192*B - 5428*G - 2758*R + 2097152 ) >> 14; \
Cr = ( -1332*B - 6860*G + 8192*R + 2097152 ) >> 14; \
void RGBConvertToYCbCr(int R, int G, int B, int& Y, int& Cr, int& Cb)
{
RGB2YCbCr(R, G, B, Y, Cb, Cr);
}
// --------------------------------------------------------------
#define RANGE_INT(iVal, iMin, iMax) ( ( ( iVal ) > ( iMin ) ) ? ( ( ( iVal ) <= ( iMax ) ) ? ( iVal ) : ( iMax ) ) : ( iMin ) )
#define ROUND_SHR_POSITIVE(Dividend, iShiftRightCount) ( ( ( Dividend ) & ( 1 << ( ( iShiftRightCount ) - 1 ) ) ) ? ( ( Dividend ) >> ( iShiftRightCount ) ) + 1 : ( ( Dividend ) >> ( iShiftRightCount ) ) )
#define ROUND_SHR_NEGATIVE(Dividend, iShiftRightCount) ( -( ( ( -( Dividend ) ) & ( 1 << ( ( iShiftRightCount ) - 1 ) ) ) ? ( ( -( Dividend ) ) >> ( iShiftRightCount ) ) + 1 : ( ( -( Dividend ) ) >> ( iShiftRightCount ) ) ) )
#define ROUND_SHR(Dividend, iShiftRightCount) ( ( ( Dividend ) >= 0 ) ? ROUND_SHR_POSITIVE( Dividend, iShiftRightCount ) : ROUND_SHR_NEGATIVE( Dividend, iShiftRightCount ) )
void YCbCrConvertToRGB(int Y, int Cr, int Cb, int& R, int& G, int& B)
{
int iTmpR = 0;
int iTmpG = 0;
int iTmpB = 0;
iTmpR = (((int)Y) << 14) + 22970*(((int)Cr) - 128);
iTmpG = (((int)Y) << 14) - 5638*(((int)Cb) - 128) - 11700*(((int)Cr) - 128);
iTmpB = (((int)Y) << 14) + 29032*(((int)Cb) - 128);
iTmpR = ROUND_SHR(iTmpR, 14);
iTmpG = ROUND_SHR(iTmpG, 14);
iTmpB = ROUND_SHR(iTmpB, 14);
R = (int)RANGE_INT(iTmpR, 0, 255);
G = (int)RANGE_INT(iTmpG, 0, 255);
B = (int)RANGE_INT(iTmpB, 0, 255);
}
// --------------------------------------------------------------
int main(void)
{
int iTestR = 30;
int iTestG = 60;
int iTestB = 90;
int iTestY = 0;
int iTestCb = 0;
int iTestCr = 0;
RGBConvertToYCbCr(iTestR, iTestG, iTestB, iTestY, iTestCb, iTestCr);
printf("1.(%d,%d,%d)->(%d,%d,%d)\r\n\r\n", iTestR, iTestG, iTestB, iTestY, iTestCb, iTestCr);
YCbCrConvertToRGB(iTestY, iTestCb, iTestCr,iTestR, iTestG, iTestB);
printf("2.(%d,%d,%d)->(%d,%d,%d)\r\n\r\n", iTestY, iTestCb, iTestCr, iTestR, iTestG, iTestB);
getchar();
return 0;
}
RGB与YCbCr相互转换
最新推荐文章于 2025-02-12 16:52:19 发布