Pixel Colour

Started by
9 comments, last by dachande 22 years, 3 months ago
Hello, Might anyone here have some good code for reading the pixel colour in a bitmap? I simply want to compare each pixel to the colour white using an If statement. Nice and simple, but I seem to be having a lot of problems with it. My current code does not seem to work. Help would be great! Thanks, Dachande
Advertisement
If you post the code that you are using, it would be easier to see what is wrong. Without knowing a little more, it''s hard to really say exactly what the problem is.

Take Care,
Nyko
    #include <windows.h>#include <fstream.h>#include <gdiplus.h>using namespace Gdiplus;struct collision {	byte hit[1024][1024];};collision myCollision;Color pixelColor;int main() {		Bitmap myBitmap(L"test.bmp");	for (int x = 0; x < 1024; x++) {		for (int y = 0; y < 1024; y++) {				myBitmap.GetPixel(x, y, &pixelColor);			if (pixelColor.GetValue() == Color::White)				myCollision.hit[x][y] = 1;			else myCollision.hit[x][y] = 0;	   }	}	ofstream dbOut;	dbOut.open("test.dat", ios::binary);	dbOut.write((char *)(&myCollision), sizeof(myCollision));	dbOut.close();	return 0;}    


I think what I'm looking for is an alternative piece of code This is simply giving wrong output (all 0's).

Any ideas?

Dachande

Edited by - dachande on January 3, 2002 2:29:23 PM
Well, the following line would be the obvious thing to look at first?

if (pixelColor.GetValue() == Color::White)

How does GetValue() work?
And how is Color::White defined?

Edited by - Shag on January 3, 2002 2:43:56 PM
hey

i posted a question about the EXACT same problem awhile ago. finally i tried using GetPixel() as well. i have a problem understanding the use of GetValue(), however, since GetValue() only works for TGauge on my compiler... there are 3 different funtions, GetRValue(), GetGValue() and GetBValue() to retrieve the corresponding rgb intensities. and those return COLORREF, then again i''m using Win32 API. the bottom line, however, remains - i, too, always get the value 0 returned... and i know that''s wrong because i''ve tested it inside out.

i know this may not be much help, but it''s what i''m stuck with...

keep cool
Hey is that just a typo or is that ''L'' next to your string for opening the file not in your code?
Anesthesia"If you like heaven so much, GO THERE! Leave me the hell alone!"
Okay here goes:

Shag: Your right, it would. By looking at the function definition in the gdi+ Color class, you can see that GetValue() returns the argb value of the stored colour. In theory that means it should work Color::White is an enumerated colour from the same class (obviously, since it uses Color:: !) which is equal to 0xFFFFFFFF (the hex code for opaque white).

glGreenhorn: You have reminded me of the other functions to get the individual colour components. Looking at the code, using more theory, using them won''t make a difference, so I''m still looking for answers, but I plan to try it at some point anyhow.

Anesthesia: The "L" prefix converts an ASCII string to a wide character string

Thanks all... anymore?

Dachande
I changed the line of code mentioned by Shag to:

if pixelColor.ToCOLORREF() == RGB(0, 0, 0))

both functions return a COLORREF. This now outputs all 1''s... which means that the code is picking up that the pixels are the colour black (0,0,0). I used 255,255,255 and the code outputted all 0''s.

This means that perhaps the problem is to do with the bitmap loading code.

Does anyone have any better ideas? Or great bitmap loading code

Dachande
hey dechande

i don''t know if you got the code workin yet, but here''s what i did (have to give credit to a chinese site for this hehe )


HBITMAP hBMP = (HBITMAP)LoadImage(NULL, Filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);

///////

HDC hDC = CreateCompatibleDC(NULL); //<-- you didn''t do this
SelectObject(hDC, hBMP);

COLORREF rgb = (COLORREF)GetPixel(hDC, x, y);
DeleteDC(hDC);

///////

now you can use the GetR/G/BValue(...) to access the different rgb intensities and compare it to e g RGB(0, 0, 0) etc...

hope this helps... sorry - lost the link to the chinese site, it was in chinese hehe

keep cool

Thank you kindly GreenHorn I will try it!

I''m half way through making my own drawing package to do what I need this code for! Hopefully I can stop now.

Dachande

This topic is closed to new replies.

Advertisement