11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 10. Bitmap<br />

dwSize=<br />

(<br />

sizeof(BITMAPINFOHEADER)+<br />

nSizeCT*sizeof(RGBQUAD)+<br />

WIDTHBYTES(8*lpBi24->bmiHeader.biWidth)*lpBi24->bmiHeader.biHeight<br />

);<br />

hDIB=::GlobalAlloc(GHND, dwSize);<br />

ASSERT(hDIB);<br />

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

ASSERT(lpBi);<br />

*lpBi=*lpBi24;<br />

lpBi->bmiHeader.biBitCount=8;<br />

lpBi->bmiHeader.biSizeImage=WIDTHBYTES<br />

(<br />

8*lpBi->bmiHeader.biWidth<br />

)*lpBi->bmiHeader.biHeight;<br />

lpBi->bmiHeader.biClrUsed=0;<br />

……<br />

for(n=0; nbmiColors[n]=*(LPRGBQUAD)arRgbQuad[n];<br />

}<br />

The differences between the new and old bitmap information headers are member bitBitCount (8 for<br />

256-color format), biSizeImage, and biClrUsed (Member biClrUsed can be used to indicate the color<br />

usage. For simplicity, it is set to zero).<br />

Next we need to convert explicit RGB values to color table indices. As mentioned before, there are two<br />

situations. If the color table is extracted directly from the bitmap, we must compare each pixel with every<br />

entry of the color table, find the index, and use it as the bitmap bit value. Otherwise the index can be<br />

formed by omitting the lower 5 bits of red and green colors, the lower 6 bits of blue color then combining<br />

them together. This eleminates the procedure of looking up the color table. It is possible for us to do so<br />

because the color table is created in a way that if we implement the above operation on any color contained<br />

in the table, the result will become the index of the corresponding entry.<br />

For example, entry 1 contains color (32, 0, 0), which is (0x20, 0x00, 0x00). After bit omission, it<br />

becomes (0x01, 0x00, 0x00). The followng calculation will result in the index of this entry:<br />

red | (green bmiHeader.biWidth)*j<br />

);<br />

lpRowSrc=<br />

(<br />

(LPSTR)lpBi24+<br />

sizeof(BITMAPINFOHEADER)+<br />

WIDTHBYTES(lpBi24->bmiHeader.biBitCount*lpBi24->bmiHeader.biWidth)*j<br />

);<br />

for(i=0; ibmiHeader.biWidth; i++)<br />

{<br />

rgbQuad.rgbBlue=*lpRowSrc;<br />

rgbQuad.rgbGreen=*(lpRowSrc+1);<br />

rgbQuad.rgbRed=*(lpRowSrc+2);<br />

rgbQuad.rgbReserved=0;<br />

if(bStandardPal == TRUE)<br />

312

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

Saved successfully!

Ooh no, something went wrong!