Read Alpha value of an image

BlitzMax Forums/BlitzMax Beginners Area/Read Alpha value of an image

Ravl(Posted 2012) [#1]
Ok, this must be an easy one. for a few days I searched the google, but I cannot find the answer.

I load in my scenes from 40 to 60 objects which are TImage s. Every object is a sprite representing an object (eg: bottle, glass, etc.), which also have a shadow. I am interested to see check what is the Alpha value of the sprite I click. I need this to see if I click on the shadow or on the object. The shadow will always have alpha < than 255 and this will help me a lot.

I only was able to check with collision detection if I click on the image or not.


Ravl(Posted 2012) [#2]
edit:

here it is what I use now:

Method objectInfo()
Local tempPixmap:TPixmap = LockImage (ho[0].Image)

p = ReadPixel (tempPixmap , MouseX() - ho[0].PosX, MouseY() - ho[0].PosY)
getPixelAlpha(p)
End Method

Function getPixelAlpha(pixelValue%)
A=(pixelValue Shr 24)&$FF
EndFunction

Is this the correct way? I mean I must use this methods in realtime..


Derron(Posted 2012) [#3]
Other way could be:

load image with alpha and without alpha (masked or different sprite).
So you could use CollideImage.


bye
Ron


Oddball(Posted 2012) [#4]
Ravl, your way is perfectly fine, but if the image isn't dynamic you don't need lockimage every time. Just do it once and store the pixmap along with the image. Otherwise your app will recreate the texture every time you use lockimage.


Ravl(Posted 2012) [#5]
@Oddball:

I used this:
In my HiddenObject type I have this method
	Method Load()
		Image = LoadImage("graphics\items\" + objectImageName)
		Pixmap = LockImage (Image)
	End Method


and in my click method this:


		pix = ReadPixel (ho[objectID].Pixmap , MouseX() - ho[objectID].objectPosX , MouseY() - ho[objectID].objectPosY )			
		Local alphaValue = getObjectAlpha (pix)	
		Return alphaValue



and is working fine, BUT I have a question.
When should I use UnlockImage?

After I finished the scene and the objects dissapear?

Last edited 2012


Oddball(Posted 2012) [#6]
There is no need to UnlockImage as that method does nothing, literally, as there is no code in the method. But if you are worried about it put it immediately after you have obtained the pixmap.
	Method Load()
		Image = LoadImage("graphics\items\" + objectImageName)
		Pixmap = LockImage (Image)
		UnlockImage (Image)
	End Method


Last edited 2012


Derron(Posted 2012) [#7]
If you are concerned about the memory usage (each image is stored as pixmap too):

you could write bitmap-masks in rgb-channels. -> One pixmap can held 3 images (or you do some kind of spritemap).

Instead of using "ReadPixel" you should try if imagescollide has better performance.
Some years ago I decided to use ImagesCollide for checking what state of a country is hovered. ReadPixel was lowering the fps in a not acceptable way (it depends on the alignment of your "images" on the screen, whether their rects overlap, ...).


bye
Ron


Ravl(Posted 2012) [#8]
Thanks,

I will stick to this for now. If to much memory will be used than I will go to mask.

Thank you guys.