/* Kurt Schwehr - NASA Ames Research Center - 8/23/94       */
/* schwehr@cs.stanford.edu                                  */
/* Program to stretch out Mars topo images to fill out a 
   square region.
*/

/*
   Due to how the earth curves, in a lat/long projection,
   an image has less width as you go N or S from the
   equator towards the poles.  Grass is not liking this in
   ll projection.  So I'm going to stretch these images
   all the way to the edges where the normally are zeros
   for padding.  It's taking a huge amount of effort to
   use i.rectify2 to do a good 3rd order polynomial 
   transform on an image.  I don't know how well this will
   work or if it's a valid method.

   This program assumes a file has already been through split
   and swap if they were needed!!  ONLY works for 2 BYTE integer
   data files!!!!!!
*/

#include <stdio.h>
#include <math.h>

void main() {

  char infile[255],outfile[255];
  FILE *InFile,*OutFile;
  int count=0;
  int lines,samples;
  int cur_line;
  int leading,trailing;
  int index;

  double delta;

  unsigned short int     in_buffer[5120], out_buffer[5120];

  printf ("Stretch an image to the edge over the padding zeros.\n"); 
  printf ("Input File:");
  scanf ("%s",infile);
  printf ("Output file:");
  scanf ("%s",outfile);
  printf ("LINES:");
  scanf ("%d",&lines);
  printf ("LINE_SAMPLES:");
  scanf ("%d",&samples);

  InFile = fopen (infile,"r");
  OutFile = fopen (outfile,"w");

  /* Loop through for each line in the image raster file */
  for (cur_line=0;cur_line<lines;cur_line++) {
    if ( (cur_line % 20) == 0) {
      fprintf (stderr,"Processing Line %d\r",cur_line);
    }

    /* Read a raw data line */
    count = fread(in_buffer,sizeof(in_buffer[0]),samples,InFile);
    /* printf ("Read %d\n",count); */


    /* Count leading and trailing zeros */
    leading=trailing=0;
    while ( (leading<samples) && (in_buffer[leading]==0) ) {
      leading++;
    }
    while ( (trailing<samples) && (in_buffer[samples-trailing]==0) ) {
      trailing++;
    }
    /*printf ("Leading: %d    Trailing: %d\n", leading, trailing); */


    /* Resample Data */
    if ( ((leading+trailing)<samples) && (leading!=0 || trailing!=0) ) {
      delta = (double)(samples-(leading+trailing)) / (double)samples;
      /* printf ("Delta=%0.3f\n",delta); */
      for (index=0;index<samples;index++){
	/* Linear Interpolation */
	if ((int)floor(index*delta) != (int)ceil (index*delta)) {
	  out_buffer[index] = (unsigned short int) ( 
	     ((in_buffer[leading+(int)floor(index*delta)]) * ((ceil(index*delta) - (index*delta)))
	     +
	     ((in_buffer[leading+(int)ceil (index*delta)]) * ((index*delta) - floor(index*delta))))
						  );
      } else {
	out_buffer[index] = in_buffer[leading+(int)ceil (index*delta)];
      }
	/*
	   printf ("%u<=(%u-%u) [%.0f] [%.0f] %.2f\n", out_buffer[index], 
	   in_buffer[leading+(int)floor(index*delta)], in_buffer[leading+(int)ceil (index*delta)],
	   leading+floor(index*delta),leading+ceil (index*delta),leading+index*delta);
	*/
      }
    } else {
      /* printf ("Didn't Need to resample this line\n"); */
      for (index=0;index<samples;index++) {
	out_buffer[index] = in_buffer[index];
      }
    }

    /* Patch Ends for Safety */
    /*out_buffer[0]        =in_buffer[leading];
    out_buffer[samples-1]=in_buffer[samples-trailing-1];*/
    /* off by one?? */

    /* Write a resampled line */
    count = fwrite(out_buffer,sizeof(out_buffer[0]),samples,OutFile);
    /* printf ("Wrote %d items\n",count); */
  }


  fclose (InFile);
  fclose (OutFile);
  printf ("\n");
}
