Click here to Skip to main content
15,881,712 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I'm trying to develop an application that saves a pixel information as bitmap file using VS2008 ( vc++ ). Currently, i have a byte buffer containing pixel data, which is in 1 bit per pixel format. The width and column of the image is 512 x 512. I used Lockbits() method to create the bitmap data and used Bitmap::Save() method to save it in a bmp file. But the problem is that, from the bitmap it seems that some data has been lost and image is not clear.

Could you please help me to create a 1bpp Bitmap ?

The code i used is:
<br />
int nNumComps = pstImageInfo_i->usBitsAllocated;<br />
         short sSamplesPerPixel = 1;<br />
         int nWidth = pstImageInfo_i->usColumns;<br />
         int nHeight = pstImageInfo_i->usRows;         <br />
         int nFormat = PixelFormat1bppIndexed;<br />
         int nNumColors = 2;<br />
         ColorPalette* pPalette = 0;<br />
        pPalette = reinterpret_cast< ColorPalette* >( malloc( sizeof( ColorPalette ) + <br />
                                                              ( nNumColors - 1 )<br />
                                                              * sizeof( ARGB )));<br />
        if( 0 != pPalette )<br />
        {<br />
            pPalette->Flags = PaletteFlagsGrayScale;<br />
            pPalette->Count = nNumColors;<br />
            Color clr( 255, 0, 0, 0 );<br />
            pPalette->Entries[0] = clr.GetValue();<br />
            Color clrWhite( 255, 255, 255, 255 );<br />
            pPalette->Entries[1] = clrWhite.GetValue();<br />
        }<br />
        Bitmap bmpOverlay( nWidth, nHeight, nFormat );<br />
        BitmapData btmpData;<br />
        ZeroMemory( &btmpData, sizeof( btmpData ));<br />
        Rect BMPRect( 0, 0, nWidth, nHeight );<br />
        bmpOverlay.LockBits( &BMPRect, ImageLockModeWrite,<br />
                             nFormat, &btmpData );<br />
        //btmpData.Stride = -btmpData.Stride;<br />
        int nSize = pstImageInfo_i->dwBufferSize;<br />
        // Considering only 1 frame<br />
        if( pstImageInfo_i->usFramesInOverlay > 1 )<br />
        {<br />
            nSize = nSize / pstImageInfo_i->usFramesInOverlay;<br />
        }<br />
         memcpy( btmpData.Scan0, pstImageInfo_i->pbData,<br />
                  nSize );<br />
        bmpOverlay.UnlockBits( &btmpData );<br />
        Status ReturnStatus = bmpOverlay.RotateFlip( RotateNoneFlipY );<br />
        if( Ok ==  ReturnStatus )<br />
        {<br />
            if( pPalette )<br />
            {<br />
                ReturnStatus = bmpOverlay.SetPalette( pPalette );<br />
                delete pPalette;<br />
                pPalette = 0;<br />
            }<br />
        }<br />
         CLSID bmpClsid;<br />
         GetEncoderClsid(L"image/bmp", &bmpClsid);<br />
         bmpOverlay.Save( _T( "ImageOverlay.bmp" ), &bmpClsid, NULL );<br />



Thanks in Advance,
Aneev
Posted
Updated 22-Feb-10 1:14am
v3

Aneev S wrote:
But the problem is that, from the bitmap it seems that some data has been lost and image is not clear.


I assume you saved it as a jpg. The jpg file format does that.
 
Share this answer
 
Show the code you used to create & save the BMP. It is common that the functions used to reduce the BPP will dither the image, which is likely what you are interpreting as "lost data".
 
Share this answer
 
I would create and save the bitmap as following :) :
...
  HBITMAP hBMP = CreateDIBSection(..);
...
  CImage cImage;
  cImage.Attach(hBMP);
  cImage.Save(..);
...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900