Converting Raw to BMP and vice versa

Posted on 2006-05-16
Last Modified: 2013-11-20
Hello there,
I am currently developing a Image Processing program using MFC. I got into a serious problem, I have a seperate program that can read a RAW image and then enhance it with features I built into it. I wanted to build an interface for it, so I wanted to use MFC (I am using SAMS Visual C++ book). Anyways right now I am stuck because in the book it only mentions that it can display BMP images. I need a solution of converting a RAW image into a Bmp image and vice cersa. Unless there is alternative solution where it can display the Raw image. Another major point is that all these images are grey-scale, so I do not need the storing the colours if that is important.
Thanks alot

Question by:killaz4life
    LVL 12

    Assisted Solution

    You need to use the API StretchDIBits.
    The example below is for image having 1 byte per pixel

    BITMAPINFO* m_pBitmapInfo;   // class member
    int i;

    m_pBitmapInfo = (BITMAPINFO*)new char[sizeof(BITMAPINFO) + sizeof(RGBQUAD)*256];

    m_pBitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    m_pBitmapInfo->bmiHeader.biWidth = width; // You need the width of the image
    m_pBitmapInfo->bmiHeader.biHeight = -height;  // You need the height of the image
    m_pBitmapInfo->bmiHeader.biPlanes = 1;
    m_pBitmapInfo->bmiHeader.biBitCount = 8;
    m_pBitmapInfo->bmiHeader.biCompression  = BI_RGB;
    m_pBitmapInfo->bmiHeader.biSizeImage = 0;
    m_pBitmapInfo->bmiHeader.biXPelsPerMeter = 0;
    m_pBitmapInfo->bmiHeader.biYPelsPerMeter = 0;
    m_pBitmapInfo->bmiHeader.biClrUsed = 256;
    m_pBitmapInfo->bmiHeader.biClrImportant = 0;

    for ( i = 0; i < 256; i++ )
            m_pBitmapInfo->bmiColors[i].rgbBlue = i;
            m_pBitmapInfo->bmiColors[i].rgbGreen = i;
            m_pBitmapInfo->bmiColors[i].rgbRed = i;
            m_pBitmapInfo->bmiColors[i].rgbReserved = 0;

    m_pImage,           // Your raw image data.
    LVL 12

    Expert Comment

    Some of the information like width, height, bits per pixel, etc. should be available with you to populate the BITMAPINFO structure.

    Good luck !
    LVL 22

    Accepted Solution


    As you know there is no header information exists in raw file. Just bitmap data bits.

    Assuming you have this buffer read from raw file in 'pDibBits' ( byte aligned bitmap buffer ).. your code for converting raw to bitmap will be something like this :

       CFile outfile;

        outfile.Open("test32bpp.bmp",  CFile::modeReadWrite | CFile::modeCreate | CFile::shareExclusive);
        int sizeofColorTable =  0; //32bpp bitmap
        int sizeofImage = imageSize;

        // construct the file header
        bfh.bfType = 0x4D42; // 'BM'
        bfh.bfSize =  sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +  sizeofColorTable + sizeofImage;
        bfh.bfReserved1 = 0;
        bfh.bfReserved2 = 0;
        bfh.bfOffBits =   sizeof(BITMAPFILEHEADER) +  sizeof(BITMAPINFOHEADER) +  sizeofColorTable;
       // write the file header
        int iSize = sizeof(bfh);

        // write bitmapfileheader
        outfile.Write(&bfh, iSize);

        iSize = sizeof(BITMAPINFOHEADER) + sizeofColorTable;

       infoHeader->biBitCount = 32;
       infoHeader->biSizeImage = sizeofImage;
       infoHeader->biClrUsed = 0;
       infoHeader->biClrImportant = 0;
       infoHeader->biWidth = Width;
       infoHeader->biHeight = -Height;

        // write bitmapinfoheader
        outfile.Write(infoHeader, iSize);

        // write the bits
        iSize = sizeofImage;

        outfile.Write(pDibBits, iSize);

    As you can see you just need to init your BITMAP INFO header then write bitmap header to file, then write your raw bitmap data thats it. There is no need to write color table as you can see in above code.

    Remember to write to BITMAP file you need to construct BITMAPFILEHEADER along with BITMAPINFOHEADER as shown in above code example.


    Author Comment

    I have a problem with both codes, when i put the code in the function, apparently it does not recognize either CFile or BITMAPINFO*
    I have the two main libraries, stdlib.h and iostream and cmath.h.

    Author Comment

    dont consider that sorry, stupid mistake
    LVL 8

    Expert Comment

    You could also use

    CreateDIBSection() to get bitmap handle
    and then initialize bitmap bits (ppvBits parameter).
    LVL 22

    Expert Comment

    killaz4life ,
     While giving code here we are assuming you are using MFC. CFile is MFC file i/o class and BITMAPINFO is declared in windows.. Which compiler you are using ?


    Author Comment

    I was using C++ builder, but I had to move the code into the MFC program. Anyways I had a another problem so I need to fix it, it will take me 2 days. Once I do it, I will then be able to add the code you guys asked me to put and then be able to accept one of the answers.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Handling string inputs in C/Linux 23 157
    Running shell command from Access 3 59
    move a line in eclipse 3 45
    has77  challenge 9 56
    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now