C++ method of reading BMP bitmap data

Picture files have a fixed format, like BMP pictures: file header + bitmap color data.

The file header generally uses the following code when reading:

BITMAPFILEHEADER fileheader={
    
    0}; 
    fread(&fileheader,sizeof(fileheader),1,fp); 
    if(fileheader.bfType!=0x4D42)  // 判断是否为BMP图片
    {
    
     
        fclose(fp); 
        return ; 
    } 
 
    BITMAPINFOHEADER head; 
    fread(&head,sizeof(BITMAPINFOHEADER),1,fp);  
    long bmpWidth = head.biWidth;  //获取图片的宽
    long bmpHeight = head.biHeight;  //获取图片的宽
    WORD biBitCount = head.biBitCount; 
    if(biBitCount != 24) // 是否为24位位图
    {
    
     
        ::AfxMessageBox(_T("请选择24位位图!")); 
        fclose(fp); 
        return ; 
    } 

When the file is read here, it is necessary to read the color data of the bitmap. Then, before reading, we need to know the size of the data, so that we can allocate the buffer of a specific university, and then read the data completely into the buffer.

Then calculating the size of the bitmap data is generally done with the following code.

int totalSize = (bmpWidth *biBitCount/8+3)/4*4*bmpHeight; 
BYTE *pBmpBuf = new BYTE[totalSize];

After these tasks are completed, we can use the fread command to directly read the contents of the file until the end of the file is read.

The entire specific code implementation is as follows:

FILE *fp=NULL; 
    int ret = fopen_s(&fp,"D:\\11.bmp","rb"); 
    if(fp==0)    
    {
    
     
        return ; 
    } 
    BITMAPFILEHEADER fileheader={
    
    0}; 
    fread(&fileheader,sizeof(fileheader),1,fp); 
    if(fileheader.bfType!=0x4D42) 
    {
    
     
        fclose(fp); 
        return ; 
    } 
 
    BITMAPINFOHEADER head; 
    fread(&head,sizeof(BITMAPINFOHEADER),1,fp);  
    long bmpWidth = head.biWidth; 
    long bmpHeight = head.biHeight; 
    WORD biBitCount = head.biBitCount; 
    if(biBitCount != 24) 
    {
    
     
        ::AfxMessageBox(_T("请选择24位位图!")); 
        fclose(fp); 
        return ; 
    } 
 
    int totalSize = (bmpWidth *biBitCount/8+3)/4*4*bmpHeight; 
    BYTE *pBmpBuf = new BYTE[totalSize]; 
    size_t size = 0; 
    while(true) 
    {
    
     
        int iret = fread(&pBmpBuf[size],1,1,fp); 
        if(iret == 0) 
            break; 
        size = size + iret; 
    } 
    fclose(fp); 
 
    int i,j; 
    CClientDC dc(this); 
    int pitch=bmpWidth%4; 
    for(i=0;i<bmpHeight;i++) 
    {
    
     
        int realPitch=i*pitch; 
        for(j=0;j<bmpWidth;j++) 
        {
    
     
            dc.SetPixel(j,i,RGB( 
                pBmpBuf[(i*bmpWidth+j)*3+2+realPitch], 
                pBmpBuf[(i*bmpWidth+j)*3+1+realPitch], 
                pBmpBuf[(i*bmpWidth+j)*3+realPitch])); 
        } 
    } 
delete [] pBmpBuf; pBmpBuf = NULL;
    return ; 
    

It is better to apply what you have learned, and there may be shortcomings in the above, welcome to point out the discussion.
(More free C/C++, Linux, Nginx, ZeroMQ, MySQL, Redis, fastdfs, MongoDB, ZK, streaming media, CDN, P2P, K8S, Docker, TCP/IP, coroutine, DPDK, etc. multiple knowledge points dry goods Learning materials plus group 960994558)

Guess you like

Origin blog.csdn.net/weixin_52622200/article/details/110392243