This code works too slowly 
Author Message
 This code works too slowly

Hi all,

    and sorry for my bad english!

I must modify the gamma of an 1024x768 b/n immage, and I wrote this loop:

    For x = 0 To Pic.ScaleWidth
        For y = 0 To Pic.ScaleHeight
            NewColor = GetPixel(Pic.hdc, x, y) 'From GDI32.dll
             r = (NewColor Mod 256)
             r = (NewColor And &HFF)
             r = gammafunction
             SetPixelV Pic.hdc, x, y, RGB(r, r, r) 'From GDI32.dll
        Next y
    Next x

The problem is that it is very very slow.
How could I increase the speed?

    Many thanks in advance,

        Andrea



Sun, 20 Feb 2005 03:22:31 GMT  
 This code works too slowly
1. Is your ScaleMode set to Pixels?  If it is twips, you will be doing 15x
the work.
2. Use C/C++ instead

David
lilchips.com


Quote:
> Hi all,

>     and sorry for my bad english!

> I must modify the gamma of an 1024x768 b/n immage, and I wrote this loop:

>     For x = 0 To Pic.ScaleWidth
>         For y = 0 To Pic.ScaleHeight
>             NewColor = GetPixel(Pic.hdc, x, y) 'From GDI32.dll
>              r = (NewColor Mod 256)
>              r = (NewColor And &HFF)
>              r = gammafunction
>              SetPixelV Pic.hdc, x, y, RGB(r, r, r) 'From GDI32.dll
>         Next y
>     Next x

> The problem is that it is very very slow.
> How could I increase the speed?

>     Many thanks in advance,

>         Andrea



Sun, 20 Feb 2005 03:30:16 GMT  
 This code works too slowly

Quote:
> 1. Is your ScaleMode set to Pixels?  If it is twips, you will be doing 15x
> the work.
> 2. Use C/C++ instead

*Groan*..  C++ isn't just a magical solution to slow code, knowing why it's
slow is far more efficient.
The reason the code is running so slowly is that you have to unlock the DC,
retrieve the colour,  lock the DC then visa-versa every time you manipulate
each pixel - Very slow!  The better way of doing this is to simply retrieve
the entire contents of the array in one shot, do your colour manipulation
then fire it all back at the object in one go.  You can do this by referring
to the image data as a DIB and using the Get/SetDIBits() API calls to
retrieve the DIB data into a local buffer for manipulation, you can do this
directly from a picture box with no extra calls required.
Also if your calculation is a simple 1:1 (Which it looks to be) then you can
optimize this further using a lookup table rather than calculating every
pixel.
This will knock spots off a C++ conversion of the code.
Hope this helps,

    Mike

 -- EDais --

 - Microsoft Visual Basic MVP -
WWW: Http://EDais.earlsoft.co.uk/




Sun, 20 Feb 2005 04:43:02 GMT  
 This code works too slowly
C++ is magical?   ;-)

Sorry, wasn't referring to it as such.
Just that VB's manipulation of graphics even via API can be frustrating and
slow if one is trying to do a large number of image manipulation iterations.
Not so much in the Blt/DIB department, but in the manipulation algorithms.
No arguments intended.

David
lilchips.com



Quote:
> > 1. Is your ScaleMode set to Pixels?  If it is twips, you will be doing
15x
> > the work.
> > 2. Use C/C++ instead

> *Groan*..  C++ isn't just a magical solution to slow code, knowing why
it's
> slow is far more efficient.
> The reason the code is running so slowly is that you have to unlock the
DC,
> retrieve the colour,  lock the DC then visa-versa every time you
manipulate
> each pixel - Very slow!  The better way of doing this is to simply
retrieve
> the entire contents of the array in one shot, do your colour manipulation
> then fire it all back at the object in one go.  You can do this by
referring
> to the image data as a DIB and using the Get/SetDIBits() API calls to
> retrieve the DIB data into a local buffer for manipulation, you can do
this
> directly from a picture box with no extra calls required.
> Also if your calculation is a simple 1:1 (Which it looks to be) then you
can
> optimize this further using a lookup table rather than calculating every
> pixel.
> This will knock spots off a C++ conversion of the code.
> Hope this helps,

>     Mike

>  -- EDais --

>  - Microsoft Visual Basic MVP -
> WWW: Http://EDais.earlsoft.co.uk/





Sun, 20 Feb 2005 04:48:02 GMT  
 This code works too slowly
You could use GetDIBits (I think thats it) and modify the byte array
directly. That would be a LOT faster, but you would have to contend with the
bizarre memory layout of one in memory.

Max Bolingbroke


Quote:
> Hi all,

>     and sorry for my bad english!

> I must modify the gamma of an 1024x768 b/n immage, and I wrote this loop:

>     For x = 0 To Pic.ScaleWidth
>         For y = 0 To Pic.ScaleHeight
>             NewColor = GetPixel(Pic.hdc, x, y) 'From GDI32.dll
>              r = (NewColor Mod 256)
>              r = (NewColor And &HFF)
>              r = gammafunction
>              SetPixelV Pic.hdc, x, y, RGB(r, r, r) 'From GDI32.dll
>         Next y
>     Next x

> The problem is that it is very very slow.
> How could I increase the speed?

>     Many thanks in advance,

>         Andrea



Sun, 20 Feb 2005 15:10:58 GMT  
 This code works too slowly

Quote:
> You could use GetDIBits (I think thats it) and modify the byte array
> directly. That would be a LOT faster, but you would have to contend with
the
> bizarre memory layout of one in memory.

What's bizarre about it?  How would _you_ lay it out in memory? ;)

    Mike

 -- EDais --

 - Microsoft Visual Basic MVP -
WWW: Http://EDais.earlsoft.co.uk/




Sun, 20 Feb 2005 18:48:35 GMT  
 This code works too slowly
This solution is good (and the idea of  lookup table is very intelligent and
faster): the routine takes only 0,5sec, the old one more than 30sec.

    Many thanks!

        Andrea



Quote:
> > 1. Is your ScaleMode set to Pixels?  If it is twips, you will be doing
15x
> > the work.
> > 2. Use C/C++ instead

> *Groan*..  C++ isn't just a magical solution to slow code, knowing why
it's
> slow is far more efficient.
> The reason the code is running so slowly is that you have to unlock the
DC,
> retrieve the colour,  lock the DC then visa-versa every time you
manipulate
> each pixel - Very slow!  The better way of doing this is to simply
retrieve
> the entire contents of the array in one shot, do your colour manipulation
> then fire it all back at the object in one go.  You can do this by
referring
> to the image data as a DIB and using the Get/SetDIBits() API calls to
> retrieve the DIB data into a local buffer for manipulation, you can do
this
> directly from a picture box with no extra calls required.
> Also if your calculation is a simple 1:1 (Which it looks to be) then you
can
> optimize this further using a lookup table rather than calculating every
> pixel.
> This will knock spots off a C++ conversion of the code.
> Hope this helps,

>     Mike

>  -- EDais --

>  - Microsoft Visual Basic MVP -
> WWW: Http://EDais.earlsoft.co.uk/





Tue, 22 Feb 2005 00:57:17 GMT  
 This code works too slowly
Mike,

Would you perhaps have some sample code for this for us primitives? I'd love
to use this method on a number of my apps, if I could get it to work. Maybe
you could point us to a sample app or something?

All the Best,

Sean



Quote:
> > 1. Is your ScaleMode set to Pixels?  If it is twips, you will be doing
15x
> > the work.
> > 2. Use C/C++ instead

> *Groan*..  C++ isn't just a magical solution to slow code, knowing why
it's
> slow is far more efficient.
> The reason the code is running so slowly is that you have to unlock the
DC,
> retrieve the colour,  lock the DC then visa-versa every time you
manipulate
> each pixel - Very slow!  The better way of doing this is to simply
retrieve
> the entire contents of the array in one shot, do your colour manipulation
> then fire it all back at the object in one go.  You can do this by
referring
> to the image data as a DIB and using the Get/SetDIBits() API calls to
> retrieve the DIB data into a local buffer for manipulation, you can do
this
> directly from a picture box with no extra calls required.
> Also if your calculation is a simple 1:1 (Which it looks to be) then you
can
> optimize this further using a lookup table rather than calculating every
> pixel.
> This will knock spots off a C++ conversion of the code.
> Hope this helps,

>     Mike

>  -- EDais --

>  - Microsoft Visual Basic MVP -
> WWW: Http://EDais.earlsoft.co.uk/





Fri, 06 May 2005 06:59:48 GMT  
 This code works too slowly

Quote:
> Would you perhaps have some sample code for this for us primitives? I'd
love
> to use this method on a number of my apps, if I could get it to work.
Maybe
> you could point us to a sample app or something?

Have a look at the DIB tutorial on my page.
Hope this helps,

    Mike

 -- EDais --

 - Microsoft Visual Basic MVP -
WWW: Http://EDais.earlsoft.co.uk/




Fri, 06 May 2005 07:44:40 GMT  
 This code works too slowly
Thanks Mike, I think I have a working version of it now! This is going to
speed a few things up... LOL

Thanks again, this was a big help.

All the Best,

Sean



Quote:
> > Would you perhaps have some sample code for this for us primitives? I'd
> love
> > to use this method on a number of my apps, if I could get it to work.
> Maybe
> > you could point us to a sample app or something?

> Have a look at the DIB tutorial on my page.
> Hope this helps,

>     Mike

>  -- EDais --

>  - Microsoft Visual Basic MVP -
> WWW: Http://EDais.earlsoft.co.uk/





Sat, 07 May 2005 04:40:17 GMT  
 This code works too slowly

Quote:
> Thanks Mike, I think I have a working version of it now! This is going to
> speed a few things up... LOL

> Thanks again, this was a big help.

You're welcome - That's what they're there for!

    Mike

 -- EDais --

 - Microsoft Visual Basic MVP -
WWW: Http://EDais.earlsoft.co.uk/




Sat, 07 May 2005 05:24:10 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. DBCombo works slowly

2. code is too slowly

3. code is too slowly

4. old code - new browser - code doesn't work

5. Pls Help!!!! Macro in background runs very slowly

6. Word closes slowly up to 3 min.

7. Macros causing document to open TOO slowly

8. Document opening TOO SLOWLY because of VB Macros

9. Application performance slowly degrades

10. VB.NET: Form slowly redraws background image behind transparent controls

11. dotNet Application loads very slowly

12. Please help: Program unloads slowly when Inet is used

 

 
Powered by phpBB® Forum Software