Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:PNG

From ReverseEngineering.wiki
Format
Developer PNG Development Group; W3C PNG Working Group
Uses Library:Zlib
Versions 1.0 (1996), 1.2 (1999), second edition / ISO/IEC 15948:2004 (2003), third edition (W3C Recommendation, 24 June 2025)
Magic 89 50 4E 47 0D 0A 1A 0A
Extensions .png, .apng
Endianness big
Website https://www.w3.org/TR/png-3/
On this wiki Facts · Code · Notes · Topics

Lossless raster image format (ISO/IEC 15948, W3C; the third edition of 2025 includes APNG): the signature 89 50 4E 47 0D 0A 1A 0A followed by big-endian chunks (length, type, data, CRC-32 of type and data). IHDR gives the size, bit depth, colour type and interlacing, the filtered scanlines form one zlib stream split across IDAT chunks, and every file ends with the 12-byte IEND chunk 00 00 00 00 49 45 4E 44 AE 42 60 82.

Overview

PNG (Portable Network Graphics; ISO/IEC 15948, W3C Recommendation, third edition 24 June 2025) is a lossless raster image format: an 8-byte signature followed by a sequence of chunks. All integers are big-endian (network order). The pixel rows are filtered one by one and compressed as a single Format:Zlib stream (Format:DEFLATE with a 2-byte header and an Adler-32 trailer) that is cut into one or more IDAT chunks. The third edition folded in animated PNG (APNG: acTL, fcTL, fdAT), the HDR chunks cICP, mDCV, cLLI, and eXIf.

The reference decoder is Library:Libpng, linked into a large share of native programs; others you meet in binaries include LodePNG, stb_image, libspng, the Rust png crate, Wuffs and the OS codecs (Windows WIC, Apple ImageIO). Games often embed plain PNG files in their archives, so the signature and the IEND trailer below are the first thing to search for in an unknown container.

Signature and chunk layout

Every PNG starts with 89 50 4E 47 0D 0A 1A 0A (\x89PNG\r\n\x1a\n). The bytes catch broken transfers: 0x89 (high bit set) catches 7-bit channels, 0D 0A catches CR LF to LF conversion, 1A stops type under MS-DOS, and the last 0A catches LF to CR LF conversion. libpng reports a mismatch in the first 4 bytes as "Not a PNG file" and a mismatch only in the last 4 as "PNG file corrupted by ASCII conversion".

Each chunk is:

Offset Size Field
0 4 length of the data, u32 BE, 0 to 2^31-1
4 4 chunk type, four ASCII letters
8 length data
8+length 4 CRC-32 of type + data (the length is not covered)

The CRC is the same reflected CRC-32 (polynomial 0xEDB88320) as zlib's crc32() and Format:Gzip#CRC-32. IHDR must come first and IEND last; PLTE comes before the first IDAT, and the IDAT chunks must be consecutive. IEND has no data, so every PNG ends with the same 12 bytes 00 00 00 00 49 45 4E 44 AE 42 60 82: searching for the signature and then this trailer carves PNG files out of memory dumps and unknown archives.

Chunk names

Bit 5 (0x20, lower case) of each of the four type bytes is a property flag:

Byte Upper case Lower case
1 critical (decoder must understand it) ancillary (may be skipped)
2 public (registered) private
3 reserved bit, must be upper case (invalid in this version)
4 unsafe to copy when the image is edited safe to copy

The APNG chunks acTL, fcTL and fdAT are registered but deliberately keep a lower-case second letter, so they read as "private" (the third edition says they are defined "as if" private for web compatibility). Programs often add their own private chunks; an unknown critical chunk (upper-case first letter) makes conforming decoders reject the file.

IHDR

13 bytes: width (u32), height (u32), bit depth (u8), colour type (u8), compression method (0 = deflate), filter method (0), interlace method (0 none, 1 Adam7). Width and height are 1 to 2^31-1.

Colour type Meaning Samples Allowed bit depths
0 greyscale 1 1, 2, 4, 8, 16
2 truecolour (RGB) 3 8, 16
3 indexed (PLTE) 1 1, 2, 4, 8
4 greyscale + alpha 2 8, 16
6 truecolour + alpha (RGBA) 4 8, 16

