Page 1 of 1

Extracting Alpha channel from PNG to make a mask?

Posted: Fri Aug 22, 2008 7:41 pm
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

Posted: Fri Aug 22, 2008 8:49 pm
by netmaestro
For straight transparency, no drop shadows or glows etc.? If so, I'll post something you can use.

Posted: Fri Aug 22, 2008 9:12 pm
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

Posted: Fri Aug 22, 2008 9:30 pm
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.

Posted: Fri Aug 22, 2008 9:39 pm
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

Posted: Fri Aug 22, 2008 9:51 pm
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.

Posted: Fri Aug 22, 2008 9:52 pm
by srod
No color table?

Posted: Fri Aug 22, 2008 10:00 pm
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.

Posted: Fri Aug 22, 2008 10:10 pm
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

Posted: Fri Aug 22, 2008 10:28 pm
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...

Posted: Sat Aug 23, 2008 10:28 am
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

Posted: Sat Aug 23, 2008 12:09 pm
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: