Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Diff of /src/parser/tparser.c [6295f8] .. [f87edd] Maximize Restore

  Switch to side-by-side view

--- a/src/parser/tparser.c
+++ b/src/parser/tparser.c
@@ -34,7 +34,7 @@
 #include "tparser.h"
 #include "parser.h"
 
-
+int yyparse(void) ;
 
 int debug;
 
@@ -53,24 +53,20 @@
 static int lMaxBuffer = PARSER_MAX_LINE;
 static char buffer[PARSER_MAX_LINE+1];
 
-void panoPrintError(const char * c)
-{
-    fprintf(stderr,"%s\n",c);
-}
-
-char panoParserDumpChar(char c) 
+
+char panoScriptParserDumpChar(char c) 
 {
     if (  isprint(c)  )
 	return c;
     return '@';
 }
 
-void panoParserDumpRow(void) 
+void panoScriptParserDumpRow(void) 
 {
     fprintf(stdout, "%6d |%.*s", nRow, lBuffer, buffer);
 }
 
-void panoParserError(char const *errorstring, ...) 
+void panoScriptParserError(char const *errorstring, ...) 
 {
     static char errmsg[1000];
     va_list args;
@@ -79,7 +75,7 @@
     int end=start + nTokenLength - 1;
     int i;
     
-    panoParserDumpRow();
+    panoScriptParserDumpRow();
     
     if (  eof  ) {
 	fprintf(stdout, "       !");
@@ -106,7 +102,7 @@
 }
 
 
-int getNextLine(void) 
+int panoScriptScannerGetNextLine(void) 
 {
   char *p;
   
@@ -130,7 +126,7 @@
 }
 
 // THis is the function that lex will use to read the next character
-int GetNextChar(char *b, int maxBuffer) 
+int panoScriptScannerGetNextChar(char *b, int maxBuffer) 
 {
   int frc;
   
@@ -139,7 +135,7 @@
   
   // read next line if at the end of the current
   while (  nBuffer >= lBuffer  ) {
-    frc = getNextLine();
+    frc = panoScriptScannerGetNextLine();
     if (  frc != 0  )
       return 0;
     }
@@ -150,11 +146,11 @@
 
   if (  debug  )
     printf("GetNextChar() => '%c'0x%02x at %d\n",
-                        panoParserDumpChar(b[0]), b[0], nBuffer);
+                        panoScriptParserDumpChar(b[0]), b[0], nBuffer);
   return b[0]==0?0:1;
 }
 
-void panoScannerTokenBegin(char *t) 
+void panoScriptScannerTokenBegin(char *t) 
 {
     // REcord where a token begins
     nTokenStart = nTokenNextStart;
@@ -165,7 +161,7 @@
 
 void yyerror (char const *st)
 {
-    panoParserError(st);
+    panoScriptParserError(st);
 }
 
 void panoParserDumpImage(pt_script_image *output)
@@ -210,20 +206,166 @@
     
 }
 
