Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function Optimization

Status
Not open for further replies.

idinkin

Programmer
Sep 12, 2002
29
0
0
IL
Hi all!
I wrote the following function:

Function FillBitmap(Buffer() As Byte, SrcColor As RGB, DestColor As RGB, Width As Long, Height As Long)
Dim Red As Double, Green As Double, Blue As Double
Dim DeltaRed As Double, DeltaBlue As Double, DeltaGreen As Double
SrcColorRed = SrcColor.Red
SrcColorGreen = SrcColor.Green
SrcColorBlue = SrcColor.Blue
DestColorRed = DestColor.Red
DestColorGreen = DestColor.Green
DestColorBlue = DestColor.Blue
DeltaRed = (SrcColorRed - DestColorRed) / Height
DeltaGreen = (SrcColorGreen - DestColorGreen) / Height
DeltaBlue = (SrcColorBlue - DestColorBlue) / Height
Red = SrcColor.Red
Green = SrcColor.Green
Blue = SrcColor.Blue
For Y = 0 To Height - 1
Red = Red - DeltaRed
Green = Green - DeltaGreen
Blue = Blue - DeltaBlue
For X = 0 To Width - 1
Buffer((Y * Width + X) * 3) = Blue
Buffer((Y * Width + X) * 3 + 1) = Green
Buffer((Y * Width + X) * 3 + 2) = Red
Next X
Next Y
End Function

But it's really slow! It takes about a second (950 ms) to fill fullscreen 800x600. How can I optimize it for better performance?

 
If you don't understand the code please tell me
 
1. Use Long instead of Double for all your Doubles
2. Use \ (Integer Division) instead of / (normal division)
3. Take your Y calcs outside your X loop thus, making sure that you Dim tempX and tempY as Long:

For Y = 0 To Height - 1
Red = Red - DeltaRed
Green = Green - DeltaGreen
Blue = Blue - DeltaBlue
tempY = Y * Width
For X = 0 To Width - 1
tempX = (tempY + X) * 3
Buffer(tempX) = Blue
Buffer(tempX + 1) = Green
Buffer(tempX + 2) = Red

Next X
Next Y
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thanx.

But if I will change double to long the function will not work properly coz I need it to do the filling gradiently.
The second idea is impossible too coz I have to get not integer numbers. Thanx for the last one.
 
Colours are always Integers!
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thank you a lot!
I checked the function, it does the gradient filling in 330ms.
 
not 'efficiency', but you include no errror handling, and no 'return value', so any problems encountered will most likely just crash the process where the procedure is executing. Further, the use of common property names ("Width" and "Height") are at best likely to generate confusion amongst people, and could easily do the same for programs ...


MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top