Inside a PNG File: Signature, Structure and MIME Type

November 08, 2026 · PNG.now Editorial · PNG Essentials

Learn how PNG is identified and what its internal components mean.

The 8-Byte Signature

Every PNG file opens with an 8-byte signature, serving as both a magic number and a transmission-integrity check. The byte sequence is:

137 80 78 71 13 10 26 10 (decimal)
89 50 4E 47 0D 0A 1A 0A (hexadecimal)

The first byte (0x89) has the high bit set, preventing text-mode transfer systems from misinterpreting the file as a 7-bit ASCII stream. Bytes two through four (0x50 0x4E 0x47) encode the ASCII letters "PNG." The pair 0x0D 0x0A forms a Windows-style CRLF line ending, included to detect accidental newline conversion during FTP or other text-mode transfers. Byte seven (0x1A) is the DOS EOF marker (Ctrl-Z), and the final 0x0A is a Unix line feed. If any byte in this sequence is altered, the file is not a valid PNG, regardless of the remaining contents.

A decoder must verify the signature before processing any chunk. The W3C specification states: "Decoders that do not recognize the signature must not attempt to decode the datastream" (PNG 3.0, §3.1). A file that lacks this signature may be a renamed file, a truncated transfer, or a different format entirely.

Observable Validation

On a Unix-like system, the signature can be inspected with xxd or od:

xxd -l 8 image.png
# Expected: 00000000: 8950 4e47 0d0a 1a0a

A mismatch on any byte means the decoder must abort before attempting chunk parsing.


Mandatory Chunk Sequence

After the signature, a PNG file is a sequence of chunks. Each chunk consists of a 4-byte length (unsigned integer, network byte order), a 4-byte ASCII type code, the chunk data (length bytes), and a 4-byte CRC-32 computed over the type code and data. Three chunk types are mandatory and must appear in this order:

  • IHDR — image header, must be the first chunk
  • IDAT — image data, one or more consecutive chunks
  • IEND — image end, must be the last chunk

All other chunks are optional and may appear between IHDR and IEND, with the restriction that ancillary chunks must not appear between consecutive IDAT chunks.


IHDR: Fixed 13-Byte Image Header

The IHDR chunk contains exactly 13 bytes of data. Every field is fixed-width and must satisfy hard constraints.

Offset Field Size Constraints
0 Width 4 bytes Unsigned 32-bit, must not be zero
4 Height 4 bytes Unsigned 32-bit, must not be zero
8 Bit depth 1 byte 1, 2, 4, 8, or 16
9 Color type 1 byte 0, 2, 3, 4, or 6
10 Compression 1 byte Must be 0 (deflate/inflate)
11 Filter method 1 byte Must be 0 (adaptive filter with five types)
12 Interlace method 1 byte 0 (no interlace) or 1 (Adam7)

Valid Bit-Depth and Color-Type Combinations

Not every (bit depth, color type) pair is legal. The spec enforces these combinations (PNG 3.0, §4.1.1):

Color Type Description Allowed Bit Depths
0 Grayscale 1, 2, 4, 8, 16
2 RGB 8, 16
3 Indexed (palette) 1, 2, 4, 8
4 Grayscale + Alpha 8, 16
6 RGBA 8, 16

Color type 3 (indexed) never permits bit depth 16. A decoder encountering an invalid combination must treat the file as unreadable and abort.

Hard Limits

  • Maximum image dimensions: 2³¹−1 × 2³¹−1 pixels (unsigned 32-bit range). No practical decoder is required to handle images at this limit; the spec notes that decoders may impose smaller limits to prevent resource exhaustion.
  • Compression and filter method fields are reserved for future expansion. Any value other than 0 is an error condition.

IDAT: Compressed Scanlines with Per-Row Filters

All IDAT chunks together form a single zlib-compressed datastream. The decoder concatenates the data portions of every IDAT chunk in file order, then decompresses the result with the inflate algorithm. Splitting IDAT across multiple chunks carries no semantic meaning—it exists solely to support streaming encoders or memory-constrained decoders.

The decompressed output is not raw pixel bytes. It is a sequence of scanlines, each prefixed by a 1-byte filter type. The five filter types are:

Value Name Prediction
0 None Raw bytes, no transformation
1 Sub Difference from left pixel
2 Up Difference from pixel above
3 Average Average of left and above
4 Paeth Paeth predictor (adaptive 3-neighbor)

Filters operate on bytes, not pixels, and are applied independently to each scanline. A single image may use different filter types on different rows. Filtering reduces entropy before deflate compression and is not part of the compression algorithm itself. The decoder must reverse the filter for each row after decompression to reconstruct the original samples.


IEND: Zero-Length Terminator

The IEND chunk has a length field of 0, the type code IEND, and a CRC-32 computed over the type code alone. It contains no data. Its sole function is to mark the end of the PNG chunk sequence. A decoder that encounters IEND stops reading and renders the image. Any bytes following IEND are outside the PNG datastream and must be ignored or may generate a non-fatal warning.


MIME Type and File Extension

The registered MIME type is image/png. The file extension is .png. Both are defined in RFC 2083 and maintained in the IANA media types registry. In HTTP responses, the Content-Type header must use image/png. The non-standard image/x-png appears in some legacy systems but is not valid and must not be emitted by conformant software.

While the 8-byte signature is the definitive in-band identifier, the MIME type provides an out-of-band label for protocol handlers, email clients, and HTML user agents.


Hard Failure Conditions

A file that passes the signature check may still be structurally invalid. The following conditions require the decoder to abort:

  • IHDR absent or not first: The first chunk after the signature must be IHDR.
  • Zero dimensions: Width or height set to 0 in IHDR.
  • Invalid bit-depth/color-type combination: Any pair not listed in the valid combinations table.
  • Non-zero compression or filter method: Reserved values; no fallback behavior is defined.
  • Missing IDAT: At least one IDAT chunk must exist between IHDR and IEND.
  • CRC mismatch on any chunk: The CRC-32 must match the computed value over the type code and data. A mismatch indicates corruption.
  • Multiple IHDR chunks: Only one IHDR is permitted. A second IHDR anywhere in the stream is a structural error.
  • Unknown critical chunk: Chunk type codes with an uppercase first letter (bit 5 clear) are critical. If a decoder encounters a critical chunk it does not recognize, it must abort. Ancillary chunks (lowercase first letter) may be skipped safely.

These boundaries are absolute. The decoder must not attempt recovery or guess pixel layout, because the data is structurally undefined.


Ancillary Chunk Overview

Ancillary chunks carry optional metadata or rendering hints. They are never required to decode pixel values, but ignoring them may produce incorrect color or lose embedded information. Common ancillary chunks include:

  • PLTE: Palette table. Required for color type 3, optional for types 2 and 6. Contains up to 256 RGB triplets (3 bytes each). The chunk length must be divisible by 3.
  • tRNS: Transparency. For color types 0 and 2, specifies a single 16-bit gray or 16-bit RGB triplet that represents fully transparent. For color type 3, provides alpha values for palette entries.
  • gAMA: Gamma. A 4-byte unsigned integer equal to gamma × 100,000. Used to correct display brightness.
  • cHRM: Chromaticity. CIE 1931 xy coordinates for the primary colors and white point, each stored as a 4-byte unsigned integer equal to the value × 100,000.
  • sRGB: Standard RGB color space. A 1-byte rendering intent: 0 (perceptual), 1 (relative colorimetric), 2 (saturation), 3 (absolute colorimetric). When present, overrides gAMA and cHRM.
  • iTXt: International text. UTF-8 key-value pairs with optional language tag and compression flag. Preferred for metadata.
  • zTXt: Compressed text. Similar to iTXt but always compressed; Latin-1 encoding.
  • tEXt: Legacy text. Latin-1 key-value pairs. Deprecated in favor of iTXt.

Decoders must silently skip unrecognized ancillary chunks (PNG 3.0, §3.2). Encoders must not place ancillary chunks between consecutive IDAT chunks.


Reference