-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeopixval2.c
137 lines (104 loc) · 3.26 KB
/
geopixval2.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
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <math.h>
#include "tiffio.h"
int verbose = 1;
// a.out filename lat long
int main(int argc, char *argv[]) {
char *fname = argv[1];
float lat = atof(argv[2]);
float lng = atof(argv[3]);
int xsize=0, ysize=0, dtype=0, bs=0;
TIFF *tif = (TIFF *)0; /* TIFF-level descriptor */
if( verbose ) printf("%s %f %f\n", fname, lat, lng);
tif = TIFFOpen(fname, "r");
if (!tif) {
printf("Failed opeing %s\n", fname);
exit(-1);
};
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &xsize);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &ysize);
TIFFGetField(tif, TIFFTAG_DATATYPE, &dtype);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bs);
if( verbose ) printf("Size %d %d type %d bps %d\n", xsize, ysize, dtype, bs);
// GTIFPrint(gtif,0,0);
#define TIFFTAG_GEOPIXELSCALE 33550
#define TIFFTAG_GEOTIEPOINTS 33922
double *data;
int count;
double xmin, ymax, xres, yres;
if (TIFFGetField(tif, TIFFTAG_GEOTIEPOINTS, &count, &data)) {
if( verbose ) {
printf("TIFFTAG_GEOTIEPOINTS: ");
for (int i = 0; i < count; i++) {
printf("%-17.15g ", data[i]);
}
printf("\n");
}
xmin = data[3]; // min long
ymax = data[4]; // max lat
}
if (TIFFGetField(tif, TIFFTAG_GEOPIXELSCALE, &count, &data)) {
if( verbose ) {
printf("TIFFTAG_PIXELSCALE: ");
for (int i = 0; i < count; i++) {
printf("%-17.15g ", data[i]);
}
printf("\n");
}
xres = data[0];
yres = data[1];
}
if( verbose ) printf("%f %f %f %f\n", xmin, ymax, xres, yres);
// find pixel of interest
long pixX = lround((lng - xmin) / xres);
long pixY = lround((ymax - lat) / yres);
if( pixX < 0 || pixX > xsize) printf("Invalid pixX %ld\n", pixX);
if( pixY < 0 || pixY > ysize) printf("Invalid pixY %ld\n", pixY);
// find position in buffer
long pos = pixY*xsize + pixX;
if( pos < 0 || pos > xsize*ysize ) printf("Invalid pos %ld\n", pos);
if( verbose ) printf("pixel x:%ld y:%ld pos:%ld\n", pixX, pixY, pos);
tstrip_t numstrips = TIFFNumberOfStrips(tif);
tstrip_t stripsize = TIFFStripSize(tif);
char* buf = malloc(numstrips * stripsize);
unsigned long imageOffset = 0;
unsigned long result;
if( verbose ) printf("strips %u size %u\n", numstrips, stripsize);
for(tstrip_t strip=0; strip < numstrips; strip++) {
TIFFReadEncodedStrip(tif,strip, &buf[strip*stripsize],(tsize_t) -1);
if((result = TIFFReadEncodedStrip (tif, strip, buf + imageOffset, stripsize)) == -1) {
fprintf(stderr, "Read error on input strip number %d\n", strip);
exit(42);
}
imageOffset += result;
}
if( bs == 16 ) {
uint16* ubuf = (uint16*)buf;
if( verbose ) printf("val %d\n", ubuf[pos]);
//for( int r=0; r<ysize; r++) {
// printf( "R: %d - ", r );
// for( int c=0; c< ysize; c++) {
// int pos = r*xsize + c;
// printf("%02d ", ubuf[pos]);;
// }
// printf("\n");
//}
} else if(bs == 8 ) {
uint8* ubuf = (uint8*)buf;
if( verbose ) printf("val %d\n", ubuf[pos]);
//for( int r=0; r<ysize; r++) {
// printf( "R: %d - ", r );
// for( int c=0; c< ysize; c++) {
// int pos = r*xsize + c;
// printf("%02d ", ubuf[pos]);;
// }
// printf("\n");
//}
} else {
printf("Invalid bs %d\n", bs);
}
free(buf);
if (tif) TIFFClose(tif);
}