/* strippgm.c	Strip pgm image file into raw image file */

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  FILE *PgmFile;
  FILE *RawFile;

  char header[80];
  
  /* parse command line */
  if (argc!=3) {
    fprintf(stderr, "Invalid number of command line arguments!");
    fprintf(stderr, "\nUsage: strippgm pgm_file raw_file\n");
    exit(1);
  }
  else
    if ( !(PgmFile=fopen(argv[1], "rb")) || !(RawFile=fopen(argv[2], "wb")) ) {
      fprintf(stderr, "Can't open one of the files!\n");
      exit(1);
    }

  /* read the header and see if it's really a PGM file */
  fgets(header, 4, PgmFile);
  if (strcmp(header, "P5\n")) {
    fprintf(stderr, "Input file is not in pgm format!\n");
    exit(1);
  }
  while (fgetc(PgmFile)!='\n');
  while (fgetc(PgmFile)!='\n');
  while (fgetc(PgmFile)!='\n');
  while (!feof(PgmFile))
    fputc(fgetc(PgmFile), RawFile);

  /* close the files */
  fclose(PgmFile);
  fclose(RawFile);
}