Handling PNG images in C++
This blog post is about png++, one of my favourite C++ libraries.
It’s a wrapper of libpng in C++ that allows to handle PNG images so easily that you’ll never want to use bare libpng again.
All you need to start is to add these two lines in you source files:
#include "png++/png.hpp" using namespace png;
This is the code to open an existing image:
The code to make a new image from scratch differs only in the parameter passed to image’s constructor:
The image class has the get_pixel() and set_pixel() memeber functions to access the individual pixels:
rgb_pixel pixel=img.get_pixel(0,0); pixel.red=min(255,pixel.red+10); //Increase red color of pixel img.set_pixel(0,0,pixel);
The image height and width can be obtained with intuitive get_height() and get_width() memeber functions:
The image can be written to file with the write() memebr function:img.write("new_image.png");
Also, this library is header-only. What does it means? That you do not have to compile the library to start playing with it, just exptract the library in the folder where you have your source code, and include the header file “png.hpp”, just that.
Lastly, here is an example code including CMake scripts to show the capabilities of the library. png++.tar.gz