ReadPixelFast
Blitz3D Forums/Blitz3D Programming/ReadPixelFast
| ||
| Hello, im having problems reading a pixel color using readpixel fast :( Im not sure if there is a problem with blitz3d being able to do this or am I doing it wrong..
Function ColorImage(x%,y%,scolor)
Local oldimage = LoadImage ("gfx/player.png")
Local newimage = CreateImage(48,64)
Local ob = ImageBuffer(oldimage) : LockBuffer ob
Local nb = ImageBuffer(newimage) : LockBuffer nb
Local ix,iy
For ix = 0 To 48 - 1
For iy = 0 To 64 - 1
Local rbgc = ReadPixelFast(ix+x,iy+y,ob)
Select scolor
Case red
If rbgc = $FF404040
WritePixelFast ix,iy,($FF400000),nb
ElseIf rbgc = $FF808080
WritePixelFast ix,iy,($FF800000),nb
ElseIf rbgc = $FFC0C0C0
WritePixelFast ix,iy,($FFC00000),nb
ElseIf rbgc = GetRGB(232,232,232)
WritePixelFast ix,iy,($FFFF4040),nb
Else
WritePixelFast ix,iy,(rbgc),nb
EndIf
Case blue
Case green
Case yellow
End Select
Next
Next
UnlockBuffer ob : UnlockBuffer nb
MaskImage newimage,255,0,255
Return newimage
End Function
here is my code please let me know if im doing somethign wrong and also this is very slow :( |
| ||
| Be more specific. I've used writepixel/readpixel etc countless times, it works. It's slow when reading though, it'll be a lot better to cache the pixel colors in a bank or similar, that'll speed it up alot. |
| ||
The first two lines already look like trouble:
Function ColorImage(x%,y%,scolor)
Local oldimage = LoadImage ("gfx/player.png")
It looks likes this function will be called many times for different values of x,y. But oldimage is being reloaded with every function call. That would explain most of the slowness. And the previously loaded copy is simply forgotten, which is a memory leak. |
| ||
| ahh okay thanks Floyd I forgot to unload the image :( Otacon: can you show me a example of using a bank to do this game process?? :EDIT: I got it fixed and it really fast now :P |
| ||
| Sure, something simple would be like, type img end type |
| ||
Sure, something simple would be like,
type img
field pix,w,h
end type
function createFast(x,y,w,h)
o.img=new img
bank=createBank( w*h*3)
lockbuffer
for gy=x to y+h
for gx=x to x+w
pokeint bank,bo,readpixelfast(gx,gy)
bo=bo+4
next
next
unlockbuffer
o\pix=bank
return handle(o)
end function
and then pass the returned handle to this func to retrieve pixel color very fastly.
function readPixelFastest( img,x,y)
in.img=object.img(img)
return peekint(in\pix,(y*in\w+x)*4)
end function
Although I strongly suggest accessing the bank directly using the same method as readpixel fastest above does, rather calling a function to do it, as that adds a lot of unneded overhead. |