-int panoParseReset(void)
-{
-    if (file != NULL) 
+void panoScriptDump(pt_script *script)
+{
+    int i;
+    pt_script_image *output;
+
+    assert(script != NULL);
+    
+    printf("Values\n");
+    printf("Pano .................");
+    printf(" width,height %dx%d ", script->pano.width, script->pano.height);
+    printf(" projection %d ", script->pano.projection);
+    printf(" name %s ", script->pano.projectionName);
+    printf(" fHorFOV %f ", script->pano.fHorFOV);
+    printf(" format %s ", script->pano.outputFormat);
+    printf(" proj parms %s\n", script->pano.projectionParmsString);
+    printf(" proj parms count %d\n", script->pano.projectionParmsCount);
+    for (i=0;i<script->pano.projectionParmsCount;i++) {
+	printf(" proj parm %d: %f\n", i, script->pano.projectionParms[i]);
+    }
+    
+    printf("Optimize ..................");
+    printf("  gamma %f", script->optimize.fGamma);
+    printf("  interpolator %d", script->optimize.interpolator);
+    printf("  fastFT %d ", script->optimize.fastFT);
+    printf("  humber %d \n", script->optimize.humberEstimator);
+    
+    printf("Output  [%d] images ..................\n", script->iOutputImagesCount);
+    
+    for (i=0;i<script->iOutputImagesCount;i++) {
+	output = &script->outputImageSpec[i];
+	printf(" image %d\n", i);
+	panoParserDumpImage(output);
+    }
+    printf("Input  [%d] images ..................\n", script->iInputImagesCount);
+
+    for (i=0;i<script->iInputImagesCount;i++) {
+	output = &script->inputImageSpec[i];
+	printf(" image %d\n", i);
+	panoParserDumpImage(output);
+    }
+
+}
+
+
+
+int panoScriptParserReset(void)
+{
+    if (file != NULL)  {
 	return FALSE;
+    }
     /* There should not be anything allocated in script */
     bzero(&script, sizeof(script));    
+
+    //but some parameters are meaningful when zero
+    script.pano.projection = -1;
     return 1;
 }
 
-
-pt_script *panoParseScript(char *filename)
-{
+int panoScriptParseProjectionParms()
+{
+    char *ptr;
+    float temp;
+    printf("Projecton parms [%s]\n", script.pano.projectionParmsString);
+
+    if (script.pano.projectionParmsString != NULL) {
+		// We got parameters which we need to parse
+	ptr = script.pano.projectionParmsString;
+	while (*ptr != '\0') {
+	    if (*ptr == ' ' || *ptr  == '\t')
+		ptr++;
+	    else {
+		if (script.pano.projectionParmsCount >= PANO_PARSER_MAX_PROJECTION_PARMS) {
+		    panoScriptParserError("Too many parameters for projection %s (limit is %d)", script.pano.projectionParmsString, PANO_PARSER_MAX_PROJECTION_PARMS);
+		    return FALSE;
+		}
+		if (sscanf(ptr, "%f", &temp) != 1) {
+		    panoScriptParserError("Illegal floating point number in projection parameters %s", script.pano.projectionParmsString);
+		    return FALSE;
+		}
+		script.pano.projectionParms[script.pano.projectionParmsCount] = temp;
+		script.pano.projectionParmsCount++;
+	    }
+	}
+    } 
+    return TRUE;
+    
+}
+
+int panoScriptDeReferenceImageVariables(pt_script_image *images, int imagesCount)
+{
+    // if the index != 0 and the indexed image is within bounds, and the indexed image is not a reference...
+    // then set the imageindex, and clear the reference
+#define UNLINK_VARIABLE(i,a,r) (ref= images[i].r-1,  \
+				(ref >= 0          &&	\
+				 ref < imagesCount &&	\
+				 images[ref].r == 0 ?	\
+				 (images[i].a = images[ref].a,		\
+				  images[i].r = 0,			\
+				  change=1)\
+				 :1\
+				 ))
+    
+#define UNLINK_VARIABLE2(a,b,c)
+
+    int change = 1;
+    int ref;
+    int i,j;
+    // dereferrence until we are done
+    while (change) {
+	change = 0;
+	for (i=0;i<imagesCount;i++) {
+	    UNLINK_VARIABLE(i,fHorFOV,fHorFOVIndex);
+	    UNLINK_VARIABLE(i,yaw,yawIndex);
+	    UNLINK_VARIABLE(i,roll,rollIndex);
+	    UNLINK_VARIABLE(i,pitch,pitchIndex);
+	    for (j=0;j<PANO_PARSER_COEF_COUNT;j++) {
+		UNLINK_VARIABLE(i,coef[j],coefIndex[j]);
+	    }
+	}
+    }
+#undef UNLINK_VARIABLE
+    char *names[7] = {"a","b","c","d","e","g","t"};
+
+#define VERIFY_VARIABLE(i,r,m)  if (images[i].r!=0) {panoScriptParserError("Unable to resolve reference for variable %s in image %i\n", m, i);return FALSE;}
+
+    // Verify that all links are resolved
+    for (i=0;i<imagesCount;i++) {
+	VERIFY_VARIABLE(i,fHorFOVIndex,"v");
+	VERIFY_VARIABLE(i,yawIndex, "y");
+	VERIFY_VARIABLE(i,rollIndex, "r");
+	VERIFY_VARIABLE(i,pitchIndex, "p");
+	for (j=0;j<PANO_PARSER_COEF_COUNT;j++) {
+	    VERIFY_VARIABLE(i,coefIndex[j], names[j]);
+	}
+    }
+#undef VERIFY_VARIABLE
+
+    return TRUE;
+}
+
+int panoScriptDeReferenceVariables(pt_script* script)
+{
+    // function that dereferences variables in scripts.
+    if (!panoScriptDeReferenceImageVariables(script->inputImageSpec, script->iInputImagesCount))
+	return FALSE;
+    if (!panoScriptDeReferenceImageVariables(script->outputImageSpec, script->iOutputImagesCount))
+	return FALSE;
+    return TRUE;
+}
+
+
+pt_script *panoScriptParse(char *filename, int deferenceVariables)
+{
+    // filaname: input file
+
+    // deferenceVariables: should references to other variables be resolved?
+
     pt_script *scriptVar;
-    if (!panoParseReset() ) {
+    file = NULL;
+    if (!panoScriptParserReset() ) {
 	// This is really an assertion
 	fprintf(stderr, "This parser is not reentrant");
 	exit(1);
@@ -232,41 +374,64 @@
     if (  file == NULL  ) {
 	return NULL;
     }
-    if ( getNextLine() == 0 ) {
+    if ( panoScriptScannerGetNextLine() == 0 ) {
 	if (yyparse() ==0) {
 	    // duplicate data structure to return
 	    scriptVar = calloc(1, sizeof(*scriptVar));
 	    if (scriptVar == NULL) {
-		panoPrintError("Not enough memory");
-		return NULL;
+		panoScriptParserError("Not enough memory");
+		goto error;
+	    }
+
+	    // AT THIS POINT WE HAVE FINISHED PARSING
+
+	    // This is the best time to verify input values
+	    // and to parse some of the optional parameters
+
+	    if (!panoScriptParseProjectionParms() ) {
+		panoScriptParserError("Illegal parameters to projection");
+		goto error;
+	    } 
+
+	    // Deference if required
+	    if (deferenceVariables) {
+		printf("REquestion referencing of variables .......................\n");
+		if (!panoScriptDeReferenceVariables(&script)) 
+		    goto error;
 	    }
 	    // Just making sure..
+	    fclose(file);
 	    assert(sizeof(*scriptVar) == sizeof(*scriptVar));
 	    memcpy(scriptVar, &script, sizeof(*scriptVar));
 	    return scriptVar;
+
 	} else  {
-	    return FALSE;
+	    goto error;
 	}
-	    
     }
     else  {
-	panoPrintError("Input file is empty");
-	return NULL;
-    }
-    close (file);
-    
-}
-
-void panoParseDispose(pt_script **scriptVar)
+	panoScriptParserError("Input file is empty");
+	goto error;
+    }
+    // It should never reach here
+    assert(0); 
+ error:
+    if (file != NULL)
+	fclose(file);
+    return NULL;
+    
+}
+
+void panoParserDispose(pt_script **scriptVar)
 {
     int i;
     // free all the data structures in it before freeing the variable itself
 
-#define FREE(a) ((a) != NULL, free(a))
+#define FREE(a) if ((a) != NULL) free(a)
     pt_script *ptr = *scriptVar;
     
     // Free pt_script_pano first
-    FREE(ptr->pano.projectionParms);
+    FREE(ptr->pano.projectionParmsString);
     
     FREE(ptr->pano.outputFormat);
 
@@ -286,3 +451,154 @@
     *scriptVar = NULL;
 }
 
+// ACCESSORS
+
+float panoScriptGetImageCoefA(pt_script_image *pImage)
+{
+    assert(pImage != NULL);
+    return pImage->coef[0];
+}
+
+float panoScriptGetImageCoefB(pt_script_image *pImage)
+{
+    assert(pImage != NULL);
+    return pImage->coef[1];
+}
+
+float panoScriptGetImageCoefC(pt_script_image *pImage)
+{
+    assert(pImage != NULL);
+    return pImage->coef[2];
+}
+
+float panoScriptGetImageCoefD(pt_script_image *pImage)
+{
+    assert(pImage != NULL);
+    return pImage->coef[3];
+}
+
+float panoScriptGetImageCoefE(pt_script_image *pImage)
+{
+    assert(pImage != NULL);
+    return pImage->coef[4];
+}
+
+float panoScriptGetImageSheerX(pt_script_image *pImage)
+{
+    assert(pImage != NULL);
+    return pImage->coef[5];
+}
+
+float panoScriptGetImageSheerY(pt_script_image *pImage)
+{
+    assert(pImage != NULL);
+    return pImage->coef[6];
+}
+
+int panoScriptGetImageCropType(pt_script_image *pImage)
+{
+    assert(pImage != NULL);
+    return pImage->cropType;
+}
+
+
+int panoScriptGetImageMorphToFit(pt_script_image *pImage)
+{
+    assert(pImage != NULL);
+    return pImage->morphToFit;
+}
+
+
+int panoScriptGetInputImagesCount(pt_script *script)
+{
+    assert(script != NULL);
+    return script->iInputImagesCount;
+}
+
+
+int panoScriptGetOutputImagesCount(pt_script *script)
+{
+    assert(script != NULL);
+    return script->iOutputImagesCount;
+}
+
+pt_script_image *panoScriptGetInputImage(pt_script *script, int i)
+{
+    assert(script != NULL);
+    if (i > panoScriptGetInputImagesCount(script)) {
+	return NULL;
+    }
+    return &(script->inputImageSpec[i]);
+}
+
+pt_script_image *panoScriptGetOutputImage(pt_script *script, int i)
+{
+    assert(script != NULL);
+    if (i > panoScriptGetOutputImagesCount(script)) {
+	printf("NONE********\n");
+	return NULL;
+    }
+    return &(script->outputImageSpec[i]);
+}
+
+int panoScriptGetPanoProjection(pt_script *script)
+{
+    return script->pano.projection;
+}
+
+int panoScriptGetPanoWidth(pt_script *script)
+{
+    return script->pano.width;
+}
+
+int panoScriptGetPanoHeight(pt_script *script)
+{
+    return script->pano.height;
+}
+
+float panoScriptGetPanoHFOV(pt_script *script)
+{
+    return script->pano.fHorFOV;
+}
+
+float panoScriptGetPanoParmsCount(pt_script *script)
+{
+    return script->pano.projectionParmsCount;
+}
+
+float panoScriptGetPanoParm(pt_script *script, int index)
+{
+    assert(index < PANO_PARSER_MAX_PROJECTION_PARMS);
+    return script->pano.projectionParms[index];
+}
+
+char *panoScriptGetPanoOutputFormat(pt_script *script)
+{
+    return script->pano.outputFormat;
+}
+
+int panoScriptGetImageProjection(pt_script_image *pImage)
+{
+    return pImage->projection;
+}
+
+
+
+
+float panoScriptGetImageHFOV(pt_script_image *pImage)
+{
+    return pImage->fHorFOV;
+}
+
+float panoScriptGetImagePitch(pt_script_image *pImage)
+{
+    return pImage->pitch;
+}
+float panoScriptGetImageYaw(pt_script_image *pImage)
+{
+    return pImage->yaw;
+}
+float panoScriptGetImageRoll(pt_script_image *pImage)
+{
+    return pImage->roll;
+}