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>
51 Exiv2::Image::AutoPtr image;
52 Exiv2::Image::AutoPtr sidecar;
53 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
54 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
55 #if EXIV2_TEST_VERSION(0,16,0)
56 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
61 _ExifData(gchar *path, gchar *sidecar_path, gint parse_color_profile)
64 image = Exiv2::ImageFactory::open(path);
65 // g_assert (image.get() != 0);
66 image->readMetadata();
68 #if EXIV2_TEST_VERSION(0,16,0)
69 printf("xmp count %d\n", image->xmpData().count());
70 if (sidecar_path && image->xmpData().empty())
72 sidecar = Exiv2::ImageFactory::open(sidecar_path);
73 sidecar->readMetadata();
74 have_sidecar = sidecar->good();
75 printf("sidecar xmp count %d\n", sidecar->xmpData().count());
83 if (have_sidecar) sidecar->writeMetadata();
84 image->writeMetadata();
87 Exiv2::ExifData &exifData ()
89 return image->exifData();
92 Exiv2::IptcData &iptcData ()
94 return image->iptcData();
97 #if EXIV2_TEST_VERSION(0,16,0)
98 Exiv2::XmpData &xmpData ()
100 return have_sidecar ? sidecar->xmpData() : image->xmpData();
108 ExifData *exif_read(gchar *path, gchar *sidecar_path, gint parse_color_profile)
110 if (debug) printf("exif read %s, sidecar: %s\n", path, sidecar_path ? sidecar_path : "-");
112 return new ExifData(path, sidecar_path, parse_color_profile);
114 catch (Exiv2::AnyError& e) {
115 std::cout << "Caught Exiv2 exception '" << e << "'\n";
121 int exif_write(ExifData *exif)
124 exif->writeMetadata();
127 catch (Exiv2::AnyError& e) {
128 std::cout << "Caught Exiv2 exception '" << e << "'\n";
135 void exif_free(ExifData *exif)
141 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
144 Exiv2::Metadatum *item;
146 Exiv2::ExifKey ekey(key);
147 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
148 if (pos == exif->exifData().end()) return NULL;
151 catch (Exiv2::AnyError& e) {
153 Exiv2::IptcKey ekey(key);
154 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
155 if (pos == exif->iptcData().end()) return NULL;
158 catch (Exiv2::AnyError& e) {
159 #if EXIV2_TEST_VERSION(0,16,0)
160 Exiv2::XmpKey ekey(key);
161 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
162 if (pos == exif->xmpData().end()) return NULL;
167 return (ExifItem *)item;
169 catch (Exiv2::AnyError& e) {
170 std::cout << "Caught Exiv2 exception '" << e << "'\n";
175 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
178 Exiv2::Metadatum *item;
180 Exiv2::ExifKey ekey(key);
181 exif->exifData().add(ekey, NULL);
182 Exiv2::ExifData::iterator pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
186 catch (Exiv2::AnyError& e) {
188 Exiv2::IptcKey ekey(key);
189 exif->iptcData().add(ekey, NULL);
190 Exiv2::IptcData::iterator pos = exif->iptcData().end();
194 catch (Exiv2::AnyError& e) {
195 #if EXIV2_TEST_VERSION(0,16,0)
196 Exiv2::XmpKey ekey(key);
197 exif->xmpData().add(ekey, NULL);
198 Exiv2::XmpData::iterator pos = exif->xmpData().end();
204 return (ExifItem *)item;
206 catch (Exiv2::AnyError& e) {
207 std::cout << "Caught Exiv2 exception '" << e << "'\n";
213 ExifItem *exif_get_first_item(ExifData *exif)
216 exif->exifIter = exif->exifData().begin();
217 exif->iptcIter = exif->iptcData().begin();
218 #if EXIV2_TEST_VERSION(0,16,0)
219 exif->xmpIter = exif->xmpData().begin();
221 if (exif->exifIter != exif->exifData().end())
223 const Exiv2::Metadatum *item = &*exif->exifIter;
225 return (ExifItem *)item;
227 if (exif->iptcIter != exif->iptcData().end())
229 const Exiv2::Metadatum *item = &*exif->iptcIter;
231 return (ExifItem *)item;
233 #if EXIV2_TEST_VERSION(0,16,0)
234 if (exif->xmpIter != exif->xmpData().end())
236 const Exiv2::Metadatum *item = &*exif->xmpIter;
238 return (ExifItem *)item;
244 catch (Exiv2::AnyError& e) {
245 std::cout << "Caught Exiv2 exception '" << e << "'\n";
250 ExifItem *exif_get_next_item(ExifData *exif)
253 if (exif->exifIter != exif->exifData().end())
255 const Exiv2::Metadatum *item = &*exif->exifIter;
257 return (ExifItem *)item;
259 if (exif->iptcIter != exif->iptcData().end())
261 const Exiv2::Metadatum *item = &*exif->iptcIter;
263 return (ExifItem *)item;
265 #if EXIV2_TEST_VERSION(0,16,0)
266 if (exif->xmpIter != exif->xmpData().end())
268 const Exiv2::Metadatum *item = &*exif->xmpIter;
270 return (ExifItem *)item;
275 catch (Exiv2::AnyError& e) {
276 std::cout << "Caught Exiv2 exception '" << e << "'\n";
281 char *exif_item_get_tag_name(ExifItem *item)
284 if (!item) return NULL;
285 return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
287 catch (Exiv2::AnyError& e) {
288 std::cout << "Caught Exiv2 exception '" << e << "'\n";
293 guint exif_item_get_tag_id(ExifItem *item)
297 return ((Exiv2::Metadatum *)item)->tag();
299 catch (Exiv2::AnyError& e) {
300 std::cout << "Caught Exiv2 exception '" << e << "'\n";
305 guint exif_item_get_elements(ExifItem *item)
309 return ((Exiv2::Metadatum *)item)->count();
311 catch (Exiv2::AnyError& e) {
312 std::cout << "Caught Exiv2 exception '" << e << "'\n";
317 char *exif_item_get_data(ExifItem *item, guint *data_len)
321 char *exif_item_get_description(ExifItem *item)
324 if (!item) return NULL;
325 return g_strdup(((Exiv2::Metadatum *)item)->tagLabel().c_str());
327 catch (std::exception& e) {
328 // std::cout << "Caught Exiv2 exception '" << e << "'\n";
334 invalidTypeId, unsignedByte, asciiString, unsignedShort,
335 unsignedLong, unsignedRational, signedByte, undefined,
336 signedShort, signedLong, signedRational, string,
337 date, time, comment, directory,
338 xmpText, xmpAlt, xmpBag, xmpSeq,
342 static guint format_id_trans_tbl [] = {
344 EXIF_FORMAT_BYTE_UNSIGNED,
346 EXIF_FORMAT_SHORT_UNSIGNED,
347 EXIF_FORMAT_LONG_UNSIGNED,
348 EXIF_FORMAT_RATIONAL_UNSIGNED,
350 EXIF_FORMAT_UNDEFINED,
353 EXIF_FORMAT_RATIONAL,
357 EXIF_FORMAT_UNDEFINED,
366 guint exif_item_get_format_id(ExifItem *item)
369 if (!item) return EXIF_FORMAT_UNKNOWN;
370 guint id = ((Exiv2::Metadatum *)item)->typeId();
371 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
372 return format_id_trans_tbl[id];
374 catch (Exiv2::AnyError& e) {
375 std::cout << "Caught Exiv2 exception '" << e << "'\n";
376 return EXIF_FORMAT_UNKNOWN;
380 const char *exif_item_get_format_name(ExifItem *item, gint brief)
383 if (!item) return NULL;
384 return ((Exiv2::Metadatum *)item)->typeName();
386 catch (Exiv2::AnyError& e) {
387 std::cout << "Caught Exiv2 exception '" << e << "'\n";
393 gchar *exif_item_get_data_as_text(ExifItem *item)
396 if (!item) return NULL;
397 // std::stringstream str; // does not work with Exiv2::Metadatum because operator<< is not virtual
398 // str << *((Exiv2::Metadatum *)item);
399 // return g_strdup(str.str().c_str());
400 return g_strdup(((Exiv2::Metadatum *)item)->toString().c_str());
402 catch (Exiv2::AnyError& e) {
407 gchar *exif_item_get_string(ExifItem *item, int idx)
410 if (!item) return NULL;
411 Exiv2::Metadatum *em = (Exiv2::Metadatum *)item;
412 #if EXIV2_TEST_VERSION(0,16,0)
413 std::string str = em->toString(idx);
415 std::string str = em->toString(); // FIXME
417 if (idx == 0 && str == "") str = em->toString();
418 if (str.length() > 5 && str.substr(0, 5) == "lang=")
420 std::string::size_type pos = str.find_first_of(' ');
421 if (pos != std::string::npos) str = str.substr(pos+1);
424 return g_strdup(str.c_str());
426 catch (Exiv2::AnyError& e) {
432 gint exif_item_get_integer(ExifItem *item, gint *value)
436 *value = ((Exiv2::Metadatum *)item)->toLong();
439 catch (Exiv2::AnyError& e) {
440 std::cout << "Caught Exiv2 exception '" << e << "'\n";
445 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign)
448 if (!item) return NULL;
449 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational();
450 static ExifRational ret;
455 catch (Exiv2::AnyError& e) {
456 std::cout << "Caught Exiv2 exception '" << e << "'\n";
461 const gchar *exif_get_tag_description_by_key(const gchar *key)
464 Exiv2::ExifKey ekey(key);
465 return Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ());
467 catch (Exiv2::AnyError& e) {
468 std::cout << "Caught Exiv2 exception '" << e << "'\n";
473 int exif_item_set_string(ExifItem *item, const char *str)
477 ((Exiv2::Metadatum *)item)->setValue(std::string(str));
480 catch (Exiv2::AnyError& e) {
485 int exif_item_delete(ExifData *exif, ExifItem *item)
489 for (Exiv2::ExifData::iterator i = exif->exifData().begin(); i != exif->exifData().end(); ++i) {
490 if (((Exiv2::Metadatum *)item) == &*i) {
491 i = exif->exifData().erase(i);
495 for (Exiv2::IptcData::iterator i = exif->iptcData().begin(); i != exif->iptcData().end(); ++i) {
496 if (((Exiv2::Metadatum *)item) == &*i) {
497 i = exif->iptcData().erase(i);
501 #if EXIV2_TEST_VERSION(0,16,0)
502 for (Exiv2::XmpData::iterator i = exif->xmpData().begin(); i != exif->xmpData().end(); ++i) {
503 if (((Exiv2::Metadatum *)item) == &*i) {
504 i = exif->xmpData().erase(i);
511 catch (Exiv2::AnyError& e) {
520 /* This is a dirty hack to support raw file preview, bassed on
521 tiffparse.cpp from Exiv2 examples */
529 const Exiv2::Value *find(uint16_t tag, uint16_t group);
531 unsigned long preview_offset();
535 Exiv2::TiffComponent::AutoPtr rootDir;
536 Exiv2::byte *map_data;
538 unsigned long offset;
541 using namespace Exiv2;
543 RawFile::RawFile(int fd) : map_data(NULL), map_len(0), offset(0)
546 if (fstat(fd, &st) == -1)
550 map_len = st.st_size;
551 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
552 if (map_data == MAP_FAILED)
556 type = Exiv2::ImageFactory::getType(map_data, map_len);
558 #if EXIV2_TEST_VERSION(0,16,0)
559 TiffHeaderBase *tiffHeader = NULL;
561 TiffHeade2 *tiffHeader = NULL;
563 Cr2Header *cr2Header = NULL;
566 case Exiv2::ImageType::tiff:
567 tiffHeader = new TiffHeade2();
569 case Exiv2::ImageType::cr2:
570 cr2Header = new Cr2Header();
572 #if EXIV2_TEST_VERSION(0,16,0)
573 case Exiv2::ImageType::orf:
574 tiffHeader = new OrfHeader();
577 #if EXIV2_TEST_VERSION(0,13,0)
578 case Exiv2::ImageType::raf:
579 if (map_len < 84 + 4) throw Error(14);
580 offset = getULong(map_data + 84, bigEndian);
583 case Exiv2::ImageType::crw:
585 // Parse the image, starting with a CIFF header component
586 Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
587 parseTree->read(map_data, map_len);
588 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
589 if (entry) offset = entry->pData() - map_data;
594 throw Error(3, "RAW");
597 // process tiff-like formats
599 TiffCompFactoryFct createFct = TiffCreator::create;
601 rootDir = createFct(Tag::root, Group::none);
602 if (0 == rootDir.get()) {
603 throw Error(1, "No root element defined in TIFF structure");
608 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
609 #if EXIV2_TEST_VERSION(0,16,0)
610 rootDir->setStart(map_data + tiffHeader->offset());
612 rootDir->setStart(map_data + tiffHeader->ifdOffset());
618 rootDir->setStart(map_data + cr2Header->offset());
621 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
623 TiffReader reader(map_data,
628 rootDir->accept(reader);
638 if (map_data && munmap(map_data, map_len) == -1)
640 printf("Failed to unmap file \n");
644 const Value * RawFile::find(uint16_t tag, uint16_t group)
646 TiffFinder finder(tag, group);
647 rootDir->accept(finder);
648 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
651 if (debug) printf("(tag: %04x %04x) ", tag, group);
658 unsigned long RawFile::preview_offset()
661 if (offset) return offset;
663 if (type == Exiv2::ImageType::cr2)
665 val = find(0x111, Group::ifd0);
666 if (val) return val->toLong();
671 val = find(0x201, Group::sub0_0);
672 if (val) return val->toLong();
674 val = find(0x201, Group::ifd0);
675 if (val) return val->toLong();
677 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
678 if (val) return val->toLong();
680 val = find(0x111, Group::sub0_1); // dng
681 if (val) return val->toLong();
687 extern "C" gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
688 unsigned char *header_data, const guint header_len,
689 guint *image_offset, guint *exif_offset)
692 unsigned long offset;
694 /* given image pathname, first do simple (and fast) file extension test */
695 if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return 0;
699 if (debug) printf("%s: offset ", path);
700 offset = rf.preview_offset();
701 if (debug) printf("%d\n", offset);
703 catch (Exiv2::AnyError& e) {
704 std::cout << "Caught Exiv2 exception '" << e << "'\n";
708 if (image_offset && offset > 0)
710 *image_offset = offset;
711 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
713 printf("Failed to seek to embedded image\n");
716 if (*exif_offset) *exif_offset = 0;