Overview
PNG (Portable Network Graphics) is a lossless raster image format that uses compression to reduce file size while preserving image quality. Understanding the binary structure is essential for this assignment.PNG file structure
Every PNG file consists of:PNG signature
The first 8 bytes of every valid PNG file are:Purpose of each byte
High bit set - detects systems that don’t handle 8-bit data
ASCII “PNG” - identifies the format
DOS-style line ending - detects DOS/Unix line ending conversion
DOS end-of-file character - stops file display under DOS
Unix-style line ending - detects Unix/DOS conversion
Validating the signature
Chunk structure
After the signature, a PNG file consists entirely of chunks. Each chunk has this structure:| Length (4 bytes) | Number of bytes in the data field |
| Type (4 bytes) | ASCII chunk type identifier |
| Data (variable) | Chunk payload (length specified above) |
| CRC (4 bytes) | Cyclic redundancy check |
Length field
- 4 bytes, big-endian unsigned 32-bit integer
- Specifies the number of bytes in the data field only
- Does NOT include the length, type, or CRC fields
- Can be zero (e.g., IEND chunk has no data)
Type field
- 4 ASCII characters (printable bytes)
- Case-sensitive
- Identifies the chunk’s purpose
Image Header - Must be first chunk, contains image dimensions and format
Palette - Color palette for indexed-color images (color type 3)
Image Data - Compressed pixel data (can appear multiple times)
Image End - Must be last chunk, marks end of file
Transparency - Transparency information
Background - Default background color
Data field
- Variable length (specified by length field)
- Content depends on chunk type
- Can be empty (length = 0)
CRC field
- 4 bytes, big-endian unsigned 32-bit integer
- Computed over the type and data fields (not length or CRC itself)
- Uses CRC-32 algorithm with polynomial 0xEDB88320
- Used to detect corruption
Big-endian encoding
Example: Converting 32-bit integers
Why endianness matters
If you forget to convert:Critical chunks
For this assignment, focus on these four chunk types:IHDR (Image Header)
Must be the first chunk. Contains 13 bytes of data:Image width in pixels (big-endian)
Image height in pixels (big-endian)
Bits per sample: 1, 2, 4, 8, or 16
- 0: Grayscale
- 2: RGB (Truecolor)
- 3: Indexed (Palette)
- 4: Grayscale + Alpha
- 6: RGB + Alpha
Must be 0 (deflate/inflate)
Must be 0 (adaptive filtering with five basic filter types)
0 = no interlace, 1 = Adam7 interlace
PLTE (Palette)
- Contains 1-256 RGB color entries
- Each entry is 3 bytes: Red, Green, Blue (0-255 each)
- Length must be divisible by 3
- Required for color type 3, optional for types 2 and 6
IDAT (Image Data)
- Contains compressed pixel data
- Uses zlib DEFLATE compression
- Multiple IDAT chunks can exist (concatenate before decompression)
- After decompression, data is organized into scanlines
- Each scanline starts with a filter type byte
IEND (Image End)
- Must be the last chunk
- Always has length 0 (no data)
- Marks the end of the PNG file
Examining PNG files
Using hex dump (hd)
Data
Bytes 16-28: 13 bytes of IHDR data
- Width:
00 00 01 40= 320 - Height:
00 00 01 40= 320 - Bit depth:
08= 8 - Color type:
03= Palette - Compression:
00 - Filter:
00 - Interlace:
00
Chunk boundaries
Notice how chunks follow each other:PNG specification reference
For complete details, see:Key takeaways
Essential concepts:
- PNG files start with an 8-byte signature
- All data after the signature is organized into chunks
- Each chunk has length, type, data, and CRC fields
- All multi-byte integers are big-endian
- IHDR must be first, IEND must be last
- CRC is computed over type + data fields
Next steps
Chunk reading
Learn how to read PNG chunks
CRC validation
Implement CRC-32 checksums