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"
61 #include "filefilter.h"
64 #include "ui-fileops.h"
66 #if EXIV2_TEST_VERSION(0,28,0)
67 #define AnyError Error
68 #define AutoPtr UniquePtr
74 const gchar *exif_key;
75 const gchar *iptc_key;
78 /* this is a list of keys that should be converted, even with the older Exiv2 which does not support it directly */
79 static constexpr AltKey alt_keys[] = {
80 {"Xmp.tiff.Orientation", "Exif.Image.Orientation", nullptr},
81 {"Xmp.dc.title", nullptr, "Iptc.Application2.ObjectName" },
82 {"Xmp.photoshop.Urgency", nullptr, "Iptc.Application2.Urgency" },
83 {"Xmp.photoshop.Category", nullptr, "Iptc.Application2.Category" },
84 {"Xmp.photoshop.SupplementalCategory", nullptr, "Iptc.Application2.SuppCategory" },
85 {"Xmp.dc.subject", nullptr, "Iptc.Application2.Keywords" },
86 {"Xmp.iptc.Location", nullptr, "Iptc.Application2.LocationName" },
87 {"Xmp.photoshop.Instruction", nullptr, "Iptc.Application2.SpecialInstructions" },
88 {"Xmp.photoshop.DateCreated", nullptr, "Iptc.Application2.DateCreated" },
89 {"Xmp.dc.creator", nullptr, "Iptc.Application2.Byline" },
90 {"Xmp.photoshop.AuthorsPosition", nullptr, "Iptc.Application2.BylineTitle" },
91 {"Xmp.photoshop.City", nullptr, "Iptc.Application2.City" },
92 {"Xmp.photoshop.State", nullptr, "Iptc.Application2.ProvinceState" },
93 {"Xmp.iptc.CountryCode", nullptr, "Iptc.Application2.CountryCode" },
94 {"Xmp.photoshop.Country", nullptr, "Iptc.Application2.CountryName" },
95 {"Xmp.photoshop.TransmissionReference", nullptr, "Iptc.Application2.TransmissionReference"},
96 {"Xmp.photoshop.Headline", nullptr, "Iptc.Application2.Headline" },
97 {"Xmp.photoshop.Credit", nullptr, "Iptc.Application2.Credit" },
98 {"Xmp.photoshop.Source", nullptr, "Iptc.Application2.Source" },
99 {"Xmp.dc.rights", nullptr, "Iptc.Application2.Copyright" },
100 {"Xmp.dc.description", nullptr, "Iptc.Application2.Caption" },
101 {"Xmp.photoshop.CaptionWriter", nullptr, "Iptc.Application2.Writer" },
104 static void _debug_exception(const char* file,
109 gchar *str = g_locale_from_utf8(e.what(), -1, nullptr, nullptr, nullptr);
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 */
124 virtual ~ExifData() = default;
126 virtual void writeMetadata(gchar * = nullptr)
128 g_critical("Unsupported method of writing metadata");
131 virtual ExifData *original()
136 virtual Exiv2::Image *image() = 0;
138 virtual Exiv2::ExifData &exifData() = 0;
140 virtual Exiv2::IptcData &iptcData() = 0;
142 #if EXIV2_TEST_VERSION(0,16,0)
143 virtual Exiv2::XmpData &xmpData() = 0;
146 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) = 0;
148 virtual guchar *get_jpeg_color_profile(guint *data_len) = 0;
150 virtual std::string image_comment() const = 0;
152 virtual void set_image_comment(const std::string& comment) = 0;
155 // This allows read-only access to the original metadata
156 struct ExifDataOriginal : public ExifData
159 Exiv2::Image::AutoPtr image_;
161 /* the icc profile in jpeg is not technically exif - store it here */
162 unsigned char *cp_data_;
167 Exiv2::ExifData emptyExifData_;
168 Exiv2::IptcData emptyIptcData_;
169 #if EXIV2_TEST_VERSION(0,16,0)
170 Exiv2::XmpData emptyXmpData_;
174 ExifDataOriginal(Exiv2::Image::AutoPtr image)
178 image_ = std::move(image);
182 ExifDataOriginal(gchar *path)
188 pathl_ = path_from_utf8(path);
191 image_ = Exiv2::ImageFactory::open(pathl_);
192 image_->readMetadata();
194 #if EXIV2_TEST_VERSION(0,16,0)
195 if (image_->mimeType() == "application/rdf+xml")
197 //Exiv2 sidecar converts xmp to exif and iptc, we don't want it.
198 image_->clearExifData();
199 image_->clearIptcData();
203 #if EXIV2_TEST_VERSION(0,14,0)
204 if (image_->mimeType() == "image/jpeg")
206 /* try to get jpeg color profile */
207 Exiv2::BasicIo &io = image_->io();
208 gint open = io.isopen();
209 if (!open) io.open();
212 auto mapped = static_cast<unsigned char*>(io.mmap());
213 if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
216 if (!open) io.close();
220 catch (Exiv2::AnyError& e)
226 ~ExifDataOriginal() override
228 if (cp_data_) g_free(cp_data_);
229 if (pathl_) g_free(pathl_);
232 Exiv2::Image *image() override
234 if (!valid_) return nullptr;
238 Exiv2::ExifData &exifData () override
240 if (!valid_) return emptyExifData_;
241 return image_->exifData();
244 Exiv2::IptcData &iptcData () override
246 if (!valid_) return emptyIptcData_;
247 return image_->iptcData();
250 #if EXIV2_TEST_VERSION(0,16,0)
251 Exiv2::XmpData &xmpData () override
253 if (!valid_) return emptyXmpData_;
254 return image_->xmpData();
258 void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) override
260 if (cp_data_) g_free(cp_data_);
262 cp_length_ = cp_length;
265 guchar *get_jpeg_color_profile(guint *data_len) override
269 if (data_len) *data_len = cp_length_;
270 #if GLIB_CHECK_VERSION(2,68,0)
271 return static_cast<unsigned char *>(g_memdup2(cp_data_, cp_length_));
273 return static_cast<unsigned char *>(g_memdup(cp_data_, cp_length_));
279 std::string image_comment() const override
281 return image_.get() ? image_->comment() : "";
284 void set_image_comment(const std::string& comment) override
287 image_->setComment(comment);
291 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data);
293 // This allows read-write access to the metadata
294 struct ExifDataProcessed : public ExifData
297 std::unique_ptr<ExifDataOriginal> imageData_;
298 std::unique_ptr<ExifDataOriginal> sidecarData_;
300 Exiv2::ExifData exifData_;
301 Exiv2::IptcData iptcData_;
302 #if EXIV2_TEST_VERSION(0,16,0)
303 Exiv2::XmpData xmpData_;
307 ExifDataProcessed(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
309 imageData_ = std::make_unique<ExifDataOriginal>(path);
310 sidecarData_ = nullptr;
311 #if EXIV2_TEST_VERSION(0,16,0)
314 sidecarData_ = std::make_unique<ExifDataOriginal>(sidecar_path);
315 xmpData_ = sidecarData_->xmpData();
319 xmpData_ = imageData_->xmpData();
323 exifData_ = imageData_->exifData();
324 iptcData_ = imageData_->iptcData();
325 #if EXIV2_TEST_VERSION(0,17,0)
328 syncExifWithXmp(exifData_, xmpData_);
332 DEBUG_1("Exiv2: Catching bug\n");
337 g_hash_table_foreach(modified_xmp, _ExifDataProcessed_update_xmp, this);
341 ExifData *original() override
343 return imageData_.get();
346 void writeMetadata(gchar *path = nullptr) override
350 #if EXIV2_TEST_VERSION(0,17,0)
351 if (options->metadata.save_legacy_IPTC)
352 copyXmpToIptc(xmpData_, iptcData_);
356 copyXmpToExif(xmpData_, exifData_);
358 Exiv2::Image *image = imageData_->image();
360 #ifdef HAVE_EXIV2_ERROR_CODE
361 #if EXIV2_TEST_VERSION(0,28,0)
362 if (!image) throw Exiv2::Error(Exiv2::ErrorCode::kerInputDataReadFailed);
364 if (!image) throw Exiv2::Error(Exiv2::kerInputDataReadFailed);
367 if (!image) throw Exiv2::Error(21);
369 image->setExifData(exifData_);
370 image->setIptcData(iptcData_);
371 #if EXIV2_TEST_VERSION(0,16,0)
372 image->setXmpData(xmpData_);
374 image->writeMetadata();
378 #if EXIV2_TEST_VERSION(0,17,0)
379 gchar *pathl = path_from_utf8(path);;
381 auto sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
385 sidecar->setXmpData(xmpData_);
386 sidecar->writeMetadata();
388 #ifdef HAVE_EXIV2_ERROR_CODE
389 throw Exiv2::Error(Exiv2::kerNotAnImage, "xmp");
391 throw Exiv2::Error(3, "xmp");
397 Exiv2::Image *image() override
399 return imageData_->image();
402 Exiv2::ExifData &exifData () override
407 Exiv2::IptcData &iptcData () override
412 #if EXIV2_TEST_VERSION(0,16,0)
413 Exiv2::XmpData &xmpData () override
419 void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) override
421 imageData_->add_jpeg_color_profile(cp_data, cp_length);
424 guchar *get_jpeg_color_profile(guint *data_len) override
426 return imageData_->get_jpeg_color_profile(data_len);
429 std::string image_comment() const override
431 return imageData_->image_comment();
434 void set_image_comment(const std::string& comment) override
436 imageData_->set_image_comment(comment);
447 #ifdef EXV_ENABLE_NLS
448 bind_textdomain_codeset (EXV_PACKAGE, "UTF-8");
451 #ifdef EXV_ENABLE_BMFF
452 Exiv2::enableBMFF(TRUE);
458 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data)
460 exif_update_metadata(static_cast<ExifData *>(data), static_cast<gchar *>(key), static_cast<GList *>(value));
463 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
465 DEBUG_1("exif read %s, sidecar: %s", path, sidecar_path ? sidecar_path : "-");
467 return new ExifDataProcessed(path, sidecar_path, modified_xmp);
469 catch (Exiv2::AnyError& e) {
476 gboolean exif_write(ExifData *exif)
479 exif->writeMetadata();
482 catch (Exiv2::AnyError& e) {
488 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
491 exif->writeMetadata(path);
494 catch (Exiv2::AnyError& e) {
502 void exif_free(ExifData *exif)
505 g_assert(dynamic_cast<ExifDataProcessed *>(exif)); // this should not be called on ExifDataOriginal
509 ExifData *exif_get_original(ExifData *exif)
511 return exif->original();
515 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
518 Exiv2::Metadatum *item = nullptr;
520 Exiv2::ExifKey ekey(key);
521 auto pos = exif->exifData().findKey(ekey);
522 if (pos == exif->exifData().end()) return nullptr;
525 catch (Exiv2::AnyError& e) {
527 Exiv2::IptcKey ekey(key);
528 auto pos = exif->iptcData().findKey(ekey);
529 if (pos == exif->iptcData().end()) return nullptr;
532 catch (Exiv2::AnyError& e) {
533 #if EXIV2_TEST_VERSION(0,16,0)
534 Exiv2::XmpKey ekey(key);
535 auto pos = exif->xmpData().findKey(ekey);
536 if (pos == exif->xmpData().end()) return nullptr;
541 return reinterpret_cast<ExifItem *>(item);
543 catch (Exiv2::AnyError& e) {
549 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
552 Exiv2::Metadatum *item = nullptr;
554 Exiv2::ExifKey ekey(key);
555 exif->exifData().add(ekey, nullptr);
556 auto pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
560 catch (Exiv2::AnyError& e) {
562 Exiv2::IptcKey ekey(key);
563 exif->iptcData().add(ekey, nullptr);
564 auto pos = exif->iptcData().end();
568 catch (Exiv2::AnyError& e) {
569 #if EXIV2_TEST_VERSION(0,16,0)
570 Exiv2::XmpKey ekey(key);
571 exif->xmpData().add(ekey, nullptr);
572 auto pos = exif->xmpData().end();
578 return reinterpret_cast<ExifItem *>(item);
580 catch (Exiv2::AnyError& e) {
587 ExifItem *exif_get_first_item(ExifData *exif)
590 exif->exifIter = exif->exifData().begin();
591 exif->iptcIter = exif->iptcData().begin();
592 #if EXIV2_TEST_VERSION(0,16,0)
593 exif->xmpIter = exif->xmpData().begin();
595 if (exif->exifIter != exif->exifData().end())
597 const Exiv2::Metadatum *item = &*exif->exifIter;
599 return (ExifItem *)item;
601 if (exif->iptcIter != exif->iptcData().end())
603 const Exiv2::Metadatum *item = &*exif->iptcIter;
605 return (ExifItem *)item;
607 #if EXIV2_TEST_VERSION(0,16,0)
608 if (exif->xmpIter != exif->xmpData().end())
610 const Exiv2::Metadatum *item = &*exif->xmpIter;
612 return (ExifItem *)item;
618 catch (Exiv2::AnyError& e) {
624 ExifItem *exif_get_next_item(ExifData *exif)
627 if (exif->exifIter != exif->exifData().end())
629 const Exiv2::Metadatum *item = &*exif->exifIter;
631 return (ExifItem *)item;
633 if (exif->iptcIter != exif->iptcData().end())
635 const Exiv2::Metadatum *item = &*exif->iptcIter;
637 return (ExifItem *)item;
639 #if EXIV2_TEST_VERSION(0,16,0)
640 if (exif->xmpIter != exif->xmpData().end())
642 const Exiv2::Metadatum *item = &*exif->xmpIter;
644 return (ExifItem *)item;
649 catch (Exiv2::AnyError& e) {
655 char *exif_item_get_tag_name(ExifItem *item)
658 if (!item) return nullptr;
659 return g_strdup((reinterpret_cast<Exiv2::Metadatum *>(item))->key().c_str());
661 catch (Exiv2::AnyError& e) {
667 guint exif_item_get_tag_id(ExifItem *item)
671 return (reinterpret_cast<Exiv2::Metadatum *>(item))->tag();
673 catch (Exiv2::AnyError& e) {
679 guint exif_item_get_elements(ExifItem *item)
683 return (reinterpret_cast<Exiv2::Metadatum *>(item))->count();
685 catch (Exiv2::AnyError& e) {
691 char *exif_item_get_data(ExifItem *item, guint *data_len)
694 if (!item) return nullptr;
695 auto md = reinterpret_cast<Exiv2::Metadatum *>(item);
696 if (data_len) *data_len = md->size();
697 auto data = static_cast<char *>(g_malloc(md->size()));
698 long res = md->copy(reinterpret_cast<Exiv2::byte *>(data), Exiv2::littleEndian /* should not matter */);
699 g_assert(res == md->size());
702 catch (Exiv2::AnyError& e) {
708 char *exif_item_get_description(ExifItem *item)
711 if (!item) return nullptr;
712 return utf8_validate_or_convert((reinterpret_cast<Exiv2::Metadatum *>(item))->tagLabel().c_str());
714 catch (std::exception& e) {
720 invalidTypeId, unsignedByte, asciiString, unsignedShort,
721 unsignedLong, unsignedRational, signedByte, undefined,
722 signedShort, signedLong, signedRational, string,
723 date, time, comment, directory,
724 xmpText, xmpAlt, xmpBag, xmpSeq,
728 static guint format_id_trans_tbl [] = {
730 EXIF_FORMAT_BYTE_UNSIGNED,
732 EXIF_FORMAT_SHORT_UNSIGNED,
733 EXIF_FORMAT_LONG_UNSIGNED,
734 EXIF_FORMAT_RATIONAL_UNSIGNED,
736 EXIF_FORMAT_UNDEFINED,
739 EXIF_FORMAT_RATIONAL,
743 EXIF_FORMAT_UNDEFINED,
752 guint exif_item_get_format_id(ExifItem *item)
755 if (!item) return EXIF_FORMAT_UNKNOWN;
756 guint id = (reinterpret_cast<Exiv2::Metadatum *>(item))->typeId();
757 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
758 return format_id_trans_tbl[id];
760 catch (Exiv2::AnyError& e) {
762 return EXIF_FORMAT_UNKNOWN;
766 const char *exif_item_get_format_name(ExifItem *item, gboolean)
769 if (!item) return nullptr;
770 return (reinterpret_cast<Exiv2::Metadatum *>(item))->typeName();
772 catch (Exiv2::AnyError& e) {
779 gchar *exif_item_get_data_as_text(ExifItem *item, ExifData *exif)
782 if (!item) return nullptr;
783 auto metadatum = reinterpret_cast<Exiv2::Metadatum *>(item);
784 #if EXIV2_TEST_VERSION(0,17,0)
785 return utf8_validate_or_convert(metadatum->print(&exif->exifData()).c_str());
787 std::stringstream str;
788 Exiv2::Exifdatum *exifdatum;
789 Exiv2::Iptcdatum *iptcdatum;
790 #if EXIV2_TEST_VERSION(0,16,0)
791 Exiv2::Xmpdatum *xmpdatum;
793 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(metadatum)))
795 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(metadatum)))
797 #if EXIV2_TEST_VERSION(0,16,0)
798 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(metadatum)))
802 return utf8_validate_or_convert(str.str().c_str());
805 catch (Exiv2::AnyError& e) {
810 gchar *exif_item_get_string(ExifItem *item, int idx)
813 if (!item) return nullptr;
814 auto em = reinterpret_cast<Exiv2::Metadatum *>(item);
815 #if EXIV2_TEST_VERSION(0,16,0)
816 std::string str = em->toString(idx);
818 std::string str = em->toString(); /**< @FIXME ?? */
820 if (idx == 0 && str.empty()) str = em->toString();
821 if (str.length() > 5 && str.substr(0, 5) == "lang=")
823 std::string::size_type pos = str.find_first_of(' ');
824 if (pos != std::string::npos) str = str.substr(pos+1);
827 return utf8_validate_or_convert(str.c_str());
829 catch (Exiv2::AnyError& e) {
835 gint exif_item_get_integer(ExifItem *item, gint *value)
838 if (!item || exif_item_get_elements(item) == 0) return 0;
840 #if EXIV2_TEST_VERSION(0,28,0)
841 *value = ((Exiv2::Metadatum *)item)->toInt64();
843 *value = (reinterpret_cast<Exiv2::Metadatum *>(item))->toLong();
847 catch (Exiv2::AnyError& e) {
853 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
856 if (!item) return nullptr;
857 if (n >= exif_item_get_elements(item)) return nullptr;
858 Exiv2::Rational v = (reinterpret_cast<Exiv2::Metadatum *>(item))->toRational(n);
859 static ExifRational ret;
862 if (sign) *sign = ((reinterpret_cast<Exiv2::Metadatum *>(item))->typeId() == Exiv2::signedRational);
865 catch (Exiv2::AnyError& e) {
871 gchar *exif_get_tag_description_by_key(const gchar *key)
874 Exiv2::ExifKey ekey(key);
875 return utf8_validate_or_convert(ekey.tagLabel().c_str());
877 catch (Exiv2::AnyError& e) {
879 Exiv2::IptcKey ikey(key);
880 return utf8_validate_or_convert(ikey.tagLabel().c_str());
882 catch (Exiv2::AnyError& e) {
884 #if EXIV2_TEST_VERSION(0,16,0)
885 Exiv2::XmpKey xkey(key);
886 return utf8_validate_or_convert(xkey.tagLabel().c_str());
889 catch (Exiv2::AnyError& e) {
898 static const AltKey *find_alt_key(const gchar *xmp_key)
900 for (const auto& k : alt_keys)
901 if (strcmp(xmp_key, k.xmp_key) == 0) return &k;
905 static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const GList *values)
908 const GList *work = values;
911 Exiv2::ExifKey ekey(key);
913 auto pos = exif->exifData().findKey(ekey);
914 while (pos != exif->exifData().end())
916 exif->exifData().erase(pos);
917 pos = exif->exifData().findKey(ekey);
922 exif->exifData()[key] = static_cast<gchar *>(work->data);
926 catch (Exiv2::AnyError& e) {
927 #if EXIV2_TEST_VERSION(0,16,0)
931 Exiv2::IptcKey ekey(key);
932 auto pos = exif->iptcData().findKey(ekey);
933 while (pos != exif->iptcData().end())
935 exif->iptcData().erase(pos);
936 pos = exif->iptcData().findKey(ekey);
941 exif->iptcData()[key] = static_cast<gchar *>(work->data);
945 #if EXIV2_TEST_VERSION(0,16,0)
946 catch (Exiv2::AnyError& e) {
947 Exiv2::XmpKey ekey(key);
948 auto pos = exif->xmpData().findKey(ekey);
949 while (pos != exif->xmpData().end())
951 exif->xmpData().erase(pos);
952 pos = exif->xmpData().findKey(ekey);
957 exif->xmpData()[key] = static_cast<gchar *>(work->data);
965 catch (Exiv2::AnyError& e) {
971 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
973 gint ret = exif_update_metadata_simple(exif, key, values);
976 #if !EXIV2_TEST_VERSION(0,17,0)
977 TRUE || /* no conversion support */
979 !values || /* deleting item */
980 !ret /* writing to the explicitly given xmp tag failed */
983 /* deleted xmp metadatum can't be converted, we have to delete also the corresponding legacy tag */
984 /* if we can't write xmp, update at least the legacy tag */
985 const AltKey *alt_key = find_alt_key(key);
986 if (alt_key && alt_key->iptc_key)
987 ret = exif_update_metadata_simple(exif, alt_key->iptc_key, values);
989 if (alt_key && alt_key->exif_key)
990 ret = exif_update_metadata_simple(exif, alt_key->exif_key, values);
996 static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, MetadataFormat format, const Exiv2::ExifData *metadata)
998 #if EXIV2_TEST_VERSION(0,16,0)
999 Exiv2::TypeId id = item.typeId();
1000 if (format == METADATA_FORMATTED ||
1001 id == Exiv2::asciiString ||
1002 id == Exiv2::undefined ||
1003 id == Exiv2::string ||
1004 id == Exiv2::date ||
1005 id == Exiv2::time ||
1006 id == Exiv2::xmpText ||
1007 id == Exiv2::langAlt ||
1008 id == Exiv2::comment
1012 /* read as a single entry */
1015 if (format == METADATA_FORMATTED)
1017 #if EXIV2_TEST_VERSION(0,17,0)
1019 #if EXIV2_TEST_VERSION(0,18,0)
1024 std::stringstream stream;
1025 Exiv2::Exifdatum *exifdatum;
1026 Exiv2::Iptcdatum *iptcdatum;
1027 #if EXIV2_TEST_VERSION(0,16,0)
1028 Exiv2::Xmpdatum *xmpdatum;
1030 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(&item)))
1031 stream << *exifdatum;
1032 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(&item)))
1033 stream << *iptcdatum;
1034 #if EXIV2_TEST_VERSION(0,16,0)
1035 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(&item)))
1036 stream << *xmpdatum;
1040 if (str.length() > 1024)
1042 /* truncate very long strings, they cause problems in gui */
1049 str = item.toString();
1051 if (str.length() > 5 && str.substr(0, 5) == "lang=")
1053 std::string::size_type pos = str.find_first_of(' ');
1054 if (pos != std::string::npos) str = str.substr(pos+1);
1056 list = g_list_append(list, utf8_validate_or_convert(str.c_str()));
1057 #if EXIV2_TEST_VERSION(0,16,0)
1061 /* read as a list */
1063 for (i = 0; i < item.count(); i++)
1064 list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str()));
1070 static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, MetadataFormat format)
1072 GList *list = nullptr;
1075 Exiv2::ExifKey ekey(key);
1076 auto pos = exif->exifData().findKey(ekey);
1077 if (pos != exif->exifData().end())
1078 list = exif_add_value_to_glist(list, *pos, format, &exif->exifData());
1081 catch (Exiv2::AnyError& e) {
1083 Exiv2::IptcKey ekey(key);
1084 auto pos = exif->iptcData().begin();
1085 while (pos != exif->iptcData().end())
1087 if (pos->key() == key)
1088 list = exif_add_value_to_glist(list, *pos, format, nullptr);
1093 catch (Exiv2::AnyError& e) {
1094 #if EXIV2_TEST_VERSION(0,16,0)
1095 Exiv2::XmpKey ekey(key);
1096 auto pos = exif->xmpData().findKey(ekey);
1097 if (pos != exif->xmpData().end())
1098 list = exif_add_value_to_glist(list, *pos, format, nullptr);
1103 catch (Exiv2::AnyError& e) {
1109 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1111 GList *list = nullptr;
1113 if (!key) return nullptr;
1115 if (format == METADATA_FORMATTED)
1119 text = exif_get_formatted_by_key(exif, key, &key_valid);
1120 if (key_valid) return g_list_append(nullptr, text);
1123 list = exif_get_metadata_simple(exif, key, format);
1125 /* the following code can be ifdefed out as soon as Exiv2 supports it */
1128 const AltKey *alt_key = find_alt_key(key);
1129 if (alt_key && alt_key->iptc_key)
1130 list = exif_get_metadata_simple(exif, alt_key->iptc_key, format);
1132 #if !EXIV2_TEST_VERSION(0,17,0)
1133 /* with older Exiv2 versions exif is not synced */
1134 if (!list && alt_key && alt_key->exif_key)
1135 list = exif_get_metadata_simple(exif, alt_key->exif_key, format);
1142 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
1144 exif->add_jpeg_color_profile(cp_data, cp_length);
1147 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1149 guchar *ret = exif->get_jpeg_color_profile(data_len);
1150 if (ret) return ret;
1152 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1153 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1154 ret = reinterpret_cast<guchar *>(exif_item_get_data(prof_item, data_len));
1158 gchar* exif_get_image_comment(FileData* fd)
1160 if (!fd || !fd->exif)
1161 return g_strdup("");
1163 return g_strdup(fd->exif->image_comment().c_str());
1166 void exif_set_image_comment(FileData* fd, const gchar* comment)
1168 if (!fd || !fd->exif)
1171 fd->exif->set_image_comment(comment ? comment : "");
1175 #if EXIV2_TEST_VERSION(0,17,90)
1177 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1179 if (!exif) return nullptr;
1181 if (!exif->image()) return nullptr;
1183 std::string const path = exif->image()->io().path();
1184 /* given image pathname, first do simple (and fast) file extension test */
1185 gboolean is_raw = filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE);
1187 if (!is_raw && requested_width == 0) return nullptr;
1191 Exiv2::PreviewManager pm(*exif->image());
1193 Exiv2::PreviewPropertiesList list = pm.getPreviewProperties();
1197 Exiv2::PreviewPropertiesList::iterator pos;
1198 auto last = --list.end();
1200 if (requested_width == 0)
1202 pos = last; // the largest
1209 if (pos->width_ >= static_cast<uint32_t>(requested_width) &&
1210 pos->height_ >= static_cast<uint32_t>(requested_height)) break;
1214 // we are not interested in smaller thumbnails in normal image formats - we can use full image instead
1217 if (pos->width_ < static_cast<uint32_t>(requested_width) || pos->height_ < static_cast<uint32_t>(requested_height)) return nullptr;
1221 Exiv2::PreviewImage image = pm.getPreviewImage(*pos);
1223 // Let's not touch data_len until we finish copy.
1224 // Just in case we run into OOM.
1225 size_t img_sz = image.size();
1226 auto* b = new Exiv2::byte[img_sz];
1227 std::copy_n(image.pData(), img_sz, b);
1233 catch (Exiv2::AnyError& e) {
1239 void exif_free_preview(const guchar *buf)
1241 delete[] static_cast<const Exiv2::byte*>(buf);
1245 #if !EXIV2_TEST_VERSION(0,17,90)
1247 /* This is a dirty hack to support raw file preview, bassed on
1248 tiffparse.cpp from Exiv2 examples */
1253 RawFile(Exiv2::BasicIo &io);
1256 const Exiv2::Value *find(uint16_t tag, uint16_t group);
1258 unsigned long preview_offset();
1262 Exiv2::TiffComponent::AutoPtr rootDir;
1263 Exiv2::BasicIo &io_;
1264 const Exiv2::byte *map_data;
1266 unsigned long offset;
1276 static GList *exif_unmap_list = 0;
1278 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1280 unsigned long offset;
1282 if (!exif) return NULL;
1283 if (!exif->image()) return NULL;
1285 std::string const path = exif->image()->io().path();
1287 /* given image pathname, first do simple (and fast) file extension test */
1288 if (!filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE)) return NULL;
1297 RawFile rf(exif->image()->io());
1298 offset = rf.preview_offset();
1299 DEBUG_1("%s: offset %lu", path.c_str(), offset);
1301 fd = open(path.c_str(), O_RDONLY);
1307 if (fstat(fd, &st) == -1)
1312 map_len = st.st_size;
1313 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1315 if (map_data == MAP_FAILED)
1319 *data_len = map_len - offset;
1320 ud = g_new(UnmapData, 1);
1321 ud->ptr = map_data + offset;
1322 ud->map_data = map_data;
1323 ud->map_len = map_len;
1325 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1329 catch (Exiv2::AnyError& e) {
1336 void exif_free_preview(const guchar *buf)
1338 GList *work = exif_unmap_list;
1342 UnmapData *ud = (UnmapData *)work->data;
1345 munmap(ud->map_data, ud->map_len);
1346 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1352 g_assert_not_reached();
1355 using namespace Exiv2;
1357 RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
1361 if (fstat(fd, &st) == -1)
1365 map_len = st.st_size;
1366 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1367 if (map_data == MAP_FAILED)
1372 if (io.open() != 0) {
1373 throw Error(9, io.path(), strError());
1376 map_data = io.mmap();
1377 map_len = io.size();
1380 type = Exiv2::ImageFactory::getType(map_data, map_len);
1382 #if EXIV2_TEST_VERSION(0,16,0)
1383 std::unique_ptr<TiffHeaderBase> tiffHeader;
1385 std::unique_ptr<TiffHeade2> tiffHeader;
1387 std::unique_ptr<Cr2Header> cr2Header;
1390 case Exiv2::ImageType::tiff:
1391 tiffHeader = std::make_unique<TiffHeade2>();
1393 case Exiv2::ImageType::cr2:
1394 cr2Header = std::make_unique<Cr2Header>();
1396 #if EXIV2_TEST_VERSION(0,16,0)
1397 case Exiv2::ImageType::orf:
1398 tiffHeader = std::make_unique<OrfHeader>();
1401 #if EXIV2_TEST_VERSION(0,13,0)
1402 case Exiv2::ImageType::raf:
1403 if (map_len < 84 + 4) throw Error(14);
1404 offset = getULong(map_data + 84, bigEndian);
1407 case Exiv2::ImageType::crw:
1409 // Parse the image, starting with a CIFF header component
1410 auto parseTree = std::make_unique<Exiv2::CiffHeader>();
1411 parseTree->read(map_data, map_len);
1412 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
1413 if (entry) offset = entry->pData() - map_data;
1418 throw Error(3, "RAW");
1421 // process tiff-like formats
1423 TiffCompFactoryFct createFct = TiffCreator::create;
1425 rootDir = createFct(Tag::root, Group::none);
1426 if (0 == rootDir.get()) {
1427 throw Error(1, "No root element defined in TIFF structure");
1432 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
1433 #if EXIV2_TEST_VERSION(0,16,0)
1434 rootDir->setStart(map_data + tiffHeader->offset());
1436 rootDir->setStart(map_data + tiffHeader->ifdOffset());
1442 rootDir->setStart(map_data + cr2Header->offset());
1445 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
1447 TiffReader reader(map_data,
1452 rootDir->accept(reader);
1455 RawFile::~RawFile(void)
1461 const Value * RawFile::find(uint16_t tag, uint16_t group)
1463 TiffFinder finder(tag, group);
1464 rootDir->accept(finder);
1465 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
1468 DEBUG_1("(tag: %04x %04x) ", tag, group);
1469 return te->pValue();
1475 unsigned long RawFile::preview_offset(void)
1478 if (offset) return offset;
1480 if (type == Exiv2::ImageType::cr2)
1482 val = find(0x111, Group::ifd0);
1483 #if EXIV2_TEST_VERSION(0,28,0)
1484 if (val) return val->toInt64();
1486 if (val) return val->tolong();
1491 val = find(0x201, Group::sub0_0);
1492 #if EXIV2_TEST_VERSION(0,28,0)
1493 if (val) return val->toInt64();
1495 if (val) return val->tolong();
1498 val = find(0x201, Group::ifd0);
1499 #if EXIV2_TEST_VERSION(0,28,0)
1500 if (val) return val->toInt64();
1502 if (val) return val->tolong();
1505 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
1506 #if EXIV2_TEST_VERSION(0,28,0)
1507 if (val) return val->toInt64();
1509 if (val) return val->tolong();
1512 val = find(0x111, Group::sub0_1); // dng
1513 #if EXIV2_TEST_VERSION(0,28,0)
1514 if (val) return val->toInt64();
1516 if (val) return val->tolong();
1528 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */