File: geo_new.c

package info (click to toggle)
libgeotiff 1.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,300 kB
  • sloc: ansic: 8,080; sh: 3,977; makefile: 166
file content (327 lines) | stat: -rw-r--r-- 10,741 bytes parent folder | download | duplicates (7)
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
/**********************************************************************
 *
 *  geo_new.c  -- Public routines for GEOTIFF GeoKey access.
 *
 *    Written By: Niles D. Ritter.
 *
 *  copyright (c) 1995   Niles D. Ritter
 *
 *  Permission granted to use this software, so long as this copyright
 *  notice accompanies any products derived therefrom.
 *
 *    20 June, 1995      Niles D. Ritter         New
 *    7 July,  1995      Greg Martin             Fix index
 *
 **********************************************************************/

#include <stdarg.h>

#include "geotiffio.h"   /* public interface        */
#include "geo_tiffp.h" /* external TIFF interface */
#include "geo_keyp.h"  /* private interface       */
#include "geo_simpletags.h"

/* private local routines */
static int ReadKey(GTIF* gt, TempKeyData* tempData,
                   KeyEntry* entptr, GeoKey* keyptr);


static void GTIFErrorFunction(GTIF* gt, int level, const char* msg, ...)
{
    va_list list;
    (void)gt;

    va_start(list, msg);
    if( level == LIBGEOTIFF_WARNING )
        fprintf(stderr, "Warning :");
    else if( level == LIBGEOTIFF_ERROR )
        fprintf(stderr, "Error :");
    vfprintf(stderr, msg, list);
    fprintf(stderr, "\n");
    va_end(list);
}

/**********************************************************************
 *
 *                        Public Routines
 *
 **********************************************************************/


/**
 * Given an open TIFF file, look for GTIF keys and
 *  values and return GTIF structure.

This function creates a GeoTIFF information interpretation handle
(GTIF *) based on a passed in TIFF handle originally from
XTIFFOpen().  Even though the argument
(<b>tif</b>) is shown as type <tt>void *</tt>, it is really normally
of type <tt>TIFF *</tt>.<p>

The returned GTIF handle can be used to read or write GeoTIFF tags
using the various GTIF functions.  The handle should be destroyed using
GTIFFree() before the file is closed with TIFFClose().<p>

If the file accessed has no GeoTIFF keys, an valid (but empty) GTIF is
still returned.  GTIFNew() is used both for existing files being read, and
for new TIFF files that will have GeoTIFF tags written to them.<p>

 */

GTIF* GTIFNew(void *tif)

{
    return GTIFNewEx( tif, GTIFErrorFunction, NULL );
}

GTIF* GTIFNewEx(void *tif,
                GTErrorCallback error_callback, void* user_data)

{
    TIFFMethod default_methods;
    _GTIFSetDefaultTIFF( &default_methods );

    return GTIFNewWithMethodsEx( tif, &default_methods,
                                 error_callback, user_data );
}

GTIF *GTIFNewSimpleTags( void *tif )

{
    TIFFMethod default_methods;
    GTIFSetSimpleTagsMethods( &default_methods );

    return GTIFNewWithMethods( tif, &default_methods );
}

/************************************************************************/
/*                         GTIFNewWithMethods()                         */
/*                                                                      */
/*      Create a new geotiff, passing in the methods structure to       */
/*      support not libtiff implementations without replacing the       */
/*      default methods.                                                */
/************************************************************************/

GTIF* GTIFNewWithMethods(void *tif, TIFFMethod* methods)
{
    return GTIFNewWithMethodsEx(tif, methods, GTIFErrorFunction, NULL);
}

