Доступ к RGB каждого пикселя в растровом изображении с помощью Win32 - PullRequest
2 голосов
/ 08 марта 2012

У меня есть демонстрационная программа, написанная на обычном C win32. Программа использует фрейм-граббер для захвата изображений с камеры и сохранения их в буфере основной памяти (изображения имеют 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 каждого пикселя по одному и сохранить его в массиве?

...