~ubuntu-branches/ubuntu/hardy/libterralib/hardy

« back to all changes in this revision

Viewing changes to src/tiff/geo_print.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2005-11-25 22:32:59 UTC
  • Revision ID: james.westby@ubuntu.com-20051125223259-3zubal8ux4ki4fjg
Tags: upstream-3.0.3b2
ImportĀ upstreamĀ versionĀ 3.0.3b2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
 *
 
3
 *  geo_print.c  -- Key-dumping routines for GEOTIFF files.
 
4
 *
 
5
 *    Written By: Niles D. Ritter.
 
6
 *
 
7
 *  copyright (c) 1995   Niles D. Ritter
 
8
 *
 
9
 *  Permission granted to use this software, so long as this copyright
 
10
 *  notice accompanies any products derived therefrom.
 
11
 *
 
12
 *  Revision History;
 
13
 *
 
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.
 
19
 *
 
20
 * $Log: geo_print.c,v $
 
21
 * Revision 1.2  2004/03/19 11:51:24  lubia
 
22
 * Atualizada as bibliotecas Tif e GeoTif
 
23
 *
 
24
 * Revision 1.4  2002/05/31 14:27:26  warmerda
 
25
 * added escaping in metadata for string key values
 
26
 *
 
27
 * Revision 1.3  1999/05/04 03:14:35  warmerda
 
28
 * avoid warnings
 
29
 *
 
30
 * Revision 1.2  1999/05/03 17:50:31  warmerda
 
31
 * avoid warnings on IRIX
 
32
 *
 
33
 *
 
34
 **********************************************************************/
 
35
 
 
36
#include "geotiff.h"   /* public interface        */
 
37
#include "geo_tiffp.h" /* external TIFF interface */
 
38
#include "geo_keyp.h"  /* private interface       */
 
39
#include "geokeys.h"
 
40
 
 
41
#include <stdio.h>     /* for sprintf             */
 
42
 
 
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"
 
53
 
 
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);
 
62
 
 
63
/*
 
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.
 
67
 *
 
68
 * The output format is a "GeoTIFF meta-data" file, which may be
 
69
 * used to import information with the GTIFFImport() routine.
 
70
 */
 
71
 
 
72
void GTIFPrint(GTIF *gtif, GTIFPrintMethod print,void *aux)
 
73
{
 
74
    int i;
 
75
    int numkeys = gtif->gt_num_keys;
 
76
    GeoKey *key = gtif->gt_keys;
 
77
    char message[1024];
 
78
        
 
79
    if (!print) print = (GTIFPrintMethod) &DefaultPrint;
 
80
    if (!aux) aux=stdout;       
 
81
 
 
82
    sprintf(message,FMT_GEOTIFF "\n"); 
 
83
    print(message,aux);
 
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,
 
88
            gtif->gt_rev_minor); 
 
89
    print("   ",aux); print(message,aux); print("\n",aux);
 
90
 
 
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);
 
94
 
 
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);
 
99
 
 
100
    sprintf(message,"   %s\n",FMT_GEOEND); print(message,aux);
 
101
}
 
102
 
 
103
static void PrintGeoTags(GTIF *gt, GTIFPrintMethod print,void *aux)
 
104
{
 
105
        double *data;
 
106
        int count;
 
107
        tiff_t *tif=gt->gt_tif;
 
108
 
 
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);
 
115
}
 
116
 
 
117
static void PrintTag(int tag, int nrows, double *dptr, int ncols, 
 
118
                                        GTIFPrintMethod print,void *aux)
 
119
{
 
120
        int i,j;
 
121
        double *data=dptr;
 
122
        char message[1024];
 
123
 
 
124
        print("      ",aux);
 
125
        print(GTIFTagName(tag),aux);
 
126
        sprintf(message," (%d,%d):\n",nrows,ncols);
 
127
        print(message,aux);
 
128
        for (i=0;i<nrows;i++)
 
129
        {
 
130
                print("         ",aux);
 
131
                for (j=0;j<ncols;j++)
 
132
                {
 
133
                        sprintf(message,FMT_DOUBLE,*data++);
 
134
                        print(message,aux);
 
135
                }
 
136
                print("\n",aux);
 
137
        }
 
138
        _GTIFFree(dptr); /* free up the allocated memory */
 
139
}
 
