This example is for b3d. It looks at the structure of an image in memory and then pokes the pixels with random colors.
I have done a speed test with this method versus WritePixelFast and it is twice as slow. I assume this is because you need to pass the value to a bank and then to memory. I also tested passing to a bank per pixel and then passing the whole image in one go with RTL_MoveMemory and it still wasn't as fast as WritePixelFast although it was very close.
I have done a speed test with this method versus WritePixelFast and it is twice as slow. I assume this is because you need to pass the value to a bank and then to memory. I also tested passing to a bank per pixel and then passing the whole image in one go with RTL_MoveMemory and it still wasn't as fast as WritePixelFast although it was very close.
;PeekPoke Example, for Blitz3D Include "peekpoke.bb" ;PeekPoke Module Graphics 640,480,0,2 SetBuffer BackBuffer() load=0 ;Flag to load or create an image If load=1 image=LoadImage("test.bmp") Else width=100 height=63 frames=1 image=CreateImage(width,height,frames) EndIf BufferList=PeekI(image+4) ;pImageBufferList BufferListEnd=PeekI(image+8) ;pImageBufferListEnd numframes=(BufferListEnd-BufferList)/4 ;Number of image frames buffer=ImageBuffer(image,0) pImageBuffer=PeekI(BufferList+(0*4)) ;pImageBuffer[frame] LockBuffer buffer ;LockBuffer to return a pointer to the pixels UnlockBuffer buffer DDSurface7=PeekI(buffer+12) ;pImageDDSurface7 PixelData=PeekI(buffer+72) ;pImagePixelData WidthImage=PeekI(buffer+92) ;ImageWidth HeightImage=PeekI(buffer+96) ;ImageHeight DrawImageText(Str(0),image,0) If GraphicsDepth()=32 pitch=(((width*8)+127)/128)*16 ;16 bit-aligned For iy=1 To height-2 offset=(pitch*iy) Shl 2 For ix=1 To width-2 pixel=BGRA(Rand(255),Rand(255),Rand(255)) PokeI(PixelData+offset+(ix Shl 2),pixel) Next Next ElseIf GraphicsDepth()=16 pitch=(((width*8)+255)/256)*32 ;32-bit aligned For iy=1 To height-2 offset=(pitch*iy) Shl 1 For ix=1 To width-2 pixel=BGR16(Rand(255),Rand(255),Rand(255)) PokeS(PixelData+offset+(ix Shl 1),pixel) Next Next EndIf While Not KeyDown(1) ;Esc key Cls Text 0,0,"width="+width+" height="+height+" frames="+frames Text 0,12,"image="+image Text 0,36,"BufferList="+BufferList Text 0,48,"BufferListEnd="+BufferListEnd Text 0,60,"numframes="+numframes Text 0,84,"buffer="+buffer Text 0,96,"pImageBuffer="+pImageBuffer Text 0,120,"DDSurface7="+DDSurface7 Text 0,132,"PixelData="+PixelData Text 0,144,"WidthImage="+WidthImage Text 0,156,"HeightImage="+HeightImage Text 0,168,"pitch="+pitch DrawImage image,MouseX()-width,MouseY()-height Flip Wend End Function BGRA(blue,green,red,alpha=$FF) ;Converts BGRA to a 32-bit value, alpha is optional Return ((alpha Shl 24) Or (red Shl 16) Or (green Shl 8) Or blue) End Function Function BGR16(blue,green,red) ;Converts BGR to a 16-bit value Return ((red Shr 3 Shl 11) Or (green Shr 2 Shl 5) Or (blue Shr 3)) End Function Function DrawImageText(textstr$,image,frame=0) ;Draw frame number on image Local buffer=GraphicsBuffer() SetBuffer ImageBuffer(image,frame) ClsColor 255,255,255 : Cls Color 255,0,0 : Text 0,0,textstr$ ClsColor 0,0,0 : Color 255,255,255 SetBuffer buffer End Function