[a07dce]: tools / PTmender.c Maximize Restore History

Download this file

PTmender.c    411 lines (315 with data), 11.0 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
/*
* PTmender
*
* Based on the program PTStitcher by Helmut Dersch.
*
* Dec 2005
*
* 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
*
*/
// TODO
// Create_Panorama requires some floating point assembly to be interpreted
#define __DEBUG__
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#ifndef _MSC_VER
#include <unistd.h>
#else
#include "compat_win32/getopt.h"
#endif
#include "tiffio.h"
#include "filter.h"
#include "panorama.h"
#include "PTmender.h"
#include "PTcommon.h"
#include "file.h"
#include "ColourBrightness.h"
// Global variables for the program
int ptDebug = 0;
#define DEFAULT_OUTPUT_NAME "pano"
#define PT_MENDER_VERSION "PTmender Version " VERSION ", originally written by Helmut Dersch, rewritten by Daniel German\n"
#define PT_MENDER_USAGE "PTmender [options] <script filename> <images>*\n\n"\
"Options:\n"\
"\t-o <prefix>\tPrefix for output filename, defaults to " DEFAULT_OUTPUT_NAME "\n"\
"\t-q\t\tQuiet run\n"\
"\t-h\t\tShow this message\n"\
"\t-s\t\tSort the filenames provided in the command line in lexicographical order (and only in the command line)\n"\
"\n\nIf no images are specified in the command line, then the 'i' lines are used. If 'i' lines do not contain "\
"a valid filename then the 'o' lines are used.\n"\
"\n"
static int hasPathInfo(char *aName);
static int panoMenderSortingFunction(const void *p1, const void *p2);
static void panoMenderDuplicateScriptFile(char *scriptFileName, char *script, fullPath *scriptPathName)
{
FILE* scriptFD;
int temp;
strcpy(scriptPathName->name, scriptFileName);
// Get a temp filename for the copy of the script
if (panoFileMakeTemp(scriptPathName) == 0) {
PrintError("Could not make Tempfile");
exit(1);
}
// Copy the script
if ((scriptFD = fopen(scriptPathName->name, "w")) == NULL) {
PrintError("Could not open temporary Scriptfile");
exit(1);
}
temp = fwrite(script, 1, (int) strlen(script), scriptFD);
if (strlen(script) != temp) {
PrintError("Could not write temporary Scriptfile");
exit(1);
}
fclose(scriptFD);
}
void panoMenderSetFileName(fullPath *ptrImageFileName, char *name, fullPath *scriptFileName)
{
//Only prepend the path to the script to the filenames if the filenames
//don't already have path information
assert(ptrImageFileName != NULL);
assert(name != NULL);
assert(scriptFileName != NULL);
if ( (hasPathInfo(name)) == 0 )
strcpy(ptrImageFileName->name, scriptFileName->name);
else
strcpy(ptrImageFileName->name, "");
InsertFileName(ptrImageFileName, name);
}
static int panoMenderImageFileNamesReadFromScript(fullPath **ptrImageFileNames, fullPath *scriptFileName)
{
char *script;
AlignInfo alignInfo;
int counter;
int i;
// We don't have any images yet. We read the Script and load them from it.
if (ptDebug) {
fprintf(stderr, "Loading script [%s]\n", scriptFileName->name);
}
script = LoadScript(scriptFileName);
if (script == NULL) {
PrintError("Could not load script [%s]", scriptFileName->name);
return -1;
}
// parse input script and set up an array of input file names
if (ParseScript(script, &alignInfo) != 0) {
// print error
PrintError("Panorama script parsing error");
return 0;
}
// The parser of panotools is really broken. To retrieve each
// input filename it reads the file,
// finds the first filename, then removes it, and writes the rest of the file again
// This is done recursively
counter = alignInfo.numIm;
if (counter != 0) {
// Try to find filenames in input section
if (ptDebug) {
fprintf(stderr, "Found %d images in script file in INPUT section\n", counter);
}
// Allocate their space
if ((*ptrImageFileNames = malloc(512 * counter)) == NULL) {
PrintError("Not enough memory");
exit(1);
}
//Iterate over input images and populate input filename array
for (i = 0; i < counter; i ++) {
//If the image filenames don't appear to have any path information, then
//prepend the path to the script (if any) that was specified on the
//command line (Note: this was the only behavior in the original
//PTStitcher. It has been moved into this conditional block because
//the script path could get prepended to an already fully qualified
//filename...not very useful.
if (ptDebug) {
fprintf(stderr, "Processing image [%s] from 'i' line %d\n", alignInfo.im[i].name, i);
}
panoMenderSetFileName(&((*ptrImageFileNames)[i]), alignInfo.im[i].name, scriptFileName);
if (ptDebug) {
fprintf(stderr, "Reading image filename [%s] from 'i' line %d\n", (*ptrImageFileNames)[i].name, i);
}
}
DisposeAlignInfo(&alignInfo);
}
if (counter == 0) {
// Sometimes the names of the images are not in the 'o' line, then assume they are
// in the 'i' line.
// create a temporary copy we can overwrite
fullPath scriptPathName;
counter = numLines(script, 'o');
if (counter == 0) {
PrintError("No images found input file script file (there are no 'o' lines nor 'i' lines");
exit(1);
}
// Allocate their space
if ((*ptrImageFileNames = malloc(512 * counter)) == NULL) {
PrintError("Not enough memory");
exit(1);
}
panoMenderDuplicateScriptFile(scriptFileName->name, script, &scriptPathName);
for (i = 0; i < counter; i++) {
aPrefs* preferences;
if ( (preferences = readAdjustLine(&scriptPathName)) == NULL) {
PrintError("No 'i' line for image number %d", i);
exit(1);
}
if (ptDebug) {
fprintf(stderr, "Processing image [%s] from 'o' line %d\n", preferences->im.name, i);
}
panoMenderSetFileName(&((*ptrImageFileNames)[i]), preferences->im.name, scriptFileName);
if (ptDebug) {
fprintf(stderr, "Reading image filename [%s] from 'i' line %d\n",
(*ptrImageFileNames)[i].name, i);
}
if (preferences->td != NULL)
free(preferences->td);
if (preferences->ts != NULL)
free(preferences->ts);
free(preferences);
} // end of for (i = 0; i < counter; i++) {
free(script);
remove(scriptPathName.name);
}
return counter;
}
int main(int argc,char *argv[])
{
int counter;
int sort = 0; // do we sort command line filenames?
fullPath *ptrImageFileNames;
fullPath scriptFileName;
fullPath panoFileName;
int i;
char opt;
char *currentParm;
ptrImageFileNames = NULL;
counter = 0;
strcpy(panoFileName.name, DEFAULT_OUTPUT_NAME);
strcpy(scriptFileName.name, "");
printf(PT_MENDER_VERSION);
while ((opt = getopt(argc, argv, "o:f:hsqd")) != -1) {
// o -> set output file
// h -> help?
// q -> quiet?
switch(opt) {
case 'o': // specifies output file name
if (StringtoFullPath(&panoFileName, optarg) != 0) {
PrintError("Syntax error: Not a valid pathname");
return(-1);
}
break;
case 's':
sort = 1;
break;
case 'd':
ptDebug = 1;
break;
case 'q':
ptQuietFlag = 1;
break;
case 'h':
PrintError(PT_MENDER_USAGE);
return -1;
default:
break;
}
}
if (ptDebug) {
fprintf(stderr, "Number of options to process %d\n", argc- optind);
}
if (optind - argc == 0) {
PrintError(PT_MENDER_USAGE);
return -1;
}
// First we get the name of the script
if (StringtoFullPath(&scriptFileName, argv[optind]) !=0) { // success
PrintError("Syntax error: Not a valid pathname");
PrintError(PT_MENDER_USAGE);
return(-1);
}
// Check if we got a filename
if (strlen(scriptFileName.name) == 0) {
PrintError("No script name provided\n");
PrintError(PT_MENDER_USAGE);
return -1;
} // end of if (scriptFileName[0] != 0) {
if (ptDebug){
fprintf(stderr,"Script filename %s\n", scriptFileName.name);
}
// optionally we receive a list of filenames
optind ++;
currentParm = NULL;
while (optind < argc ) {
currentParm = argv[optind];
optind++;
counter++;
if((ptrImageFileNames = realloc(ptrImageFileNames, counter * 512)) == NULL) {
PrintError("Not enough memory");
exit(1);
}
if (StringtoFullPath(&ptrImageFileNames[counter-1], currentParm) != 0) {
PrintError("Syntax error: Not a valid pathname");
return(-1);
}
if (ptDebug){
fprintf(stderr,"Getting file from command line %s index %d\n", ptrImageFileNames[counter-1].name, counter);
}
} // end of while loop while (optind < argc ) {
if (sort && counter > 0) {
// We have filenames that need to be sorted
qsort(ptrImageFileNames, counter, 512, panoMenderSortingFunction);;
}
// Handle some lack of information
if (strlen(panoFileName.name) == 0) {
PrintError("No output filename specified\n");
PrintError(PT_MENDER_USAGE);
return -1;
}
if (counter == 0) {
counter = panoMenderImageFileNamesReadFromScript(&ptrImageFileNames, &scriptFileName);
}
if (counter == 0) {
PrintError("No images found input file script file (there are no 'o' lines nor 'i' lines");
exit(1);
}
// By now we should have loaded up the input filename array, the output
// panorama name, and the name of the script file (copied to a temporary
// directory). Now we can create the output image.
return panoCreatePanorama(ptrImageFileNames, counter, &panoFileName, &scriptFileName);
}
//////////////////////////////////////////////////////////////////////
char* Filename(fullPath* path)
{
char *temp;
if ((temp = strrchr(path->name, '/')) != NULL) {
temp++;
} else {
temp = path->name;
}
return temp;
}
static int hasPathInfo(char *aName)
{
return ((strchr(aName, PATH_SEP) == NULL) ? 0 : 1);
}
static int panoMenderSortingFunction(const void *p1, const void *p2)
{
return strcmp(p1, p2);
}