GTIF* GTIFNewWithMethodsEx(void *tif, TIFFMethod* methods,
                           GTErrorCallback error_callback, void* user_data)
{
    GTIF* gt;
    int count,bufcount,nIndex;
    GeoKey *keyptr;
    pinfo_t *data;
    KeyEntry *entptr;
    KeyHeader *header;
    TempKeyData tempData;

    memset( &tempData, 0, sizeof(tempData) );
    gt = (GTIF*)_GTIFcalloc( sizeof(GTIF));
    if (!gt) goto failure;
    gt->gt_error_callback = error_callback;
    gt->gt_user_data = user_data;

    /* install TIFF file and I/O methods */
    gt->gt_tif = (tiff_t *)tif;
    memcpy( &gt->gt_methods, methods, sizeof(TIFFMethod) );

    /* since this is an array, GTIF will allocate the memory */
    if ( tif == NULL
         || !(gt->gt_methods.get)(tif, GTIFF_GEOKEYDIRECTORY, &gt->gt_nshorts, &data ))
    {
        /* No ProjectionInfo, create a blank one */
        data=(pinfo_t*)_GTIFcalloc((4+MAX_VALUES)*sizeof(pinfo_t));
        if (!data) goto failure;
        header = (KeyHeader *)data;
        header->hdr_version = GvCurrentVersion;
        header->hdr_rev_major = GvCurrentRevision;
        header->hdr_rev_minor = GvCurrentMinorRev;
        gt->gt_nshorts=sizeof(KeyHeader)/sizeof(pinfo_t);
    }
    else
    {
        /* resize data array so it can be extended if needed */
        data = (pinfo_t*) _GTIFrealloc(data,(4+MAX_VALUES)*sizeof(pinfo_t));
    }
    gt->gt_short = data;
    header = (KeyHeader *)data;

    if (header->hdr_version > GvCurrentVersion) goto failure;
    if (header->hdr_rev_major > GvCurrentRevision)
    {
        /* issue warning */
    }

    /* If we got here, then the geokey can be parsed */
    count = header->hdr_num_keys;

    if (count * sizeof(KeyEntry) >= (4 + MAX_VALUES) * sizeof(pinfo_t))
        goto failure;

    gt->gt_num_keys = count;
    gt->gt_version  = header->hdr_version;
    gt->gt_rev_major  = header->hdr_rev_major;
    gt->gt_rev_minor  = header->hdr_rev_minor;

    bufcount = count+MAX_KEYS; /* allow for expansion */

    /* Get the PARAMS Tags, if any */
    if (tif == NULL
        || !(gt->gt_methods.get)(tif, GTIFF_DOUBLEPARAMS,
                                 &gt->gt_ndoubles, &gt->gt_double ))
    {
        gt->gt_double=(double*)_GTIFcalloc(MAX_VALUES*sizeof(double));
        if (!gt->gt_double) goto failure;
    }
    else
    {
        if( gt->gt_ndoubles > MAX_VALUES )
            goto failure;
        /* resize data array so it can be extended if needed */
        gt->gt_double = (double*) _GTIFrealloc(gt->gt_double,
                                               (MAX_VALUES)*sizeof(double));
    }
    if ( tif == NULL
         || !(gt->gt_methods.get)(tif, GTIFF_ASCIIPARAMS,
                                  &tempData.tk_asciiParamsLength,
                                  &tempData.tk_asciiParams ))
    {
        tempData.tk_asciiParams         = 0;
        tempData.tk_asciiParamsLength   = 0;
    }
    else
    {
        /* last NULL doesn't count; "|" used for delimiter */
        if( tempData.tk_asciiParamsLength > 0
            && tempData.tk_asciiParams[tempData.tk_asciiParamsLength-1] == '\0')
        {
            --tempData.tk_asciiParamsLength;
        }
    }

    /* allocate space for GeoKey array and its index */
    gt->gt_keys = (GeoKey *)_GTIFcalloc( sizeof(GeoKey)*bufcount);
    if (!gt->gt_keys) goto failure;
    gt->gt_keyindex = (int *)_GTIFcalloc( sizeof(int)*(MAX_KEYINDEX+1));
    if (!gt->gt_keyindex) goto failure;

    /*  Loop to get all GeoKeys */
    entptr = ((KeyEntry *)data) + 1;
    keyptr = gt->gt_keys;
    gt->gt_keymin = MAX_KEYINDEX;
    gt->gt_keymax = 0;
    for (nIndex=1; nIndex<=count; nIndex++,entptr++)
    {
        if (!ReadKey(gt, &tempData, entptr, ++keyptr))
            goto failure;

        /* Set up the index (start at 1, since 0=unset) */
        gt->gt_keyindex[entptr->ent_key] = nIndex;
    }

    if( tempData.tk_asciiParams != NULL )
        _GTIFFree( tempData.tk_asciiParams );

    return gt;

  failure:
    /* Notify of error */
    if( tempData.tk_asciiParams != NULL )
        _GTIFFree( tempData.tk_asciiParams );
    GTIFFree (gt);
    return (GTIF *)0;
}

