Extracting Alpha channel from PNG to make a mask?

Just starting out? Need help? Post your questions and find answers here.
kawasaki
Enthusiast
Enthusiast
Posts: 182
Joined: Thu Oct 16, 2003 8:09 pm

Extracting Alpha channel from PNG to make a mask?

Post by kawasaki »

Hey folks,

I was wondering if anyone has any tried and tested procedure to extract the alpha channel of a PNG image, in order to create a mask?

It's a pretty urgent request, since I have been stomped with it for a while now.


Any help is very much appreciated.

Thankyou,

Mike
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

For straight transparency, no drop shadows or glows etc.? If so, I'll post something you can use.
BERESHEIT
kawasaki
Enthusiast
Enthusiast
Posts: 182
Joined: Thu Oct 16, 2003 8:09 pm

Post by kawasaki »

Hey Netmaestro,

What I essentially need is to be able to extract the Alpha channel of each pixel, so for instance if I were to process an image 32 by 32 pixels, the extracted data would be a 32x32 table, of the alpha of each pixel from 0 - 255.


Thanks,

Mike
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

@netmeastro
if in this process you'll reveal how to create an 8bit grayscale image with PB,
I would be really glad to find your example explainingly enough to learn how to do it in general,
to be able to create heightfields and such for OGRE right with PB.
oh... and have a nice day.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Here's a basic no-nonsense way of extracting the alpha bytes :

Code: Select all

Procedure GetAlphaBytes(image, bytes.b(2))
  Protected result, bmp.BITMAP, row, col, alpha.b, *px.LONG
  If image
    If GetObject_(image, SizeOf(BITMAP), bmp) And bmp\bmBitsPixel = 32
      For row = 0 To bmp\bmHeight-1
        *px = bmp\bmBits + row*bmp\bmWidthBytes
        For col = 0 To bmp\bmWidth-1
          alpha = *px\l>>24&$ff
          bytes(row, col) = alpha
          *px + SizeOf(LONG)
        Next
      Next
      result = #True
    EndIf
  EndIf
  ProcedureReturn result
EndProcedure


UsePNGImageDecoder()
LoadImage(1,"source.png")

Dim alphaBytes.b(ImageHeight(1)-1,ImageWidth(1)-1)

If GetAlphaBytes(ImageID(1), alphaBytes())
  MessageRequester("Heyho!", "The 2-dimensional alphaBytes() array now contains the alpha bytes for the image.")
EndIf
@Kaeru, is this any use ? http://www.purebasic.fr/english/viewtop ... =grayscale
I may look like a mule, but I'm not a complete ass.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

@srod
as far I see at first glance those are only routines to make a 32bit image greyvalued,
not to make it a 8bit grayscale image, or do I get this wrong?
an 8bit grayscale image (like you need for heightfields or AlphaSprites)
is an 8bit image without a color palette.
oh... and have a nice day.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

No color table?
I may look like a mule, but I'm not a complete ass.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

nope. the byteval directly is the grayval.
that's the problem, not every grafix program can write them.
... I would love to find a way creating them with PB.
oh... and have a nice day.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Make a new memory bitmap with the pixel format set to grey scale. Then read
the coloured pixels one by one, average the red, green and blue value and
make that number the greyscale intensity and write it to the corresponding
pixel location of the memory bitmap. Then save the memory bitmap.
Interesting approach... Maybe there is a DLL out there
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

well the problem is
Make a new memory bitmap with the pixel format set to grey scale.
I don't know how to make it with PB-only commands, or if it even is possible.
sure there are API solutions, but I would like to do it with CreateImage...
oh... and have a nice day.
kawasaki
Enthusiast
Enthusiast
Posts: 182
Joined: Thu Oct 16, 2003 8:09 pm

Post by kawasaki »

Hey Folks,


Thanks Srod, that was precisely what I was looking for. It is much appreciated :).


As for converting an image to greyscale, the technique I used to use would be to make a table containing the RGB values of each pixel (at the time I used the PointFast library), and then run through each pixel in turn, and pass the following equation;

Greyscale.b = (Red + Green + Blue) / 3

Thanks,

Mike
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

kawasaki wrote: Greyscale.b = (Red + Green + Blue) / 3
this is the technical average, it will not produce very realistic grayvalues,
because the three basecolors have different lumina.

the 'natural' grayvalue is
Gry = Red * 0.299 + Grn * 0.587 + Blu * 0.114
:wink:
oh... and have a nice day.
Post Reply