11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10. Bitmap<br />

pBmp->GetBitmap(&bm);<br />

bi.biSize=sizeof(BITMAPINFOHEADER);<br />

bi.biWidth=bm.bmWidth;<br />

bi.biHeight=bm.bmHeight;<br />

bi.biPlanes=bm.bmPlanes;<br />

bi.biBitCount=bm.bmPlanes*bm.bmBitsPixel;<br />

bi.biCompression=BI_RGB;<br />

bi.biSizeImage=0;<br />

bi.biXPelsPerMeter=0;<br />

bi.biYPelsPerMeter=0;<br />

bi.biClrUsed=0;<br />

bi.biClrImportant=0;<br />

dwSizeCT=GetColorTableSize(bi.biBitCount);<br />

dwDibLen=bi.biSize+dwSizeCT*sizeof(RGBQUAD);<br />

pPalOld=dc.SelectPalette(&m_palDraw, FALSE);<br />

dc.RealizePalette();<br />

hDib=::GlobalAlloc(GHND, dwDibLen);<br />

lpBi=(LPBITMAPINFO)::GlobalLock(hDib);<br />

……<br />

lpBi->bmiHeader=bi;<br />

We first fill the information obtained previously into a BITMAPINFOHEADER object. This is necessary<br />

because when calling function ::GetDIBits(…), we need to provide a BITMAPINFOHEADER type pointer<br />

which contains useful information. Here, some unimportant members of BITMAPINFOHEADER are assigned 0s<br />

(biSizeImage, biXPelsPerMeter…). Then the size of the color table is calculated and a global memory<br />

that is big enough for holding bitmap information header and color table is allocated, and the bitmap<br />

information header is stored into the buffers. We will use these buffers to receive color table.<br />

Although the memory size for storing bitmap data can be calculated from the information already<br />

known, usually it is not done at this point. Generally the color table and the bitmap data are retrieved<br />

separately, in the first step, only the memory that is big enough for storing structure BITMAPINFOHEADER and<br />

the color table is prepared. When color table is being retrieved, the bitmap information header will also be<br />

updated at the same time. Since it is more desirable to calculate the bitmap data size using the updated<br />

information, in the sample, the memory size is updated after the color table is obtained successfully, and<br />

the global memory is reallocated for retrieving the bitmap data.<br />

We also need to select logical palette into the DC and realize it so that the bitmap pixels will be<br />

intepreted by its own color table.<br />

Function ::GetDIBits(…) is called in the next step to recieve BITMAPINFOHEADER data and the color<br />

table. Because some device drivers do not fill member biImageSize (This member carries redunant<br />

information with members biWidth, biHeight, and biBitCount), we need to calculate it if necessary:<br />

……<br />

……<br />

VERIFY<br />

(<br />

::GetDIBits<br />

(<br />

dc.GetSafeHdc(),<br />

(HBITMAP)pBmp->GetSafeHandle(),<br />

0,<br />

(WORD)bi.biHeight,<br />

NULL,<br />

lpBi,<br />

DIB_RGB_COLORS<br />

)<br />

);<br />

bi=lpBi->bmiHeader;<br />

::GlobalUnlock(hDib);<br />

if(bi.biSizeImage == 0)<br />

{<br />

bi.biSizeImage=WIDTHBYTES(bi.biBitCount*bi.biWidth)*bi.biHeight;<br />

}<br />

298

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!