Extracting Alpha channel from PNG to make a mask?
Extracting Alpha channel from PNG to make a mask?
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
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
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
Here's a basic no-nonsense way of extracting the alpha bytes :
@Kaeru, is this any use ? http://www.purebasic.fr/english/viewtop ... =grayscale
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
I may look like a mule, but I'm not a complete ass.
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
- Rook Zimbabwe
- Addict
- Posts: 4322
- Joined: Tue Jan 02, 2007 8:16 pm
- Location: Cypress TX
- Contact:
Interesting approach... Maybe there is a DLL out thereMake 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.
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
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
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
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany