GrabPixmap

BlitzMax Forums/BlitzMax Programming/GrabPixmap

Hello!

I have a little program where the user can put various shapes with different colors on the screen.

Now I need to know how may pixels of the whole screen are completely black.

To to this I grab the whole screen into a Pixmap using GrabPixmap(0,0,640,480). Then I read pixel by pixel and if the pixel has the required rgb values I increment a counter variable.

Luckily I only need this when the user puts a new shape on the screen so the speed is not a very important issue.

However, I wondered if anyone of you has an better and much faster idea on how to get the numbers of black pixels on the screen.

Can the shapes be defined by primitive polygon shapes?
Do they overlap?
Does it need to be 100% pixel perfect?

If yes, No and No... how about totalling up polygon/shape areas? In other words, create some polygon representations of your shapes. I'm guessing this would need some overlapping code...hmm.

Type point
	Field x#,y#
	
	Method Set(xx#,yy#)
		x=xx
		y=yy
	End Method
End Type

Type polygon
	Field pointList:point[]
	
	Function Create:polygon(pointCount:Int = 3) 'minimum for a poly
		Local p:polygon = New polygon
		p.pointList = New point[pointCount]
		
		For Local i = 0 Until p.pointList.length
			p.pointList[i] = New point
		Next
		Return p
	End Function
	
	Method GetArea:Int()
		Local i,j
		Local area
		
		For i=0 Until pointList.length
			j=(i+1) Mod pointList.length
			area:+ pointList[i].x * pointList[j].y
			area:- pointList[i].y * pointList[j].x
		Next
		area:/ 2
		Return area
	End Method
	
	Method Draw()
		SetColor 0,255,0
		Local i,j
		
		For i = 0 Until pointList.length
			j=(i+1) Mod pointList.length
			DrawLine pointList[i].x,pointList[i].y,pointList[j].x,pointList[j].y,True
		Next
		SetColor 255,255,255
	End Method
End Type


Local p:polygon = polygon.Create(4)

p.pointList[0].Set(100,100)
p.pointList[1].Set(500,100)
p.pointList[2].Set(500,500)
p.pointList[3].Set(100,500)



Graphics 800,600,0

While Not MouseHit(1)
	Cls
	
	p.pointList[2].Set(MouseX(),MouseY())
	p.Draw()
	If MouseHit(2) MoveMouse(500,500)
	DrawText "When this poly is perfectly square, area should equal 400*400 = 160000",10,10
	DrawText "Note! I've found it hard to move my mouse to make a perfect square, so RMB will move it to 500,500 :)",10,30
	DrawText "Area of poly = "+p.GetArea()+" pixels",10,60
	Flip
Wend
EndGraphics()


Tom

Thank you for your suggestion. Currently I only have very simple shapes, so you suggestion should work. But in the future I might to include more complex forms.

I have choosed another solution now:

I grab a little image from the screen each loop, for example 40x40 Pixels and check it for black pixels. This is very fast, in the next loop I will check the next 40x40 rectangle of the screen and so on.

When I reached the end of the screen I add all pixels counted in the previous loops and voilá I have the correct number of black pixels.

Of course this solution only works if you don't need the correct number in each loop, but it my case it's perfect.

You mean you grab a little Pixmap, not image? (GrabImage is grabpixmap + convert. For readout you would have to convert back to pixmap which is done through lockimage. So using GrabPixmap straight aways is much faster :))

@Dreamora:

yes, you're right!