Advanced MFC Programming

Advanced MFC Programming Advanced MFC Programming

math.hcmuns.edu.vn
from math.hcmuns.edu.vn More from this publisher
11.04.2014 Views

Chapter 10. Bitmap LPBITMAPINFO lpBi24; HGLOBAL hDIB; DWORD dwSize; int nSizeCT; int i, j; int n; LPSTR lpRowSrc; LPSTR lpRowTgt; POSITION pos; CGDIView *ptrView; CPtrArray arRgbQuad; LPRGBQUAD lpRgbQuad; RGBQUAD rgbQuad; BOOL bHit; BOOL bStandardPal; BYTE red, green, blue; …… AfxGetApp()->DoWaitCursor(TRUE); lpBi24=(LPBITMAPINFO)::GlobalLock(m_hDIB); ASSERT(lpBi24); for(j=0; jbmiHeader.biHeight; j++) { lpRowSrc= ( (LPSTR)lpBi24+ sizeof(BITMAPINFOHEADER)+ WIDTHBYTES(lpBi24->bmiHeader.biBitCount*lpBi24->bmiHeader.biWidth)*j ); for(i=0; ibmiHeader.biWidth; i++) { rgbQuad.rgbBlue=*lpRowSrc; rgbQuad.rgbGreen=*(lpRowSrc+1); rgbQuad.rgbRed=*(lpRowSrc+2); rgbQuad.rgbReserved=0; bHit=FALSE; for(n=0; n

Chapter 10. Bitmap …… …… if(arRgbQuad.GetSize() > 256) { while(arRgbQuad.GetSize()) { delete (LPRGBQUAD)arRgbQuad.GetAt(0); arRgbQuad.RemoveAt(0); } red=green=blue=0; for(i=0; irgbBlue=blue; lpRgbQuad->rgbGreen=green; lpRgbQuad->rgbRed=red; lpRgbQuad->rgbReserved=0; if(!(red+=32))if(!(green+=32))blue+=64; arRgbQuad.Add(lpRgbQuad); } bStandardPal=TRUE; } else bStandardPal=FALSE; If the size of color table is greater than 256, we first delete the color table, then create a new color table that contains only 256 colors. This color table comprises 256 colors that are evenly distributed in a 8×8×4 3-D space, which has the following contents: (0, 0, 0) (32, 0, 0) (64, 0, 0) … (224, 0, 0) (0, 32, 0) (32, 32, 0) (64, 0, 0) … (224, 32, 0) (0, 64, 0, ) (32, 64, 0) (64, 64, 0) … (224, 64, 0) …… (0, 224, 0, ) (32, 224, 0) (64, 224, 0) … (224, 224, 0) (0, 0, 64) (32, 0, 64) (64, 0, 64) … (224, 0, 64) (0, 32, 64) (32, 32, 64) (64, 32, 64) … (224, 32, 64) (0, 64, 64) (32, 64, 64) (64, 64, 64) … (224, 64, 64) …… (0, 224, 64) (32, 224, 64) (64, 224, 64) … (224, 224, 64) …… …… (0, 0, 192) (32, 0, 192) (64, 0, 192) … (224, 0, 192) (0, 32, 192) (32, 32, 192) (64, 32, 192) … (224, 32, 192) (0, 64, 192) (32, 64, 192) (64, 64, 192) … (224, 64, 192) …… (0, 224, 192) (32, 224, 192) (64, 224, 192) … (224, 224, 192) For a 24-bit color, if we use only 3 most significant bits of red and green colors, and 2 most significant bits of blue color, and set rest bits to 0. Every possible RGB combination (8 bits for each color) has a corresponding entry in this table. We use a flag bStandardPal to indicate which algorithm was used to generate the color table. This is important because for the two situations the procedure of converting explicit RGB values to indices of color table is different. If the color table is generated directly from the colors contained in the bitmap (first case), each pixel can be mapped to an index in the color table by comparing it with every color in the color table (there must be a hit). Otherwise, we must omit some bits before looking up the color table (second case). Following is a portion of function CGDIDoc::OnConvertRGBto256() that allocates buffers from global memory, fill the buffers with bitmap information header and color table: …… nSizeCT=256; 311

Chapter 10. Bitmap<br />

……<br />

……<br />

if(arRgbQuad.GetSize() > 256)<br />

{<br />

while(arRgbQuad.GetSize())<br />

{<br />

delete (LPRGBQUAD)arRgbQuad.GetAt(0);<br />

arRgbQuad.RemoveAt(0);<br />

}<br />

red=green=blue=0;<br />

for(i=0; irgbBlue=blue;<br />

lpRgbQuad->rgbGreen=green;<br />

lpRgbQuad->rgbRed=red;<br />

lpRgbQuad->rgbReserved=0;<br />

if(!(red+=32))if(!(green+=32))blue+=64;<br />

arRgbQuad.Add(lpRgbQuad);<br />

}<br />

bStandardPal=TRUE;<br />

}<br />

else bStandardPal=FALSE;<br />

If the size of color table is greater than 256, we first delete the color table, then create a new color table<br />

that contains only 256 colors. This color table comprises 256 colors that are evenly distributed in a 8×8×4<br />

3-D space, which has the following contents:<br />

(0, 0, 0) (32, 0, 0) (64, 0, 0) … (224, 0, 0)<br />

(0, 32, 0) (32, 32, 0) (64, 0, 0) … (224, 32, 0)<br />

(0, 64, 0, ) (32, 64, 0) (64, 64, 0) … (224, 64, 0)<br />

……<br />

(0, 224, 0, ) (32, 224, 0) (64, 224, 0) … (224, 224, 0)<br />

(0, 0, 64) (32, 0, 64) (64, 0, 64) … (224, 0, 64)<br />

(0, 32, 64) (32, 32, 64) (64, 32, 64) … (224, 32, 64)<br />

(0, 64, 64) (32, 64, 64) (64, 64, 64) … (224, 64, 64)<br />

……<br />

(0, 224, 64) (32, 224, 64) (64, 224, 64) … (224, 224, 64)<br />

……<br />

……<br />

(0, 0, 192) (32, 0, 192) (64, 0, 192) … (224, 0, 192)<br />

(0, 32, 192) (32, 32, 192) (64, 32, 192) … (224, 32, 192)<br />

(0, 64, 192) (32, 64, 192) (64, 64, 192) … (224, 64, 192)<br />

……<br />

(0, 224, 192) (32, 224, 192) (64, 224, 192) … (224, 224, 192)<br />

For a 24-bit color, if we use only 3 most significant bits of red and green colors, and 2 most significant<br />

bits of blue color, and set rest bits to 0. Every possible RGB combination (8 bits for each color) has a<br />

corresponding entry in this table.<br />

We use a flag bStandardPal to indicate which algorithm was used to generate the color table. This is<br />

important because for the two situations the procedure of converting explicit RGB values to indices of<br />

color table is different. If the color table is generated directly from the colors contained in the bitmap (first<br />

case), each pixel can be mapped to an index in the color table by comparing it with every color in the color<br />

table (there must be a hit). Otherwise, we must omit some bits before looking up the color table (second<br />

case).<br />

Following is a portion of function CGDIDoc::OnConvertRGBto256() that allocates buffers from global<br />

memory, fill the buffers with bitmap information header and color table:<br />

……<br />

nSizeCT=256;<br />

311

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

Saved successfully!

Ooh no, something went wrong!