Alpha Channel
Blitz3D Forums/Blitz3D Programming/Alpha Channel
| ||
Does anybody know how to read alpha channel information from a png file? I need my application to be able to create a greyscale 'transparency map' from an image but I can't work out how to get at the required data. Loadimage seems to discard alpha information and loadtexture automatically scales images whose dimensions are not powers of two, so it seems I need to read straight from the file. Can anyone help? |
| ||
simply load as texture, with flag 2:tex=loadtexture("name.png",2) setbuffer texturebuffer(tex) lockbuffer() for y=0 to textureheight(tex)-1 for x=0 to texturewidth(tex)-1 argb=readpixelfast(x,y) alpha=(argb shr 24) next next unlockbuffer() Make sure it's a 32 Bit PNG. |
| ||
Uhm wait a minute, I just see what you said about the power of two scaling. Well you still could do the following: load as texture, then read the alphas as I just described. THEN shift the alpha channel to the blue channel, or to all 3 RGB channels, however. THEN create an image of the same size as the texture, then copyrect the texture to the image, then resize the image to the original size. Now you can access the alpha in the blue channel. Sounds complicated, but I guess it's a lot easier than unpacking a PNG, that is compressed with the huffman algorithm I think. The only problems I see are: - resizing an image is pretty slow. - no pixelperfect alpha channel due to resize. |
| ||
Thanks jfk. - resizing an image is pretty slow. I'm not too worried about speed. I expect any method to be fairly slow. - no pixelperfect alpha channel due to resize That's the problem right there. If I use, for example, a 600x600 image (or I guess even 513x513), it will be scaled up to 1024*1024 automatically. After scaling it back down again the data is quite badly degraded. I really need the original data for each pixel. I can load an alpha channel from a separate 8 bit png and apply it (using your own routine from the code arcs). This works fine but I can't really expect end users to create a separate image for the alpha. Too messy. If only loadtexture could be forced to tile or 'pad' a wrong-sized image instead of scaling it I would be sorted, but I don't think that's possible. EDIT - Is it my imagination, or does the 'preview' button now actually post what you've written? |
| ||
hey wow, preview! However, probably you should use a dll to reasd from the png. Wasn't there something, FreeImage.dll or something? It was capable of saving PNG, so maybe it can also read them. A further solution would be to use uncompressed TGA instead, they can be read easily. |
| ||
hey wow, preview! Have you not spotted that before? Its been there quite a while. Except now it seems to actually post instead of just previewing as it should. A further solution would be to use uncompressed TGA instead, they can be read easily. That might do. I forgot about tga as I always use png. How does one 'easily' read the alpha channel from a tga then? |
| ||
not sure. but I think I remember there's a save-TGA sourcecode in the archives somewhere. |
| ||
There is a `scaleimage_fast` routine in the archive. S'bliddy good! |