Read Alpha value of an image
BlitzMax Forums/BlitzMax Beginners Area/Read Alpha value of an image
| ||
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. |
| ||
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.. |
| ||
Other way could be: load image with alpha and without alpha (masked or different sprite). So you could use CollideImage. bye Ron |
| ||
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. |
| ||
@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 |
| ||
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 |
| ||
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 |
| ||
Thanks, I will stick to this for now. If to much memory will be used than I will go to mask. Thank you guys. |