Active Forums => Programs => Topic started by: SMcNeill on September 09, 2018, 02:05:02 pm
Title: PNG chunk information
Post by: SMcNeill on September 09, 2018, 02:05:02 pm
Since TerryRichie was asking how to get the background color from a PNG file, I thought I'd go ahead and take a few moments to write up a program to do that for us:
Test image is below, which has a yellow background set as found here: http://www.schaik.com/pngsuite/pngsuite_bck_png.html
Title: Re: PNG chunk information
Post by: TerryRitchie on September 09, 2018, 02:25:19 pm
My goodness! That's awesome. Thank you for taking the time to create this.
I've been reading about your personal struggles too. I'm thinking about you and hoping for the best Steve.
Title: Re: PNG chunk information
Post by: SMcNeill on September 09, 2018, 02:30:22 pm
PNGs are actually quite easy to sort out; everything is stored in set chunks of data.
4 bytes for data length 4 bytes for data type data of length given above 4 bytes for CRC check
Last chunk is always "IEND", so just DO.... LOOP and read each chunk as you're ready to deal with them. ;)
Title: Re: PNG chunk information
Post by: TerryRitchie on September 09, 2018, 05:30:01 pm
I'm not sure what is going on here. The image you supplied worked fine with the code, however whenever I try to bring it into my paint program the program freaks with it. It shows no image and complains there is "insufficient memory" when I try to do anything with it. Windows picture viewer shows it no problem though.
I then tried to use one of the PNGs I created with a clear background and the code fails to find it. I then downloaded a random picture from the Internet (dog.png below) and the code fails to find the background as well.
Is there something missing?
Title: Re: PNG chunk information
Post by: Petr on September 09, 2018, 05:40:52 pm
TerryRitchie: Your file contains none bKGD flag, Steve´s file it contains.
maybe try read tRNS chunk. Its also about transparency, if is one color transparent.
Title: Re: PNG chunk information
Post by: SMcNeill on September 09, 2018, 06:00:43 pm
TerryRitchie: Your file contains none bKGD flag, Steve´s file it contains.
Petr's correct; your file has no background color saved. "bKGD" is an optional chunk and you're not guaranteed that any file will have it. If it's there, it's easy to detect and extract. Hopefully Sprite Sheets you're using have it, otherwise you're going to have to manually find it.
Title: Re: PNG chunk information
Post by: TerryRitchie on September 09, 2018, 06:11:30 pm
So there are different versions/types of PNG files out there I take it, sort of like the Gif87/Gif89a.
I don't want a user of the library having to deal with PNG versions to make sure he/she has the correct type. I'll use the crappy detection routine I have for now. I'll need to investigate that document about PNG files for more info on the subject.
Thanks for your help though Steve. I definitely want to eventually use a solution such as yours.
Title: Re: PNG chunk information
Post by: SMcNeill on September 09, 2018, 06:33:02 pm
PNG's selling point is flexibility. All they *have* to contain is the Header, Pixel Data, End of Data tag; anything else is considered optional. Some hold text, like creation date/time, image creator, version, ect. Others contain dithering, palettes, compression, background data, ect...
What'd I do for a library like yours is to look for the background code first, and if available, go with it. If it's not found, then I'd manually look for the background color (or just ask the user to supply it). If it's there, you *know* it's right; if not, you have to make a "best guess" at the color.
Title: Re: PNG chunk information
Post by: Petr on September 10, 2018, 09:51:24 am
Hello. I tried to write a program for a more detailed PNG statement. It seems that 15 color pallets from 256 in the picture with the dog has some transparency, pallet number 1 is fully transparent, it should be the color _RGBA32 (71, 112, 76, 0) - palette 1 and RGBA32 (74, 65 , 50, 3) - palette 8
'PNG reader / read some (not all) information from PNG file. Source informations for this program are from http://www.fileformat.info/format/png/corion.htm