Jump to content

Problems comparing two (in memory) bitmaps using memcmp


DHL
 Share

Recommended Posts

Hi, 

I'm trying to compare two bitmaps (screenshots of the desktop) that I create using BitBlt. The bitmaps can be successfully saved to disk, but when I run the two bitmaps through a CompareBitmaps()-function I found in this thread, which calls memcmp, the script crashes with "Exit code: -1073741819". 

This crash only happens when I try to compare bitmaps that I got by using BitBlt. If I use the CompareBitmaps()-function on bitmaps created by _GDIPlus_ImageLoadFromFile, everything works fine. (I'm using AutoIT 3.3.6.1)

I'll attach my script that crashes, and the  CompareBitmaps() example script that works (which I got from the thread previosly mentioned)

My script, that for some reason crashes : 

#include <ScreenCapture.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>

_GDIPlus_Startup()

Local $hDesktop = _WinAPI_GetDesktopWindow()
$clientS = WinGetClientSize($hDesktop)
Local $Width = $clientS[0]
Local $Height = $clientS[1]

Local $hDC = _WinAPI_GetDC($hDesktop)
Local $memDC = _WinAPI_CreateCompatibleDC($hDC)
Local $memBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)
_WinAPI_SelectObject ($memDC, $memBmp)
_WinAPI_BitBlt($memDC, 0, 0, $Width, $Height, $hDC, 0,0, $__SCREENCAPTURECONSTANT_SRCCOPY)
_WinAPI_DeleteDC($memDC)
_WinAPI_ReleaseDC($hDesktop, $hDC)

Local $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($memBmp)
_WinAPI_DeleteObject($memBmp)

_GDIPlus_ImageSaveToFile($hImage1, @ScriptDir & "\GDIPlus_Image1.jpg")

MsgBox(0, "Debug", "Screenshot taken & saved to disk. Ready to take new screenshot" )

Local $hDC2 = _WinAPI_GetDC($hDesktop)
Local $memDC2 = _WinAPI_CreateCompatibleDC($hDC2)
Local $memBmp2 = _WinAPI_CreateCompatibleBitmap($hDC2, $Width, $Height)
_WinAPI_SelectObject ($memDC2, $memBmp2)
_WinAPI_BitBlt($memDC2, 0, 0, $Width, $Height, $hDC2, 0,0, $__SCREENCAPTURECONSTANT_SRCCOPY)
_WinAPI_DeleteDC($memDC2)
_WinAPI_ReleaseDC($hDesktop, $hDC2)

Local $hImage2 =_GDIPlus_BitmapCreateFromHBITMAP($memBmp2)
_WinAPI_DeleteObject($memBmp2)

_GDIPlus_ImageSaveToFile($hImage2, @ScriptDir & "\GDIPlus_Image2.jpg")

MsgBox(0, "Debug", "Screenshot2 taken & saved to disk. Ready to compare images (from memory)" )

MsgBox(0, "bm1==bm2", CompareBitmaps($hImage1, $hImage2) )

; Clean up resources
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)

; Shut down GDI+ library
_GDIPlus_ShutDown ()

