#!/usr/local/bin/php -q
<?

#$filename = 'ccd.txt';
$filename 'I.points';
#$filename = 'filters.txt';
#$filename = 'filters_1.txt';



function scale$x$ofsin$scale$ofsout ){

$rv = (( $x $ofsin ) * $scale ) + $ofsout;

$rv number_format$rv);

return( 
$rv );
}



$ofsx   767.1;
$scalex 0.278552;

$ofsy   622;
$scaley 0.097847;

$i fopen$filename'r' );

$count 0;
$HeaderValid true;

#echo "Process header\n";

$line fgets$i1024 );
while( !
feof$i ) and $count ) {
   
$line trim$line );
   
$exploded explode','$line );

   list( 
$x$y$mode$realx$realy ) = $exploded;
   echo 
"$count\t$x\t$y\t$realx\t$realy\n";

   switch( 
$count ) {
      case 
:
         list( 
$minx1$miny1$mode$realminx1$realminy1 ) = $exploded;
         break;

      case 
:
         list( 
$minx2$maxy1$mode$realminx2$realmaxy1 ) = $exploded;
         break;

      case 
:
         list( 
$maxx1$miny2$mode$realmaxx1$realminy2 ) = $exploded;
         break;

      case 
:
         list( 
$maxx2$maxy2$mode$realmaxx2$realmaxy2 ) = $exploded;
         break;

      }

   
$count ++;

   if( 
!= $mode ) {
      
$HeaderValid false;
      echo 
"Invalid button pressed in header line $count\n";
      }

   
$line fgets$i1024 );
   }


if( 
$realminx1 != $realminx2 ) {
   
$HeaderValid false;
   echo 
"actual min x values do not match\n";
   }

if( 
$realmaxx1 != $realmaxx2 ) {
   
$HeaderValid false;
   echo 
"actual max x values do not match\n";
   }

if( 
$realminy1 != $realminy2 ) {
   
$HeaderValid false;
   echo 
"actual min y values do not match\n";
   }

if( 
$realmayy1 != $realmayy2 ) {
   
$HeaderValid false;
   echo 
"actual may y values do not match\n";
   }


if( !
$HeaderValid ) {
   echo 
"Invalid Header\n\n";
   echo 
"The first four lines of the data file should be the points of\n";
   echo 
"the lower left, upper left, lower right and upper right points\n";
   echo 
"with button 1 pressed.  There must be exactly four entries with\n";
   echo 
"button 1, and they must be the first four entries.  Each of the\n";
   echo 
"entries must have the actual values that match the digitizer\n";
   echo 
"points appended.\n\n";
   exit;
   }


#echo "Past parse...  calculate values\n";

$rangex $maxx1 $minx1;
$realrangex $realmaxx1 $realminx1;
#$scalex = $rangex / $realrangex;
$scalex $realrangex $rangex;

echo 
"Range x: $rangex     Real Range x: $realrangex     Scale x: $scalex\n";


$rangey $maxy1 $miny1;
$realrangey $realmaxy1 $realminy1;
#$scaley = $rangey / $realrangey;
$scaley $realrangey $rangey;

echo 
"Range y: $rangey     Real Range y: $realrangey     Scale y: $scaley\n";


echo 
"\n";
echo 
"min x 1: $minx1\t$realminx1\t"scale$minx1$minx1$scalex$realminx1 ), "\n";
echo 
"min x 2: $minx2\t$realminx1\t"scale$minx2$minx1$scalex$realminx1 ), "\n";
echo 
"max x 1: $maxx1\t$realmaxx1\t"scale$maxx1$minx1$scalex$realminx1 ), "\n";
echo 
"max x 2: $maxx2\t$realmaxx2\t"scale$maxx2$minx1$scalex$realminx1 ), "\n\n";

echo 
"min y 1: $miny1\t$realminy1\t"scale$miny1$miny1$scaley$realminy1 ), "\n";
echo 
"min y 2: $miny2\t$realminy1\t"scale$miny2$miny1$scaley$realminy1 ), "\n";
echo 
"max y 1: $maxy1\t$realmaxy1\t"scale$maxy1$miny1$scaley$realminy1 ), "\n";
echo 
"max y 2: $maxy2\t$realmaxy1\t"scale$maxy2$miny1$scaley$realminy1 ), "\n\n";

$Count 0;
while( !
feof$i )) {
   
$line trim$line );
   
$exploded explode','$line );

#   echo "$line\n";

   
list( $x$y$mode$description ) = $exploded;


   if( 
!= $mode ) {
      echo 
"\n\n$description\n";
      }
   echo 
scale$x$minx1$scalex$realminx1 ), "\t",
        
scale$y$miny1$scaley$realminy1 ), "\n";

   
$line fgets$i1024 );
   }

fclose$i );

?>