RGB与YCbCr相互转换

#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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值