/src/gdal/frmts/gtiff/libgeotiff/geo_write.c
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * geo_write.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 source code derived therefrom. |
11 | | * |
12 | | **********************************************************************/ |
13 | | |
14 | | #include "geotiffio.h" /* public interface */ |
15 | | #include "geo_tiffp.h" /* external TIFF interface */ |
16 | | #include "geo_keyp.h" /* private interface */ |
17 | | |
18 | | static int WriteKey(GTIF* gt, TempKeyData* tempData, |
19 | | KeyEntry* entptr, GeoKey* keyptr); |
20 | | static int SortKeys(GTIF* gt,int *sortkeys); |
21 | | |
22 | | |
23 | | /** |
24 | | This function flushes all the GeoTIFF keys that have been set with the |
25 | | GTIFKeySet() function into the associated |
26 | | TIFF file. |
27 | | |
28 | | @param gt The GeoTIFF handle returned by GTIFNew. |
29 | | |
30 | | GTIFWriteKeys() should be called before |
31 | | GTIFFree() is used to deallocate a GeoTIFF access handle. |
32 | | */ |
33 | | |
34 | | int GTIFWriteKeys(GTIF *gt) |
35 | 198 | { |
36 | | |
37 | 198 | if (!(gt->gt_flags & FLAG_FILE_MODIFIED)) return 1; |
38 | | |
39 | 135 | if( gt->gt_tif == NULL ) |
40 | 0 | return 0; |
41 | | |
42 | 135 | TempKeyData tempData; |
43 | 135 | tempData.tk_asciiParams = 0; |
44 | 135 | tempData.tk_asciiParamsLength = 0; |
45 | 135 | tempData.tk_asciiParamsOffset = 0; |
46 | | |
47 | | /* Sort the Keys into numerical order */ |
48 | 135 | int sortkeys[MAX_KEYS]; |
49 | 135 | if (!SortKeys(gt,sortkeys)) |
50 | 0 | { |
51 | | /* XXX error: a key was not recognized */ |
52 | 0 | } |
53 | | |
54 | | /* Set up header of ProjectionInfo tag */ |
55 | 135 | KeyHeader *header = (KeyHeader *)gt->gt_short; |
56 | 135 | header->hdr_num_keys = (pinfo_t) gt->gt_num_keys; |
57 | 135 | header->hdr_version = gt->gt_version; |
58 | 135 | header->hdr_rev_major = gt->gt_rev_major; |
59 | 135 | header->hdr_rev_minor = gt->gt_rev_minor; |
60 | | |
61 | | /* Sum up the ASCII tag lengths */ |
62 | 1.35k | for (int i = 0; i < gt->gt_num_keys; i++) |
63 | 1.22k | { |
64 | 1.22k | GeoKey *keyptr = gt->gt_keys + sortkeys[i]; |
65 | 1.22k | if (keyptr->gk_type == TYPE_ASCII) |
66 | 185 | { |
67 | 185 | tempData.tk_asciiParamsLength += keyptr->gk_count; |
68 | 185 | } |
69 | 1.22k | } |
70 | 135 | if (tempData.tk_asciiParamsLength > 0) |
71 | 119 | { |
72 | 119 | tempData.tk_asciiParams = |
73 | 119 | (char *)_GTIFcalloc(tempData.tk_asciiParamsLength + 1); |
74 | 119 | if( tempData.tk_asciiParams == NULL ) |
75 | 0 | return 0; |
76 | 119 | tempData.tk_asciiParams[tempData.tk_asciiParamsLength] = '\0'; |
77 | 119 | } |
78 | | |
79 | | /* Set up the rest of SHORT array properly */ |
80 | 135 | GeoKey *keyptr = gt->gt_keys; |
81 | 135 | KeyEntry *entptr = (KeyEntry*)(gt->gt_short + 4); |
82 | 1.35k | for (int i=0; i< gt->gt_num_keys; i++,entptr++) |
83 | 1.22k | { |
84 | 1.22k | if (!WriteKey(gt,&tempData,entptr,keyptr+sortkeys[i])) |
85 | 0 | { |
86 | 0 | if (tempData.tk_asciiParamsLength > 0) |
87 | 0 | { |
88 | 0 | _GTIFFree (tempData.tk_asciiParams); |
89 | 0 | } |
90 | 0 | return 0; |
91 | 0 | } |
92 | 1.22k | } |
93 | | |
94 | | /* Write out the Key Directory */ |
95 | 135 | (gt->gt_methods.set)(gt->gt_tif, GTIFF_GEOKEYDIRECTORY, gt->gt_nshorts, gt->gt_short ); |
96 | | |
97 | | /* Write out the params directories */ |
98 | 135 | if (gt->gt_ndoubles) |
99 | 86 | (gt->gt_methods.set)(gt->gt_tif, GTIFF_DOUBLEPARAMS, gt->gt_ndoubles, gt->gt_double ); |
100 | 135 | if (tempData.tk_asciiParamsLength > 0) |
101 | 119 | { |
102 | | /* just to be safe */ |
103 | 119 | tempData.tk_asciiParams[tempData.tk_asciiParamsLength] = '\0'; |
104 | 119 | (gt->gt_methods.set)(gt->gt_tif, |
105 | 119 | GTIFF_ASCIIPARAMS, 0, tempData.tk_asciiParams); |
106 | 119 | } |
107 | | |
108 | 135 | gt->gt_flags &= ~FLAG_FILE_MODIFIED; |
109 | | |
110 | 135 | if (tempData.tk_asciiParamsLength > 0) |
111 | 119 | { |
112 | 119 | _GTIFFree (tempData.tk_asciiParams); |
113 | 119 | } |
114 | 135 | return 1; |
115 | 135 | } |
116 | | |
117 | | /********************************************************************** |
118 | | * |
119 | | * Private Routines |
120 | | * |
121 | | **********************************************************************/ |
122 | | |
123 | | /* |
124 | | * Given GeoKey, write out the KeyEntry entries, returning 0 if failure. |
125 | | * This is the exact complement of ReadKey(). |
126 | | */ |
127 | | |
128 | | static int WriteKey(GTIF* gt, TempKeyData* tempData, |
129 | | KeyEntry* entptr, GeoKey* keyptr) |
130 | 1.22k | { |
131 | 1.22k | entptr->ent_key = (pinfo_t) keyptr->gk_key; |
132 | 1.22k | entptr->ent_count = (pinfo_t) keyptr->gk_count; |
133 | 1.22k | const int count = entptr->ent_count; |
134 | | |
135 | 1.22k | if (count==1 && keyptr->gk_type==TYPE_SHORT) |
136 | 679 | { |
137 | 679 | entptr->ent_location = GTIFF_LOCAL; |
138 | 679 | memcpy(&(entptr->ent_val_offset), &keyptr->gk_data, sizeof(pinfo_t)); |
139 | 679 | return 1; |
140 | 679 | } |
141 | | |
142 | 543 | switch (keyptr->gk_type) |
143 | 543 | { |
144 | 0 | case TYPE_SHORT: |
145 | 0 | entptr->ent_location = GTIFF_GEOKEYDIRECTORY; |
146 | 0 | entptr->ent_val_offset = (pinfo_t) |
147 | 0 | ((pinfo_t*)keyptr->gk_data - gt->gt_short); |
148 | 0 | break; |
149 | 358 | case TYPE_DOUBLE: |
150 | 358 | entptr->ent_location = GTIFF_DOUBLEPARAMS; |
151 | 358 | entptr->ent_val_offset = (pinfo_t) |
152 | 358 | ((double*)keyptr->gk_data - gt->gt_double); |
153 | 358 | break; |
154 | 185 | case TYPE_ASCII: |
155 | 185 | if( tempData->tk_asciiParams == NULL ) |
156 | 0 | return 0; |
157 | 185 | entptr->ent_location = GTIFF_ASCIIPARAMS; |
158 | 185 | entptr->ent_val_offset = (pinfo_t) tempData->tk_asciiParamsOffset; |
159 | 185 | _GTIFmemcpy (tempData->tk_asciiParams + tempData->tk_asciiParamsOffset |
160 | 185 | , keyptr->gk_data, keyptr->gk_count); |
161 | 185 | tempData->tk_asciiParams[tempData->tk_asciiParamsOffset+keyptr->gk_count-1] = '|'; |
162 | 185 | tempData->tk_asciiParamsOffset += keyptr->gk_count; |
163 | 185 | break; |
164 | 0 | default: |
165 | 0 | return 0; /* failure */ |
166 | 543 | } |
167 | | |
168 | 543 | return 1; /* success */ |
169 | 543 | } |
170 | | |
171 | | |
172 | | /* |
173 | | * Numerically sort the GeoKeys. |
174 | | * We just do a linear search through |
175 | | * the list and pull out the keys that were set. |
176 | | */ |
177 | | |
178 | | static int SortKeys(GTIF* gt,int *sortkeys) |
179 | 135 | { |
180 | | /* A bit convoluted to make Clang Static Analyzer happy */ |
181 | 135 | if( gt->gt_num_keys <= 0 ) |
182 | 0 | return 1; |
183 | | |
184 | 135 | sortkeys[0] = 1; |
185 | 1.22k | for( int i = 1; i < gt->gt_num_keys; i++ ) |
186 | 1.08k | sortkeys[i] = i+1; |
187 | | |
188 | 135 | int did_work; |
189 | 851 | do { /* simple bubble sort */ |
190 | 851 | did_work = 0; |
191 | 10.5k | for( int i = 0; i < gt->gt_num_keys-1; i++ ) |
192 | 9.67k | { |
193 | 9.67k | if( gt->gt_keys[sortkeys[i]].gk_key |
194 | 9.67k | > gt->gt_keys[sortkeys[i+1]].gk_key ) |
195 | 2.82k | { |
196 | | /* swap keys in sort list */ |
197 | 2.82k | int j = sortkeys[i]; |
198 | 2.82k | sortkeys[i] = sortkeys[i+1]; |
199 | 2.82k | sortkeys[i+1] = j; |
200 | | |
201 | 2.82k | did_work = 1; |
202 | 2.82k | } |
203 | 9.67k | } |
204 | 851 | } while( did_work ); |
205 | | |
206 | 135 | return 1; |
207 | 135 | } |