The colour type is a bit field: 1 palette, 2 colour, 4 alpha (libpng's PNG_COLOR_MASK_*). 16-bit samples are big-endian; sub-byte samples are packed most significant bit first.

Image data

The inflated IDAT stream is a list of scanlines, each one filter-type byte followed by ceil(width * bits_per_pixel / 8) bytes. The filters predict each byte from the byte one pixel to the left (a, at least one byte back), the byte above (b) and the byte above-left (c); outside the image they are 0:

Type Name Reconstruction
0 None x
1 Sub x + a
2 Up x + b
3 Average x + floor((a + b) / 2)
4 Paeth x + Paeth(a, b, c): p = a + b - c, take whichever of a, b, c is nearest to p, ties in that order

With interlace method 1 (Adam7) the image is stored as seven reduced images, each filtered on its own (the "previous row" restarts at zero for each pass). Pass (x start, y start, x step, y step): 1 (0,0,8,8), 2 (4,0,8,8), 3 (0,4,4,8), 4 (2,0,4,4), 5 (0,2,2,4), 6 (1,0,2,2), 7 (0,1,1,2). A pass with no pixels (narrow or short images) contributes no bytes at all, not even filter bytes. The inflated size is therefore height * (1 + ceil(width*bpp/8)) without interlacing, and the sum of that formula over the non-empty passes with it: PngSuite's 32x32 1-bit greyscale image is 160 bytes as basn0g01.png and 192 bytes as the interlaced ibasn0g01.png.

The zlib stream may be split between IDAT chunks at any byte; the chunk boundaries carry no meaning. The zlib window is at most 32 KiB; encoders such as libpng shrink the window size in the CMF byte for small images, so headers other than 78 xx (for example 18 D3 or 08 ..) are normal in PNG files (see Library:Libpng#Pitfalls).

Common ancillary chunks

Chunk Length Content
tRNS varies alpha for palette entries, or one transparent grey/RGB value
gAMA 4 gamma * 100000
cHRM 32 white point and primaries * 100000
sRGB 1 rendering intent
iCCP varies profile name, NUL, compression method 0, zlib-compressed ICC profile
cICP 4 colour primaries, transfer function, matrix coefficients (0), full-range flag (ITU-T H.273 codes)
mDCV 24 mastering display primaries, white point, max and min luminance
cLLI 8 MaxCLL, MaxFALL in units of 0.0001 cd/m2
tEXt varies keyword (1-79 Latin-1 bytes), NUL, text
zTXt varies keyword, NUL, compression method 0, zlib-compressed text
iTXt varies keyword, NUL, compression flag, method, language tag, NUL, translated keyword, NUL, UTF-8 text
bKGD 1, 2 or 6 background colour
pHYs 9 pixels per unit X, Y (u32), unit (1 = metre)
sBIT 1-4 significant bits per channel
tIME 7 last modification (year u16, month, day, hour, minute, second)
eXIf varies an Exif block (TIFF header II*\0 or MM\0*), before IDAT
sPLT, hIST varies suggested palette, palette histogram

APNG

acTL (8 bytes: num_frames, num_plays, 0 = loop forever) comes before the first IDAT. Each frame has one fcTL (26 bytes: sequence_number, width, height, x_offset, y_offset as u32, delay_num, delay_den as u16, dispose_op 0 none / 1 background / 2 previous, blend_op 0 source / 1 over; delay_den 0 means 1/100 s). Frame data after the first frame is in fdAT chunks: a 4-byte sequence number followed by data laid out like IDAT. fcTL and fdAT share one sequence counter that starts at 0 with no gaps. If an fcTL precedes the IDAT, the default image is the first frame; otherwise the default image is not part of the animation. Decoders without APNG support skip the ancillary chunks and show the default image, which is how APNG stays backward compatible. libpng 1.6 does not decode APNG (see Library:Libpng#Versions).

Identification

  • Signature 89 50 4E 47 0D 0A 1A 0A at offset 0, IEND trailer 49 45 4E 44 AE 42 60 82 at the end.
  • An IHDR chunk header 00 00 00 0D 49 48 44 52 right after the signature.
  • In executables: the decoder library (for libpng see Library:Libpng#Identification); chunk names compared as 32-bit integers (0x49484452 for IHDR, 0x49444154 for IDAT) appear byte-swapped (52 44 48 49) in little-endian code.

Code & repositories

  • https://github.com/pnggroup/libpng @ cd952f49f95bb27154ae77dbb103032d95f6e580 · license: libpng-2.0 (declared: the detector found none; check the repository)
    The official libpng source (tag v1.6.59; branches libpng16 for the 1.6 series, libpng18 for 1.8 with APNG, libpng12 and older). contrib/pngsuite holds Willem van Schaik's PngSuite conformance images (basic and interlaced) and contrib/testpngs images written by libpng's own makepng, including malformed 'crashers': good free test data for any PNG parser. License: PNG Reference Library License version 2 (LICENSE.md).
    Tested on: Read at tag v1.6.59 and at every v1.5.x/v1.6.x tag for the version markers; pngdump run over contrib/pngsuite and contrib/testpngs (166 images); not built (no C compiler on the test machine). · checked 2026-10-17; last activity 2026-10-12

Pitfalls

  • The CRC covers the type and the data, not the length; tools that "fix CRCs" after editing must recompute over type + data.
  • Palette images: compare decoded colours, not indices. PngSuite's basn3p02/ibasn3p02 and basn3p08/ibasn3p08 use different palettes for the same picture (index hashes differ, RGB hashes match).
  • Interlaced images: empty Adam7 passes have no filter bytes; a decoder that expects one row per pass misreads small images.
  • Unverified: Apple's iOS build tools write "crushed" PNGs (a private CgBI chunk before IHDR, raw deflate without the zlib header, BGRA byte order with premultiplied alpha) that standard decoders reject. Test: run a decoder that understands CgBI and one that does not on a PNG taken from an iOS app bundle.

Evidence & references

  • W3C PNG Specification (Third Edition), https://www.w3.org/TR/png-3/ : section 5.2 signature, 5.3 chunk layout, 5.4 chunk naming, 8.1 Adam7, 9 filters, 11.3.6 APNG chunks.
  • libpng source at https://github.com/pnggroup/libpng tag v1.6.59 (commit cd952f49f95bb27154ae77dbb103032d95f6e580): png.c (signature, png_check_IHDR), pngrutil.c (filters, Adam7 tables, chunk table), pngpriv.h (chunk names and property bits).
  • Checked with the wiki's pngdump decoder on the 60 PngSuite images and 106 libpng test images in libpng's contrib/ (all CRCs, filters 0-4, Adam7 at 1-16 bits; interlaced and non-interlaced versions decode to identical pixels) and on files written by hand.