/** pgm.h - a little library to handle raw pgm images
 *
 * Originally designed by Anton Nokolaev, implemented by Efstathios
 * Hadjidemetriou for Shree Nayar's computer vision.  
 *
 * Modified into its current format by Paul Blaer <psb15@cs.columbia.edu>
 * Updated by Lenny Volchok <lv71@columbia.edu>
 *
 * Last Modified: 3/24/2001
 *
 **/

#ifndef _PGM_H
#define _PGM_H

/* macro to keep the value of A to the bounds L and H */
#ifdef CLAMP
#undef CLAMP
#endif
#define CLAMP(A,L,H)       ((A)<=(L) ? (L) : (A)<=(H) ? (A) : (H))

/* macro to swap 2 variables bit by bit */
#ifdef SWAP
#undef SWAP
#endif
#define SWAP(a,b) {a^=b; b^=a; a^=b;}

struct image {
  int rows;
  int columns;
  int colors;
  unsigned char * data;
};

typedef unsigned char uchar;

/* sets the size of the image to the given 
   height (# of rows) and width (# of columns);	
   returns 0 if OK or -1 if fails;*/
int setSize(struct image *im, int rows, int columns);

/*  returns the number of columns in the image;*/
int getNCols(struct image *im);

/*  returns the number of rows in the image;*/
int getNRows(struct image *im);

/*  sets the number of gray-level colors in the image
    (not counting 0);
    returns 0 if OK or -1 if fails;*/
int setColors(struct image *, int colors);

/*  returns the number of gray-level colors in the image;*/
int getColors(struct image *);

/*  sets the pixel in the image at row i and column j 
    to a particular color;*/   
int setPixel(struct image *im, int i, int j, int color);

/*  returns the color of the pixel in the image at row i and column j;*/	
int getPixel(struct image *im, int i, int j);

/* reads image from fname;
   returns 0 if OK or -1 if something goes wrong.*/
int readImage(struct image *im, const char *fname);

/* writes the image into fname;
   returns 0 if OK or -1 if something goes wrong.
*/
int writeImage(struct image *im, const char *fname);

#endif
