# memcmp & bitmaps

This topic is 4828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i am trying to find an instance of a small bitmap in a screen grab of a window. so far i have managed to load both bitmaps into two BYTE arrays. I found some code which searches through the bitmaps and uses memcmp to see if the smaller one is present in the larger. the trouble is, i am having trouble with the memcmp function:
memcmp(ScreenBmp[tmpY],CardBmp[x],  card_line_length )
What should ScreenBmp and CardBmp represent. I have the two bitmaps in BYTE buffer arrays, so how can i get them into the format above. The code i used to get them into these arrays is below:
   HANDLE hBitMap = LoadImage(0, "img.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

BITMAP bitmap;
GetObject(hBitMap,sizeof(BITMAP),&bitmap);
int size = bitmap.bmHeight*bitmap.bmWidth*bitmap.bmBitsPixel/8;
/// cout << bitmap.bmHeight << endl;
BYTE *lpBits = new BYTE[ size ];

GetBitmapBits((HBITMAP)hBitMap,size,lpBits );

//delete []lpBits;

BITMAP bitmap2;
GetObject(hBitMap2,sizeof(BITMAP),&bitmap2);
int size2 = bitmap2.bmHeight*bitmap2.bmWidth*bitmap2.bmBitsPixel/8;
/// cout << bitmap.bmHeight << endl;
BYTE *lpBits2 = new BYTE[ size2 ];

GetBitmapBits((HBITMAP)hBitMap2,size2,lpBits2 );
cheers!

##### Share on other sites
Quote:
 Original post by scheisskopfi am trying to find an instance of a small bitmap in a screen grab of a window.so far i have managed to load both bitmaps into two BYTE arrays. I found some code which searches through the bitmaps and uses memcmp to see if the smaller one is present in the larger. the trouble is, i am having trouble with the memcmp function:memcmp(ScreenBmp[tmpY],CardBmp[x], card_line_length )What should ScreenBmp and CardBmp represent. I have the two bitmaps in BYTE buffer arrays, so how can i get them into the format above.

Well, the memcmp line you present is obviously taken from someone elses code and the question of how to map your arrays to the ones presented in the memcmp line cannot be answered without a more complete listing.
Anyway, you can probably find this answer yourself by reading the MSDN entry on memcmp.

Tom

##### Share on other sites
well i didn't want to post my complete code, but heregoes anyway (you can find the memcmp function at the bottom):

int main() {     HANDLE hBitMap = LoadImage(0, "img.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);        BITMAP bitmap;    GetObject(hBitMap,sizeof(BITMAP),&bitmap);    int size = bitmap.bmHeight*bitmap.bmWidth*bitmap.bmBitsPixel/8;    BYTE *lpBits = new BYTE[ size ];        GetBitmapBits((HBITMAP)hBitMap,size,lpBits );           HANDLE hBitMap2 = LoadImage(0, "img2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);        BITMAP bitmap2;    GetObject(hBitMap2,sizeof(BITMAP),&bitmap2);    int size2 = bitmap2.bmHeight*bitmap2.bmWidth*bitmap2.bmBitsPixel/8;      BYTE *lpBits2 = new BYTE[ size2 ];        GetBitmapBits((HBITMAP)hBitMap2,size2,lpBits2 );            int ScreenWidth = bitmap2.bmWidth;    int ScreenHeight = bitmap2.bmHeight;    int numOfCard = 1;    int cardHeight = bitmap.bmHeight;    int card_line_length = bitmap.bmWidth;            for( int i = 0 ; i < ScreenWidth; i++ ) {         for( int j = 0 ; j < ScreenHeight; j++ ) {              for ( int k= 0; k < numOfCard; k++ ) {                  int tmpY = j;                  for ( int x = 0; x < cardHeight; x++ ) {                      if ( memcmp(lpBits2[tmpY],lpBits[x],  card_line_length ) != 0 )                           break; // we didnt find macth of a line                      tmpY++; // move to next line                  }                  if ( x == cardHeight) {                      cout << "found";                       }              }         }    }    system("PAUSE");    return 0;}

As you can see, i have changed it a little to
memcmp(lpBits2[tmpY],lpBits[x],  card_line_length )

i know how memcmp works, i am having trouble with getting the bitmaps bits

##### Share on other sites
Quote:
 Original post by scheisskopfwell i didn't want to post my complete code, but heregoes anyway (you can find the memcmp function at the bottom):*** code ***As you can see, i have changed it a little to memcmp(lpBits2[tmpY],lpBits[x], card_line_length )i know how memcmp works, i am having trouble with getting the bitmaps bits

I don't know anything about BITMAP functions, so if your problem is related to the bitmap part I can't help. I may have misunderstood your initial post that your problem was with memcpy.

Anyway, I do notice two problems in your code:
- If the bmBitsPixel is different for bitmap1 and bitmap2, a comparison is impossible and your function should immediately conclude that bitmap2 is not present in bitmap1.
- The value of card_line_length in itself is correct, but when used in the memcmp (which requires the length in bytes not pixels) it should be multiplied with the bmBitsPixel/8 value, which is guaranteed to be the same value for bitmap1 and bitmap2 if you followed my first advice.

Tom

[Edited by - dimebolt on August 24, 2005 7:26:48 AM]

##### Share on other sites

i am shore both for loop part and the part which loads the images is fine, but i do not know what to put in memcpy.

the for loops loop over each pixel in the gui image, and for each, loops over the smaller images lines to see if there is a match. if there is a match for all lines in the smaller picture, then it's present in the larger gui image.

i don't know what to put into memcpy. at present i have a buffer byte array, as i was instructed. but i do not know how to use this to see if the pixels are similar.

cheers

##### Share on other sites
Quote:
 Original post by scheisskopfi don't know what to put into memcpy. at present i have a buffer byte array, as i was instructed. but i do not know how to use this to see if the pixels are similar.

That's what memcmp does for you. It will do a byte-by-byte comparison of the two buffers. If memcmp returns 0 the contents of the two lines are identical. If you follow the advice in my previous post, the parameters you provide to memcmp are probably correct and your program may just work...

Tom

Note that I wrote 'memcpy' in my previous post, but I edited it to the correct 'memcmp'...

EDIT: and I made the same mistake in this post too :)

##### Share on other sites
Quote:
 and your program may just work...

it better,it's been three days so far!

my first c++ and my first windows program though

##### Share on other sites
i followed your advice, however i am still getting the same error:

invalid types unsigned char[int]' for array subscript

this corresponds to my memcmp line!

i didn't even know that lpBits was multidimensional

##### Share on other sites
Quote:
 Original post by scheisskopfi followed your advice, however i am still getting the same error:invalid types unsigned char[int]' for array subscript this corresponds to my memcmp line!i didn't even know that lpBits was multidimensional

Ah, yes I overlooked that.

Change the memcmp to this:
memcmp((void *)(lpBits2 + i + tmpY * ScreenWidth), (void *)(lpBits + x), card_line_length * bitmap2.bmBitsPixel / 8)

That should essentially do the same as the [][] without requiring lpBits2 to be 2 dimensional.

Tom

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633270
• Total Posts
3011156
• ### Who's Online (See full list)

There are no registered users currently online

×