Pixel Manipulating ala SetBuffer()

BlitzMax Forums/BlitzMax Beginners Area/Pixel Manipulating ala SetBuffer()

Clyde(Posted 2005) [#1]
In ye older Blitz programs, setbuffer, Lockbuffer and writepixelfast would be combined to achieve various sorts of effects.

And I've had a last and final attempt to do something akin to it in BMAX. Nothing is happening with the colours being manipulated for the example effect. If you could help that would be great. Also better and clearer details and descriptions in the manual on what the commands do would be great.

The Theory is, this should work with all kinds of special effects, and other drawing like how you would with SetBuffer() etc.

Here's the duff code, for anyone interested in taking a look. As Im flumuxed over it all.


Thanks


tonyg(Posted 2005) [#2]
I think you've seen this


Clyde(Posted 2005) [#3]
Yeah i used that ideas for trying to do these effects. And thanks TonyG.

I've made a bit of a change, but still the same nothing ness.



tonyg(Posted 2005) [#4]
I think you're making it too complex as you're mixing pixmaps and images.
When you lock an image it automatically returns a pixmap.
You can readpixel/writepixel from/to this pixmap as you would have done an image buffer.
When you unlock the image the pixmap is deleted and the image is updated.
I can try this later if I have time but pseudo code would be...
Load image
My_pixmap:TPixmap = lock(image)
readpixel(my_pixmap)
Change RGB
writepixel(my_pixmap)
unlock(image)
drawimage(image)
.
If you also want to do composite sprites you can...
Loadimage(image1)
loadimage(image2)
cls
drawimage image1
drawimage image2
grabimage image3)
cls
drawimage image3
flip
.
If you want to make an image from a pixmap you simply...
image4:TImage = loadimage(my_pixmap:TPixmap)
Hope it helps
P.S. What you can't do is write two (or more) images to the same pixmap (hence the need to grabimage).


Clyde(Posted 2005) [#5]
If you have time please could you give it ago?
Many thanks,
THUMBZ


tonyg(Posted 2005) [#6]
I'll try and get time tonight.
List what it is you want to do as the code I gave before is used to lock an image to create a pixmap, read/write pixel the pixmap, unlock to update the image, display the image.
I'm assuming you want to join two images BEFORE you do this which is simply draw image1, draw image2, grab image3 and then do the read/write.


Clyde(Posted 2005) [#7]
No, all I want to do is make effects - how you would do with WritePixelFast. Store the colours, then manipulate them (for example a sinus) into another image over the top of the main image (the one that the colours were obtained from)

Thanks Tony!


tonyg(Posted 2005) [#8]
Think this is it Thumbz...
'====================================================
' Copying and manipulating images
' Care Of Setbuffer Imagebuffer() and WritePixelFast
' attempt 4001
'====================================================

Strict

'
' Screen setup dimensions.
'
Const XRES=640
Const YRES=480

Graphics XRES,YRES


Global MainImage=CreateImage(32,32,dynamicimage)		' the main image to be used for manipulating.
' The mainimage needs to be created with dynamicimage if it's to be changed in any way.
'
' create a custom makeshift image.
'
For Local a=0 To 31
	SetColor Rand(255),Rand(255),Rand(255)
	DrawRect 0,a,32,1
Next
GrabImage MainImage,0,0
'need to set the color back as it affects subsequent drawing and text
SetColor 255,255,255
Cls
DrawImage mainimage,0,0
DrawText "Mainimage",0,40
Global my_pixmap:TPIxmap=LockImage(MainImage)
' took out the arrays for a simple test and I'm happier with SHR and SHL
	For Local x=0 To 31
		For Local y=0 To 31
			Local RGB=ReadPixel(my_pixmap,x,y)
			Local alpha = RGB Shr 24 & 255
			Local Red = RGB Shr 16 & 255
			Local Green = RGB Shr 8 & 255
			Local blue = RGB Shr 0 & 255
			' used the same values as yourself.
             red= red * 0.222 ; green=green*0.555 ; blue=blue*0.125
             RGB = (alpha Shl 24) | (red Shl 16) | (green Shl 8) | (blue Shl 0)
             WritePixel (my_pixmap,x,y,rgb)
		Next
	Next
DrawPixmap my_pixmap,0,60
DrawText "Pixmap after pixelling",0,100
UnlockImage(mainimage)
' notice that once unlocked the changes made to the pixmap are reflected on the image.
DrawImage mainimage,0,120
DrawText "MainImage after pixelling",0,160
DrawPixmap my_pixmap,0,180
DrawText "..and the pixmap is still there",0,230
FlushMem
DrawPixmap my_pixmap,0,250
DrawText "even after flushmem",0,310
Flip
WaitKey()

This all seems OK. The only thing missing from the old Imagebuffers is being able to draw 2 images to the same pixmap (unless somebody else knows how to do it). Obvious answer, as before, is to write them both to backbuffer and grab them. Small images aren't a problem but larger images, which took 1 ms in B2D, take about 226ms due to the grabimage.
Anyway, hope it helps.


Dreamora(Posted 2005) [#9]
sidequote: If you use grabing in "realtime" environment use pixmaps instead of images. Grabimage grabs a pixmap and converts it where the convert is the slow thing.


tonyg(Posted 2005) [#10]
That reduces it to 160ms. 50ms for the GrabPixmap and 110ms for the conversion back to an image. I suppose you could use the pixmap directly.
What are the downsides of using pixmaps rather than images?I guess images are quicker while pixmaps are more versatile.


Clyde(Posted 2005) [#11]
Mega Thanks for the time Tony, will take a deeper look into it all. And thanks Dreamora for the extra info.


ImaginaryHuman(Posted 2005) [#12]
The only problem with using pixmaps is that a) they are in main memory so all rendering is done with the CPU, and b) you can't use any opengl rendering to them.


Dreamora(Posted 2005) [#13]
Problem with Pixmaps is mostly that they are only a memory block of color data, while images are real images that the graphic card already knows as such.

images are about 10 times as fast as pixmaps.


Clyde(Posted 2005) [#14]
That'll be why LockImage and Write Pixelling to them is so slow on my machine. I thought that BMAX was going to be alot more quicker than previous Blitz Basics.

Thanks for that. I will have to look to alternatives.


tonyg(Posted 2005) [#15]
I actually found the writepixel a LOT quicker in BlitzMax (maybe 3 time quicker). Pixmaps are, basically, equivalent to BB images.


Clyde(Posted 2005) [#16]
I get the same results, (sometimes alot slower) then the other blitz editions.

And I've this machine:
GF4 MX 420, P4 2Ghz, 512 Ram
Windows XP SP2


tonyg(Posted 2005) [#17]
And you haven't got debug enabled on one or either of them?
Doing the same test as above but changing everything for a 640*480 screen takes 150ms in BMX and 260 in BB..
AMD 2700 with 9800 Pro and 1G Ram
WinXP SP2


Clyde(Posted 2005) [#18]
Ahuh, Debug is off and all.

Try it with altering and manipulating alot of colours at once, and it's mighty slow. its not looking very promising at the moment.


tonyg(Posted 2005) [#19]
These are the tests I ran...
'====================================================
' Copying and manipulating images
' Care Of Setbuffer Imagebuffer() and WritePixelFast
' attempt 4001
'====================================================
Strict
'
' Screen setup dimensions.
'
Const XRES=640
Const YRES=480

Graphics XRES,YRES

Global MainImage=CreateImage(640,480,dynamicimage)		' the main image to be used for manipulating.
' The mainimage needs to be created with dynamicimage if it's to be changed in any way.
'
' create a custom makeshift image.
'
For Local a=0 To 479
	SetColor Rand(255),Rand(255),Rand(255)
	DrawRect 0,a,640,1
Next
GrabImage MainImage,0,0
'need to set the color back as it affects subsequent drawing and text
SetColor 255,255,255
Cls
DrawImage mainimage,0,0
DrawText "Mainimage",0,40
Local time_start:Int = MilliSecs()
Global my_pixmap:TPIxmap=LockImage(MainImage)
' took out the arrays for a simple test and I'm happier with SHR and SHL
	For Local x=0 To 639
		For Local y=0 To 479
			Local RGB=ReadPixel(my_pixmap,x,y)
			Local alpha = RGB Shr 24 & 255
			Local Red = RGB Shr 16 & 255
			Local Green = RGB Shr 8 & 255
			Local blue = RGB Shr 0 & 255
			' used the same values as yourself.
             red= red * 0.222 ; green=green*0.555 ; blue=blue*0.125
             RGB = (alpha Shl 24) | (red Shl 16) | (green Shl 8) | (blue Shl 0)
             WritePixel (my_pixmap,x,y,rgb)
		Next
	Next
UnlockImage(mainimage)
' notice that once unlocked the changes made to the pixmap are reflected on the image.
DrawImage mainimage,0,0
DrawText "MainImage after pixelling",0,160
Local time_end:Int=MilliSecs()
Local total_time:Int = time_end - time_start
DrawText "Total_time " + total_time,0,170
Flip
FlushMem
WaitKey()

and in B2D...
Const XRES=640
Const YRES=480

Graphics XRES,YRES
SetBuffer BackBuffer()

Global MainImage=CreateImage(640,480)	
For a=0 To 480
	Color Rand(255),Rand(255),Rand(255)
	Rect 0,a,640,1,solid
Next
GrabImage MainImage,0,0
Color 255,255,255
Cls
DrawImage mainimage,0,0
Text 0,40,"Mainimage"
time_start = MilliSecs()
SetBuffer ImageBuffer(mainimage)
LockBuffer ImageBuffer(MainImage)
	For  x=0 To 640
		For  y=0 To 480
			 RGB=ReadPixelFast(x,y,ImageBuffer(mainimage))
			 alpha = RGB Shr 24 And 255
			 Red = RGB Shr 16 And 255
			 Green = RGB Shr 8 And 255
			 blue = RGB Shr 0 And 255
             red= red * 0.222 : green=green*0.555 : blue=blue*0.125
             RGB = (alpha Shl 24) Or (red Shl 16) Or (green Shl 8) Or (blue Shl 0)
             WritePixelFast x,y,rgb,ImageBuffer(mainimage)
		Next
	Next
UnlockBuffer ImageBuffer(mainimage)
SetBuffer BackBuffer()
DrawImage mainimage,0,0
Text 0,160,"MainImage after pixelling"
time_end=MilliSecs()
total_time = time_end - time_start
Text 0,170,"Total_time " + total_time
Flip
WaitKey()



Clyde(Posted 2005) [#20]
Good, I get 150 in the BMAX one, and the example is with changing the colours just once.

Have a go with making more of an effect with colours and Writepixel and for me at least it's 24 fps.


tonyg(Posted 2005) [#21]
Can you post some code?
Just to confirm that, with the tests above, you see a difference between bmx and bb or not?


Clyde(Posted 2005) [#22]
with the Blitz 2D one I get a whopping 350 MS. Almost double.
If I have anycode with faster writepixelling I'll post it up, as I've deleted alot of them.