Jump to content

How to "plot" ?


 Share

Recommended Posts

Well then .., that worked. How da hell do you know stuff like that ... an error in AutoIt .
This works ... 

#include <ScreenCapture.au3>
Global $MUI=(GUICreate("Test     ",187,155,-1,-1,+BitOR(-2147483648, 144),-1))
;Global $MUI=(GUICreate("Test     ",187,155,-1,-1,BitOR(768,8,256),-1))
GUISetBkColor(0x000000)
GUISetState(@SW_SHOW)

$hdc = DllCall("user32.dll","int","GetDC","hwnd",$MUI)
$x = 100
$y = 100

$mode=1 ; mode=1 to make pic / Mode=not 1 to read pic

If($mode=(1))Then
 DllCall("gdi32.dll","int","SetPixel","handle",$hdc[0],"int",$x,"int",$y,"int",0xFDFFFF)
 Sleep(100)
 $pix= DllCall("gdi32.dll","int","GetPixel","handle",$hdc[0],"int",$x,"int",$y)
 MsgBox(4096,"",hex($pix[0],6),0)

 $pic=_ScreenCapture_CaptureWnd (@WorkingDir&"\Pic.bmp",$MUI,0,0,-1,-1,false)
 _WinAPI_DeleteObject($pic)
Else

 $pic=(GUICtrlCreatePic(@WorkingDir&"\Pic.bmp",0,0,0,0,14,-1))
 Sleep(100)
 $pix= DllCall("gdi32.dll","int","GetPixel","handle",$hdc[0],"int",$x,"int",$y)
 MsgBox(4096,"",hex($pix[0],6),0)
Endif
DllCall("user32.dll","int","ReleaseDC","hwnd",0,"int",$hdc[0])

While 1
 $nMsg = GUIGetMsg()
 If $nMsg<>0 Then
  Switch $nMsg
   Case -3
    Exit
  EndSwitch
 Else
  Sleep(300)
 EndIf
WEnd

 

Edited by Comatose
Link to comment
Share on other sites

This is so beautiful. Thanks for your help. 100% would have never got that part.
Thanks to both of you. 1st time I've ever got any real help on any forum.
Feel like I should send out a few checks now ... you're both worth it!

This is not really coding. More of a scrap code to try and prove a concept to myself
for a much larger project. Now that this part is working I can complete this. I did
come up with a few work around's but never could get that picture part to work right. 
Now I'm can do it correctly and the picture part works ... again, thank you!

Edited by Comatose
Link to comment
Share on other sites

Ohhh can I have some of your 'Checks' too!!??

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
Global $iW = 240, $iH = 240

$hGui = GUICreate("GDIPlus Example", $iW, $iH)
GUISetState()

_GDIPlus_Startup()
$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphicGUI)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsClear($hGraphic, Default)

;--------------------------------------------------------------------------------------------------------------------------
;Draw to the bitmap
;--------------------------------------------------------------------------------------------------------------------------
Local $tBitmapData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $iW, $iH, BitOR($GDIP_ILMWRITE, $GDIP_ILMREAD), $GDIP_PXF32RGB)
Local $iScan0 = DllStructGetData($tBitmapData, "Scan0") ;get scan0 (pixel data) from locked bitmap
Local $tPixel = DllStructCreate("int[" & $iW * $iH & "];", $iScan0)

For $iY = 0 To $iH - 1 ;Set whole bmp black
    $iRowOffset = $iY * $iW + 1
    For $iX = 0 To $iW - 1 ;set each pixel in each line and row
        DllStructSetData($tPixel, 1, 0x000000, $iRowOffset + $iX)
    Next
Next

For $iY = 0 To $iH - 1 Step 5
    $iRowOffset = $iY * $iW + 1
    For $iX = 0 To $iW - 1 Step 5
        DllStructSetData($tPixel, 1, Random(0x0, 0xFFFFFF), $iRowOffset + $iX)
    Next
Next

_GDIPlus_BitmapUnlockBits($hBitmap, $tBitmapData) ;unlocks a portion of a bitmap that was locked by _GDIPlus_BitmapLockBits
_GDIPlus_ImageSaveToFile($hBitmap, "test.png")
ShellExecute("test.png")

;--------------------------------------------------------------------------------------------------------------------------
;load file and put the pixels in an array
;--------------------------------------------------------------------------------------------------------------------------

Local $hBitmap_load = _GDIPlus_BitmapCreateFromFile("test.png")
Local $tBitmapData_load = _GDIPlus_BitmapLockBits($hBitmap_load, 0, 0, $iW, $iH, BitOR($GDIP_ILMWRITE, $GDIP_ILMREAD), $GDIP_PXF32RGB)
Local $iScan0_load = DllStructGetData($tBitmapData_load, "Scan0") ;get scan0 (pixel data) from locked bitmap
Local $tPixel_load = DllStructCreate("int[" & $iW * $iH & "];", $iScan0_load)

