Need to optimize a loop accessing an array and two bitmap objects (VB)

Frasse

Newcomer
Joined
Jul 20, 2003
Messages
13
Need to optimize a loop accessing an array and two bitmap objects

I have the following code. It's purpose it to take a source Bitmap object, read it and manipulate it throwing out an resulting Bitmap object. This should be done in realtime since it's done while moving the mouse over an image.

The code calling this method is managed, the bitmap objects are allocated as managed objects.

Now I wonder how I make the best optimization of this code. It might not mean that it should be unmanaged, just as fast as possible.

Private _ManipulationSize As Size

Private _XMap(,) As Integer
Private _YMap(,) As Integer

Public Function GenerateManipulated(ByVal Source As Bitmap) As Bitmap

Dim Dest As New Bitmap(Source)

Dim XCo As Integer, YCo As Integer

For YCo = 0 To Me._ManipulationSize.Height - 1
For XCo = 0 To Me._ManipulationSize.Width - 1
Dest.SetPixel(XCo, YCo, Source.GetPixel(_XMap(XCo, YCo), _YMap(XCo, YCo)))
Next
Next

Return Dest

End Function

Thanks..

/Frasse
 
I don't understand the whole thing you're trying to do - but it looks like you want to offset some pixels (with the X and YMap)... wouldn't it be easier to copy the whole Bitmap in one step and only replace the "offsetted" Areas pixel by pixel? I guess this would be much faster. But I don't know how you create these maps and if it would be easy to check whether the point is offsetted or not....

maybe some thing like this would help:
Code:
Public Function GenerateManipulated(ByVal Source As Bitmap) As Bitmap

   Dim Dest As New Bitmap(Source)

   Dim XCo As Integer, YCo As Integer

   For YCo = 0 To Me._ManipulationSize.Height - 1
      For XCo = 0 To Me._ManipulationSize.Width - 1
         If XCo <> _XMap(XCo,YCo) Or YCo <> _YMap(XCo,YCo) Then
            Dest.SetPixel(XCo, YCo, Source.GetPixel(_XMap(XCo, YCo),_YMap(XCo, YCo)))
         End If
      Next
   Next

   Return Dest

End Function
 
Good thought, I might be able to do something out of it. Basicly what i want to do is to take the source which holds an original copy of the picture. And move the pixels around based on the remapping that the _XMap and _YMap arrays hold.

So if i take the 1X1 pixel out of the source, and I check in the _XMap and _YMap table to see where the 1x1 pixel should be placed in the destination Bitmap. So if _XMap(1,1) = 3 and _YMap(1,1) = 5 the source pixel 1x1 should be on coordinate 3x5 in the destination Bitmap.

Hope you get it (I'm in a hurry, GF is on me ;))

/Frasse
 
then I understood it quite correct... so try my suggestion... checking whether the coordinates in the Maps are changed and only set the Bitmaps pixel if they are! another possibility would be splitting the Bitmap into multiple parts (for example 4) and process them in different threads and put them together again at the end.... but this could be quite complicated to manage.....
 
Yeah you did ;), but I miss understod your code at first. But after taking a second glance at it I see that it was just right.

Also since I create the Dest Bitmap out of the Source there is a lot to gain here.

Thanks..

Still, I wan't to know if there is more to gain. Take a look at my other postings regarding this matter from different views: http://www.xtremedotnettalk.com/t74685/s.html and http://www.xtremedotnettalk.com/t74651/s.html

Thanks again

/Frasse
 
Back
Top