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, gint n)
518 if (!item) return NULL;
519 if (n >= exif_item_get_elements(item)) return NULL;
520 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational(n);
521 static ExifRational ret;
524 if (sign) *sign = (((Exiv2::Metadatum *)item)->typeId() == Exiv2::signedRational);
527 catch (Exiv2::AnyError& e) {
528 std::cout << "Caught Exiv2 exception '" << e << "'\n";
533 const gchar *exif_get_tag_description_by_key(const gchar *key)
536 Exiv2::ExifKey ekey(key);
537 return Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ());
539 catch (Exiv2::AnyError& e) {
540 std::cout << "Caught Exiv2 exception '" << e << "'\n";
545 int exif_item_set_string(ExifItem *item, const char *str)
549 ((Exiv2::Metadatum *)item)->setValue(std::string(str));
552 catch (Exiv2::AnyError& e) {
557 int exif_item_delete(ExifData *exif, ExifItem *item)
561 for (Exiv2::ExifData::iterator i = exif->exifData().begin(); i != exif->exifData().end(); ++i) {
562 if (((Exiv2::Metadatum *)item) == &*i) {
563 i = exif->exifData().erase(i);
567 for (Exiv2::IptcData::iterator i = exif->iptcData().begin(); i != exif->iptcData().end(); ++i) {
568 if (((Exiv2::Metadatum *)item) == &*i) {
569 i = exif->iptcData().erase(i);
573 #if EXIV2_TEST_VERSION(0,16,0)
574 for (Exiv2::XmpData::iterator i = exif->xmpData().begin(); i != exif->xmpData().end(); ++i) {
575 if (((Exiv2::Metadatum *)item) == &*i) {
576 i = exif->xmpData().erase(i);
583 catch (Exiv2::AnyError& e) {
588 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
590 if (exif->cp_data) g_free(exif->cp_data);
591 exif->cp_data = cp_data;
592 exif->cp_length =cp_length;
595 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
599 if (data_len) *data_len = exif->cp_length;
600 return (unsigned char *) g_memdup(exif->cp_data, exif->cp_length);
602 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
603 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
604 return (unsigned char *) exif_item_get_data(prof_item, data_len);
608 #if EXIV2_TEST_VERSION(0,17,90)
610 guchar *exif_get_preview(ExifData *exif, guint *data_len)
612 if (!exif) return NULL;
614 const char* path = exif->image->io().path().c_str();
615 /* given image pathname, first do simple (and fast) file extension test */
616 if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return NULL;
620 Exiv2::PreviewImageLoader loader(*exif->image);
622 Exiv2::PreviewPropertiesList list = loader.getPreviewPropertiesList();
626 Exiv2::PreviewPropertiesList::iterator pos = --list.end();
628 Exiv2::PreviewImage image = loader.getPreviewImage(*pos);
630 Exiv2::DataBuf &buf = image.data();
631 std::pair<Exiv2::byte*, long> p = buf.release();
633 *data_len = p.second;
638 catch (Exiv2::AnyError& e) {
639 std::cout << "Caught Exiv2 exception '" << e << "'\n";
644 void exif_free_preview(guchar *buf)
646 delete[] (Exiv2::byte*)buf;
651 #if !EXIV2_TEST_VERSION(0,17,90)
653 /* This is a dirty hack to support raw file preview, bassed on
654 tiffparse.cpp from Exiv2 examples */
659 RawFile(Exiv2::BasicIo &io);
662 const Exiv2::Value *find(uint16_t tag, uint16_t group);
664 unsigned long preview_offset();
668 Exiv2::TiffComponent::AutoPtr rootDir;
670 const Exiv2::byte *map_data;
672 unsigned long offset;
675 typedef struct _UnmapData UnmapData;
683 static GList *exif_unmap_list = 0;
685 extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len)
687 unsigned long offset;
689 if (!exif) return NULL;
690 const char* path = exif->image->io().path().c_str();
692 /* given image pathname, first do simple (and fast) file extension test */
693 if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return NULL;
702 RawFile rf(exif->image->io());
703 offset = rf.preview_offset();
704 DEBUG_1("%s: offset %lu", path, offset);
706 fd = open(path, O_RDONLY);
712 if (fstat(fd, &st) == -1)
717 map_len = st.st_size;
718 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
720 if (map_data == MAP_FAILED)
724 *data_len = map_len - offset;
725 ud = g_new(UnmapData, 1);
726 ud->ptr = map_data + offset;
727 ud->map_data = map_data;
728 ud->map_len = map_len;
730 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
734 catch (Exiv2::AnyError& e) {
735 std::cout << "Caught Exiv2 exception '" << e << "'\n";
741 void exif_free_preview(guchar *buf)
743 GList *work = exif_unmap_list;
747 UnmapData *ud = (UnmapData *)work->data;
750 munmap(ud->map_data, ud->map_len);
751 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
757 g_assert_not_reached();
760 using namespace Exiv2;
762 RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
766 if (fstat(fd, &st) == -1)
770 map_len = st.st_size;
771 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
772 if (map_data == MAP_FAILED)
777 if (io.open() != 0) {
778 throw Error(9, io.path(), strError());
781 map_data = io.mmap();
785 type = Exiv2::ImageFactory::getType(map_data, map_len);
787 #if EXIV2_TEST_VERSION(0,16,0)
788 TiffHeaderBase *tiffHeader = NULL;
790 TiffHeade2 *tiffHeader = NULL;
792 Cr2Header *cr2Header = NULL;
795 case Exiv2::ImageType::tiff:
796 tiffHeader = new TiffHeade2();
798 case Exiv2::ImageType::cr2:
799 cr2Header = new Cr2Header();
801 #if EXIV2_TEST_VERSION(0,16,0)
802 case Exiv2::ImageType::orf:
803 tiffHeader = new OrfHeader();
806 #if EXIV2_TEST_VERSION(0,13,0)
807 case Exiv2::ImageType::raf:
808 if (map_len < 84 + 4) throw Error(14);
809 offset = getULong(map_data + 84, bigEndian);
812 case Exiv2::ImageType::crw:
814 // Parse the image, starting with a CIFF header component
815 Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
816 parseTree->read(map_data, map_len);
817 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
818 if (entry) offset = entry->pData() - map_data;
823 throw Error(3, "RAW");
826 // process tiff-like formats
828 TiffCompFactoryFct createFct = TiffCreator::create;
830 rootDir = createFct(Tag::root, Group::none);
831 if (0 == rootDir.get()) {
832 throw Error(1, "No root element defined in TIFF structure");
837 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
838 #if EXIV2_TEST_VERSION(0,16,0)
839 rootDir->setStart(map_data + tiffHeader->offset());
841 rootDir->setStart(map_data + tiffHeader->ifdOffset());
847 rootDir->setStart(map_data + cr2Header->offset());
850 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
852 TiffReader reader(map_data,
857 rootDir->accept(reader);
865 RawFile::~RawFile(void)
871 const Value * RawFile::find(uint16_t tag, uint16_t group)
873 TiffFinder finder(tag, group);
874 rootDir->accept(finder);
875 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
878 DEBUG_1("(tag: %04x %04x) ", tag, group);
885 unsigned long RawFile::preview_offset(void)
888 if (offset) return offset;
890 if (type == Exiv2::ImageType::cr2)
892 val = find(0x111, Group::ifd0);
893 if (val) return val->toLong();
898 val = find(0x201, Group::sub0_0);
899 if (val) return val->toLong();
901 val = find(0x201, Group::ifd0);
902 if (val) return val->toLong();
904 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
905 if (val) return val->toLong();
907 val = find(0x111, Group::sub0_1); // dng
908 if (val) return val->toLong();