blob: afd4b127aad8c85acf9854775530337bb8ad14a8 [file] [log] [blame]
The Android Open Source Project34a25642009-03-03 19:30:03 -08001//--------------------------------------------------------------------------
2// Parsing of GPS info from exif header.
3//
4// Matthias Wandel, Dec 1999 - Dec 2002
5//--------------------------------------------------------------------------
6#include "jhead.h"
7
8#include <string.h>
9#include <utils/Log.h>
10
11
12#define TAG_GPS_LAT_REF 1
13#define TAG_GPS_LAT 2
14#define TAG_GPS_LONG_REF 3
15#define TAG_GPS_LONG 4
16#define TAG_GPS_ALT_REF 5
17#define TAG_GPS_ALT 6
Ray Chena39920c2010-01-15 13:58:10 -080018#define TAG_GPS_TIMESTAMP 7
19#define TAG_GPS_DATESTAMP 29
The Android Open Source Project34a25642009-03-03 19:30:03 -080020
21static TagTable_t GpsTags[]= {
22 { 0x00, "GPSVersionID", FMT_BYTE, 4},
23 { 0x01, "GPSLatitudeRef", FMT_STRING, 2},
24 { 0x02, "GPSLatitude", FMT_URATIONAL, 3},
25 { 0x03, "GPSLongitudeRef", FMT_STRING, 2},
26 { 0x04, "GPSLongitude", FMT_URATIONAL, 3},
27 { 0x05, "GPSAltitudeRef", FMT_BYTE, 1},
28 { 0x06, "GPSAltitude", FMT_SRATIONAL, 1},
29 { 0x07, "GPSTimeStamp", FMT_SRATIONAL, 3},
30 { 0x08, "GPSSatellites", FMT_STRING, -1},
31 { 0x09, "GPSStatus", FMT_STRING, 2},
32 { 0x0A, "GPSMeasureMode", FMT_STRING, 2},
33 { 0x0B, "GPSDOP", FMT_SRATIONAL, 1},
34 { 0x0C, "GPSSpeedRef", FMT_STRING, 2},
35 { 0x0D, "GPSSpeed", FMT_SRATIONAL, 1},
36 { 0x0E, "GPSTrackRef", FMT_STRING, 2},
37 { 0x0F, "GPSTrack", FMT_SRATIONAL, 1},
38 { 0x10, "GPSImgDirectionRef", FMT_STRING, -1},
39 { 0x11, "GPSImgDirection", FMT_SRATIONAL, 1},
40 { 0x12, "GPSMapDatum", FMT_STRING, -1},
41 { 0x13, "GPSDestLatitudeRef", FMT_STRING, 2},
42 { 0x14, "GPSDestLatitude", FMT_SRATIONAL, 3},
43 { 0x15, "GPSDestLongitudeRef", FMT_STRING, 2},
44 { 0x16, "GPSDestLongitude", FMT_SRATIONAL, 3},
45 { 0x17, "GPSDestBearingRef", FMT_STRING, 1},
46 { 0x18, "GPSDestBearing", FMT_SRATIONAL, 1},
47 { 0x19, "GPSDestDistanceRef", FMT_STRING, 2},
48 { 0x1A, "GPSDestDistance", FMT_SRATIONAL, 1},
49 { 0x1B, "GPSProcessingMethod", FMT_STRING, -1},
50 { 0x1C, "GPSAreaInformation", FMT_STRING, -1},
51 { 0x1D, "GPSDateStamp", FMT_STRING, 11},
52 { 0x1E, "GPSDifferential", FMT_SSHORT, 1},
53};
54
55#define MAX_GPS_TAG (sizeof(GpsTags) / sizeof(TagTable_t))
56
57// Define the line below to turn on poor man's debugging output
58#undef SUPERDEBUG
59
60#ifdef SUPERDEBUG
61#define printf LOGE
62#endif
63
64
65int IsGpsTag(const char* tag) {
66 return strstr(tag, "GPS") == tag;
67}
68
69TagTable_t* GpsTagToTagTableEntry(unsigned short tag)
70{
71 unsigned int i;
72 for (i = 0; i < MAX_GPS_TAG; i++) {
73 if (GpsTags[i].Tag == tag) {
74 printf("found tag %d", tag);
75 int format = GpsTags[i].Format;
76 if (format == 0) {
77 printf("tag %s format not defined", GpsTags[i].Desc);
78 return NULL;
79 }
80 return &GpsTags[i];
81 }
82 }
83 printf("tag %d NOT FOUND", tag);
84 return NULL;
85}
86
87int GpsTagToFormatType(unsigned short tag)
88{
89 unsigned int i;
90 for (i = 0; i < MAX_GPS_TAG; i++) {
91 if (GpsTags[i].Tag == tag) {
92 printf("found tag %d", tag);
93 int format = GpsTags[i].Format;
94 if (format == 0) {
95 printf("tag %s format not defined", GpsTags[i].Desc);
96 return -1;
97 }
98 return format;
99 }
100 }
101 printf("tag %d NOT FOUND", tag);
102 return -1;
103}
104
105int GpsTagNameToValue(const char* tagName)
106{
107 unsigned int i;
108 for (i = 0; i < MAX_GPS_TAG; i++) {
109 if (strcmp(GpsTags[i].Desc, tagName) == 0) {
110 printf("found GPS tag %s val %d", GpsTags[i].Desc, GpsTags[i].Tag);
111 return GpsTags[i].Tag;
112 }
113 }
114 printf("GPS tag %s NOT FOUND", tagName);
115 return -1;
116}
117
118
119//--------------------------------------------------------------------------
120// Process GPS info directory
121//--------------------------------------------------------------------------
122void ProcessGpsInfo(unsigned char * DirStart, int ByteCountUnused, unsigned char * OffsetBase, unsigned ExifLength)
123{
124 int de;
125 unsigned a;
126 int NumDirEntries;
127
128 NumDirEntries = Get16u(DirStart);
129 #define DIR_ENTRY_ADDR(Start, Entry) (Start+2+12*(Entry))
130
131 if (ShowTags){
132 printf("(dir has %d entries)\n",NumDirEntries);
133 }
134
135 ImageInfo.GpsInfoPresent = TRUE;
136 strcpy(ImageInfo.GpsLat, "? ?");
137 strcpy(ImageInfo.GpsLong, "? ?");
138 ImageInfo.GpsAlt[0] = 0;
Ray Chena39920c2010-01-15 13:58:10 -0800139 bzero(ImageInfo.GpsTimeStamp, sizeof(ImageInfo.GpsTimeStamp));
140 bzero(ImageInfo.GpsDateStamp, sizeof(ImageInfo.GpsDateStamp));
The Android Open Source Project34a25642009-03-03 19:30:03 -0800141
142 for (de=0;de<NumDirEntries;de++){
143 unsigned Tag, Format, Components;
144 unsigned char * ValuePtr;
145 int ComponentSize;
146 unsigned ByteCount;
147 unsigned char * DirEntry;
148 DirEntry = DIR_ENTRY_ADDR(DirStart, de);
149
150 if (DirEntry+12 > OffsetBase+ExifLength){
151 ErrNonfatal("GPS info directory goes past end of exif",0,0);
152 return;
153 }
154
155 Tag = Get16u(DirEntry);
156 Format = Get16u(DirEntry+2);
157 Components = Get32u(DirEntry+4);
158
159 if ((Format-1) >= NUM_FORMATS) {
160 // (-1) catches illegal zero case as unsigned underflows to positive large.
161 ErrNonfatal("Illegal number format %d for tag %04x", Format, Tag);
162 continue;
163 }
164
165 ComponentSize = BytesPerFormat[Format];
166 ByteCount = Components * ComponentSize;
167
168#ifdef SUPERDEBUG
169 printf("GPS tag %x format %s #components %d componentsize %d bytecount %d", Tag, formatStr(Format), Components, ComponentSize,
170 ByteCount);
171#endif
172
173 if (ByteCount > 4){
174 unsigned OffsetVal;
175 OffsetVal = Get32u(DirEntry+8);
176 // If its bigger than 4 bytes, the dir entry contains an offset.
177 if (OffsetVal+ByteCount > ExifLength){
178 // Bogus pointer offset and / or bytecount value
179 ErrNonfatal("Illegal value pointer for tag %04x", Tag,0);
180 continue;
181 }
182 ValuePtr = OffsetBase+OffsetVal;
183 }else{
184 // 4 bytes or less and value is in the dir entry itself
185 ValuePtr = DirEntry+8;
186 }
187
188 switch(Tag){
189 char FmtString[21];
190 char TempString[50];
191 double Values[3];
192
193 case TAG_GPS_LAT_REF:
194 ImageInfo.GpsLat[0] = ValuePtr[0];
195 ImageInfo.GpsLatRef[0] = ValuePtr[0];
196 ImageInfo.GpsLatRef[1] = '\0';
197 break;
198
199 case TAG_GPS_LONG_REF:
200 ImageInfo.GpsLong[0] = ValuePtr[0];
201 ImageInfo.GpsLongRef[0] = ValuePtr[0];
202 ImageInfo.GpsLongRef[1] = '\0';
203 break;
204
205 case TAG_GPS_LAT:
206 case TAG_GPS_LONG:
207 if (Format != FMT_URATIONAL){
208 ErrNonfatal("Inappropriate format (%d) for GPS coordinates!", Format, 0);
209 }
210 strcpy(FmtString, "%0.0fd %0.0fm %0.0fs");
The Android Open Source Project34a25642009-03-03 19:30:03 -0800211 for (a=0;a<3;a++){
212 int den, digits;
213
214 den = Get32s(ValuePtr+4+a*ComponentSize);
215 digits = 0;
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700216 while (den > 1 && digits <= 6){
The Android Open Source Project34a25642009-03-03 19:30:03 -0800217 den = den / 10;
218 digits += 1;
219 }
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700220 if (digits > 6) digits = 6;
The Android Open Source Project34a25642009-03-03 19:30:03 -0800221 FmtString[1+a*7] = (char)('2'+digits+(digits ? 1 : 0));
222 FmtString[3+a*7] = (char)('0'+digits);
223
224 Values[a] = ConvertAnyFormat(ValuePtr+a*ComponentSize, Format);
225 }
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700226
The Android Open Source Project34a25642009-03-03 19:30:03 -0800227 sprintf(TempString, FmtString, Values[0], Values[1], Values[2]);
228
229 if (Tag == TAG_GPS_LAT){
230 strncpy(ImageInfo.GpsLat+2, TempString, 29);
231 }else{
232 strncpy(ImageInfo.GpsLong+2, TempString, 29);
233 }
234
235 sprintf(TempString, "%d/%d,%d/%d,%d/%d",
236 Get32s(ValuePtr), Get32s(4+(char*)ValuePtr),
237 Get32s(8+(char*)ValuePtr), Get32s(12+(char*)ValuePtr),
238 Get32s(16+(char*)ValuePtr), Get32s(20+(char*)ValuePtr));
239 if (Tag == TAG_GPS_LAT){
240 strncpy(ImageInfo.GpsLatRaw, TempString, 31);
241 }else{
242 strncpy(ImageInfo.GpsLongRaw, TempString, 31);
243 }
244 break;
245
246 case TAG_GPS_ALT_REF:
247 ImageInfo.GpsAlt[0] = (char)(ValuePtr[0] ? '-' : ' ');
248 break;
249
250 case TAG_GPS_ALT:
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700251 sprintf(ImageInfo.GpsAlt + 1, "%.2fm",
252 ConvertAnyFormat(ValuePtr, Format));
The Android Open Source Project34a25642009-03-03 19:30:03 -0800253 break;
Ray Chena39920c2010-01-15 13:58:10 -0800254
255 case TAG_GPS_TIMESTAMP:
256 snprintf(ImageInfo.GpsTimeStamp,
257 sizeof(ImageInfo.GpsTimeStamp), "%d:%d:%d",
258 (int) ConvertAnyFormat(ValuePtr, Format),
259 (int) ConvertAnyFormat(ValuePtr + 8, Format),
260 (int) ConvertAnyFormat(ValuePtr + 16, Format)
261 );
262 break;
263
264 case TAG_GPS_DATESTAMP:
265 strncpy(ImageInfo.GpsDateStamp, (char*)ValuePtr, sizeof(ImageInfo.GpsDateStamp));
266 break;
The Android Open Source Project34a25642009-03-03 19:30:03 -0800267 }
268
269 if (ShowTags){
270 // Show tag value.
271 if (Tag < MAX_GPS_TAG){
272 printf(" %s =", GpsTags[Tag].Desc);
273 }else{
274 // Show unknown tag
275 printf(" Illegal GPS tag %04x=", Tag);
276 }
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700277
The Android Open Source Project34a25642009-03-03 19:30:03 -0800278 switch(Format){
279 case FMT_UNDEFINED:
280 // Undefined is typically an ascii string.
281
282 case FMT_STRING:
283 // String arrays printed without function call (different from int arrays)
284 {
285 printf("\"");
286 for (a=0;a<ByteCount;a++){
287 int ZeroSkipped = 0;
288 if (ValuePtr[a] >= 32){
289 if (ZeroSkipped){
290 printf("?");
291 ZeroSkipped = 0;
292 }
293 putchar(ValuePtr[a]);
294 }else{
295 if (ValuePtr[a] == 0){
296 ZeroSkipped = 1;
297 }
298 }
299 }
300 printf("\"\n");
301 }
302 break;
303
304 default:
305 // Handle arrays of numbers later (will there ever be?)
306 for (a=0;;){
307 PrintFormatNumber(ValuePtr+a*ComponentSize, Format, ByteCount);
308 if (++a >= Components) break;
309 printf(", ");
310 }
311 printf("\n");
312 }
313 }
314 }
315}
316
317