PDA

Click to See Complete Forum and Search --> : If If If If


ADF-Sniper
Jun 22nd, 2007, 07:11 PM
I'm trying to find colour's in an image. Its working but what im trying to say is....

If GetPixel(pctMain.hdc, x, y) = 15663343 or 65280 or 65535 Then

The problem I am having is its using up way too much cpu usage.

A little help would be good :D

Private Sub Command1_Click()
Dim x As Long, y As Long

If Not LoadPNGFile(App.Path & "\test.png", pctMain) Then
MsgBox "There was an error loading the test.png file", vbCritical, "Error"
End If

For x = 103 To Width - 103
For y = 10 To 300
If GetPixel(pctMain.hdc, x, y) = 65280 Then
MsgBox "Colour Found"
GoTo Found
Else
If GetPixel(pctMain.hdc, x, y) = 15663343 Then
MsgBox "Colour Found"
GoTo Found
Else
If GetPixel(pctMain.hdc, x, y) = 65025 Then
MsgBox "Colour Found"
GoTo Found
Else
If GetPixel(pctMain.hdc, x, y) = 65535 Then
MsgBox "Colour Found"
GoTo Found
Else
If GetPixel(pctMain.hdc, x, y) = 255 Then
MsgBox "Colour Found"
GoTo Found
Else

End If
End If
End If
End If
End If
Next
Next

Merri
Jun 22nd, 2007, 07:36 PM
You can make it five times faster if you store the result of GetPixel(pctMain.hdc, x, y) into a temporary Long variable before the series of If statements. Thus:
lngColor = GetPixel(pctMain.hdc, x, y)
If lngColor = 65280 Then
MsgBox "Colour Found"
GoTo Found
ElseIf lngColor = 15663343 Then
MsgBox "Colour Found"
GoTo Found
ElseIf lngColor = 65025 Then
MsgBox "Colour Found"
GoTo Found
ElseIf lngColor = 65535 Then
MsgBox "Colour Found"
GoTo Found
ElseIf lngColor = 255 Then
MsgBox "Colour Found"
GoTo Found
End If

You could also store the hDC value temporarily in a Long variable. However, that's the most you can do while still using GetPixel. If you need even more speed, then the only way is to handle raw bitmap data in a long array or a byte array.

ADF-Sniper
Jun 22nd, 2007, 08:30 PM
Thank you, you have been a great help.

Regards
Nathum

si_the_geek
Jun 23rd, 2007, 10:16 AM
Note that an alternative is to use a Select Case instead of multiple If's, eg:
Select Case GetPixel(pctMain.hdc, x, y)
Case 65280, 15663343, 65025, 65535, 255
MsgBox "Colour Found"
GoTo Found
End If I think this is one of the cases where Select Case is faster, but I'm sure Merri will correct me if that's wrong!

If after making these changes you are looking for more speed, you will need to do something like Merri suggested at the end of his post.. I'd recommend looking for a tutorial about DIB sections (which in my experience are over 50 times faster than SetPixel/GetPixel).


ps: any chance of seeing the code for LoadPNGFile? (I have a potential use for it) :D

Merri
Jun 23rd, 2007, 10:59 AM
si_the_geek: with GetPixel in the same loop, it doesn't really matter.

However, if you're looking for PNG stuff: AlphaImage Control (http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=68262&lngWId=1), 32bpp DIB Suite (http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=67466&lngWId=1) and PNG Reader (http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=63684&lngWId=1).

ADF-Sniper
Jun 30th, 2007, 04:15 AM
Thankyou all for your help.

This is what I have been using.

Case 65280 ' Green
GoTo Found
Case 15663343 ' Pink
GoTo Found
' Case 16776960 'Light Blue
' GoTo Found

Ill change it a bit ;) now.

Regards
Nathum