#ifndef __W_PNG_H
#define __W_PNG_H

#include <string>
#include <png.h>
#include <iostream>

class PNGImage
{


public:
  std::string filename;
  png_structp png_ptr;
  png_infop   info_ptr;
  unsigned    width, height;
  png_byte  **image;
  png_byte  **row_pointers;
  FILE       *fp;
  int bytes_per_pixel;
  
public:
  // appelé pour charger une image..
  PNGImage(int _width=512, int _height=512) : fp(NULL), bytes_per_pixel(3)
  {
    std::cout << "Void cstor";
    width = _width;
    height = _height;
    row_pointers = new png_byte*[width*bytes_per_pixel];
    //std::cerr.rdbuf(0);
  }

  // constructeur que l'on va appeler pour creer une image
  PNGImage(const std::string& _filename, int _width, int _height)
    : filename(_filename),
      fp(NULL),
      png_ptr(NULL), info_ptr(NULL),
      width(_width), height(_height),
      bytes_per_pixel(3)
  {
    // allocation de ** image
    // [height][width*bytes_per_pixel]
    //std::cerr.rdbuf(0);

    image = new png_byte* [height];
    for (int i=0;i<height;i++)
        image[i] = new png_byte [width*bytes_per_pixel];

    row_pointers = new png_byte*[width*bytes_per_pixel];
  }

/*
  PNGImage(const PNGImage& _png)
  {
    filename = _png.filename;
    width    = _png.width;
    height   = _png.height;
    bytes_per_pixel = 3;

    memcpy(png_ptr,_png.png_ptr,sizeof(*(_png.png_ptr)));
    memcpy(info_ptr,_png.info_ptr,sizeof(*(_png.info_ptr)));

  }

  PNGImage& operator=(const PNGImage& _png)
  {
    filename = _png.filename;
    png_ptr  = _png.png_ptr;
    info_ptr = _png.info_ptr;
    width    = _png.width;
    height   = _png.height;
    bytes_per_pixel = 3;
    image    = _png.image;
  }*/

  ~PNGImage();

  bool openFileRead();
  bool openFileWrite();
  bool isPNG(int bytesToCheck);
  bool initReadStructs();
  bool initWriteStructs();
  bool writeHeader();

  png_byte** writeImage_Start();
  bool       writeImage_End();

  bool writeEnd();

  unsigned char **loadPNG(const std::string& fName, unsigned& size_x, unsigned& size_y);

};

#endif
