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>
61 Exiv2::Image::AutoPtr image;
62 Exiv2::Image::AutoPtr sidecar;
63 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
64 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
65 #if EXIV2_TEST_VERSION(0,16,0)
66 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
70 /* the icc profile in jpeg is not technically exif - store it here */
71 unsigned char *cp_data;
74 _ExifData(gchar *path, gchar *sidecar_path)
79 image = Exiv2::ImageFactory::open(path);
80 // g_assert (image.get() != 0);
81 image->readMetadata();
83 #if EXIV2_TEST_VERSION(0,16,0)
84 DEBUG_2("xmp count %li\n", image->xmpData().count());
85 if (sidecar_path && image->xmpData().empty())
87 sidecar = Exiv2::ImageFactory::open(sidecar_path);
88 sidecar->readMetadata();
89 have_sidecar = sidecar->good();
90 DEBUG_2("sidecar xmp count %li\n", sidecar->xmpData().count());
94 #if EXIV2_TEST_VERSION(0,14,0)
95 if (image->mimeType() == std::string("image/jpeg"))
97 /* try to get jpeg color profile */
98 Exiv2::BasicIo &io = image->io();
99 gint open = io.isopen();
100 if (!open) io.open();
101 unsigned char *mapped = (unsigned char*)io.mmap();
102 if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
104 if (!open) io.close();
111 if (cp_data) g_free(cp_data);
116 if (have_sidecar) sidecar->writeMetadata();
117 image->writeMetadata();
120 Exiv2::ExifData &exifData ()
122 return image->exifData();
125 Exiv2::IptcData &iptcData ()
127 return image->iptcData();
130 #if EXIV2_TEST_VERSION(0,16,0)
131 Exiv2::XmpData &xmpData ()
133 return have_sidecar ? sidecar->xmpData() : image->xmpData();
141 ExifData *exif_read(gchar *path, gchar *sidecar_path)
143 DEBUG_1("exif read %s, sidecar: %s\n", path, sidecar_path ? sidecar_path : "-");
145 return new ExifData(path, sidecar_path);
147 catch (Exiv2::AnyError& e) {
148 std::cout << "Caught Exiv2 exception '" << e << "'\n";
154 int exif_write(ExifData *exif)
157 exif->writeMetadata();
160 catch (Exiv2::AnyError& e) {
161 std::cout << "Caught Exiv2 exception '" << e << "'\n";
168 void exif_free(ExifData *exif)
174 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
177 Exiv2::Metadatum *item = NULL;
179 Exiv2::ExifKey ekey(key);
180 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
181 if (pos == exif->exifData().end()) return NULL;
184 catch (Exiv2::AnyError& e) {
186 Exiv2::IptcKey ekey(key);
187 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
188 if (pos == exif->iptcData().end()) return NULL;
191 catch (Exiv2::AnyError& e) {
192 #if EXIV2_TEST_VERSION(0,16,0)
193 Exiv2::XmpKey ekey(key);
194 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
195 if (pos == exif->xmpData().end()) return NULL;
200 return (ExifItem *)item;
202 catch (Exiv2::AnyError& e) {
203 std::cout << "Caught Exiv2 exception '" << e << "'\n";
208 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
211 Exiv2::Metadatum *item = NULL;
213 Exiv2::ExifKey ekey(key);
214 exif->exifData().add(ekey, NULL);
215 Exiv2::ExifData::iterator pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
219 catch (Exiv2::AnyError& e) {
221 Exiv2::IptcKey ekey(key);
222 exif->iptcData().add(ekey, NULL);
223 Exiv2::IptcData::iterator pos = exif->iptcData().end();
227 catch (Exiv2::AnyError& e) {
228 #if EXIV2_TEST_VERSION(0,16,0)
229 Exiv2::XmpKey ekey(key);
230 exif->xmpData().add(ekey, NULL);
231 Exiv2::XmpData::iterator pos = exif->xmpData().end();
237 return (ExifItem *)item;
239 catch (Exiv2::AnyError& e) {
240 std::cout << "Caught Exiv2 exception '" << e << "'\n";
246 ExifItem *exif_get_first_item(ExifData *exif)
249 exif->exifIter = exif->exifData().begin();
250 exif->iptcIter = exif->iptcData().begin();
251 #if EXIV2_TEST_VERSION(0,16,0)
252 exif->xmpIter = exif->xmpData().begin();
254 if (exif->exifIter != exif->exifData().end())
256 const Exiv2::Metadatum *item = &*exif->exifIter;
258 return (ExifItem *)item;
260 if (exif->iptcIter != exif->iptcData().end())
262 const Exiv2::Metadatum *item = &*exif->iptcIter;
264 return (ExifItem *)item;
266 #if EXIV2_TEST_VERSION(0,16,0)
267 if (exif->xmpIter != exif->xmpData().end())
269 const Exiv2::Metadatum *item = &*exif->xmpIter;
271 return (ExifItem *)item;
277 catch (Exiv2::AnyError& e) {
278 std::cout << "Caught Exiv2 exception '" << e << "'\n";
283 ExifItem *exif_get_next_item(ExifData *exif)
286 if (exif->exifIter != exif->exifData().end())
288 const Exiv2::Metadatum *item = &*exif->exifIter;
290 return (ExifItem *)item;
292 if (exif->iptcIter != exif->iptcData().end())
294 const Exiv2::Metadatum *item = &*exif->iptcIter;
296 return (ExifItem *)item;
298 #if EXIV2_TEST_VERSION(0,16,0)
299 if (exif->xmpIter != exif->xmpData().end())
301 const Exiv2::Metadatum *item = &*exif->xmpIter;
303 return (ExifItem *)item;
308 catch (Exiv2::AnyError& e) {
309 std::cout << "Caught Exiv2 exception '" << e << "'\n";
314 char *exif_item_get_tag_name(ExifItem *item)
317 if (!item) return NULL;
318 return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
320 catch (Exiv2::AnyError& e) {
321 std::cout << "Caught Exiv2 exception '" << e << "'\n";
326 guint exif_item_get_tag_id(ExifItem *item)
330 return ((Exiv2::Metadatum *)item)->tag();
332 catch (Exiv2::AnyError& e) {
333 std::cout << "Caught Exiv2 exception '" << e << "'\n";
338 guint exif_item_get_elements(ExifItem *item)
342 return ((Exiv2::Metadatum *)item)->count();
344 catch (Exiv2::AnyError& e) {
345 std::cout << "Caught Exiv2 exception '" << e << "'\n";
350 char *exif_item_get_data(ExifItem *item, guint *data_len)
354 Exiv2::Metadatum *md = (Exiv2::Metadatum *)item;
355 if(data_len) *data_len = md->size();
356 char *data = (char *)g_malloc(md->size());
357 long res = md->copy((Exiv2::byte *)data, Exiv2::littleEndian /* should not matter */);
358 g_assert(res == md->size());
361 catch (Exiv2::AnyError& e) {
362 std::cout << "Caught Exiv2 exception '" << e << "'\n";
367 char *exif_item_get_description(ExifItem *item)
370 if (!item) return NULL;
371 return g_strdup(((Exiv2::Metadatum *)item)->tagLabel().c_str());
373 catch (std::exception& e) {
374 // std::cout << "Caught Exiv2 exception '" << e << "'\n";
380 invalidTypeId, unsignedByte, asciiString, unsignedShort,
381 unsignedLong, unsignedRational, signedByte, undefined,
382 signedShort, signedLong, signedRational, string,
383 date, time, comment, directory,
384 xmpText, xmpAlt, xmpBag, xmpSeq,
388 static guint format_id_trans_tbl [] = {
390 EXIF_FORMAT_BYTE_UNSIGNED,
392 EXIF_FORMAT_SHORT_UNSIGNED,
393 EXIF_FORMAT_LONG_UNSIGNED,
394 EXIF_FORMAT_RATIONAL_UNSIGNED,
396 EXIF_FORMAT_UNDEFINED,
399 EXIF_FORMAT_RATIONAL,
403 EXIF_FORMAT_UNDEFINED,
412 guint exif_item_get_format_id(ExifItem *item)
415 if (!item) return EXIF_FORMAT_UNKNOWN;
416 guint id = ((Exiv2::Metadatum *)item)->typeId();
417 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
418 return format_id_trans_tbl[id];
420 catch (Exiv2::AnyError& e) {
421 std::cout << "Caught Exiv2 exception '" << e << "'\n";
422 return EXIF_FORMAT_UNKNOWN;
426 const char *exif_item_get_format_name(ExifItem *item, gint brief)
429 if (!item) return NULL;
430 return ((Exiv2::Metadatum *)item)->typeName();
432 catch (Exiv2::AnyError& e) {
433 std::cout << "Caught Exiv2 exception '" << e << "'\n";
439 gchar *exif_item_get_data_as_text(ExifItem *item)
442 if (!item) return NULL;
443 // std::stringstream str; // does not work with Exiv2::Metadatum because operator<< is not virtual
444 // str << *((Exiv2::Metadatum *)item);
445 // return g_strdup(str.str().c_str());
446 return g_strdup(((Exiv2::Metadatum *)item)->toString().c_str());
448 catch (Exiv2::AnyError& e) {
453 gchar *exif_item_get_string(ExifItem *item, int idx)
456 if (!item) return NULL;
457 Exiv2::Metadatum *em = (Exiv2::Metadatum *)item;
458 #if EXIV2_TEST_VERSION(0,16,0)
459 std::string str = em->toString(idx);
461 std::string str = em->toString(); // FIXME
463 if (idx == 0 && str == "") str = em->toString();
464 if (str.length() > 5 && str.substr(0, 5) == "lang=")
466 std::string::size_type pos = str.find_first_of(' ');
467 if (pos != std::string::npos) str = str.substr(pos+1);
470 return g_strdup(str.c_str());
472 catch (Exiv2::AnyError& e) {
478 gint exif_item_get_integer(ExifItem *item, gint *value)
482 *value = ((Exiv2::Metadatum *)item)->toLong();
485 catch (Exiv2::AnyError& e) {
486 std::cout << "Caught Exiv2 exception '" << e << "'\n";
491 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign)
494 if (!item) return NULL;
495 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational();
496 static ExifRational ret;
499 if (sign) *sign = (((Exiv2::Metadatum *)item)->typeId() == Exiv2::signedRational);
502 catch (Exiv2::AnyError& e) {
503 std::cout << "Caught Exiv2 exception '" << e << "'\n";
508 const gchar *exif_get_tag_description_by_key(const gchar *key)
511 Exiv2::ExifKey ekey(key);
512 return Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ());
514 catch (Exiv2::AnyError& e) {
515 std::cout << "Caught Exiv2 exception '" << e << "'\n";
520 int exif_item_set_string(ExifItem *item, const char *str)
524 ((Exiv2::Metadatum *)item)->setValue(std::string(str));
527 catch (Exiv2::AnyError& e) {
532 int exif_item_delete(ExifData *exif, ExifItem *item)
536 for (Exiv2::ExifData::iterator i = exif->exifData().begin(); i != exif->exifData().end(); ++i) {
537 if (((Exiv2::Metadatum *)item) == &*i) {
538 i = exif->exifData().erase(i);
542 for (Exiv2::IptcData::iterator i = exif->iptcData().begin(); i != exif->iptcData().end(); ++i) {
543 if (((Exiv2::Metadatum *)item) == &*i) {
544 i = exif->iptcData().erase(i);
548 #if EXIV2_TEST_VERSION(0,16,0)
549 for (Exiv2::XmpData::iterator i = exif->xmpData().begin(); i != exif->xmpData().end(); ++i) {
550 if (((Exiv2::Metadatum *)item) == &*i) {
551 i = exif->xmpData().erase(i);
558 catch (Exiv2::AnyError& e) {
563 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
565 if (exif->cp_data) g_free(exif->cp_data);
566 exif->cp_data = cp_data;
567 exif->cp_length =cp_length;
570 unsigned char *exif_get_color_profile(ExifData *exif, guint *data_len)
574 if (data_len) *data_len = exif->cp_length;
575 return (unsigned char *) g_memdup(exif->cp_data, exif->cp_length);
577 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
578 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
579 return (unsigned char *) exif_item_get_data(prof_item, data_len);
587 /* This is a dirty hack to support raw file preview, bassed on
588 tiffparse.cpp from Exiv2 examples */
596 const Exiv2::Value *find(uint16_t tag, uint16_t group);
598 unsigned long preview_offset();
602 Exiv2::TiffComponent::AutoPtr rootDir;
603 Exiv2::byte *map_data;
605 unsigned long offset;
608 using namespace Exiv2;
610 RawFile::RawFile(int fd) : map_data(NULL), map_len(0), offset(0)
613 if (fstat(fd, &st) == -1)
617 map_len = st.st_size;
618 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
619 if (map_data == MAP_FAILED)
623 type = Exiv2::ImageFactory::getType(map_data, map_len);
625 #if EXIV2_TEST_VERSION(0,16,0)
626 TiffHeaderBase *tiffHeader = NULL;
628 TiffHeade2 *tiffHeader = NULL;
630 Cr2Header *cr2Header = NULL;
633 case Exiv2::ImageType::tiff:
634 tiffHeader = new TiffHeade2();
636 case Exiv2::ImageType::cr2:
637 cr2Header = new Cr2Header();
639 #if EXIV2_TEST_VERSION(0,16,0)
640 case Exiv2::ImageType::orf:
641 tiffHeader = new OrfHeader();
644 #if EXIV2_TEST_VERSION(0,13,0)
645 case Exiv2::ImageType::raf:
646 if (map_len < 84 + 4) throw Error(14);
647 offset = getULong(map_data + 84, bigEndian);
650 case Exiv2::ImageType::crw:
652 // Parse the image, starting with a CIFF header component
653 Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
654 parseTree->read(map_data, map_len);
655 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
656 if (entry) offset = entry->pData() - map_data;
661 throw Error(3, "RAW");
664 // process tiff-like formats
666 TiffCompFactoryFct createFct = TiffCreator::create;
668 rootDir = createFct(Tag::root, Group::none);
669 if (0 == rootDir.get()) {
670 throw Error(1, "No root element defined in TIFF structure");
675 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
676 #if EXIV2_TEST_VERSION(0,16,0)
677 rootDir->setStart(map_data + tiffHeader->offset());
679 rootDir->setStart(map_data + tiffHeader->ifdOffset());
685 rootDir->setStart(map_data + cr2Header->offset());
688 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
690 TiffReader reader(map_data,
695 rootDir->accept(reader);
705 if (map_data && munmap(map_data, map_len) == -1)
707 printf("Failed to unmap file \n");
711 const Value * RawFile::find(uint16_t tag, uint16_t group)
713 TiffFinder finder(tag, group);
714 rootDir->accept(finder);
715 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
718 DEBUG_1("(tag: %04x %04x) ", tag, group);
725 unsigned long RawFile::preview_offset()
728 if (offset) return offset;
730 if (type == Exiv2::ImageType::cr2)
732 val = find(0x111, Group::ifd0);
733 if (val) return val->toLong();
738 val = find(0x201, Group::sub0_0);
739 if (val) return val->toLong();
741 val = find(0x201, Group::ifd0);
742 if (val) return val->toLong();
744 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
745 if (val) return val->toLong();
747 val = find(0x111, Group::sub0_1); // dng
748 if (val) return val->toLong();
754 extern "C" gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
755 unsigned char *header_data, const guint header_len,
756 guint *image_offset, guint *exif_offset)
759 unsigned long offset;
761 /* given image pathname, first do simple (and fast) file extension test */
762 if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return 0;
766 offset = rf.preview_offset();
767 DEBUG_1("%s: offset %lu\n", path, offset);
769 catch (Exiv2::AnyError& e) {
770 std::cout << "Caught Exiv2 exception '" << e << "'\n";
774 if (image_offset && offset > 0)
776 *image_offset = offset;
777 if ((unsigned long) lseek(fd, *image_offset, SEEK_SET) != *image_offset)
779 printf("Failed to seek to embedded image\n");
782 if (*exif_offset) *exif_offset = 0;