Sign in to follow this  
Followers 0
MasonMill

would like some help with a small scrip

8 posts in this topic

Hi Guys,

I am getting an error "Array variable subscript badly formatted". When i use the function in other scripts i don't get this error, any one know why? Also, another question. I am making this script to create arrays from an area where i dont know if colors change so i want to find out what they change to if they do change by looking at a matrix. In essence im making a slide show of matrices. By calling each one $a and giving a = a + 1 is this correct? or is the function return from fileimagetoarray not such that i can do this. Thanks!

here is the code:

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

$pong_hWnd = WinGetHandle("pong")
Sleep(100)
WinActivate("pong")
sleep(3000)

$k = 0
Global $array

For $i = 0 to 200 Step 1
    $k = _ScreenCapture_CaptureWnd(@ScriptDir & "\ArrayTest_screenshot.bmp", $pong_hWnd, 61, 49, 119, 61, False)
    sleep(100)
    $a = _FileImageToArray($k, $array)
    Sleep(10)
    $a = $a + 1

Next

MsgBox(4096, "", "Script is done!", 10)



Func _FileImageToArray($sFileName, ByRef $aArray)
    Local $Reslt, $stride, $format, $Scan0, $iIW, $iIH, $hImage
    Local $v_Buffer, $width, $height
    $hImage = _GDIPlus_ImageLoadFromFile($sFileName)
    $iIW = _GDIPlus_ImageGetWidth($hImage)
    $iIH = _GDIPlus_ImageGetHeight($hImage)

    ProgressOn("Progress Bar", "Filling a " & $iIW & " x " & $iIH & " size array.", "0 percent")
    $Reslt = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iIW, $iIH, $GDIP_ILMREAD, $GDIP_PXF32ARGB)

    ;Get the returned values of _GDIPlus_BitmapLockBits ()
    $width = DllStructGetData($Reslt, "width")
    $height = DllStructGetData($Reslt, "height")
    $stride = DllStructGetData($Reslt, "stride")
    $format = DllStructGetData($Reslt, "format")
    $Scan0 = DllStructGetData($Reslt, "Scan0")

    Dim $aArray[$height][$width]
    For $j = 0 To $iIH - 1
        For $i = 0 To $iIW - 1
            $v_Buffer = DllStructCreate("dword", $Scan0 + ($j * $stride) + ($i * 4))
            $aArray[$j][$i] = Hex(DllStructGetData($v_Buffer, 1), 6)
        Next
        ProgressSet(Int(100 * $j / ($iIH)), Int(100 * $j / ($iIH)) & " percent")
    Next
    _GDIPlus_BitmapUnlockBits($hImage, $Reslt)
    ProgressOff()
    _GDIPlus_ImageDispose($hImage)

    Return
EndFunc ;==>_FileImageToArray

Share this post


Link to post
Share on other sites



MasonMill,

Looking at the Help file for _ScreenCapture_CaptureWnd I see that there is no return value - so the $k variable probably still holds 0. When you pass $k to your _FileImageToArray function, the function is expecting a file name, which it is most unlikely to be getting. As a result, your $height and $width variables within the function are also very unlikely to hold any sensible values - and when you try and use them to declare $aArray you could well get the error you mention.

I freely admit that I am only guessing here because I have never seen that particular error before, but I am pretty sure of my diagnosis! :P

The solution is to pass a valid filename to the function - you know it works when you do! :blink:

As to the slideshow - you are most certainly not getting what you think you are with your code. There is no return value from the _FileImageToArray function - seems to be a trend here : - so that $a could hold anything.

What I would suggest is to do something like this:

; Set the file name
$sFile = @ScriptDir & "\ArrayTest_screenshot.bmp"

; Declare an array to hold all the other arrays
Global $aSlideshow[201]

; Get your arrays from each screenshot
For $i = 0 to 200 Step 1
    Local $aArray
    _ScreenCapture_CaptureWnd($sFile, $pong_hWnd, 61, 49, 119, 61, False)
    Sleep(100)
    _FileImageToArray($sFile, $aArray)
    ; And store them in the big array
    $aSlideshow[$i] = $aArray
Next

There is no prohibition in storing arrays in another array in AutoIt - however, the Help file page <Language Reference - Variables> does make the following comments:

