B4J Question [Solved] Bitmap comparison

jroriz

Active Member
Licensed User
Longtime User
I need to compare if 2 bitmaps are the same.
Is there an easier way to accomplish this?

B4X:
Sub ImageAreSame(bitmap1 As B4XBitmap, bitmap2 As B4XBitmap) As Boolean
    
    Dim bc1 As BitmapCreator
    bc1.Initialize(bitmap1.Width,bitmap1.Height)
    bc1.CopyPixelsFromBitmap(bitmap1)
    
    Dim bc2 As BitmapCreator
    bc2.Initialize(bitmap1.Width,bitmap2.Height)
    bc2.CopyPixelsFromBitmap(bitmap2)

    Dim x, y As Int
    Dim ARGB1, ARGB2 As ARGBColor
    
    For x = 0 To bc1.mWidth-1
        For y = 0 To bc1.mHeight-1

            bc1.GetARGB(x, y, ARGB1)
            bc1.GetARGB(x, y, ARGB2)

            If ARGB1<>ARGB2 Then Return False
        Next
    Next

    Return True
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Make sure to check the lengths first.
2. Your code is wrong. ARGB1 will never be equal to ARGB2. They are two different objects. You need to test their fields:
B4X:
If ARGB1.A <> ARGB2.A OR ARGB1.R <> ARGB2.R ... Then Return False
3. The second line in the loop should start with bc2.

It will be simpler and faster to compare the internal buffers:
B4X:
For i = 0 To bc1.mBuffer.length - 1
 If bc1.mBuffer(i) <> bc2.mBuffer(i) Then Return False
Next
 
Upvote 0
Top