Converting Raw to BMP and vice versa

Posted on 2006-05-16
Medium Priority
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
  • 3
  • 2
  • 2
  • +1
LVL 12

Assisted Solution

rajeev_devin earned 750 total points
ID: 16696170
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

ID: 16696173
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

mahesh1402 earned 750 total points
ID: 16696732

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.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 16697795
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

ID: 16697813
dont consider that sorry, stupid mistake

Expert Comment

ID: 16697995
You could also use

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

Expert Comment

ID: 16699837
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

ID: 16708860
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

850 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