GetPixel function

This is a discussion on GetPixel function within the C++ Programming forums, part of the General Programming Boards category; ok so if i wanted to check if a pixel is white, what would i have to do? I dont ...

  1. #16
    Registered User
    Join Date
    Jul 2007
    Posts
    17
    ok so if i wanted to check if a pixel is white, what would i have to do? I dont understand the numbers GetPixel returns. Like "13026246" - its not enough numbers to be an RGB value and "4294967295" - thats to many numbers to be an RGB value. I really don't get it lol
    Last edited by Muzzy A; 07-04-2007 at 03:41 AM.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well if you looked at the link Indigo posted, it shows you how to interpret a COLORREF. The lower eight bits are for red, the next eight for green, and the next eight are blue. The last eight are zero. So one way to break up the parts would be to use bit shifting. Once you do this, each part (red, green, and blue) will be a value from 0 to 255.
    Code:
    COLORREF color = GetPixel(hdc, x, y);
    unsigned int r = color & 0xff;
    unsigned int g = (color & 0xff00) >> 8;
    unsigned int b = (color & 0xff0000) >> 16;
    
    cout << "red: " << r << endl;
    cout << "green: " << g << endl;
    cout << "blue: " << b << endl;
    I think there are also some window macros which will break up a COLORREF. Or you could also use the bitset class from the standard library to make things easier.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >ok so if i wanted to check if a pixel is white, what would i have to do?
    White would be a 255 for all three components (red=255, green=255, blue=255). Red of course would be (red=255, green=0, blue=0). So to check for white using the above code:
    Code:
    if (r == 255 && g == 255 && b == 255)
    {
        //Pixel is white
    }
    Or you could also write:
    Code:
    if (color == 0xffffff)
    {
        //Pixel is white
    }

  4. #19
    Registered User
    Join Date
    Jul 2007
    Posts
    17
    Code:
    0xffffff
    it doesnt work, neither does
    Code:
    0x000000
    for black

    and for some reason r g b are all getting the value 255 every time no matter what.

    do you know what could cause this?

  5. #20
    Registered User
    Join Date
    Jul 2007
    Posts
    17
    [EDIT] sorry for the double post

  6. #21
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,678
    COLORREF is a waste of finger time, just use DWORD. (It's the same thing. )

    Try this instead: It may mork and may not, so hey...
    Code:
    DWORD color = GetPixel(hdc, x, y);
    unsigned int r = GetRValue(color);
    unsigned int g = GetGValue(color);
    unsigned int b = GetBValue(color);
    
    cout << "red: " << r << endl;
    cout << "green: " << g << endl;
    cout << "blue: " << b << endl;

  7. #22
    Registered User
    Join Date
    Jul 2007
    Posts
    17
    its the same thing with that to :S maybe if you all look at my code something may catch your eye that im doing wrong lol.

    Code:
    int x,y,SX=347,SY=375;
     	unsigned char pixels[30][100];
     	unsigned int r,g,b;
     	HDC hdc = GetDC(NULL);
     	DWORD color=GetPixel(hdc,x,y);
    
     	for(y=SY;y<SY+30;y++)
     	for(x=SX;x<SX+100;x++)
     	{
     	color;
     	r=GetRValue(color);
     	g=GetGValue(color);
     	b=GetBValue(color);
     	if(r==255 && g==255 && b==255)
    	 		  cout << "white ";
        else if(r==0 && g==0 && b==0)
     		      cout << "black ";
        }

  8. #23
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,678
    Hmm, what's this?
    Code:
    int x,y,SX=347,SY=375;
     	unsigned char pixels[30][100];
     	unsigned int r,g,b;
     	HDC hdc = GetDC(NULL);
     	DWORD color=GetPixel(hdc,x,y);
     	Sleep(2000);
     	for(y=SY;y<SY+30;y++)
     	for(x=SX;x<SX+100;x++)
     	{
     	color;
     	r=GetRValue(color);
     	g=GetGValue(color);
     	b=GetBValue(color);
     	if(r==255 && g==255 && b==255)
    	 		  cout << "white ";
        else if(r==0 && g==0 && b==0)
     		      cout << "black ";
        }

  9. #24
    Registered User
    Join Date
    Jul 2007
    Posts
    17
    that does the GetPixel() function doesnt it?

  10. #25
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,678
    No, this does:
    Code:
    int x,y,SX=347,SY=375;
     	unsigned char pixels[30][100];
     	unsigned int r,g,b;
     	HDC hdc = GetDC(NULL);
     	DWORD color=GetPixel(hdc,x,y);
     	Sleep(2000);
     	for(y=SY;y<SY+30;y++)
     	for(x=SX;x<SX+100;x++)
     	{
     	color;
     	r=GetRValue(color);
     	g=GetGValue(color);
     	b=GetBValue(color);
     	if(r==255 && g==255 && b==255)
    	 		  cout << "white ";
        else if(r==0 && g==0 && b==0)
     		      cout << "black ";
        }
    You both create and assign color on the same line.

  11. #26
    Registered User
    Join Date
    Jul 2007
    Posts
    17
    ah ok i see your point, i'll give it a shot.

  12. #27
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,678
    What are you trying to get an image of anyway? If it's another window then you need to set your hdc to it's hdc, and start getting the pixels at 0x0.

  13. #28
    Registered User
    Join Date
    Jul 2007
    Posts
    17
    i fixed it here's what ive got, and ya im getting the pixel values of an image in a browser

    Code:
    int x,y,pixel=-1,wpix,offset;
     	unsigned char pixels[30][100];
     	unsigned int r,g,b;
     	HDC hdc = GetDC(NULL);
     	DWORD color;
     	Sleep(3000);
     	for(y=SY;y<SY+30;y++)
     						 for(x=SX;x<SX+100;x++)
     						 {
        					  pixel=pixel+1;
     						  color=GetPixel(hdc,x,y);
     						  r=GetRValue(color);
     						  g=GetGValue(color);
     						  b=GetBValue(color);
     						  if(r==255 && g==255 && b==255)
     						   // white pixel

  14. #29
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Yarin View Post
    COLORREF is a waste of finger time, just use DWORD. (It's the same thing. )
    Until the definition of DWORD or COLORREF changes.

  15. #30
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,678
    Code:
    pixel=pixel+1;
    can be
    Code:
    pixel += 1;

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21