Diff of /APSCpp/APSCpp.c [03d2ea] .. [d9c482] Maximize Restore

  Switch to side-by-side view

--- a/APSCpp/APSCpp.c
+++ b/APSCpp/APSCpp.c
@@ -23,8 +23,6 @@
 #include "saInterp.h"
 #include <math.h>
 #include <tiffio.h>
-
-extern double globFov;  // hfov if no project file
 
 // create empty
 pDIinfo DIinfo_new0(){
@@ -350,10 +348,13 @@
   18 July 2009 new stgfov argument
     <0 forbids reprojection
 	>=0 allows reprojection when hfov exceeds this value
+
+  26 July 2009 add argument globFov, used when pdi is null.
+
 */
 ///#define _DEBUG_INTERP
 KeypointXMLList * GenerateKeyspp( char * imgname, int maxdim, DIinfo * pdi, 
-								  double stgfov, int * KPcount )
+								  double stgfov, int * KPcount, double globFov )
 {  
 #ifdef _DEBUG_INTERP
 	char tmp[262];
@@ -367,6 +368,8 @@
 	KeypointXMLList* kpp;
 
 	double Scale = 1.0;
+	double xcen, ycen;	// scaled center
+
 	double hfov = 0;
 
 
@@ -390,8 +393,8 @@
 
 	pW = pic->width;
 	pH = pic->height;
-	Xcen = 0.5 * pW;
-	Ycen = 0.5 * pH;
+	xcen = Xcen = 0.5 * pW;
+	ycen = Ycen = 0.5 * pH;
 
 
 // convert to monochrome float and discard input image
@@ -409,7 +412,7 @@
   also convert to stereographic if pdi is valid
 */
   // prescale factor
-	if( maxdim > 0) Scale = max( pW, pH ) / (double) maxdim;
+	if( maxdim > 0) Scale = ( pW > pH ? pW : pH ) / (double) maxdim;
 	if( Scale <= 1 ) Scale = 1;
 
  // interpolate if necessary
@@ -420,15 +423,19 @@
 		int dwid = pW, dhgt = pH;
 	// reduced dimensions
 		if( Scale > 1 ){
-			dwid = (int)(0.5 + pW / Scale );
-			dhgt = (int)(0.5 + pH / Scale );
-			Scale = (double) pW / (double) dwid;	// exact
-		WriteLine("  reduce size to %d x %d ...", dwid, dhgt );
+		// scaled dimensions
+			dwid = (int)( pW / Scale ); xcen = 0.5 * dwid;
+			dhgt = (int)( pH / Scale ); ycen = 0.5 * dhgt;
+		// exact scale factor
+			if( pW > pH ) Scale = (double) pW / (double) dwid;
+			else Scale = (double) pH / (double) dhgt;
+		WriteLine("  reduce to %d x %d (Scale %.4f)...", dwid, dhgt, Scale );
 		// smooth the source for antialiasing
 			ImageMap* tmp = ImageMap_GaussianConvolution( picMap, 0.75 * Scale );
 			ImageMap_delete( picMap );
 			picMap = tmp;
 		}
+	
 	// create result map
 		pn = ImageMap_new( dwid, dhgt );
 
@@ -443,6 +450,8 @@
 		  prm = saRemap_new( psp, pdp );
 		  saRemap_inv ( prm, 0.5 * dwid, 0.5 * dhgt, &Xcen, &Ycen );
 		  WriteLine("  convert to stereographic projection ...");
+		  CamLens_delete( psp );
+		  CamLens_delete( pdp );
 		}
 	// interpolate
 		saInterpSetup((void *)picMap->values,	// void * pdata
@@ -460,7 +469,10 @@
 			for( y = 0; y < pn->yDim; y++ ){
 				X = (double) x; Y = (double) y;		
 				if( prm ) saRemap_inv ( prm, X, Y, &X, &Y );	// ++
-				else { X *= Scale; Y *= Scale; }				// legacy
+				else { 		// linear rescale...
+				// note no need to subtract and re-add center, that just adds noise
+					X *= Scale; Y *= Scale;
+				}
 				saInterp_float( &(pd[y]), X, Y );
 			}
 		}
@@ -468,6 +480,7 @@
 		ImageMap_delete( picMap );
 		picMap = pn;
 		saRemap_delete( prm ); prm = 0;
+
 #ifdef _DEBUG_INTERP
 	strcpy(tmp, imgname);
 	strcat(tmp,"-MAP-PROJ.TIF");
@@ -479,6 +492,7 @@
 
 
 /* find the features 
+  Note feature point coords are at source scale
 */
 	WriteLine("  find keypoints ..." );
 
@@ -488,30 +502,33 @@
 	WriteLine("  %d keypoints found\n", nKeys );
 	*KPcount = nKeys;
 
- /* build the return value
-     for ANN mode a list of Keypoint, 
-	 that will be converted to keypointN after matching
+ /* build the return value.  For APSCPP this is a list of keypoint (float
+    feature vector) not of keypointN (char feature vector).  The feature
+	detector returns KP coordinates at source scale, so here all we need
+	to do is undo the stereographic projection if one was done.
  */
   {
     
 	ArrayList * globalNaturalKeypoints = 0;
 	int i;
-
-	if( pdi ){ // build remapping function at input scale
-	  CamLens * psp, * pdp;
-	  psp = CamLens_new1( pW, pH, pdi->format, hfov, 16.0 );
-	  pdp = CamLens_new1( pW, pH, _stereographic, hfov, 0 );
-	  prm = saRemap_new( psp, pdp );	// dest=>src
+	if( pdi && stgfov >= 0 && hfov >= stgfov ){ 
+	// build remapping function at source scale
+		CamLens * psp, * pdp;
+		psp = CamLens_new1( pW, pH, pdi->format, hfov, 16.0 );
+		pdp = CamLens_new1( pW, pH, _stereographic, hfov, 0 );
+		prm = saRemap_new( psp, pdp );	// dest=>src
+		CamLens_delete( psp );
+		CamLens_delete( pdp );
 	} else prm = 0;
 
 	for(i=0; i < ArrayList_Count(lf->globalKeypoints); i++) {
 		Keypoint* kp = (Keypoint *) ArrayList_GetItem( lf->globalKeypoints, i );
 		if( prm ){		// remap coordinates to image projection
 			saRemap_inv ( prm,
-						  kp->x , kp->y ,	// scale to image size
-						  &kp->x, &kp->y		// remapped coords
+						  kp->x , kp->y ,	
+						  &kp->x, &kp->y	
 						);
-		}
+		} 
 	}
 
   // package the list