exif.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /**************************************************************************
  2. exif.cpp -- A simple ISO C++ library to parse basic EXIF
  3. information from a JPEG file.
  4. Copyright (c) 2010-2015 Mayank Lahiri
  5. mlahiri@gmail.com
  6. All rights reserved (BSD License).
  7. See exif.h for version history.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are met:
  10. -- Redistributions of source code must retain the above copyright notice,
  11. this list of conditions and the following disclaimer.
  12. -- Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS
  16. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  18. NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  19. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  20. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  24. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "exif.h"
  27. #include <algorithm>
  28. #include <cstdint>
  29. #include <stdio.h>
  30. #include <vector>
  31. using std::string;
  32. namespace {
  33. struct Rational {
  34. uint32_t numerator, denominator;
  35. operator double() const {
  36. if (denominator < 1e-20) {
  37. return 0;
  38. }
  39. return static_cast<double>(numerator) / static_cast<double>(denominator);
  40. }
  41. };
  42. // IF Entry
  43. class IFEntry {
  44. public:
  45. using byte_vector = std::vector<uint8_t>;
  46. using ascii_vector = std::string;
  47. using short_vector = std::vector<uint16_t>;
  48. using long_vector = std::vector<uint32_t>;
  49. using rational_vector = std::vector<Rational>;
  50. IFEntry()
  51. : tag_(0xFF), format_(0xFF), data_(0), length_(0), val_byte_(nullptr) {}
  52. IFEntry(const IFEntry &) = delete;
  53. IFEntry &operator=(const IFEntry &) = delete;
  54. IFEntry(IFEntry &&other)
  55. : tag_(other.tag_),
  56. format_(other.format_),
  57. data_(other.data_),
  58. length_(other.length_),
  59. val_byte_(other.val_byte_) {
  60. other.tag_ = 0xFF;
  61. other.format_ = 0xFF;
  62. other.data_ = 0;
  63. other.length_ = 0;
  64. other.val_byte_ = nullptr;
  65. }
  66. ~IFEntry() { delete_union(); }
  67. unsigned short tag() const { return tag_; }
  68. void tag(unsigned short tag) { tag_ = tag; }
  69. unsigned short format() const { return format_; }
  70. bool format(unsigned short format) {
  71. switch (format) {
  72. case 0x01:
  73. case 0x02:
  74. case 0x03:
  75. case 0x04:
  76. case 0x05:
  77. case 0x07:
  78. case 0x09:
  79. case 0x0a:
  80. case 0xff:
  81. break;
  82. default:
  83. return false;
  84. }
  85. delete_union();
  86. format_ = format;
  87. new_union();
  88. return true;
  89. }
  90. unsigned data() const { return data_; }
  91. void data(unsigned data) { data_ = data; }
  92. unsigned length() const { return length_; }
  93. void length(unsigned length) { length_ = length; }
  94. // functions to access the data
  95. //
  96. // !! it's CALLER responsibility to check that format !!
  97. // !! is correct before accessing it's field !!
  98. //
  99. // - getters are use here to allow future addition
  100. // of checks if format is correct
  101. byte_vector &val_byte() { return *val_byte_; }
  102. ascii_vector &val_string() { return *val_string_; }
  103. short_vector &val_short() { return *val_short_; }
  104. long_vector &val_long() { return *val_long_; }
  105. rational_vector &val_rational() { return *val_rational_; }
  106. private:
  107. // Raw fields
  108. unsigned short tag_;
  109. unsigned short format_;
  110. unsigned data_;
  111. unsigned length_;
  112. // Parsed fields
  113. union {
  114. byte_vector *val_byte_;
  115. ascii_vector *val_string_;
  116. short_vector *val_short_;
  117. long_vector *val_long_;
  118. rational_vector *val_rational_;
  119. };
  120. void delete_union() {
  121. switch (format_) {
  122. case 0x1:
  123. delete val_byte_;
  124. val_byte_ = nullptr;
  125. break;
  126. case 0x2:
  127. delete val_string_;
  128. val_string_ = nullptr;
  129. break;
  130. case 0x3:
  131. delete val_short_;
  132. val_short_ = nullptr;
  133. break;
  134. case 0x4:
  135. delete val_long_;
  136. val_long_ = nullptr;
  137. break;
  138. case 0x5:
  139. delete val_rational_;
  140. val_rational_ = nullptr;
  141. break;
  142. case 0xff:
  143. break;
  144. default:
  145. // should not get here
  146. // should I throw an exception or ...?
  147. break;
  148. }
  149. }
  150. void new_union() {
  151. switch (format_) {
  152. case 0x1:
  153. val_byte_ = new byte_vector();
  154. break;
  155. case 0x2:
  156. val_string_ = new ascii_vector();
  157. break;
  158. case 0x3:
  159. val_short_ = new short_vector();
  160. break;
  161. case 0x4:
  162. val_long_ = new long_vector();
  163. break;
  164. case 0x5:
  165. val_rational_ = new rational_vector();
  166. break;
  167. case 0xff:
  168. break;
  169. default:
  170. // should not get here
  171. // should I throw an exception or ...?
  172. break;
  173. }
  174. }
  175. };
  176. // Helper functions
  177. template <typename T, bool alignIntel>
  178. T parse(const unsigned char *buf);
  179. template <>
  180. uint8_t parse<uint8_t, false>(const unsigned char *buf) {
  181. return *buf;
  182. }
  183. template <>
  184. uint8_t parse<uint8_t, true>(const unsigned char *buf) {
  185. return *buf;
  186. }
  187. template <>
  188. uint16_t parse<uint16_t, false>(const unsigned char *buf) {
  189. return (static_cast<uint16_t>(buf[0]) << 8) | buf[1];
  190. }
  191. template <>
  192. uint16_t parse<uint16_t, true>(const unsigned char *buf) {
  193. return (static_cast<uint16_t>(buf[1]) << 8) | buf[0];
  194. }
  195. template <>
  196. uint32_t parse<uint32_t, false>(const unsigned char *buf) {
  197. return (static_cast<uint32_t>(buf[0]) << 24) |
  198. (static_cast<uint32_t>(buf[1]) << 16) |
  199. (static_cast<uint32_t>(buf[2]) << 8) | buf[3];
  200. }
  201. template <>
  202. uint32_t parse<uint32_t, true>(const unsigned char *buf) {
  203. return (static_cast<uint32_t>(buf[3]) << 24) |
  204. (static_cast<uint32_t>(buf[2]) << 16) |
  205. (static_cast<uint32_t>(buf[1]) << 8) | buf[0];
  206. }
  207. template <>
  208. Rational parse<Rational, true>(const unsigned char *buf) {
  209. Rational r;
  210. r.numerator = parse<uint32_t, true>(buf);
  211. r.denominator = parse<uint32_t, true>(buf + 4);
  212. return r;
  213. }
  214. template <>
  215. Rational parse<Rational, false>(const unsigned char *buf) {
  216. Rational r;
  217. r.numerator = parse<uint32_t, false>(buf);
  218. r.denominator = parse<uint32_t, false>(buf + 4);
  219. return r;
  220. }
  221. /**
  222. * Try to read entry.length() values for this entry.
  223. *
  224. * Returns:
  225. * true - entry.length() values were read
  226. * false - something went wrong, vec's content was not touched
  227. */
  228. template <typename T, bool alignIntel, typename C>
  229. bool extract_values(C &container, const unsigned char *buf, const unsigned base,
  230. const unsigned len, const IFEntry &entry) {
  231. const unsigned char *data;
  232. uint32_t reversed_data;
  233. // if data fits into 4 bytes, they are stored directly in
  234. // the data field in IFEntry
  235. if (sizeof(T) * entry.length() <= 4) {
  236. if (alignIntel) {
  237. reversed_data = entry.data();
  238. } else {
  239. reversed_data = entry.data();
  240. // this reversing works, but is ugly
  241. unsigned char *data = reinterpret_cast<unsigned char *>(&reversed_data);
  242. unsigned char tmp;
  243. tmp = data[0];
  244. data[0] = data[3];
  245. data[3] = tmp;
  246. tmp = data[1];
  247. data[1] = data[2];
  248. data[2] = tmp;
  249. }
  250. data = reinterpret_cast<const unsigned char *>(&(reversed_data));
  251. } else {
  252. data = buf + base + entry.data();
  253. if (data + sizeof(T) * entry.length() > buf + len) {
  254. return false;
  255. }
  256. }
  257. container.resize(entry.length());
  258. for (size_t i = 0; i < entry.length(); ++i) {
  259. container[i] = parse<T, alignIntel>(data + sizeof(T) * i);
  260. }
  261. return true;
  262. }
  263. template <bool alignIntel>
  264. void parseIFEntryHeader(const unsigned char *buf, unsigned short &tag,
  265. unsigned short &format, unsigned &length,
  266. unsigned &data) {
  267. // Each directory entry is composed of:
  268. // 2 bytes: tag number (data field)
  269. // 2 bytes: data format
  270. // 4 bytes: number of components
  271. // 4 bytes: data value or offset to data value
  272. tag = parse<uint16_t, alignIntel>(buf);
  273. format = parse<uint16_t, alignIntel>(buf + 2);
  274. length = parse<uint32_t, alignIntel>(buf + 4);
  275. data = parse<uint32_t, alignIntel>(buf + 8);
  276. }
  277. template <bool alignIntel>
  278. void parseIFEntryHeader(const unsigned char *buf, IFEntry &result) {
  279. unsigned short tag;
  280. unsigned short format;
  281. unsigned length;
  282. unsigned data;
  283. parseIFEntryHeader<alignIntel>(buf, tag, format, length, data);
  284. result.tag(tag);
  285. result.format(format);
  286. result.length(length);
  287. result.data(data);
  288. }
  289. template <bool alignIntel>
  290. IFEntry parseIFEntry_temp(const unsigned char *buf, const unsigned offs,
  291. const unsigned base, const unsigned len) {
  292. IFEntry result;
  293. // check if there even is enough data for IFEntry in the buffer
  294. if (buf + offs + 12 > buf + len) {
  295. result.tag(0xFF);
  296. return result;
  297. }
  298. parseIFEntryHeader<alignIntel>(buf + offs, result);
  299. // Parse value in specified format
  300. switch (result.format()) {
  301. case 1:
  302. if (!extract_values<uint8_t, alignIntel>(result.val_byte(), buf, base,
  303. len, result)) {
  304. result.tag(0xFF);
  305. }
  306. break;
  307. case 2:
  308. // string is basically sequence of uint8_t (well, according to EXIF even
  309. // uint7_t, but
  310. // we don't have that), so just read it as bytes
  311. if (!extract_values<uint8_t, alignIntel>(result.val_string(), buf, base,
  312. len, result)) {
  313. result.tag(0xFF);
  314. }
  315. // and cut zero byte at the end, since we don't want that in the
  316. // std::string
  317. if (result.val_string()[result.val_string().length() - 1] == '\0') {
  318. result.val_string().resize(result.val_string().length() - 1);
  319. }
  320. break;
  321. case 3:
  322. if (!extract_values<uint16_t, alignIntel>(result.val_short(), buf, base,
  323. len, result)) {
  324. result.tag(0xFF);
  325. }
  326. break;
  327. case 4:
  328. if (!extract_values<uint32_t, alignIntel>(result.val_long(), buf, base,
  329. len, result)) {
  330. result.tag(0xFF);
  331. }
  332. break;
  333. case 5:
  334. if (!extract_values<Rational, alignIntel>(result.val_rational(), buf,
  335. base, len, result)) {
  336. result.tag(0xFF);
  337. }
  338. break;
  339. case 7:
  340. case 9:
  341. case 10:
  342. break;
  343. default:
  344. result.tag(0xFF);
  345. }
  346. return result;
  347. }
  348. // helper functions for convinience
  349. template <typename T>
  350. T parse_value(const unsigned char *buf, bool alignIntel) {
  351. if (alignIntel) {
  352. return parse<T, true>(buf);
  353. } else {
  354. return parse<T, false>(buf);
  355. }
  356. }
  357. void parseIFEntryHeader(const unsigned char *buf, bool alignIntel,
  358. unsigned short &tag, unsigned short &format,
  359. unsigned &length, unsigned &data) {
  360. if (alignIntel) {
  361. parseIFEntryHeader<true>(buf, tag, format, length, data);
  362. } else {
  363. parseIFEntryHeader<false>(buf, tag, format, length, data);
  364. }
  365. }
  366. IFEntry parseIFEntry(const unsigned char *buf, const unsigned offs,
  367. const bool alignIntel, const unsigned base,
  368. const unsigned len) {
  369. if (alignIntel) {
  370. return parseIFEntry_temp<true>(buf, offs, base, len);
  371. } else {
  372. return parseIFEntry_temp<false>(buf, offs, base, len);
  373. }
  374. }
  375. }
  376. //
  377. // Locates the EXIF segment and parses it using parseFromEXIFSegment
  378. //
  379. int easyexif::EXIFInfo::parseFrom(const unsigned char *buf, unsigned len) {
  380. // Sanity check: all JPEG files start with 0xFFD8.
  381. if (!buf || len < 4) return PARSE_EXIF_ERROR_NO_JPEG;
  382. if (buf[0] != 0xFF || buf[1] != 0xD8) return PARSE_EXIF_ERROR_NO_JPEG;
  383. // Sanity check: some cameras pad the JPEG image with some bytes at the end.
  384. // Normally, we should be able to find the JPEG end marker 0xFFD9 at the end
  385. // of the image buffer, but not always. As long as there are some bytes
  386. // except 0xD9 at the end of the image buffer, keep decrementing len until
  387. // an 0xFFD9 is found. If JPEG end marker 0xFFD9 is not found,
  388. // then we can be reasonably sure that the buffer is not a JPEG.
  389. /*
  390. while (len > 2) {
  391. if (buf[len - 1] == 0xD9 && buf[len - 2] == 0xFF)
  392. break;
  393. len--;
  394. }
  395. if (len <= 2)
  396. return PARSE_EXIF_ERROR_NO_JPEG;
  397. */
  398. clear();
  399. // Scan for EXIF header (bytes 0xFF 0xE1) and do a sanity check by
  400. // looking for bytes "Exif\0\0". The marker length data is in Motorola
  401. // byte order, which results in the 'false' parameter to parse16().
  402. // The marker has to contain at least the TIFF header, otherwise the
  403. // EXIF data is corrupt. So the minimum length specified here has to be:
  404. // 2 bytes: section size
  405. // 6 bytes: "Exif\0\0" string
  406. // 2 bytes: TIFF header (either "II" or "MM" string)
  407. // 2 bytes: TIFF magic (short 0x2a00 in Motorola byte order)
  408. // 4 bytes: Offset to first IFD
  409. // =========
  410. // 16 bytes
  411. unsigned offs = 0; // current offset into buffer
  412. for (offs = 0; offs < len - 1; offs++)
  413. if (buf[offs] == 0xFF && buf[offs + 1] == 0xE1) break;
  414. if (offs + 4 > len) return PARSE_EXIF_ERROR_NO_EXIF;
  415. offs += 2;
  416. unsigned short section_length = parse_value<uint16_t>(buf + offs, false);
  417. if (offs + section_length > len || section_length < 16)
  418. return PARSE_EXIF_ERROR_CORRUPT;
  419. offs += 2;
  420. return parseFromEXIFSegment(buf + offs, len - offs);
  421. }
  422. int easyexif::EXIFInfo::parseFrom(const string &data) {
  423. return parseFrom(
  424. reinterpret_cast<const unsigned char *>(data.data()), static_cast<unsigned>(data.length()));
  425. }
  426. //
  427. // Main parsing function for an EXIF segment.
  428. //
  429. // PARAM: 'buf' start of the EXIF TIFF, which must be the bytes "Exif\0\0".
  430. // PARAM: 'len' length of buffer
  431. //
  432. int easyexif::EXIFInfo::parseFromEXIFSegment(const unsigned char *buf,
  433. unsigned len) {
  434. bool alignIntel = true; // byte alignment (defined in EXIF header)
  435. unsigned offs = 0; // current offset into buffer
  436. if (!buf || len < 6) return PARSE_EXIF_ERROR_NO_EXIF;
  437. if (!std::equal(buf, buf + 6, "Exif\0\0")) return PARSE_EXIF_ERROR_NO_EXIF;
  438. offs += 6;
  439. // Now parsing the TIFF header. The first two bytes are either "II" or
  440. // "MM" for Intel or Motorola byte alignment. Sanity check by parsing
  441. // the unsigned short that follows, making sure it equals 0x2a. The
  442. // last 4 bytes are an offset into the first IFD, which are added to
  443. // the global offset counter. For this block, we expect the following
  444. // minimum size:
  445. // 2 bytes: 'II' or 'MM'
  446. // 2 bytes: 0x002a
  447. // 4 bytes: offset to first IDF
  448. // -----------------------------
  449. // 8 bytes
  450. if (offs + 8 > len) return PARSE_EXIF_ERROR_CORRUPT;
  451. unsigned tiff_header_start = offs;
  452. if (buf[offs] == 'I' && buf[offs + 1] == 'I')
  453. alignIntel = true;
  454. else {
  455. if (buf[offs] == 'M' && buf[offs + 1] == 'M')
  456. alignIntel = false;
  457. else
  458. return PARSE_EXIF_ERROR_UNKNOWN_BYTEALIGN;
  459. }
  460. this->ByteAlign = alignIntel;
  461. offs += 2;
  462. if (0x2a != parse_value<uint16_t>(buf + offs, alignIntel))
  463. return PARSE_EXIF_ERROR_CORRUPT;
  464. offs += 2;
  465. unsigned first_ifd_offset = parse_value<uint32_t>(buf + offs, alignIntel);
  466. offs += first_ifd_offset - 4;
  467. if (offs >= len) return PARSE_EXIF_ERROR_CORRUPT;
  468. // Now parsing the first Image File Directory (IFD0, for the main image).
  469. // An IFD consists of a variable number of 12-byte directory entries. The
  470. // first two bytes of the IFD section contain the number of directory
  471. // entries in the section. The last 4 bytes of the IFD contain an offset
  472. // to the next IFD, which means this IFD must contain exactly 6 + 12 * num
  473. // bytes of data.
  474. if (offs + 2 > len) return PARSE_EXIF_ERROR_CORRUPT;
  475. int num_entries = parse_value<uint16_t>(buf + offs, alignIntel);
  476. if (offs + 6 + 12 * num_entries > len) return PARSE_EXIF_ERROR_CORRUPT;
  477. offs += 2;
  478. unsigned exif_sub_ifd_offset = len;
  479. unsigned gps_sub_ifd_offset = len;
  480. while (--num_entries >= 0) {
  481. IFEntry result =
  482. parseIFEntry(buf, offs, alignIntel, tiff_header_start, len);
  483. offs += 12;
  484. switch (result.tag()) {
  485. case 0x102:
  486. // Bits per sample
  487. if (result.format() == 3 && result.val_short().size())
  488. this->BitsPerSample = result.val_short().front();
  489. break;
  490. case 0x10E:
  491. // Image description
  492. if (result.format() == 2) this->ImageDescription = result.val_string();
  493. break;
  494. case 0x10F:
  495. // Digicam make
  496. if (result.format() == 2) this->Make = result.val_string();
  497. break;
  498. case 0x110:
  499. // Digicam model
  500. if (result.format() == 2) this->Model = result.val_string();
  501. break;
  502. case 0x112:
  503. // Orientation of image
  504. if (result.format() == 3 && result.val_short().size())
  505. this->Orientation = result.val_short().front();
  506. break;
  507. case 0x131:
  508. // Software used for image
  509. if (result.format() == 2) this->Software = result.val_string();
  510. break;
  511. case 0x132:
  512. // EXIF/TIFF date/time of image modification
  513. if (result.format() == 2) this->DateTime = result.val_string();
  514. break;
  515. case 0x8298:
  516. // Copyright information
  517. if (result.format() == 2) this->Copyright = result.val_string();
  518. break;
  519. case 0x8825:
  520. // GPS IFS offset
  521. gps_sub_ifd_offset = tiff_header_start + result.data();
  522. break;
  523. case 0x8769:
  524. // EXIF SubIFD offset
  525. exif_sub_ifd_offset = tiff_header_start + result.data();
  526. break;
  527. }
  528. }
  529. // Jump to the EXIF SubIFD if it exists and parse all the information
  530. // there. Note that it's possible that the EXIF SubIFD doesn't exist.
  531. // The EXIF SubIFD contains most of the interesting information that a
  532. // typical user might want.
  533. if (exif_sub_ifd_offset + 4 <= len) {
  534. offs = exif_sub_ifd_offset;
  535. int num_entries = parse_value<uint16_t>(buf + offs, alignIntel);
  536. if (offs + 6 + 12 * num_entries > len) return PARSE_EXIF_ERROR_CORRUPT;
  537. offs += 2;
  538. while (--num_entries >= 0) {
  539. IFEntry result =
  540. parseIFEntry(buf, offs, alignIntel, tiff_header_start, len);
  541. switch (result.tag()) {
  542. case 0x829a:
  543. // Exposure time in seconds
  544. if (result.format() == 5 && result.val_rational().size())
  545. this->ExposureTime = result.val_rational().front();
  546. break;
  547. case 0x829d:
  548. // FNumber
  549. if (result.format() == 5 && result.val_rational().size())
  550. this->FNumber = result.val_rational().front();
  551. break;
  552. case 0x8822:
  553. // Exposure Program
  554. if (result.format() == 3 && result.val_short().size())
  555. this->ExposureProgram = result.val_short().front();
  556. break;
  557. case 0x8827:
  558. // ISO Speed Rating
  559. if (result.format() == 3 && result.val_short().size())
  560. this->ISOSpeedRatings = result.val_short().front();
  561. break;
  562. case 0x9003:
  563. // Original date and time
  564. if (result.format() == 2)
  565. this->DateTimeOriginal = result.val_string();
  566. break;
  567. case 0x9004:
  568. // Digitization date and time
  569. if (result.format() == 2)
  570. this->DateTimeDigitized = result.val_string();
  571. break;
  572. case 0x9201:
  573. // Shutter speed value
  574. if (result.format() == 5 && result.val_rational().size())
  575. this->ShutterSpeedValue = result.val_rational().front();
  576. break;
  577. case 0x9204:
  578. // Exposure bias value
  579. if (result.format() == 5 && result.val_rational().size())
  580. this->ExposureBiasValue = result.val_rational().front();
  581. break;
  582. case 0x9206:
  583. // Subject distance
  584. if (result.format() == 5 && result.val_rational().size())
  585. this->SubjectDistance = result.val_rational().front();
  586. break;
  587. case 0x9209:
  588. // Flash used
  589. if (result.format() == 3 && result.val_short().size()) {
  590. uint16_t data = result.val_short().front();
  591. this->Flash = data & 1;
  592. this->FlashReturnedLight = (data & 6) >> 1;
  593. this->FlashMode = (data & 24) >> 3;
  594. }
  595. break;
  596. case 0x920a:
  597. // Focal length
  598. if (result.format() == 5 && result.val_rational().size())
  599. this->FocalLength = result.val_rational().front();
  600. break;
  601. case 0x9207:
  602. // Metering mode
  603. if (result.format() == 3 && result.val_short().size())
  604. this->MeteringMode = result.val_short().front();
  605. break;
  606. case 0x9291:
  607. // Subsecond original time
  608. if (result.format() == 2)
  609. this->SubSecTimeOriginal = result.val_string();
  610. break;
  611. case 0xa002:
  612. // EXIF Image width
  613. if (result.format() == 4 && result.val_long().size())
  614. this->ImageWidth = result.val_long().front();
  615. if (result.format() == 3 && result.val_short().size())
  616. this->ImageWidth = result.val_short().front();
  617. break;
  618. case 0xa003:
  619. // EXIF Image height
  620. if (result.format() == 4 && result.val_long().size())
  621. this->ImageHeight = result.val_long().front();
  622. if (result.format() == 3 && result.val_short().size())
  623. this->ImageHeight = result.val_short().front();
  624. break;
  625. case 0xa20e:
  626. // EXIF Focal plane X-resolution
  627. if (result.format() == 5) {
  628. this->LensInfo.FocalPlaneXResolution = result.val_rational()[0];
  629. }
  630. break;
  631. case 0xa20f:
  632. // EXIF Focal plane Y-resolution
  633. if (result.format() == 5) {
  634. this->LensInfo.FocalPlaneYResolution = result.val_rational()[0];
  635. }
  636. break;
  637. case 0xa210:
  638. // EXIF Focal plane resolution unit
  639. if (result.format() == 3 && result.val_short().size()) {
  640. this->LensInfo.FocalPlaneResolutionUnit = result.val_short().front();
  641. }
  642. break;
  643. case 0xa405:
  644. // Focal length in 35mm film
  645. if (result.format() == 3 && result.val_short().size())
  646. this->FocalLengthIn35mm = result.val_short().front();
  647. break;
  648. case 0xa432:
  649. // Focal length and FStop.
  650. if (result.format() == 5) {
  651. int sz = static_cast<unsigned>(result.val_rational().size());
  652. if (sz)
  653. this->LensInfo.FocalLengthMin = result.val_rational()[0];
  654. if (sz > 1)
  655. this->LensInfo.FocalLengthMax = result.val_rational()[1];
  656. if (sz > 2)
  657. this->LensInfo.FStopMin = result.val_rational()[2];
  658. if (sz > 3)
  659. this->LensInfo.FStopMax = result.val_rational()[3];
  660. }
  661. break;
  662. case 0xa433:
  663. // Lens make.
  664. if (result.format() == 2) {
  665. this->LensInfo.Make = result.val_string();
  666. }
  667. break;
  668. case 0xa434:
  669. // Lens model.
  670. if (result.format() == 2) {
  671. this->LensInfo.Model = result.val_string();
  672. }
  673. break;
  674. }
  675. offs += 12;
  676. }
  677. }
  678. // Jump to the GPS SubIFD if it exists and parse all the information
  679. // there. Note that it's possible that the GPS SubIFD doesn't exist.
  680. if (gps_sub_ifd_offset + 4 <= len) {
  681. offs = gps_sub_ifd_offset;
  682. int num_entries = parse_value<uint16_t>(buf + offs, alignIntel);
  683. if (offs + 6 + 12 * num_entries > len) return PARSE_EXIF_ERROR_CORRUPT;
  684. offs += 2;
  685. while (--num_entries >= 0) {
  686. unsigned short tag, format;
  687. unsigned length, data;
  688. parseIFEntryHeader(buf + offs, alignIntel, tag, format, length, data);
  689. switch (tag) {
  690. case 1:
  691. // GPS north or south
  692. this->GeoLocation.LatComponents.direction = *(buf + offs + 8);
  693. if (this->GeoLocation.LatComponents.direction == 0) {
  694. this->GeoLocation.LatComponents.direction = '?';
  695. }
  696. if ('S' == this->GeoLocation.LatComponents.direction) {
  697. this->GeoLocation.Latitude = -this->GeoLocation.Latitude;
  698. }
  699. break;
  700. case 2:
  701. // GPS latitude
  702. if ((format == 5 || format == 10) && length == 3) {
  703. this->GeoLocation.LatComponents.degrees = parse_value<Rational>(
  704. buf + data + tiff_header_start, alignIntel);
  705. this->GeoLocation.LatComponents.minutes = parse_value<Rational>(
  706. buf + data + tiff_header_start + 8, alignIntel);
  707. this->GeoLocation.LatComponents.seconds = parse_value<Rational>(
  708. buf + data + tiff_header_start + 16, alignIntel);
  709. this->GeoLocation.Latitude =
  710. this->GeoLocation.LatComponents.degrees +
  711. this->GeoLocation.LatComponents.minutes / 60 +
  712. this->GeoLocation.LatComponents.seconds / 3600;
  713. if ('S' == this->GeoLocation.LatComponents.direction) {
  714. this->GeoLocation.Latitude = -this->GeoLocation.Latitude;
  715. }
  716. }
  717. break;
  718. case 3:
  719. // GPS east or west
  720. this->GeoLocation.LonComponents.direction = *(buf + offs + 8);
  721. if (this->GeoLocation.LonComponents.direction == 0) {
  722. this->GeoLocation.LonComponents.direction = '?';
  723. }
  724. if ('W' == this->GeoLocation.LonComponents.direction) {
  725. this->GeoLocation.Longitude = -this->GeoLocation.Longitude;
  726. }
  727. break;
  728. case 4:
  729. // GPS longitude
  730. if ((format == 5 || format == 10) && length == 3) {
  731. this->GeoLocation.LonComponents.degrees = parse_value<Rational>(
  732. buf + data + tiff_header_start, alignIntel);
  733. this->GeoLocation.LonComponents.minutes = parse_value<Rational>(
  734. buf + data + tiff_header_start + 8, alignIntel);
  735. this->GeoLocation.LonComponents.seconds = parse_value<Rational>(
  736. buf + data + tiff_header_start + 16, alignIntel);
  737. this->GeoLocation.Longitude =
  738. this->GeoLocation.LonComponents.degrees +
  739. this->GeoLocation.LonComponents.minutes / 60 +
  740. this->GeoLocation.LonComponents.seconds / 3600;
  741. if ('W' == this->GeoLocation.LonComponents.direction)
  742. this->GeoLocation.Longitude = -this->GeoLocation.Longitude;
  743. }
  744. break;
  745. case 5:
  746. // GPS altitude reference (below or above sea level)
  747. this->GeoLocation.AltitudeRef = *(buf + offs + 8);
  748. if (1 == this->GeoLocation.AltitudeRef) {
  749. this->GeoLocation.Altitude = -this->GeoLocation.Altitude;
  750. }
  751. break;
  752. case 6:
  753. // GPS altitude
  754. if ((format == 5 || format == 10)) {
  755. this->GeoLocation.Altitude = parse_value<Rational>(
  756. buf + data + tiff_header_start, alignIntel);
  757. if (1 == this->GeoLocation.AltitudeRef) {
  758. this->GeoLocation.Altitude = -this->GeoLocation.Altitude;
  759. }
  760. }
  761. break;
  762. case 11:
  763. // GPS degree of precision (DOP)
  764. if ((format == 5 || format == 10)) {
  765. this->GeoLocation.DOP = parse_value<Rational>(
  766. buf + data + tiff_header_start, alignIntel);
  767. }
  768. break;
  769. }
  770. offs += 12;
  771. }
  772. }
  773. return PARSE_EXIF_SUCCESS;
  774. }
  775. void easyexif::EXIFInfo::clear() {
  776. // Strings
  777. ImageDescription = "";
  778. Make = "";
  779. Model = "";
  780. Software = "";
  781. DateTime = "";
  782. DateTimeOriginal = "";
  783. DateTimeDigitized = "";
  784. SubSecTimeOriginal = "";
  785. Copyright = "";
  786. // Shorts / unsigned / double
  787. ByteAlign = 0;
  788. Orientation = 0;
  789. BitsPerSample = 0;
  790. ExposureTime = 0;
  791. FNumber = 0;
  792. ExposureProgram = 0;
  793. ISOSpeedRatings = 0;
  794. ShutterSpeedValue = 0;
  795. ExposureBiasValue = 0;
  796. SubjectDistance = 0;
  797. FocalLength = 0;
  798. FocalLengthIn35mm = 0;
  799. Flash = 0;
  800. FlashReturnedLight = 0;
  801. FlashMode = 0;
  802. MeteringMode = 0;
  803. ImageWidth = 0;
  804. ImageHeight = 0;
  805. // Geolocation
  806. GeoLocation.Latitude = 0;
  807. GeoLocation.Longitude = 0;
  808. GeoLocation.Altitude = 0;
  809. GeoLocation.AltitudeRef = 0;
  810. GeoLocation.DOP = 0;
  811. GeoLocation.LatComponents.degrees = 0;
  812. GeoLocation.LatComponents.minutes = 0;
  813. GeoLocation.LatComponents.seconds = 0;
  814. GeoLocation.LatComponents.direction = '?';
  815. GeoLocation.LonComponents.degrees = 0;
  816. GeoLocation.LonComponents.minutes = 0;
  817. GeoLocation.LonComponents.seconds = 0;
  818. GeoLocation.LonComponents.direction = '?';
  819. // LensInfo
  820. LensInfo.FocalLengthMax = 0;
  821. LensInfo.FocalLengthMin = 0;
  822. LensInfo.FStopMax = 0;
  823. LensInfo.FStopMin = 0;
  824. LensInfo.FocalPlaneYResolution = 0;
  825. LensInfo.FocalPlaneXResolution = 0;
  826. LensInfo.FocalPlaneResolutionUnit = 0;
  827. LensInfo.Make = "";
  828. LensInfo.Model = "";
  829. }