Func CompareBitmaps($bm1, $bm2)

    $Bm1W = _GDIPlus_ImageGetWidth($bm1)
    $Bm1H = _GDIPlus_ImageGetHeight($bm1)

    $BitmapData1 = _GDIPlus_BitmapLockBits($bm1, 0, 0, $Bm1W, $Bm1H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    $Stride = DllStructGetData($BitmapData1, "Stride")
    $Scan0 = DllStructGetData($BitmapData1, "Scan0")

    $ptr1 = $Scan0
    $size1 = ($Bm1H - 1) * $Stride + ($Bm1W - 1) * 4
    $Bm2W = _GDIPlus_ImageGetWidth($bm2)
    $Bm2H = _GDIPlus_ImageGetHeight($bm2)
    $BitmapData2 = _GDIPlus_BitmapLockBits($bm2, 0, 0, $Bm2W, $Bm2H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    $Stride = DllStructGetData($BitmapData2, "Stride")
    $Scan0 = DllStructGetData($BitmapData2, "Scan0")
    $ptr2 = $Scan0
    $size2 = ($Bm2H - 1) * $Stride + ($Bm2W - 1) * 4

    $smallest = $size1
    If $size2 < $smallest Then $smallest = $size2
    $call = DllCall("msvcrt.dll", "int:cdecl", "memcmp", "ptr", $ptr1, "ptr", $ptr2, "int", $smallest)

    _GDIPlus_BitmapUnlockBits($bm1, $BitmapData1)
    _GDIPlus_BitmapUnlockBits($bm2, $BitmapData2)
    Return ($call[0]=0)
EndFunc  ;==>CompareBitmaps

 

Example Script (that basically does the same thing, but works fine)

#include <GDIPlus.au3>

_GDIPlus_Startup()
$fname1=FileOpenDialog("First image","","All images(*.bmp;*.jpg;*.png;)")
If $fname1="" Then Exit
$fname2=FileOpenDialog("Second image image","","All images(*.bmp;*.jpg;*.png;)")
If $fname2="" Then Exit
$bm1 = _GDIPlus_ImageLoadFromFile($fname1)
$bm2 = _GDIPlus_ImageLoadFromFile($fname2)

MsgBox(0, "bm1==bm2", CompareBitmaps($bm1, $bm2))
_GDIPlus_ImageDispose($bm1)
_GDIPlus_ImageDispose($bm2)
_GDIPlus_Shutdown()

Func CompareBitmaps($bm1, $bm2)

    $Bm1W = _GDIPlus_ImageGetWidth($bm1)
    $Bm1H = _GDIPlus_ImageGetHeight($bm1)
    $BitmapData1 = _GDIPlus_BitmapLockBits($bm1, 0, 0, $Bm1W, $Bm1H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    $Stride = DllStructGetData($BitmapData1, "Stride")
    $Scan0 = DllStructGetData($BitmapData1, "Scan0")

    $ptr1 = $Scan0
    $size1 = ($Bm1H - 1) * $Stride + ($Bm1W - 1) * 4


    $Bm2W = _GDIPlus_ImageGetWidth($bm2)
    $Bm2H = _GDIPlus_ImageGetHeight($bm2)
    $BitmapData2 = _GDIPlus_BitmapLockBits($bm2, 0, 0, $Bm2W, $Bm2H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    $Stride = DllStructGetData($BitmapData2, "Stride")
    $Scan0 = DllStructGetData($BitmapData2, "Scan0")

    $ptr2 = $Scan0
    $size2 = ($Bm2H - 1) * $Stride + ($Bm2W - 1) * 4

    $smallest = $size1
    If $size2 < $smallest Then $smallest = $size2
    $call = DllCall("msvcrt.dll", "int:cdecl", "memcmp", "ptr", $ptr1, "ptr", $ptr2, "int", $smallest)



    _GDIPlus_BitmapUnlockBits($bm1, $BitmapData1)
    _GDIPlus_BitmapUnlockBits($bm2, $BitmapData2)

    Return ($call[0]=0)


EndFunc  ;==>CompareBitmaps

 

Any one got a clue why my script crashes? 

Link to comment
Share on other sites

Trying a little bump. 

I still haven't figured out why the second script works fine and the first doesn't.

They both call 

DllCall("msvcrt.dll", "int:cdecl", "memcmp", "ptr", $ptr1, "ptr", $ptr2, "int", $smallest)

The main difference is that the second script get the bitmap-handles from

$bm1 = _GDIPlus_ImageLoadFromFile($fname1)

while the first (which crashes) acquires the bitmap-handles using BitBlt and these lines:

Local $hDC = _WinAPI_GetDC($hDesktop)
Local $memDC = _WinAPI_CreateCompatibleDC($hDC)
Local $memBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)
_WinAPI_SelectObject ($memDC, $memBmp)
_WinAPI_BitBlt($memDC, 0, 0, $Width, $Height, $hDC, 0,0, $__SCREENCAPTURECONSTANT_SRCCOPY)
_WinAPI_DeleteDC($memDC)
_WinAPI_ReleaseDC($hDesktop, $hDC)

Local $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($memBmp)

Anyone who can point me in the right direction on how to solve this problem? 

Link to comment
Share on other sites

Yeah, I agree it must be crashing because of some type mismatch.

However, shouldn't 

_GDIPlus_BitmapCreateFromHBITMAP($memBmp)

and 

_GDIPlus_ImageLoadFromFile($fname1)

both return a handle to a bitmap object ?

It seems so by the docs. What am I missing?

https://www.autoitscript.com/autoit3/docs/libfunctions/_GDIPlus_BitmapCreateFromHBITMAP.htm

https://www.autoitscript.com/autoit3/docs/libfunctions/_GDIPlus_ImageLoadFromFile.htm

Link to comment
Share on other sites

As doc states one returns a bitmap object and the other an image object. So not both returning bitmap object as you state.

Bitmap class inherits from image I think. Maybe this function can convert.

https://msdn.microsoft.com/en-us/library/dd183485(VS.85).aspx

Link to comment
Share on other sites

Its a while ago but maybe this one helps

https://www.autoitscript.com/autoit3/docs/libfunctions/_GDIPlus_BitmapCreateFromMemory.htm

and long ago I made this one

 

in that reference you see

If $BMPFile="SCREEN" Then
        $hbScreen=_ScreenCapture_Capture("",0,0,-1,-1,False)
        $pBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hbScreen); returns memory bitmap
    Else
;try to get a handle
        $handle = WinGetHandle($BMPFile)
        If @error Then
;Assume its an unknown handle so correct filename should be given
            $pBitmap = _GDIPlus_BitmapCreateFromFile($BMPFile)
        Else
            $hbScreen=_ScreenCapture_CaptureWnd("",$handle,0,0,-1,-1,False)
            $pBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hbScreen); returns memory bitmap
        EndIf
    EndIf

 

Edited by junkew
Link to comment
Share on other sites

Try this function instead:

Func _GDIPlus_ImageCompare($hImage1, $hImage2, $bFastCmp = True)
    Local Const $iW = _GDIPlus_ImageGetWidth($hImage1), $iH = _GDIPlus_ImageGetHeight($hImage1)
    If ($iW <> _GDIPlus_ImageGetWidth($hImage2)) Then Return SetError(1, 0, 0)
    If ($iH <> _GDIPlus_ImageGetHeight($hImage2)) Then Return SetError(2, 0, 0)
    Local $t = TimerInit()
    Local $tBitmapData1 = _GDIPlus_BitmapLockBits($hImage1, 0, 0, $iW, $iH, $GDIP_ILMREAD, $GDIP_PXF32ARGB)
    Local $tBitmapData2 = _GDIPlus_BitmapLockBits($hImage2, 0, 0, $iW, $iH, $GDIP_ILMREAD, $GDIP_PXF32ARGB)

    Local $pScan1 = DllStructGetData($tBitmapData1, "Scan0")
    Local $tPixel1 = DllStructCreate("uint[" & $iW * $iH & "];", $pScan1)
    Local $iStride = Abs(DllStructGetData($tBitmapData1, "Stride"))

    Local $pScan2 = DllStructGetData($tBitmapData2, "Scan0")
    Local $tPixel2 = DllStructCreate("uint[" & $iW * $iH & "];", $pScan2)


    If $bFastCmp Then
        $iResult = DllCall("msvcrt.dll", "int:cdecl", "memcmp", "ptr", $pScan1, "ptr", $pScan2, "uint", DllStructGetSize($tPixel1))[0]
    Else
        If ($iW * $iH + 1) * 3 > 16 * 1024^2 Then Return SetError(3, 0, 0)
        Local $iX, $iY, $iRowOffset, $iPixel1, $iPixel2, $c = 1, $aDiff[$iW * $iH + 1][3]
        For $iY = 0 To $iH - 1
            $iRowOffset = $iY * $iW + 1
            For $iX = 0 To $iW - 1
                $iPixel1 = DllStructGetData($tPixel1, 1, $iRowOffset + $iX) ;get pixel color
                $iPixel2 = DllStructGetData($tPixel2, 1, $iRowOffset + $iX) ;get pixel color
                If $iPixel1 <> $iPixel2 Then
                    $aDiff[$c][0] = $iX & ", " & $iY
                    $aDiff[$c][1] = "0x" & Hex($iPixel1, 8)
                    $aDiff[$c][2] = "0x" & Hex($iPixel2, 8)
                    $c += 1
                EndIf
            Next
        Next
        $aDiff[0][0] = TimerDiff($t)
        $aDiff[0][1] = $iW
        $aDiff[0][2] = $iH
    EndIf

    _GDIPlus_BitmapUnlockBits($hImage1, $tBitmapData1)
    _GDIPlus_BitmapUnlockBits($hImage2, $tBitmapData2)

    If $bFastCmp Then Return SetError(0, Int(TimerDiff($t)), $iResult = 0)

    ReDim $aDiff[$c][3]
    Return $aDiff
EndFunc

 

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thanks for pointing out that one returns a bitmap object and the other an image object, @junkew. I'm not really sure what the difference between those two are, but I guess they are different GDI+ objects.

I tried replacing the compare function in my code with @UEZ's _GDIPlus_ImageCompare and it just worked. I didn't have to do any "bitmap-object to image-object"-juggling. Amazing stuff and lightning fast. Thank you so much.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...