forked from GPSBabel/gpsbabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exif.cc
1565 lines (1335 loc) · 37.3 KB
/
exif.cc
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Support for embedded (JPEG) Exif-GPS information.
Copyright (C) 2008 Olaf Klein, [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
*/
/*
* Exif specifications can be found at
* 2012, version 2.3: http://www.cipa.jp/english/hyoujunka/kikaku/pdf/DC-008-2012_E.pdf
* 2010, version 2.3: http://www.cipa.jp/english/hyoujunka/kikaku/pdf/DC-008-2010_E.pdf
* 2002, version 2.2: http://www.exif.org/Exif2-2.PDF
* 1998, version 2.1: http://www.exif.org/Exif2-1.PDF
*/
#include "defs.h"
#include "garmin_tables.h"
#include "jeeps/gpsmath.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MYNAME "exif"
// #define EXIF_DBG
#define UNKNOWN_TIMESTAMP 999999999
#define IFD0 0
#define IFD1 1
#define EXIF_IFD 2 /* dummy index */
#define GPS_IFD 3 /* dummy index */
#define INTER_IFD 4 /* dummy index */
#define EXIF_TYPE_BYTE 1
#define EXIF_TYPE_ASCII 2
#define EXIF_TYPE_SHORT 3
#define EXIF_TYPE_LONG 4
#define EXIF_TYPE_RAT 5
/* TIFF 6.0 */
#define EXIF_TYPE_SBYTE 6
#define EXIF_TYPE_UNK 7
#define EXIF_TYPE_SSHORT 8
#define EXIF_TYPE_SLONG 9
#define EXIF_TYPE_SRAT 10
#define EXIF_TYPE_FLOAT 11
#define EXIF_TYPE_DOUBLE 12
#define EXIF_TYPE_IFD 13
#define EXIF_TYPE_UNICODE 14
#define EXIF_TYPE_COMPLEX 15
#define EXIF_TYPE_LONG8 16
#define EXIF_TYPE_SLONG8 17
#define EXIF_TYPE_IFD8 18
#define BYTE_TYPE(a) ( (a==EXIF_TYPE_BYTE) || (a==EXIF_TYPE_ASCII) || (a==EXIF_TYPE_UNK) )
#define WORD_TYPE(a) ( (a==EXIF_TYPE_SHORT) || (a==EXIF_TYPE_SSHORT) )
#define LONG_TYPE(a) ( (a==EXIF_TYPE_LONG) || (a==EXIF_TYPE_SLONG) || (a==EXIF_TYPE_IFD) )
#define IFD0_TAG_EXIF_IFD_OFFS 0x8769
#define IFD0_TAG_GPS_IFD_OFFS 0x8825
#define IFD1_TAG_STRIP_OFFS 0x0111
#define IFD1_TAG_JPEG_OFFS 0x0201
#define IFD1_TAG_JPEG_SIZE 0x0202
#define EXIF_IFD_TAG_USER_CMT 0x9286
#define EXIF_IFD_TAG_INTER_IFD_OFFS 0xA005
#define GPS_IFD_TAG_VERSION 0x0000
#define GPS_IFD_TAG_LATREF 0x0001
#define GPS_IFD_TAG_LAT 0x0002
#define GPS_IFD_TAG_LONREF 0x0003
#define GPS_IFD_TAG_LON 0x0004
#define GPS_IFD_TAG_ALTREF 0x0005
#define GPS_IFD_TAG_ALT 0x0006
#define GPS_IFD_TAG_TIMESTAMP 0x0007
#define GPS_IFD_TAG_SAT 0x0008
#define GPS_IFD_TAG_MODE 0x000A
#define GPS_IFD_TAG_DOP 0x000B
#define GPS_IFD_TAG_SPEEDREF 0x000C
#define GPS_IFD_TAG_SPEED 0x000D
#define GPS_IFD_TAG_DATUM 0x0012
#define GPS_IFD_TAG_DATESTAMP 0x001D
typedef struct exif_tag_s {
queue Q;
uint16_t id;
uint16_t type;
uint32_t count;
uint32_t value;
uint32_t origin;
uint32_t size;
#ifdef EXIF_DBG
uint32_t offs;
#endif
unsigned char data_is_dynamic:1;
void* data;
} exif_tag_t;
typedef struct exif_ifd_s {
queue Q;
uint32_t next_ifd;
uint16_t nr;
uint16_t count;
queue tags;
} exif_ifd_t, *exif_ifd_p;
typedef struct exif_app_s {
queue Q;
uint16_t marker;
gbsize_t len;
gbfile* fcache;
gbfile* fexif;
queue ifds;
} exif_app_t;
static gbfile* fin, *fout;
static queue exif_apps;
static exif_app_t* exif_app;
const Waypoint* exif_wpt_ref;
time_t exif_time_ref;
static char exif_success;
static char* exif_fout_name;
static char* opt_filename, *opt_overwrite, *opt_frame, *opt_name;
static uint8_t writer_gps_tag_version[4] = {2, 0, 0, 0};
arglist_t exif_args[] = {
{ "filename", &opt_filename, "Set waypoint name to source filename", "Y", ARGTYPE_BOOL, ARG_NOMINMAX },
{ "frame", &opt_frame, "Time-frame (in seconds)", "10", ARGTYPE_INT, "0", NULL },
{ "name", &opt_name, "Locate waypoint for tagging by this name", NULL, ARGTYPE_STRING, ARG_NOMINMAX },
{ "overwrite", &opt_overwrite, "!OVERWRITE! the original file. Default=N", "N", ARGTYPE_BOOL, ARG_NOMINMAX },
ARG_TERMINATOR
};
#ifdef EXIF_DBG
static void
print_buff(const char* buf, int sz, const char* cmt)
{
int i;
printf("%s: ", cmt);
for (i = 0; i < sz; i++) {
printf("%02x ", buf[i] & 0xFF);
}
for (i = 0; i < sz; i++) {
char c = buf[i];
if (isspace(c)) {
c = ' ';
} else if (! isprint(c)) {
c = '.';
}
printf("%c", c);
}
}
#endif
static uint16_t
exif_type_size(const uint16_t type)
{
uint16_t size;
switch (type) {
case EXIF_TYPE_BYTE:
case EXIF_TYPE_ASCII:
case EXIF_TYPE_UNK:
size = 1;
break;
case EXIF_TYPE_SHORT:
case EXIF_TYPE_SSHORT:
case EXIF_TYPE_UNICODE:
size = 2;
break;
case EXIF_TYPE_IFD:
case EXIF_TYPE_LONG:
case EXIF_TYPE_SLONG:
case EXIF_TYPE_FLOAT:
size = 4;
break;
case EXIF_TYPE_RAT:
case EXIF_TYPE_SRAT:
case EXIF_TYPE_DOUBLE:
case EXIF_TYPE_LONG8:
case EXIF_TYPE_SLONG8:
case EXIF_TYPE_IFD8:
size = 8;
break;
default:
fatal(MYNAME ": Unknown data type %d! Please report.\n", type);
}
return size;
}
// TODO: If this were actually ever used (!?!?!) it could probably be
// replaced by return QDateTime(time).toString("yyyy/MM/dd, hh:mm:ss);
static QString
exif_time_str(const time_t time)
{
struct tm tm;
tm = *localtime(&time);
tm.tm_year += 1900;
tm.tm_mon += 1;
return QString().sprintf("%04d/%02d/%02d, %02d:%02d:%02d",
tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
}
static char*
exif_read_str(exif_tag_t* tag)
{
// Panasonic DMC-TZ10 stores datum with trailing spaces.
char* buf = xstrndup((char*)tag->data, tag->size);
rtrim(buf);
return buf;
}
static double
exif_read_double(const exif_tag_t* tag, const int index)
{
unsigned int num, den;
int32_t* data = (int32_t*)tag->data;
num = data[index * 2];
den = data[(index * 2) + 1];
if (den == 0) {
den = 1;
}
return (double)num / (double)den;
}
static double
exif_read_coord(const exif_tag_t* tag)
{
double res, min, sec;
res = exif_read_double(tag, 0);
if (tag->count == 1) {
return res;
}
min = exif_read_double(tag, 1);
res += (min / 60);
if (tag->count == 2) {
return res;
}
sec = exif_read_double(tag, 2);
res += (sec / 3600);
return res;
}
static time_t
exif_read_timestamp(const exif_tag_t* tag)
{
double hour, min, sec;
hour = exif_read_double(tag, 0);
min = exif_read_double(tag, 1);
sec = exif_read_double(tag, 2);
return ((int)hour * SECONDS_PER_HOUR) + ((int)min * 60) + (int)sec;
}
static time_t
exif_read_datestamp(const exif_tag_t* tag)
{
struct tm tm;
char* str;
memset(&tm, 0, sizeof(tm));
str = xstrndup((char*)tag->data, tag->size);
sscanf(str, "%d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
xfree(str);
tm.tm_year -= 1900;
tm.tm_mon -= 1;
return mkgmtime(&tm);
}
static void
exif_release_tag(exif_tag_t* tag)
{
dequeue(&tag->Q);
if (tag->data_is_dynamic) {
xfree(tag->data);
}
xfree(tag);
}
static void
exif_release_ifd(exif_ifd_t* ifd)
{
if (ifd != NULL) {
queue* elem, *tmp;
QUEUE_FOR_EACH(&ifd->tags, elem, tmp) {
exif_release_tag((exif_tag_t*)elem);
}
xfree(ifd);
}
}
static void
exif_release_apps(void)
{
queue* e0, *t0;
QUEUE_FOR_EACH(&exif_apps, e0, t0) {
queue* e1, *t1;
exif_app_t* app = (exif_app_t*)dequeue(e0);
if (app->fcache) {
gbfclose(app->fcache);
}
if (app->fexif) {
gbfclose(app->fexif);
}
QUEUE_FOR_EACH(&app->ifds, e1, t1) {
exif_ifd_t* ifd = (exif_ifd_t*)dequeue(e1);
exif_release_ifd(ifd);
}
xfree(app);
}
}
static uint32_t
exif_ifd_size(exif_ifd_t* ifd)
{
queue* elem, *tmp;
uint32_t res = 6; /* nr of tags + next_ifd */
res += (ifd->count * 12);
QUEUE_FOR_EACH(&ifd->tags, elem, tmp) {
exif_tag_t* tag = (exif_tag_t*)elem;
if (tag->size > 4) {
uint32_t size = tag->size;
if (size & 1) {
size++;
}
res += size;
}
}
return res;
}
static exif_app_t*
exif_load_apps(void)
{
exif_app_t* exif_app = NULL;
while (! gbfeof(fin)) {
exif_app_t* app = (exif_app_t*) xcalloc(sizeof(*app), 1);
ENQUEUE_TAIL(&exif_apps, &app->Q);
QUEUE_INIT(&app->ifds);
app->fcache = gbfopen(NULL, "wb", MYNAME);
app->marker = gbfgetuint16(fin);
app->len = gbfgetuint16(fin);
#ifdef EXIF_DBG
printf(MYNAME ": api = %02X, len = %d, offs = %04X\n", app->marker & 0xFF, app->len, gbftell(fin));
#endif
if (exif_app || (app->marker == 0xFFDA)) /* compressed data */ {
gbfcopyfrom(app->fcache, fin, 0x7FFFFFFF);
#ifdef EXIF_DBG
printf(MYNAME ": compressed data size = %d\n", gbftell(app->fcache));
#endif
} else {
gbfcopyfrom(app->fcache, fin, app->len - 2);
if (app->marker == 0xFFE1) {
exif_app = app;
}
}
}
return exif_app;
}
static exif_ifd_t*
exif_read_ifd(exif_app_t* app, const uint16_t ifd_nr, gbsize_t offs,
uint32_t* exif_ifd_ofs, uint32_t* gps_ifd_ofs, uint32_t* inter_ifd_ofs)
{
queue* elem, *tmp;
uint16_t i;
exif_ifd_t* ifd;
gbfile* fin = app->fexif;
ifd = (exif_ifd_t*) xcalloc(sizeof(*ifd), 1);
QUEUE_INIT(&ifd->tags);
ENQUEUE_TAIL(&app->ifds, &ifd->Q);
ifd->nr = ifd_nr;
gbfseek(fin, offs, SEEK_SET);
ifd->count = gbfgetuint16(fin);
#ifdef EXIF_DBG
{
char* name;
switch (ifd_nr) {
case IFD0:
name = "IFD0";
break;
case IFD1:
name = "IFD1";
break;
case GPS_IFD:
name = "GPS";
break;
case EXIF_IFD:
name = "EXIF";
break;
case INTER_IFD:
name = "INTER";
break;
default:
name = "private";
break;
}
printf(MYNAME "-offs 0x%04X: Number of items in IFD%d \"%s\" = %d (0x%2x)\n",
offs, ifd_nr, name, ifd->count, ifd->count);
}
#endif
if (ifd->count == 0) {
return ifd;
}
for (i = 0; i < ifd->count; i++) {
exif_tag_t* tag;
tag = (exif_tag_t*) xcalloc(sizeof(*tag), 1);
#ifdef EXIF_DBG
tag->offs = offs;
offs = gbftell(fin);
#endif
ENQUEUE_TAIL(&ifd->tags, &tag->Q);
tag->id = gbfgetuint16(fin);
tag->type = gbfgetuint16(fin);
tag->count = gbfgetuint32(fin);
tag->size = exif_type_size(tag->type) * tag->count;
tag->data = &tag->value;
if (BYTE_TYPE(tag->type) && (tag->count <= 4)) {
gbfread(tag->data, 4, 1, fin);
} else {
tag->value = gbfgetuint32(fin);
tag->origin = tag->value;
}
if (ifd_nr == IFD0) {
if (tag->id == IFD0_TAG_EXIF_IFD_OFFS) {
*exif_ifd_ofs = tag->value;
} else if (tag->id == IFD0_TAG_GPS_IFD_OFFS) {
*gps_ifd_ofs = tag->value;
}
} else if (ifd_nr == EXIF_IFD) {
if (tag->id == EXIF_IFD_TAG_INTER_IFD_OFFS) {
*inter_ifd_ofs = tag->value;
}
}
}
ifd->next_ifd = gbfgetuint16(fin);
QUEUE_FOR_EACH(&ifd->tags, elem, tmp) {
exif_tag_t* tag = (exif_tag_t*)elem;
if ((tag->size > 4) && (tag->value)) {
uint16_t i;
char* ptr;
tag->data = xmalloc(tag->size);
tag->data_is_dynamic = 1;
ptr = (char*) tag->data;
gbfseek(fin, tag->value, SEEK_SET);
if (BYTE_TYPE(tag->type)) {
gbfread(ptr, tag->count, 1, fin);
} else for (i = 0; i < tag->count; i++) {
switch (tag->type) {
case EXIF_TYPE_SHORT:
case EXIF_TYPE_SSHORT:
*(int16_t*)ptr = gbfgetuint16(fin);
break;
case EXIF_TYPE_IFD:
case EXIF_TYPE_LONG:
case EXIF_TYPE_SLONG:
*(int32_t*)ptr = gbfgetuint32(fin);
break;
case EXIF_TYPE_RAT:
case EXIF_TYPE_SRAT:
*(int32_t*)ptr = gbfgetuint32(fin);
*(int32_t*)(ptr+4) = gbfgetuint32(fin);
break;
case EXIF_TYPE_FLOAT:
*(float*)ptr = gbfgetflt(fin);
break;
case EXIF_TYPE_DOUBLE:
*(double*)ptr = gbfgetdbl(fin);
break;
default:
gbfread(ptr, 1, 1, fin);
break;
}
ptr += (tag->size / tag->count);
}
}
#ifdef EXIF_DBG
printf(MYNAME "-offs 0x%04X: ifd=%d id=0x%04X t=0x%04X c=%4d s=%4d v=0x%08X",
tag->offs, ifd->nr, tag->id, tag->type, tag->count, tag->size, tag->value);
if (tag->type == EXIF_TYPE_ASCII) {
printf(" \"%s\"", exif_read_str(tag));
}
printf("\n");
#endif
}
return ifd;
}
static void
exif_read_app(exif_app_t* app)
{
gbsize_t offs;
uint32_t exif_ifd_ofs, gps_ifd_ofs, inter_ifd_ofs;
exif_ifd_t* ifd;
gbfile* fin = app->fexif;
#ifdef EXIF_DBG
printf(MYNAME ": read_app...\n");
print_buff((const char*)fin->handle.mem, 16, MYNAME);
printf("\n");
#endif
exif_ifd_ofs = gps_ifd_ofs = inter_ifd_ofs = 0;
gbfseek(fin, 4, SEEK_SET);
offs = gbfgetuint32(fin);
ifd = exif_read_ifd(app, IFD0, offs, &exif_ifd_ofs, &gps_ifd_ofs, &inter_ifd_ofs);
if (ifd == NULL) {
return;
}
if (ifd->next_ifd) {
ifd = exif_read_ifd(app, IFD1, ifd->next_ifd, &exif_ifd_ofs, &gps_ifd_ofs, &inter_ifd_ofs);
}
if (exif_ifd_ofs) {
ifd = exif_read_ifd(app, EXIF_IFD, exif_ifd_ofs, NULL, NULL, &inter_ifd_ofs);
}
if (gps_ifd_ofs) {
ifd = exif_read_ifd(app, 3, gps_ifd_ofs, NULL, NULL, NULL);
}
if (inter_ifd_ofs) {
ifd = exif_read_ifd(app, 4, inter_ifd_ofs, NULL, NULL, NULL);
}
// The return values of exif_read_ifd above aren't actually used.
// Warning hush.
(void) ifd;
}
static void
exif_examine_app(exif_app_t* app)
{
uint16_t endianess;
uint32_t ident;
gbfile* ftmp = exif_app->fcache;
gbfrewind(ftmp);
ident = gbfgetuint32(ftmp);
is_fatal(ident != 0x66697845, MYNAME ": Invalid EXIF header magic.");
is_fatal(gbfgetint16(ftmp) != 0, MYNAME ": Error in EXIF header.");
endianess = gbfgetint16(ftmp);
#ifdef EXIF_DBG
printf(MYNAME ": endianess = 0x%04X\n", endianess);
#endif
if (endianess == 0x4949) {
ftmp->big_endian = 0;
} else if (endianess == 0x4D4D) {
ftmp->big_endian = 1;
} else {
fatal(MYNAME ": Invalid endianess identifier 0x%04X!\n", endianess);
}
gbfseek(ftmp, 6, SEEK_SET);
app->fexif = gbfopen(NULL, "wb", MYNAME);
app->fexif->big_endian = ftmp->big_endian;
gbfcopyfrom(app->fexif, ftmp, 0x7FFFFFFF);
exif_read_app(exif_app);
}
static exif_ifd_t*
exif_find_ifd(exif_app_t* app, const uint16_t ifd_nr)
{
queue* e0, *t0;
QUEUE_FOR_EACH(&app->ifds, e0, t0) {
exif_ifd_t* ifd = (exif_ifd_t*)e0;
if (ifd->nr == ifd_nr) {
return ifd;
}
}
return NULL;
}
static exif_tag_t*
exif_find_tag(exif_app_t* app, const uint16_t ifd_nr, const uint16_t tag_id)
{
exif_ifd_t* ifd = exif_find_ifd(app, ifd_nr);
if (ifd != NULL) {
queue* elem, *tmp;
QUEUE_FOR_EACH(&ifd->tags, elem, tmp) {
exif_tag_t* tag = (exif_tag_t*)elem;
if (tag->id == tag_id) {
return tag;
}
}
}
return NULL;
}
static time_t
exif_get_exif_time(exif_app_t* app)
{
QDateTime res;
exif_tag_t* tag;
tag = exif_find_tag(app, EXIF_IFD, 0x9003); /* DateTimeOriginal from EXIF */
if (! tag) {
tag = exif_find_tag(app, IFD0, 0x0132); /* DateTime from IFD0 */
}
if (! tag) {
tag = exif_find_tag(app, EXIF_IFD, 0x9004); /* DateTimeDigitized from EXIF */
}
if (tag) {
char* str;
str = exif_read_str(tag);
res = QDateTime::fromString(str, "yyyy:MM:dd hh:mm:ss");
xfree(str);
}
return res.toTime_t();
}
static Waypoint*
exif_waypt_from_exif_app(exif_app_t* app)
{
Waypoint* wpt;
queue* elem, *tmp;
exif_ifd_t* ifd;
exif_tag_t* tag;
char lat_ref = '\0';
char lon_ref = '\0';
char alt_ref = 0;
char speed_ref = 'K';
char* datum = NULL;
char mode = '\0';
double gpsdop = unknown_alt;
double alt = unknown_alt;
time_t timestamp = UNKNOWN_TIMESTAMP;
time_t datestamp = UNKNOWN_TIMESTAMP;
ifd = exif_find_ifd(app, GPS_IFD);
if (ifd == NULL) {
return NULL;
}
wpt = new Waypoint;
wpt->latitude = unknown_alt;
wpt->longitude = unknown_alt;
QUEUE_FOR_EACH(&ifd->tags, elem, tmp) {
tag = (exif_tag_t*)elem;
switch (tag->id) {
case GPS_IFD_TAG_VERSION:
break;
case GPS_IFD_TAG_LATREF:
lat_ref = *(char*)tag->data;
break;
case GPS_IFD_TAG_LAT:
wpt->latitude = exif_read_coord(tag);
break;
case GPS_IFD_TAG_LONREF:
lon_ref = *(char*)tag->data;
break;
case GPS_IFD_TAG_LON:
wpt->longitude = exif_read_coord(tag);
break;
case GPS_IFD_TAG_ALTREF:
alt_ref = *(char*)tag->data;
break;
case GPS_IFD_TAG_ALT:
alt = exif_read_double(tag, 0);
break;
case GPS_IFD_TAG_TIMESTAMP:
timestamp = exif_read_timestamp(tag);
break;
case GPS_IFD_TAG_SAT:
wpt->sat = atoi((char*)tag->data);
break;
case GPS_IFD_TAG_MODE:
mode = *(char*)tag->data;
break;
case GPS_IFD_TAG_DOP:
gpsdop = exif_read_double(tag, 0);
break;
case GPS_IFD_TAG_SPEEDREF:
speed_ref = *(char*)tag->data;
break;
case GPS_IFD_TAG_SPEED:
WAYPT_SET(wpt, speed, exif_read_double(tag, 0));
break;
case GPS_IFD_TAG_DATUM:
datum = exif_read_str(tag);
break;
case GPS_IFD_TAG_DATESTAMP:
datestamp = exif_read_datestamp(tag);
break;
}
}
if ((wpt->latitude == unknown_alt) || (wpt->longitude == unknown_alt)) {
fatal(MYNAME ": Missing GPSLatitude and/or GPSLongitude!\n");
}
if (lat_ref == 'S') {
wpt->latitude *= -1;
} else if (lat_ref != 'N') {
warning(MYNAME ": GPSLatitudeRef not set! Using N(orth).\n");
}
if (lon_ref == 'W') {
wpt->longitude *= -1;
} else if (lon_ref != 'E') {
warning(MYNAME ": GPSLongitudeRef not set! Using E(east).\n");
}
#ifdef EXIF_DBG
printf(MYNAME "-GPSLatitude = %12.7f\n", wpt->latitude);
printf(MYNAME "-GPSLongitude = %12.7f\n", wpt->longitude);
#endif
if (datum) {
int idatum = gt_lookup_datum_index(datum, MYNAME);
if (idatum < 0) {
fatal(MYNAME ": Unknown GPSMapDatum \"%s\"!\n", datum);
}
if (idatum != DATUM_WGS84) {
double alt;
GPS_Math_WGS84_To_Known_Datum_M(wpt->latitude, wpt->longitude, 0.0,
&wpt->latitude, &wpt->longitude, &alt, idatum);
}
xfree(datum);
}
if (alt != unknown_alt) {
double sign;
switch (alt_ref) {
case 0:
sign = 1.0;
break;
case 1:
sign = -1.0;
break;
default:
warning(MYNAME ": Invalid GPSAltitudeRef (%d)! Using default value 0 (= Sea level).\n", alt_ref);
sign = 1.0;
}
wpt->altitude = sign * alt;
#ifdef EXIF_DBG
printf(MYNAME "-GPSAltitude = %12.7f m\n", wpt->altitude);
#endif
}
if WAYPT_HAS(wpt, speed) {
switch (speed_ref) {
case 'K':
wpt->speed = KPH_TO_MPS(wpt->speed);
break;
case 'M':
wpt->speed = MPH_TO_MPS(wpt->speed);
break;
case 'N':
wpt->speed = KNOTS_TO_MPS(wpt->speed);
break;
default:
wpt->speed = 0;
WAYPT_UNSET(wpt, speed);
warning(MYNAME ": Unknown GPSSpeedRef unit %c (0x%02x)!\n", speed_ref, speed_ref);
}
#ifdef EXIF_DBG
if WAYPT_HAS(wpt, speed) {
printf(MYNAME "-GPSSpeed = %12.2f m/s\n", wpt->speed);
}
#endif
}
if (mode == '2') {
wpt->fix = fix_2d;
if (gpsdop != unknown_alt) {
wpt->hdop = gpsdop;
}
} else if (mode == '3') {
wpt->fix = fix_3d;
if (gpsdop != unknown_alt) {
wpt->pdop = gpsdop;
}
}
if (timestamp != UNKNOWN_TIMESTAMP) {
if (datestamp != UNKNOWN_TIMESTAMP) {
timestamp += datestamp;
}
} else {
timestamp = datestamp;
}
if (timestamp != UNKNOWN_TIMESTAMP) {
#ifdef EXIF_DBG
char* str = exif_time_str(timestamp);
printf(MYNAME "-GPSTimeStamp = %s\n", str);
xfree(str);
#endif
wpt->SetCreationTime(timestamp);
} else {
wpt->SetCreationTime(exif_get_exif_time(app));
}
tag = exif_find_tag(app, EXIF_IFD, EXIF_IFD_TAG_USER_CMT); /* UserComment */
if (tag && (tag->size > 8)) {
QString str;
if (memcmp(tag->data, "ASCII\0\0\0", 8) == 0) {
wpt->notes = QString::fromLatin1((char*) tag->data + 8, tag->size - 8);
} else if (memcmp(tag->data, "UNICODE\0", 8) == 0) {
// I'm not at all sure that casting alignment away like this is a good
// idea in light of arches that don't allow unaligned loads, but in the
// absence of test data that captures it and the grubbiness of the code
// that was here before, I'm going to do this and then come back to it
// if it's a problem.
wpt->notes = QString::fromUtf16((const uint16_t*)((char*) tag->data + 8), tag->size - 8);
}
}
if (opt_filename) {
char* c, *cx;
char* str = xstrdup(fin->name);
cx = str;
if ((c = strrchr(cx, ':'))) {
cx = c + 1;
}
if ((c = strrchr(cx, '\\'))) {
cx = c + 1;
}
if ((c = strrchr(cx, '/'))) {
cx = c + 1;
}
if (((c = strchr(cx, '.'))) && (c != cx)) {
*c = '\0';
}
wpt->shortname = cx;
xfree(str);
}
return wpt;
}
static void
exif_dec2frac(double val, int32_t* num, int32_t* den)
{
char sval[16], snum[16];
char dot = 0;
int den1 = 1;
int num1, num2, den2, rem;
char* cx;
double vx;
if (val < 0.000000001) {
val = 0.0;
} else if (val > 999999999.0) {
fatal(MYNAME ": Value (%f) to big for a rational representation!\n", val);
}
num1 = 0;
vx = fabs(val);
while (vx > 1) {
num1++;
vx = vx / 10;
}
snprintf(sval, sizeof(sval), "%*.*f", 9, 9 - num1, fabs(val));
snum[0] = '\0';
cx = sval;
while (*cx) {
if (dot) {
den1 *= 10;
}
if (*cx == '.') {
dot = 1;
} else {
strncat(snum, cx, 1);
}
cx++;
}
num1 = atoi(snum);
if (den1 == 1) {
*num = num1;
*den = den1;
}
num2 = num1;
den2 = den1;
rem = 1;
/* Euclid's Algorithm to find the gcd */
while (num2 % den2) {
rem = num2 % den2;
num2 = den2;
den2 = rem;
}
if (den2 != den1) {
rem = den2;
}
*num = num1 / rem;
*den = den1 / rem;
}
static exif_tag_t*
exif_put_value(const int ifd_nr, const uint16_t tag_id, const uint16_t type, const uint32_t count, const int index, const void* data)
{
exif_tag_t* tag = NULL;
exif_ifd_t* ifd;
uint16_t item_size, size;
ifd = exif_find_ifd(exif_app, ifd_nr);
if (ifd == NULL) {
ifd = (exif_ifd_t*) xcalloc(sizeof(*ifd), 1);
ifd->nr = ifd_nr;
QUEUE_INIT(&ifd->tags);
ENQUEUE_TAIL(&exif_app->ifds, &ifd->Q);
} else {
tag = exif_find_tag(exif_app, ifd_nr, tag_id);
}
item_size = exif_type_size(type);
if ((data == NULL) || (count < 1) || (index < 0)) {
size = 0;
} else {
size = (index + count) * item_size;
}
if (tag == NULL) {
if (size == 0) {
return NULL;
}
tag = (exif_tag_t*) xcalloc(sizeof(*tag), 1);
tag->id = tag_id;
tag->type = type;
tag->count = index + count;
tag->size = size;
tag->data = xcalloc((size < 4) ? 4 : size, 1);
tag->data_is_dynamic = 1;
ifd->count++;