CGDipSnapShot - GDI replacement for PixelGetColor

Post your working scripts, libraries and tools
liij
Posts: 51
Joined: 23 Apr 2016, 13:25

Re: CGDipSnapShot - GDI replacement for PixelGetColor

06 Jan 2017, 17:43

edit=response to your edit
evilC wrote:[code]


I would advise using a debugger or doing something like chuck a msgbox in there to show you what is in the variable. Edit - I see the msgbox at the end now.


Not sure why cdig is not working in any way in my script, just thought it could be stupidly stuping thing from my side/incompability something with something. This "incompability" is too hard to me and I will check this several months later...
User avatar
evilC
Posts: 4281
Joined: 27 Feb 2014, 12:30

Re: CGDipSnapShot - GDI replacement for PixelGetColor

06 Jan 2017, 17:47

see previous post. It is likely to do with flow of control.
Again, would advise using a debugger such as Scite. The "red dot" you place on a line is called a "breakpoint". You can set them for lines you are interested and then let the code run until it hits one of those lines. Can also be useful to tell you if / when it is doing something.
Another useful tool is the debug log (See the OutputDebug command). I think Scite has a built in log viewer, but I use DebugView (Google it, it's MS Technet so safe) so I get a separate window with the log in that I can run even when not editing and see the logging from a script.
So yeah, check that it is actually taking a snapshot before it is checking the contents - that would me my guess as to what is wrong.
Also, with a line such as val:=snap.PixelScreen[1571,890].rgb bear in mind that if snap is empty, then val will be empty and AHK will not throw an error.
Last edited by evilC on 06 Jan 2017, 17:50, edited 1 time in total.
liij
Posts: 51
Joined: 23 Apr 2016, 13:25

Re: CGDipSnapShot - GDI replacement for PixelGetColor

06 Jan 2017, 17:49

Whole code before your advice was looking like that

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


There is

Code: [Select all] [Download] GeSHi © Codebox Plus

snap := new CGdipSnapshot(300,700,1620,380)
snap.TakeSnapshot()

before asking/msgboxing script about val, so I am not sure where to put it. It is already in good place I think...
Edit: computer is freezing and crashing after pressing alt+f5 (hotkey running script with snapshot), never happened to me :? :shock:
Yesterday it was giving with blank/empty msgbox
User avatar
evilC
Posts: 4281
Joined: 27 Feb 2014, 12:30

Re: CGDipSnapShot - GDI replacement for PixelGetColor

06 Jan 2017, 18:00

It's also entirely possible that I goofed on the "Screen" coordinate mode.
The "Snapshot" coordinate mode is the one that is internally used, so it may be worth seeing what happens if you work out what the snapshot coords you would be referencing are.
ie it may just be possible that you do have a snapshot, but you are requesting pixels from outside it (because either I or you goofed).
A simple test would be to replace snap.PixelScreen[1571,890].rgb with snap.PixelSnap[0,0].rgb and see if you get a value back. PixelSnap[0,0] will always hold something if there is a valid snapshot.
liij
Posts: 51
Joined: 23 Apr 2016, 13:25

Re: CGDipSnapShot - GDI replacement for PixelGetColor

06 Jan 2017, 18:09

I think i exluded this possibility with this script

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


And this script with your CGDipSnapShot is working perfectly.
This is part of my big script that I run separately from my big script...
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Re: CGDipSnapShot - GDI replacement for PixelGetColor

23 Jan 2017, 17:13

I just implemented this as a replacement for PixelGetColor in my script, and it is way, way faster, which is great, but it seems like I have a memory leak now. Here's my function that calls CGDSS:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



I've watched the memory usage of AHK using Windows Task Manager, and every time this function is called, as soon as it hits the "gemColorsRGB[x, y, 1] := snap.PixelSnap[px,py].r" line, the memory usage goes up by 4 MB, and it never comes back down. Do I need to destroy the snapshot when I'm done with it? If so, how do I do that? I read the wiki for this library and couldn't find it.
User avatar
evilC
Posts: 4281
Joined: 27 Feb 2014, 12:30

Re: CGDipSnapShot - GDI replacement for PixelGetColor

24 Jan 2017, 07:49

I always see the script as using ~4MB.
I had a little look and I cannot see any memory leaks.
If you can provide me with some short, simple code the reproduces the issue, I will investigate.
It doesn't have to do anything useful, just take a bunch of snapshots or whatever...
just me
Posts: 5431
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: CGDipSnapShot - GDI replacement for PixelGetColor

24 Jan 2017, 09:30

@Entropy42:

Code: [Select all] [Download] GeSHi © Codebox Plus

gemColorsRGB[x, y, 1] := snap.PixelSnap[px,py].r		; somehow this line eats up 4 MB of RAM
In your function the GDIP bitmap will be created when this line is executed the first time.

@evilC: The bitmap should be deleted when the function returns and the snap object is freed, but the call of __Delete() might be prevented because of the references to the embedded classes.
User avatar
evilC
Posts: 4281
Joined: 27 Feb 2014, 12:30

Re: CGDipSnapShot - GDI replacement for PixelGetColor

24 Jan 2017, 09:50

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Debug the script, every time TakeSnap() runs, the destructor is called.
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Re: CGDipSnapShot - GDI replacement for PixelGetColor

24 Jan 2017, 10:12

I didn't mean to say that the script above will just eat up 4 MB per loop iteration. As you can see, in that function, the gemColorsRGB[x, y, 1] := snap.PixelSnap[px,py].r line gets called 64 times, but it only goes up by 4 MB the first time it encounters that line after the snap is taken. The scanGridInnerLoop() function gets called inside another loop, and the memory usage is going up every time around THAT loop. I cut it way down and verified that this version eats up 4 MB every time I press F10. I was just calling this with my SciTE4AHK window in focus to test it.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

just me
Posts: 5431
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: CGDipSnapShot - GDI replacement for PixelGetColor

24 Jan 2017, 10:14

This one doesn't call the destructor here:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

I added a MsgBox to__Delete().
User avatar
evilC
Posts: 4281
Joined: 27 Feb 2014, 12:30

Re: CGDipSnapShot - GDI replacement for PixelGetColor

24 Jan 2017, 11:23

OK, it seems that the problem is caused by passing a reference to the CGDipSnapShot class into the _CColor class

ie the problem line is:

Code: [Select all] [Download] GeSHi © Codebox Plus

	Class _CColor {
__New(RGB, parent){
this._RGB := RGB
this._parent := parent ; <--- This line causes the problem
}


It is entirely possible that the system can be re-engineered to automatically fire the destructor, or maybe I could add a method to delete.
User avatar
evilC
Posts: 4281
Joined: 27 Feb 2014, 12:30

Re: CGDipSnapShot - GDI replacement for PixelGetColor

24 Jan 2017, 11:32

The only things that seem to use the parent property are:

Code: [Select all] [Download] GeSHi © Codebox Plus

		; Returns the Difference between two colors
Diff(c2){
return this._parent.Diff(this, c2)
}

The parent class does not have a Diff() method, so this code seems pointless.

Plus a bunch of checks like this:

Code: [Select all] [Download] GeSHi © Codebox Plus

if (!this._parent._SnapshotTaken){
return -1
}

But I do not see the point in this code. If the class exists, it will have been passed an RGB value, so it can return something.

So I removed this offending code, please try out this version of the library and let me know if it fixes your issues.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Re: CGDipSnapShot - GDI replacement for PixelGetColor

24 Jan 2017, 12:34

As far as I can tell, this fixed the issue. Thanks!
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Re: CGDipSnapShot - GDI replacement for PixelGetColor

26 Jan 2017, 11:22

I was trying to use Compare function of this library, but I'm not sure how to make the color object to compare to.
I have a pixel color that I get from
col1 := snap.PixelSnap[x,y].rgb
From the documentation, it sounds like this is going to be a color object, so to compare it I need to make another Color object, but don't know how. My colors are in RGB format, like [115,24,41], so I tried defining it by:
col2 := { r:115, g:24, b:41 }
And then comparing using:
match := col1.Compare(col2, 10)

But that didn't work, presumably because I made the color object wrong.
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Re: CGDipSnapShot - GDI replacement for PixelGetColor

26 Jan 2017, 14:33

Ah, ok, so it is not possible currently. I will just do a compare manually. Thanks!
+MeleaB+
Posts: 11
Joined: 26 Oct 2017, 18:46

Re: CGDipSnapShot - GDI replacement for PixelGetColor

26 Oct 2017, 19:00

EDIT: I just switched my default version of AHK from Unicode 64-bit to Unicode 32-bit. It looks like this may have stopped the crashing.

------------------------------------------------------

Hello evilC

I taught myself AHK a year ago, and used your CGDipSnapShot library to dramatically speed up the program I wrote. However, I ran into a problem which I am again experiencing now, as I attempt to include it in another program I am writing. The issue seems to be that CGDipSnapShot is causing AHK to crash whenever a GUI is closed. If I run, say, your 'example.ahk" then the program works as it should until I attempt to close it. Then I receive the following message:

"AutoHotKey Unicode 64-bit has stopped working. A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available."

If I remove the GUI lines from your example.ahk, then the program runs as it should. if I use it within my own program with a GUI, then it produces the error if I try and close the GUI (and also leaves the PC with the mouse "stuttering" and only moving very slowly, as it there's a process stealing all the focus.)

CGDipSnapShot is amazing, but unfortunately I'm prevented from using it with my GUI (which is imperative!) Any help would be greatly appreciated!

Cheers!

Return to “Scripts and Functions”

Who is online

Users browsing this forum: JnLlnd and 19 guests