QQ登录

只需一步,快速开始

MFC如何使用灰度值绘制图像

[ 复制链接 ]
有时灰度图像比颜色图像更具有表达效果,
下面的函数DrawGrayScale() 实现了灰度图像的绘制,它仅能处理256色。

如果显示卡支持256色调色板,函数将从DIB的颜色信息中创建灰度调色板,在绘制图像到设备环境前,实现该调色板。
灰度颜色与红、绿和蓝颜色相等同,如果显示卡支持256色以上,它将不支持调色板,调色板操作也不会正常工作,
这样,就得改变DIB中的颜色表,在显示位图之前,改变所有的颜色项。
  1. // DrawGrayScale        - Draws a bitmap in gray scale
  2. // pDC                        - Pointer to target device context
  3. // hDIB                        - Handle of device-independent bitmap
  4. //
  5. void DrawGrayScale( CDC *pDC, HANDLE hDIB )
  6. {
  7.         CPalette pal;
  8.         CPalette *pOldPalette;

  9.         BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
  10.        
  11.         int nColors = bmInfo.bmiHeader.biClrUsed ? bmInfo.bmiHeader.biClrUsed :
  12.                                 1 << bmInfo.bmiHeader.biBitCount; // Create the palette if needed if( pDC-GetDeviceCaps(RASTERCAPS) & RC_PALETTE && nColors <= 256 ) { // The device supports a palette and bitmap has color table // Allocate memory for a palette UINT nSize="sizeof(LOGPALETTE)" + (sizeof(PALETTEENTRY) * nColors); LOGPALETTE *pLP="(LOGPALETTE" *) new BYTE[nSize]; pLP-palVersion = 0x300;
  13.                 pLP->palNumEntries = nColors;
  14.                
  15.                 for( int i=0; i <ncolors; i++) { long lSquareSum="bmInfo.bmiColors[i].rgbRed" * bmInfo.bmiColors[i].rgbRed + bmInfo.bmiColors[i].rgbGreen * bmInfo.bmiColors[i].rgbGreen + bmInfo.bmiColors[i].rgbBlue * bmInfo.bmiColors[i].rgbBlue; int nGray="(int)sqrt(((double)lSquareSum)/3);" pLP-palPalEntry[i].peRed = nGray;
  16.                         pLP->palPalEntry[i].peGreen = nGray;
  17.                         pLP->palPalEntry[i].peBlue = nGray;
  18.                         pLP->palPalEntry[i].peFlags = 0;
  19.                 }
  20.                
  21.                 pal.CreatePalette( pLP );
  22.                
  23.                 delete[] pLP;

  24.                 // Select the palette
  25.                 pOldPalette = pDC->SelectPalette(&pal, FALSE);
  26.                 pDC->RealizePalette();
  27.         }
  28.         else if((pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE) == 0 && nColors <= 256 ) { // Device does not supports palettes but bitmap has a color table // Modify the bitmaps color table directly // Note : This ends up changing the DIB. If that is not acceptable then // copy the DIB and then change the copy rather than the original for( int i="0;" i < nColors; i++) { long lSquareSum="bmInfo.bmiColors[i].rgbRed" * bmInfo.bmiColors[i].rgbRed + bmInfo.bmiColors[i].rgbGreen * bmInfo.bmiColors[i].rgbGreen + bmInfo.bmiColors[i].rgbBlue * bmInfo.bmiColors[i].rgbBlue; int nGray="(int)sqrt(((double)lSquareSum)/3);" bmInfo.bmiColors[i].rgbRed="nGray;" bmInfo.bmiColors[i].rgbGreen="nGray;" bmInfo.bmiColors[i].rgbBlue="nGray;" } } int nWidth="bmInfo.bmiHeader.biWidth;" int nHeight="bmInfo.bmiHeader.biHeight;" // Draw the image LPVOID lpDIBBits="(LPVOID)(bmInfo.bmiColors" + nColors); ::SetDIBitsToDevice(pDC-m_hDC,        // hDC
  29.                 0,                                // XDest
  30.                 0,                                // YDest
  31.                 nWidth,                                // nDestWidth
  32.                 nHeight,                        // nDestHeight
  33.                 0,                                // XSrc
  34.                 0,                                // YSrc
  35.                 0,                                // nStartScan
  36.                 nHeight,                        // nNumScans
  37.                 lpDIBBits,                        // lpBits
  38.                 (LPBITMAPINFO)hDIB,                // lpBitsInfo
  39.                 DIB_RGB_COLORS);                // wUsage
  40.        
  41.        
  42.         pDC->SelectPalette(pOldPalette, FALSE);
  43. }
复制代码

回复

使用道具 举报

点击查看
快速回复 返回列表 客服中心 搜索