Overview
Thepng_reader.h module provides functions for reading PNG files and extracting chunk information. It handles file opening, signature validation, chunk reading, and convenience functions for extracting specific chunk types.
Functions
png_open
0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A. If the signature is valid, the file pointer is returned positioned immediately after the signature, ready to read the first chunk.
File path to open
Returns a
FILE* pointer on success (positioned after signature), NULL on error (file not found, invalid signature, etc.)Example
Notes
- The PNG signature serves as a magic number to identify PNG files
- The signature includes specific byte values that help detect file corruption
- After successful opening, the file pointer is positioned at the start of the first chunk
png_read_chunk
File pointer (must be positioned at start of chunk length field)
Pointer to chunk structure where data will be written
Returns
0 on success, -1 on error (EOF, read error, CRC mismatch, memory allocation failure)Example
Memory Management
The chunk data is dynamically allocated. The caller must free it usingpng_free_chunk().
Notes
- All PNG chunks consist of 4 fields: Length (4 bytes), Type (4 bytes), Data (variable), CRC (4 bytes)
- The CRC is computed over the chunk type and data (not the length)
- Multi-byte integers are stored in big-endian format
- The function automatically validates the CRC and returns an error if it doesn’t match
png_free_chunk
Pointer to chunk structure to free
Example
Notes
- Only frees the
datafield of the chunk structure, not the structure itself - Safe to call multiple times on the same chunk
- Safe to call with NULL chunk pointer
png_extract_ihdr
File pointer (must be positioned after PNG signature)
Pointer to IHDR structure where parsed data will be written
Returns
0 on success, -1 on error (chunk not found, invalid chunk, parse error)Example
Notes
- The IHDR chunk must be the first chunk in a PNG file (after the signature)
- This function automatically handles chunk reading, parsing, and cleanup
- No need to call
png_free_chunk()- handled internally
png_extract_plte
File pointer (must be positioned after PNG signature)
Pointer to output pointer where allocated color array will be written
Pointer to output variable where color count will be written
Returns
0 on success, -1 on error (PLTE not found, parse error, or IDAT/IEND encountered before PLTE)Example
Memory Management
The caller is responsible for freeing the allocated color array usingfree().
Notes
- PLTE chunks are only present in color type 3 (palette) images
- According to PNG specification, PLTE must appear before the first IDAT chunk
- This function searches for PLTE by reading chunks sequentially
- Returns error if IDAT or IEND is encountered before finding PLTE
png_summary
Path to PNG file
Pointer to output pointer where allocated summary array will be written
Returns
0 on success, -1 on errorExample
Memory Management
The caller is responsible for freeing the summary array usingfree(). Note that the summary chunks have data = NULL (data is not stored in the summary).
Notes
- This function opens and closes the file internally
- The summary array contains chunk metadata but not actual chunk data
- Useful for displaying chunk information in
-s(summary) mode - The array is terminated when IEND chunk is encountered