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!
18 #include <exiv2/image.hpp>
19 #include <exiv2/exif.hpp>
22 // EXIV2_TEST_VERSION is defined in Exiv2 0.15 and newer.
23 #ifndef EXIV2_TEST_VERSION
24 # define EXIV2_TEST_VERSION(major,minor,patch) \
25 ( EXIV2_VERSION >= EXIV2_MAKE_VERSION(major,minor,patch) )
29 #include <sys/types.h>
35 #include <exiv2/tiffparser.hpp>
36 #include <exiv2/tiffcomposite.hpp>
37 #include <exiv2/tiffvisitor.hpp>
38 #include <exiv2/tiffimage.hpp>
39 #include <exiv2/cr2image.hpp>
40 #include <exiv2/crwimage.hpp>
41 #if EXIV2_TEST_VERSION(0,16,0)
42 #include <exiv2/orfimage.hpp>
44 #if EXIV2_TEST_VERSION(0,13,0)
45 #include <exiv2/rafimage.hpp>
47 #include <exiv2/futils.hpp>
56 #include "ui_fileops.h"
62 Exiv2::Image::AutoPtr image;
63 Exiv2::Image::AutoPtr sidecar;
64 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
65 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
66 #if EXIV2_TEST_VERSION(0,16,0)
67 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
71 /* the icc profile in jpeg is not technically exif - store it here */
72 unsigned char *cp_data;
75 _ExifData(gchar *path, gchar *sidecar_path)
80 gchar *pathl = path_from_utf8(path);
81 image = Exiv2::ImageFactory::open(pathl);
83 // g_assert (image.get() != 0);
84 image->readMetadata();
86 #if EXIV2_TEST_VERSION(0,16,0)
87 DEBUG_2("xmp count %li", image->xmpData().count());
88 if (sidecar_path && image->xmpData().empty())
90 gchar *sidecar_pathl = path_from_utf8(sidecar_path);
91 sidecar = Exiv2::ImageFactory::open(sidecar_pathl);
92 g_free(sidecar_pathl);
93 sidecar->readMetadata();
94 have_sidecar = sidecar->good();
95 DEBUG_2("sidecar xmp count %li", sidecar->xmpData().count());
99 #if EXIV2_TEST_VERSION(0,14,0)
100 if (image->mimeType() == std::string("image/jpeg"))
102 /* try to get jpeg color profile */
103 Exiv2::BasicIo &io = image->io();
104 gint open = io.isopen();
105 if (!open) io.open();
106 unsigned char *mapped = (unsigned char*)io.mmap();
107 if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
109 if (!open) io.close();
116 if (cp_data) g_free(cp_data);
121 if (have_sidecar) sidecar->writeMetadata();
122 image->writeMetadata();
125 Exiv2::ExifData &exifData ()
127 return image->exifData();
130 Exiv2::IptcData &iptcData ()
132 return image->iptcData();
135 #if EXIV2_TEST_VERSION(0,16,0)
136 Exiv2::XmpData &xmpData ()
138 return have_sidecar ? sidecar->xmpData() : image->xmpData();
146 ExifData *exif_read(gchar *path, gchar *sidecar_path)
148 DEBUG_1("exif read %s, sidecar: %s", path, sidecar_path ? sidecar_path : "-");
150 return new ExifData(path, sidecar_path);
152 catch (Exiv2::AnyError& e) {
153 std::cout << "Caught Exiv2 exception '" << e << "'\n";
159 int exif_write(ExifData *exif)
162 exif->writeMetadata();
165 catch (Exiv2::AnyError& e) {
166 std::cout << "Caught Exiv2 exception '" << e << "'\n";
173 void exif_free(ExifData *exif)
179 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
182 Exiv2::Metadatum *item = NULL;
184 Exiv2::ExifKey ekey(key);
185 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
186 if (pos == exif->exifData().end()) return NULL;
189 catch (Exiv2::AnyError& e) {
191 Exiv2::IptcKey ekey(key);
192 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
193 if (pos == exif->iptcData().end()) return NULL;
196 catch (Exiv2::AnyError& e) {
197 #if EXIV2_TEST_VERSION(0,16,0)
198 Exiv2::XmpKey ekey(key);
199 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
200 if (pos == exif->xmpData().end()) return NULL;
205 return (ExifItem *)item;
207 catch (Exiv2::AnyError& e) {
208 std::cout << "Caught Exiv2 exception '" << e << "'\n";
213 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
216 Exiv2::Metadatum *item = NULL;
218 Exiv2::ExifKey ekey(key);
219 exif->exifData().add(ekey, NULL);
220 Exiv2::ExifData::iterator pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
224 catch (Exiv2::AnyError& e) {
226 Exiv2::IptcKey ekey(key);
227 exif->iptcData().add(ekey, NULL);
228 Exiv2::IptcData::iterator pos = exif->iptcData().end();
232 catch (Exiv2::AnyError& e) {
233 #if EXIV2_TEST_VERSION(0,16,0)
234 Exiv2::XmpKey ekey(key);
235 exif->xmpData().add(ekey, NULL);
236 Exiv2::XmpData::iterator pos = exif->xmpData().end();
242 return (ExifItem *)item;
244 catch (Exiv2::AnyError& e) {
245 std::cout << "Caught Exiv2 exception '" << e << "'\n";
251 ExifItem *exif_get_first_item(ExifData *exif)
254 exif->exifIter = exif->exifData().begin();
255 exif->iptcIter = exif->iptcData().begin();
256 #if EXIV2_TEST_VERSION(0,16,0)
257 exif->xmpIter = exif->xmpData().begin();
259 if (exif->exifIter != exif->exifData().end())
261 const Exiv2::Metadatum *item = &*exif->exifIter;
263 return (ExifItem *)item;
265 if (exif->iptcIter != exif->iptcData().end())
267 const Exiv2::Metadatum *item = &*exif->iptcIter;
269 return (ExifItem *)item;
271 #if EXIV2_TEST_VERSION(0,16,0)
272 if (exif->xmpIter != exif->xmpData().end())
274 const Exiv2::Metadatum *item = &*exif->xmpIter;
276 return (ExifItem *)item;
282 catch (Exiv2::AnyError& e) {
283 std::cout << "Caught Exiv2 exception '" << e << "'\n";
288 ExifItem *exif_get_next_item(ExifData *exif)
291 if (exif->exifIter != exif->exifData().end())
293 const Exiv2::Metadatum *item = &*exif->exifIter;
295 return (ExifItem *)item;
297 if (exif->iptcIter != exif->iptcData().end())
299 const Exiv2::Metadatum *item = &*exif->iptcIter;
301 return (ExifItem *)item;
303 #if EXIV2_TEST_VERSION(0,16,0)
304 if (exif->xmpIter != exif->xmpData().end())
306 const Exiv2::Metadatum *item = &*exif->xmpIter;
308 return (ExifItem *)item;
313 catch (Exiv2::AnyError& e) {
314 std::cout << "Caught Exiv2 exception '" << e << "'\n";
319 char *exif_item_get_tag_name(ExifItem *item)
322 if (!item) return NULL;
323 return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
325 catch (Exiv2::AnyError& e) {
326 std::cout << "Caught Exiv2 exception '" << e << "'\n";
331 guint exif_item_get_tag_id(ExifItem *item)
335 return ((Exiv2::Metadatum *)item)->tag();
337 catch (Exiv2::AnyError& e) {
338 std::cout << "Caught Exiv2 exception '" << e << "'\n";
343 guint exif_item_get_elements(ExifItem *item)
347 return ((Exiv2::Metadatum *)item)->count();
349 catch (Exiv2::AnyError& e) {
350 std::cout << "Caught Exiv2 exception '" << e << "'\n";
355 char *exif_item_get_data(ExifItem *item, guint *data_len)
359 Exiv2::Metadatum *md = (Exiv2::Metadatum *)item;
360 if(data_len) *data_len = md->size();
361 char *data = (char *)g_malloc(md->size());
362 long res = md->copy((Exiv2::byte *)data, Exiv2::littleEndian /* should not matter */);
363 g_assert(res == md->size());
366 catch (Exiv2::AnyError& e) {
367 std::cout << "Caught Exiv2 exception '" << e << "'\n";
372 char *exif_item_get_description(ExifItem *item)
375 if (!item) return NULL;
376 return g_strdup(((Exiv2::Metadatum *)item)->tagLabel().c_str());
378 catch (std::exception& e) {
379 // std::cout << "Caught Exiv2 exception '" << e << "'\n";
385 invalidTypeId, unsignedByte, asciiString, unsignedShort,
386 unsignedLong, unsignedRational, signedByte, undefined,
387 signedShort, signedLong, signedRational, string,
388 date, time, comment, directory,
389 xmpText, xmpAlt, xmpBag, xmpSeq,
393 static guint format_id_trans_tbl [] = {
395 EXIF_FORMAT_BYTE_UNSIGNED,
397 EXIF_FORMAT_SHORT_UNSIGNED,
398 EXIF_FORMAT_LONG_UNSIGNED,
399 EXIF_FORMAT_RATIONAL_UNSIGNED,
401 EXIF_FORMAT_UNDEFINED,
404 EXIF_FORMAT_RATIONAL,
408 EXIF_FORMAT_UNDEFINED,
417 guint exif_item_get_format_id(ExifItem *item)
420 if (!item) return EXIF_FORMAT_UNKNOWN;
421 guint id = ((Exiv2::Metadatum *)item)->typeId();
422 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
423 return format_id_trans_tbl[id];
425 catch (Exiv2::AnyError& e) {
426 std::cout << "Caught Exiv2 exception '" << e << "'\n";
427 return EXIF_FORMAT_UNKNOWN;
431 const char *exif_item_get_format_name(ExifItem *item, gint brief)
434 if (!item) return NULL;
435 return ((Exiv2::Metadatum *)item)->typeName();
437 catch (Exiv2::AnyError& e) {
438 std::cout << "Caught Exiv2 exception '" << e << "'\n";
444 gchar *exif_item_get_data_as_text(ExifItem *item)
447 if (!item) return NULL;
448 // std::stringstream str; // does not work with Exiv2::Metadatum because operator<< is not virtual
449 // str << *((Exiv2::Metadatum *)item);
450 // return g_strdup(str.str().c_str());
451 return g_strdup(((Exiv2::Metadatum *)item)->toString().c_str());
453 catch (Exiv2::AnyError& e) {
458 gchar *exif_item_get_string(ExifItem *item, int idx)
461 if (!item) return NULL;
462 Exiv2::Metadatum *em = (Exiv2::Metadatum *)item;
463 #if EXIV2_TEST_VERSION(0,16,0)
464 std::string str = em->toString(idx);
466 std::string str = em->toString(); // FIXME
468 if (idx == 0 && str == "") str = em->toString();
469 if (str.length() > 5 && str.substr(0, 5) == "lang=")
471 std::string::size_type pos = str.find_first_of(' ');
472 if (pos != std::string::npos) str = str.substr(pos+1);
475 return g_strdup(str.c_str());
477 catch (Exiv2::AnyError& e) {
483 gint exif_item_get_integer(ExifItem *item, gint *value)
487 *value = ((Exiv2::Metadatum *)item)->toLong();
490 catch (Exiv2::AnyError& e) {
491 std::cout << "Caught Exiv2 exception '" << e << "'\n";
496 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign)
499 if (!item) return NULL;
500 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational();
501 static ExifRational ret;
504 if (sign) *sign = (((Exiv2::Metadatum *)item)->typeId() == Exiv2::signedRational);
507 catch (Exiv2::AnyError& e) {
508 std::cout << "Caught Exiv2 exception '" << e << "'\n";
513 const gchar *exif_get_tag_description_by_key(const gchar *key)
516 Exiv2::ExifKey ekey(key);
517 return Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ());
519 catch (Exiv2::AnyError& e) {
520 std::cout << "Caught Exiv2 exception '" << e << "'\n";
525 int exif_item_set_string(ExifItem *item, const char *str)
529 ((Exiv2::Metadatum *)item)->setValue(std::string(str));
532 catch (Exiv2::AnyError& e) {
537 int exif_item_delete(ExifData *exif, ExifItem *item)
541 for (Exiv2::ExifData::iterator i = exif->exifData().begin(); i != exif->exifData().end(); ++i) {
542 if (((Exiv2::Metadatum *)item) == &*i) {
543 i = exif->exifData().erase(i);
547 for (Exiv2::IptcData::iterator i = exif->iptcData().begin(); i != exif->iptcData().end(); ++i) {
548 if (((Exiv2::Metadatum *)item) == &*i) {
549 i = exif->iptcData().erase(i);
553 #if EXIV2_TEST_VERSION(0,16,0)
554 for (Exiv2::XmpData::iterator i = exif->xmpData().begin(); i != exif->xmpData().end(); ++i) {
555 if (((Exiv2::Metadatum *)item) == &*i) {
556 i = exif->xmpData().erase(i);
563 catch (Exiv2::AnyError& e) {
568 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
570 if (exif->cp_data) g_free(exif->cp_data);
571 exif->cp_data = cp_data;
572 exif->cp_length =cp_length;
575 unsigned char *exif_get_color_profile(ExifData *exif, guint *data_len)
579 if (data_len) *data_len = exif->cp_length;
580 return (unsigned char *) g_memdup(exif->cp_data, exif->cp_length);
582 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
583 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
584 return (unsigned char *) exif_item_get_data(prof_item, data_len);
592 /* This is a dirty hack to support raw file preview, bassed on
593 tiffparse.cpp from Exiv2 examples */
601 const Exiv2::Value *find(uint16_t tag, uint16_t group);
603 unsigned long preview_offset();
607 Exiv2::TiffComponent::AutoPtr rootDir;
608 Exiv2::byte *map_data;
610 unsigned long offset;
613 using namespace Exiv2;
615 RawFile::RawFile(int fd) : map_data(NULL), map_len(0), offset(0)
618 if (fstat(fd, &st) == -1)
622 map_len = st.st_size;
623 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
624 if (map_data == MAP_FAILED)
628 type = Exiv2::ImageFactory::getType(map_data, map_len);
630 #if EXIV2_TEST_VERSION(0,16,0)
631 TiffHeaderBase *tiffHeader = NULL;
633 TiffHeade2 *tiffHeader = NULL;
635 Cr2Header *cr2Header = NULL;
638 case Exiv2::ImageType::tiff:
639 tiffHeader = new TiffHeade2();
641 case Exiv2::ImageType::cr2:
642 cr2Header = new Cr2Header();
644 #if EXIV2_TEST_VERSION(0,16,0)
645 case Exiv2::ImageType::orf:
646 tiffHeader = new OrfHeader();
649 #if EXIV2_TEST_VERSION(0,13,0)
650 case Exiv2::ImageType::raf:
651 if (map_len < 84 + 4) throw Error(14);
652 offset = getULong(map_data + 84, bigEndian);
655 case Exiv2::ImageType::crw:
657 // Parse the image, starting with a CIFF header component
658 Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
659 parseTree->read(map_data, map_len);
660 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
661 if (entry) offset = entry->pData() - map_data;
666 throw Error(3, "RAW");
669 // process tiff-like formats
671 TiffCompFactoryFct createFct = TiffCreator::create;
673 rootDir = createFct(Tag::root, Group::none);
674 if (0 == rootDir.get()) {
675 throw Error(1, "No root element defined in TIFF structure");
680 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
681 #if EXIV2_TEST_VERSION(0,16,0)
682 rootDir->setStart(map_data + tiffHeader->offset());
684 rootDir->setStart(map_data + tiffHeader->ifdOffset());
690 rootDir->setStart(map_data + cr2Header->offset());
693 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
695 TiffReader reader(map_data,
700 rootDir->accept(reader);
710 if (map_data && munmap(map_data, map_len) == -1)
712 printf("Failed to unmap file \n");
716 const Value * RawFile::find(uint16_t tag, uint16_t group)
718 TiffFinder finder(tag, group);
719 rootDir->accept(finder);
720 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
723 DEBUG_1("(tag: %04x %04x) ", tag, group);
730 unsigned long RawFile::preview_offset()
733 if (offset) return offset;
735 if (type == Exiv2::ImageType::cr2)
737 val = find(0x111, Group::ifd0);
738 if (val) return val->toLong();
743 val = find(0x201, Group::sub0_0);
744 if (val) return val->toLong();
746 val = find(0x201, Group::ifd0);
747 if (val) return val->toLong();
749 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
750 if (val) return val->toLong();
752 val = find(0x111, Group::sub0_1); // dng
753 if (val) return val->toLong();
759 extern "C" gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
760 unsigned char *header_data, const guint header_len,
761 guint *image_offset, guint *exif_offset)
764 unsigned long offset;
766 /* given image pathname, first do simple (and fast) file extension test */
767 if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return 0;
771 offset = rf.preview_offset();
772 DEBUG_1("%s: offset %lu", path, offset);
774 catch (Exiv2::AnyError& e) {
775 std::cout << "Caught Exiv2 exception '" << e << "'\n";
779 if (image_offset && offset > 0)
781 *image_offset = offset;
782 if ((unsigned long) lseek(fd, *image_offset, SEEK_SET) != *image_offset)
784 printf("Failed to seek to embedded image\n");
787 if (*exif_offset) *exif_offset = 0;