使用C Win32访问位图中每个像素的RGB

时间:2012-03-08 15:36:00

标签: c bitmap pixel rgb

我有一个用简单的C win32编写的演示程序。该程序使用framegrabber从相机中抓取图像并将其存储在主存储缓冲区中(图像为8位/像素灰度)。该演示可以在计算机屏幕上显示抓取的图像。并将它们保存在硬盘上。

现在我想为这个程序添加一个函数,从每个帧的缓冲区中读取原始数据,然后在控制台上显示每个像素颜色(RGB),然后我可以使用它们进行分析。

到目前为止,我的代码中包含以下信息:

LPSTR CreateBMP( HWND hAppWnd, int nImageType )
{          
void            * pWinGBits = NULL;
int             i;
Z_BITMAPINFO    zWinGHeader;    //  bitmapinfo for cerating the DIB
//  create DC for bitmap.
hDCBits = CreateCompatibleDC( ghDCMain );

switch ( nImageType )
{
    case bayer_filter: 
    break;

    case color32:
    break;

    case color24:
    break;

    case color3x16:
    break;

    case bw1x10:
    break;

    default:
    case bw8:
    //  create bitmap-infoheader.
    zWinGHeader.bmiHeader.biSize        = sizeof( BITMAPINFOHEADER );
    zWinGHeader.bmiHeader.biPlanes      = 1;
    zWinGHeader.bmiHeader.biBitCount    = 8;
    zWinGHeader.bmiHeader.biCompression = BI_RGB;
    zWinGHeader.bmiHeader.biSizeImage   = 0;        
    zWinGHeader.bmiHeader.biClrUsed     = (1<<8);       
    zWinGHeader.bmiHeader.biClrImportant= 0;
    zWinGHeader.bmiHeader.biHeight      = -lYSize;
    zWinGHeader.bmiHeader.biWidth       = lXSize;
    //  create colortable fot bitmap (grayvalues).
    for (i = 0; i < 256; i++) 
    {
        zWinGHeader.bmiColors[i].rgbGreen   = i;        
        zWinGHeader.bmiColors[i].rgbBlue    = i;
        zWinGHeader.bmiColors[i].rgbRed     = i;            
        zWinGHeader.bmiColors[i].rgbReserved = 0;           
    }
    break;
}

//  cerate identity palette 
hPal = CreateIdentityPalette( zWinGHeader.bmiColors );

//  get new palette into DC and map into physical palette register.
hOldPal = SelectPalette( ghDCMain, hPal, FALSE);    
RealizePalette( ghDCMain );

//  cerate DIB-Section f黵 direct access of image-data.
hBitmap = CreateDIBSection(
    hDCBits,                        //  handle of device context
    (BITMAPINFO *)&zWinGHeader,     //  address of structure containing
                                    //  bitmap size, format and color data
    DIB_RGB_COLORS,                 //  color data type indicator: RGB values
                                    //  or palette indices
    &pWinGBits,                     //  pointer to variable to receive a pointer
                                    //  to the bitmap's bit values
    NULL,                           //  optional handle to a file mapping object
    0                               //  offset to the bitmap bit values within
                                    //  the file mapping object
);      
//  get bitmap into DC .
hOldBitmap = (HBITMAP)SelectObject( hDCBits, hBitmap );

return pWinGBits;           //  return pointer to DIB 
}

这里也是用于在磁盘上保存位图的代码:

BOOL SaveToFile(HBITMAP hBitmap3, LPCTSTR lpszFileName)
{   
hDC = CreateDC("DISPLAY", NULL, NULL, NULL);
iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
DeleteDC(hDC);
if (iBits <= 1)
    wBitCount = 1;
else if (iBits <= 4)
    wBitCount = 4;
else if (iBits <= 8)
    wBitCount = 8;
else
    wBitCount = 24; 
GetObject(hBitmap3, sizeof(Bitmap0), (LPSTR)&Bitmap0);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap0.bmWidth;
bi.biHeight =-Bitmap0.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrImportant = 0;
bi.biClrUsed = 256;
dwBmBitsSize = ((Bitmap0.bmWidth * wBitCount +31) & ~31) /8 * Bitmap0.bmHeight; 
hDib = GlobalAlloc(GHND,dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;

hPal = GetStockObject(DEFAULT_PALETTE);
if (hPal)
{ 
    hDC = GetDC(NULL);
    hOldPal2 = SelectPalette(hDC, (HPALETTE)hPal, FALSE);
    RealizePalette(hDC);
}


GetDIBits(hDC, hBitmap3, 0, (UINT) Bitmap0.bmHeight, (LPSTR)lpbi + sizeof(BITMAPINFOHEADER) 
+dwPaletteSize, (BITMAPINFO *)lpbi, DIB_RGB_COLORS);

if (hOldPal2)
{
    SelectPalette(hDC, (HPALETTE)hOldPal2, TRUE);
    RealizePalette(hDC);
    ReleaseDC(NULL, hDC);
}
//create the file and save it on the disk
} 

这是调色板功能:

HPALETTE CreateIdentityPalette( RGBQUAD* aRGB )
{
int i;
int nStaticColors;
struct {
    WORD Version;
    WORD NumberOfEntries;
    PALETTEENTRY aEntries[256];
} Palette =
{
    0x300,
    256
};

HDC hdc = GetDC( NULL );

//  read the 20 static colors of the systempalette .
nStaticColors = GetDeviceCaps ( hdc, NUMCOLORS );
GetSystemPaletteEntries ( hdc, 0, 256, Palette.aEntries );

//  release the palettenregister in order to insert our colors in the desired
//  order into the hardware-register
SetSystemPaletteUse( hdc, SYSPAL_NOSTATIC );
SetSystemPaletteUse( hdc, SYSPAL_STATIC );

//  reset the 'peFlags' of the lower static colors to 0
for (i=0; i < 10; i++)
        Palette.aEntries[i].peFlags = 0;

//  insert the colors of the given colortable
for (i=10; i < 246; i++)
{
    Palette.aEntries[i].peRed   = aRGB[i].rgbRed;
    Palette.aEntries[i].peGreen = aRGB[i].rgbGreen;
    Palette.aEntries[i].peBlue  = aRGB[i].rgbBlue;

    Palette.aEntries[i].peFlags = PC_NOCOLLAPSE;
}

//  reset the 'peFlags' of the upper static colors to 0
for (i=246; i<256; i++)
        Palette.aEntries[i].peFlags = 0;

//  release the DC
ReleaseDC (NULL, hdc);

//  return palette
return CreatePalette( (LOGPALETTE *)&Palette );
}

任何人都可以想知道我怎么能用这个函数和我有的信息逐一得到每个像素的RGB值,并把它保存在一个数组中?

0 个答案:

没有答案