A propos de GetDiGiBits pour remplacer GetPixel

admax74 Messages postés 3 Date d'inscription mardi 18 novembre 2008 Statut Membre Dernière intervention 20 novembre 2008 - 19 nov. 2008 à 07:47
nanonavich Messages postés 54 Date d'inscription mardi 25 mars 2008 Statut Membre Dernière intervention 15 août 2015 - 19 nov. 2008 à 18:17
Bonjour!

Tout d'abord je suis débutant en c/c++ : je m'y suis mis la semaine dernière.
j'utilise le programme CodeBlocks.

Je souhaite dans mon programme console tout simple chercher sur l'écran un pixel de couleur donnée.
J'ai bien entendu commencé par utiliser GetPixel, mais si le pixel cherché est tout en bas
à droite de l'écran, le programme va utiliser un grand nombre de fois l'instruction GetPixel
et il se passe bien au moins 5secondes avant de trouver le pixel de couleur donnée,
ce qui n'est pas acceptable pour moi!

Donc j'ai lu les sujets à propos de l'utilisation de GetDiGiBits qui semble extremement plus rapide,
mais en contrepartie son utilisation me semble plus délicate! Voilà le code très inspiré de certains codes
qu'on peut trouver sur ce forum, mais dans la mesure où je veux travailler directement avec l'écran, il m'a semblé
devoir écrire ce code ci dessous....J'ai tenté d'isoler et d'utiliser simplement GetDiGiBits ...

Je ne comprends pas chaque ligne, et le fait est que ce code renvoie toujours la même couleur de pixel, quel que soit
le pixel que je teste!!! ce qui est gênant n'est ce pas!!! Comment dois je modifier ce code pour qu'il me renvoie bien
la couleur du pixel que je teste?!

Un énorme merci à tous ceux qui peuvent m'aider,

CODE:

#include <stdio.h>
#include <stdlib.h>
#include "windows.h"
#include "Wingdi.h"

int main()
{

HDC screendc = GetDC(HWND_DESKTOP);
HDC dc = CreateCompatibleDC(screendc);
int ScreenSizeX = GetDeviceCaps(screendc, HORZRES);
int ScreenSizeY = GetDeviceCaps(screendc, VERTRES);
int count = ScreenSizeX * ScreenSizeY * 3;
BYTE * ScreenBuffer=(BYTE *)malloc(count);
BITMAPINFO BitmapInfo;
HBITMAP Bitmap;

memset(&BitmapInfo,sizeof(BitmapInfo),0);
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biWidth = ScreenSizeX;
BitmapInfo.bmiHeader.biHeight = ScreenSizeY;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 24;
BitmapInfo.bmiHeader.biCompression = BI_RGB;

Bitmap = CreateCompatibleBitmap(screendc, ScreenSizeX, ScreenSizeY);
SelectObject(dc, Bitmap);

BitBlt(dc, 0, 0, ScreenSizeX, ScreenSizeY, screendc, 0, 0, SRCCOPY);
GetDIBits(dc,Bitmap,0,ScreenSizeY,ScreenBuffer,&BitmapInfo,DIB_RGB_COLORS);

int i=0;
int start=0;
int end=10 ;

for(i=start;i<end;i++)
{
    int R=ScreenBuffer[3*i];
    int G=ScreenBuffer[(3*i)+1];
    int B=ScreenBuffer[(3*i)+2];
    printf("i=%i et R=%i et G=%i et B=%i \n",i,R,G,B);
    system("PAUSE");
}

ReleaseDC(0,dc);
ReleaseDC(0,screendc);
DeleteObject(Bitmap);
free(ScreenBuffer);

return(0);
}

2 réponses

admax74 Messages postés 3 Date d'inscription mardi 18 novembre 2008 Statut Membre Dernière intervention 20 novembre 2008
19 nov. 2008 à 12:13
PS: au fait ce n'est pas GetDiGiBits mais plutot GetDIBits,

par contre je n'ai pas vu où l'on peut éditer/modifier le titre de mon message précédent !
0
nanonavich Messages postés 54 Date d'inscription mardi 25 mars 2008 Statut Membre Dernière intervention 15 août 2015
19 nov. 2008 à 18:17
en moin vite

COLORREF couleur;

couleur= GetPixel(dc,100,100);

printf("i=%i et R=%i et G=%i et B=%i \n", couleur
.r,couleur.g,couleur.b);


sinon va falloir que tu fais plus de tweaking

