// ==========================================================================
// $Id: pnm_image_io.h,v 1.1 2011/12/05 17:25:20 jlang Exp $
// Templated PNM Image IO class based on CSI2372 lab 5
// ==========================================================================
// (C)opyright:
//
//   Jochen Lang
//   SITE, University of Ottawa
//   800 King Edward Ave.
//   Ottawa, On., K1N 6N5
//   Canada. 
//   http://www.site.uottawa.ca
// 
// Creator: jlang (Jochen Lang)
// Email:   jlang@site.uottawa.ca
// ==========================================================================
// $Log: pnm_image_io.h,v $
// Revision 1.1  2011/12/05 17:25:20  jlang
// Adapted pnm_image_io to lecture 14
//
// Revision 1.2  2007/11/11 01:00:09  jlang
// Created solution to project assignment
//
//
// ==========================================================================
#ifndef PNM_IMAGE_IO_HH_
#define PNM_IMAGE_IO_HH_

#include <string>
#include <iostream>
#include <fstream>

using std::string;
using std::fstream;

/**
 * This class assumes that all pnm formats use a max value of 255
 * This is no longer true.
 */
template <typename T, int noChannels> class PNM_ImageIO { 
  fstream::openmode d_mode;
  int d_nRows;
  int d_nCols;
  string MAGIC_ASCII;
  string MAGIC_BIN;
  string DEFAULT_FILE_EXTENSION;

public:
  // redefine default constructor
  PNM_ImageIO();

  // set binary with true will switch output to binary output
  void setBinary( bool _bin);

  T* addAlpha( unsigned char *_image, 
	       int _nRows, int _nCols,
	       unsigned char _alpha=0 ); 

  unsigned char* removeAlpha( T *_image, 
			      int _nRows, int _nCols );
  
  bool write( string _fileName, 
	      unsigned char *_image,
	      int _nRows, int _nCols );

  unsigned char *read( string _fileName,
		       int &_nRows, int &_nCols );


protected:
  bool writeHeader( fstream &_outFile );

  bool parseHeader( fstream &_inFile );

  bool openFile(string _fileName, fstream& _outFile );
};


#endif // PNM_IMAGE_IO_HH


