#include <stdio.h>
#define progname "enlarge"

/* puts n thick blank border around image */

short asize=128 ;    /* pic dimensions */
short header=1;/* set if pic file contains a header char */
short n=11;     

FILE *fp, *fq = NULL;            /* input,output picture channels */
 
main(argc,argv)
char **argv;
{

  argv++;
  while ((argc>1)&&((*argv)[0] == '-'))
  {

    switch((*argv)[1]) {
    case 'x':
      sscanf(*argv+2,"%hd",&asize);
      if(asize<0) error("picture size");
      header=0;
      break;
    case 'n':
      sscanf(*argv+2,"%hd",&n);
      if(n<0) error("bad n");
      break;
    default:
      error("unknown flag");
      break;
    }
    --argc;argv++;
  }
 
  if (argc<3)
    error("missing arguments");
  else
  {
    if((fp = fopen(*argv++,"r")) == NULL)
      error("can't access input file");
    --argc;

    if((fq = fopen(*argv,"w")) == NULL)
      error("can't access output file");
  }
  convert_pic();
  fclose(fp);
  fclose(fq);
}
 
error(s)
char *s;
{
  fprintf(stderr,"%s: %s\n",progname,s);
  exit(1);
}
 
convert_pic()
{
long i,j;
short newsize;

  if (header)    asize=getc(fp)&0377;
  if (asize<0)   error("array size out of range");

  newsize=asize+n+n;
  printf("oldsize = %d, newsize = %d\n",asize,newsize);
  if(newsize<256)   putc(newsize,fq);

  for (i=0;i<n;i++)                           /* top edge blanks */
    for (j=0;j<newsize;j++) putc(0,fq);

  for (i=0;i<asize;i++) {
    for (j=0;j<n;j++) putc(0,fq);              /* left edge blanks */
    for (j=0;j<asize;j++) putc(getc(fp),fq);
    for (j=0;j<n;j++) putc(0,fq);              /* right edge blanks */
   }

  for (i=0;i<n;i++)                            /* bottom edge blanks */
    for (j=0;j<newsize;j++) putc(0,fq);
}
