matching pixels

i have two images which my program loads, and if the program find a yellow pixel in either image, it test whether it's the same colour at the same position in the next image, throughout the entire image.

this checks to see whether one colour is the same in both images. to do this i have looped through using memcmp, but i am afraid my code is gonna be real slow, as i have many large images to test.

can anyone suggets things to make my code quicker?

cheers

int main(int argc, char* argv[])
{
// 1st image
HANDLE hBm1 = ::LoadImage(0, "C:\\Documents and Settings\\ftwyman.MARINEDATA\\Desktop\\1.bmp",
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION );
BITMAP bm1;
::GetObject(hBm1, sizeof(BITMAP), &bm1);
int size1 = bm1.bmHeight * bm1.bmWidth * bm1.bmBitsPixel / 8;
BYTE *bytes1 = new BYTE[size1];
::GetBitmapBits((HBITMAP)hBm1, size1, bytes1);

// 2nd image
HANDLE hBm2 = ::LoadImage(0, "C:\\Documents and Settings\\ftwyman.MARINEDATA\\Desktop\\2.bmp",
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION );
BITMAP bm2;
::GetObject(hBm2, sizeof(BITMAP), &bm2);
int size2 = bm2.bmHeight * bm2.bmWidth * bm2.bmBitsPixel / 8;
BYTE *bytes2 = new BYTE[size2];
::GetBitmapBits((HBITMAP)hBm2, size2, bytes2);

int i;
BYTE yellow[] = {0x00,0xff,0xff};

DWORD a = GetTickCount();
Sleep(100);
//for(a = 0; a <100; a++){
for(i = 0; i < size1; i+=3)
{
if(!memcmp(yellow, &bytes1[i], 3) ||
!memcmp(yellow, &bytes2[i], 3))
{
if(memcmp(&bytes1[i], &bytes2[i], 3))
{
break;
}
}
}
//}

cout << (GetTickCount()-a);

if(i == size1) cout << "matching!";
_getch();
return 0;
}
[2074 byte] By [dave2k] at [2007-11-19 18:29:39]
# 1 Re: matching pixels
can anyone suggets things to make my code quicker?

First, remove the Sleep() :)
Then, in a loop, if a point in first bitmap is yellow and a point in second bitmap is yellow, you don't need third memcmp to compare them - they are equal.
If you use LR_CREATEDIBSECTION flag, you do not need to call GetBitmapBits() - they already are in the BITMAP struct. However, you get color depth of the bmp image that you load. If you use LR_VGACOLOR, your bitmap will be converted to 32 bits per pixel, and you won't need memcmp at all - just use int compare. Also, performance might increase because everything will be 32-bits aligned.
VladimirF at 2007-11-9 13:19:53 >
# 2 Re: matching pixels
if a point in first bitmap is yellow and a point in second bitmap is yellow, you don't need third memcmp to compare them - they are equal.How can i test for this? The problem is i can test using if a is yellow and b is yellow keep searching but it also has to keep searching if both aren't yellow.
However, you get color depth of the bmp image that you load. I need this to see how many bytes there are to a pixel.

In the end i want to compare a bitmap against a pixel array created from the DC of a window.

what would be the easiest way to get the bits into the same format?
dave2k at 2007-11-9 13:21:04 >
# 3 Re: matching pixels
How can i test for this? The problem is i can test using if a is yellow and b is yellow keep searching but it also has to keep searching if both aren't yellow.
I would write a function that tests for yellow color. like:
bool IsYellow(BYTE*);
then you could call it for pixels from both of your images and use XOR (exclusive OR) operator to see if only one of them is yellow:
if( IsYellow(p1) ^ IsYellow(p2))
break;
...I need this to see how many bytes there are to a pixel.

In the end i want to compare a bitmap against a pixel array created from the DC of a window.

what would be the easiest way to get the bits into the same format?
What is your screen's color resolution? 24 bits? 32? Actually, if you provide different IsYellow() functions for your screen and bitmap image, you don't even have to have the same color depth.
VladimirF at 2007-11-9 13:22:04 >