BITMAP bm;

    
    HDC hDC;
   
    
    WORD biBitCount;
   
 
    int nColorTableEntries;

 
    if(!hBitmap){
        return FALSE;
    }

   
 
    Empty();
   
 
    if(!GetObject(hBitmap,sizeof(BITMAP),(LPBYTE)&bm)){
        return FALSE;
    }

    
    biBitCount =bm.bmPlanes*bm.bmBitsPixel;
    if(biBitCount<=1)
        biBitCount=1;
    else if(biBitCount<=4)
        biBitCount=4;
    else if(biBitCount<=8)
        biBitCount=8;
    else
        biBitCount=24;

 
    switch(biBitCount) {
        case 1:
            nColorTableEntries = 2;
            break;
        case 4:
            nColorTableEntries = 16;
            break;
        case 8:
            nColorTableEntries = 256;
            break;
        case 16:
        case 24:
        case 32:
            nColorTableEntries = 0;
            break;
        default:
            ASSERT(FALSE);
    }   
    ASSERT((nColorTableEntries >= 0) && (nColorTableEntries <= 256));
    m_nColorTableEntries = nColorTableEntries;

 
    m_lpBMIH = (LPBITMAPINFOHEADER) new char
        [sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * nColorTableEntries];    m_nBmihAlloc m_nImageAlloc crtAlloc;

    m_lpBMIH->biSize = sizeof(BITMAPINFOHEADER);
    m_lpBMIH->biWidth = bm.bmWidth;
    m_lpBMIH->biHeight = bm.bmHeight;
    m_lpBMIH->biPlanes = 1;
    m_lpBMIH->biBitCount = biBitCount;
    m_lpBMIH->biCompression = BI_RGB;
    m_lpBMIH->biSizeImage = 0;
    m_lpBMIH->biXPelsPerMeter = 0;
    m_lpBMIH->biYPelsPerMeter = 0;
    m_lpBMIH->biClrUsed = nColorTableEntries;
    m_lpBMIH->biClrImportant = nColorTableEntries;   

 
    hDC=GetDC(NULL);
      
 
    if(hPal==NULL){
        hPal = GetSystemPalette();
    }
    hPal = SelectPalette(hDC, hPal, FALSE);
    RealizePalette(hDC);
 

 
    GetDIBits( hDC, hBitmap, 0, (UINT)m_lpBMIH->biHeight, NULL, (LPBITMAPINFO)m_lpBMIH, DIB_RGB_COLORS);
 
    if( m_lpBMIH->biSizeImage == 0 ){
        m_lpBMIH->biSizeImage=(((bm.bmWidth*biBitCount) + 31) / 32 * 4)*bm.bmHeight;
    }

  
    m_lpImage = (LPBYTE) new char[m_lpBMIH->biSizeImage];
 
    if( GetDIBits( hDC, hBitmap, 0, (UINT)m_lpBMIH->biHeight, (LPBYTE)m_lpImage,
        (LPBITMAPINFO)m_lpBMIH, DIB_RGB_COLORS) == 0 ){
        //clean up and return NULL
        Empty();

        SelectPalette( hDC, hPal, TRUE );
        RealizePalette( hDC );
        ReleaseDC( NULL, hDC );
       
        return FALSE;
    }

 
    SelectPalette(hDC, hPal, TRUE);
    RealizePalette(hDC);
    ReleaseDC(NULL, hDC);

GetDibSaveDim()
{
    CSize sizeSaveDim;
    sizeSaveDim.cx    = ( m_lpBMIH->biWidth * m_lpBMIH->biBitCount + 31)/32 * 4;
    sizeSaveDim.cy    = m_lpBMIH->biHeight;
    return sizeSaveDim;

}

 
LONG  GetPixelOffset(int  x, int y)
{
    CSize sizeSaveDim;
    sizeSaveDim = GetDibSaveDim();

    LONG lOffset = (LONG) (sizeSaveDim.cy - y - 1) * sizeSaveDim.cx +
        x  / (8 / m_lpBMIH->biBitCount);
    return lOffset;
}

 
RGBQUAD  GetPixel(int x, int y)
{
 
    RGBQUAD cColor;
 
    switch (m_lpBMIH->biBitCount)
    {
        case 1 :
            if (1<<(7-x%8) & *(LPBYTE)(m_lpImage+GetPixelOffset(x, y)))
            {           
                cColor.rgbBlue  = 255;
                cColor.rgbGreen = 255;
                cColor.rgbRed   = 255;
                cColor.rgbReserved =0;
            }
            else
            {           
                cColor.rgbBlue  = 0;
                cColor.rgbGreen = 0;
                cColor.rgbRed   = 0;   
                cColor.rgbReserved =0;
            }
            break;
        case 4 :   
            {
                int nIndex = (*(LPBYTE)(m_lpImage+GetPixelOffset(x, y)) &
                               (x%2 ? 0x0f : 0xf0)) >> (x%2 ? 0 : 4);
                LPRGBQUAD pDibQuad = (LPRGBQUAD) (m_lpvColorTable) + nIndex;
                cColor.rgbBlue  = pDibQuad->rgbBlue;
                cColor.rgbGreen = pDibQuad->rgbGreen;
                cColor.rgbRed   = pDibQuad->rgbRed;
                cColor.rgbReserved =0;
            }
                    break;
        case 8 :   
            {
                int nIndex = *(BYTE*)(m_lpImage+GetPixelOffset(x, y));
                LPRGBQUAD pDibQuad = (LPRGBQUAD) (m_lpvColorTable) + nIndex;
                cColor.rgbBlue  = pDibQuad->rgbBlue;
                cColor.rgbGreen = pDibQuad->rgbGreen;
                cColor.rgbRed   = pDibQuad->rgbRed;
                cColor.rgbReserved =0;
            }
                    break;
        default:
                int nIndex = *(BYTE*)(m_lpImage+GetPixelOffset(x, y));                   
                cColor.rgbRed   = m_lpImage[nIndex];
                cColor.rgbGreen = m_lpImage[nIndex + 1];
                cColor.rgbBlue  = m_lpImage[nIndex + 2];
                cColor.rgbReserved =0;   
                break;
    }
 
    return cColor;
}
0
Rejoignez-nous