-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpsinfo.c
333 lines (289 loc) · 11.4 KB
/
gpsinfo.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
//--------------------------------------------------------------------------
// Parsing of GPS info from exif header.
//
// Matthias Wandel, Dec 1999 - Dec 2002
//--------------------------------------------------------------------------
#include "jhead.h"
#include <string.h>
#define TAG_GPS_LAT_REF 1
#define TAG_GPS_LAT 2
#define TAG_GPS_LONG_REF 3
#define TAG_GPS_LONG 4
#define TAG_GPS_ALT_REF 5
#define TAG_GPS_ALT 6
#define TAG_GPS_TIMESTAMP 7
#define TAG_GPS_PROCESSING_METHOD 27
#define TAG_GPS_DATESTAMP 29
static TagTable_t GpsTags[]= {
{ 0x00, "GPSVersionID", FMT_BYTE, 4},
{ 0x01, "GPSLatitudeRef", FMT_STRING, 2},
{ 0x02, "GPSLatitude", FMT_URATIONAL, 3},
{ 0x03, "GPSLongitudeRef", FMT_STRING, 2},
{ 0x04, "GPSLongitude", FMT_URATIONAL, 3},
{ 0x05, "GPSAltitudeRef", FMT_BYTE, 1},
{ 0x06, "GPSAltitude", FMT_URATIONAL, 1},
{ 0x07, "GPSTimeStamp", FMT_SRATIONAL, 3},
{ 0x08, "GPSSatellites", FMT_STRING, -1},
{ 0x09, "GPSStatus", FMT_STRING, 2},
{ 0x0A, "GPSMeasureMode", FMT_STRING, 2},
{ 0x0B, "GPSDOP", FMT_SRATIONAL, 1},
{ 0x0C, "GPSSpeedRef", FMT_STRING, 2},
{ 0x0D, "GPSSpeed", FMT_SRATIONAL, 1},
{ 0x0E, "GPSTrackRef", FMT_STRING, 2},
{ 0x0F, "GPSTrack", FMT_SRATIONAL, 1},
{ 0x10, "GPSImgDirectionRef", FMT_STRING, -1},
{ 0x11, "GPSImgDirection", FMT_SRATIONAL, 1},
{ 0x12, "GPSMapDatum", FMT_STRING, -1},
{ 0x13, "GPSDestLatitudeRef", FMT_STRING, 2},
{ 0x14, "GPSDestLatitude", FMT_SRATIONAL, 3},
{ 0x15, "GPSDestLongitudeRef", FMT_STRING, 2},
{ 0x16, "GPSDestLongitude", FMT_SRATIONAL, 3},
{ 0x17, "GPSDestBearingRef", FMT_STRING, 1},
{ 0x18, "GPSDestBearing", FMT_SRATIONAL, 1},
{ 0x19, "GPSDestDistanceRef", FMT_STRING, 2},
{ 0x1A, "GPSDestDistance", FMT_SRATIONAL, 1},
{ 0x1B, "GPSProcessingMethod", FMT_UNDEFINED, -1},
{ 0x1C, "GPSAreaInformation", FMT_STRING, -1},
{ 0x1D, "GPSDateStamp", FMT_STRING, 11},
{ 0x1E, "GPSDifferential", FMT_SSHORT, 1},
};
#define MAX_GPS_TAG (sizeof(GpsTags) / sizeof(TagTable_t))
#define EXIF_ASCII_PREFIX_LEN (sizeof(ExifAsciiPrefix))
// Define the line below to turn on poor man's debugging output
#undef SUPERDEBUG
#ifdef SUPERDEBUG
#define printf printf
#endif
int IsGpsTag(const char* tag) {
return strstr(tag, "GPS") == tag;
}
TagTable_t* GpsTagToTagTableEntry(unsigned short tag)
{
unsigned int i;
for (i = 0; i < MAX_GPS_TAG; i++) {
if (GpsTags[i].Tag == tag) {
printf("found tag %d", tag);
int format = GpsTags[i].Format;
if (format == 0) {
printf("tag %s format not defined", GpsTags[i].Desc);
return NULL;
}
return &GpsTags[i];
}
}
printf("tag %d NOT FOUND", tag);
return NULL;
}
int GpsTagToFormatType(unsigned short tag)
{
unsigned int i;
for (i = 0; i < MAX_GPS_TAG; i++) {
if (GpsTags[i].Tag == tag) {
printf("found tag %d", tag);
int format = GpsTags[i].Format;
if (format == 0) {
printf("tag %s format not defined", GpsTags[i].Desc);
return -1;
}
return format;
}
}
printf("tag %d NOT FOUND", tag);
return -1;
}
int GpsTagNameToValue(const char* tagName)
{
unsigned int i;
for (i = 0; i < MAX_GPS_TAG; i++) {
if (strcmp(GpsTags[i].Desc, tagName) == 0) {
printf("found GPS tag %s val %d", GpsTags[i].Desc, GpsTags[i].Tag);
return GpsTags[i].Tag;
}
}
printf("GPS tag %s NOT FOUND", tagName);
return -1;
}
//--------------------------------------------------------------------------
// Process GPS info directory
//--------------------------------------------------------------------------
void ProcessGpsInfo(unsigned char * DirStart, int ByteCountUnused, unsigned char * OffsetBase, unsigned ExifLength)
{
int de;
unsigned a;
int NumDirEntries;
NumDirEntries = Get16u(DirStart);
#define DIR_ENTRY_ADDR(Start, Entry) (Start+2+12*(Entry))
if (ShowTags){
printf("(dir has %d entries)\n",NumDirEntries);
}
ImageInfo.GpsInfoPresent = TRUE;
strcpy(ImageInfo.GpsLat, "? ?");
strcpy(ImageInfo.GpsLong, "? ?");
ImageInfo.GpsAlt[0] = 0;
for (de=0;de<NumDirEntries;de++){
unsigned Tag, Format, Components;
unsigned char * ValuePtr;
int ComponentSize;
unsigned ByteCount;
unsigned char * DirEntry;
DirEntry = DIR_ENTRY_ADDR(DirStart, de);
if (DirEntry+12 > OffsetBase+ExifLength){
ErrNonfatal("GPS info directory goes past end of exif",0,0);
return;
}
Tag = Get16u(DirEntry);
Format = Get16u(DirEntry+2);
Components = Get32u(DirEntry+4);
if ((Format-1) >= NUM_FORMATS) {
// (-1) catches illegal zero case as unsigned underflows to positive large.
ErrNonfatal("Illegal number format %d for tag %04x", Format, Tag);
continue;
}
ComponentSize = BytesPerFormat[Format];
ByteCount = Components * ComponentSize;
#ifdef SUPERDEBUG
printf("GPS tag %x format %s #components %d componentsize %d bytecount %d", Tag, formatStr(Format), Components, ComponentSize,
ByteCount);
#endif
if (ByteCount > 4){
unsigned OffsetVal;
OffsetVal = Get32u(DirEntry+8);
// If its bigger than 4 bytes, the dir entry contains an offset.
if (OffsetVal+ByteCount > ExifLength){
// Bogus pointer offset and / or bytecount value
ErrNonfatal("Illegal value pointer for tag %04x", Tag,0);
continue;
}
ValuePtr = OffsetBase+OffsetVal;
}else{
// 4 bytes or less and value is in the dir entry itself
ValuePtr = DirEntry+8;
}
switch(Tag){
char FmtString[21];
char TempString[MAX_BUF_SIZE];
double Values[3];
case TAG_GPS_LAT_REF:
ImageInfo.GpsLat[0] = ValuePtr[0];
ImageInfo.GpsLatRef[0] = ValuePtr[0];
ImageInfo.GpsLatRef[1] = '\0';
break;
case TAG_GPS_LONG_REF:
ImageInfo.GpsLong[0] = ValuePtr[0];
ImageInfo.GpsLongRef[0] = ValuePtr[0];
ImageInfo.GpsLongRef[1] = '\0';
break;
case TAG_GPS_LAT:
case TAG_GPS_LONG:
if (Format != FMT_URATIONAL){
ErrNonfatal("Inappropriate format (%d) for GPS coordinates!", Format, 0);
}
strcpy(FmtString, "%0.0fd %0.0fm %0.0fs");
for (a=0;a<3;a++){
int den, digits;
den = Get32s(ValuePtr+4+a*ComponentSize);
digits = 0;
while (den > 1 && digits <= 6){
den = den / 10;
digits += 1;
}
if (digits > 6) digits = 6;
FmtString[1+a*7] = (char)('2'+digits+(digits ? 1 : 0));
FmtString[3+a*7] = (char)('0'+digits);
Values[a] = ConvertAnyFormat(ValuePtr+a*ComponentSize, Format);
}
sprintf(TempString, FmtString, Values[0], Values[1], Values[2]);
if (Tag == TAG_GPS_LAT){
strncpy(ImageInfo.GpsLat+2, TempString, 29);
}else{
strncpy(ImageInfo.GpsLong+2, TempString, 29);
}
sprintf(TempString, "%d/%d,%d/%d,%d/%d",
Get32s(ValuePtr), Get32s(4+(char*)ValuePtr),
Get32s(8+(char*)ValuePtr), Get32s(12+(char*)ValuePtr),
Get32s(16+(char*)ValuePtr), Get32s(20+(char*)ValuePtr));
if (Tag == TAG_GPS_LAT){
strncpy(ImageInfo.GpsLatRaw, TempString, MAX_BUF_SIZE);
}else{
strncpy(ImageInfo.GpsLongRaw, TempString, MAX_BUF_SIZE);
}
break;
case TAG_GPS_ALT_REF:
ImageInfo.GpsAlt[0] = (char)(ValuePtr[0] ? '-' : ' ');
ImageInfo.GpsAltRef = (char)ValuePtr[0];
break;
case TAG_GPS_ALT:
sprintf(ImageInfo.GpsAlt + 1, "%.2fm",
ConvertAnyFormat(ValuePtr, Format));
ImageInfo.GpsAltRaw.num = Get32u(ValuePtr);
ImageInfo.GpsAltRaw.denom = Get32u(4+(char *)ValuePtr);
break;
case TAG_GPS_TIMESTAMP:
snprintf(ImageInfo.GpsTimeStamp,
sizeof(ImageInfo.GpsTimeStamp), "%d:%d:%d",
(int) ConvertAnyFormat(ValuePtr, Format),
(int) ConvertAnyFormat(ValuePtr + 8, Format),
(int) ConvertAnyFormat(ValuePtr + 16, Format)
);
break;
case TAG_GPS_DATESTAMP:
strncpy(ImageInfo.GpsDateStamp, (char*)ValuePtr, sizeof(ImageInfo.GpsDateStamp));
break;
case TAG_GPS_PROCESSING_METHOD:
if (ByteCount > EXIF_ASCII_PREFIX_LEN &&
memcmp(ValuePtr, ExifAsciiPrefix, EXIF_ASCII_PREFIX_LEN) == 0) {
int length =
ByteCount < GPS_PROCESSING_METHOD_LEN + EXIF_ASCII_PREFIX_LEN ?
ByteCount - EXIF_ASCII_PREFIX_LEN : GPS_PROCESSING_METHOD_LEN;
memcpy(ImageInfo.GpsProcessingMethod,
(char*)(ValuePtr + EXIF_ASCII_PREFIX_LEN), length);
ImageInfo.GpsProcessingMethod[length] = 0;
} else {
printf("Unsupported encoding for GPSProcessingMethod");
}
break;
}
if (ShowTags){
// Show tag value.
if (Tag < MAX_GPS_TAG){
printf(" %s =", GpsTags[Tag].Desc);
}else{
// Show unknown tag
printf(" Illegal GPS tag %04x=", Tag);
}
switch(Format){
case FMT_UNDEFINED:
// Undefined is typically an ascii string.
case FMT_STRING:
// String arrays printed without function call (different from int arrays)
{
printf("\"");
for (a=0;a<ByteCount;a++){
int ZeroSkipped = 0;
if (ValuePtr[a] >= 32){
if (ZeroSkipped){
printf("?");
ZeroSkipped = 0;
}
putchar(ValuePtr[a]);
}else{
if (ValuePtr[a] == 0){
ZeroSkipped = 1;
}
}
}
printf("\"\n");
}
break;
default:
// Handle arrays of numbers later (will there ever be?)
for (a=0;;){
PrintFormatNumber(ValuePtr+a*ComponentSize, Format, ByteCount);
if (++a >= Components) break;
printf(", ");
}
printf("\n");
}
}
}
}