3 * Copyright (C) 2008 - 2012 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>
51 #if EXIV2_TEST_VERSION(0,17,0)
52 #include <exiv2/convert.hpp>
53 #include <exiv2/xmpsidecar.hpp>
62 #include "filefilter.h"
63 #include "ui_fileops.h"
68 typedef struct _AltKey AltKey;
73 const gchar *exif_key;
74 const gchar *iptc_key;
77 /* this is a list of keys that should be converted, even with the older Exiv2 which does not support it directly */
78 static const AltKey alt_keys[] = {
79 {"Xmp.tiff.Orientation", "Exif.Image.Orientation", NULL},
80 {"Xmp.dc.title", NULL, "Iptc.Application2.ObjectName" },
81 {"Xmp.photoshop.Urgency", NULL, "Iptc.Application2.Urgency" },
82 {"Xmp.photoshop.Category", NULL, "Iptc.Application2.Category" },
83 {"Xmp.photoshop.SupplementalCategory", NULL, "Iptc.Application2.SuppCategory" },
84 {"Xmp.dc.subject", NULL, "Iptc.Application2.Keywords" },
85 {"Xmp.iptc.Location", NULL, "Iptc.Application2.LocationName" },
86 {"Xmp.photoshop.Instruction", NULL, "Iptc.Application2.SpecialInstructions" },
87 {"Xmp.photoshop.DateCreated", NULL, "Iptc.Application2.DateCreated" },
88 {"Xmp.dc.creator", NULL, "Iptc.Application2.Byline" },
89 {"Xmp.photoshop.AuthorsPosition", NULL, "Iptc.Application2.BylineTitle" },
90 {"Xmp.photoshop.City", NULL, "Iptc.Application2.City" },
91 {"Xmp.photoshop.State", NULL, "Iptc.Application2.ProvinceState" },
92 {"Xmp.iptc.CountryCode", NULL, "Iptc.Application2.CountryCode" },
93 {"Xmp.photoshop.Country", NULL, "Iptc.Application2.CountryName" },
94 {"Xmp.photoshop.TransmissionReference", NULL, "Iptc.Application2.TransmissionReference"},
95 {"Xmp.photoshop.Headline", NULL, "Iptc.Application2.Headline" },
96 {"Xmp.photoshop.Credit", NULL, "Iptc.Application2.Credit" },
97 {"Xmp.photoshop.Source", NULL, "Iptc.Application2.Source" },
98 {"Xmp.dc.rights", NULL, "Iptc.Application2.Copyright" },
99 {"Xmp.dc.description", NULL, "Iptc.Application2.Caption" },
100 {"Xmp.photoshop.CaptionWriter", NULL, "Iptc.Application2.Writer" },
104 static void _debug_exception(const char* file,
109 gchar *str = g_locale_from_utf8(e.what(), -1, NULL, NULL, NULL);
110 DEBUG_1("%s:%d:%s:Exiv2: %s", file, line, func, str);
114 #define debug_exception(e) _debug_exception(__FILE__, __LINE__, __func__, e)
118 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
119 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
120 #if EXIV2_TEST_VERSION(0,16,0)
121 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
128 virtual void writeMetadata(gchar *path = NULL)
130 g_critical("Unsupported method of writing metadata");
133 virtual ExifData *original()
138 virtual Exiv2::Image *image() = 0;
140 virtual Exiv2::ExifData &exifData() = 0;
142 virtual Exiv2::IptcData &iptcData() = 0;
144 #if EXIV2_TEST_VERSION(0,16,0)
145 virtual Exiv2::XmpData &xmpData() = 0;
148 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) = 0;
150 virtual guchar *get_jpeg_color_profile(guint *data_len) = 0;
153 // This allows read-only access to the original metadata
154 struct _ExifDataOriginal : public _ExifData
157 Exiv2::Image::AutoPtr image_;
159 /* the icc profile in jpeg is not technically exif - store it here */
160 unsigned char *cp_data_;
164 Exiv2::ExifData emptyExifData_;
165 Exiv2::IptcData emptyIptcData_;
166 #if EXIV2_TEST_VERSION(0,16,0)
167 Exiv2::XmpData emptyXmpData_;
171 _ExifDataOriginal(Exiv2::Image::AutoPtr image)
179 _ExifDataOriginal(gchar *path)
185 gchar *pathl = path_from_utf8(path);
188 image_ = Exiv2::ImageFactory::open(pathl);
189 // g_assert (image.get() != 0);
190 image_->readMetadata();
192 #if EXIV2_TEST_VERSION(0,16,0)
193 if (image_->mimeType() == "application/rdf+xml")
195 //Exiv2 sidecar converts xmp to exif and iptc, we don't want it.
196 image_->clearExifData();
197 image_->clearIptcData();
201 #if EXIV2_TEST_VERSION(0,14,0)
202 if (image_->mimeType() == "image/jpeg")
204 /* try to get jpeg color profile */
205 Exiv2::BasicIo &io = image_->io();
206 gint open = io.isopen();
207 if (!open) io.open();
210 unsigned char *mapped = (unsigned char*)io.mmap();
211 if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
214 if (!open) io.close();
218 catch (Exiv2::AnyError& e)
225 virtual ~_ExifDataOriginal()
227 if (cp_data_) g_free(cp_data_);
230 virtual Exiv2::Image *image()
232 if (!valid_) return NULL;
236 virtual Exiv2::ExifData &exifData ()
238 if (!valid_) return emptyExifData_;
239 return image_->exifData();
242 virtual Exiv2::IptcData &iptcData ()
244 if (!valid_) return emptyIptcData_;
245 return image_->iptcData();
248 #if EXIV2_TEST_VERSION(0,16,0)
249 virtual Exiv2::XmpData &xmpData ()
251 if (!valid_) return emptyXmpData_;
252 return image_->xmpData();
256 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
258 if (cp_data_) g_free(cp_data_);
260 cp_length_ = cp_length;
263 virtual guchar *get_jpeg_color_profile(guint *data_len)
267 if (data_len) *data_len = cp_length_;
268 return (unsigned char *) g_memdup(cp_data_, cp_length_);
275 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data);
278 // This allows read-write access to the metadata
279 struct _ExifDataProcessed : public _ExifData
282 _ExifDataOriginal *imageData_;
283 _ExifDataOriginal *sidecarData_;
285 Exiv2::ExifData exifData_;
286 Exiv2::IptcData iptcData_;
287 #if EXIV2_TEST_VERSION(0,16,0)
288 Exiv2::XmpData xmpData_;
292 _ExifDataProcessed(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
294 imageData_ = new _ExifDataOriginal(path);
296 #if EXIV2_TEST_VERSION(0,16,0)
299 sidecarData_ = new _ExifDataOriginal(sidecar_path);
300 xmpData_ = sidecarData_->xmpData();
304 xmpData_ = imageData_->xmpData();
308 exifData_ = imageData_->exifData();
309 iptcData_ = imageData_->iptcData();
310 #if EXIV2_TEST_VERSION(0,17,0)
313 syncExifWithXmp(exifData_, xmpData_);
317 DEBUG_1("Exiv2: Catching bug\n");
322 g_hash_table_foreach(modified_xmp, _ExifDataProcessed_update_xmp, this);
326 virtual ~_ExifDataProcessed()
328 if (imageData_) delete imageData_;
329 if (sidecarData_) delete sidecarData_;
332 virtual ExifData *original()
337 virtual void writeMetadata(gchar *path = NULL)
341 #if EXIV2_TEST_VERSION(0,17,0)
342 if (options->metadata.save_legacy_IPTC)
343 copyXmpToIptc(xmpData_, iptcData_);
347 copyXmpToExif(xmpData_, exifData_);
349 Exiv2::Image *image = imageData_->image();
351 if (!image) Exiv2::Error(21);
352 image->setExifData(exifData_);
353 image->setIptcData(iptcData_);
354 #if EXIV2_TEST_VERSION(0,16,0)
355 image->setXmpData(xmpData_);
357 image->writeMetadata();
361 #if EXIV2_TEST_VERSION(0,17,0)
362 gchar *pathl = path_from_utf8(path);;
364 Exiv2::Image::AutoPtr sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
368 sidecar->setXmpData(xmpData_);
369 sidecar->writeMetadata();
371 throw Exiv2::Error(3, "xmp");
376 virtual Exiv2::Image *image()
378 return imageData_->image();
381 virtual Exiv2::ExifData &exifData ()
386 virtual Exiv2::IptcData &iptcData ()
391 #if EXIV2_TEST_VERSION(0,16,0)
392 virtual Exiv2::XmpData &xmpData ()
398 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
400 imageData_->add_jpeg_color_profile(cp_data, cp_length);
403 virtual guchar *get_jpeg_color_profile(guint *data_len)
405 return imageData_->get_jpeg_color_profile(data_len);
417 #ifdef EXV_ENABLE_NLS
418 bind_textdomain_codeset (EXV_PACKAGE, "UTF-8");
424 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data)
426 exif_update_metadata((ExifData *)data, (gchar *)key, (GList *)value);
429 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
431 DEBUG_1("exif read %s, sidecar: %s", path, sidecar_path ? sidecar_path : "-");
433 return new _ExifDataProcessed(path, sidecar_path, modified_xmp);
435 catch (Exiv2::AnyError& e) {
442 gboolean exif_write(ExifData *exif)
445 exif->writeMetadata();
448 catch (Exiv2::AnyError& e) {
454 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
457 exif->writeMetadata(path);
460 catch (Exiv2::AnyError& e) {
468 void exif_free(ExifData *exif)
471 g_assert(dynamic_cast<_ExifDataProcessed *>(exif)); // this should not be called on ExifDataOriginal
475 ExifData *exif_get_original(ExifData *exif)
477 return exif->original();
481 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
484 Exiv2::Metadatum *item = NULL;
486 Exiv2::ExifKey ekey(key);
487 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
488 if (pos == exif->exifData().end()) return NULL;
491 catch (Exiv2::AnyError& e) {
493 Exiv2::IptcKey ekey(key);
494 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
495 if (pos == exif->iptcData().end()) return NULL;
498 catch (Exiv2::AnyError& e) {
499 #if EXIV2_TEST_VERSION(0,16,0)
500 Exiv2::XmpKey ekey(key);
501 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
502 if (pos == exif->xmpData().end()) return NULL;
507 return (ExifItem *)item;
509 catch (Exiv2::AnyError& e) {
515 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
518 Exiv2::Metadatum *item = NULL;
520 Exiv2::ExifKey ekey(key);
521 exif->exifData().add(ekey, NULL);
522 Exiv2::ExifData::iterator pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
526 catch (Exiv2::AnyError& e) {
528 Exiv2::IptcKey ekey(key);
529 exif->iptcData().add(ekey, NULL);
530 Exiv2::IptcData::iterator pos = exif->iptcData().end();
534 catch (Exiv2::AnyError& e) {
535 #if EXIV2_TEST_VERSION(0,16,0)
536 Exiv2::XmpKey ekey(key);
537 exif->xmpData().add(ekey, NULL);
538 Exiv2::XmpData::iterator pos = exif->xmpData().end();
544 return (ExifItem *)item;
546 catch (Exiv2::AnyError& e) {
553 ExifItem *exif_get_first_item(ExifData *exif)
556 exif->exifIter = exif->exifData().begin();
557 exif->iptcIter = exif->iptcData().begin();
558 #if EXIV2_TEST_VERSION(0,16,0)
559 exif->xmpIter = exif->xmpData().begin();
561 if (exif->exifIter != exif->exifData().end())
563 const Exiv2::Metadatum *item = &*exif->exifIter;
565 return (ExifItem *)item;
567 if (exif->iptcIter != exif->iptcData().end())
569 const Exiv2::Metadatum *item = &*exif->iptcIter;
571 return (ExifItem *)item;
573 #if EXIV2_TEST_VERSION(0,16,0)
574 if (exif->xmpIter != exif->xmpData().end())
576 const Exiv2::Metadatum *item = &*exif->xmpIter;
578 return (ExifItem *)item;
584 catch (Exiv2::AnyError& e) {
590 ExifItem *exif_get_next_item(ExifData *exif)
593 if (exif->exifIter != exif->exifData().end())
595 const Exiv2::Metadatum *item = &*exif->exifIter;
597 return (ExifItem *)item;
599 if (exif->iptcIter != exif->iptcData().end())
601 const Exiv2::Metadatum *item = &*exif->iptcIter;
603 return (ExifItem *)item;
605 #if EXIV2_TEST_VERSION(0,16,0)
606 if (exif->xmpIter != exif->xmpData().end())
608 const Exiv2::Metadatum *item = &*exif->xmpIter;
610 return (ExifItem *)item;
615 catch (Exiv2::AnyError& e) {
621 char *exif_item_get_tag_name(ExifItem *item)
624 if (!item) return NULL;
625 return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
627 catch (Exiv2::AnyError& e) {
633 guint exif_item_get_tag_id(ExifItem *item)
637 return ((Exiv2::Metadatum *)item)->tag();
639 catch (Exiv2::AnyError& e) {
645 guint exif_item_get_elements(ExifItem *item)
649 return ((Exiv2::Metadatum *)item)->count();
651 catch (Exiv2::AnyError& e) {
657 char *exif_item_get_data(ExifItem *item, guint *data_len)
661 Exiv2::Metadatum *md = (Exiv2::Metadatum *)item;
662 if (data_len) *data_len = md->size();
663 char *data = (char *)g_malloc(md->size());
664 long res = md->copy((Exiv2::byte *)data, Exiv2::littleEndian /* should not matter */);
665 g_assert(res == md->size());
668 catch (Exiv2::AnyError& e) {
674 char *exif_item_get_description(ExifItem *item)
677 if (!item) return NULL;
678 return utf8_validate_or_convert(((Exiv2::Metadatum *)item)->tagLabel().c_str());
680 catch (std::exception& e) {
681 // debug_exception(e);
687 invalidTypeId, unsignedByte, asciiString, unsignedShort,
688 unsignedLong, unsignedRational, signedByte, undefined,
689 signedShort, signedLong, signedRational, string,
690 date, time, comment, directory,
691 xmpText, xmpAlt, xmpBag, xmpSeq,
695 static guint format_id_trans_tbl [] = {
697 EXIF_FORMAT_BYTE_UNSIGNED,
699 EXIF_FORMAT_SHORT_UNSIGNED,
700 EXIF_FORMAT_LONG_UNSIGNED,
701 EXIF_FORMAT_RATIONAL_UNSIGNED,
703 EXIF_FORMAT_UNDEFINED,
706 EXIF_FORMAT_RATIONAL,
710 EXIF_FORMAT_UNDEFINED,
719 guint exif_item_get_format_id(ExifItem *item)
722 if (!item) return EXIF_FORMAT_UNKNOWN;
723 guint id = ((Exiv2::Metadatum *)item)->typeId();
724 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
725 return format_id_trans_tbl[id];
727 catch (Exiv2::AnyError& e) {
729 return EXIF_FORMAT_UNKNOWN;
733 const char *exif_item_get_format_name(ExifItem *item, gboolean brief)
736 if (!item) return NULL;
737 return ((Exiv2::Metadatum *)item)->typeName();
739 catch (Exiv2::AnyError& e) {
746 gchar *exif_item_get_data_as_text(ExifItem *item)
749 if (!item) return NULL;
750 Exiv2::Metadatum *metadatum = (Exiv2::Metadatum *)item;
751 #if EXIV2_TEST_VERSION(0,17,0)
752 return utf8_validate_or_convert(metadatum->print().c_str());
754 std::stringstream str;
755 Exiv2::Exifdatum *exifdatum;
756 Exiv2::Iptcdatum *iptcdatum;
757 #if EXIV2_TEST_VERSION(0,16,0)
758 Exiv2::Xmpdatum *xmpdatum;
760 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(metadatum)))
762 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(metadatum)))
764 #if EXIV2_TEST_VERSION(0,16,0)
765 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(metadatum)))
769 return utf8_validate_or_convert(str.str().c_str());
772 catch (Exiv2::AnyError& e) {
777 gchar *exif_item_get_string(ExifItem *item, int idx)
780 if (!item) return NULL;
781 Exiv2::Metadatum *em = (Exiv2::Metadatum *)item;
782 #if EXIV2_TEST_VERSION(0,16,0)
783 std::string str = em->toString(idx);
785 std::string str = em->toString(); // FIXME
787 if (idx == 0 && str == "") str = em->toString();
788 if (str.length() > 5 && str.substr(0, 5) == "lang=")
790 std::string::size_type pos = str.find_first_of(' ');
791 if (pos != std::string::npos) str = str.substr(pos+1);
794 return utf8_validate_or_convert(str.c_str());
796 catch (Exiv2::AnyError& e) {
802 gint exif_item_get_integer(ExifItem *item, gint *value)
805 if (!item || exif_item_get_elements(item) == 0) return 0;
806 *value = ((Exiv2::Metadatum *)item)->toLong();
809 catch (Exiv2::AnyError& e) {
815 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
818 if (!item) return NULL;
819 if (n >= exif_item_get_elements(item)) return NULL;
820 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational(n);
821 static ExifRational ret;
824 if (sign) *sign = (((Exiv2::Metadatum *)item)->typeId() == Exiv2::signedRational);
827 catch (Exiv2::AnyError& e) {
833 gchar *exif_get_tag_description_by_key(const gchar *key)
836 Exiv2::ExifKey ekey(key);
837 return utf8_validate_or_convert(ekey.tagLabel().c_str());
839 catch (Exiv2::AnyError& e) {
841 Exiv2::IptcKey ikey(key);
842 return utf8_validate_or_convert(ikey.tagLabel().c_str());
844 catch (Exiv2::AnyError& e) {
846 #if EXIV2_TEST_VERSION(0,16,0)
847 Exiv2::XmpKey xkey(key);
848 return utf8_validate_or_convert(xkey.tagLabel().c_str());
851 catch (Exiv2::AnyError& e) {
860 static const AltKey *find_alt_key(const gchar *xmp_key)
864 while (alt_keys[i].xmp_key)
866 if (strcmp(xmp_key, alt_keys[i].xmp_key) == 0) return &alt_keys[i];
872 static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const GList *values)
875 const GList *work = values;
878 Exiv2::ExifKey ekey(key);
880 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
881 while (pos != exif->exifData().end())
883 exif->exifData().erase(pos);
884 pos = exif->exifData().findKey(ekey);
889 exif->exifData()[key] = (gchar *)work->data;
893 catch (Exiv2::AnyError& e) {
894 #if EXIV2_TEST_VERSION(0,16,0)
898 Exiv2::IptcKey ekey(key);
899 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
900 while (pos != exif->iptcData().end())
902 exif->iptcData().erase(pos);
903 pos = exif->iptcData().findKey(ekey);
908 exif->iptcData()[key] = (gchar *)work->data;
912 #if EXIV2_TEST_VERSION(0,16,0)
913 catch (Exiv2::AnyError& e) {
914 Exiv2::XmpKey ekey(key);
915 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
916 while (pos != exif->xmpData().end())
918 exif->xmpData().erase(pos);
919 pos = exif->xmpData().findKey(ekey);
924 exif->xmpData()[key] = (gchar *)work->data;
932 catch (Exiv2::AnyError& e) {
938 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
940 gint ret = exif_update_metadata_simple(exif, key, values);
943 #if !EXIV2_TEST_VERSION(0,17,0)
944 TRUE || /* no conversion support */
946 !values || /* deleting item */
947 !ret /* writing to the explicitely given xmp tag failed */
950 /* deleted xmp metadatum can't be converted, we have to delete also the corresponding legacy tag */
951 /* if we can't write xmp, update at least the legacy tag */
952 const AltKey *alt_key = find_alt_key(key);
953 if (alt_key && alt_key->iptc_key)
954 ret = exif_update_metadata_simple(exif, alt_key->iptc_key, values);
956 if (alt_key && alt_key->exif_key)
957 ret = exif_update_metadata_simple(exif, alt_key->exif_key, values);
963 static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, MetadataFormat format, const Exiv2::ExifData *metadata)
965 #if EXIV2_TEST_VERSION(0,16,0)
966 Exiv2::TypeId id = item.typeId();
967 if (format == METADATA_FORMATTED ||
968 id == Exiv2::asciiString ||
969 id == Exiv2::undefined ||
970 id == Exiv2::string ||
973 id == Exiv2::xmpText ||
974 id == Exiv2::langAlt ||
979 /* read as a single entry */
982 if (format == METADATA_FORMATTED)
984 #if EXIV2_TEST_VERSION(0,17,0)
986 #if EXIV2_TEST_VERSION(0,18,0)
991 std::stringstream stream;
992 Exiv2::Exifdatum *exifdatum;
993 Exiv2::Iptcdatum *iptcdatum;
994 #if EXIV2_TEST_VERSION(0,16,0)
995 Exiv2::Xmpdatum *xmpdatum;
997 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(&item)))
998 stream << *exifdatum;
999 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(&item)))
1000 stream << *iptcdatum;
1001 #if EXIV2_TEST_VERSION(0,16,0)
1002 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(&item)))
1003 stream << *xmpdatum;
1007 if (str.length() > 1024)
1009 /* truncate very long strings, they cause problems in gui */
1016 str = item.toString();
1018 if (str.length() > 5 && str.substr(0, 5) == "lang=")
1020 std::string::size_type pos = str.find_first_of(' ');
1021 if (pos != std::string::npos) str = str.substr(pos+1);
1023 list = g_list_append(list, utf8_validate_or_convert(str.c_str()));
1024 #if EXIV2_TEST_VERSION(0,16,0)
1028 /* read as a list */
1030 for (i = 0; i < item.count(); i++)
1031 list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str()));
1037 static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, MetadataFormat format)
1042 Exiv2::ExifKey ekey(key);
1043 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
1044 if (pos != exif->exifData().end())
1045 list = exif_add_value_to_glist(list, *pos, format, &exif->exifData());
1048 catch (Exiv2::AnyError& e) {
1050 Exiv2::IptcKey ekey(key);
1051 Exiv2::IptcData::iterator pos = exif->iptcData().begin();
1052 while (pos != exif->iptcData().end())
1054 if (pos->key() == key)
1055 list = exif_add_value_to_glist(list, *pos, format, NULL);
1060 catch (Exiv2::AnyError& e) {
1061 #if EXIV2_TEST_VERSION(0,16,0)
1062 Exiv2::XmpKey ekey(key);
1063 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
1064 if (pos != exif->xmpData().end())
1065 list = exif_add_value_to_glist(list, *pos, format, NULL);
1070 catch (Exiv2::AnyError& e) {
1076 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1080 if (!key) return NULL;
1082 if (format == METADATA_FORMATTED)
1086 text = exif_get_formatted_by_key(exif, key, &key_valid);
1087 if (key_valid) return g_list_append(NULL, text);
1090 list = exif_get_metadata_simple(exif, key, format);
1092 /* the following code can be ifdefed out as soon as Exiv2 supports it */
1095 const AltKey *alt_key = find_alt_key(key);
1096 if (alt_key && alt_key->iptc_key)
1097 list = exif_get_metadata_simple(exif, alt_key->iptc_key, format);
1099 #if !EXIV2_TEST_VERSION(0,17,0)
1100 /* with older Exiv2 versions exif is not synced */
1101 if (!list && alt_key && alt_key->exif_key)
1102 list = exif_get_metadata_simple(exif, alt_key->exif_key, format);
1109 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
1111 exif->add_jpeg_color_profile(cp_data, cp_length);
1114 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1116 guchar *ret = exif->get_jpeg_color_profile(data_len);
1117 if (ret) return ret;
1119 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1120 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1121 ret = (guchar *)exif_item_get_data(prof_item, data_len);
1125 #if EXIV2_TEST_VERSION(0,17,90)
1127 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1129 if (!exif) return NULL;
1131 if (!exif->image()) return NULL;
1133 const char* path = exif->image()->io().path().c_str();
1134 /* given image pathname, first do simple (and fast) file extension test */
1135 gboolean is_raw = filter_file_class(path, FORMAT_CLASS_RAWIMAGE);
1137 if (!is_raw && requested_width == 0) return NULL;
1141 Exiv2::PreviewManager pm(*exif->image());
1143 Exiv2::PreviewPropertiesList list = pm.getPreviewProperties();
1147 Exiv2::PreviewPropertiesList::iterator pos;
1148 Exiv2::PreviewPropertiesList::iterator last = --list.end();
1150 if (requested_width == 0)
1152 pos = last; // the largest
1159 if (pos->width_ >= (uint32_t)requested_width &&
1160 pos->height_ >= (uint32_t)requested_height) break;
1164 // we are not interested in smaller thumbnails in normal image formats - we can use full image instead
1167 if (pos->width_ < (uint32_t)requested_width || pos->height_ < (uint32_t)requested_height) return NULL;
1171 Exiv2::PreviewImage image = pm.getPreviewImage(*pos);
1173 Exiv2::DataBuf buf = image.copy();
1174 std::pair<Exiv2::byte*, long> p = buf.release();
1176 *data_len = p.second;
1181 catch (Exiv2::AnyError& e) {
1187 void exif_free_preview(guchar *buf)
1189 delete[] (Exiv2::byte*)buf;
1194 #if !EXIV2_TEST_VERSION(0,17,90)
1196 /* This is a dirty hack to support raw file preview, bassed on
1197 tiffparse.cpp from Exiv2 examples */
1202 RawFile(Exiv2::BasicIo &io);
1205 const Exiv2::Value *find(uint16_t tag, uint16_t group);
1207 unsigned long preview_offset();
1211 Exiv2::TiffComponent::AutoPtr rootDir;
1212 Exiv2::BasicIo &io_;
1213 const Exiv2::byte *map_data;
1215 unsigned long offset;
1218 typedef struct _UnmapData UnmapData;
1226 static GList *exif_unmap_list = 0;
1228 extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1230 unsigned long offset;
1232 if (!exif) return NULL;
1233 if (!exif->image()) return NULL;
1235 const char* path = exif->image()->io().path().c_str();
1237 /* given image pathname, first do simple (and fast) file extension test */
1238 if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return NULL;
1247 RawFile rf(exif->image()->io());
1248 offset = rf.preview_offset();
1249 DEBUG_1("%s: offset %lu", path, offset);
1251 fd = open(path, O_RDONLY);
1257 if (fstat(fd, &st) == -1)
1262 map_len = st.st_size;
1263 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1265 if (map_data == MAP_FAILED)
1269 *data_len = map_len - offset;
1270 ud = g_new(UnmapData, 1);
1271 ud->ptr = map_data + offset;
1272 ud->map_data = map_data;
1273 ud->map_len = map_len;
1275 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1279 catch (Exiv2::AnyError& e) {
1286 void exif_free_preview(guchar *buf)
1288 GList *work = exif_unmap_list;
1292 UnmapData *ud = (UnmapData *)work->data;
1295 munmap(ud->map_data, ud->map_len);
1296 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1302 g_assert_not_reached();
1305 using namespace Exiv2;
1307 RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
1311 if (fstat(fd, &st) == -1)
1315 map_len = st.st_size;
1316 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1317 if (map_data == MAP_FAILED)
1322 if (io.open() != 0) {
1323 throw Error(9, io.path(), strError());
1326 map_data = io.mmap();
1327 map_len = io.size();
1330 type = Exiv2::ImageFactory::getType(map_data, map_len);
1332 #if EXIV2_TEST_VERSION(0,16,0)
1333 TiffHeaderBase *tiffHeader = NULL;
1335 TiffHeade2 *tiffHeader = NULL;
1337 Cr2Header *cr2Header = NULL;
1340 case Exiv2::ImageType::tiff:
1341 tiffHeader = new TiffHeade2();
1343 case Exiv2::ImageType::cr2:
1344 cr2Header = new Cr2Header();
1346 #if EXIV2_TEST_VERSION(0,16,0)
1347 case Exiv2::ImageType::orf:
1348 tiffHeader = new OrfHeader();
1351 #if EXIV2_TEST_VERSION(0,13,0)
1352 case Exiv2::ImageType::raf:
1353 if (map_len < 84 + 4) throw Error(14);
1354 offset = getULong(map_data + 84, bigEndian);
1357 case Exiv2::ImageType::crw:
1359 // Parse the image, starting with a CIFF header component
1360 Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
1361 parseTree->read(map_data, map_len);
1362 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
1363 if (entry) offset = entry->pData() - map_data;
1368 throw Error(3, "RAW");
1371 // process tiff-like formats
1373 TiffCompFactoryFct createFct = TiffCreator::create;
1375 rootDir = createFct(Tag::root, Group::none);
1376 if (0 == rootDir.get()) {
1377 throw Error(1, "No root element defined in TIFF structure");
1382 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
1383 #if EXIV2_TEST_VERSION(0,16,0)
1384 rootDir->setStart(map_data + tiffHeader->offset());
1386 rootDir->setStart(map_data + tiffHeader->ifdOffset());
1392 rootDir->setStart(map_data + cr2Header->offset());
1395 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
1397 TiffReader reader(map_data,
1402 rootDir->accept(reader);
1410 RawFile::~RawFile(void)
1416 const Value * RawFile::find(uint16_t tag, uint16_t group)
1418 TiffFinder finder(tag, group);
1419 rootDir->accept(finder);
1420 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
1423 DEBUG_1("(tag: %04x %04x) ", tag, group);
1424 return te->pValue();
1430 unsigned long RawFile::preview_offset(void)
1433 if (offset) return offset;
1435 if (type == Exiv2::ImageType::cr2)
1437 val = find(0x111, Group::ifd0);
1438 if (val) return val->toLong();
1443 val = find(0x201, Group::sub0_0);
1444 if (val) return val->toLong();
1446 val = find(0x201, Group::ifd0);
1447 if (val) return val->toLong();
1449 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
1450 if (val) return val->toLong();
1452 val = find(0x111, Group::sub0_1); // dng
1453 if (val) return val->toLong();
1464 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */