Recent

Author Topic: Reading alpha channel value from PNG  (Read 6303 times)

pefrik

  • Newbie
  • Posts: 4
Reading alpha channel value from PNG
« on: September 23, 2009, 08:36:59 pm »
Hi, I'm struggling with one big problem(for me at least).
For my program I used PNG image wich is being loaded to TPortableNetworkGraphic.
And now I don't know how to read alpha channel from TPNG... :-/

I've already tried with
Code: [Select]
PNG.RawImage.ReadChannelsbut I don't know if it's good way and actually I don't know how to use "TRawImagePosition"...

I've tried also with
Code: [Select]
png.Canvas.Colors[x,y].red
png.Canvas.Colors[x,y].green
png.Canvas.Colors[x,y].blue
png.Canvas.Colors[x,y].alpha
but now alpha value for every pixel is 255... :-/

I just don't have any other ideas.
Thanks for help

--- edit ---

Ok, I solved that problem :-)


« Last Edit: September 24, 2009, 03:54:50 pm by pefrik »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Reading alpha channel value from PNG
« Reply #1 on: February 05, 2010, 07:35:30 pm »
Bump on old post, sry.

But you said that you solved the problem and i'm really curious how you did it. I have tried almost everything without success so far trying to get r,g,b,a values from PNG and JPG formats... from TPortableNetworkGraphic and TJpegImage. canvas.colors[x,y] works for BMP but not for these.

Edit: Finally! Don't know why i didn't notice ReadChannels() in your post, it however worked. But only after i used image that had alpha channel. Source of function gives hints on how to use RawImage though.
« Last Edit: February 05, 2010, 07:45:33 pm by User137 »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Reading alpha channel value from PNG
« Reply #2 on: February 06, 2010, 03:41:34 pm »
And also it was worth noting that colors from ReadChannels() come in format of word, so to get your usual RGBA (0..255) you have to divide result by 256. Image looked very weird without it. Also, you are using position variable that contains byte and bit value. Consider the whole loading process going by bits instead of bytes and it worked.
Code: [Select]
ny:=RawImage.Description.BitsPerLine*y;
n2:=ny+x*RawImage.Description.BitsPerPixel;
rp.Byte:=n2 div 8;
rp.Bit:=n2 mod 8;
RawImage.ReadChannels(rp,r,g,b,a);
r:=r div 256;
g:=g div 256;
b:=b div 256;
a:=a div 256;
« Last Edit: February 06, 2010, 03:45:48 pm by User137 »

 

TinyPortal © 2005-2018