/* i/o for pictures, with input for maps 

MODIFIED 9/12/83 for use under apm integer only arithmetic
change MIRU format: now just <xsize> square data..

*/
 
 
#include <stdio.h>
#define progname "hex"

#define LINE 32                  /* line length */
 
 
short asize, area, asize_2 ;          /* pic dimensions */
char header = 1;                /* pic has header */
 
FILE *fp, *fq = NULL;                /* picture channels */
 
 
 
 
 
 
main(argc,argv)
char **argv;
{
char invert;
 
  invert=0;
  argv++;
  while ((argc>1)&&((*argv)[0] == '-'))
  {
    switch((*argv)[1]) {
    case '1':
      invert=1;
      break;
    default:
      error("unknown flag");
      break;
    }
    --argc;argv++;
  }
 
  if (argc<2)
    error("missing arguments");
  if((fp = fopen(*argv++,"r")) == NULL)
    error("can't access input file");
  --argc;
  if (argc<2)
    error("missing arguments");
  if((fq = fopen(*argv++,"w")) == NULL)
    error("can't access output file");
  --argc;

 
  if (invert)
    unhex();
  else
    enhex();

  fclose(fp);
  fclose(fq);
}
 
 
 
 
 
error(s)
char *s;
{
  fprintf(stderr,"%s: %s\n",progname,s);
  exit(1);
}
 


enhex()
{
  char eof;
  short c,i;

  for (eof=0;!eof;)
  {
    for (i=0;(i<LINE)&&(!eof);i++)
    {
      if (!(eof = (c=getc(fp))==EOF))
      {
        c &= 0377;
        putc('a'+(c&017),fq);
        putc('a'+(c>>4),fq);
      }
    }
    putc(' ',fq);
    putc('\n',fq);
  }
}
 
 


unhex()
{
  char eof;
  short c1, c2;

  for (eof=0;!eof;)
  {
    eof = ((c1=getc(fp))==EOF)||((c2=getc(fp))==EOF);
    if ((!eof)&&(c2!='\n'))
    {
      c1 = c1 - 'a';
      c2 = c2 - 'a';
      c1 += (c2<<4);
      putc(c1,fq);
    }
  }
}
 