[...]an AutoIt-Array could also contain [...] other Arrays. [...]This has not been strictly forbidden in AutoIt. However, it is NOT ADVISABLE to mix different datatypes in an Array. Especially the use of an Array inside another Array will severely affect the execution speed of your script.

As long as you do not try anything silly, like sorting $aSlideshow, you should find no problems. Just remember that to access the arrays within the $aSlideshow array you need to extract them - something like this: ;)

For $i = 0 To 200
    Local $aArray = $aSlideshow[$i]
    ; Now you can access the individual array you created from the screenshot
    ; More code dealing with $aArray
    ; ...
Next

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

You are always so helpful! Thank you very much, when i get off work today i will see what i can do!

Share this post


Link to post
Share on other sites

OK, i did some changes but i am still getting the error. What i didnt notice erlier that my progress bar was showin that it was building a -1 x -1 matrix which means that the error is due to the fact that with $iIW = _GDIPlus_ImageGetWidth($hImage) and $iIH = _GDIPlus_ImageGetHeight($hImage) are -1 which would be an error due to $hImage = _GDIPlus_ImageLoadFromFile($sFileName) not being properly pulled. Why would it not be pulling that file? Everything looks in order. Here is my script again:

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

Opt('MustDeclareVars', 1)

Global $aSlideshow[201]
Global $k = "C:\Users\Mason\Desktop\scripts\ArrayTest_screenshot.bmp"
Global $pong_hWnd = WinGetHandle("pong")

WinActivate("pong")

_ScreenCapture_SetBMPFormat(2)

For $i = 0 to 200 Step 1
    Local $anArray
    _ScreenCapture_CaptureWnd($k, $pong_hWnd, 61, 49, 119, 61, False)
    sleep(100)
    _FileImageToArray($k, $anArray)
    $aSlideshow[$i] = $aArray

Next

MsgBox(4096, "", "Script is done!", 10)



Func _FileImageToArray($sFileName, ByRef $aArray)
    Local $Reslt, $stride, $format, $Scan0, $iIW, $iIH, $hImage
    Local $v_Buffer, $width, $height
    $hImage = _GDIPlus_ImageLoadFromFile($sFileName)
    $iIW = _GDIPlus_ImageGetWidth($hImage)
    $iIH = _GDIPlus_ImageGetHeight($hImage)

    ProgressOn("Progress Bar", "Filling a " & $iIW & " x " & $iIH & " size array.", "0 percent")
    $Reslt = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iIW, $iIH, $GDIP_ILMREAD, $GDIP_PXF32ARGB)

    ;Get the returned values of _GDIPlus_BitmapLockBits ()
    $width = DllStructGetData($Reslt, "width")
    $height = DllStructGetData($Reslt, "height")
    $stride = DllStructGetData($Reslt, "stride")
    $format = DllStructGetData($Reslt, "format")
    $Scan0 = DllStructGetData($Reslt, "Scan0")

    Dim $aArray[$height][$width]
    For $j = 0 To $iIH - 1
        For $i = 0 To $iIW - 1
            $v_Buffer = DllStructCreate("dword", $Scan0 + ($j * $stride) + ($i * 4))
            $aArray[$j][$i] = Hex(DllStructGetData($v_Buffer, 1), 6)
        Next
        ProgressSet(Int(100 * $j / ($iIH)), Int(100 * $j / ($iIH)) & " percent")
    Next
    _GDIPlus_BitmapUnlockBits($hImage, $Reslt)
    ProgressOff()
    _GDIPlus_ImageDispose($hImage)

    Return
EndFunc ;==>_FileImageToArray

Share this post


Link to post
Share on other sites

line 45

Share this post


Link to post
Share on other sites

i mean 44, the line with Dim $aArray[$height][$width]

Share this post


Link to post
Share on other sites

MasonMill,

Just where I suggested it would be. :blink:

I bet you are not opening the GDI+ library so you never open the file. ;)

Add these lines at the beginning and end of your function and try again - it is working fine for me if I do that:

; Initialize GDI+ library
_GDIPlus_Startup ()

; Rest of function

; Shut down GDI+ library
_GDIPlus_ShutDown ()

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

OMG im so dumb lol. thanks!

Share this post


Link to post
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
Sign in to follow this  
Followers 0