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_strdup(((Exiv2::Metadatum *)item)->tagLabel().c_str());
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_strdup(metadatum->print().c_str());
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_strdup(str.str().c_str());
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_strdup(str.c_str());
496 catch (Exiv2::AnyError& e) {
502 gint exif_item_get_integer(ExifItem *item, gint *value)
506 *value = ((Exiv2::Metadatum *)item)->toLong();
509 catch (Exiv2::AnyError& e) {
510 std::cout << "Caught Exiv2 exception '" << e << "'\n";
515 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign)
518 if (!item) return NULL;
519 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational();
520 static ExifRational ret;
523 if (sign) *sign = (((Exiv2::Metadatum *)item)->typeId() == Exiv2::signedRational);
526 catch (Exiv2::AnyError& e) {
527 std::cout << "Caught Exiv2 exception '" << e << "'\n";
532 const gchar *exif_get_tag_description_by_key(const gchar *key)
535 Exiv2::ExifKey ekey(key);
536 return Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ());
538 catch (Exiv2::AnyError& e) {
539 std::cout << "Caught Exiv2 exception '" << e << "'\n";
544 int exif_item_set_string(ExifItem *item, const char *str)
548 ((Exiv2::Metadatum *)item)->setValue(std::string(str));
551 catch (Exiv2::AnyError& e) {
556 int exif_item_delete(ExifData *exif, ExifItem *item)
560 for (Exiv2::ExifData::iterator i = exif->exifData().begin(); i != exif->exifData().end(); ++i) {
561 if (((Exiv2::Metadatum *)item) == &*i) {
562 i = exif->exifData().erase(i);
566 for (Exiv2::IptcData::iterator i = exif->iptcData().begin(); i != exif->iptcData().end(); ++i) {
567 if (((Exiv2::Metadatum *)item) == &*i) {
568 i = exif->iptcData().erase(i);
572 #if EXIV2_TEST_VERSION(0,16,0)
573 for (Exiv2::XmpData::iterator i = exif->xmpData().begin(); i != exif->xmpData().end(); ++i) {
574 if (((Exiv2::Metadatum *)item) == &*i) {
575 i = exif->xmpData().erase(i);
582 catch (Exiv2::AnyError& e) {
587 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
589 if (exif->cp_data) g_free(exif->cp_data);
590 exif->cp_data = cp_data;
591 exif->cp_length =cp_length;
594 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
598 if (data_len) *data_len = exif->cp_length;
599 return (unsigned char *) g_memdup(exif->cp_data, exif->cp_length);
601 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
602 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
603 return (unsigned char *) exif_item_get_data(prof_item, data_len);
607 #if EXIV2_TEST_VERSION(0,17,90)
609 guchar *exif_get_preview(ExifData *exif, guint *data_len)
611 if (!exif) return NULL;
614 Exiv2::PreviewImageList list(*exif->image);
617 Exiv2::PreviewImageList::iterator pos = list.begin();
618 if (pos != list.end())
620 Exiv2::DataBuf buf = pos->copy();
621 std::pair<Exiv2::byte*, long> p = buf.release();
623 *data_len = p.second;
628 catch (Exiv2::AnyError& e) {
629 std::cout << "Caught Exiv2 exception '" << e << "'\n";
634 void exif_free_preview(guchar *buf)
636 delete[] (Exiv2::byte*)buf;
641 #if !EXIV2_TEST_VERSION(0,17,90)
643 /* This is a dirty hack to support raw file preview, bassed on
644 tiffparse.cpp from Exiv2 examples */
649 RawFile(Exiv2::BasicIo &io);
652 const Exiv2::Value *find(uint16_t tag, uint16_t group);
654 unsigned long preview_offset();
658 Exiv2::TiffComponent::AutoPtr rootDir;
660 const Exiv2::byte *map_data;
662 unsigned long offset;
665 typedef struct _UnmapData UnmapData;
673 static GList *exif_unmap_list = 0;
675 extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len)
677 unsigned long offset;
679 if (!exif) return NULL;
680 const char* path = exif->image->io().path().c_str();
682 /* given image pathname, first do simple (and fast) file extension test */
683 if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return 0;
692 RawFile rf(exif->image->io());
693 offset = rf.preview_offset();
694 DEBUG_1("%s: offset %lu", path, offset);
696 fd = open(path, O_RDONLY);
702 if (fstat(fd, &st) == -1)
707 map_len = st.st_size;
708 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
710 if (map_data == MAP_FAILED)
714 *data_len = map_len - offset;
715 ud = g_new(UnmapData, 1);
716 ud->ptr = map_data + offset;
717 ud->map_data = map_data;
718 ud->map_len = map_len;
720 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
724 catch (Exiv2::AnyError& e) {
725 std::cout << "Caught Exiv2 exception '" << e << "'\n";
731 void exif_free_preview(guchar *buf)
733 GList *work = exif_unmap_list;
737 UnmapData *ud = (UnmapData *)work->data;
740 munmap(ud->map_data, ud->map_len);
741 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
747 g_assert_not_reached();
750 using namespace Exiv2;
752 RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
756 if (fstat(fd, &st) == -1)
760 map_len = st.st_size;
761 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
762 if (map_data == MAP_FAILED)
767 if (io.open() != 0) {
768 throw Error(9, io.path(), strError());
771 map_data = io.mmap();
775 type = Exiv2::ImageFactory::getType(map_data, map_len);
777 #if EXIV2_TEST_VERSION(0,16,0)
778 TiffHeaderBase *tiffHeader = NULL;
780 TiffHeade2 *tiffHeader = NULL;
782 Cr2Header *cr2Header = NULL;
785 case Exiv2::ImageType::tiff:
786 tiffHeader = new TiffHeade2();
788 case Exiv2::ImageType::cr2:
789 cr2Header = new Cr2Header();
791 #if EXIV2_TEST_VERSION(0,16,0)
792 case Exiv2::ImageType::orf:
793 tiffHeader = new OrfHeader();
796 #if EXIV2_TEST_VERSION(0,13,0)
797 case Exiv2::ImageType::raf:
798 if (map_len < 84 + 4) throw Error(14);
799 offset = getULong(map_data + 84, bigEndian);
802 case Exiv2::ImageType::crw:
804 // Parse the image, starting with a CIFF header component
805 Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
806 parseTree->read(map_data, map_len);
807 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
808 if (entry) offset = entry->pData() - map_data;
813 throw Error(3, "RAW");
816 // process tiff-like formats
818 TiffCompFactoryFct createFct = TiffCreator::create;
820 rootDir = createFct(Tag::root, Group::none);
821 if (0 == rootDir.get()) {
822 throw Error(1, "No root element defined in TIFF structure");
827 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
828 #if EXIV2_TEST_VERSION(0,16,0)
829 rootDir->setStart(map_data + tiffHeader->offset());
831 rootDir->setStart(map_data + tiffHeader->ifdOffset());
837 rootDir->setStart(map_data + cr2Header->offset());
840 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
842 TiffReader reader(map_data,
847 rootDir->accept(reader);
855 RawFile::~RawFile(void)
861 const Value * RawFile::find(uint16_t tag, uint16_t group)
863 TiffFinder finder(tag, group);
864 rootDir->accept(finder);
865 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
868 DEBUG_1("(tag: %04x %04x) ", tag, group);
875 unsigned long RawFile::preview_offset(void)
878 if (offset) return offset;
880 if (type == Exiv2::ImageType::cr2)
882 val = find(0x111, Group::ifd0);
883 if (val) return val->toLong();
888 val = find(0x201, Group::sub0_0);
889 if (val) return val->toLong();
891 val = find(0x201, Group::ifd0);
892 if (val) return val->toLong();
894 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
895 if (val) return val->toLong();
897 val = find(0x111, Group::sub0_1); // dng
898 if (val) return val->toLong();