140
 
 
141
 
 
142
static void PrintKey(GeoKey *key, GTIFPrintMethod print, void *aux)
 
143
{
 
144
    char *data;
 
145
    geokey_t keyid = (geokey_t) key->gk_key;
 
146
    int count = key->gk_count;
 
147
    int vals_now,i;
 
148
    pinfo_t *sptr;
 
149
    double *dptr;
 
150
    char message[40];
 
151
 
 
152
    print("      ",aux);
 
153
    print(GTIFKeyName(keyid),aux);
 
154
        
 
155
    sprintf(message," (%s,%d): ",GTIFTypeName(key->gk_type),count);
 
156
    print(message,aux);
 
157
        
 
158
    if (key->gk_type==TYPE_SHORT && count==1)
 
159
        data = (char *)&key->gk_data;
 
160
    else
 
161
        data = key->gk_data;
 
162
                
 
163
    switch (key->gk_type)
 
164
    {
 
165
      case TYPE_ASCII: 
 
166
      {
 
167
          int  in_char, out_char;
 
168
 
 
169
          print("\"",aux);
 
170
 
 
171
          in_char = 0;
 
172
          out_char = 0;
 
173
          while( in_char < count-1 )
 
174
          {
 
175
              char ch = ((char *) data)[in_char++];
 
176
 
 
177
              if( ch == '\n' )
 
178
              {
 
179
                  message[out_char++] = '\\';
 
180
                  message[out_char++] = 'n';
 
181
              }
 
182
              else if( ch == '\\' )
 
183
              {
 
184
                  message[out_char++] = '\\';
 
185
                  message[out_char++] = '\\';
 
186
              }
 
187
              else
 
188
                  message[out_char++] = ch;
 
189
 
 
190
              /* flush message if buffer full */
 
191
              if( out_char == sizeof(message)-3 )
 
192
              {
 
193
                  message[out_char] = '\0';
 
194
                  print(message,aux);
 
195
                  out_char = 0;
 
196
              }
 
197
          }
 
198
 
 
199
          message[out_char]='\0';
 
200
          print(message,aux);
 
201
 
 
202
          print("\"\n",aux);
 
203
      }
 
204
      break;
 
205
        
 
206
      case TYPE_DOUBLE: 
 
207
        for (dptr = (double *)data; count > 0; count-= vals_now)
 
208
        {
 
209
            vals_now = count > 3? 3: count;
 
210
            for (i=0; i<vals_now; i++,dptr++)
 
211
            {
 
212
                sprintf(message,FMT_DOUBLE ,*dptr);
 
213
                print(message,aux);
 
214
            }
 
215
            print("\n",aux);
 
216
        }
 
217
        break;
 
218
        
 
219
      case TYPE_SHORT: 
 
220
        sptr = (pinfo_t *)data;
 
221
        if (count==1)
 
222
        {
 
223
            sprintf(message,"%s\n",GTIFValueName(keyid,*sptr));
 
224
            print(message,aux);
 
225
        }
 
226
        else
 
227
            for (; count > 0; count-= vals_now)
 
228
            {
 
229
                vals_now = count > 3? 3: count;
 
230
                for (i=0; i<vals_now; i++,sptr++)
 
231
                {
 
232
                    sprintf(message,FMT_SHORT,*sptr);
 
233
                    print(message,aux);
 
234
                }
 
235
                print("\n",aux);
 
236
            }
 
237
        break;
 
238
        
 
239
      default: 
 
240
        sprintf(message, "Unknown Type (%d)\n",key->gk_type);
 
241
        print(message,aux);
 
242
        break;
 
243
    }
 
244
}
 
245
 
 
246
static void DefaultPrint(char *string, void *aux)
 
247
{
 
248
    /* Pretty boring */
 
249
    fprintf((FILE *)aux,string);
 
250
}
 
251
 
 
252
 
 
253
/*
 
254
 *  Importing metadata file
 
255
 */
 
256
 
 
257
/*
 
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.
 
261
 *
 
262
 * The input format is a "GeoTIFF meta-data" file, which may be
 
263
 * generated by the GTIFFPrint() routine.
 
264
 */
 
265
 
 
266
int GTIFImport(GTIF *gtif, GTIFReadMethod scan,void *aux)
 