/**********************************************************************
 *
 *                        Private Routines
 *
 **********************************************************************/

/*
 * Given KeyEntry, read in the GeoKey value location and set up
 *  the Key structure, returning 0 if failure.
 */

static int ReadKey(GTIF* gt, TempKeyData* tempData,
                   KeyEntry* entptr, GeoKey* keyptr)
{
    int offset,count;

    keyptr->gk_key = entptr->ent_key;
    keyptr->gk_count = entptr->ent_count;
    count = entptr->ent_count;
    offset = entptr->ent_val_offset;
    if (gt->gt_keymin > keyptr->gk_key)  gt->gt_keymin=keyptr->gk_key;
    if (gt->gt_keymax < keyptr->gk_key)  gt->gt_keymax=keyptr->gk_key;

    if (entptr->ent_location)
        keyptr->gk_type = (gt->gt_methods.type)(gt->gt_tif,entptr->ent_location);
    else
        keyptr->gk_type = (gt->gt_methods.type)(gt->gt_tif,GTIFF_GEOKEYDIRECTORY);

    switch (entptr->ent_location)
    {
        case GTIFF_LOCAL:
            /* store value into data value */
            memcpy(&keyptr->gk_data, &(entptr->ent_val_offset), sizeof(pinfo_t));
            break;
        case GTIFF_GEOKEYDIRECTORY:
            keyptr->gk_data = (char *)(gt->gt_short+offset);
            if (gt->gt_nshorts < offset+count)
                return 0;
            break;
        case GTIFF_DOUBLEPARAMS:
            keyptr->gk_data = (char *)(gt->gt_double+offset);
            if (gt->gt_ndoubles < offset+count)
                return 0;
            break;
        case GTIFF_ASCIIPARAMS:
            if( tempData->tk_asciiParams == NULL )
                return 0;
            if( offset + count == tempData->tk_asciiParamsLength + 1
                && count > 0 )
            {
                /* some vendors seem to feel they should not use the
                   terminating '|' char, but do include a terminating '\0'
                   which we lose in the low level reading code.
                   If this is the case, drop the extra character */
                count--;
            }
            else if (offset < tempData->tk_asciiParamsLength
                     && offset + count > tempData->tk_asciiParamsLength )
            {
                count = tempData->tk_asciiParamsLength - offset;
                /* issue warning... if we could */
            }
            else if (offset + count > tempData->tk_asciiParamsLength)
                return 0;

            keyptr->gk_count = MAX(1,count+1);
            keyptr->gk_data = (char *) _GTIFcalloc (keyptr->gk_count);

            _GTIFmemcpy (keyptr->gk_data,
                         tempData->tk_asciiParams + offset, count);
            if( keyptr->gk_data[MAX(0,count-1)] == '|' )
            {
                keyptr->gk_data[MAX(0,count-1)] = '\0';
                keyptr->gk_count = count;
            }
            else
                keyptr->gk_data[MAX(0,count)] = '\0';
            break;
        default:
            return 0; /* failure */
    }
    keyptr->gk_size = _gtiff_size[keyptr->gk_type];

    return 1; /* success */
}

void *GTIFGetUserData(GTIF *gtif)
{
    return gtif->gt_user_data;
}