/*******************************************************/
/*                                                     */
/* program to reduce picture size by half(area by 1/4) */
/*                                                     */
/* to run :                                            */
/*  reduce {-x<input pic size>} infile outfile         */
/*                                                     */
/*******GJB 19/6/85*************************************/

#define PROGNAME "reduce"
#include <stdio.h>
short xsize=512,xsize_2=256;   /* pic file size          */
short header=1;/* set if pic file contains a header char */

FILE *fq = NULL ,*fp = NULL; /* pointer to output & input files */

main(argc,argv)
char **argv;
{
  argv++;

  while((argc>0)&&(*argv[0] == '-'))
  {
    switch((*argv)[1]) {
    case 'x':
      sscanf(*argv+2,"%hd",&xsize);
      if(xsize<0) error("bad pic size");
      header=(!header);
      break;
    default:
      error("bad option");
      break;
    }
    argc--;
    argv++;
  }

  if(--argc!=2) error("arg count");
  if((fp = fopen(*argv++,"r")) == NULL)
    error("can't open pic file");
  argc--;
  if((fq = fopen(*argv,"w")) == NULL)
    error("can't open output map file");

    do_headers();
    reduce();

    fclose(fp);
    fclose(fq);
}
  

error(s)
char *s;
{
  fprintf(stderr,"%s: %s\n",PROGNAME,s);
  exit(0);
}

do_headers()
{
 if(header) xsize = ((getc(fp)&0377));
 xsize_2=xsize/2;
 if(xsize_2<256) putc(xsize_2,fq);
}

reduce()
{
register short i;

  for(i=0;i<xsize_2;i++) {
    reduc_line();
    skipline();
  }
}

reduc_line()
{
register short i;

  for(i=0;i<xsize_2;i++) {
    putc( (getc(fp)&0377) , fq);
    getc(fp);
  }
}

skipline()
{
register short i;
  
  for(i=0;i<xsize;i++)
    getc(fp);
}
