📜 ⬆️ ⬇️

PNG - not gif!

Good day!
Have you ever wanted to know how PNG files are organized? Not? And I still tell.
The PNG (Portable Network Graphics) format was invented in 1995 to become a replacement for GIF , and as early as 1996, with the release of version 1.0, it was recommended by the W3C as a full-fledged network format. Today PNG is one of the main formats of web graphics.

Under the cat you will find a general description of the structure of the PNG-file, a number of picture schemes, preparation in the hex editor, and, of course, the link to the specification.

Common building


The PNG structure in its most general form is presented in the following figure.

That is, the file consists of a signature and a certain number of blocks (chunks, chunks), each of which carries some information (thanks to KO!). But why the signature can not be considered one of the chunks? Let's see more in detail.

File signature

The signature of the PNG file is always the same, consists of 8 bytes, and is (in hex)
89 50 4E 47 0D 0A 1A 0A

What does this mean?

')
Chunks

Chunks are the data blocks that make up a file. Each chunk consists of 4 sections.

Let's sort these sections in order.

Length

Well, everything seems to be clear with length. Just the numeric value of the length of the data block.

Type (name)

With type a bit more interesting. The type is 4 case sensitive ASCII characters. The character registers (the fifth bit in the numeric character entry) in the name of the chunk are not for nothing, these are flags that tell the decoder some additional information.

For a better understanding, let's look at the flags using the example of a chunk containing text.


Below is a list of chunk types with brief explanations.
Critical chunks


Auxiliary chunks

More information can be found in the specification.

CRC

CRC-32 checksum. By the way the other day was a topic about counting it in Windows.

Minimal PNG


With the overall structure sorted out. Now analyze the content of the mandatory chunks. But which of them are mandatory (not critical, critical are required to be recognized by the decoder, and not present in each file), and what does the minimal PNG file look like? That's how:


Ihdr

The data block in the IHDR contains the following fields:

Adam7 interlacing perfectly demonstrates a picture from Wikipedia (yes, GIF in the article about PNG):
image

IEND

Signals about the end of the file, the data block of this chunk does not contain anything.

IDAT

Contains the data encoded according to the compression method field in the header. The decoding algorithm is beyond the scope of this article (however, if desired, it may appear in the following), but it is described quite well (and in Russian) here .

Thus, the simplest PNG file (by the example of ) as follows.


Conclusion


When writing this article, I set myself the task of giving the reader general knowledge about the structure of the PNG file, for a deeper understanding it is recommended to read the specifications .

Topic on habre about the structure of JPEG: habrahabr.ru/blogs/algorithm/102521
Topic on habre about the structure of the GIF: habrahabr.ru/blogs/algorithm/127083

Thank you for your attention, I will be glad to any criticism!

Source: https://habr.com/ru/post/130472/


All Articles