-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinyEXIF.cpp
1317 lines (1186 loc) · 36.2 KB
/
TinyEXIF.cpp
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
/*
TinyEXIF.cpp -- A simple ISO C++ library to parse basic EXIF and XMP
information from a JPEG file.
Copyright (c) 2015-2017 Seacave
cdc.seacave@gmail.com
All rights reserved.
Based on the easyexif library (2013 version)
https://github.com/mayanklahiri/easyexif
of Mayank Lahiri (mlahiri@gmail.com).
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "TinyEXIF.h"
#ifndef TINYEXIF_NO_XMP_SUPPORT
#include <tinyxml2.h>
#endif // TINYEXIF_NO_XMP_SUPPORT
#include <cstdint>
#include <cstdio>
#include <cmath>
#include <cfloat>
#include <vector>
#include <algorithm>
#include <iostream>
#ifdef _MSC_VER
namespace {
int strcasecmp(const char* a, const char* b) {
return _stricmp(a, b);
}
}
#else
#include <string.h>
#endif
namespace Tools {
// search string inside a string, case sensitive
static const char* strrnstr(const char* haystack, const char* needle, size_t len) {
const size_t needle_len(strlen(needle));
if (0 == needle_len)
return haystack;
if (len <= needle_len)
return NULL;
for (size_t i=len-needle_len; i-- > 0; ) {
if (haystack[0] == needle[0] &&
0 == strncmp(haystack, needle, needle_len))
return haystack;
haystack++;
}
return NULL;
}
// split an input string with a delimiter and fill a string vector
static void strSplit(const std::string& str, char delim, std::vector<std::string>& values) {
values.clear();
std::string::size_type start(0), end(0);
while (end != std::string::npos) {
end = str.find(delim, start);
values.emplace_back(str.substr(start, end-start));
start = end + 1;
}
}
// make sure the given degrees value is between -180 and 180
static double NormD180(double d) {
return (d = fmod(d+180.0, 360.0)) < 0 ? d+180.0 : d-180.0;
}
} // namespace Tools
namespace TinyEXIF {
enum JPEG_MARKERS {
JM_START = 0xFF,
JM_SOF0 = 0xC0,
JM_SOF1 = 0xC1,
JM_SOF2 = 0xC2,
JM_SOF3 = 0xC3,
JM_DHT = 0xC4,
JM_SOF5 = 0xC5,
JM_SOF6 = 0xC6,
JM_SOF7 = 0xC7,
JM_JPG = 0xC8,
JM_SOF9 = 0xC9,
JM_SOF10 = 0xCA,
JM_SOF11 = 0xCB,
JM_DAC = 0xCC,
JM_SOF13 = 0xCD,
JM_SOF14 = 0xCE,
JM_SOF15 = 0xCF,
JM_RST0 = 0xD0,
JM_RST1 = 0xD1,
JM_RST2 = 0xD2,
JM_RST3 = 0xD3,
JM_RST4 = 0xD4,
JM_RST5 = 0xD5,
JM_RST6 = 0xD6,
JM_RST7 = 0xD7,
JM_SOI = 0xD8,
JM_EOI = 0xD9,
JM_SOS = 0xDA,
JM_DQT = 0xDB,
JM_DNL = 0xDC,
JM_DRI = 0xDD,
JM_DHP = 0xDE,
JM_EXP = 0xDF,
JM_APP0 = 0xE0,
JM_APP1 = 0xE1, // EXIF and XMP
JM_APP2 = 0xE2,
JM_APP3 = 0xE3,
JM_APP4 = 0xE4,
JM_APP5 = 0xE5,
JM_APP6 = 0xE6,
JM_APP7 = 0xE7,
JM_APP8 = 0xE8,
JM_APP9 = 0xE9,
JM_APP10 = 0xEA,
JM_APP11 = 0xEB,
JM_APP12 = 0xEC,
JM_APP13 = 0xED, // IPTC
JM_APP14 = 0xEE,
JM_APP15 = 0xEF,
JM_JPG0 = 0xF0,
JM_JPG1 = 0xF1,
JM_JPG2 = 0xF2,
JM_JPG3 = 0xF3,
JM_JPG4 = 0xF4,
JM_JPG5 = 0xF5,
JM_JPG6 = 0xF6,
JM_JPG7 = 0xF7,
JM_JPG8 = 0xF8,
JM_JPG9 = 0xF9,
JM_JPG10 = 0xFA,
JM_JPG11 = 0xFB,
JM_JPG12 = 0xFC,
JM_JPG13 = 0xFD,
JM_COM = 0xFE
};
// Parser helper
class EntryParser {
private:
const uint8_t* buf;
const unsigned len;
const unsigned tiff_header_start;
const bool alignIntel; // byte alignment (defined in EXIF header)
unsigned offs; // current offset into buffer
uint16_t tag, format;
uint32_t length;
public:
EntryParser(const uint8_t* _buf, unsigned _len, unsigned _tiff_header_start, bool _alignIntel)
: buf(_buf), len(_len), tiff_header_start(_tiff_header_start), alignIntel(_alignIntel), offs(0) {}
void Init(unsigned _offs) {
offs = _offs - 12;
}
void ParseTag() {
offs += 12;
tag = parse16(buf + offs, alignIntel);
format = parse16(buf + offs + 2, alignIntel);
length = parse32(buf + offs + 4, alignIntel);
}
const uint8_t* GetBuffer() const { return buf; }
unsigned GetOffset() const { return offs; }
bool IsIntelAligned() const { return alignIntel; }
uint16_t GetTag() const { return tag; }
uint32_t GetLength() const { return length; }
uint32_t GetData() const { return parse32(buf + offs + 8, alignIntel); }
uint32_t GetSubIFD() const { return tiff_header_start + GetData(); }
bool IsShort() const { return format == 3; }
bool IsLong() const { return format == 4; }
bool IsRational() const { return format == 5 || format == 10; }
bool IsSRational() const { return format == 10; }
bool IsFloat() const { return format == 11; }
bool IsUndefined() const { return format == 7; }
std::string FetchString() const {
return parseString(buf, length, GetData(), tiff_header_start, len, alignIntel);
}
bool Fetch(std::string& val) const {
if (format != 2 || length == 0)
return false;
val = FetchString();
return true;
}
bool Fetch(uint8_t& val) const {
if ((format != 1 && format != 2 && format != 6) || length == 0)
return false;
val = parse8(buf + offs + 8);
return true;
}
bool Fetch(uint16_t& val) const {
if (!IsShort() || length == 0)
return false;
val = parse16(buf + offs + 8, alignIntel);
return true;
}
bool Fetch(uint16_t& val, uint32_t idx) const {
if (!IsShort() || length <= idx)
return false;
val = parse16(buf + GetSubIFD() + idx*2, alignIntel);
return true;
}
bool Fetch(uint32_t& val) const {
if (!IsLong() || length == 0)
return false;
val = parse32(buf + offs + 8, alignIntel);
return true;
}
bool Fetch(float& val) const {
if (!IsFloat() || length == 0)
return false;
val = parseFloat(buf + offs + 8, alignIntel);
return true;
}
bool Fetch(double& val) const {
if (!IsRational() || length == 0)
return false;
val = parseRational(buf + GetSubIFD(), alignIntel, IsSRational());
return true;
}
bool Fetch(double& val, uint32_t idx) const {
if (!IsRational() || length <= idx)
return false;
val = parseRational(buf + GetSubIFD() + idx*8, alignIntel, IsSRational());
return true;
}
bool FetchFloat(double& val) const {
float _val;
if (!Fetch(_val))
return false;
val = _val;
return true;
}
public:
static uint8_t parse8(const uint8_t* buf) {
return buf[0];
}
static uint16_t parse16(const uint8_t* buf, bool intel) {
if (intel)
return ((uint16_t)buf[1]<<8) | buf[0];
return ((uint16_t)buf[0]<<8) | buf[1];
}
static uint32_t parse32(const uint8_t* buf, bool intel) {
if (intel)
return ((uint32_t)buf[3]<<24) |
((uint32_t)buf[2]<<16) |
((uint32_t)buf[1]<<8) |
buf[0];
return ((uint32_t)buf[0]<<24) |
((uint32_t)buf[1]<<16) |
((uint32_t)buf[2]<<8) |
buf[3];
}
static float parseFloat(const uint8_t* buf, bool intel) {
union {
uint32_t i;
float f;
} i2f;
i2f.i = parse32(buf, intel);
return i2f.f;
}
static double parseRational(const uint8_t* buf, bool intel, bool isSigned) {
const uint32_t denominator = parse32(buf+4, intel);
if (denominator == 0)
return 0.0;
const uint32_t numerator = parse32(buf, intel);
return isSigned ?
(double)(int32_t)numerator/(double)(int32_t)denominator :
(double)numerator/(double)denominator;
}
static std::string parseString(const uint8_t* buf,
unsigned num_components,
unsigned data,
unsigned base,
unsigned len,
bool intel)
{
std::string value;
if (num_components <= 4) {
value.resize(num_components);
char j = intel ? 0 : 24;
char j_m = intel ? -8 : 8;
for (unsigned i=0; i<num_components; ++i, j -= j_m)
value[i] = (data >> j) & 0xff;
if (value[num_components-1] == '\0')
value.resize(num_components-1);
} else
if (base+data+num_components <= len) {
const char* const sz((const char*)buf+base+data);
unsigned num(0);
while (num < num_components && sz[num] != '\0')
++num;
while (num && sz[num-1] == ' ')
--num;
value.assign(sz, num);
}
return value;
}
};
// Constructors
EXIFInfo::EXIFInfo() : Fields(FIELD_NA) {
}
EXIFInfo::EXIFInfo(EXIFStream& stream) {
parseFrom(stream);
}
EXIFInfo::EXIFInfo(std::istream& stream) {
parseFrom(stream);
}
EXIFInfo::EXIFInfo(const uint8_t* data, unsigned length) {
parseFrom(data, length);
}
// Parse tag as Image IFD
void EXIFInfo::parseIFDImage(EntryParser& parser, unsigned& exif_sub_ifd_offset, unsigned& gps_sub_ifd_offset) {
switch (parser.GetTag()) {
case 0x0102:
// Bits per sample
parser.Fetch(BitsPerSample);
break;
case 0x010e:
// Image description
parser.Fetch(ImageDescription);
break;
case 0x010f:
// Camera maker
parser.Fetch(Make);
break;
case 0x0110:
// Camera model
parser.Fetch(Model);
break;
case 0x0112:
// Orientation of image
parser.Fetch(Orientation);
break;
case 0x011a:
// XResolution
parser.Fetch(XResolution);
break;
case 0x011b:
// YResolution
parser.Fetch(YResolution);
break;
case 0x0128:
// Resolution Unit
parser.Fetch(ResolutionUnit);
break;
case 0x0131:
// Software used for image
parser.Fetch(Software);
break;
case 0x0132:
// EXIF/TIFF date/time of image modification
parser.Fetch(DateTime);
break;
case 0x1001:
// Original Image width
if (!parser.Fetch(RelatedImageWidth)) {
uint16_t _RelatedImageWidth;
if (parser.Fetch(_RelatedImageWidth))
RelatedImageWidth = _RelatedImageWidth;
}
break;
case 0x1002:
// Original Image height
if (!parser.Fetch(RelatedImageHeight)) {
uint16_t _RelatedImageHeight;
if (parser.Fetch(_RelatedImageHeight))
RelatedImageHeight = _RelatedImageHeight;
}
break;
case 0x8298:
// Copyright information
parser.Fetch(Copyright);
break;
case 0x8769:
// EXIF SubIFD offset
exif_sub_ifd_offset = parser.GetSubIFD();
break;
case 0x8825:
// GPS IFS offset
gps_sub_ifd_offset = parser.GetSubIFD();
break;
default:
// Try to parse as EXIF tag, as some images store them in here
parseIFDExif(parser);
break;
}
}
// Parse tag as Exif IFD
void EXIFInfo::parseIFDExif(EntryParser& parser) {
switch (parser.GetTag()) {
case 0x02bc:
#ifndef TINYEXIF_NO_XMP_SUPPORT
// XMP Metadata (Adobe technote 9-14-02)
if (parser.IsUndefined()) {
const std::string strXML(parser.FetchString());
parseFromXMPSegmentXML(strXML.c_str(), (unsigned)strXML.length());
}
#endif // TINYEXIF_NO_XMP_SUPPORT
break;
case 0x829a:
// Exposure time in seconds
parser.Fetch(ExposureTime);
break;
case 0x829d:
// FNumber
parser.Fetch(FNumber);
break;
case 0x8822:
// Exposure Program
parser.Fetch(ExposureProgram);
break;
case 0x8827:
// ISO Speed Rating
parser.Fetch(ISOSpeedRatings);
break;
case 0x9003:
// Original date and time
parser.Fetch(DateTimeOriginal);
break;
case 0x9004:
// Digitization date and time
parser.Fetch(DateTimeDigitized);
break;
case 0x9201:
// Shutter speed value
parser.Fetch(ShutterSpeedValue);
ShutterSpeedValue = 1.0/exp(ShutterSpeedValue*log(2));
break;
case 0x9202:
// Aperture value
parser.Fetch(ApertureValue);
ApertureValue = exp(ApertureValue*log(2)*0.5);
break;
case 0x9203:
// Brightness value
parser.Fetch(BrightnessValue);
break;
case 0x9204:
// Exposure bias value
parser.Fetch(ExposureBiasValue);
break;
case 0x9206:
// Subject distance
parser.Fetch(SubjectDistance);
break;
case 0x9207:
// Metering mode
parser.Fetch(MeteringMode);
break;
case 0x9208:
// Light source
parser.Fetch(LightSource);
break;
case 0x9209:
// Flash info
parser.Fetch(Flash);
break;
case 0x920a:
// Focal length
parser.Fetch(FocalLength);
break;
case 0x9214:
// Subject area
if (parser.IsShort() && parser.GetLength() > 1) {
SubjectArea.resize(parser.GetLength());
for (uint32_t i=0; i<parser.GetLength(); ++i)
parser.Fetch(SubjectArea[i], i);
}
break;
case 0x927c:
// MakerNote
parseIFDMakerNote(parser);
break;
case 0x9291:
// Fractions of seconds for DateTimeOriginal
parser.Fetch(SubSecTimeOriginal);
break;
case 0xa002:
// EXIF Image width
if (!parser.Fetch(ImageWidth)) {
uint16_t _ImageWidth;
if (parser.Fetch(_ImageWidth))
ImageWidth = _ImageWidth;
}
break;
case 0xa003:
// EXIF Image height
if (!parser.Fetch(ImageHeight)) {
uint16_t _ImageHeight;
if (parser.Fetch(_ImageHeight))
ImageHeight = _ImageHeight;
}
break;
case 0xa20e:
// Focal plane X resolution
parser.Fetch(LensInfo.FocalPlaneXResolution);
break;
case 0xa20f:
// Focal plane Y resolution
parser.Fetch(LensInfo.FocalPlaneYResolution);
break;
case 0xa210:
// Focal plane resolution unit
parser.Fetch(LensInfo.FocalPlaneResolutionUnit);
break;
case 0xa215:
// Exposure Index and ISO Speed Rating are often used interchangeably
if (ISOSpeedRatings == 0) {
double ExposureIndex;
if (parser.Fetch(ExposureIndex))
ISOSpeedRatings = (uint16_t)ExposureIndex;
}
break;
case 0xa404:
// Digital Zoom Ratio
parser.Fetch(LensInfo.DigitalZoomRatio);
break;
case 0xa405:
// Focal length in 35mm film
if (!parser.Fetch(LensInfo.FocalLengthIn35mm)) {
uint16_t _FocalLengthIn35mm;
if (parser.Fetch(_FocalLengthIn35mm))
LensInfo.FocalLengthIn35mm = (double)_FocalLengthIn35mm;
}
break;
case 0xa431:
// Serial number of the camera
parser.Fetch(SerialNumber);
break;
case 0xa432:
// Focal length and FStop.
if (parser.Fetch(LensInfo.FocalLengthMin, 0))
if (parser.Fetch(LensInfo.FocalLengthMax, 1))
if (parser.Fetch(LensInfo.FStopMin, 2))
parser.Fetch(LensInfo.FStopMax, 3);
break;
case 0xa433:
// Lens make.
parser.Fetch(LensInfo.Make);
break;
case 0xa434:
// Lens model.
parser.Fetch(LensInfo.Model);
break;
}
}
// Parse tag as MakerNote IFD
void EXIFInfo::parseIFDMakerNote(EntryParser& parser) {
const unsigned startOff = parser.GetOffset();
const uint32_t off = parser.GetSubIFD();
if (0 != strcasecmp(Make.c_str(), "DJI"))
return;
int num_entries = EntryParser::parse16(parser.GetBuffer()+off, parser.IsIntelAligned());
if (uint32_t(2 + 12 * num_entries) > parser.GetLength())
return;
parser.Init(off+2);
parser.ParseTag();
--num_entries;
std::string maker;
if (parser.GetTag() == 1 && parser.Fetch(maker)) {
if (0 == strcasecmp(maker.c_str(), "DJI")) {
while (--num_entries >= 0) {
parser.ParseTag();
switch (parser.GetTag()) {
case 3:
// SpeedX
parser.FetchFloat(GeoLocation.SpeedX);
break;
case 4:
// SpeedY
parser.FetchFloat(GeoLocation.SpeedY);
break;
case 5:
// SpeedZ
parser.FetchFloat(GeoLocation.SpeedZ);
break;
case 9:
// Camera Pitch
parser.FetchFloat(GeoLocation.PitchDegree);
break;
case 10:
// Camera Yaw
parser.FetchFloat(GeoLocation.YawDegree);
break;
case 11:
// Camera Roll
parser.FetchFloat(GeoLocation.RollDegree);
break;
}
}
}
}
parser.Init(startOff+12);
}
// Parse tag as GPS IFD
void EXIFInfo::parseIFDGPS(EntryParser& parser) {
switch (parser.GetTag()) {
case 1:
// GPS north or south
parser.Fetch(GeoLocation.LatComponents.direction);
break;
case 2:
// GPS latitude
if (parser.IsRational() && parser.GetLength() == 3) {
parser.Fetch(GeoLocation.LatComponents.degrees, 0);
parser.Fetch(GeoLocation.LatComponents.minutes, 1);
parser.Fetch(GeoLocation.LatComponents.seconds, 2);
}
break;
case 3:
// GPS east or west
parser.Fetch(GeoLocation.LonComponents.direction);
break;
case 4:
// GPS longitude
if (parser.IsRational() && parser.GetLength() == 3) {
parser.Fetch(GeoLocation.LonComponents.degrees, 0);
parser.Fetch(GeoLocation.LonComponents.minutes, 1);
parser.Fetch(GeoLocation.LonComponents.seconds, 2);
}
break;
case 5:
// GPS altitude reference (below or above sea level)
parser.Fetch((uint8_t&)GeoLocation.AltitudeRef);
break;
case 6:
// GPS altitude
parser.Fetch(GeoLocation.Altitude);
break;
case 7:
// GPS timestamp
if (parser.IsRational() && parser.GetLength() == 3) {
double h,m,s;
parser.Fetch(h, 0);
parser.Fetch(m, 1);
parser.Fetch(s, 2);
char buffer[256];
snprintf(buffer, 256, "%g %g %g", h, m, s);
GeoLocation.GPSTimeStamp = buffer;
}
break;
case 11:
// Indicates the GPS DOP (data degree of precision)
parser.Fetch(GeoLocation.GPSDOP);
break;
case 18:
// GPS geodetic survey data
parser.Fetch(GeoLocation.GPSMapDatum);
break;
case 29:
// GPS date-stamp
parser.Fetch(GeoLocation.GPSDateStamp);
break;
case 30:
// GPS differential indicates whether differential correction is applied to the GPS receiver
parser.Fetch(GeoLocation.GPSDifferential);
break;
}
}
//
// Locates the JM_APP1 segment and parses it using
// parseFromEXIFSegment() or parseFromXMPSegment()
//
int EXIFInfo::parseFrom(EXIFStream& stream) {
clear();
if (!stream.IsValid())
return PARSE_INVALID_JPEG;
// Sanity check: all JPEG files start with 0xFFD8 and end with 0xFFD9
// This check also ensures that the user has supplied a correct value for len.
const uint8_t* buf(stream.GetBuffer(2));
if (buf == NULL || buf[0] != JM_START || buf[1] != JM_SOI)
return PARSE_INVALID_JPEG;
// Scan for JM_APP1 header (bytes 0xFF 0xE1) and parse its length.
// Exit if both EXIF and XMP sections were parsed.
struct APP1S {
uint32_t& val;
inline APP1S(uint32_t& v) : val(v) {}
inline operator uint32_t () const { return val; }
inline operator uint32_t& () { return val; }
inline int operator () (int code=PARSE_ABSENT_DATA) const { return val&FIELD_ALL ? (int)PARSE_SUCCESS : code; }
} app1s(Fields);
while ((buf=stream.GetBuffer(2)) != NULL) {
// find next marker;
// in cases of markers appended after the compressed data,
// optional JM_START fill bytes may precede the marker
if (*buf++ != JM_START)
break;
uint8_t marker;
while ((marker=buf[0]) == JM_START && (buf=stream.GetBuffer(1)) != NULL);
// select marker
uint16_t sectionLength;
switch (marker) {
case 0x00:
case 0x01:
case JM_START:
case JM_RST0:
case JM_RST1:
case JM_RST2:
case JM_RST3:
case JM_RST4:
case JM_RST5:
case JM_RST6:
case JM_RST7:
case JM_SOI:
break;
case JM_SOS: // start of stream: and we're done
case JM_EOI: // no data? not good
return app1s();
case JM_APP1:
if ((buf=stream.GetBuffer(2)) == NULL)
return app1s(PARSE_INVALID_JPEG);
sectionLength = EntryParser::parse16(buf, false);
if (sectionLength <= 2 || (buf=stream.GetBuffer(sectionLength-=2)) == NULL)
return app1s(PARSE_INVALID_JPEG);
switch (int ret=parseFromEXIFSegment(buf, sectionLength)) {
case PARSE_ABSENT_DATA:
#ifndef TINYEXIF_NO_XMP_SUPPORT
switch (ret=parseFromXMPSegment(buf, sectionLength)) {
case PARSE_ABSENT_DATA:
break;
case PARSE_SUCCESS:
if ((app1s|=FIELD_XMP) == FIELD_ALL)
return PARSE_SUCCESS;
break;
default:
return app1s(ret); // some error
}
#endif // TINYEXIF_NO_XMP_SUPPORT
break;
case PARSE_SUCCESS:
if ((app1s|=FIELD_EXIF) == FIELD_ALL)
return PARSE_SUCCESS;
break;
default:
return app1s(ret); // some error
}
break;
default:
// skip the section
if ((buf=stream.GetBuffer(2)) == NULL ||
(sectionLength=EntryParser::parse16(buf, false)) <= 2 ||
!stream.SkipBuffer(sectionLength-2))
return app1s(PARSE_INVALID_JPEG);
}
}
return app1s();
}
int EXIFInfo::parseFrom(std::istream& stream) {
class EXIFStdStream : public EXIFStream {
public:
EXIFStdStream(std::istream& stream)
: stream(stream) {
// Would be nice to assert here that the stream was opened in binary mode, but
// apparently that's not possible: https://stackoverflow.com/a/224259/19254
}
bool IsValid() const override {
return !!stream;
}
const uint8_t* GetBuffer(unsigned desiredLength) override {
buffer.resize(desiredLength);
if (!stream.read(reinterpret_cast<char*>(buffer.data()), desiredLength))
return NULL;
return buffer.data();
}
bool SkipBuffer(unsigned desiredLength) override {
return (bool)stream.seekg(desiredLength, std::ios::cur);
}
private:
std::istream& stream;
std::vector<uint8_t> buffer;
};
EXIFStdStream streamWrapper(stream);
return parseFrom(streamWrapper);
}
int EXIFInfo::parseFrom(const uint8_t* buf, unsigned len) {
class EXIFStreamBuffer : public EXIFStream {
public:
explicit EXIFStreamBuffer(const uint8_t* buf, unsigned len)
: it(buf), end(buf+len) {}
bool IsValid() const override {
return it != NULL;
}
const uint8_t* GetBuffer(unsigned desiredLength) override {
const uint8_t* const itNext(it+desiredLength);
if (itNext >= end)
return NULL;
const uint8_t* const begin(it);
it = itNext;
return begin;
}
bool SkipBuffer(unsigned desiredLength) override {
return GetBuffer(desiredLength) != NULL;
}
private:
const uint8_t* it, * const end;
};
EXIFStreamBuffer stream(buf, len);
return parseFrom(stream);
}
//
// Main parsing function for an EXIF segment.
// Do a sanity check by looking for bytes "Exif\0\0".
// The marker has to contain at least the TIFF header, otherwise the
// JM_APP1 data is corrupt. So the minimum length specified here has to be:
// 6 bytes: "Exif\0\0" string
// 2 bytes: TIFF header (either "II" or "MM" string)
// 2 bytes: TIFF magic (short 0x2a00 in Motorola byte order)
// 4 bytes: Offset to first IFD
// =========
// 14 bytes
//
// PARAM: 'buf' start of the EXIF TIFF, which must be the bytes "Exif\0\0".
// PARAM: 'len' length of buffer
//
int EXIFInfo::parseFromEXIFSegment(const uint8_t* buf, unsigned len) {
unsigned offs = 6; // current offset into buffer
if (!buf || len < offs)
return PARSE_ABSENT_DATA;
if (!std::equal(buf, buf+offs, "Exif\0\0"))
return PARSE_ABSENT_DATA;
// Now parsing the TIFF header. The first two bytes are either "II" or
// "MM" for Intel or Motorola byte alignment. Sanity check by parsing
// the uint16_t that follows, making sure it equals 0x2a. The
// last 4 bytes are an offset into the first IFD, which are added to
// the global offset counter. For this block, we expect the following
// minimum size:
// 2 bytes: 'II' or 'MM'
// 2 bytes: 0x002a
// 4 bytes: offset to first IDF
// -----------------------------
// 8 bytes
if (offs + 8 > len)
return PARSE_CORRUPT_DATA;
bool alignIntel;
if (buf[offs] == 'I' && buf[offs+1] == 'I')
alignIntel = true; // 1: Intel byte alignment
else
if (buf[offs] == 'M' && buf[offs+1] == 'M')
alignIntel = false; // 0: Motorola byte alignment
else
return PARSE_UNKNOWN_BYTEALIGN;
EntryParser parser(buf, len, offs, alignIntel);
offs += 2;
if (0x2a != EntryParser::parse16(buf + offs, alignIntel))
return PARSE_CORRUPT_DATA;
offs += 2;
const unsigned first_ifd_offset = EntryParser::parse32(buf + offs, alignIntel);
offs += first_ifd_offset - 4;
if (offs >= len)
return PARSE_CORRUPT_DATA;
// Now parsing the first Image File Directory (IFD0, for the main image).
// An IFD consists of a variable number of 12-byte directory entries. The
// first two bytes of the IFD section contain the number of directory
// entries in the section. The last 4 bytes of the IFD contain an offset
// to the next IFD, which means this IFD must contain exactly 6 + 12 * num
// bytes of data.
if (offs + 2 > len)
return PARSE_CORRUPT_DATA;
int num_entries = EntryParser::parse16(buf + offs, alignIntel);
if (offs + 6 + 12 * num_entries > len)
return PARSE_CORRUPT_DATA;
unsigned exif_sub_ifd_offset = len;
unsigned gps_sub_ifd_offset = len;
parser.Init(offs+2);
while (--num_entries >= 0) {
parser.ParseTag();
parseIFDImage(parser, exif_sub_ifd_offset, gps_sub_ifd_offset);
}
// Jump to the EXIF SubIFD if it exists and parse all the information
// there. Note that it's possible that the EXIF SubIFD doesn't exist.
// The EXIF SubIFD contains most of the interesting information that a
// typical user might want.
if (exif_sub_ifd_offset + 4 <= len) {
offs = exif_sub_ifd_offset;
num_entries = EntryParser::parse16(buf + offs, alignIntel);
if (offs + 6 + 12 * num_entries > len)
return PARSE_CORRUPT_DATA;
parser.Init(offs+2);
while (--num_entries >= 0) {
parser.ParseTag();
parseIFDExif(parser);
}
}
// Jump to the GPS SubIFD if it exists and parse all the information
// there. Note that it's possible that the GPS SubIFD doesn't exist.