IO-PNG is a lightweight, object-oriented C++17 library for encoding and decoding PNG (Portable Network Graphics) images from scratch. It is designed with a focus on modern C++ practices, simplicity, and portability, offering full control over the PNG file format without relying on external imaging libraries like libpng.
The library correctly implements the PNG specification, including critical chunks, filtering, and DEFLATE compression, making it a powerful tool for projects that require direct, low-level control over PNG data streams.
- Core Philosophy
- Key Features
- Performance
- Prerequisites
- Building from Source
- Getting Started
- API Documentation
- Author
- License
The goal of IO-PNG was to create a PNG library that is simple, lightweight, and written in a modern, object-oriented style. Many existing libraries are C-based, heavy, or have complex build requirements. IO-PNG provides a powerful yet easy-to-integrate solution for developers who want to understand and manipulate the PNG format at a fundamental level.
- Modern C++ Design: Built with C++17, emphasizing an Object-Oriented paradigm with clear class responsibilities (
PNG,IHDR_CHUNK, etc.) and exception handling for robust error management. - No External Image Dependencies: Reads and writes PNG files from scratch. The only dependency is
zlibfor DEFLATE compression, which is a standard part of most systems. - High-Performance Encoding: Utilizes multi-threading to dynamically select the best scanline filter for each line of the image. This unique approach optimizes the time-to-compression ratio, resulting in smaller file sizes without a significant performance penalty.
- Cross-Platform Portability: Automatically handles big-endian and little-endian systems to ensure correct file I/O on any architecture.
- Full PNG Specification Support:
- Color Modes: Grayscale, Grayscale with Alpha, RGB, and RGBA.
- Bit Depths: Supports 8-bit and 16-bit per channel.
- Multi-IDAT Chunk Handling: Correctly reads and writes PNG files that split image data across multiple
IDATchunks. - Ancillary Chunk Support: Includes support for the
pHYs(Physical Pixel Dimensions) chunk.
- Compression Control: Allows developers to choose the compression level (from no compression to best compression) when saving files.
Before building the project, ensure you have the following installed:
- A C++17 compliant compiler (e.g., GCC, Clang, or MinGW for Windows).
- Make (for Linux/macOS).
- zlib development library.
- On Debian / Ubuntu:
sudo apt-get update sudo apt-get install build-essential zlib1g-dev
- On macOS (with Homebrew):
brew install gcc zlib
- On Windows:
Use a development environment like MinGW-w64 (installable via MSYS2). Ensure the
bindirectory of your toolchain is in your system'sPATH.
First, clone the repository:
git clone https://github.com/IgorGreenIGM/IO_PNG
cd IO_PNGUse the provided Makefile to build the project.
- Open a terminal in the project's root directory.
- Run
make:make
- The executable will be created at
bin/output.
To clean up build files:
make clean # Removes object files (*.o)
make mrproper # Removes object files and the final executableBatch scripts are provided for compiling with a MinGW-w64 toolchain.
- Open a Command Prompt (cmd) or PowerShell in the project's root directory.
- Step 1: Compile Sources
Run
compile.batto compile all.cppfiles into object files.compile.bat
- Step 2: Link Executable
Run
link.batto link the object files and create the final executable.link.bat
- The executable will be created at
bin\output.exe.
The project includes a simple main.cpp that demonstrates how to read a PNG file and write it back out.
- Place an input image named
origin.pngin the root directory of the project. - Build the project using the instructions above for your operating system.
- Run the executable:
- On Linux / macOS:
./bin/output
- On Windows:
.\bin\output.exe
- On Linux / macOS:
- Check the output: The program will read
origin.pngand create two new files:out.png: A copy of the original, re-encoded with the best compression.out2.png: A copy created using the PNG copy constructor.
This project is documented using Doxygen-style comments. You can generate a full HTML and LaTeX API reference.
- Ensure you have Doxygen installed.
- From the project root, run:
doxygen doxyfile
- The documentation will be generated in the
html/andlatex/directories. Openhtml/index.htmlin your browser to view it.
- @igorgreenIGM - Idea & Initial work
This project is licensed under the MIT License. See the LICENSE file for details.