267
{
 
268
    int status;
 
269
    char message[1024];
 
270
        
 
271
    if (!scan) scan = (GTIFReadMethod) &DefaultRead;
 
272
    if (!aux) aux=stdin;        
 
273
        
 
274
    scan(message,aux);
 
275
    if (strncmp(message,FMT_GEOTIFF,8)) return 0; 
 
276
    scan(message,aux);
 
277
    if (!sscanf(message,FMT_VERSION,&gtif->gt_version)) return 0;
 
278
    scan(message,aux);
 
279
    if (sscanf(message,FMT_REV,&gtif->gt_rev_major,
 
280
               &gtif->gt_rev_minor) !=2) return 0;
 
281
 
 
282
    scan(message,aux);
 
283
    if (strncmp(message,FMT_TAGS,8)) return 0;
 
284
    while ((status=ReadTag(gtif,scan,aux))>0);
 
285
    if (status < 0) return 0;
 
286
 
 
287
    scan(message,aux);
 
288
    if (strncmp(message,FMT_KEYS,8)) return 0;
 
289
    while ((status=ReadKey(gtif,scan,aux))>0);
 
290
        
 
291
    return (status==0); /* success */
 
292
}
 
293
 
 
294
static int StringError(char *string)
 
295
{
 
296
    fprintf(stderr,"Parsing Error at \'%s\'\n",string);
 
297
    return -1;
 
298
}
 
299
 
 
300
#define SKIPWHITE(vptr) \
 
301
  while (*vptr && (*vptr==' '||*vptr=='\t')) vptr++
 
302
#define FINDCHAR(vptr,c) \
 
303
  while (*vptr && *vptr!=(c)) vptr++
 
304
 
 
305
static int ReadTag(GTIF *gt,GTIFReadMethod scan,void *aux)
 
306
{
 
307
    int i,j,tag;
 
308
    char *vptr;
 
309
    char tagname[100];
 
310
    double data[100],*dptr=data;
 
311
    int count,nrows,ncols,num;
 
312
    char message[1024];
 
313
 
 
314
    scan(message,aux);
 
315
    if (!strncmp(message,FMT_TAGEND,8)) return 0;
 
316
 
 
317
    num=sscanf(message,"%[^( ] (%d,%d):\n",tagname,&nrows,&ncols);
 
318
    if (num!=3) return StringError(message);
 
319
        
 
320
    tag = GTIFTagCode(tagname);
 
321
    if (tag < 0) return StringError(tagname);
 
322
        
 
323
    count = nrows*ncols;
 
324
    for (i=0;i<nrows;i++)
 
325
    {
 
326
        scan(message,aux);
 
327
        vptr = message;
 
328
        for (j=0;j<ncols;j++)
 
329
        {
 
330
            if (!sscanf(vptr,"%lg",dptr++))
 
331
                return StringError(vptr);
 
332
            FINDCHAR(vptr,' ');
 
333
            SKIPWHITE(vptr);
 
334
        }
 
335
    }   
 
336
    (gt->gt_methods.set)(gt->gt_tif, tag, count, data );        
 
337
 
 
338
    return 1;
 
339
}
 
340
 
 
341
 
 
342
static int ReadKey(GTIF *gt, GTIFReadMethod scan, void *aux)
 
