[f87edd]: src / parser / tparser.c Maximize Restore History

Download this file

tparser.c    605 lines (498 with data), 14.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
* tparser.c
*
* Copyright Daniel M. German
*
* April 2007
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* Author: Daniel M German dmgerman at uvic doooot ca
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <assert.h>
#include "tparser.h"
#include "parser.h"
int yyparse(void) ;
int debug;
extern char *yytext;
extern pt_script script;
static FILE *file = NULL;
static int eof = 0;
static int nRow = 0;
static int nBuffer = 0;
static int lBuffer = 0;
static int nTokenStart = 0;
static int nTokenLength = 0;
static int nTokenNextStart = 0;
static int lMaxBuffer = PARSER_MAX_LINE;
static char buffer[PARSER_MAX_LINE+1];
char panoScriptParserDumpChar(char c)
{
if ( isprint(c) )
return c;
return '@';
}
void panoScriptParserDumpRow(void)
{
fprintf(stdout, "%6d |%.*s", nRow, lBuffer, buffer);
}
void panoScriptParserError(char const *errorstring, ...)
{
static char errmsg[1000];
va_list args;
int start=nTokenStart;
int end=start + nTokenLength - 1;
int i;
panoScriptParserDumpRow();
if ( eof ) {
fprintf(stdout, " !");
for (i=0; i<lBuffer; i++)
fprintf(stdout, ".");
fprintf(stdout, "^-EOF\n");
} else {
fprintf(stdout, " !");
for (i=1; i<start; i++)
fprintf(stdout, ".");
for (i=start; i<=end; i++)
fprintf(stdout, "^");
fprintf(stdout, " at line %d column %d\n", nRow, start);
}
/* print it using variable arguments -----------------------------*/
va_start(args, errorstring);
vsprintf(errmsg, errorstring, args);
va_end(args);
fprintf(stdout, "Error: %s\n", errmsg);
// exit
}
int panoScriptScannerGetNextLine(void)
{
char *p;
nBuffer = 0;
nTokenStart = -1;
nTokenNextStart = 1;
eof = FALSE;
p = fgets(buffer, lMaxBuffer, file);
if ( p == NULL ) {
if ( ferror(file) )
return -1;
eof = TRUE;
return 1;
}
nRow += 1;
lBuffer = strlen(buffer);
return 0;
}
// THis is the function that lex will use to read the next character
int panoScriptScannerGetNextChar(char *b, int maxBuffer)
{
int frc;
if ( eof )
return 0;
// read next line if at the end of the current
while ( nBuffer >= lBuffer ) {
frc = panoScriptScannerGetNextLine();
if ( frc != 0 )
return 0;
}
// ok, return character
b[0] = buffer[nBuffer];
nBuffer += 1;
if ( debug )
printf("GetNextChar() => '%c'0x%02x at %d\n",
panoScriptParserDumpChar(b[0]), b[0], nBuffer);
return b[0]==0?0:1;
}
void panoScriptScannerTokenBegin(char *t)
{
// REcord where a token begins
nTokenStart = nTokenNextStart;
nTokenLength = strlen(t);
nTokenNextStart = nBuffer; // + 1;
}
void yyerror (char const *st)
{
panoScriptParserError(st);
}
void panoParserDumpImage(pt_script_image *output)
{
printf(" projection %d\n", output->projection);
printf(" width, height %dx%d ", output->width, output->height);
printf(" fHorFOV %f\n", output->fHorFOV);
printf(" yaw %f ", output->yaw);
printf(" pitch %f ", output->pitch);
printf(" roll %f ", output->roll);
printf(" a %f", output->coef[0]);
printf(" b %f", output->coef[1]);
printf(" c %f", output->coef[2]);
printf(" d %f", output->coef[3]);
printf(" e %f", output->coef[4]);
printf(" g %f", output->coef[5]);
printf(" t %f\n", output->coef[6]);
printf(" name %s\n", output->name);
printf(" cropType %d ", output->cropType);
printf(" cropArea %i,%i,%i,%i ",
output->cropArea[0],
output->cropArea[1],
output->cropArea[2],
output->cropArea[3]);
printf(" morphToFit %d\n", output->morphToFit);
printf(" References: ");
printf(" fHorFOV %d", output->fHorFOVIndex);
printf(" yaw %d", output->yawIndex);
printf(" pitch %d", output->pitchIndex);
printf(" roll %d", output->rollIndex);
printf(" a %d", output->coefIndex[0]);
printf(" b %d", output->coefIndex[1]);
printf(" c %d", output->coefIndex[2]);
printf(" d %d", output->coefIndex[3]);
printf(" e %d", output->coefIndex[4]);
printf(" g %d", output->coefIndex[5]);
printf(" t %d\n", output->coefIndex[6]);
}
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;
}
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;
file = NULL;
if (!panoScriptParserReset() ) {
// This is really an assertion
fprintf(stderr, "This parser is not reentrant");
exit(1);
}
file = fopen(filename, "r");
if ( file == NULL ) {
return NULL;
}
if ( panoScriptScannerGetNextLine() == 0 ) {
if (yyparse() ==0) {
// duplicate data structure to return
scriptVar = calloc(1, sizeof(*scriptVar));
if (scriptVar == 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 {
goto error;
}
}
else {
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) if ((a) != NULL) free(a)
pt_script *ptr = *scriptVar;
// Free pt_script_pano first
FREE(ptr->pano.projectionParmsString);
FREE(ptr->pano.outputFormat);
for (i=0;i<ptr->iOutputImagesCount;i++) {
FREE(ptr->outputImageSpec[i].name);
}
for (i=0;i<ptr->iInputImagesCount;i++) {
FREE(ptr->inputImageSpec[i].name);
}
FREE(ptr->outputImageSpec);
FREE(ptr->inputImageSpec);
FREE(ptr->morphingPointsSpec);
#undef FREE
free(ptr);
*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;
}