dave2k
February 27th, 2006, 10:01 AM
i have two images which are 4 bytes per pixel. i need to compare them to make sure that if the colour black, i.e. oxff,oxff,oxff occurs in either image, it occurs in the other image and the same coorindates. so far i am looping through using memcmp, and my code i below. But i have heard that because each pixel is a 32 bit value, i could use a simple int comparison. How could i change my code below to do this? And which method is quicker: mine or the nit comparison? Thanks.
bool IsBlack(BYTE* byt)
{
BYTE black[] = {0x00,0x00,0x00};
return memcmp(black, byt, 3) == 0;
} // 1st image
HANDLE hBm1 = ::LoadImage(0, "C:\\Documents and Settings\\ftwyman.MARINEDATA\\Desktop\\1.bmp",
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
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 );
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);
for(int i=0; i < size1; i+=4)
{
if(IsBlack(&bytes1[i]) ^ IsBlack(&bytes2[i]))
{
break;
}
}
if(i == size1)
std::cout << "matching!";
bool IsBlack(BYTE* byt)
{
BYTE black[] = {0x00,0x00,0x00};
return memcmp(black, byt, 3) == 0;
} // 1st image
HANDLE hBm1 = ::LoadImage(0, "C:\\Documents and Settings\\ftwyman.MARINEDATA\\Desktop\\1.bmp",
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
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 );
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);
for(int i=0; i < size1; i+=4)
{
if(IsBlack(&bytes1[i]) ^ IsBlack(&bytes2[i]))
{
break;
}
}
if(i == size1)
std::cout << "matching!";