Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How can I compare .bmp images ?

zivlakszivlaks Member Posts: 3
I'm making a 3x4 piece puzzle game and I need to compare images to see if the player completed the game! Can you tell me how to do that please ?They are in .bmp format I tried

[code]if(image1->Picture==image2->Picture)ShowMessage(" YOU DID IT ! ");

[/code]

But it doesn't work :(

Comments

  • JohanDewanckerJohanDewancker Member Posts: 1
    : I'm making a 3x4 piece puzzle game and I need to compare images to
    : see if the player completed the game! Can you tell me how to do that
    : please ?They are in .bmp format I tried
    :
    : [code]: if(image1->Picture==image2->Picture)ShowMessage(" YOU DID IT ! ");
    :
    : [/code]:
    :
    : But it doesn't work :(
    :
    The property Picture is probably only a reference to the bitmap. To compare the two images, it's necessary to create a compare function. In this function it's possible to compare the bitmaps pixel by pixel. However, it's possible that this will be slow.
  • zivlakszivlaks Member Posts: 3
    can you please post an example please ?
  • LundinLundin Member Posts: 3,711
    Since you have both bitmaps in memory already, to simply run a memcmp() on the raw data should do the trick. This will be very efficient as memcmp() will start with the bitmap header and sort out most of the cases early on.

    However, I'm sure there is some method to compare them in Builder. Look at the methods of the image class and see what they have.
  • tojoheretojohere Member Posts: 4
    : can you please post an example please ?
    :

    //Load picture in first image box.
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    TOpenDialog *fdlg = new TOpenDialog(this);
    fdlg->Filter = "Bitmap Files | *.bmp";
    if( fdlg->Execute() )
    {
    Image1->Picture->LoadFromFile(fdlg->FileName);
    }
    delete fdlg;
    }
    //---------------------------------------------------------------------------

    //Load picture in second image box.
    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
    TOpenDialog *fdlg = new TOpenDialog(this);
    fdlg->Filter = "Bitmap Files | *.bmp";
    if( fdlg->Execute() )
    {
    Image2->Picture->LoadFromFile(fdlg->FileName);
    }
    delete fdlg;
    }
    //---------------------------------------------------------------------------

    //Compare two bitmaps and return true if both are same else return false.
    bool TForm1::CompareBitmaps(Graphics::TBitmap* bmp1, Graphics::TBitmap* bmp2)
    {
    if( bmp1->Height != bmp2->Height || bmp1->Width != bmp2->Width ||
    bmp1->PixelFormat != bmp2->PixelFormat )
    {
    return false;
    }
    int bitsPerPix = 0;
    switch(bmp1->PixelFormat)
    {
    case pf1bit:
    bitsPerPix = 1;
    break;
    case pf4bit:
    bitsPerPix = 4;
    break;
    case pf8bit:
    bitsPerPix = 8;
    break;
    case pf15bit:
    bitsPerPix = 15;
    break;
    case pf16bit:
    bitsPerPix = 16;
    break;
    case pf24bit:
    bitsPerPix = 24;
    break;
    case pf32bit:
    bitsPerPix = 32;
    break;

    }
    int scanLinelength = bmp1->Width * bitsPerPix / 8;
    for(int i =0; i < bmp1->Height ; i++ )
    {
    if( memcmp(bmp1->ScanLine[i], bmp2->ScanLine[i],scanLinelength) != 0)
    {
    return false;
    }
    }
    return true;
    }
    //Compare button click handler
    void __fastcall TForm1::BitBtn1Click(TObject *Sender)
    {
    //Display the comaprison result ina label box.
    Label1->Caption = (CompareBitmaps(Image1->Picture->Bitmap,
    Image2->Picture->Bitmap )?
    "Equal" : "Different");
    }
    //---------------------------------------------------------------------------
    Hope this helps...I am anew bye in C++ builder
    Donno there exist some other easy method.
    Thanks,
Sign In or Register to comment.