1
/**********************************************************************
3
* geo_print.c -- Key-dumping routines for GEOTIFF files.
5
* Written By: Niles D. Ritter.
7
* copyright (c) 1995 Niles D. Ritter
9
* Permission granted to use this software, so long as this copyright
10
* notice accompanies any products derived therefrom.
14
* 20 June, 1995 Niles D. Ritter New
15
* 7 July, 1995 NDR Fix indexing
16
* 27 July, 1995 NDR Added Import utils
17
* 28 July, 1995 NDR Made parser more strict.
18
* 29 Sep, 1995 NDR Fixed matrix printing.
20
* $Log: geo_print.c,v $
21
* Revision 1.2 2004/03/19 11:51:24 lubia
22
* Atualizada as bibliotecas Tif e GeoTif
24
* Revision 1.4 2002/05/31 14:27:26 warmerda
25
* added escaping in metadata for string key values
27
* Revision 1.3 1999/05/04 03:14:35 warmerda
30
* Revision 1.2 1999/05/03 17:50:31 warmerda
31
* avoid warnings on IRIX
34
**********************************************************************/
36
#include "geotiff.h" /* public interface */
37
#include "geo_tiffp.h" /* external TIFF interface */
38
#include "geo_keyp.h" /* private interface */
41
#include <stdio.h> /* for sprintf */
43
#define FMT_GEOTIFF "Geotiff_Information:"
44
#define FMT_VERSION "Version: %hd"
45
#define FMT_REV "Key_Revision: %1hd.%hd"
46
#define FMT_TAGS "Tagged_Information:"
47
#define FMT_TAGEND "End_Of_Tags."
48
#define FMT_KEYS "Keyed_Information:"
49
#define FMT_KEYEND "End_Of_Keys."
50
#define FMT_GEOEND "End_Of_Geotiff."
51
#define FMT_DOUBLE "%-17.9g"
52
#define FMT_SHORT "%-11hd"
54
static void DefaultPrint(char *string, void *aux);
55
static void PrintKey(GeoKey *key, GTIFPrintMethod print,void *aux);
56
static void PrintGeoTags(GTIF *gtif,GTIFReadMethod scan,void *aux);
57
static void PrintTag(int tag, int nrows, double *data, int ncols,
58
GTIFPrintMethod print,void *aux);
59
static void DefaultRead(char *string, void *aux);
60
static int ReadKey(GTIF *gt, GTIFReadMethod scan, void *aux);
61
static int ReadTag(GTIF *gt,GTIFReadMethod scan,void *aux);
64
* Print off the directory info, using whatever method is specified
65
* (defaults to fprintf if null). The "aux" parameter is provided for user
66
* defined method for passing parameters or whatever.
68
* The output format is a "GeoTIFF meta-data" file, which may be
69
* used to import information with the GTIFFImport() routine.
72
void GTIFPrint(GTIF *gtif, GTIFPrintMethod print,void *aux)
75
int numkeys = gtif->gt_num_keys;
76
GeoKey *key = gtif->gt_keys;
79
if (!print) print = (GTIFPrintMethod) &DefaultPrint;
82
sprintf(message,FMT_GEOTIFF "\n");
84
sprintf(message, "Version: %hd" ,gtif->gt_version);
85
sprintf(message, FMT_VERSION,gtif->gt_version);
86
print(" ",aux); print(message,aux); print("\n",aux);
87
sprintf(message, FMT_REV,gtif->gt_rev_major,
89
print(" ",aux); print(message,aux); print("\n",aux);
91
sprintf(message," %s\n",FMT_TAGS); print(message,aux);
92
PrintGeoTags(gtif,print,aux);
93
sprintf(message," %s\n",FMT_TAGEND); print(message,aux);
95
sprintf(message," %s\n",FMT_KEYS); print(message,aux);
96
for (i=0; i<numkeys; i++)
97
PrintKey(++key,print,aux);
98
sprintf(message," %s\n",FMT_KEYEND); print(message,aux);
100
sprintf(message," %s\n",FMT_GEOEND); print(message,aux);
103
static void PrintGeoTags(GTIF *gt, GTIFPrintMethod print,void *aux)
107
tiff_t *tif=gt->gt_tif;
109
if ((gt->gt_methods.get)(tif, GTIFF_TIEPOINTS, &count, &data ))
110
PrintTag(GTIFF_TIEPOINTS,count/3, data, 3, print, aux);
111
if ((gt->gt_methods.get)(tif, GTIFF_PIXELSCALE, &count, &data ))
112
PrintTag(GTIFF_PIXELSCALE,count/3, data, 3, print, aux);
113
if ((gt->gt_methods.get)(tif, GTIFF_TRANSMATRIX, &count, &data ))
114
PrintTag(GTIFF_TRANSMATRIX,count/4, data, 4, print, aux);
117
static void PrintTag(int tag, int nrows, double *dptr, int ncols,
118
GTIFPrintMethod print,void *aux)
125
print(GTIFTagName(tag),aux);
126
sprintf(message," (%d,%d):\n",nrows,ncols);
128
for (i=0;i<nrows;i++)
131
for (j=0;j<ncols;j++)
133
sprintf(message,FMT_DOUBLE,*data++);
138
_GTIFFree(dptr); /* free up the allocated memory */
142
static void PrintKey(GeoKey *key, GTIFPrintMethod print, void *aux)
145
geokey_t keyid = (geokey_t) key->gk_key;
146
int count = key->gk_count;
153
print(GTIFKeyName(keyid),aux);
155
sprintf(message," (%s,%d): ",GTIFTypeName(key->gk_type),count);
158
if (key->gk_type==TYPE_SHORT && count==1)
159
data = (char *)&key->gk_data;
163
switch (key->gk_type)
167
int in_char, out_char;
173
while( in_char < count-1 )
175
char ch = ((char *) data)[in_char++];
179
message[out_char++] = '\\';
180
message[out_char++] = 'n';
182
else if( ch == '\\' )
184
message[out_char++] = '\\';
185
message[out_char++] = '\\';
188
message[out_char++] = ch;
190
/* flush message if buffer full */
191
if( out_char == sizeof(message)-3 )
193
message[out_char] = '\0';
199
message[out_char]='\0';
207
for (dptr = (double *)data; count > 0; count-= vals_now)
209
vals_now = count > 3? 3: count;
210
for (i=0; i<vals_now; i++,dptr++)
212
sprintf(message,FMT_DOUBLE ,*dptr);
220
sptr = (pinfo_t *)data;
223
sprintf(message,"%s\n",GTIFValueName(keyid,*sptr));
227
for (; count > 0; count-= vals_now)
229
vals_now = count > 3? 3: count;
230
for (i=0; i<vals_now; i++,sptr++)
232
sprintf(message,FMT_SHORT,*sptr);
240
sprintf(message, "Unknown Type (%d)\n",key->gk_type);
246
static void DefaultPrint(char *string, void *aux)
249
fprintf((FILE *)aux,string);
254
* Importing metadata file
258
* Import the directory info, using whatever method is specified
259
* (defaults to fscanf if null). The "aux" parameter is provided for user
260
* defined method for passing file or whatever.
262
* The input format is a "GeoTIFF meta-data" file, which may be
263
* generated by the GTIFFPrint() routine.
266
int GTIFImport(GTIF *gtif, GTIFReadMethod scan,void *aux)
271
if (!scan) scan = (GTIFReadMethod) &DefaultRead;
275
if (strncmp(message,FMT_GEOTIFF,8)) return 0;
277
if (!sscanf(message,FMT_VERSION,>if->gt_version)) return 0;
279
if (sscanf(message,FMT_REV,>if->gt_rev_major,
280
>if->gt_rev_minor) !=2) return 0;
283
if (strncmp(message,FMT_TAGS,8)) return 0;
284
while ((status=ReadTag(gtif,scan,aux))>0);
285
if (status < 0) return 0;
288
if (strncmp(message,FMT_KEYS,8)) return 0;
289
while ((status=ReadKey(gtif,scan,aux))>0);
291
return (status==0); /* success */
294
static int StringError(char *string)
296
fprintf(stderr,"Parsing Error at \'%s\'\n",string);
300
#define SKIPWHITE(vptr) \
301
while (*vptr && (*vptr==' '||*vptr=='\t')) vptr++
302
#define FINDCHAR(vptr,c) \
303
while (*vptr && *vptr!=(c)) vptr++
305
static int ReadTag(GTIF *gt,GTIFReadMethod scan,void *aux)
310
double data[100],*dptr=data;
311
int count,nrows,ncols,num;
315
if (!strncmp(message,FMT_TAGEND,8)) return 0;
317
num=sscanf(message,"%[^( ] (%d,%d):\n",tagname,&nrows,&ncols);
318
if (num!=3) return StringError(message);
320
tag = GTIFTagCode(tagname);
321
if (tag < 0) return StringError(tagname);
324
for (i=0;i<nrows;i++)
328
for (j=0;j<ncols;j++)
330
if (!sscanf(vptr,"%lg",dptr++))
331
return StringError(vptr);
336
(gt->gt_methods.set)(gt->gt_tif, tag, count, data );
342
static int ReadKey(GTIF *gt, GTIFReadMethod scan, void *aux)
360
if (!strncmp(message,FMT_KEYEND,8)) return 0;
362
num=sscanf(message,"%[^( ] (%[^,],%d):\n",name,type,&count);
363
if (num!=3) return StringError(message);
367
if (!*vptr) return StringError(message);
370
if( GTIFKeyCode(name) < 0 )
371
return StringError(name);
373
key = (geokey_t) GTIFKeyCode(name);
375
if( GTIFTypeCode(type) < 0 )
376
return StringError(type);
378
ktype = (tagtype_t) GTIFTypeCode(type);
380
/* skip white space */
382
if (!*vptr) return StringError(message);
392
if (!*vptr) return StringError(message);
394
cdata = (char *) _GTIFcalloc( count+1 );
397
while( out_char < count-1 )
402
else if( vptr[0] == '\\' && vptr[1] == 'n' )
404
cdata[out_char++] = '\n';
407
else if( vptr[0] == '\\' && vptr[1] == '\\' )
409
cdata[out_char++] = '\\';
413
cdata[out_char++] = *(vptr++);
416
if( out_char < count-1 ) return StringError(message);
417
if( *vptr != '"' ) return StringError(message);
419
cdata[count-1] = '\0';
420
GTIFKeySet(gt,key,ktype,count,cdata);
428
for (dptr = data; count > 0; count-= vals_now)
430
vals_now = count > 3? 3: count;
431
for (i=0; i<vals_now; i++,dptr++)
433
if (!sscanf(vptr,"%lg" ,dptr))
445
GTIFKeySet(gt,key,ktype,outcount,data[0]);
447
GTIFKeySet(gt,key,ktype,outcount,data);
453
icode = GTIFValueCode(key,vptr);
454
if (icode < 0) return StringError(vptr);
456
GTIFKeySet(gt,key,ktype,count,code);
458
else /* multi-valued short - no such thing yet */
460
sptr = (short *)data;
462
for (; count > 0; count-= vals_now)
464
vals_now = count > 3? 3: count;
465
for (i=0; i<vals_now; i++,sptr++)
469
/* note: FMT_SHORT (%11hd) not supported on IRIX */
470
sscanf(message,"%11d",&work_int);
480
GTIFKeySet(gt,key,ktype,outcount,sptr);
491
static void DefaultRead(char *string, void *aux)
494
fscanf((FILE *)aux,"%[^\n]\n",string);