343
{
 
344
    tagtype_t ktype;
 
345
    int count,outcount;
 
346
    int vals_now,i;
 
347
    geokey_t key;
 
348
    int icode;
 
349
    pinfo_t code;
 
350
    short  *sptr;
 
351
    char name[1000];
 
352
    char type[20];
 
353
    double data[100];
 
354
    double *dptr;
 
355
    char *vptr;
 
356
    int num;
 
357
    char message[2048];
 
358
 
 
359
    scan(message,aux); 
 
360
    if (!strncmp(message,FMT_KEYEND,8)) return 0;
 
361
 
 
362
    num=sscanf(message,"%[^( ] (%[^,],%d):\n",name,type,&count);
 
363
    if (num!=3) return StringError(message);
 
364
 
 
365
    vptr = message;
 
366
    FINDCHAR(vptr,':'); 
 
367
    if (!*vptr) return StringError(message);
 
368
    vptr+=2;
 
369
 
 
370
    if( GTIFKeyCode(name) < 0 )
 
371
        return StringError(name);
 
372
    else
 
373
        key = (geokey_t) GTIFKeyCode(name);
 
374
 
 
375
    if( GTIFTypeCode(type) < 0 )
 
376
        return StringError(type);
 
377
    else
 
378
        ktype = (tagtype_t) GTIFTypeCode(type);
 
379
 
 
380
    /* skip white space */
 
381
    SKIPWHITE(vptr);
 
382
    if (!*vptr) return StringError(message);
 
383
                        
 
384
    switch (ktype)
 
385
    {
 
386
      case TYPE_ASCII: 
 
387
      {
 
388
          char *cdata;
 
389
          int out_char = 0;
 
390
 
 
391
          FINDCHAR(vptr,'"');
 
392
          if (!*vptr) return StringError(message);
 
393
 
 
394
          cdata = (char *) _GTIFcalloc( count+1 );
 
395
 
 
396
          vptr++;
 
397
          while( out_char < count-1 )
 
398
          {
 
399
              if( *vptr == '\0' )
 
400
                  break;
 
401
 
 
402
              else if( vptr[0] == '\\' && vptr[1] == 'n' )
 
403
              {
 
404
                  cdata[out_char++] = '\n';
 
405
                  vptr += 2;
 
406
              }
 
407
              else if( vptr[0] == '\\' && vptr[1] == '\\' )
 
408
              {
 
409
                  cdata[out_char++] = '\\';
 
410
                  vptr += 2;
 
411
              }
 
412
              else
 
413
                  cdata[out_char++] = *(vptr++);
 
414
          }
 
415
 
 
416
          if( out_char < count-1 ) return StringError(message);
 
417
          if( *vptr != '"' ) return StringError(message);
 
418
 
 
419
          cdata[count-1] = '\0';
 
420
          GTIFKeySet(gt,key,ktype,count,cdata);
 
421
 
 
422
          _GTIFFree( cdata );
 
423
      }
 
424
      break;
 
425
        
 
426
      case TYPE_DOUBLE: 
 
427
        outcount = count;
 
428
        for (dptr = data; count > 0; count-= vals_now)
 
429
        {
 
430
            vals_now = count > 3? 3: count;
 
431
            for (i=0; i<vals_now; i++,dptr++)
 
432
            {
 
433
                if (!sscanf(vptr,"%lg" ,dptr))
 
434
                    StringError(vptr);
 
435
                FINDCHAR(vptr,' ');
 
436
                SKIPWHITE(vptr);
 
437
            }
 
438
            if (vals_now<count)
 
439
            {
 
440
                scan(message,aux);
 
441
                vptr = message;
 
442
            }
 
443
        }
 
444
        if (outcount==1)
 
445
            GTIFKeySet(gt,key,ktype,outcount,data[0]);
 
446
        else
 
447
            GTIFKeySet(gt,key,ktype,outcount,data);
 
448
        break;
 
449
        
 
450
      case TYPE_SHORT: 
 
451
        if (count==1)
 
452
        {
 
453
            icode = GTIFValueCode(key,vptr);
 
454
            if (icode < 0) return StringError(vptr);
 
455
            code = icode;
 
456
            GTIFKeySet(gt,key,ktype,count,code);
 
457
        }
 
458
        else  /* multi-valued short - no such thing yet */
 
459
        {
 
460
            sptr = (short *)data;
 
461
            outcount = count;
 
462
            for (; count > 0; count-= vals_now)
 
463
            {
 
464
                vals_now = count > 3? 3: count;
 
465
                for (i=0; i<vals_now; i++,sptr++)
 
466
                {
 
467
                    int         work_int;
 
468
 
 
469
                    /* note: FMT_SHORT (%11hd) not supported on IRIX */
 
470
                    sscanf(message,"%11d",&work_int);
 
471
                    *sptr = work_int;
 
472
                    scan(message,aux);
 
473
                }
 
474
                if (vals_now<count)
 
475
                {
 
476
                    scan(message,aux);
 
477
                    vptr = message;
 
478
                }
 
479
            }
 
480
            GTIFKeySet(gt,key,ktype,outcount,sptr);                     
 
481
        }
 
482
        break;
 
483
        
 
484
      default: 
 
485
        return -1;
 
486
    }
 
487
    return 1;
 
488
}
 
489
 
 
490
 
 
491
static void DefaultRead(char *string, void *aux)
 
492
{
 
493
        /* Pretty boring */
 
494
        fscanf((FILE *)aux,"%[^\n]\n",string);
 
495
}
 
496