BITMAP转YUV420

这段代码提供了从BITMAP 888、1555和565格式转换到YUV420sp的函数。通过读取BITMAP文件并进行颜色空间转换,将位图数据适配为YUV420sp格式,适用于视频处理和显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// only support to BITMAP 888 convert to YUV420sp
U8 *OSD_Bitmap888ToYUV(U8 *bitmapFileBuff, int *pYuvWidth, int *pYuvHeight)
{
    BITMAPFILEHEADER fileHeader;
    BITMAPINFOHEADER infoHead;
    long offset = 0;

    if(NULL == bitmapFileBuff)
    {
        LOG_ERROR(DBG_MODULE_OSD, "[%s] param NULL, buf:%p, \n", __func__, bitmapFileBuff);
        return NULL;
    }

    memcpy(&fileHeader, bitmapFileBuff, sizeof(BITMAPFILEHEADER));
    offset += sizeof(BITMAPFILEHEADER);

    memcpy(&infoHead, bitmapFileBuff + offset, sizeof(BITMAPINFOHEADER));
    offset += sizeof(BITMAPINFOHEADER);


    LOG_INFO(DBG_MODULE_OSD, "w:%d, h:%d, bitCount:%d offset:%d\n", (int)infoHead.ciWidth, (int)infoHead.ciHeight, infoHead.ciBitCount, (int)fileHeader.cfoffBits);
    LOG_INFO(DBG_MODULE_OSD, " infoHead.ciCompress:%x\n", (int)infoHead.ciCompress);

    int yuvWidth = infoHead.ciWidth;
    int yuvHeight = infoHead.ciHeight;
    *pYuvWidth = yuvWidth;
    *pYuvHeight = yuvHeight;
    U8 *yuvBuffer = (U8 *)malloc(yuvWidth * yuvHeight * 1.5);

    if(yuvBuffer == NULL)
    {
        LOG_ERROR(DBG_MODULE_OSD, "[%s] malloc failed \n", __func__);
        return NULL;
    }

    U8 *UVBuffer = yuvBuffer + yuvWidth * yuvHeight;
    U8 *bitmapBuff = bitmapFileBuff + offset;
    int bitmapLineByte = (infoHead.ciWidth * BIMAP888_BIT_COUNT + 3) / 4 * 4; //计算图像每行像素所占的字节数

    {
        int x , y;
        U8 *bitmapData;
        U8 *pYData = yuvBuffer;
        U8 *pUVData = UVBuffer;

        for(y = 0; y < (yuvHeight - yuvHeight % 2); y++)
        {
            bitmapData = bitmapBuff + bitmapLineByte * (yuvHeight - y - 1);

            for(x = 0; x < (yuvWidth - yuvWidth % 2); x++)
            {
                U8 bValue = *(bitmapData  + 0) ;
                U8 gValue = *(bitmapData  + 1) ;
                U8 rValue = *(bitmapData  + 2) ;
                // convert color rgb to yuv
                U8 yValue = (((66 * rValue + 129 * gValue +  25 * bValue + 128) >> 8) + 16);
                U8 uValue = ((-38 * rValue -  74 * gValue + 112 * bValue + 128) >> 8) + 128 ;
                U8 vValue = ((112 * rValue -  94 * gValue -  18 * bValue + 128) >> 8) + 128 ;

                *(pYData  + x) = yValue;

                if((!(x & 1)) && (!(y & 1)))
                {
                    *(pUVData + x) = uValue;
                    *(pUVData + x + 1) =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值