-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathread_geotiff.c
449 lines (403 loc) · 14.2 KB
/
read_geotiff.c
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
File: read_geotiff.c
Author: Jonathan Beezley <[email protected]>
Date: 1-18-2010
Functions for reading geotiff files, plus various utilities.
GeogridIndex get_index_from_geotiff(TIFF*) :
Populate GeogridIndex structure with information that can be
obtained from the geotiff tags.
float* get_tiff_buffer(TIFF*) :
Read the data from the tiff image file cast as an array of floats.
*/
#include "read_geotiff.h"
#ifdef RELATIVE_GTIFF
#include <geotiff/geo_tiffp.h>
#else
#include <geo_tiffp.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/* common buffer conversion code for different data formats */
#define CONV_CHAR_BUFFER_TO(_DTYPE,_DVAR) \
bufferasfloat=(float*) alloc_buffer(inx*iny*inz*sizeof(float)); \
for(i=0;i<inx*iny*inz;i++) { \
j=i*bytes_per_sample; \
_DVAR=*(_DTYPE*)(buffer+j); \
bufferasfloat[i]=(float) _DVAR; \
} \
free_buffer(buffer);
const int BIGENDIAN_TEST_VAL=1;
GeogridIndex get_index_from_geotiff(
TIFF *file /* handle to an open tiff file */
) {
GTIF *gtifh;
GTIFDefn gtifp;
int projid,count;
short modeltype;
GeogridIndex idx;
double stdpar1,stdpar2,stdlon,olat,olon;
double pixelscale[3];
uint32 inx,iny,inz;
uint16 orientation,format;
/* Try and initialize any fields possible. */
/* Invalid fields are filled with 0. */
gtifh = GTIFNew(file);
GTIFGetDefn(gtifh,>ifp);
GTIFKeyGet(gtifh,ProjStdParallel1GeoKey,&stdpar1,0,1);
idx.truelat1=stdpar1;
GTIFKeyGet(gtifh,ProjStdParallel2GeoKey,&stdpar2,0,1);
idx.truelat2=stdpar2;
GTIFKeyGet(gtifh,ProjCenterLongGeoKey,&stdlon,0,1);
idx.stdlon=stdlon;
TIFFGetField(file,GTIFF_PIXELSCALE,&count,&pixelscale);
idx.dx=pixelscale[0];
idx.dy=pixelscale[1];
/* Fill projection specific parameters. */
/* WARNING: This is far from robust and will likely break
for certain geotiff files. */
GTIFKeyGet(gtifh,GTModelTypeGeoKey,&modeltype,0,1);
projid=gtifp.CTProjection;
switch (projid) {
case CT_AlbersEqualArea:
idx.proj=albers_nad83;
GTIFKeyGet(gtifh,ProjNatOriginLatGeoKey,&olat,0,1);
idx.known_lat=olat;
GTIFKeyGet(gtifh,ProjNatOriginLongGeoKey,&olon,0,1);
idx.known_lon=olon;
break;
case CT_TransverseMercator:
idx.proj=mercator;
GTIFKeyGet(gtifh,ProjNatOriginLatGeoKey,&olat,0,1);
idx.known_lat=olat;
GTIFKeyGet(gtifh,ProjNatOriginLongGeoKey,&olon,0,1);
idx.known_lon=olon;
break;
case CT_PolarStereographic:
idx.proj=polar;
GTIFKeyGet(gtifh,ProjNatOriginLatGeoKey,&olat,0,1);
idx.known_lat=olat;
GTIFKeyGet(gtifh,ProjNatOriginLongGeoKey,&olon,0,1);
idx.known_lon=olon;
break;
case CT_LambertConfConic:
idx.proj=lambert;
GTIFKeyGet(gtifh,ProjFalseOriginLatGeoKey,&olat,0,1);
idx.known_lat=olat;
GTIFKeyGet(gtifh,ProjFalseOriginLongGeoKey,&olon,0,1);
idx.known_lon=olon;
break;
default :
if( modeltype == ModelTypeGeographic) {
idx.proj=regular_ll;
/*
GTIFKeyGet(gtifh,ProjCenterLatGeoKey,&olat,0,1);
idx.known_lat=olat;
GTIFKeyGet(gtifh,ProjCenterLongGeoKey,&olon,0,1);
idx.known_lon=olon; */
//GTIFKeyGet(gtifh,TIFFTAG_GEOPIXELSCALE,&dx,0,1);
//GTIFKeyGet(gtifh,TIFFTAG_GEOPIXELSCALE,&dy,1,1);
}
else {
fprintf(stderr,"Unknown projection ID: %i\n",projid);
exit(EXIT_FAILURE);
}
}
/* get coordinates of lower left corner */
olon=0;
olat=0;
idx.known_x=1;
idx.known_y=1;
if (modeltype == ModelTypeGeographic) {
if ( ! GTIFImageToPCS(gtifh,&olon,&olat) ) {
fprintf(stderr,"WARNING: cannot get coordinates of lower left corner.\n");
fprintf(stderr,"You will have to edit the index file manually.\n");
}
idx.known_lat=olat;
idx.known_lon=olon;
olat=1;
olon=1;
GTIFImageToPCS(gtifh,&olon,&olat);
if(idx.dx <= 0. && idx.dy <= 0) {
// As a last resort, get dx/dy from projection conversion.
idx.dx=(float)fabs(olon-(double)idx.known_lon);
idx.dy=(float)fabs(olat-(double)idx.known_lat);
}
}
else {
if ( ! GTIFImageToPCS(gtifh,&olon,&olat) ) {
fprintf(stderr,"WARNING: cannot get coordinates of lower left corner.\n");
fprintf(stderr,"You will have to edit the index file manually.\n");
}
if (! GTIFProj4ToLatLong(>ifp,1,&olon,&olat) ) {
fprintf(stderr,"WARNING: cannot convert from PCS to lat/lon.\n");
}
idx.known_lat=olat;
idx.known_lon=olon;
olat=1;
olon=1;
GTIFImageToPCS(gtifh,&olon,&olat);
GTIFProj4ToLatLong(>ifp,1,&olon,&olat);
if(idx.dx <= 0. && idx.dy <= 0) {
// As a last resort, get dx/dy from projection conversion.
idx.dx=(float)fabs(olon-(double)idx.known_lon);
idx.dy=(float)fabs(olat-(double)idx.known_lat);
}
}
/* fill parameters from TIFF i/o */
if( ! TIFFGetField(file,TIFFTAG_IMAGEWIDTH,&inx) ||
! TIFFGetField(file,TIFFTAG_IMAGELENGTH,&iny)) {
fprintf(stderr,"Could not find image dimensions in open file.\n");
exit(EXIT_FAILURE);
}
idx.nx=inx;
idx.ny=iny;
if( TIFFGetField(file,TIFFTAG_IMAGEDEPTH,&inz) ) idx.nz=inz;
else idx.nz=1;
idx.tz_s=0;
idx.tz_e=idx.tz_s+idx.nz-1;
/* get orientation of the data, defaults to TOPLEFT */
if ( ! TIFFGetField(file,TIFFTAG_ORIENTATION,&orientation) ) {
orientation=ORIENTATION_TOPLEFT;
}
switch (orientation) {
case ORIENTATION_TOPLEFT:
idx.bottom_top=0;
/* if TOPLEFT orientation pixel (0,0) is actually the top
left pixel, adjusting known_y here */
idx.known_y=idx.ny;
break;
case ORIENTATION_BOTLEFT:
idx.bottom_top=1;
break;
default:
fprintf(stderr,"Unsupported image orientation.\n");
exit(EXIT_FAILURE);
}
/* get the data type of the pixels, only supporting b/w images */
if( ! TIFFGetField(file,TIFFTAG_SAMPLEFORMAT,&format) )
format=SAMPLEFORMAT_UINT;
switch (format) {
case SAMPLEFORMAT_UINT:
idx.isigned=0;
break;
case SAMPLEFORMAT_INT:
idx.isigned=1;
break;
case SAMPLEFORMAT_IEEEFP:
idx.isigned=1;
break;
default:
fprintf(stderr,"Unsupport pixel format.\n");
exit(EXIT_FAILURE);
}
/* libtiff will always return the buffer in native machine endian */
if ( IS_BIGENDIAN() ) idx.endian=0;
else idx.endian=1;
/* free the geotiff handle, and return */
GTIFFree(gtifh);
return (idx);
}
unsigned char* alloc_buffer(tsize_t n) {
/* Use the libtiff macro for allocating an `n' byte array.
Typically, this is just a call to malloc. */
tdata_t buf;
buf=_TIFFmalloc(n);
//memset((void*)buf,0xEE,n);
return ( (unsigned char*) buf );
}
void free_buffer( unsigned char *buf ) {
/* Use the libtiff macro for deallocating an array.
Typically, this is just a call to free. */
_TIFFfree( (tdata_t) buf );
}
float* get_tiff_buffer(
TIFF *file /* handle to an open tiff file */
) {
/* Read tiff file into a buffer cast as float.
Allocates the buffer, should be freed by caller after use. */
uint32 inx,iny,inz;
tsize_t buffersize;
uint32 tileWidth, tileLength;
uint16 bits_per_sample,samples_per_pixel,sample_format,/*fill_order,*/bytes_per_sample;
long unsigned int stripMax,stripCount,i,j,k,i0,j0,j1,i1,cc,idx;
tsize_t stripSize;
unsigned long imageOffset,result;
unsigned char *buffer;
float *bufferasfloat;
uint8 iutemp8;
uint16 iutemp16;
uint32 iutemp32;
int8 itemp8;
int16 itemp16;
int32 itemp32;
double dtemp;
unsigned char *tilebuf,*tptr,*bptr;
/* get the global dimensions of the image */
if( ! TIFFGetField(file,TIFFTAG_IMAGEWIDTH,&inx) ||
! TIFFGetField(file,TIFFTAG_IMAGELENGTH,&iny)) {
fprintf(stderr,"Could not find image dimensions in open file.\n");
exit(EXIT_FAILURE);
}
if( ! TIFFGetField(file,TIFFTAG_IMAGEDEPTH,&inz) ) inz=1;
/* get the number of bits in a pixel, determines how to cast to output */
if( ! TIFFGetField(file,TIFFTAG_BITSPERSAMPLE,&bits_per_sample) ) {
fprintf(stderr,"Could not find TIFFTAG_BITSPERSAMPLE.\n");
exit(EXIT_FAILURE);
}
/* only 1,2, and 4 byte are supported for now */
switch (bits_per_sample) {
case 8:
bytes_per_sample=1;
break;
case 16:
bytes_per_sample=2;
break;
case 32:
bytes_per_sample=4;
break;
default:
fprintf(stderr,"Unsupport bits_per_sample=%i.\n",bits_per_sample);
exit(EXIT_FAILURE);
}
/* we only support scalar valued data (no color images) */
if( ! TIFFGetField(file,TIFFTAG_SAMPLESPERPIXEL,&samples_per_pixel) ) {
fprintf(stderr,"Could not find TIFFTAG_SAMPLESPERPIXEL.\n");
exit(EXIT_FAILURE);
}
if (samples_per_pixel != 1 ) {
fprintf(stderr,"Currently only single channel images (black and white) are supported.\n");
exit(EXIT_FAILURE);
}
if( ! TIFFGetField(file,TIFFTAG_SAMPLEFORMAT,&sample_format) )
sample_format=SAMPLEFORMAT_UINT;
/* This next bit was supposed to check bit ordering, but most files
don't seem to contain the information. TIFF should do the conversion anyway. */
/*
if( ! TIFFGetField(file,TIFFTAG_FILLORDER,&fill_order)
|| fill_order != FILLORDER_MSB2LSB) {
fprintf(stderr,"Undefined or unsupported bit order.\n");
exit(EXIT_FAILURE);
}
*/
/* allocate data buffer for image pixel size */
buffersize=((size_t) inx)*((size_t) iny)*((size_t) inz)*((size_t) samples_per_pixel)*((size_t) bytes_per_sample);
buffer=alloc_buffer(buffersize);
/* tiff images can be tiled or striped, we handle each seperately. */
if ( TIFFIsTiled(file) ) {
/* set up a buffer for reading each tile, this is copied to the global buffer */
tilebuf = _TIFFmalloc(TIFFTileSize(file));
/* get the tile dimensions in bytes */
TIFFGetField(file, TIFFTAG_TILEWIDTH, &tileWidth);
TIFFGetField(file, TIFFTAG_TILELENGTH, &tileLength);
for(i=0;i<buffersize;i++) buffer[i]=2;
for(k=0;k<inz;k++) { /* loop over vertical levels */
for(j=0;j<iny;j += tileLength) { /* loop over columns of tiles */
for(i=0;i<inx;i += tileWidth) { /* loop over rows of tiles */
/* read the tile into memory, check for error */
//memset((void*)tilebuf,0xFF,TIFFTileSize(file));
if( (result=TIFFReadTile (file,tilebuf,i,j,k,0)) == -1){
fprintf(stderr, "Read error on input tile number %lu,%lu\n", i,j);
exit(EXIT_FAILURE);
}
/* copy tile into global buffer */
cc=0;
tptr=tilebuf;
for(j0=0;j0<tileLength;j0++) { /* loop over columns in the tile */
j1=j0+j;
i1=i;
/* here we set a pointer to the first element of current column in the current tile. */
idx=k*inx*iny+j1*inx+i1;
if(idx < inx*iny*inz) {
bptr=( buffer + (k*inx*iny+j1*inx+i1)*bytes_per_sample);
//fprintf(stdout,"%i\n",k*inx*bytes_per_sample*iny*bytes_per_sample+j1*inx*bytes_per_sample+i1);
for(i0=0;i0<tileWidth*bytes_per_sample;i0++) {
//fprintf(stdout,"%i ",*(unsigned char*)tptr);
*bptr++=*tptr++;
cc++; /* keep track of number of bytes copied to compare to what was read from TIFFReadTile */
} /* i0 */
}
//fprintf(stdout,"\n");
} /* j0 */
/* sanity check my programming skills */
//if (cc > result) { /* this might be possible for imcomplete tiles */
// fprintf(stderr,"WARNING: Tile size=%i < copy size=%i. This could indicate a bug.\n",(int)result,(int)cc);
//}
//if (cc < result) { /* this is almost certainly a bug */
// fprintf(stderr,"ERROR: Tile size=%i > copy size=%i!\n",(int)result,(int)cc);
// }
} /* i */
} /* j */
} /* k */
} /* endif tiled */
else {
/* Read in the possibly multiple strips. This is easier than tiles because
we don't have to worry about strides. */
stripSize = TIFFStripSize (file);
stripMax = TIFFNumberOfStrips (file);
imageOffset = 0;
for (stripCount = 0; stripCount < stripMax; stripCount++){
if((result = TIFFReadEncodedStrip (file, stripCount,
buffer + imageOffset,
stripSize)) == -1){
fprintf(stderr, "Read error on input strip number %lu\n", stripCount);
exit(EXIT_FAILURE);
}
imageOffset += result;
}
}
/* convert image buffer into float */
switch (sample_format) {
case SAMPLEFORMAT_UINT:
switch (bytes_per_sample) {
case 1:
CONV_CHAR_BUFFER_TO(uint8,iutemp8)
break;
case 2:
CONV_CHAR_BUFFER_TO(uint16,iutemp16)
break;
case 4:
CONV_CHAR_BUFFER_TO(uint32,iutemp32)
break;
default:
fprintf(stderr,"Unsupported bytes per sample=%i for uint.\n",bytes_per_sample);
exit(EXIT_FAILURE);
}
break;
case SAMPLEFORMAT_INT:
switch (bytes_per_sample) {
case 1:
CONV_CHAR_BUFFER_TO(int8,itemp8)
break;
case 2:
CONV_CHAR_BUFFER_TO(int16,itemp16)
break;
case 4:
CONV_CHAR_BUFFER_TO(int32,itemp32)
break;
default:
fprintf(stderr,"Unsupported bytes per sample=%i for int.\n",bytes_per_sample);
exit(EXIT_FAILURE);
}
break;
case SAMPLEFORMAT_IEEEFP:
switch (bytes_per_sample) {
case sizeof(float):
/* no conversion needs to be done if image is single precision float */
bufferasfloat = (float*) buffer;
break;
case sizeof(double):
CONV_CHAR_BUFFER_TO(double,dtemp)
break;
default:
fprintf(stderr,"Unsupported bytes per sample=%i for IEEEFP.\n",bytes_per_sample);
exit(EXIT_FAILURE);
}
break;
default:
fprintf(stderr,"Unsupported data type in image.\n");
exit(EXIT_FAILURE);
}
return bufferasfloat;
}