how to cut out a circle from image?

BlitzMax Forums/BlitzMax Beginners Area/how to cut out a circle from image?

puredave(Posted 2009) [#1]
Is there a simple/fast way to cut out/copy a circular shape from an image and use it as a sprite or store as a separate image?


slenkar(Posted 2009) [#2]
not simple or fast but the only way i can think of is to do grabpixmap and then black out pixels around the circle you want.


Dabhand(Posted 2009) [#3]
One way you could do it is by using a template image with two colours to specify the oval you want to cut/copy.

One colour for the actual circle, the other for the remainder of the image rectangle.

Then with a bit of jiggery pokery using the SetMaskColor before and after grabbing the pixmap, you'll have your round snippet! :)

SuperStrict
Graphics 640,480

'Mouse location variables
Local mx:Int, my:Int
'Set the masking color for the first bit to pink
SetMaskColor 255,0,255 

'Load the required images, first up, the background
Local background:TImage = LoadImage("Sunset.jpg") ' Taken from My Pictures/Sample Pictures
'Load up the 'snapper' circle, which contains only two colours, one for the oval, and one for the outer
'part of the circle.
Local snapperCircle:TImage = LoadImage("ovalSnap.bmp")
'Get the size of the image
Local snapperWidth:Int = ImageWidth(snapperCircle)
Local snapperHeight:Int = ImageHeight(snapperCircle)

'Variable to store if the user has clicked the mouse
Local clicked:Byte = False

'Change the cls color
SetClsColor 100,100,255

Repeat
Cls
'Pickup mouse location
mx = MouseX()
my = MouseY()

'Checked to see if the user has clicked the button
If clicked = False
	'If not, draw the background
	DrawImage background,0,0
	'Check for input
	If MouseHit(1)
		'Let the app know if the user has clicked the mouse
		clicked = True
		'Quickly draw the snapperCircle
		DrawImage snapperCircle,mx,my
		'Create a pixmap
		Local pixmap:TPixmap = CreatePixmap(snapperWidth,snapperHeight,PF_RGB888)
		'Grab the pixmap from the background
		pixmap = GrabPixmap(mx,my,snapperWidth,snapperHeight)
		'Set the mask colour to the same as the outer colour in the 'snapperCircle' TImage
		SetMaskColor 128,0,128
		'Load the pixmap into original 'snapperCircle' TImage
		snapperCircle = LoadImage(pixmap)
	End If
End If
'Draw the snapperCircle at the mouse location 
DrawImage snapperCircle,mx,my

Flip
Until KeyDown(KEY_ESCAPE)
End 


Download with media: http://dabz.syntaxbomb.com/Files/ovalSnap.zip

You may be able to resize the snapper template with scaling, or even use various pre-created template sizes, or build them in ze codez!

All it takes is to think outside the box, erm, I mean, circle! ;) hehehe

Dabz


puredave(Posted 2009) [#4]
nice!

i tried doing that, then i thought hmmmm what about a semicircle, or a quarter cirlce or any other shape!

all worked fine until i decided i wanted to rotate the image (to get the semicircle or quarter circle etc at any angle i wanted) and of course we all know what happens if you try that! it's a no go zone :/

doh!


ImaginaryHuman(Posted 2009) [#5]
I didn't know you could pass a pixmap into LoadImage() :-)


ImaginaryHuman(Posted 2009) [#6]
Another way would be to just grab a rectangular area and then draw black pixels on the OUTSIDE of the circle area over the top of it. There is code in the code archives by me for how to draw circles/ovals using interger math - you could just draw outside the circle rather than inside.