Advanced MFC Programming
Advanced MFC Programming Advanced MFC Programming
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
- Page 275 and 276: Chapter 9. Font } CScrollView::OnKe
- Page 277 and 278: Chapter 9. Font …… } nKeyState=
- Page 279 and 280: Chapter 9. Font To check out if the
- Page 281 and 282: Chapter 9. Font the same, it indica
- Page 283 and 284: Chapter 9. Font } if(m_nSelIndexBgn
- Page 285 and 286: Chapter 9. Font GHND GPTR Same with
- Page 287 and 288: Chapter 9. Font …… } { Since th
- Page 289 and 290: Chapter 9. Font Function CDocument:
- Page 291 and 292: Chapter 9. Font …… void CGDIDoc
- Page 293 and 294: Chapter 9. Font …… { } UpdateAl
- Page 295 and 296: Chapter 9. Font …… …… case
- Page 297 and 298: Chapter 9. Font client window to be
- Page 299 and 300: Chapter 10. Bitmap ); DWORD dwRop T
- Page 301 and 302: Chapter 10. Bitmap Sample 10.1-2\GD
- Page 303 and 304: Chapter 10. Bitmap The next part of
- Page 305 and 306: Chapter 10. Bitmap Sample Sample 10
- Page 307 and 308: Chapter 10. Bitmap } pPalDraw=pDoc-
- Page 309 and 310: Chapter 10. Bitmap Member bfType mu
- Page 311 and 312: Chapter 10. Bitmap …… dwSize=bf
- Page 313 and 314: Chapter 10. Bitmap …… …… }
- Page 315 and 316: Chapter 10. Bitmap Now the size of
- Page 317 and 318: Chapter 10. Bitmap fuColorUse Speci
- Page 319 and 320: Chapter 10. Bitmap if(hData != NULL
- Page 321 and 322: Chapter 10. Bitmap …… …… cl
- Page 323 and 324: Chapter 10. Bitmap used as the orig
- Page 325: Chapter 10. Bitmap With the above i
- Page 329 and 330: Chapter 10. Bitmap …… } } { if(
- Page 331 and 332: Chapter 10. Bitmap …… } rgbQuad
- Page 333 and 334: Chapter 10. Bitmap DIB Section Both
- Page 335 and 336: Chapter 10. Bitmap } } if(m_pBmpMas
- Page 337 and 338: Chapter 10. Bitmap …… m_pBmpOld
- Page 339 and 340: Chapter 10. Bitmap BITMAP bm; …
- Page 341 and 342: Chapter 10. Bitmap Draw this portio
- Page 343 and 344: Chapter 10. Bitmap 1) Paint the des
- Page 345 and 346: Chapter 10. Bitmap color bitmap, bu
- Page 347 and 348: Chapter 10. Bitmap will destroy the
- Page 349 and 350: Chapter 10. Bitmap 10) The total im
- Page 351 and 352: Chapter 11. Sample: Simple Paint Af
- Page 353 and 354: Chapter 11. Sample: Simple Paint CG
- Page 355 and 356: Chapter 11. Sample: Simple Paint Th
- Page 357 and 358: Chapter 11. Sample: Simple Paint Co
- Page 359 and 360: Chapter 11. Sample: Simple Paint vo
- Page 361 and 362: Chapter 11. Sample: Simple Paint m_
- Page 363 and 364: Chapter 11. Sample: Simple Paint cu
- Page 365 and 366: Chapter 11. Sample: Simple Paint Te
- Page 367 and 368: Chapter 11. Sample: Simple Paint
- Page 369 and 370: Chapter 11. Sample: Simple Paint CS
- Page 371 and 372: Chapter 11. Sample: Simple Paint fu
- Page 373 and 374: Chapter 11. Sample: Simple Paint
- Page 375 and 376: Chapter 11. Sample: Simple Paint DW
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