Recent

Author Topic: How to get the information of a png file.  (Read 3051 times)

dcelso

  • Full Member
  • ***
  • Posts: 158
How to get the information of a png file.
« on: October 05, 2012, 10:06:43 am »
Hello to all,
I'm trying to get the color  depth of a loaded png with TImage.
I created with gimp sereval png images with color  depth equals to 1,4,8.
Lazarus seems to load it as a 32 bit image.
The next code always return "32"
Code: [Select]
function getBPP(var picture :TPicture) : Integer;
var
  tmp_pic : TPicture;
begin
 tmp_pic := TPicture.Create;
 tmp_pic.PNG:= TPortableNetworkGraphic.Create;
 if picture.Graphic.MimeType = tmp_pic.Graphic.MimeType then
 begin
     getBPP:= PIXELFORMAT_BPP[picture.PNG.PixelFormat];
 end;
 tmp_pic.Free;
end;
How can I obtain the real information of the png files that have contained in the file header?

I need to know when a png is a 1,4 or 8 or 32 bit image to do differents things.

Thanks to all.
« Last Edit: October 05, 2012, 10:35:46 am by dcelso »

HomePlaneR

  • New Member
  • *
  • Posts: 34
Re: How to get the information of a png file.
« Reply #1 on: October 10, 2012, 05:16:44 pm »
Hi.

Probably the image loader converts automatically png data to 32 bit image to faster representation.

To get the the real color depth you may have to use the direct access to the png file.
See the source code of PNGcomn. There are several chunks described, one of them is THeaderChunk that contains BPP value. Read PNG specification to find out the exact position of the chunk into the file and you will be able to get the BPP. It is quite hard way, but it could help...

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: How to get the information of a png file.
« Reply #2 on: October 10, 2012, 11:34:58 pm »
Somewheer in attached my small picslib.pp unit (used by LazEdit)  is a description of TPngHeader.
(I renamed it ot picslib.pp.txt to get th forum to accept it)
You can see how I read it and extract image size.
It also contains a filed for bitsperpixel.
Maybe it helps?

Bart

 

TinyPortal © 2005-2018