Page 1 of 1

realtime image manipulation - how to speed up this example?

Posted: Thu Mar 25, 2004 10:37 pm
by Ralf
i am planing to write a little tool for creating realtime filter/manipulation and drawing effects on graphics (images). in my first coding experiences i used an image and drawed all needed stuff by using PB drawing funtions while setting the drawingoutput direct to the image. this methode worked fine for drawing lines, boxes, circles only.

i wanted to speed this up and having some more flexibility for realtime effects like twirl. in this way i created an array and started to write my own drawing functions (writing changed pixels directs into the array).

the last methode works too but is very slow in compare to the drawing direct to screen routine 8O I ever tought handling a graphic in an array would be mutch better and faster for manipulations.

when getting my tool ready i want that the routines are platform indpendent (for example an array;) if possible. so it would be nice to finding a way that dont require DX, OGL, GDI. The user should be able to use the results for his DX, OGL, GDI routines ;)

If you think that DX based routines would speed my routines for realtime drawing and manipulations a lot up - then my routines should work from now only on windows with dx platforms 8)

following working small example will show you the big different between drawing direct to an image and drawing to an array. please dont blame me that plotting the finished drawed/manipulated array is slow - i know and please remeber that this only happen when all operations are finished.

Code: Select all

Dim dTexture(256,256) 

; ---- routine for drawing a box direct into array ------ 

Procedure DrawArray_Box(x,y,width,height,col)      
  For ix = x To x+width 
    For iy = y To x+height 
      dTexture(ix,iy) = col 
    Next 
  Next 
EndProcedure 

; ------ draw box direct into array ------ 

MessageRequester("Start","Drawing  box into array",0) 
starttime=TimeGetTime_() 
  For i = 0 To 100 
    DrawArray_Box(100,100,100,100,255) 
  Next 
totaltime=TimeGetTime_()-starttime 
MessageRequester("Finish","Array finished in "+Str(totaltime)+" milliseconds",0) 

; ------ draw box direct to bitmap ------ 

CreateImage(0,256,256) 

MessageRequester("Start","Draw box direct to bitmap",0) 
starttime=TimeGetTime_()    
StartDrawing(ImageOutput()) 
  For i = 0 To 100 
    Box(100,100,100,100,255) 
  Next 
StopDrawing() 
totaltime=TimeGetTime_()-starttime 
MessageRequester("Finish","Bitmap finished in "+Str(totaltime)+" milliseconds",0) 

; ---- Save Bitmap - Array Methode ---- 

SaveImage(0,"c:\DrawTest_ToBitmap.bmp") 

; ---- Save Bitmap - Bitmap Methode ---- 

CreateImage(1,256,256) 

StartDrawing(ImageOutput()) 
For x = 0 To 255 
  For y = 0 To 255 
    col = dTexture(x,y) 
    Plot (x,y,col) 
  Next  
Next 
StopDrawing() 

SaveImage(1,"c:\DrawTest_ToArray.bmp") 

End 
On my 1ghz machine i get follwing speed results:

91 ms - drawing direct into array
2 ms - drawing direct to bitmap

i have seen code snips and answers from guys to try GetDIBits_ and SetDIBits_ and using pointers but havent really understand it :cry: . what about DrawingBuffer() - faster results, DX required?

please let us discuse the fastest way with full working examples and good documentation. many thanks :wink:

i think direct memory access would be the best and fastest way (no array!?). how to code this?

Posted: Fri Mar 26, 2004 12:03 am
by dmoc
Maybe mmx/ 3dnow extensions will help? Search the forums for some examples.

Posted: Sat Mar 27, 2004 12:25 am
by Ralf
dmoc wrote:Maybe mmx/ 3dnow extensions will help? Search the forums for some examples.
sorry, i dont know what you mean (compiler option)? i dont think that this makes any different. in fact the routine should work on every computer (not only 3dnow on amd or mmx on intel). i know that there must be a way by using direct access to the memory but i dont know how to do this with purebasic ;(

Posted: Sat Mar 27, 2004 1:00 am
by dmoc
By "every computer" what do you mean? Intel, amd, mac + Windows, Linux and OSX? You say you want "realtime" effects - possible but without being platform specific or using a 3D library you would need to do a lot of research on optimised assembly. Best source for that is the demo scene. From what I know, which admittedly isn't very much, most realtime effects sw now use either dx or opengl, the latter if you want a degree of x-platform. When you see "product X is cross-platform" it either means one of two things: either it does the bare minimum thus ensuring it will work on several platforms or there are several code paths, each specific to a particular platform, eg mmx for intel, 3dnow for amd. I suggested these because unless you want to utilise say opengl, effects like filtering/ twirling (distortion) may be a lot faster using instructions designed to perform the same operation on a lot of data. Good luck.

Posted: Sat Mar 27, 2004 1:30 am
by Ralf
@dmoc
my idea was to have a routine that for realtime effects, compatible on all systems. this would be possible while using an array and using own drawing routines to draw direct into this arrays ;)

in the meantime i think the best would be to program the routines only for windows platform (maybe later for others too!?) using arrays or without arrays - just only the best and fatest way (direct memory access or dx ;)

now we know that the first version of my tool will only work on windows systems and if it require, i will use dx. so, someone an idea how to speed up my example without drawing direct to the image using PB drawing functions? because i dont want use pixel and color for bumpmap effects ;)