WriteImagePixels

Monkey Forums/Monkey Programming/WriteImagePixels

clonk(Posted 2012) [#1]
Hi ,

i'am new to Monkey and working on a little digging Game.
I use the new ReadPixel and WritePixel Method from Monkey v63 and an little Module from Invaderjim http://www.monkeycoder.co.nz/Community/posts.php?topic=3482#36684 .
Everytime i try to Draw the Image back to the Screen it vanishes from the Playfield.

This is the part of my Code :
Method Render()
		If Self.firstrender Then
			Self.terrainPixels = GetImagePixels(img, 0, 0, Self.img.Width(),Self.img.Height())
	
			stampPixels = GetImagePixels(tool, 0, 0, tool.Width(), tool.Height())
			Self.firstrender = False
		End If
	
		If Self.stamp Then
        		
			Local sourcewidth:Int = Self.img.Width()
			Local sourceheight:Int = Self.img.Height()
			Local toolwidth:Int = Self.tool.Width()
			Local toolheight:Int = Self.tool.Height()
			Local mx:Int = 100 - ( toolwidth / 2 )
			Local my:Int = 350 - ( toolheight / 2 )
			Local ti : Int = sourcewidth * my + mx
			Local loop:Int = 0
			For Local y : Int = 0 Until toolheight
				For Local x : Int = 0 Until toolwidth
					If mx + x < sourcewidth And my + y < sourceheight And mx + x > -1 And my + y > -1
						Local argb : Int
						argb = terrainPixels[ ti + x ]
						Local ga : Int = ( argb Shr 24 ) & $ff
						argb = stampPixels[( y * toolwidth ) + x ]
						Local ta : Int = ( argb Shr 24 ) & $ff                                  
						Local tr : Int = ( argb Shr 16 ) & $ff
						Local tg : Int = ( argb Shr 8 ) & $ff
						Local tb : Int = argb & $ff
						If ga <> 0 And ta <> 0
							If tr = 255 And tg = 255 And tb = 255
								terrainPixels[ ti + x ] = 0
							Else
								'terrainPixels[ ti + x ] = stampPixels[( y * toolwidth ) + x ]
							Endif
						Endif
					Endif
					loop += 1
				Next For
				
				ti = ti + sourcewidth
			Next For	
			'img.WritePixels( terrainPixels, 0, 0, img.Width(), img.Height() )
			WriteImagePixels(img2, terrainPixels, 0, 0, img.Width(), img.Height())
			
			Self.stamp = False	
		End If
		
		'DrawImage(img, Self.x, Self.y)
		DrawImage(img2, 50, 50)	
		
	End Method




samowitsch(Posted 2012) [#2]
Hi Clonk.

I think i found your problem. It seems that WritePixels works only with a new created image?

[monkeycode]
' don't work
img = LoadImage('foo.png')
terrainPixels = GetImagePixels(img, 0, 0, Self.img.Width(),Self.img.Height())
WriteImagePixels(img, terrainPixels, 0, 0, img.Width(), img.Height())

'work
img = LoadImage('foo.png')
new = CreateImage(300, 300)
terrainPixels = GetImagePixels(img, 0, 0, Self.img.Width(),Self.img.Height())
WriteImagePixels(new, terrainPixels, 0, 0, img.Width(), img.Height())
[/monkeycode]

Regards samowitsch


clonk(Posted 2012) [#3]
Hi samowitsch,

thanks this work great.

Greats
Clonk