#include <stdio.h>

/* Writes out a PGM Ascii format file */
/* This format can be read by XV on the unix systems */
/* and Paint shop Pro on a PC */

int
WriteImage( char *outfile, int rows, int cols, unsigned char *pixeldata )
{
        int             err_status;
        int             i, j;
        FILE            *fp;

  err_status = 0;

  if ( (fp = fopen(outfile, "w")) == NULL)
  {
     err_status = 1;
  }
  else
  {
     fprintf(fp, "P2\n");
     fprintf(fp, "%i\n", cols);
     fprintf(fp, "%i\n", rows);
     fprintf(fp, "255\n");           /* Magic number */

     for (j=0; j<rows; j++)
     {
        for (i=0; i<cols; i++)
        {
           fprintf(fp, "%i\n", pixeldata[j*cols+i]);
        }
     }

     fprintf(fp, "\n");
  }


  return (err_status);   

}
