/* Kurt Schwehr - NASA Ames Research Center - 8/26/94       */
/* schwehr@cs.stanford.edu                                  */
/* This program corrects the elevation model for use in VISTA PRO
*/

/*
   This program takes in a 16 bit unsigned  value and apply this 
function:  correct elevation (in meters) = (2 * in_value) - 6000
The reason I'm writting this is that r.mapcalc gave me a 3 byte
output file.  We need 16 bit (2 byte) data for Vista Pro.

Reads in data an integer at a time, does the calculation, then
writes out the result.  Pretty Simple.

*/

#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;
  int mini,maxi, mino,maxo;
  double delta;
  signed short int     in_buffer, out_buffer;
  int test;

  printf ("Change mars elevation values to be true meters.\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");

  mini=mino= 10000000;
  maxi=maxo=-10000000;

  while (0 != fread (&in_buffer, sizeof (in_buffer), 1, InFile)) {
    
    out_buffer = (in_buffer * 2.0) - 6000;

    if (mini>in_buffer) mini = in_buffer;
    if (mino>out_buffer) mino = out_buffer;

    if (maxi<in_buffer) maxi = in_buffer;
    if (maxo<in_buffer) maxo = out_buffer;

    fwrite (&out_buffer, sizeof (out_buffer), 1, OutFile);
  }

  fclose (InFile);
  fclose (OutFile);
  printf ("Input:  Min=%d  Max=%d\n",mini,maxi);
  printf ("Output: Min=%d  Max=%d\n",mino,maxo);
  printf ("\n");
}