Local $aArr[$iW][$iH]
For $iY = 0 To $iH - 1
    $iRowOffset = $iY * $iW + 1
    For $iX = 0 To $iW - 1
        $aArr[$iX][$iY] = Hex(DllStructGetData($tPixel_load, 1, $iRowOffset + $iX))
    Next
Next
_GDIPlus_BitmapUnlockBits($hBitmap_load, $tBitmapData_load) ;unlocks a portion of a bitmap that was locked by _GDIPlus_BitmapLockBits
_WinAPI_DeleteObject($hBitmap_load)

_ArrayDisplay($aArr)


;--------------------------------------------------------------------------------------------------------------------------
;
;--------------------------------------------------------------------------------------------------------------------------
GUIRegisterMsg($WM_PAINT, "WM_PAINT")
_GDIPlus_GraphicsClear($hGraphicGUI, Default)
_GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBitmap, 0, 0)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            _GDIPlus_GraphicsDispose($hGraphic)
            _GDIPlus_GraphicsDispose($hGraphicGUI)
            _WinAPI_DeleteObject($hBitmap)
            _GDIPlus_Shutdown()
            Exit
    EndSwitch
WEnd

;Func to redraw on PAINT MSG
Func WM_PAINT($hWnd, $msg, $wParam, $lParam)
    _GDIPlus_GraphicsClear($hGraphicGUI, 0xFFEEEEEE)
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBitmap, 0, 0)
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_PAINT

Here it is with gdi plus

I don't know that LockBits is more efficient but it expands the possibilities

like this for instance:

https://www.autoitscript.com/forum/topic/198124-are-there-any-way-to-get-an-array-of-pixelcolour-almost-instantly/?do=findComment&comment=1421226

 

Edited by Bilgus
Return $GUI_RUNDEFMSG, Hex()
Link to comment
Share on other sites

That's amazing. I've been working on this for a long time. Came up with a dozen ways to do it but never could finish it off.
Who would ever think AutoIt itself had a error I didn't know about. Sorry if I seemed rude. This has been more than frustrating.
I've been programming in circles for so long I may have lost myself a bit. You guys here are the best. Mass respect!
 

Edited by Comatose
Link to comment
Share on other sites

Pix.au3  (test script)

#NoTrayIcon
#include <Dot.au3>
$pic=(@WorkingDir&"\Pic.bmp")
CreateUi(150,150)
$mode=(1)

If($mode=(1))Then
 Plot(100,100,"FFCCEE")
 PicSet($pic)
Else
 PicGet($pic)
 $pix=(Point(100,100))
 MsgBox(4096,"",$pix,0)
Endif

ReleaseUi()
Sleep(100)
Exit

Dot.au3  (personal include for Pix.au3)

#include-once
#include <ScreenCapture.au3>
Func CreateUi($xs,$ys)
 Global $ui=(GUICreate("",$xs,$ys,-1,-1,+BitOR(-2147483648,144),-1))
 GUISetBkColor(0x000000)
 GUISetState(@SW_SHOW)
 ControlUi()
EndFunc
Func ControlUi()
 Global $uDLL=(DllOpen("user32.dll")),$gDLL=(DllOpen("gdi32.dll"))
 Global $hdc=(DllCall($uDLL,"int","GetDC","hwnd",$ui))
EndFunc
Func ReleaseUi()
 DllCall($uDLL,"int","ReleaseDC","hwnd",0,"int",$hdc[0])
 DllCall($gDLL,"int","ReleaseDC","hwnd",0,"int",$hdc[0]) ; ???
 $uDLL=(DllClose("user32.dll"))
 $gDLL=(DllClose("gdi32.dll"))
EndFunc
Func Plot($xp,$yp,$rgb)
 DllCall($gDLL, "int", "SetPixel", "handle", $hdc[0], "int", $xp, "int", $yp, "int", IsNumber($rgb) ? $rgb : "0x" & $rgb)
EndFunc
Func Point($xp,$yp)
 $tem=(DllCall($gDLL,"int","GetPixel","handle",$hdc[0],"int",$xp,"int",$yp))
 Return(Hex($tem[0],6))
EndFunc
Func PicSet($path)
 Sleep(100)
 $tem=(_ScreenCapture_CaptureWnd($path,$ui,0,0,-1,-1,false))
 _WinAPI_DeleteObject($tem)
EndFunc
Func PicGet($path)
 $tem=(GUICtrlCreatePic($path,0,0,0,0,14,-1))
 Sleep(100)
EndFunc

