35#ifndef _PWP_PNGIMAGE_H_ 
   36#define _PWP_PNGIMAGE_H_ 
   56        PngImage (
int _width, 
int _height, 
int _bgcolor = 0xFFFFFF);
 
   67        void putPixel (
int x, 
int y, 
int color = 0x000000);
 
   81        void save (
const char *filename) 
const;
 
  104        imgWidth (_width), imgHeight (_height), buffer (0)
 
  107                throw "Wrong size of a PNG image requested.";
 
  112                static_cast<unsigned char> ((_bgcolor >> 16) & 0xFF);
 
  113        unsigned char green =
 
  114                static_cast<unsigned char> ((_bgcolor >> 8) & 0xFF);
 
  116                static_cast<unsigned char> (_bgcolor & 0xFF);
 
  117        unsigned char *buf = 
buffer;
 
  149        *(buf ++) = 
static_cast<unsigned char> ((color >> 16) & 0xFF); 
 
  150        *(buf ++) = 
static_cast<unsigned char> ((color >> 8) & 0xFF); 
 
  151        *(buf ++) = 
static_cast<unsigned char> (color & 0xFF); 
 
  160        return (buf [0] << 16) | (buf [1] << 8) | buf [2];
 
  176        imgWidth (0), imgHeight (0), buffer (0)
 
  181        FILE *fp = fopen (filename, 
"rb");
 
  183                throw "Unable to open a PNG file.";
 
  186        for (
int i = 0; i < 8; ++ i)
 
  188        fread (header, 1, 8, fp);
 
  189        if (png_sig_cmp (header, 0, 8))
 
  190                throw "The file does not contain a PNG image.";
 
  192        png_structp png_ptr = png_create_read_struct
 
  193                (PNG_LIBPNG_VER_STRING, 0, 0, 0);
 
  197                throw "Unable to create a PNG write struct.";
 
  199        png_infop info_ptr = png_create_info_struct (png_ptr);
 
  202                png_destroy_read_struct (&png_ptr, 0, 0);
 
  203                throw "Unable to create a PNG info struct.";
 
  206        if (setjmp (png_jmpbuf (png_ptr)))
 
  208                png_destroy_read_struct (&png_ptr, &info_ptr, 0);
 
  210                throw "An error occurred while reading the PNG file.";
 
  213        png_init_io (png_ptr, fp);
 
  214        png_set_sig_bytes (png_ptr, 8);
 
  216        int png_transforms = PNG_TRANSFORM_STRIP_16 |
 
  217                PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING |
 
  218                PNG_TRANSFORM_EXPAND | 
 
  220        png_read_png (png_ptr, info_ptr, png_transforms, 0);
 
  222        unsigned width = png_get_image_width (png_ptr, info_ptr);
 
  223        unsigned height = png_get_image_height (png_ptr, info_ptr);
 
  225        unsigned rowbytes = png_get_rowbytes (png_ptr, info_ptr);
 
  226        bool gray_image = (rowbytes == 
width);
 
  227        if (!gray_image && (rowbytes != 3 * 
width))
 
  229                throw "Wrong number of bytes per row in a PNG file.";
 
  232        png_bytep *row_pointers = png_get_rows (png_ptr, info_ptr);
 
  234        for (
unsigned row = 0; row < 
height; ++ row)
 
  236                unsigned char *rowBuffer = 
buffer + 3 * row * 
width;
 
  237                unsigned char *rowSource = row_pointers [row];
 
  240                        for (
int i = 
width; i > 0; -- i)
 
  242                                unsigned char byte = *(rowSource ++);
 
  243                                *(rowBuffer ++) = 
byte;
 
  244                                *(rowBuffer ++) = 
byte;
 
  245                                *(rowBuffer ++) = 
byte;
 
  250                        for (
int i = 3 * 
width; i > 0; -- i)
 
  251                                *(rowBuffer ++) = *(rowSource ++);
 
  255        png_destroy_read_struct (&png_ptr, &info_ptr, 0);
 
  272        FILE *fp = fopen (filename, 
"wb");
 
  274                throw "Unable to create a PNG file.";
 
  276        png_structp png_ptr = png_create_write_struct
 
  277                (PNG_LIBPNG_VER_STRING, 0, 0, 0);
 
  281                throw "Unable to create a PNG write struct.";
 
  283        png_infop info_ptr = png_create_info_struct (png_ptr);
 
  286                png_destroy_write_struct (&png_ptr, (png_infopp) 0);
 
  287                throw "Unable to create a PNG info struct.";
 
  290        png_bytep *row_pointers = 
new png_bytep [
imgHeight];
 
  294        if (setjmp (png_jmpbuf (png_ptr)))
 
  296                png_destroy_write_struct (&png_ptr, &info_ptr);
 
  297                delete [] row_pointers;
 
  299                throw "An error occurred while writing the PNG file.";
 
  302        png_init_io (png_ptr, fp);
 
  303        png_set_rows (png_ptr, info_ptr, row_pointers);
 
  305                8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
 
  306                PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
 
  308        png_write_png (png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, 0);
 
  310        png_destroy_write_struct (&png_ptr, &info_ptr);
 
  311        delete [] row_pointers;
 
An interface to the PNG library.
 
int imgHeight
The height of the image in pixels.
 
PngImage(int _width, int _height, int _bgcolor=0xFFFFFF)
The constructor of an empty 24-bit PNG image.
 
int width() const
Returns the width of the image.
 
PngImage & operator=(const PngImage &src)
The assignment operator is not allowed.
 
int height() const
Returns the height of the image.
 
int imgWidth
The width of the image in pixels.
 
void putPixel(int x, int y, int color=0x000000)
Plots a pixel in the image with the given RGB color.
 
void save(const char *filename) const
Writes the image to a PNG file.
 
unsigned char * buffer
The buffer of the picture in RGB byte sequences.
 
~PngImage()
The destructor.
 
int getPixel(int x, int y) const
Returns the 0xRRGGBB color value of the given pixel or -1 if the pixel is outside the image.