8 #include <exiv2/image.hpp>
9 #include <exiv2/exif.hpp>
12 // EXIV2_TEST_VERSION is defined in Exiv2 0.15 and newer.
13 #ifndef EXIV2_TEST_VERSION
14 # define EXIV2_TEST_VERSION(major,minor,patch) \
15 ( EXIV2_VERSION >= EXIV2_MAKE_VERSION(major,minor,patch) )
19 #include <sys/types.h>
25 #include <exiv2/tiffparser.hpp>
26 #include <exiv2/tiffcomposite.hpp>
27 #include <exiv2/tiffvisitor.hpp>
28 #include <exiv2/tiffimage.hpp>
29 #include <exiv2/cr2image.hpp>
30 #include <exiv2/crwimage.hpp>
31 #if EXIV2_TEST_VERSION(0,16,0)
32 #include <exiv2/orfimage.hpp>
34 #if EXIV2_TEST_VERSION(0,13,0)
35 #include <exiv2/rafimage.hpp>
37 #include <exiv2/futils.hpp>
50 Exiv2::Image::AutoPtr image;
51 Exiv2::Image::AutoPtr sidecar;
52 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
53 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
54 #if EXIV2_TEST_VERSION(0,16,0)
55 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
60 _ExifData(gchar *path, gchar *sidecar_path, gint parse_color_profile)
63 image = Exiv2::ImageFactory::open(path);
64 // g_assert (image.get() != 0);
65 image->readMetadata();
67 #if EXIV2_TEST_VERSION(0,16,0)
68 printf("xmp count %d\n", image->xmpData().count());
69 if (sidecar_path && image->xmpData().empty())
71 sidecar = Exiv2::ImageFactory::open(sidecar_path);
72 sidecar->readMetadata();
73 have_sidecar = sidecar->good();
74 printf("sidecar xmp count %d\n", sidecar->xmpData().count());
82 if (have_sidecar) sidecar->writeMetadata();
83 image->writeMetadata();
86 Exiv2::ExifData &exifData ()
88 return image->exifData();
91 Exiv2::IptcData &iptcData ()
93 return image->iptcData();
96 #if EXIV2_TEST_VERSION(0,16,0)
97 Exiv2::XmpData &xmpData ()
99 return have_sidecar ? sidecar->xmpData() : image->xmpData();
107 ExifData *exif_read(gchar *path, gchar *sidecar_path, gint parse_color_profile)
109 printf("exif %s %s\n", path, sidecar_path ? sidecar_path : "-");
111 return new ExifData(path, sidecar_path, parse_color_profile);
113 catch (Exiv2::AnyError& e) {
114 std::cout << "Caught Exiv2 exception '" << e << "'\n";
120 int exif_write(ExifData *exif)
123 exif->writeMetadata();
126 catch (Exiv2::AnyError& e) {
127 std::cout << "Caught Exiv2 exception '" << e << "'\n";
134 void exif_free(ExifData *exif)
140 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
143 Exiv2::Metadatum *item;
145 Exiv2::ExifKey ekey(key);
146 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
147 if (pos == exif->exifData().end()) return NULL;
150 catch (Exiv2::AnyError& e) {
152 Exiv2::IptcKey ekey(key);
153 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
154 if (pos == exif->iptcData().end()) return NULL;
157 catch (Exiv2::AnyError& e) {
158 #if EXIV2_TEST_VERSION(0,16,0)
159 Exiv2::XmpKey ekey(key);
160 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
161 if (pos == exif->xmpData().end()) return NULL;
166 return (ExifItem *)item;
168 catch (Exiv2::AnyError& e) {
169 std::cout << "Caught Exiv2 exception '" << e << "'\n";
174 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
177 Exiv2::Metadatum *item;
179 Exiv2::ExifKey ekey(key);
180 exif->exifData().add(ekey, NULL);
181 Exiv2::ExifData::iterator pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
185 catch (Exiv2::AnyError& e) {
187 Exiv2::IptcKey ekey(key);
188 exif->iptcData().add(ekey, NULL);
189 Exiv2::IptcData::iterator pos = exif->iptcData().end();
193 catch (Exiv2::AnyError& e) {
194 #if EXIV2_TEST_VERSION(0,16,0)
195 Exiv2::XmpKey ekey(key);
196 exif->xmpData().add(ekey, NULL);
197 Exiv2::XmpData::iterator pos = exif->xmpData().end();
203 return (ExifItem *)item;
205 catch (Exiv2::AnyError& e) {
206 std::cout << "Caught Exiv2 exception '" << e << "'\n";
212 ExifItem *exif_get_first_item(ExifData *exif)
215 exif->exifIter = exif->exifData().begin();
216 exif->iptcIter = exif->iptcData().begin();
217 #if EXIV2_TEST_VERSION(0,16,0)
218 exif->xmpIter = exif->xmpData().begin();
220 if (exif->exifIter != exif->exifData().end())
222 const Exiv2::Metadatum *item = &*exif->exifIter;
224 return (ExifItem *)item;
226 if (exif->iptcIter != exif->iptcData().end())
228 const Exiv2::Metadatum *item = &*exif->iptcIter;
230 return (ExifItem *)item;
232 #if EXIV2_TEST_VERSION(0,16,0)
233 if (exif->xmpIter != exif->xmpData().end())
235 const Exiv2::Metadatum *item = &*exif->xmpIter;
237 return (ExifItem *)item;
243 catch (Exiv2::AnyError& e) {
244 std::cout << "Caught Exiv2 exception '" << e << "'\n";
249 ExifItem *exif_get_next_item(ExifData *exif)
252 if (exif->exifIter != exif->exifData().end())
254 const Exiv2::Metadatum *item = &*exif->exifIter;
256 return (ExifItem *)item;
258 if (exif->iptcIter != exif->iptcData().end())
260 const Exiv2::Metadatum *item = &*exif->iptcIter;
262 return (ExifItem *)item;
264 #if EXIV2_TEST_VERSION(0,16,0)
265 if (exif->xmpIter != exif->xmpData().end())
267 const Exiv2::Metadatum *item = &*exif->xmpIter;
269 return (ExifItem *)item;
274 catch (Exiv2::AnyError& e) {
275 std::cout << "Caught Exiv2 exception '" << e << "'\n";
280 char *exif_item_get_tag_name(ExifItem *item)
283 if (!item) return NULL;
284 return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
286 catch (Exiv2::AnyError& e) {
287 std::cout << "Caught Exiv2 exception '" << e << "'\n";
292 guint exif_item_get_tag_id(ExifItem *item)
296 return ((Exiv2::Metadatum *)item)->tag();
298 catch (Exiv2::AnyError& e) {
299 std::cout << "Caught Exiv2 exception '" << e << "'\n";
304 guint exif_item_get_elements(ExifItem *item)
308 return ((Exiv2::Metadatum *)item)->count();
310 catch (Exiv2::AnyError& e) {
311 std::cout << "Caught Exiv2 exception '" << e << "'\n";
316 char *exif_item_get_data(ExifItem *item, guint *data_len)
320 char *exif_item_get_description(ExifItem *item)
323 if (!item) return NULL;
324 return g_strdup(((Exiv2::Metadatum *)item)->tagLabel().c_str());
326 catch (std::exception& e) {
327 // std::cout << "Caught Exiv2 exception '" << e << "'\n";
333 invalidTypeId, unsignedByte, asciiString, unsignedShort,
334 unsignedLong, unsignedRational, signedByte, undefined,
335 signedShort, signedLong, signedRational, string,
336 date, time, comment, directory,
337 xmpText, xmpAlt, xmpBag, xmpSeq,
341 static guint format_id_trans_tbl [] = {
343 EXIF_FORMAT_BYTE_UNSIGNED,
345 EXIF_FORMAT_SHORT_UNSIGNED,
346 EXIF_FORMAT_LONG_UNSIGNED,
347 EXIF_FORMAT_RATIONAL_UNSIGNED,
349 EXIF_FORMAT_UNDEFINED,
352 EXIF_FORMAT_RATIONAL,
356 EXIF_FORMAT_UNDEFINED,
365 guint exif_item_get_format_id(ExifItem *item)
368 if (!item) return EXIF_FORMAT_UNKNOWN;
369 guint id = ((Exiv2::Metadatum *)item)->typeId();
370 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
371 return format_id_trans_tbl[id];
373 catch (Exiv2::AnyError& e) {
374 std::cout << "Caught Exiv2 exception '" << e << "'\n";
375 return EXIF_FORMAT_UNKNOWN;
379 const char *exif_item_get_format_name(ExifItem *item, gint brief)
382 if (!item) return NULL;
383 return ((Exiv2::Metadatum *)item)->typeName();
385 catch (Exiv2::AnyError& e) {
386 std::cout << "Caught Exiv2 exception '" << e << "'\n";
392 gchar *exif_item_get_data_as_text(ExifItem *item)
395 if (!item) return NULL;
396 // std::stringstream str; // does not work with Exiv2::Metadatum because operator<< is not virtual
397 // str << *((Exiv2::Metadatum *)item);
398 // return g_strdup(str.str().c_str());
399 return g_strdup(((Exiv2::Metadatum *)item)->toString().c_str());
401 catch (Exiv2::AnyError& e) {
406 gchar *exif_item_get_string(ExifItem *item, int idx)
409 if (!item) return NULL;
410 Exiv2::Metadatum *em = (Exiv2::Metadatum *)item;
411 #if EXIV2_TEST_VERSION(0,16,0)
412 std::string str = em->toString(idx);
414 std::string str = em->toString(); // FIXME
416 if (idx == 0 && str == "") str = em->toString();
417 if (str.length() > 5 && str.substr(0, 5) == "lang=")
419 std::string::size_type pos = str.find_first_of(' ');
420 if (pos != std::string::npos) str = str.substr(pos+1);
423 return g_strdup(str.c_str());
425 catch (Exiv2::AnyError& e) {
431 gint exif_item_get_integer(ExifItem *item, gint *value)
435 *value = ((Exiv2::Metadatum *)item)->toLong();
438 catch (Exiv2::AnyError& e) {
439 std::cout << "Caught Exiv2 exception '" << e << "'\n";
444 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign)
447 if (!item) return NULL;
448 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational();
449 static ExifRational ret;
454 catch (Exiv2::AnyError& e) {
455 std::cout << "Caught Exiv2 exception '" << e << "'\n";
460 const gchar *exif_get_tag_description_by_key(const gchar *key)
463 Exiv2::ExifKey ekey(key);
464 return Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ());
466 catch (Exiv2::AnyError& e) {
467 std::cout << "Caught Exiv2 exception '" << e << "'\n";
472 int exif_item_set_string(ExifItem *item, const char *str)
476 ((Exiv2::Metadatum *)item)->setValue(std::string(str));
479 catch (Exiv2::AnyError& e) {
484 int exif_item_delete(ExifData *exif, ExifItem *item)
488 for (Exiv2::ExifData::iterator i = exif->exifData().begin(); i != exif->exifData().end(); ++i) {
489 if (((Exiv2::Metadatum *)item) == &*i) {
490 i = exif->exifData().erase(i);
494 for (Exiv2::IptcData::iterator i = exif->iptcData().begin(); i != exif->iptcData().end(); ++i) {
495 if (((Exiv2::Metadatum *)item) == &*i) {
496 i = exif->iptcData().erase(i);
500 #if EXIV2_TEST_VERSION(0,16,0)
501 for (Exiv2::XmpData::iterator i = exif->xmpData().begin(); i != exif->xmpData().end(); ++i) {
502 if (((Exiv2::Metadatum *)item) == &*i) {
503 i = exif->xmpData().erase(i);
510 catch (Exiv2::AnyError& e) {
519 /* This is a dirty hack to support raw file preview, bassed on
520 tiffparse.cpp from Exiv2 examples */
528 const Exiv2::Value *find(uint16_t tag, uint16_t group);
530 unsigned long preview_offset();
534 Exiv2::TiffComponent::AutoPtr rootDir;
535 Exiv2::byte *map_data;
537 unsigned long offset;
540 using namespace Exiv2;
542 RawFile::RawFile(int fd) : map_data(NULL), map_len(0), offset(0)
545 if (fstat(fd, &st) == -1)
549 map_len = st.st_size;
550 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
551 if (map_data == MAP_FAILED)
555 type = Exiv2::ImageFactory::getType(map_data, map_len);
557 #if EXIV2_TEST_VERSION(0,16,0)
558 TiffHeaderBase *tiffHeader = NULL;
560 TiffHeade2 *tiffHeader = NULL;
562 Cr2Header *cr2Header = NULL;
565 case Exiv2::ImageType::tiff:
566 tiffHeader = new TiffHeade2();
568 case Exiv2::ImageType::cr2:
569 cr2Header = new Cr2Header();
571 #if EXIV2_TEST_VERSION(0,16,0)
572 case Exiv2::ImageType::orf:
573 tiffHeader = new OrfHeader();
576 #if EXIV2_TEST_VERSION(0,13,0)
577 case Exiv2::ImageType::raf:
578 if (map_len < 84 + 4) throw Error(14);
579 offset = getULong(map_data + 84, bigEndian);
582 case Exiv2::ImageType::crw:
584 // Parse the image, starting with a CIFF header component
585 Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
586 parseTree->read(map_data, map_len);
587 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
588 if (entry) offset = entry->pData() - map_data;
593 throw Error(3, "RAW");
596 // process tiff-like formats
598 TiffCompFactoryFct createFct = TiffCreator::create;
600 rootDir = createFct(Tag::root, Group::none);
601 if (0 == rootDir.get()) {
602 throw Error(1, "No root element defined in TIFF structure");
607 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
608 #if EXIV2_TEST_VERSION(0,16,0)
609 rootDir->setStart(map_data + tiffHeader->offset());
611 rootDir->setStart(map_data + tiffHeader->ifdOffset());
617 rootDir->setStart(map_data + cr2Header->offset());
620 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
622 TiffReader reader(map_data,
627 rootDir->accept(reader);
637 if (map_data && munmap(map_data, map_len) == -1)
639 printf("Failed to unmap file \n");
643 const Value * RawFile::find(uint16_t tag, uint16_t group)
645 TiffFinder finder(tag, group);
646 rootDir->accept(finder);
647 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
650 if (debug) printf("(tag: %04x %04x) ", tag, group);
657 unsigned long RawFile::preview_offset()
660 if (offset) return offset;
662 if (type == Exiv2::ImageType::cr2)
664 val = find(0x111, Group::ifd0);
665 if (val) return val->toLong();
670 val = find(0x201, Group::sub0_0);
671 if (val) return val->toLong();
673 val = find(0x201, Group::ifd0);
674 if (val) return val->toLong();
676 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
677 if (val) return val->toLong();
679 val = find(0x111, Group::sub0_1); // dng
680 if (val) return val->toLong();
686 const static char *raw_ext_list[] = { ".cr2", ".nef", ".pef", ".arw", NULL };
688 extern "C" gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
689 unsigned char *header_data, const guint header_len,
690 guint *image_offset, guint *exif_offset)
693 unsigned long offset;
695 /* given image pathname, first do simple (and fast) file extension test */
702 ext = strrchr(path, '.');
703 if (!ext) return FALSE;
705 for (i = 0; raw_ext_list[i]; i++)
707 if (strcasecmp(raw_ext_list[i], ext) == 0)
714 if (!match) return FALSE;
720 if (debug) printf("%s: offset ", path);
721 offset = rf.preview_offset();
722 if (debug) printf("%d\n", offset);
724 catch (Exiv2::AnyError& e) {
725 std::cout << "Caught Exiv2 exception '" << e << "'\n";
729 if (image_offset && offset > 0)
731 *image_offset = offset;
732 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
734 printf("Failed to seek to embedded image\n");
737 if (*exif_offset) *exif_offset = 0;