Images and Banks

BlitzPlus Forums/BlitzPlus Programming/Images and Banks

Nicstt(Posted 2005) [#1]
Just wondering
As i need to perform a large number of operations on images, to speed the process up, is there a way of getting an image into a bank so I can use the bank to quicken the process?


CS_TBL(Posted 2005) [#2]
uh, just manually put every R G and B value in a bank? But you need your own bound-checks ofcourse.. unless you're sure you never break out of your typical for x for y loop. I use something similar for my private texture-generator.. all actions done in banks..

banksize = imagewidth*imageheight*3


Nicstt(Posted 2005) [#3]
Do banks have a max size, looking at occasional bank size of over a million


Bremer(Posted 2005) [#4]
From the manual:

LockedPixels( [buffer] )
Parameters
buffer - A graphics buffer handle  

Description
Returns a bank representing the pixels of a locked graphics buffer.  



Nicstt(Posted 2005) [#5]
My manual doesnt mention bank only buffer, which manual are you talking about?


Mr. Write Errors Man(Posted 2005) [#6]
Is there a way to do the reverse: set bank contents into a buffer? Or do I have to do it pixel by pixel?


Bremer(Posted 2005) [#7]
@Nicoust, Its from the BlitzPlus manual. You posted the question in the BlitzPlus section, so I assumed its BlitzPlus you are using.

Here is an example of how to poke directly into buffers.

Graphics 640,480,32,2

temp = CreateImage( 256, 256 )
SetBuffer ImageBuffer( temp )
LockBuffer ImageBuffer( temp )
imagebank = LockedPixels()
imagepitch = LockedPitch()
UnlockBuffer ImageBuffer( temp )

SetBuffer BackBuffer()
LockBuffer BackBuffer()
backbank = LockedPixels()
backpitch = LockedPitch()
UnlockBuffer BackBuffer()

While Not KeyHit(1)

	Cls

	; poking random colors into the imagebuffer
		For y =0 To 255
			For x = 0 To 255
				PokeInt( imagebank, y*imagepitch+x*4, Rnd($FFFFFF) )
			Next
		Next

	; poking random colors into the backbuffer
		For y =0 To 255
			For x = 0 To 255
				PokeInt( backbank, y*backpitch+x*4, Rnd($FFFFFF) )
			Next
		Next

	DrawBlock temp, 260, 0

	Flip

Wend
End


@Arctic Fox, you can use copybank to move large amounts of pixel data from a bank to an imagebuffer or the backbuffer.


Bremer(Posted 2005) [#8]
Do banks have a max size, looking at occasional bank size of over a million


I believe its only limited by the amount of available ram.


Nicstt(Posted 2005) [#9]
thx for respones

yeh blitzplus

dont have manual, bought online, have 3d one, nothing in the docs included with game, thanks for posting the code


Bremer(Posted 2005) [#10]
The IDE have the manual built into it. Click on the home button (the one with the house) and then click Command Reference. That should bring you to the command list.


Nicstt(Posted 2005) [#11]
yeh seen the info there Zawran, its a little lacking in detail for the likes of me.

ok been trying this, it reeds the colours of my image, almost correctly, but there is extra info every so many.


Been wondering if there is alpha detail or something else, and if so how i get round it.

I've tried int as well as byte, but byte is nearer to being correct

 window = CreateWindow("Test", 100, 200, 640, 480, 0, 11)

temp = LoadImage("tinytest.bmp")
SetBuffer ImageBuffer( temp )
LockBuffer ImageBuffer( temp )
imagebank = LockedPixels()
imagepitch = LockedPitch()
UnlockBuffer ImageBuffer( temp )



savingdata = WriteFile("C:\Documents and Settings\Administrator\My Documents\Blitz Progs\newest.txt")
WriteLine savingdata, ImageWidth(temp) : WriteLine savingdata, ImageHeight(temp) ;+ " image height" + " image width"

size = ImageWidth(temp) * ImageWidth(temp)
For count = 0 To size - 1
		peekdata = PeekByte (imagebank, count)

		WriteLine savingdata, peekdata
Next		
CloseFile(savingdata)

Notify "done"


loadingdata = ReadFile("C:\Documents and Settings\Administrator\My Documents\Blitz Progs\newest.txt")
width1 = ReadLine (loadingdata) : height1  = ReadLine (loadingdata) ;+ " image height" + " image width"
canvasPic 	= CreateCanvas (0, 0, width1, height1 , window)

SetBuffer CanvasBuffer (canvasPic)

LockBuffer CanvasBuffer(canvasPic)
backbank = LockedPixels()
backpitch = LockedPitch()
format = LockedFormat(CanvasBuffer(canvasPic))
UnlockBuffer CanvasBuffer(canvasPic)
Notify format
size = width1 * height1
For count = 0 To size - 1
	pokedata = ReadLine (loadingdata)
	PokeByte (backbank, count, pokedata)
	FlipCanvas canvasPic
Next

CloseFile(loadingdata)

gfxBank3 = CreateImage ( width1, height1 )
CopyRect 0, 0, width1, height1, 0, 0, CanvasBuffer(canvasPic), ImageBuffer(gfxBank3)
SaveImage(gfxBank3, "C:\Documents and Settings\Administrator\My Documents\Blitz Progs\newest.bmp")

WaitKey()

End 



Bremer(Posted 2005) [#12]
Pixel data is in the following format if using 24bit, AARRGGBB, which means that if you only want the color data, and not have any alpha you can do an "and $FFFFFF" on the value you get with peekInt. The individual colors are extraced like so:

red = ( RGB shr 16 ) and $FF
green = ( RGB shr 8 ) and $FF
blue = RGB and $FF

You can get back to the RGB color like so:

RGB = red shl 16 + green shl 8 + blue

I hope that helps somewhat.


Nicstt(Posted 2005) [#13]
ok im getting somewhere due to all the help:)

however testing my code and i get errors after its completed the first line of the image correctly

i thought a bank was a continuous section, or is info placed in the image to take care of a new line?

window = CreateWindow("Test", 100, 200, 640, 480, 0, 11)

temp = LoadImage("test 20 x 2.bmp") ; width and height in pixels
SetBuffer ImageBuffer( temp )
LockBuffer ImageBuffer( temp )
imagebank = LockedPixels()
imagepitch = LockedPitch()
UnlockBuffer ImageBuffer( temp )

Const CONST_StepA = 3
Const CONST_StepB = 4

savingdata = WriteFile("C:\Documents and Settings\Administrator\My Documents\Blitz Progs\newest.txt")
WriteLine savingdata, ImageWidth(temp) : WriteLine savingdata, ImageHeight(temp) ;+ " image height" + " image width"

size = ImageWidth(temp) * ImageHeight(temp) * CONST_StepA
For count = 0 To size - 1 Step CONST_StepA
		peekdata = PeekInt (imagebank, count)
		peekdata = peekdata And $FFFFFF
		;red = ( peekdata Shr 16 ) And $FF
		;green = ( peekdata Shr 8 ) And $FF
		;blue = peekdata And $FF

		WriteLine savingdata, peekdata ;Str red +" "+ Str green +" "+ Str blue
Next		
CloseFile(savingdata)



Bremer(Posted 2005) [#14]
When you load an image, it is stored with alpha value regardless of whether it had it or not. So each pixel color is 4 bytes long.

The pixel data is stored in one continuous row, and if you wanted to pokeInt data into it at specific X/Y positions you will have to know the pitch of the imagebuffer. This determines how many bytes there is until a new line is stored. See the previous example I posted for how to do the offset calculations.

The following is untested as I don't have the option of using my blitz at this computer I am at.

size = ImageWidth( temp ) * ImageHeight( temp )
for count = 0 to size-1
   peekdata = peekInt( imagebank, count*4 ) and $FFFFFF
   red = ( peekdata shr 16 ) and $FF
   green = ( peekdata shr 8 ) and $FF
   blue = peekdata and $FF
   WriteLine( saveingdata, peekdata )
   ; Writeline( savingdata, red+","+green+","+blue )
next



Nicstt(Posted 2005) [#15]
@Zawran - Finally got it done. Big thanks for help.

I need to read what you wrote, i mis-typed something.


Bremer(Posted 2005) [#16]
Glad that I could help you on your way :)