Png++ aims to provide simple yet powerful C++ interface to libpng, the PNG reference implementation library. Png++ is free software distributed under a modified variant of BSD license.
png::image< png::rgb_pixel > image("input.png"); image.write("output.png");
The code reads an image from the file named input.png
, then writes the image to a file named output.png
. The image class template allows you to specify the desired pixel type for the image data. The available pixel types include: RGB, Grayscale and Indexed pixels. Some of the pixel types can have an alpha channel.
The png++ naturally supports reading PNG images of any color type into RGB or Grayscale pixel buffers (with optional alpha channel). This is particularly useful for an image viewer if it needs to display the PNG image on, for example, RGB device regardless of the image color type.
On the other hand one might want to read only images of particular type. With png++ you can specify it this way:
png::image< png::rgb_pixel > image("rgb.png", png::require_color_space< png::rgb_pixel >());
$ tar -zxf png++-0.2.x.tar.gz -C ~/src
$ cd ~/src/png++-0.2.x
make
to test how it's doing: $ make
example
directory. If everything goes well, try $ make test
make
check
which is the same as above) to run the test suite. If tests do not produce error messages then probably all is OK.$ make docs
doxygen
in the sources directory.root
and install png++ into your system. It's OK to issue make
install
under ordinary user permissions if you want to install png++ into your home directory. Run the following command: $ make install PREFIX=$HOME
~/include/png++
and documentation files to ~/share/doc/png++-0.2.x
. Without a PREFIX
png++ installs to /usr/local
.
#include <png++/png.hpp> //... png::image< png::rgb_pixel > image(128, 128); for (size_t y = 0; y < image.get_height(); ++y) { for (size_t x = 0; x < image.get_width(); ++x) { image[y][x] = png::rgb_pixel(x, y, x + y); // non-checking equivalent of image.set_pixel(x, y, ...); } } image.write("rgb.png");
Optionally, you may specify
image.set_interlace_type(png::interlace_adam7)
If you are writing an indexed colors image, you should provide a palette (colormap). One of the ways to do this is the following:
#include <png++/png.hpp> //... png::image< png::index_pixel > image; png::palette pal(256); for (size_t i = 0; i < pal.size(); ++i) { pal[i] = png::color(i, 255 - i, i); } image.set_palette(pal); ... image.write("palette.png");
It is not absolutely necessary to have the whole image data in memory in order to write a PNG file. You can use generator class template to write the image row-by-row. An example of this is provided in example/pixel_generator.cpp
bundled with the sources package.
The same holds for reading images too. You can use consumer class template in order to read the image data row-by-row. This might help in applications which have to deal with large PNG images but do not want to read the entire image into memory.
You can read or write images from/to generic IO stream, not only file on disk. Check out image::read(std::istream&)
, image::write(std::ostream&)
overloads in the reference manual.
$ g++ -c example.cpp `libpng-config --cflags`
and the following to link it:
$ g++ -o example example.o `libpng-config --ldflags`
When compiling you should add -I
$PREFIX/include
if you have installed png++ to non-standard location, like your home directory.
In your program, the line
#include <png++/png.hpp>
brings in all the header files in png++ which should be suitable for the most of the applications. You may include only the headers you really use, for example:
#include <png++/image.hpp> #include <png++/rgb_pixel.hpp>
If do not want to install png++ headers you still can compile your programs. Just create a subdirectory named png++
somewhere in your project tree and copy all of the .hpp files in png++ distribution there. Then use appropriate compiler options to add this directory into the header search path.
example/
directory of the source distribution. You may also find sources in the test/
directory insightful (well, somewhat).:-)
Released source packages can be found here: http://download.savannah.nongnu.org/releases/pngpp/
Also, you can check out sources directly from SVN repository: svn://svn.sv.nongnu.org/pngpp/trunk/ or http://svn.sv.nongnu.org/pngpp/trunk/ (for people w/o outgoing svn).
Online version of this documentation can be found here: http://www.nongnu.org/pngpp/doc/0.2.1/index.html
;-)
To report bugs, please use Savannah bug tracker: http://savannah.nongnu.org/bugs/?group=pngpp&func=additem
Do not forget to check if the bug was already filed. :-)
You can also contact me by dropping a mail to <alex.shulgin@gmail.com>.
Happy hacking!
Alex Shulgin