2 * Copyright (C) 2008 - 2016 The Geeqie Team
4 * Author: Vladimir Nadvornik
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 // Don't include the <exiv2/version.hpp> file directly
26 // Early Exiv2 versions didn't have version.hpp and the macros.
27 #include <exiv2/exiv2.hpp>
31 // EXIV2_TEST_VERSION is defined in Exiv2 0.15 and newer.
33 #ifndef EXIV2_TEST_VERSION
34 #define EXIV2_TEST_VERSION(major,minor,patch) \
35 ( EXIV2_VERSION >= EXIV2_MAKE_VERSION(major,minor,patch) )
38 #define EXIV2_TEST_VERSION(major,minor,patch) (false)
41 #if EXIV2_TEST_VERSION(0,27,0)
42 #define HAVE_EXIV2_ERROR_CODE
45 #include <sys/types.h>
51 #if EXIV2_TEST_VERSION(0,27,0)
52 #define EXV_PACKAGE "exiv2"
60 #include "filefilter.h"
61 #include "ui-fileops.h"
65 #if EXIV2_TEST_VERSION(0,28,0)
66 #define AnyError Error
67 #define AutoPtr UniquePtr
70 typedef struct _AltKey AltKey;
75 const gchar *exif_key;
76 const gchar *iptc_key;
79 /* this is a list of keys that should be converted, even with the older Exiv2 which does not support it directly */
80 static const AltKey alt_keys[] = {
81 {"Xmp.tiff.Orientation", "Exif.Image.Orientation", NULL},
82 {"Xmp.dc.title", NULL, "Iptc.Application2.ObjectName" },
83 {"Xmp.photoshop.Urgency", NULL, "Iptc.Application2.Urgency" },
84 {"Xmp.photoshop.Category", NULL, "Iptc.Application2.Category" },
85 {"Xmp.photoshop.SupplementalCategory", NULL, "Iptc.Application2.SuppCategory" },
86 {"Xmp.dc.subject", NULL, "Iptc.Application2.Keywords" },
87 {"Xmp.iptc.Location", NULL, "Iptc.Application2.LocationName" },
88 {"Xmp.photoshop.Instruction", NULL, "Iptc.Application2.SpecialInstructions" },
89 {"Xmp.photoshop.DateCreated", NULL, "Iptc.Application2.DateCreated" },
90 {"Xmp.dc.creator", NULL, "Iptc.Application2.Byline" },
91 {"Xmp.photoshop.AuthorsPosition", NULL, "Iptc.Application2.BylineTitle" },
92 {"Xmp.photoshop.City", NULL, "Iptc.Application2.City" },
93 {"Xmp.photoshop.State", NULL, "Iptc.Application2.ProvinceState" },
94 {"Xmp.iptc.CountryCode", NULL, "Iptc.Application2.CountryCode" },
95 {"Xmp.photoshop.Country", NULL, "Iptc.Application2.CountryName" },
96 {"Xmp.photoshop.TransmissionReference", NULL, "Iptc.Application2.TransmissionReference"},
97 {"Xmp.photoshop.Headline", NULL, "Iptc.Application2.Headline" },
98 {"Xmp.photoshop.Credit", NULL, "Iptc.Application2.Credit" },
99 {"Xmp.photoshop.Source", NULL, "Iptc.Application2.Source" },
100 {"Xmp.dc.rights", NULL, "Iptc.Application2.Copyright" },
101 {"Xmp.dc.description", NULL, "Iptc.Application2.Caption" },
102 {"Xmp.photoshop.CaptionWriter", NULL, "Iptc.Application2.Writer" },
106 static void _debug_exception(const char* file,
111 gchar *str = g_locale_from_utf8(e.what(), -1, NULL, NULL, NULL);
112 DEBUG_1("%s:%d:%s:Exiv2: %s", file, line, func, str);
116 #define debug_exception(e) _debug_exception(__FILE__, __LINE__, __func__, e)
120 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
121 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
122 #if EXIV2_TEST_VERSION(0,16,0)
123 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
130 virtual void writeMetadata(gchar *UNUSED(path) = NULL)
132 g_critical("Unsupported method of writing metadata");
135 virtual ExifData *original()
140 virtual Exiv2::Image *image() = 0;
142 virtual Exiv2::ExifData &exifData() = 0;
144 virtual Exiv2::IptcData &iptcData() = 0;
146 #if EXIV2_TEST_VERSION(0,16,0)
147 virtual Exiv2::XmpData &xmpData() = 0;
150 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) = 0;
152 virtual guchar *get_jpeg_color_profile(guint *data_len) = 0;
154 virtual std::string image_comment() const = 0;
156 virtual void set_image_comment(const std::string& comment) = 0;
159 // This allows read-only access to the original metadata
160 struct _ExifDataOriginal : public _ExifData
163 Exiv2::Image::AutoPtr image_;
165 /* the icc profile in jpeg is not technically exif - store it here */
166 unsigned char *cp_data_;
171 Exiv2::ExifData emptyExifData_;
172 Exiv2::IptcData emptyIptcData_;
173 #if EXIV2_TEST_VERSION(0,16,0)
174 Exiv2::XmpData emptyXmpData_;
178 _ExifDataOriginal(Exiv2::Image::AutoPtr image)
182 image_ = std::move(image);
186 _ExifDataOriginal(gchar *path)
192 pathl_ = path_from_utf8(path);
195 image_ = Exiv2::ImageFactory::open(pathl_);
196 // g_assert (image.get() != 0);
197 image_->readMetadata();
199 #if EXIV2_TEST_VERSION(0,16,0)
200 if (image_->mimeType() == "application/rdf+xml")
202 //Exiv2 sidecar converts xmp to exif and iptc, we don't want it.
203 image_->clearExifData();
204 image_->clearIptcData();
208 #if EXIV2_TEST_VERSION(0,14,0)
209 if (image_->mimeType() == "image/jpeg")
211 /* try to get jpeg color profile */
212 Exiv2::BasicIo &io = image_->io();
213 gint open = io.isopen();
214 if (!open) io.open();
217 auto mapped = static_cast<unsigned char*>(io.mmap());
218 if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
221 if (!open) io.close();
225 catch (Exiv2::AnyError& e)
231 virtual ~_ExifDataOriginal()
233 if (cp_data_) g_free(cp_data_);
234 if (pathl_) g_free(pathl_);
237 virtual Exiv2::Image *image()
239 if (!valid_) return NULL;
243 virtual Exiv2::ExifData &exifData ()
245 if (!valid_) return emptyExifData_;
246 return image_->exifData();
249 virtual Exiv2::IptcData &iptcData ()
251 if (!valid_) return emptyIptcData_;
252 return image_->iptcData();
255 #if EXIV2_TEST_VERSION(0,16,0)
256 virtual Exiv2::XmpData &xmpData ()
258 if (!valid_) return emptyXmpData_;
259 return image_->xmpData();
263 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
265 if (cp_data_) g_free(cp_data_);
267 cp_length_ = cp_length;
270 virtual guchar *get_jpeg_color_profile(guint *data_len)
274 if (data_len) *data_len = cp_length_;
275 #if GLIB_CHECK_VERSION(2,68,0)
276 return (unsigned char *) g_memdup2(cp_data_, cp_length_);
278 return static_cast<unsigned char *>(g_memdup(cp_data_, cp_length_));
284 virtual std::string image_comment() const
286 return image_.get() ? image_->comment() : "";
289 virtual void set_image_comment(const std::string& comment)
292 image_->setComment(comment);
296 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data);
298 // This allows read-write access to the metadata
299 struct _ExifDataProcessed : public _ExifData
302 _ExifDataOriginal *imageData_;
303 _ExifDataOriginal *sidecarData_;
305 Exiv2::ExifData exifData_;
306 Exiv2::IptcData iptcData_;
307 #if EXIV2_TEST_VERSION(0,16,0)
308 Exiv2::XmpData xmpData_;
312 _ExifDataProcessed(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
314 imageData_ = new _ExifDataOriginal(path);
316 #if EXIV2_TEST_VERSION(0,16,0)
319 sidecarData_ = new _ExifDataOriginal(sidecar_path);
320 xmpData_ = sidecarData_->xmpData();
324 xmpData_ = imageData_->xmpData();
328 exifData_ = imageData_->exifData();
329 iptcData_ = imageData_->iptcData();
330 #if EXIV2_TEST_VERSION(0,17,0)
333 syncExifWithXmp(exifData_, xmpData_);
337 DEBUG_1("Exiv2: Catching bug\n");
342 g_hash_table_foreach(modified_xmp, _ExifDataProcessed_update_xmp, this);
346 virtual ~_ExifDataProcessed()
348 if (imageData_) delete imageData_;
349 if (sidecarData_) delete sidecarData_;
352 virtual ExifData *original()
357 virtual void writeMetadata(gchar *path = NULL)
361 #if EXIV2_TEST_VERSION(0,17,0)
362 if (options->metadata.save_legacy_IPTC)
363 copyXmpToIptc(xmpData_, iptcData_);
367 copyXmpToExif(xmpData_, exifData_);
369 Exiv2::Image *image = imageData_->image();
371 #ifdef HAVE_EXIV2_ERROR_CODE
372 #if EXIV2_TEST_VERSION(0,28,0)
373 if (!image) throw Exiv2::Error(Exiv2::ErrorCode::kerInputDataReadFailed);
375 if (!image) throw Exiv2::Error(Exiv2::kerInputDataReadFailed);
378 if (!image) throw Exiv2::Error(21);
380 image->setExifData(exifData_);
381 image->setIptcData(iptcData_);
382 #if EXIV2_TEST_VERSION(0,16,0)
383 image->setXmpData(xmpData_);
385 image->writeMetadata();
389 #if EXIV2_TEST_VERSION(0,17,0)
390 gchar *pathl = path_from_utf8(path);;
392 Exiv2::Image::AutoPtr sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
396 sidecar->setXmpData(xmpData_);
397 sidecar->writeMetadata();
399 #ifdef HAVE_EXIV2_ERROR_CODE
400 throw Exiv2::Error(Exiv2::kerNotAnImage, "xmp");
402 throw Exiv2::Error(3, "xmp");
408 virtual Exiv2::Image *image()
410 return imageData_->image();
413 virtual Exiv2::ExifData &exifData ()
418 virtual Exiv2::IptcData &iptcData ()
423 #if EXIV2_TEST_VERSION(0,16,0)
424 virtual Exiv2::XmpData &xmpData ()
430 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
432 imageData_->add_jpeg_color_profile(cp_data, cp_length);
435 virtual guchar *get_jpeg_color_profile(guint *data_len)
437 return imageData_->get_jpeg_color_profile(data_len);
440 virtual std::string image_comment() const
442 return imageData_->image_comment();
445 virtual void set_image_comment(const std::string& comment)
447 imageData_->set_image_comment(comment);
458 #ifdef EXV_ENABLE_NLS
459 bind_textdomain_codeset (EXV_PACKAGE, "UTF-8");
462 #ifdef EXV_ENABLE_BMFF
463 Exiv2::enableBMFF(TRUE);
469 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data)
471 exif_update_metadata(static_cast<ExifData *>(data), static_cast<gchar *>(key), static_cast<GList *>(value));
474 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
476 DEBUG_1("exif read %s, sidecar: %s", path, sidecar_path ? sidecar_path : "-");
478 return new _ExifDataProcessed(path, sidecar_path, modified_xmp);
480 catch (Exiv2::AnyError& e) {
487 gboolean exif_write(ExifData *exif)
490 exif->writeMetadata();
493 catch (Exiv2::AnyError& e) {
499 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
502 exif->writeMetadata(path);
505 catch (Exiv2::AnyError& e) {
513 void exif_free(ExifData *exif)
516 g_assert(dynamic_cast<_ExifDataProcessed *>(exif)); // this should not be called on ExifDataOriginal
520 ExifData *exif_get_original(ExifData *exif)
522 return exif->original();
526 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
529 Exiv2::Metadatum *item = NULL;
531 Exiv2::ExifKey ekey(key);
532 auto pos = exif->exifData().findKey(ekey);
533 if (pos == exif->exifData().end()) return NULL;
536 catch (Exiv2::AnyError& e) {
538 Exiv2::IptcKey ekey(key);
539 auto pos = exif->iptcData().findKey(ekey);
540 if (pos == exif->iptcData().end()) return NULL;
543 catch (Exiv2::AnyError& e) {
544 #if EXIV2_TEST_VERSION(0,16,0)
545 Exiv2::XmpKey ekey(key);
546 auto pos = exif->xmpData().findKey(ekey);
547 if (pos == exif->xmpData().end()) return NULL;
552 return reinterpret_cast<ExifItem *>(item);
554 catch (Exiv2::AnyError& e) {
560 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
563 Exiv2::Metadatum *item = NULL;
565 Exiv2::ExifKey ekey(key);
566 exif->exifData().add(ekey, NULL);
567 auto pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
571 catch (Exiv2::AnyError& e) {
573 Exiv2::IptcKey ekey(key);
574 exif->iptcData().add(ekey, NULL);
575 auto pos = exif->iptcData().end();
579 catch (Exiv2::AnyError& e) {
580 #if EXIV2_TEST_VERSION(0,16,0)
581 Exiv2::XmpKey ekey(key);
582 exif->xmpData().add(ekey, NULL);
583 auto pos = exif->xmpData().end();
589 return reinterpret_cast<ExifItem *>(item);
591 catch (Exiv2::AnyError& e) {
598 ExifItem *exif_get_first_item(ExifData *exif)
601 exif->exifIter = exif->exifData().begin();
602 exif->iptcIter = exif->iptcData().begin();
603 #if EXIV2_TEST_VERSION(0,16,0)
604 exif->xmpIter = exif->xmpData().begin();
606 if (exif->exifIter != exif->exifData().end())
608 const Exiv2::Metadatum *item = &*exif->exifIter;
610 return (ExifItem *)item;
612 if (exif->iptcIter != exif->iptcData().end())
614 const Exiv2::Metadatum *item = &*exif->iptcIter;
616 return (ExifItem *)item;
618 #if EXIV2_TEST_VERSION(0,16,0)
619 if (exif->xmpIter != exif->xmpData().end())
621 const Exiv2::Metadatum *item = &*exif->xmpIter;
623 return (ExifItem *)item;
629 catch (Exiv2::AnyError& e) {
635 ExifItem *exif_get_next_item(ExifData *exif)
638 if (exif->exifIter != exif->exifData().end())
640 const Exiv2::Metadatum *item = &*exif->exifIter;
642 return (ExifItem *)item;
644 if (exif->iptcIter != exif->iptcData().end())
646 const Exiv2::Metadatum *item = &*exif->iptcIter;
648 return (ExifItem *)item;
650 #if EXIV2_TEST_VERSION(0,16,0)
651 if (exif->xmpIter != exif->xmpData().end())
653 const Exiv2::Metadatum *item = &*exif->xmpIter;
655 return (ExifItem *)item;
660 catch (Exiv2::AnyError& e) {
666 char *exif_item_get_tag_name(ExifItem *item)
669 if (!item) return NULL;
670 return g_strdup((reinterpret_cast<Exiv2::Metadatum *>(item))->key().c_str());
672 catch (Exiv2::AnyError& e) {
678 guint exif_item_get_tag_id(ExifItem *item)
682 return (reinterpret_cast<Exiv2::Metadatum *>(item))->tag();
684 catch (Exiv2::AnyError& e) {
690 guint exif_item_get_elements(ExifItem *item)
694 return (reinterpret_cast<Exiv2::Metadatum *>(item))->count();
696 catch (Exiv2::AnyError& e) {
702 char *exif_item_get_data(ExifItem *item, guint *data_len)
706 auto md = reinterpret_cast<Exiv2::Metadatum *>(item);
707 if (data_len) *data_len = md->size();
708 auto data = static_cast<char *>(g_malloc(md->size()));
709 long res = md->copy(reinterpret_cast<Exiv2::byte *>(data), Exiv2::littleEndian /* should not matter */);
710 g_assert(res == md->size());
713 catch (Exiv2::AnyError& e) {
719 char *exif_item_get_description(ExifItem *item)
722 if (!item) return NULL;
723 return utf8_validate_or_convert((reinterpret_cast<Exiv2::Metadatum *>(item))->tagLabel().c_str());
725 catch (std::exception& e) {
726 // debug_exception(e);
732 invalidTypeId, unsignedByte, asciiString, unsignedShort,
733 unsignedLong, unsignedRational, signedByte, undefined,
734 signedShort, signedLong, signedRational, string,
735 date, time, comment, directory,
736 xmpText, xmpAlt, xmpBag, xmpSeq,
740 static guint format_id_trans_tbl [] = {
742 EXIF_FORMAT_BYTE_UNSIGNED,
744 EXIF_FORMAT_SHORT_UNSIGNED,
745 EXIF_FORMAT_LONG_UNSIGNED,
746 EXIF_FORMAT_RATIONAL_UNSIGNED,
748 EXIF_FORMAT_UNDEFINED,
751 EXIF_FORMAT_RATIONAL,
755 EXIF_FORMAT_UNDEFINED,
764 guint exif_item_get_format_id(ExifItem *item)
767 if (!item) return EXIF_FORMAT_UNKNOWN;
768 guint id = (reinterpret_cast<Exiv2::Metadatum *>(item))->typeId();
769 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
770 return format_id_trans_tbl[id];
772 catch (Exiv2::AnyError& e) {
774 return EXIF_FORMAT_UNKNOWN;
778 const char *exif_item_get_format_name(ExifItem *item, gboolean UNUSED(brief))
781 if (!item) return NULL;
782 return (reinterpret_cast<Exiv2::Metadatum *>(item))->typeName();
784 catch (Exiv2::AnyError& e) {
791 gchar *exif_item_get_data_as_text(ExifItem *item)
794 if (!item) return NULL;
795 auto metadatum = reinterpret_cast<Exiv2::Metadatum *>(item);
796 #if EXIV2_TEST_VERSION(0,17,0)
797 return utf8_validate_or_convert(metadatum->print().c_str());
799 std::stringstream str;
800 Exiv2::Exifdatum *exifdatum;
801 Exiv2::Iptcdatum *iptcdatum;
802 #if EXIV2_TEST_VERSION(0,16,0)
803 Exiv2::Xmpdatum *xmpdatum;
805 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(metadatum)))
807 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(metadatum)))
809 #if EXIV2_TEST_VERSION(0,16,0)
810 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(metadatum)))
814 return utf8_validate_or_convert(str.str().c_str());
817 catch (Exiv2::AnyError& e) {
822 gchar *exif_item_get_string(ExifItem *item, int idx)
825 if (!item) return NULL;
826 auto em = reinterpret_cast<Exiv2::Metadatum *>(item);
827 #if EXIV2_TEST_VERSION(0,16,0)
828 std::string str = em->toString(idx);
830 std::string str = em->toString(); /**< @FIXME ?? */
832 if (idx == 0 && str == "") str = em->toString();
833 if (str.length() > 5 && str.substr(0, 5) == "lang=")
835 std::string::size_type pos = str.find_first_of(' ');
836 if (pos != std::string::npos) str = str.substr(pos+1);
839 return utf8_validate_or_convert(str.c_str());
841 catch (Exiv2::AnyError& e) {
847 gint exif_item_get_integer(ExifItem *item, gint *value)
850 if (!item || exif_item_get_elements(item) == 0) return 0;
852 #if EXIV2_TEST_VERSION(0,28,0)
853 *value = ((Exiv2::Metadatum *)item)->toInt64();
855 *value = (reinterpret_cast<Exiv2::Metadatum *>(item))->toLong();
859 catch (Exiv2::AnyError& e) {
865 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
868 if (!item) return NULL;
869 if (n >= exif_item_get_elements(item)) return NULL;
870 Exiv2::Rational v = (reinterpret_cast<Exiv2::Metadatum *>(item))->toRational(n);
871 static ExifRational ret;
874 if (sign) *sign = ((reinterpret_cast<Exiv2::Metadatum *>(item))->typeId() == Exiv2::signedRational);
877 catch (Exiv2::AnyError& e) {
883 gchar *exif_get_tag_description_by_key(const gchar *key)
886 Exiv2::ExifKey ekey(key);
887 return utf8_validate_or_convert(ekey.tagLabel().c_str());
889 catch (Exiv2::AnyError& e) {
891 Exiv2::IptcKey ikey(key);
892 return utf8_validate_or_convert(ikey.tagLabel().c_str());
894 catch (Exiv2::AnyError& e) {
896 #if EXIV2_TEST_VERSION(0,16,0)
897 Exiv2::XmpKey xkey(key);
898 return utf8_validate_or_convert(xkey.tagLabel().c_str());
901 catch (Exiv2::AnyError& e) {
910 static const AltKey *find_alt_key(const gchar *xmp_key)
914 while (alt_keys[i].xmp_key)
916 if (strcmp(xmp_key, alt_keys[i].xmp_key) == 0) return &alt_keys[i];
922 static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const GList *values)
925 const GList *work = values;
928 Exiv2::ExifKey ekey(key);
930 auto pos = exif->exifData().findKey(ekey);
931 while (pos != exif->exifData().end())
933 exif->exifData().erase(pos);
934 pos = exif->exifData().findKey(ekey);
939 exif->exifData()[key] = static_cast<gchar *>(work->data);
943 catch (Exiv2::AnyError& e) {
944 #if EXIV2_TEST_VERSION(0,16,0)
948 Exiv2::IptcKey ekey(key);
949 auto pos = exif->iptcData().findKey(ekey);
950 while (pos != exif->iptcData().end())
952 exif->iptcData().erase(pos);
953 pos = exif->iptcData().findKey(ekey);
958 exif->iptcData()[key] = static_cast<gchar *>(work->data);
962 #if EXIV2_TEST_VERSION(0,16,0)
963 catch (Exiv2::AnyError& e) {
964 Exiv2::XmpKey ekey(key);
965 auto pos = exif->xmpData().findKey(ekey);
966 while (pos != exif->xmpData().end())
968 exif->xmpData().erase(pos);
969 pos = exif->xmpData().findKey(ekey);
974 exif->xmpData()[key] = static_cast<gchar *>(work->data);
982 catch (Exiv2::AnyError& e) {
988 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
990 gint ret = exif_update_metadata_simple(exif, key, values);
993 #if !EXIV2_TEST_VERSION(0,17,0)
994 TRUE || /* no conversion support */
996 !values || /* deleting item */
997 !ret /* writing to the explicitly given xmp tag failed */
1000 /* deleted xmp metadatum can't be converted, we have to delete also the corresponding legacy tag */
1001 /* if we can't write xmp, update at least the legacy tag */
1002 const AltKey *alt_key = find_alt_key(key);
1003 if (alt_key && alt_key->iptc_key)
1004 ret = exif_update_metadata_simple(exif, alt_key->iptc_key, values);
1006 if (alt_key && alt_key->exif_key)
1007 ret = exif_update_metadata_simple(exif, alt_key->exif_key, values);
1013 static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, MetadataFormat format, const Exiv2::ExifData *metadata)
1015 #if EXIV2_TEST_VERSION(0,16,0)
1016 Exiv2::TypeId id = item.typeId();
1017 if (format == METADATA_FORMATTED ||
1018 id == Exiv2::asciiString ||
1019 id == Exiv2::undefined ||
1020 id == Exiv2::string ||
1021 id == Exiv2::date ||
1022 id == Exiv2::time ||
1023 id == Exiv2::xmpText ||
1024 id == Exiv2::langAlt ||
1025 id == Exiv2::comment
1029 /* read as a single entry */
1032 if (format == METADATA_FORMATTED)
1034 #if EXIV2_TEST_VERSION(0,17,0)
1036 #if EXIV2_TEST_VERSION(0,18,0)
1041 std::stringstream stream;
1042 Exiv2::Exifdatum *exifdatum;
1043 Exiv2::Iptcdatum *iptcdatum;
1044 #if EXIV2_TEST_VERSION(0,16,0)
1045 Exiv2::Xmpdatum *xmpdatum;
1047 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(&item)))
1048 stream << *exifdatum;
1049 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(&item)))
1050 stream << *iptcdatum;
1051 #if EXIV2_TEST_VERSION(0,16,0)
1052 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(&item)))
1053 stream << *xmpdatum;
1057 if (str.length() > 1024)
1059 /* truncate very long strings, they cause problems in gui */
1066 str = item.toString();
1068 if (str.length() > 5 && str.substr(0, 5) == "lang=")
1070 std::string::size_type pos = str.find_first_of(' ');
1071 if (pos != std::string::npos) str = str.substr(pos+1);
1073 list = g_list_append(list, utf8_validate_or_convert(str.c_str()));
1074 #if EXIV2_TEST_VERSION(0,16,0)
1078 /* read as a list */
1080 for (i = 0; i < item.count(); i++)
1081 list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str()));
1087 static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, MetadataFormat format)
1092 Exiv2::ExifKey ekey(key);
1093 auto pos = exif->exifData().findKey(ekey);
1094 if (pos != exif->exifData().end())
1095 list = exif_add_value_to_glist(list, *pos, format, &exif->exifData());
1098 catch (Exiv2::AnyError& e) {
1100 Exiv2::IptcKey ekey(key);
1101 auto pos = exif->iptcData().begin();
1102 while (pos != exif->iptcData().end())
1104 if (pos->key() == key)
1105 list = exif_add_value_to_glist(list, *pos, format, NULL);
1110 catch (Exiv2::AnyError& e) {
1111 #if EXIV2_TEST_VERSION(0,16,0)
1112 Exiv2::XmpKey ekey(key);
1113 auto pos = exif->xmpData().findKey(ekey);
1114 if (pos != exif->xmpData().end())
1115 list = exif_add_value_to_glist(list, *pos, format, NULL);
1120 catch (Exiv2::AnyError& e) {
1126 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1130 if (!key) return NULL;
1132 if (format == METADATA_FORMATTED)
1136 text = exif_get_formatted_by_key(exif, key, &key_valid);
1137 if (key_valid) return g_list_append(NULL, text);
1140 list = exif_get_metadata_simple(exif, key, format);
1142 /* the following code can be ifdefed out as soon as Exiv2 supports it */
1145 const AltKey *alt_key = find_alt_key(key);
1146 if (alt_key && alt_key->iptc_key)
1147 list = exif_get_metadata_simple(exif, alt_key->iptc_key, format);
1149 #if !EXIV2_TEST_VERSION(0,17,0)
1150 /* with older Exiv2 versions exif is not synced */
1151 if (!list && alt_key && alt_key->exif_key)
1152 list = exif_get_metadata_simple(exif, alt_key->exif_key, format);
1159 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
1161 exif->add_jpeg_color_profile(cp_data, cp_length);
1164 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1166 guchar *ret = exif->get_jpeg_color_profile(data_len);
1167 if (ret) return ret;
1169 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1170 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1171 ret = reinterpret_cast<guchar *>(exif_item_get_data(prof_item, data_len));
1175 gchar* exif_get_image_comment(FileData* fd)
1177 if (!fd || !fd->exif)
1178 return g_strdup("");
1180 return g_strdup(fd->exif->image_comment().c_str());
1183 void exif_set_image_comment(FileData* fd, const gchar* comment)
1185 if (!fd || !fd->exif)
1188 fd->exif->set_image_comment(comment ? comment : "");
1192 #if EXIV2_TEST_VERSION(0,17,90)
1194 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1196 if (!exif) return NULL;
1198 if (!exif->image()) return NULL;
1200 std::string const path = exif->image()->io().path();
1201 /* given image pathname, first do simple (and fast) file extension test */
1202 gboolean is_raw = filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE);
1204 if (!is_raw && requested_width == 0) return NULL;
1208 Exiv2::PreviewManager pm(*exif->image());
1210 Exiv2::PreviewPropertiesList list = pm.getPreviewProperties();
1214 Exiv2::PreviewPropertiesList::iterator pos;
1215 auto last = --list.end();
1217 if (requested_width == 0)
1219 pos = last; // the largest
1226 if (pos->width_ >= static_cast<uint32_t>(requested_width) &&
1227 pos->height_ >= static_cast<uint32_t>(requested_height)) break;
1231 // we are not interested in smaller thumbnails in normal image formats - we can use full image instead
1234 if (pos->width_ < static_cast<uint32_t>(requested_width) || pos->height_ < static_cast<uint32_t>(requested_height)) return NULL;
1238 Exiv2::PreviewImage image = pm.getPreviewImage(*pos);
1240 Exiv2::DataBuf buf = image.copy();
1242 #if EXIV2_TEST_VERSION(0,28,0)
1243 *data_len = buf.size();
1244 auto b = buf.data();
1248 std::pair<Exiv2::byte*, long> p = buf.release();
1250 *data_len = p.second;
1256 catch (Exiv2::AnyError& e) {
1262 void exif_free_preview(guchar *buf)
1264 delete[] static_cast<Exiv2::byte*>(buf);
1268 #if !EXIV2_TEST_VERSION(0,17,90)
1270 /* This is a dirty hack to support raw file preview, bassed on
1271 tiffparse.cpp from Exiv2 examples */
1276 RawFile(Exiv2::BasicIo &io);
1279 const Exiv2::Value *find(uint16_t tag, uint16_t group);
1281 unsigned long preview_offset();
1285 Exiv2::TiffComponent::AutoPtr rootDir;
1286 Exiv2::BasicIo &io_;
1287 const Exiv2::byte *map_data;
1289 unsigned long offset;
1292 typedef struct _UnmapData UnmapData;
1300 static GList *exif_unmap_list = 0;
1302 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1304 unsigned long offset;
1306 if (!exif) return NULL;
1307 if (!exif->image()) return NULL;
1309 std::string const path = exif->image()->io().path();
1311 /* given image pathname, first do simple (and fast) file extension test */
1312 if (!filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE)) return NULL;
1321 RawFile rf(exif->image()->io());
1322 offset = rf.preview_offset();
1323 DEBUG_1("%s: offset %lu", path.c_str(), offset);
1325 fd = open(path.c_str(), O_RDONLY);
1331 if (fstat(fd, &st) == -1)
1336 map_len = st.st_size;
1337 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1339 if (map_data == MAP_FAILED)
1343 *data_len = map_len - offset;
1344 ud = g_new(UnmapData, 1);
1345 ud->ptr = map_data + offset;
1346 ud->map_data = map_data;
1347 ud->map_len = map_len;
1349 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1353 catch (Exiv2::AnyError& e) {
1360 void exif_free_preview(guchar *buf)
1362 GList *work = exif_unmap_list;
1366 UnmapData *ud = (UnmapData *)work->data;
1369 munmap(ud->map_data, ud->map_len);
1370 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1376 g_assert_not_reached();
1379 using namespace Exiv2;
1381 RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
1385 if (fstat(fd, &st) == -1)
1389 map_len = st.st_size;
1390 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1391 if (map_data == MAP_FAILED)
1396 if (io.open() != 0) {
1397 throw Error(9, io.path(), strError());
1400 map_data = io.mmap();
1401 map_len = io.size();
1404 type = Exiv2::ImageFactory::getType(map_data, map_len);
1406 #if EXIV2_TEST_VERSION(0,16,0)
1407 TiffHeaderBase *tiffHeader = NULL;
1409 TiffHeade2 *tiffHeader = NULL;
1411 Cr2Header *cr2Header = NULL;
1414 case Exiv2::ImageType::tiff:
1415 tiffHeader = new TiffHeade2();
1417 case Exiv2::ImageType::cr2:
1418 cr2Header = new Cr2Header();
1420 #if EXIV2_TEST_VERSION(0,16,0)
1421 case Exiv2::ImageType::orf:
1422 tiffHeader = new OrfHeader();
1425 #if EXIV2_TEST_VERSION(0,13,0)
1426 case Exiv2::ImageType::raf:
1427 if (map_len < 84 + 4) throw Error(14);
1428 offset = getULong(map_data + 84, bigEndian);
1431 case Exiv2::ImageType::crw:
1433 // Parse the image, starting with a CIFF header component
1434 Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
1435 parseTree->read(map_data, map_len);
1436 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
1437 if (entry) offset = entry->pData() - map_data;
1442 throw Error(3, "RAW");
1445 // process tiff-like formats
1447 TiffCompFactoryFct createFct = TiffCreator::create;
1449 rootDir = createFct(Tag::root, Group::none);
1450 if (0 == rootDir.get()) {
1451 throw Error(1, "No root element defined in TIFF structure");
1456 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
1457 #if EXIV2_TEST_VERSION(0,16,0)
1458 rootDir->setStart(map_data + tiffHeader->offset());
1460 rootDir->setStart(map_data + tiffHeader->ifdOffset());
1466 rootDir->setStart(map_data + cr2Header->offset());
1469 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
1471 TiffReader reader(map_data,
1476 rootDir->accept(reader);
1484 RawFile::~RawFile(void)
1490 const Value * RawFile::find(uint16_t tag, uint16_t group)
1492 TiffFinder finder(tag, group);
1493 rootDir->accept(finder);
1494 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
1497 DEBUG_1("(tag: %04x %04x) ", tag, group);
1498 return te->pValue();
1504 unsigned long RawFile::preview_offset(void)
1507 if (offset) return offset;
1509 if (type == Exiv2::ImageType::cr2)
1511 val = find(0x111, Group::ifd0);
1512 #if EXIV2_TEST_VERSION(0,28,0)
1513 if (val) return val->toInt64();
1515 if (val) return val->tolong();
1520 val = find(0x201, Group::sub0_0);
1521 #if EXIV2_TEST_VERSION(0,28,0)
1522 if (val) return val->toInt64();
1524 if (val) return val->tolong();
1527 val = find(0x201, Group::ifd0);
1528 #if EXIV2_TEST_VERSION(0,28,0)
1529 if (val) return val->toInt64();
1531 if (val) return val->tolong();
1534 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
1535 #if EXIV2_TEST_VERSION(0,28,0)
1536 if (val) return val->toInt64();
1538 if (val) return val->tolong();
1541 val = find(0x111, Group::sub0_1); // dng
1542 #if EXIV2_TEST_VERSION(0,28,0)
1543 if (val) return val->toInt64();
1545 if (val) return val->tolong();
1557 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */