Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
efficient way to compare 2 images?
#46
Code: (Select All)
Const FALSE = 0
Const TRUE = Not FALSE

Type CompareType
    r1 As Long
    g1 As Long
    b1 As Long
    a1 As Long
    r2 As Long
    g2 As Long
    b2 As Long
    a2 As Long
End Type

ReDim arrCompare(-1) As CompareType
Dim As _Unsigned Long p1
Dim As _Unsigned Long p2
Dim image1&
Dim image2&
Dim total$
Dim rgb1$
Dim rgb2$
Dim r1&, g1&, b1&, a1&
Dim r2&, g2&, b2&, a2&

' INITIALIZE
Screen _NewImage(1024, 768, 32): _Dest 0: Cls , cBlack
image1& = _NewImage(100, 100, 32)
image2& = _NewImage(100, 100, 32)


_DontBlend


' READ TEST COLORS
Restore TestData
For iLoop% = 1 To 1000
    Read r1&: Read g1&: Read b1&: Read a1&
    Read r2&: Read g2&: Read b2&: Read a2&

    Print _Trim$(Str$(r1&)) + ", " + _Trim$(Str$(g1&)) + ", " + _Trim$(Str$(b1&)) + ", " + _Trim$(Str$(a1&))
    Print _Trim$(Str$(r2&)) + ", " + _Trim$(Str$(g2&)) + ", " + _Trim$(Str$(b2&)) + ", " + _Trim$(Str$(a2&))

    If r1& = -2 Then Exit For

    ReDim _Preserve arrCompare(UBound(arrCompare) + 1) As CompareType
    arrCompare(UBound(arrCompare)).r1 = r1&
    arrCompare(UBound(arrCompare)).g1 = g1&
    arrCompare(UBound(arrCompare)).b1 = b1&
    arrCompare(UBound(arrCompare)).a1 = a1&
    arrCompare(UBound(arrCompare)).r2 = r2&
    arrCompare(UBound(arrCompare)).g2 = g2&
    arrCompare(UBound(arrCompare)).b2 = b2&
    arrCompare(UBound(arrCompare)).a2 = a2&

    'print "UBOUND(arrCompare) = " + _Trim$(Str$(UBOUND(arrCompare)))
Next iLoop%

'print "LBOUND(arrCompare) = " + _Trim$(Str$(LBOUND(arrCompare)))
'print "UBOUND(arrCompare) = " + _Trim$(Str$(UBOUND(arrCompare)))
'print "Finished reading data. Press any key to begin testing." : sleep

TestData:
' -1 means no alpha channel, -2 means end of data
Data 255,0,0,-1
Data 0,0,255,-1
Data 128,128,128,-1
Data 64,64,64,-1
Data 0,255,255,-1
Data 0,254,254,-1

Data 255,0,0,255
Data 255,0,0,-1
Data 255,0,0,254
Data 255,0,0,-1
Data 255,0,0,253
Data 255,0,0,-1
Data 255,0,0,255
Data 255,0,0,254
Data 255,0,0,255
Data 255,0,0,253
Data 255,0,0,254
Data 255,0,0,253
Data 255,0,0,254
Data 255,0,0,252
Data 255,0,0,253
Data 255,0,0,252
Data 255,0,0,253
Data 255,0,0,251
Data -2,-2,-2,-2
Data -2,-2,-2,-2
Data -2,-2,-2,-2
Data -2,-2,-2,-2

' TEST WITH VARIOUS COMBINATIONS OF ALPHA VALUES AND NO ALPHA VALUES
total$ = " of " + _Trim$(Str$(UBound(arrCompare)))
For iLoop% = LBound(arrCompare) To UBound(arrCompare)
    ' GET COLOR #1
    If arrCompare(iLoop%).a1 < 0 Then
        p1 = _RGB32(arrCompare(iLoop%).r1, arrCompare(iLoop%).g1, arrCompare(iLoop%).b1)
        rgb1$ = "_RGB32(" + _Trim$(Str$(arrCompare(iLoop%).r1)) + ", " + _Trim$(Str$(arrCompare(iLoop%).g1)) + ", " + _Trim$(Str$(arrCompare(iLoop%).b1)) + ")"
    Else
        p1 = _RGB32(arrCompare(iLoop%).r1, arrCompare(iLoop%).g1, arrCompare(iLoop%).b1, arrCompare(iLoop%).a1)
        rgb1$ = "_RGB32(" + _Trim$(Str$(arrCompare(iLoop%).r1)) + ", " + _Trim$(Str$(arrCompare(iLoop%).g1)) + ", " + _Trim$(Str$(arrCompare(iLoop%).b1)) + ", " + _Trim$(Str$(arrCompare(iLoop%).a1)) + ")"
    End If
    _Dest image1&
    Line (1, 1)-(100, 100), p1, BF ' Draw a solid box
    _Source image1&
    p1 = Point(50, 50)
    r1& = _Red32(p1): g1& = _Green32(p1): b1& = _Blue32(p1): a1& = _Alpha32(p1)
    Circle (50, 50), 5

    ' GET COLOR #2
    If arrCompare(iLoop%).a2 < 0 Then
        p2 = _RGB32(arrCompare(iLoop%).r2, arrCompare(iLoop%).g2, arrCompare(iLoop%).b2)
        rgb2$ = "_RGB32(" + _Trim$(Str$(arrCompare(iLoop%).r2)) + ", " + _Trim$(Str$(arrCompare(iLoop%).g2)) + ", " + _Trim$(Str$(arrCompare(iLoop%).b2)) + ")"
    Else
        p2 = _RGB32(arrCompare(iLoop%).r2, arrCompare(iLoop%).g2, arrCompare(iLoop%).b2, arrCompare(iLoop%).a2)
        rgb2$ = "_RGB32(" + _Trim$(Str$(arrCompare(iLoop%).r2)) + ", " + _Trim$(Str$(arrCompare(iLoop%).g2)) + ", " + _Trim$(Str$(arrCompare(iLoop%).b2)) + ", " + _Trim$(Str$(arrCompare(iLoop%).a2)) + ")"
    End If
    _Dest image2&
    Line (1, 1)-(100, 100), p2, BF ' Draw a solid box
    _Source image2&
    p2 = Point(50, 50)
    r2& = _Red32(p2): g2& = _Green32(p2): b2& = _Blue32(p2): a2& = _Alpha32(p2)
    Circle (50, 50), 5

    ' SHOW RESULTS
    _Dest 0: Cls , cBlack

    Locate 2, 1: Print "Compare image alpha values, test " + _Trim$(Str$(iLoop%)) + total$

    Locate 10, 3: Print "image1&:";
    Locate 10, 28: Print "image2&:";
    _PutImage (100, 100), image1&, 0
    _PutImage (300, 100), image2&, 0

    Locate 14, 1
    Print "Comparing " + rgb1$ + _
        " _Source image1& : p1 = Point(50, 50) = " + _Trim$(Str$(p1%)) + _
        "_RED=" + _Trim$(Str$(r1&)) + ", " + "_GREEN=" + _Trim$(Str$(g1&)) + ", " + "_BLUE=" + _Trim$(Str$(b1&)) + ", " + "_ALPHA=" + _Trim$(Str$(a1&))
    Print "    with " + rgb2$ + _
        " _Source image2& : p2 = Point(50, 50) = " + _Trim$(Str$(p2%)) + _
        "_RED=" + _Trim$(Str$(r2&)) + ", " + "_GREEN=" + _Trim$(Str$(g2&)) + ", " + "_BLUE=" + _Trim$(Str$(b2&)) + ", " + "_ALPHA=" + _Trim$(Str$(a2&))
    Print

    ' comparing points
    Print "Compare images with Point(x, y):"
    If p1 = p2 Then
        Print "No difference detected: I'm a monkey's uncle."
    Else
        Print "Different: Ha!"
    End If
    Print

    ' bplus's function
    Print "Compare images with Function CompareImages&:"
    If CompareImages&(image1&, image2&) = TRUE Then
        Print "No difference detected: I'm a monkey's uncle."
    Else
        Print "Different: Ha!"
    End If
    Print

    ' bplus's function prettified by madsci
    Print "Compare images with Function CompareImagesBPlus%:"
    If CompareImagesBPlus%(image1&, image2&) = TRUE Then
        Print "No difference detected: I'm a monkey's uncle."
    Else
        Print "Different: Ha!"
    End If
    Print

    If iLoop% < UBound(arrCompare) Then Print "Press any key to continue": Sleep

    _KeyClear
Next iLoop%

' cleanup
If image1& < -1 Then _FreeImage image1&
If image2& < -1 Then _FreeImage image2&

' done
Print "That concludes our test. Press any key to exit..."
Sleep
System

'DrawRectSolid x%, y%, width%, height%, fgColor~&
Sub DrawRectSolid (iX As Integer, iY As Integer, iSizeW As Integer, iSizeH As Integer, fgColor As _Unsigned Long)
    Line (iX, iY)-(iX + (iSizeW - 1), iY + (iSizeH - 1)), fgColor, BF ' Draw a solid rectangle
End Sub ' DrawRectSolid

' /////////////////////////////////////////////////////////////////////////////
' FROM: bplus
' https://qb64phoenix.com/forum/showthread.php?tid=1151&pid=10262#pid10262
' Don't know what you are doing but:
' Point can distinguish between 2 alpha settings!

Sub TestPointAlpha
    Screen _NewImage(800, 600, 32)
    Line (100, 100)-Step(100, 100), _RGB32(255, 0, 0, 236), BF
    Line (300, 100)-Step(100, 100), _RGB32(255, 0, 0, 237), BF
    If Point(150, 150) = Point(350, 150) Then Print "I'm a monkey's uncle." Else Print "Ha!"
    Circle (150, 150), 5
    Circle (350, 150), 5
End Sub ' TestPointAlpha

' /////////////////////////////////////////////////////////////////////////////
' FROM: bplus
' https://qb64phoenix.com/forum/showthread.php?tid=1151&pid=10242#pid10242
' Well my idea didn't take so long for one compare, just blink and there's your answer:
' Not nearly as quick as memory methods though.
' b = b + ...

Function CompareImages& (handle1 As Long, handle2 As Long)
    Dim As _Unsigned Long p1
    For y = 0 To _Height(handle1) - 1
        For x = 0 To _Width(handle1) - 1
            _Source handle1
            p1 = Point(x, y)
            _Source handle2
            If Point(x, y) <> p1 Then Exit Function
        Next
    Next
    CompareImages& = -1
End Function

Function CompareImagesBPlus% (handle1 As Long, handle2 As Long)
    Dim bResult%: bResult% = TRUE
    Dim p1 As _Unsigned Long
    Dim y, x As Integer
    Dim bFinished%: bFinished% = FALSE
    For y = 0 To _Height(handle1) - 1
        For x = 0 To _Width(handle1) - 1
            _Source handle1
            p1 = Point(x, y)
            _Source handle2
            If Point(x, y) <> p1 Then
                bResult% = FALSE
                bFinished% = FALSE
                Exit For
            End If
        Next x
        If bFinished% = TRUE Then Exit For
    Next y
    CompareImagesBPlus% = bResult%
End Function ' CompareImagesBPlus%


The issue is really rather simple -- all your values that you're comparing *ARE* exactly the same.

Let me ask you guys a simple question and then you tell me what happens:  What happens when you put a 50 alpha image onto an already existing image?

Say for example, you normally have a pure black screen: 255 alpha, 0 red, 0 green, 0 blue.
Now, you decide to put a faded white color onto that screen:  50 alpha, 255 red, 255 green, 255 blue.

What is the final result that you get?  Does it just add everything together and then divide by two?   150 alpha, 150 red, 150 green, 150 blue??

Think about it....




If you don't know, and don't want to give it a try to test, I'll go ahead and give you the answer:

When two colors blend together, the end result is a color with 255 alpha.  Poll your display screen at any time you want, and check the alpha levels on it.  255.  255. 255. 255.   Blend whatever you want onto it.  255. 255. 255.   Toss full alpha at it... 255. 255. 255.  Toss zero alpha at it.... 255. 255. 255.   Toss 125 alpha at it....  255. 255. 255.  (Those 255's are your alpha levels.)

As long as blending is on, your display is always going to blend to 255 alpha.   Your alpha check is telling you that all the screen colors have the same level of alpha with your display because they do!

Don't want that blending to blend everything to 255 alpha?

_DONTBLEND
Reply


Messages In This Thread
efficient way to compare 2 images? - by madscijr - 11-18-2022, 07:51 PM
RE: efficient way to compare 2 images? - by bplus - 11-18-2022, 08:10 PM
RE: efficient way to compare 2 images? - by Pete - 11-18-2022, 08:26 PM
RE: efficient way to compare 2 images? - by Pete - 11-18-2022, 08:55 PM
RE: efficient way to compare 2 images? - by Pete - 11-18-2022, 09:30 PM
RE: efficient way to compare 2 images? - by Pete - 11-18-2022, 09:52 PM
RE: efficient way to compare 2 images? - by Pete - 11-18-2022, 11:08 PM
RE: efficient way to compare 2 images? - by bplus - 11-18-2022, 11:40 PM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 12:22 AM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 01:22 AM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 05:39 PM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 06:28 PM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 10:54 PM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 11:54 PM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 11:57 PM
RE: efficient way to compare 2 images? - by SMcNeill - 11-19-2022, 11:59 PM
RE: efficient way to compare 2 images? - by bplus - 11-20-2022, 12:02 AM
RE: efficient way to compare 2 images? - by bplus - 11-20-2022, 12:14 AM
RE: efficient way to compare 2 images? - by bplus - 11-20-2022, 12:36 AM
RE: efficient way to compare 2 images? - by bplus - 11-20-2022, 02:37 AM



Users browsing this thread: 22 Guest(s)