forked from joseamidesfigueroa/ojo-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeopixval.c
189 lines (147 loc) · 4.34 KB
/
geopixval.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
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <math.h>
//#include "geotiff.h"
#include "xtiffio.h"
#include "geo_normalize.h"
#include "geo_simpletags.h"
#include "geovalues.h"
#include "tiffio.h"
#include "geo_tiffp.h" /* external TIFF interface */
#include "geo_keyp.h" /* private interface */
#include "geokeys.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, ysize, dtype, bs;
TIFF *tif = (TIFF *)0; /* TIFF-level descriptor */
GTIF *gtif = (GTIF *)0; /* GeoKey-level descriptor */
if( verbose ) printf("%s %f %f\n", fname, lat, lng);
tif = XTIFFOpen(fname, "r");
if (!tif) {
printf("Failed opeing %s\n", fname);
exit(-1);
};
gtif = GTIFNew(tif);
if (!gtif) {
fprintf(stderr, "failed in GTIFNew\n");
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 ((gtif->gt_methods.get)(tif, GTIFF_TIEPOINTS, &count, &data)) {
if( verbose ) {
printf("GTIFF_TIEPOINTS: ");
for (int i = 0; i < count; i++) {
printf("%-17.15g ", data[i]);
}
printf("\n");
}
xmin = data[3]; // min long
ymax = data[4]; // max lat
_GTIFFree(data);
}
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");
}
}
if ((gtif->gt_methods.get)(tif, GTIFF_PIXELSCALE, &count, &data )) {
if( verbose ) {
printf("GTIFF_PIXELSCALE: ");
for (int i = 0; i < count; i++) {
printf("%-17.15g ", data[i]);
}
printf("\n");
}
xres = data[0];
yres = data[1];
_GTIFFree(data);
}
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");
}
}
if ((gtif->gt_methods.get)(tif, GTIFF_TRANSMATRIX, &count, &data )) {
if( verbose ) {
printf("GTIFF_TRANSMATRIX: ");
for (int i = 0; i < count; i++) {
printf("%-17.15g ", data[i]);
}
printf("\n");
}
_GTIFFree(data);
}
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");
//}
}
free(buf);
if (tif) XTIFFClose(tif);
if (gtif) GTIFFree(gtif);
}