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 1505 : GTIF* GTIFNew(void *tif)
55 :
56 : {
57 : TIFFMethod default_methods;
58 1505 : _GTIFSetDefaultTIFF( &default_methods );
59 :
60 1505 : return GTIFNewWithMethods( tif, &default_methods );
61 : }
62 :
63 0 : GTIF *GTIFNewSimpleTags( void *tif )
64 :
65 : {
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 1505 : GTIF* GTIFNewWithMethods(void *tif, TIFFMethod* methods)
81 : {
82 1505 : 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 1505 : memset( &tempData, 0, sizeof(tempData) );
91 1505 : gt = (GTIF*)_GTIFcalloc( sizeof(GTIF));
92 1505 : if (!gt) goto failure;
93 :
94 : /* install TIFF file and I/O methods */
95 1505 : gt->gt_tif = (tiff_t *)tif;
96 1505 : memcpy( >->gt_methods, methods, sizeof(TIFFMethod) );
97 :
98 : /* since this is an array, GTIF will allocate the memory */
99 3010 : if ( tif == NULL
100 1505 : || !(gt->gt_methods.get)(tif, GTIFF_GEOKEYDIRECTORY, >->gt_nshorts, &data ))
101 : {
102 : /* No ProjectionInfo, create a blank one */
103 642 : data=(pinfo_t*)_GTIFcalloc((4+MAX_VALUES)*sizeof(pinfo_t));
104 642 : if (!data) goto failure;
105 642 : header = (KeyHeader *)data;
106 642 : header->hdr_version = GvCurrentVersion;
107 642 : header->hdr_rev_major = GvCurrentRevision;
108 642 : header->hdr_rev_minor = GvCurrentMinorRev;
109 642 : gt->gt_nshorts=sizeof(KeyHeader)/sizeof(pinfo_t);
110 : }
111 1505 : gt->gt_short = data;
112 1505 : header = (KeyHeader *)data;
113 :
114 1505 : if (header->hdr_version > GvCurrentVersion) goto failure;
115 1505 : if (header->hdr_rev_major > GvCurrentRevision)
116 : {
117 : /* issue warning */
118 : }
119 :
120 : /* If we got here, then the geokey can be parsed */
121 1505 : count = header->hdr_num_keys;
122 1505 : gt->gt_num_keys = count;
123 1505 : gt->gt_version = header->hdr_version;
124 1505 : gt->gt_rev_major = header->hdr_rev_major;
125 1505 : gt->gt_rev_minor = header->hdr_rev_minor;
126 :
127 1505 : bufcount = count+MAX_KEYS; /* allow for expansion */
128 :
129 : /* Get the PARAMS Tags, if any */
130 3010 : if (tif == NULL
131 3010 : || !(gt->gt_methods.get)(tif, GTIFF_DOUBLEPARAMS,
132 1505 : >->gt_ndoubles, >->gt_double ))
133 : {
134 935 : gt->gt_double=(double*)_GTIFcalloc(MAX_VALUES*sizeof(double));
135 935 : if (!gt->gt_double) goto failure;
136 : }
137 3657 : if ( tif == NULL
138 1505 : || !(gt->gt_methods.get)(tif, GTIFF_ASCIIPARAMS,
139 : &tempData.tk_asciiParamsLength,
140 : &tempData.tk_asciiParams ))
141 : {
142 647 : tempData.tk_asciiParams = 0;
143 647 : tempData.tk_asciiParamsLength = 0;
144 : }
145 : else
146 : {
147 : /* last NULL doesn't count; "|" used for delimiter */
148 858 : --tempData.tk_asciiParamsLength;
149 : }
150 :
151 : /* allocate space for GeoKey array and its index */
152 1505 : gt->gt_keys = (GeoKey *)_GTIFcalloc( sizeof(GeoKey)*bufcount);
153 1505 : if (!gt->gt_keys) goto failure;
154 1505 : gt->gt_keyindex = (int *)_GTIFcalloc( sizeof(int)*(MAX_KEYINDEX+1));
155 1505 : if (!gt->gt_keyindex) goto failure;
156 :
157 : /* Loop to get all GeoKeys */
158 1505 : entptr = ((KeyEntry *)data) + 1;
159 1505 : keyptr = gt->gt_keys;
160 1505 : gt->gt_keymin = MAX_KEYINDEX;
161 1505 : gt->gt_keymax = 0;
162 7140 : for (index=1; index<=count; index++,entptr++)
163 : {
164 5635 : if (!ReadKey(gt, &tempData, entptr, ++keyptr))
165 0 : goto failure;
166 :
167 : /* Set up the index (start at 1, since 0=unset) */
168 5635 : gt->gt_keyindex[entptr->ent_key] = index;
169 : }
170 :
171 1505 : if( tempData.tk_asciiParams != NULL )
172 858 : _GTIFFree( tempData.tk_asciiParams );
173 :
174 1505 : return gt;
175 :
176 : failure:
177 : /* Notify of error */
178 0 : if( tempData.tk_asciiParams != NULL )
179 0 : _GTIFFree( tempData.tk_asciiParams );
180 0 : GTIFFree (gt);
181 0 : return (GTIF *)0;
182 : }
183 :
184 : /**********************************************************************
185 : *
186 : * Private Routines
187 : *
188 : **********************************************************************/
189 :
190 : /*
191 : * Given KeyEntry, read in the GeoKey value location and set up
192 : * the Key structure, returning 0 if failure.
193 : */
194 :
195 5635 : static int ReadKey(GTIF* gt, TempKeyData* tempData,
196 : KeyEntry* entptr, GeoKey* keyptr)
197 : {
198 : int offset,count;
199 :
200 5635 : keyptr->gk_key = entptr->ent_key;
201 5635 : keyptr->gk_count = entptr->ent_count;
202 5635 : count = entptr->ent_count;
203 5635 : offset = entptr->ent_val_offset;
204 5635 : if (gt->gt_keymin > keyptr->gk_key) gt->gt_keymin=keyptr->gk_key;
205 5635 : if (gt->gt_keymax < keyptr->gk_key) gt->gt_keymax=keyptr->gk_key;
206 :
207 5635 : if (entptr->ent_location)
208 2097 : keyptr->gk_type = (gt->gt_methods.type)(gt->gt_tif,entptr->ent_location);
209 : else
210 3538 : keyptr->gk_type = (gt->gt_methods.type)(gt->gt_tif,GTIFF_GEOKEYDIRECTORY);
211 :
212 5635 : switch (entptr->ent_location)
213 : {
214 : case GTIFF_LOCAL:
215 : /* store value into data value */
216 3538 : *(pinfo_t *)(&keyptr->gk_data) = entptr->ent_val_offset;
217 3538 : break;
218 : case GTIFF_GEOKEYDIRECTORY:
219 0 : keyptr->gk_data = (char *)(gt->gt_short+offset);
220 0 : if (gt->gt_nshorts < offset+count)
221 0 : gt->gt_nshorts = offset+count;
222 0 : break;
223 : case GTIFF_DOUBLEPARAMS:
224 1146 : keyptr->gk_data = (char *)(gt->gt_double+offset);
225 1146 : if (gt->gt_ndoubles < offset+count)
226 0 : gt->gt_ndoubles = offset+count;
227 1146 : break;
228 : case GTIFF_ASCIIPARAMS:
229 1905 : if( offset + count == tempData->tk_asciiParamsLength + 1
230 951 : && count > 0 )
231 : {
232 : /* some vendors seem to feel they should not use the
233 : terminating '|' char, but do include a terminating '\0'
234 : which we lose in the low level reading code.
235 : If this is the case, drop the extra character */
236 3 : count--;
237 : }
238 1896 : else if (offset < tempData->tk_asciiParamsLength
239 1896 : && offset + count > tempData->tk_asciiParamsLength )
240 : {
241 0 : count = tempData->tk_asciiParamsLength - offset;
242 : /* issue warning... if we could */
243 : }
244 948 : else if (offset + count > tempData->tk_asciiParamsLength)
245 0 : return (0);
246 :
247 951 : keyptr->gk_count = MAX(1,count+1);
248 951 : keyptr->gk_data = (char *) _GTIFcalloc (keyptr->gk_count);
249 :
250 951 : _GTIFmemcpy (keyptr->gk_data,
251 : tempData->tk_asciiParams + offset, count);
252 951 : if( keyptr->gk_data[MAX(0,count-1)] == '|' )
253 : {
254 948 : keyptr->gk_data[MAX(0,count-1)] = '\0';
255 948 : keyptr->gk_count = count;
256 : }
257 : else
258 3 : keyptr->gk_data[MAX(0,count)] = '\0';
259 951 : break;
260 : default:
261 0 : return 0; /* failure */
262 : }
263 5635 : keyptr->gk_size = _gtiff_size[keyptr->gk_type];
264 :
265 5635 : return 1; /* success */
266 : }
|