Generic usable include file:  (returns string)
Inputs: Plot(GUI handle, X position, Y position, "FFFFFF" or 0xFFFFFF)
           Point(GUI handle, X position, Y position)
Program Files (x86)\AutoIt3\Include\ ...

PlotAndPoint.au3

#include-once
Func Plot($_ui, $_x, $_y, $_rgb)
 $_hdc=(DllCall("user32.dll", "int", "GetDC", "hwnd", $_ui))
 DllCall("gdi32.dll", "int", "SetPixel", "handle", $_hdc[0], "int", $_x, "int", $_y, "int", IsNumber($_rgb) ? $_rgb : "0x" & $_rgb)
EndFunc
Func Point($_ui, $_x, $_y)
 $_hdc=(DllCall("user32.dll", "int", "GetDC", "hwnd", $_ui))
 $_pix=(DllCall("gdi32.dll", "int", "GetPixel", "handle", $_hdc[0], "int", $_x, "int", $_y))
 Return(Hex($_pix[0], 6)) ; Return("0x" & Hex($_pix[0], 6))
EndFunc

 

Edited by Comatose
Link to comment
Share on other sites

Optimization:

orig:

Func Plot($xp,$yp,$rgb)
 If(IsNumber($rgb))Then DllCall($gDLL,"int","SetPixel","handle",$hdc[0],"int",$xp,"int",$yp,"int",$rgb)
 If Not(IsNumber($rgb))Then DllCall($gDLL,"int","SetPixel","handle",$hdc[0],"int",$xp,"int",$yp,"int","0x"&$rgb)
EndFunc

new:

Func Plot($xp,$yp,$rgb)
 If IsNumber($rgb) Then 
   DllCall($gDLL,"int","SetPixel","handle",$hdc[0],"int",$xp,"int",$yp,"int",$rgb)
 Else
   DllCall($gDLL,"int","SetPixel","handle",$hdc[0],"int",$xp,"int",$yp,"int","0x"&$rgb)
 EndIf
EndFunc

 

Link to comment
Share on other sites

Streamlined:

Func Plot($xp, $yp, $rgb)
    DllCall($gDLL, "int", "SetPixel", "handle", $hdc[0], "int", $xp, "int", $yp, "int", IsNumber($rgb) ? $rgb : "0x" & $rgb)
EndFunc

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

52 minutes ago, Zedna said:

 ...

Actually ... orig version is 0.0001 seconds faster ...
Just added for testing anyways ... best case would be pick one and remove the other and the isNumber statement. 

Edited by Comatose
Link to comment
Share on other sites

On 10/21/2019 at 7:57 AM, InnI said:

@Comatose

There is the bug in AutoIt 3.3.14.5: https://www.autoitscript.com/trac/autoit/ticket/3682

Use real picture coords in function

$pic=(GUICtrlCreatePic(@WorkingDir&"\Pic.bmp",0,0,187,155,14,-1))

 

Did you notice 0,0 worked and acts like how the -1,-1 should have ...

Edited by Comatose
Link to comment
Share on other sites

This still has a logic error. It seems the red, green, blue values are a bit backwards. It's coming out blue, green red. 
Work around ... due to lack of knowledge. Now input and return is a string ("FFFFFF"). 

Func Plot($ui,$xp,$yp,$rgb)
 $a=(StringMid($rgb,1,2))
 $b=(StringMid($rgb,3,2))
 $c=(StringMid($rgb,5,2))
 $rgb=($c&$b&$a)
 $hdc=(DllCall("user32.dll","int","GetDC","hwnd",$ui))
 DllCall("gdi32.dll", "int", "SetPixel", "handle", $hdc[0], "int", $xp, "int", $yp, "int", "0x" & $rgb)
EndFunc
Func Point($ui,$xp,$yp)
 $hdc=(DllCall("user32.dll","int","GetDC","hwnd",$ui))
 $tem=(DllCall("gdi32.dll","int","GetPixel","handle",$hdc[0],"int",$xp,"int",$yp))
 $rgb=(Hex($tem[0],6))
 $a=(StringMid($rgb,1,2))
 $b=(StringMid($rgb,3,2))
 $c=(StringMid($rgb,5,2))
 $rgb=($c&$b&$a)
 Return $rgb
EndFunc

 

Edited by Comatose
Link to comment
Share on other sites

57 minutes ago, Comatose said:

This still has a logic error. It seems the red, green, blue values are a bit backwards. It's coming out blue, green red. 

Have a look at the function _WinAPI_SwitchColor (converts a color from BGR to RGB and vice versa) ;).

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

  • 1 year later...

This worked fine with the work around and gave a solid base line test to continue the project.
I did end up going with a modified version using _GDIPlus however. This was a good step in understand all this
and helped a lot with the final version.  

Edited by Comatose
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...