/* Kurt Schwehr - NASA Ames Research Center - 8/10/94       */
/* schwehr@cs.stanford.edu                                  */
/* Program to swap byte pairs in a two bite image file.     */
/* I'm using this to bo between Big-endian and Little-      */
/* endian.  ( VAX vrs MIPS/SGI )  Slow, but it works.       */

/*
   The Mars DTM files I brought in from the usgs viking cdrom
   listed the data type at 16bit vax_integers.  pcs and vaxes have
   a different byte order than mips (sgi) machines in their integers 
   longer than one byte.  This program simple reads in each pair of
   bytes and writes them out to a different file.  The symtoms of
   the wrong byte order is having really strange and rapidly varying
   values in small areas on the image.
*/

#include <stdio.h>


void main() {

  char infile[255],img_outfile[255];
  FILE *InFile,*OutFile;
  int count=0;
  int buffer0,buffer1;

  printf ("Swap bytes for a 16bit image file.\n"); 
  printf ("File to read from:");
  scanf ("%s",infile);
  printf ("Img Output file:");
  scanf ("%s",img_outfile);

  InFile = fopen (infile,"r");

  /* Read/Write the trailing binary image */
  OutFile = fopen (img_outfile,"w");
  count=0;
  while ( (buffer0=getc(InFile)) != EOF ) {
    buffer1=getc(InFile);
    putc(buffer1,OutFile);
    putc(buffer0,OutFile);
    count++;
  }

  printf ("%d characters written\n",count);

  fclose (InFile);
  fclose (OutFile);
}
