3 * Copyright (C) 2008 The Geeqie Team
5 * Author: Vladimir Nadvornik
7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
16 #include <exiv2/image.hpp>
17 #include <exiv2/exif.hpp>
20 // EXIV2_TEST_VERSION is defined in Exiv2 0.15 and newer.
21 #ifndef EXIV2_TEST_VERSION
22 # define EXIV2_TEST_VERSION(major,minor,patch) \
23 ( EXIV2_VERSION >= EXIV2_MAKE_VERSION(major,minor,patch) )
27 #include <sys/types.h>
33 #if !EXIV2_TEST_VERSION(0,17,90)
34 #include <exiv2/tiffparser.hpp>
35 #include <exiv2/tiffcomposite.hpp>
36 #include <exiv2/tiffvisitor.hpp>
37 #include <exiv2/tiffimage.hpp>
38 #include <exiv2/cr2image.hpp>
39 #include <exiv2/crwimage.hpp>
40 #if EXIV2_TEST_VERSION(0,16,0)
41 #include <exiv2/orfimage.hpp>
43 #if EXIV2_TEST_VERSION(0,13,0)
44 #include <exiv2/rafimage.hpp>
46 #include <exiv2/futils.hpp>
48 #include <exiv2/preview.hpp>
58 #include "filefilter.h"
59 #include "ui_fileops.h"
64 Exiv2::Image::AutoPtr image;
65 Exiv2::Image::AutoPtr sidecar;
66 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
67 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
68 #if EXIV2_TEST_VERSION(0,16,0)
69 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
73 /* the icc profile in jpeg is not technically exif - store it here */
74 unsigned char *cp_data;
77 _ExifData(gchar *path, gchar *sidecar_path)
82 gchar *pathl = path_from_utf8(path);
83 image = Exiv2::ImageFactory::open(pathl);
85 // g_assert (image.get() != 0);
86 image->readMetadata();
88 #if EXIV2_TEST_VERSION(0,16,0)
89 DEBUG_2("xmp count %li", image->xmpData().count());
90 if (sidecar_path && image->xmpData().empty())
92 gchar *sidecar_pathl = path_from_utf8(sidecar_path);
93 sidecar = Exiv2::ImageFactory::open(sidecar_pathl);
94 g_free(sidecar_pathl);
95 sidecar->readMetadata();
96 have_sidecar = sidecar->good();
97 DEBUG_2("sidecar xmp count %li", sidecar->xmpData().count());
101 #if EXIV2_TEST_VERSION(0,14,0)
102 if (image->mimeType() == std::string("image/jpeg"))
104 /* try to get jpeg color profile */
105 Exiv2::BasicIo &io = image->io();
106 gint open = io.isopen();
107 if (!open) io.open();
108 unsigned char *mapped = (unsigned char*)io.mmap();
109 if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
111 if (!open) io.close();
118 if (cp_data) g_free(cp_data);
123 if (have_sidecar) sidecar->writeMetadata();
124 image->writeMetadata();
127 Exiv2::ExifData &exifData ()
129 return image->exifData();
132 Exiv2::IptcData &iptcData ()
134 return image->iptcData();
137 #if EXIV2_TEST_VERSION(0,16,0)
138 Exiv2::XmpData &xmpData ()
140 return have_sidecar ? sidecar->xmpData() : image->xmpData();
148 ExifData *exif_read(gchar *path, gchar *sidecar_path)
150 DEBUG_1("exif read %s, sidecar: %s", path, sidecar_path ? sidecar_path : "-");
152 return new ExifData(path, sidecar_path);
154 catch (Exiv2::AnyError& e) {
155 std::cout << "Caught Exiv2 exception '" << e << "'\n";
161 int exif_write(ExifData *exif)
164 exif->writeMetadata();
167 catch (Exiv2::AnyError& e) {
168 std::cout << "Caught Exiv2 exception '" << e << "'\n";
175 void exif_free(ExifData *exif)
181 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
184 Exiv2::Metadatum *item = NULL;
186 Exiv2::ExifKey ekey(key);
187 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
188 if (pos == exif->exifData().end()) return NULL;
191 catch (Exiv2::AnyError& e) {
193 Exiv2::IptcKey ekey(key);
194 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
195 if (pos == exif->iptcData().end()) return NULL;
198 catch (Exiv2::AnyError& e) {
199 #if EXIV2_TEST_VERSION(0,16,0)
200 Exiv2::XmpKey ekey(key);
201 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
202 if (pos == exif->xmpData().end()) return NULL;
207 return (ExifItem *)item;
209 catch (Exiv2::AnyError& e) {
210 std::cout << "Caught Exiv2 exception '" << e << "'\n";
215 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
218 Exiv2::Metadatum *item = NULL;
220 Exiv2::ExifKey ekey(key);
221 exif->exifData().add(ekey, NULL);
222 Exiv2::ExifData::iterator pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
226 catch (Exiv2::AnyError& e) {
228 Exiv2::IptcKey ekey(key);
229 exif->iptcData().add(ekey, NULL);
230 Exiv2::IptcData::iterator pos = exif->iptcData().end();
234 catch (Exiv2::AnyError& e) {
235 #if EXIV2_TEST_VERSION(0,16,0)
236 Exiv2::XmpKey ekey(key);
237 exif->xmpData().add(ekey, NULL);
238 Exiv2::XmpData::iterator pos = exif->xmpData().end();
244 return (ExifItem *)item;
246 catch (Exiv2::AnyError& e) {
247 std::cout << "Caught Exiv2 exception '" << e << "'\n";
253 ExifItem *exif_get_first_item(ExifData *exif)
256 exif->exifIter = exif->exifData().begin();
257 exif->iptcIter = exif->iptcData().begin();
258 #if EXIV2_TEST_VERSION(0,16,0)
259 exif->xmpIter = exif->xmpData().begin();
261 if (exif->exifIter != exif->exifData().end())
263 const Exiv2::Metadatum *item = &*exif->exifIter;
265 return (ExifItem *)item;
267 if (exif->iptcIter != exif->iptcData().end())
269 const Exiv2::Metadatum *item = &*exif->iptcIter;
271 return (ExifItem *)item;
273 #if EXIV2_TEST_VERSION(0,16,0)
274 if (exif->xmpIter != exif->xmpData().end())
276 const Exiv2::Metadatum *item = &*exif->xmpIter;
278 return (ExifItem *)item;
284 catch (Exiv2::AnyError& e) {
285 std::cout << "Caught Exiv2 exception '" << e << "'\n";
290 ExifItem *exif_get_next_item(ExifData *exif)
293 if (exif->exifIter != exif->exifData().end())
295 const Exiv2::Metadatum *item = &*exif->exifIter;
297 return (ExifItem *)item;
299 if (exif->iptcIter != exif->iptcData().end())
301 const Exiv2::Metadatum *item = &*exif->iptcIter;
303 return (ExifItem *)item;
305 #if EXIV2_TEST_VERSION(0,16,0)
306 if (exif->xmpIter != exif->xmpData().end())
308 const Exiv2::Metadatum *item = &*exif->xmpIter;
310 return (ExifItem *)item;
315 catch (Exiv2::AnyError& e) {
316 std::cout << "Caught Exiv2 exception '" << e << "'\n";
321 char *exif_item_get_tag_name(ExifItem *item)
324 if (!item) return NULL;
325 return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
327 catch (Exiv2::AnyError& e) {
328 std::cout << "Caught Exiv2 exception '" << e << "'\n";
333 guint exif_item_get_tag_id(ExifItem *item)
337 return ((Exiv2::Metadatum *)item)->tag();
339 catch (Exiv2::AnyError& e) {
340 std::cout << "Caught Exiv2 exception '" << e << "'\n";
345 guint exif_item_get_elements(ExifItem *item)
349 return ((Exiv2::Metadatum *)item)->count();
351 catch (Exiv2::AnyError& e) {
352 std::cout << "Caught Exiv2 exception '" << e << "'\n";
357 char *exif_item_get_data(ExifItem *item, guint *data_len)
361 Exiv2::Metadatum *md = (Exiv2::Metadatum *)item;
362 if (data_len) *data_len = md->size();
363 char *data = (char *)g_malloc(md->size());
364 long res = md->copy((Exiv2::byte *)data, Exiv2::littleEndian /* should not matter */);
365 g_assert(res == md->size());
368 catch (Exiv2::AnyError& e) {
369 std::cout << "Caught Exiv2 exception '" << e << "'\n";
374 char *exif_item_get_description(ExifItem *item)
377 if (!item) return NULL;
378 return g_locale_to_utf8(((Exiv2::Metadatum *)item)->tagLabel().c_str(), -1, NULL, NULL, NULL);
380 catch (std::exception& e) {
381 // std::cout << "Caught Exiv2 exception '" << e << "'\n";
387 invalidTypeId, unsignedByte, asciiString, unsignedShort,
388 unsignedLong, unsignedRational, signedByte, undefined,
389 signedShort, signedLong, signedRational, string,
390 date, time, comment, directory,
391 xmpText, xmpAlt, xmpBag, xmpSeq,
395 static guint format_id_trans_tbl [] = {
397 EXIF_FORMAT_BYTE_UNSIGNED,
399 EXIF_FORMAT_SHORT_UNSIGNED,
400 EXIF_FORMAT_LONG_UNSIGNED,
401 EXIF_FORMAT_RATIONAL_UNSIGNED,
403 EXIF_FORMAT_UNDEFINED,
406 EXIF_FORMAT_RATIONAL,
410 EXIF_FORMAT_UNDEFINED,
419 guint exif_item_get_format_id(ExifItem *item)
422 if (!item) return EXIF_FORMAT_UNKNOWN;
423 guint id = ((Exiv2::Metadatum *)item)->typeId();
424 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
425 return format_id_trans_tbl[id];
427 catch (Exiv2::AnyError& e) {
428 std::cout << "Caught Exiv2 exception '" << e << "'\n";
429 return EXIF_FORMAT_UNKNOWN;
433 const char *exif_item_get_format_name(ExifItem *item, gint brief)
436 if (!item) return NULL;
437 return ((Exiv2::Metadatum *)item)->typeName();
439 catch (Exiv2::AnyError& e) {
440 std::cout << "Caught Exiv2 exception '" << e << "'\n";
446 gchar *exif_item_get_data_as_text(ExifItem *item)
449 if (!item) return NULL;
450 Exiv2::Metadatum *metadatum = (Exiv2::Metadatum *)item;
451 #if EXIV2_TEST_VERSION(0,17,0)
452 return g_locale_to_utf8(metadatum->print().c_str(), -1, NULL, NULL, NULL);
454 std::stringstream str;
455 Exiv2::Exifdatum *exifdatum;
456 Exiv2::Iptcdatum *iptcdatum;
457 #if EXIV2_TEST_VERSION(0,16,0)
458 Exiv2::Xmpdatum *xmpdatum;
460 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(metadatum)))
462 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(metadatum)))
464 #if EXIV2_TEST_VERSION(0,16,0)
465 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(metadatum)))
469 return g_locale_to_utf8(str.str().c_str(), -1, NULL, NULL, NULL);
472 catch (Exiv2::AnyError& e) {
477 gchar *exif_item_get_string(ExifItem *item, int idx)
480 if (!item) return NULL;
481 Exiv2::Metadatum *em = (Exiv2::Metadatum *)item;
482 #if EXIV2_TEST_VERSION(0,16,0)
483 std::string str = em->toString(idx);
485 std::string str = em->toString(); // FIXME
487 if (idx == 0 && str == "") str = em->toString();
488 if (str.length() > 5 && str.substr(0, 5) == "lang=")
490 std::string::size_type pos = str.find_first_of(' ');
491 if (pos != std::string::npos) str = str.substr(pos+1);
494 // return g_locale_to_utf8(str.c_str(), -1, NULL, NULL, NULL); // FIXME
495 return g_strdup(str.c_str());
497 catch (Exiv2::AnyError& e) {
503 gint exif_item_get_integer(ExifItem *item, gint *value)
507 *value = ((Exiv2::Metadatum *)item)->toLong();
510 catch (Exiv2::AnyError& e) {
511 std::cout << "Caught Exiv2 exception '" << e << "'\n";
516 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
519 if (!item) return NULL;
520 if (n >= exif_item_get_elements(item)) return NULL;
521 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational(n);
522 static ExifRational ret;
525 if (sign) *sign = (((Exiv2::Metadatum *)item)->typeId() == Exiv2::signedRational);
528 catch (Exiv2::AnyError& e) {
529 std::cout << "Caught Exiv2 exception '" << e << "'\n";
534 gchar *exif_get_tag_description_by_key(const gchar *key)
537 Exiv2::ExifKey ekey(key);
538 return g_locale_to_utf8(Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ()), -1, NULL, NULL, NULL);
540 catch (Exiv2::AnyError& e) {
541 std::cout << "Caught Exiv2 exception '" << e << "'\n";
546 int exif_item_set_string(ExifItem *item, const char *str)
550 ((Exiv2::Metadatum *)item)->setValue(std::string(str));
553 catch (Exiv2::AnyError& e) {
558 int exif_item_delete(ExifData *exif, ExifItem *item)
562 for (Exiv2::ExifData::iterator i = exif->exifData().begin(); i != exif->exifData().end(); ++i) {
563 if (((Exiv2::Metadatum *)item) == &*i) {
564 i = exif->exifData().erase(i);
568 for (Exiv2::IptcData::iterator i = exif->iptcData().begin(); i != exif->iptcData().end(); ++i) {
569 if (((Exiv2::Metadatum *)item) == &*i) {
570 i = exif->iptcData().erase(i);
574 #if EXIV2_TEST_VERSION(0,16,0)
575 for (Exiv2::XmpData::iterator i = exif->xmpData().begin(); i != exif->xmpData().end(); ++i) {
576 if (((Exiv2::Metadatum *)item) == &*i) {
577 i = exif->xmpData().erase(i);
584 catch (Exiv2::AnyError& e) {
589 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
591 if (exif->cp_data) g_free(exif->cp_data);
592 exif->cp_data = cp_data;
593 exif->cp_length =cp_length;
596 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
600 if (data_len) *data_len = exif->cp_length;
601 return (unsigned char *) g_memdup(exif->cp_data, exif->cp_length);
603 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
604 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
605 return (unsigned char *) exif_item_get_data(prof_item, data_len);
609 #if EXIV2_TEST_VERSION(0,17,90)
611 guchar *exif_get_preview(ExifData *exif, guint *data_len)
613 if (!exif) return NULL;
615 const char* path = exif->image->io().path().c_str();
616 /* given image pathname, first do simple (and fast) file extension test */
617 if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return NULL;
621 Exiv2::PreviewImageLoader loader(*exif->image);
623 Exiv2::PreviewPropertiesList list = loader.getPreviewPropertiesList();
627 Exiv2::PreviewPropertiesList::iterator pos = --list.end();
629 Exiv2::PreviewImage image = loader.getPreviewImage(*pos);
631 Exiv2::DataBuf &buf = image.data();
632 std::pair<Exiv2::byte*, long> p = buf.release();
634 *data_len = p.second;
639 catch (Exiv2::AnyError& e) {
640 std::cout << "Caught Exiv2 exception '" << e << "'\n";
645 void exif_free_preview(guchar *buf)
647 delete[] (Exiv2::byte*)buf;
652 #if !EXIV2_TEST_VERSION(0,17,90)
654 /* This is a dirty hack to support raw file preview, bassed on
655 tiffparse.cpp from Exiv2 examples */
660 RawFile(Exiv2::BasicIo &io);
663 const Exiv2::Value *find(uint16_t tag, uint16_t group);
665 unsigned long preview_offset();
669 Exiv2::TiffComponent::AutoPtr rootDir;
671 const Exiv2::byte *map_data;
673 unsigned long offset;
676 typedef struct _UnmapData UnmapData;
684 static GList *exif_unmap_list = 0;
686 extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len)
688 unsigned long offset;
690 if (!exif) return NULL;
691 const char* path = exif->image->io().path().c_str();
693 /* given image pathname, first do simple (and fast) file extension test */
694 if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return NULL;
703 RawFile rf(exif->image->io());
704 offset = rf.preview_offset();
705 DEBUG_1("%s: offset %lu", path, offset);
707 fd = open(path, O_RDONLY);
713 if (fstat(fd, &st) == -1)
718 map_len = st.st_size;
719 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
721 if (map_data == MAP_FAILED)
725 *data_len = map_len - offset;
726 ud = g_new(UnmapData, 1);
727 ud->ptr = map_data + offset;
728 ud->map_data = map_data;
729 ud->map_len = map_len;
731 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
735 catch (Exiv2::AnyError& e) {
736 std::cout << "Caught Exiv2 exception '" << e << "'\n";
742 void exif_free_preview(guchar *buf)
744 GList *work = exif_unmap_list;
748 UnmapData *ud = (UnmapData *)work->data;
751 munmap(ud->map_data, ud->map_len);
752 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
758 g_assert_not_reached();
761 using namespace Exiv2;
763 RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
767 if (fstat(fd, &st) == -1)
771 map_len = st.st_size;
772 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
773 if (map_data == MAP_FAILED)
778 if (io.open() != 0) {
779 throw Error(9, io.path(), strError());
782 map_data = io.mmap();
786 type = Exiv2::ImageFactory::getType(map_data, map_len);
788 #if EXIV2_TEST_VERSION(0,16,0)
789 TiffHeaderBase *tiffHeader = NULL;
791 TiffHeade2 *tiffHeader = NULL;
793 Cr2Header *cr2Header = NULL;
796 case Exiv2::ImageType::tiff:
797 tiffHeader = new TiffHeade2();
799 case Exiv2::ImageType::cr2:
800 cr2Header = new Cr2Header();
802 #if EXIV2_TEST_VERSION(0,16,0)
803 case Exiv2::ImageType::orf:
804 tiffHeader = new OrfHeader();
807 #if EXIV2_TEST_VERSION(0,13,0)
808 case Exiv2::ImageType::raf:
809 if (map_len < 84 + 4) throw Error(14);
810 offset = getULong(map_data + 84, bigEndian);
813 case Exiv2::ImageType::crw:
815 // Parse the image, starting with a CIFF header component
816 Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
817 parseTree->read(map_data, map_len);
818 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
819 if (entry) offset = entry->pData() - map_data;
824 throw Error(3, "RAW");
827 // process tiff-like formats
829 TiffCompFactoryFct createFct = TiffCreator::create;
831 rootDir = createFct(Tag::root, Group::none);
832 if (0 == rootDir.get()) {
833 throw Error(1, "No root element defined in TIFF structure");
838 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
839 #if EXIV2_TEST_VERSION(0,16,0)
840 rootDir->setStart(map_data + tiffHeader->offset());
842 rootDir->setStart(map_data + tiffHeader->ifdOffset());
848 rootDir->setStart(map_data + cr2Header->offset());
851 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
853 TiffReader reader(map_data,
858 rootDir->accept(reader);
866 RawFile::~RawFile(void)
872 const Value * RawFile::find(uint16_t tag, uint16_t group)
874 TiffFinder finder(tag, group);
875 rootDir->accept(finder);
876 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
879 DEBUG_1("(tag: %04x %04x) ", tag, group);
886 unsigned long RawFile::preview_offset(void)
889 if (offset) return offset;
891 if (type == Exiv2::ImageType::cr2)
893 val = find(0x111, Group::ifd0);
894 if (val) return val->toLong();
899 val = find(0x201, Group::sub0_0);
900 if (val) return val->toLong();
902 val = find(0x201, Group::ifd0);
903 if (val) return val->toLong();
905 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
906 if (val) return val->toLong();
908 val = find(0x111, Group::sub0_1); // dng
909 if (val) return val->toLong();
920 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */