1 : /**********************************************************************
2 : *
3 : * geo_new.c -- Public routines for GEOTIFF GeoKey access.
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 : * 20 June, 1995 Niles D. Ritter New
13 : * 7 July, 1995 Greg Martin Fix index
14 : *
15 : **********************************************************************/
16 :
17 : #include "geotiffio.h" /* public interface */
18 : #include "geo_tiffp.h" /* external TIFF interface */
19 : #include "geo_keyp.h" /* private interface */
20 : #include "geo_simpletags.h"
21 :
22 : /* private local routines */
23 : static int ReadKey(GTIF* gt, TempKeyData* tempData,
24 : KeyEntry* entptr, GeoKey* keyptr);
25 :
26 :
27 : /**********************************************************************
28 : *
29 : * Public Routines
30 : *
31 : **********************************************************************/
32 :
33 :
34 : /**
35 : * Given an open TIFF file, look for GTIF keys and
36 : * values and return GTIF structure.
37 :
38 : This function creates a GeoTIFF information interpretation handle
39 : (GTIF *) based on a passed in TIFF handle originally from
40 : XTIFFOpen(). Even though the argument
41 : (<b>tif</b>) is shown as type <tt>void *</tt>, it is really normally
42 : of type <tt>TIFF *</tt>.<p>
43 :
44 : The returned GTIF handle can be used to read or write GeoTIFF tags
45 : using the various GTIF functions. The handle should be destroyed using
46 : GTIFFree() before the file is closed with TIFFClose().<p>
47 :
48 : If the file accessed has no GeoTIFF keys, an valid (but empty) GTIF is
49 : still returned. GTIFNew() is used both for existing files being read, and
50 : for new TIFF files that will have GeoTIFF tags written to them.<p>
51 :
52 : */
53 :
54 : GTIF* GTIFNew(void *tif)
55 :
56 1724 : {
57 : TIFFMethod default_methods;
58 1724 : _GTIFSetDefaultTIFF( &default_methods );
59 :
60 1724 : return GTIFNewWithMethods( tif, &default_methods );
61 : }
62 :
63 : GTIF *GTIFNewSimpleTags( void *tif )
64 :
65 0 : {
66 : TIFFMethod default_methods;
67 0 : GTIFSetSimpleTagsMethods( &default_methods );
68 :
69 0 : return GTIFNewWithMethods( tif, &default_methods );
70 : }
71 :
72 : /************************************************************************/
73 : /* GTIFNewWithMethods() */
74 : /* */
75 : /* Create a new geotiff, passing in the methods structure to */
76 : /* support not libtiff implementations without replacing the */
77 : /* default methods. */
78 : /************************************************************************/
79 :
80 : GTIF* GTIFNewWithMethods(void *tif, TIFFMethod* methods)
81 1724 : {
82 1724 : GTIF* gt=(GTIF*)0;
83 : int count,bufcount,index;
84 : GeoKey *keyptr;
85 : pinfo_t *data;
86 : KeyEntry *entptr;
87 : KeyHeader *header;
88 : TempKeyData tempData;
89 :
90 1724 : memset( &tempData, 0, sizeof(tempData) );
91 1724 : gt = (GTIF*)_GTIFcalloc( sizeof(GTIF));
92 1724 : if (!gt) goto failure;
93 :
94 : /* install TIFF file and I/O methods */
95 1724 : gt->gt_tif = (tiff_t *)tif;
96 1724 : memcpy( >->gt_methods, methods, sizeof(TIFFMethod) );
97 :
98 : /* since this is an array, GTIF will allocate the memory */
99 2447 : if ( tif == NULL
100 : || !(gt->gt_methods.get)(tif, GTIFF_GEOKEYDIRECTORY, >->gt_nshorts, &data ))
101 : {
102 : /* No ProjectionInfo, create a blank one */
103 723 : data=(pinfo_t*)_GTIFcalloc((4+MAX_VALUES)*sizeof(pinfo_t));
104 723 : if (!data) goto failure;
105 723 : header = (KeyHeader *)data;
106 723 : header->hdr_version = GvCurrentVersion;
107 723 : header->hdr_rev_major = GvCurrentRevision;
108 723 : header->hdr_rev_minor = GvCurrentMinorRev;
109 723 : gt->gt_nshorts=sizeof(KeyHeader)/sizeof(pinfo_t);
110 : }
111 : else
112 : {
113 : /* resize data array so it can be extended if needed */
114 1001 : data = (pinfo_t*) _GTIFrealloc(data,(4+MAX_VALUES)*sizeof(pinfo_t));
115 : }
116 1724 : gt->gt_short = data;
117 1724 : header = (KeyHeader *)data;
118 :
119 1724 : if (header->hdr_version > GvCurrentVersion) goto failure;
120 1724 : if (header->hdr_rev_major > GvCurrentRevision)
121 : {
122 : /* issue warning */
123 : }
124 :
125 : /* If we got here, then the geokey can be parsed */
126 1724 : count = header->hdr_num_keys;
127 :
128 1724 : if (count * sizeof(KeyEntry) >= (4 + MAX_VALUES) * sizeof(pinfo_t))
129 0 : goto failure;
130 :
131 1724 : gt->gt_num_keys = count;
132 1724 : gt->gt_version = header->hdr_version;
133 1724 : gt->gt_rev_major = header->hdr_rev_major;
134 1724 : gt->gt_rev_minor = header->hdr_rev_minor;
135 :
136 1724 : bufcount = count+MAX_KEYS; /* allow for expansion */
137 :
138 : /* Get the PARAMS Tags, if any */
139 2819 : if (tif == NULL
140 : || !(gt->gt_methods.get)(tif, GTIFF_DOUBLEPARAMS,
141 : >->gt_ndoubles, >->gt_double ))
142 : {
143 1095 : gt->gt_double=(double*)_GTIFcalloc(MAX_VALUES*sizeof(double));
144 1095 : if (!gt->gt_double) goto failure;
145 : }
146 : else
147 : {
148 : /* resize data array so it can be extended if needed */
149 629 : gt->gt_double = (double*) _GTIFrealloc(gt->gt_double,
150 : (MAX_VALUES)*sizeof(double));
151 : }
152 2452 : if ( tif == NULL
153 : || !(gt->gt_methods.get)(tif, GTIFF_ASCIIPARAMS,
154 : &tempData.tk_asciiParamsLength,
155 : &tempData.tk_asciiParams ))
156 : {
157 728 : tempData.tk_asciiParams = 0;
158 728 : tempData.tk_asciiParamsLength = 0;
159 : }
160 : else
161 : {
162 : /* last NULL doesn't count; "|" used for delimiter */
163 996 : --tempData.tk_asciiParamsLength;
164 : }
165 :
166 : /* allocate space for GeoKey array and its index */
167 1724 : gt->gt_keys = (GeoKey *)_GTIFcalloc( sizeof(GeoKey)*bufcount);
168 1724 : if (!gt->gt_keys) goto failure;
169 1724 : gt->gt_keyindex = (int *)_GTIFcalloc( sizeof(int)*(MAX_KEYINDEX+1));
170 1724 : if (!gt->gt_keyindex) goto failure;
171 :
172 : /* Loop to get all GeoKeys */
173 1724 : entptr = ((KeyEntry *)data) + 1;
174 1724 : keyptr = gt->gt_keys;
175 1724 : gt->gt_keymin = MAX_KEYINDEX;
176 1724 : gt->gt_keymax = 0;
177 8183 : for (index=1; index<=count; index++,entptr++)
178 : {
179 6459 : if (!ReadKey(gt, &tempData, entptr, ++keyptr))
180 0 : goto failure;
181 :
182 : /* Set up the index (start at 1, since 0=unset) */
183 6459 : gt->gt_keyindex[entptr->ent_key] = index;
184 : }
185 :
186 1724 : if( tempData.tk_asciiParams != NULL )
187 996 : _GTIFFree( tempData.tk_asciiParams );
188 :
189 1724 : return gt;
190 :
191 0 : failure:
192 : /* Notify of error */
193 0 : if( tempData.tk_asciiParams != NULL )
194 0 : _GTIFFree( tempData.tk_asciiParams );
195 0 : GTIFFree (gt);
196 0 : return (GTIF *)0;
197 : }
198 :
199 : /**********************************************************************
200 : *
201 : * Private Routines
202 : *
203 : **********************************************************************/
204 :
205 : /*
206 : * Given KeyEntry, read in the GeoKey value location and set up
207 : * the Key structure, returning 0 if failure.
208 : */
209 :
210 : static int ReadKey(GTIF* gt, TempKeyData* tempData,
211 : KeyEntry* entptr, GeoKey* keyptr)
212 6459 : {
213 : int offset,count;
214 :
215 6459 : keyptr->gk_key = entptr->ent_key;
216 6459 : keyptr->gk_count = entptr->ent_count;
217 6459 : count = entptr->ent_count;
218 6459 : offset = entptr->ent_val_offset;
219 6459 : if (gt->gt_keymin > keyptr->gk_key) gt->gt_keymin=keyptr->gk_key;
220 6459 : if (gt->gt_keymax < keyptr->gk_key) gt->gt_keymax=keyptr->gk_key;
221 :
222 6459 : if (entptr->ent_location)
223 2339 : keyptr->gk_type = (gt->gt_methods.type)(gt->gt_tif,entptr->ent_location);
224 : else
225 4120 : keyptr->gk_type = (gt->gt_methods.type)(gt->gt_tif,GTIFF_GEOKEYDIRECTORY);
226 :
227 6459 : switch (entptr->ent_location)
228 : {
229 : case GTIFF_LOCAL:
230 : /* store value into data value */
231 4120 : *(pinfo_t *)(&keyptr->gk_data) = entptr->ent_val_offset;
232 4120 : break;
233 : case GTIFF_GEOKEYDIRECTORY:
234 0 : keyptr->gk_data = (char *)(gt->gt_short+offset);
235 0 : if (gt->gt_nshorts < offset+count)
236 0 : gt->gt_nshorts = offset+count;
237 0 : break;
238 : case GTIFF_DOUBLEPARAMS:
239 1202 : keyptr->gk_data = (char *)(gt->gt_double+offset);
240 1202 : if (gt->gt_ndoubles < offset+count)
241 0 : gt->gt_ndoubles = offset+count;
242 1202 : break;
243 : case GTIFF_ASCIIPARAMS:
244 1137 : if( offset + count == tempData->tk_asciiParamsLength + 1
245 : && count > 0 )
246 : {
247 : /* some vendors seem to feel they should not use the
248 : terminating '|' char, but do include a terminating '\0'
249 : which we lose in the low level reading code.
250 : If this is the case, drop the extra character */
251 0 : count--;
252 : }
253 1137 : else if (offset < tempData->tk_asciiParamsLength
254 : && offset + count > tempData->tk_asciiParamsLength )
255 : {
256 0 : count = tempData->tk_asciiParamsLength - offset;
257 : /* issue warning... if we could */
258 : }
259 1137 : else if (offset + count > tempData->tk_asciiParamsLength)
260 0 : return (0);
261 :
262 1137 : keyptr->gk_count = MAX(1,count+1);
263 1137 : keyptr->gk_data = (char *) _GTIFcalloc (keyptr->gk_count);
264 :
265 1137 : _GTIFmemcpy (keyptr->gk_data,
266 : tempData->tk_asciiParams + offset, count);
267 1137 : if( keyptr->gk_data[MAX(0,count-1)] == '|' )
268 : {
269 1137 : keyptr->gk_data[MAX(0,count-1)] = '\0';
270 1137 : keyptr->gk_count = count;
271 : }
272 : else
273 0 : keyptr->gk_data[MAX(0,count)] = '\0';
274 1137 : break;
275 : default:
276 0 : return 0; /* failure */
277 : }
278 6459 : keyptr->gk_size = _gtiff_size[keyptr->gk_type];
279 :
280 6459 : return 1; /* success */
281 : }
|