24位RGB数据保存为BMP图片

在做Qt与ffmpeg结合的视频播放器时,由于解码后是RGB24数据格式,不知道解码的数据是否正确,于是在网上找了很久才找到一个RGB24转bmp文件的方法,于是尝试了一下,发现生成的bmp文件是电影中的片段截图,才知道解码的RGB24数据是正确的,最终才成功用Qt显示出视频图像。
以下代码为转载的,原文地址:
http://blog.csdn.net/wesleyluo/article/details/6561518

bool SaveDIB2Bmp(int fileNum, const char * fileName, const char * filePath, int iWidth, int iHeight, BYTE *pBuffer)  
{  
    BITMAPINFOHEADER bih;  
    ConstructBih(iWidth,iHeight,bih);  
    BITMAPFILEHEADER bhh;  
    ContructBhh(iWidth,iHeight,bhh);  

    CHAR BMPFileName[1024];  
    int widthStep = (((iWidth * 24) + 31) & (~31)) / 8 ; //每行实际占用的大小(每行都被填充到一个4字节边界)  
    int DIBSize = widthStep * iHeight ;  //buffer的大小 (字节为单位)  

    //save  
    char path[1024];  
    char str[1024];  
    char num[10];
    sprintf(num,"%d",fileNum);    
    sprintf(str, fileName);  
    strcat(str, num);
    strcat(str,".bmp");  //frame100.bmp  
    sprintf(path,"%s", filePath);  
    strcat(path,str); //Path//frame100.bmp  

    strcpy(BMPFileName,path);  
    FILE *file;  

    if(file = fopen(BMPFileName, "wb"))  
    {//写入文件  
        fwrite((LPSTR)&bhh,sizeof(BITMAPFILEHEADER), 1, file);  
        fwrite((LPSTR)&bih,sizeof(BITMAPINFOHEADER), 1, file);  
        fwrite(pBuffer, sizeof(char), DIBSize, file);  
        fclose(file);  
        return true;  
    }  
    return false;  
}  


//构建BMP位图文件头  
void ContructBhh(int nWidth,int nHeight,BITMAPFILEHEADER& bhh) //add 2010-9-04  
{  
    int widthStep = (((nWidth * 24) + 31) & (~31)) / 8 ; //每行实际占用的大小(每行都被填充到一个4字节边界)  
    bhh.bfType = ((WORD) ('M' << 8) | 'B');  //'BM'  
    bhh.bfSize = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + widthStep * nHeight;  
    bhh.bfReserved1 = 0;  
    bhh.bfReserved2 = 0;  
    bhh.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);  
}  




//构建BMP文件信息头  
void ConstructBih(int nWidth,int nHeight,BITMAPINFOHEADER& bih)   
{  
    int widthStep = (((nWidth * 24) + 31) & (~31)) / 8 ;   


    bih.biSize=40;       // header size  
    bih.biWidth=nWidth;  
    bih.biHeight=nHeight;  
    bih.biPlanes=1;  
    bih.biBitCount=24;     // RGB encoded, 24 bit  
    bih.biCompression=BI_RGB;   // no compression 非压缩  
    bih.biSizeImage=widthStep*nHeight*3;  
    bih.biXPelsPerMeter=0;  
    bih.biYPelsPerMeter=0;  
    bih.biClrUsed=0;  
    bih.biClrImportant=0;   
}  
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值