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.
35 #include <exiv2/exiv2.hpp>
43 #include "filefilter.h"
47 #include "ui-fileops.h"
51 #if EXIV2_TEST_VERSION(0,27,0)
52 #define HAVE_EXIV2_ERROR_CODE
55 #if EXIV2_TEST_VERSION(0,27,0)
56 #define EXV_PACKAGE "exiv2"
59 #if EXIV2_TEST_VERSION(0,28,0)
60 #define AnyError Error
61 #define AutoPtr UniquePtr
67 const gchar *exif_key;
68 const gchar *iptc_key;
71 /* this is a list of keys that should be converted, even with the older Exiv2 which does not support it directly */
72 static constexpr AltKey alt_keys[] = {
73 {"Xmp.tiff.Orientation", "Exif.Image.Orientation", nullptr},
74 {"Xmp.dc.title", nullptr, "Iptc.Application2.ObjectName" },
75 {"Xmp.photoshop.Urgency", nullptr, "Iptc.Application2.Urgency" },
76 {"Xmp.photoshop.Category", nullptr, "Iptc.Application2.Category" },
77 {"Xmp.photoshop.SupplementalCategory", nullptr, "Iptc.Application2.SuppCategory" },
78 {"Xmp.dc.subject", nullptr, "Iptc.Application2.Keywords" },
79 {"Xmp.iptc.Location", nullptr, "Iptc.Application2.LocationName" },
80 {"Xmp.photoshop.Instruction", nullptr, "Iptc.Application2.SpecialInstructions" },
81 {"Xmp.photoshop.DateCreated", nullptr, "Iptc.Application2.DateCreated" },
82 {"Xmp.dc.creator", nullptr, "Iptc.Application2.Byline" },
83 {"Xmp.photoshop.AuthorsPosition", nullptr, "Iptc.Application2.BylineTitle" },
84 {"Xmp.photoshop.City", nullptr, "Iptc.Application2.City" },
85 {"Xmp.photoshop.State", nullptr, "Iptc.Application2.ProvinceState" },
86 {"Xmp.iptc.CountryCode", nullptr, "Iptc.Application2.CountryCode" },
87 {"Xmp.photoshop.Country", nullptr, "Iptc.Application2.CountryName" },
88 {"Xmp.photoshop.TransmissionReference", nullptr, "Iptc.Application2.TransmissionReference"},
89 {"Xmp.photoshop.Headline", nullptr, "Iptc.Application2.Headline" },
90 {"Xmp.photoshop.Credit", nullptr, "Iptc.Application2.Credit" },
91 {"Xmp.photoshop.Source", nullptr, "Iptc.Application2.Source" },
92 {"Xmp.dc.rights", nullptr, "Iptc.Application2.Copyright" },
93 {"Xmp.dc.description", nullptr, "Iptc.Application2.Caption" },
94 {"Xmp.photoshop.CaptionWriter", nullptr, "Iptc.Application2.Writer" },
97 static void _debug_exception(const char* file,
102 gchar *str = g_locale_from_utf8(e.what(), -1, nullptr, nullptr, nullptr);
103 DEBUG_1("%s:%d:%s:Exiv2: %s", file, line, func, str);
107 #define debug_exception(e) _debug_exception(__FILE__, __LINE__, __func__, e)
111 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
112 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
113 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
115 virtual ~ExifData() = default;
117 virtual void writeMetadata(gchar * = nullptr)
119 g_critical("Unsupported method of writing metadata");
122 virtual ExifData *original()
127 virtual Exiv2::Image *image() = 0;
129 virtual Exiv2::ExifData &exifData() = 0;
131 virtual Exiv2::IptcData &iptcData() = 0;
133 virtual Exiv2::XmpData &xmpData() = 0;
135 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) = 0;
137 virtual guchar *get_jpeg_color_profile(guint *data_len) = 0;
139 virtual std::string image_comment() const = 0;
141 virtual void set_image_comment(const std::string& comment) = 0;
144 // This allows read-only access to the original metadata
145 struct ExifDataOriginal : public ExifData
148 Exiv2::Image::AutoPtr image_;
150 /* the icc profile in jpeg is not technically exif - store it here */
151 unsigned char *cp_data_;
156 Exiv2::ExifData emptyExifData_;
157 Exiv2::IptcData emptyIptcData_;
158 Exiv2::XmpData emptyXmpData_;
161 ExifDataOriginal(Exiv2::Image::AutoPtr image)
165 image_ = std::move(image);
169 ExifDataOriginal(gchar *path)
175 pathl_ = path_from_utf8(path);
178 image_ = Exiv2::ImageFactory::open(pathl_);
179 image_->readMetadata();
181 if (image_->mimeType() == "application/rdf+xml")
183 //Exiv2 sidecar converts xmp to exif and iptc, we don't want it.
184 image_->clearExifData();
185 image_->clearIptcData();
188 if (image_->mimeType() == "image/jpeg")
190 /* try to get jpeg color profile */
191 Exiv2::BasicIo &io = image_->io();
192 gint open = io.isopen();
193 if (!open) io.open();
196 auto mapped = static_cast<unsigned char*>(io.mmap());
197 if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
200 if (!open) io.close();
203 catch (Exiv2::AnyError& e)
209 ~ExifDataOriginal() override
211 if (cp_data_) g_free(cp_data_);
212 if (pathl_) g_free(pathl_);
215 Exiv2::Image *image() override
217 if (!valid_) return nullptr;
221 Exiv2::ExifData &exifData () override
223 if (!valid_) return emptyExifData_;
224 return image_->exifData();
227 Exiv2::IptcData &iptcData () override
229 if (!valid_) return emptyIptcData_;
230 return image_->iptcData();
233 Exiv2::XmpData &xmpData () override
235 if (!valid_) return emptyXmpData_;
236 return image_->xmpData();
239 void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) override
241 if (cp_data_) g_free(cp_data_);
243 cp_length_ = cp_length;
246 guchar *get_jpeg_color_profile(guint *data_len) override
250 if (data_len) *data_len = cp_length_;
251 #if GLIB_CHECK_VERSION(2,68,0)
252 return static_cast<unsigned char *>(g_memdup2(cp_data_, cp_length_));
254 return static_cast<unsigned char *>(g_memdup(cp_data_, cp_length_));
260 std::string image_comment() const override
262 return image_.get() ? image_->comment() : "";
265 void set_image_comment(const std::string& comment) override
268 image_->setComment(comment);
272 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data);
274 // This allows read-write access to the metadata
275 struct ExifDataProcessed : public ExifData
278 std::unique_ptr<ExifDataOriginal> imageData_;
279 std::unique_ptr<ExifDataOriginal> sidecarData_;
281 Exiv2::ExifData exifData_;
282 Exiv2::IptcData iptcData_;
283 Exiv2::XmpData xmpData_;
286 ExifDataProcessed(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
288 imageData_ = std::make_unique<ExifDataOriginal>(path);
289 sidecarData_ = nullptr;
292 sidecarData_ = std::make_unique<ExifDataOriginal>(sidecar_path);
293 xmpData_ = sidecarData_->xmpData();
297 xmpData_ = imageData_->xmpData();
300 exifData_ = imageData_->exifData();
301 iptcData_ = imageData_->iptcData();
304 syncExifWithXmp(exifData_, xmpData_);
308 DEBUG_1("Exiv2: Catching bug\n");
312 g_hash_table_foreach(modified_xmp, _ExifDataProcessed_update_xmp, this);
316 ExifData *original() override
318 return imageData_.get();
321 void writeMetadata(gchar *path = nullptr) override
325 if (options->metadata.save_legacy_IPTC)
326 copyXmpToIptc(xmpData_, iptcData_);
330 copyXmpToExif(xmpData_, exifData_);
331 Exiv2::Image *image = imageData_->image();
334 #ifdef HAVE_EXIV2_ERROR_CODE
335 # if EXIV2_TEST_VERSION(0,28,0)
336 throw Exiv2::Error(Exiv2::ErrorCode::kerInputDataReadFailed);
338 throw Exiv2::Error(Exiv2::kerInputDataReadFailed);
341 throw Exiv2::Error(21);
343 image->setExifData(exifData_);
344 image->setIptcData(iptcData_);
345 image->setXmpData(xmpData_);
346 image->writeMetadata();
350 gchar *pathl = path_from_utf8(path);;
352 auto sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
356 sidecar->setXmpData(xmpData_);
357 sidecar->writeMetadata();
361 Exiv2::Image *image() override
363 return imageData_->image();
366 Exiv2::ExifData &exifData () override
371 Exiv2::IptcData &iptcData () override
376 Exiv2::XmpData &xmpData () override
381 void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) override
383 imageData_->add_jpeg_color_profile(cp_data, cp_length);
386 guchar *get_jpeg_color_profile(guint *data_len) override
388 return imageData_->get_jpeg_color_profile(data_len);
391 std::string image_comment() const override
393 return imageData_->image_comment();
396 void set_image_comment(const std::string& comment) override
398 imageData_->set_image_comment(comment);
409 #ifdef EXV_ENABLE_NLS
410 bind_textdomain_codeset (EXV_PACKAGE, "UTF-8");
413 #ifdef EXV_ENABLE_BMFF
414 Exiv2::enableBMFF(TRUE);
420 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data)
422 exif_update_metadata(static_cast<ExifData *>(data), static_cast<gchar *>(key), static_cast<GList *>(value));
425 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
427 DEBUG_1("exif read %s, sidecar: %s", path, sidecar_path ? sidecar_path : "-");
429 return new ExifDataProcessed(path, sidecar_path, modified_xmp);
431 catch (Exiv2::AnyError& e) {
438 gboolean exif_write(ExifData *exif)
441 exif->writeMetadata();
444 catch (Exiv2::AnyError& e) {
450 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
453 exif->writeMetadata(path);
456 catch (Exiv2::AnyError& e) {
464 void exif_free(ExifData *exif)
467 g_assert(dynamic_cast<ExifDataProcessed *>(exif)); // this should not be called on ExifDataOriginal
471 ExifData *exif_get_original(ExifData *exif)
473 return exif->original();
477 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
480 Exiv2::Metadatum *item = nullptr;
482 Exiv2::ExifKey ekey(key);
483 auto pos = exif->exifData().findKey(ekey);
484 if (pos == exif->exifData().end()) return nullptr;
487 catch (Exiv2::AnyError& e) {
489 Exiv2::IptcKey ekey(key);
490 auto pos = exif->iptcData().findKey(ekey);
491 if (pos == exif->iptcData().end()) return nullptr;
494 catch (Exiv2::AnyError& e) {
495 Exiv2::XmpKey ekey(key);
496 auto pos = exif->xmpData().findKey(ekey);
497 if (pos == exif->xmpData().end()) return nullptr;
501 return reinterpret_cast<ExifItem *>(item);
503 catch (Exiv2::AnyError& e) {
509 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
512 Exiv2::Metadatum *item = nullptr;
514 Exiv2::ExifKey ekey(key);
515 exif->exifData().add(ekey, nullptr);
516 auto pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
520 catch (Exiv2::AnyError& e) {
522 Exiv2::IptcKey ekey(key);
523 exif->iptcData().add(ekey, nullptr);
524 auto pos = exif->iptcData().end();
528 catch (Exiv2::AnyError& e) {
529 Exiv2::XmpKey ekey(key);
530 exif->xmpData().add(ekey, nullptr);
531 auto pos = exif->xmpData().end();
536 return reinterpret_cast<ExifItem *>(item);
538 catch (Exiv2::AnyError& e) {
545 ExifItem *exif_get_first_item(ExifData *exif)
548 exif->exifIter = exif->exifData().begin();
549 exif->iptcIter = exif->iptcData().begin();
550 exif->xmpIter = exif->xmpData().begin();
551 if (exif->exifIter != exif->exifData().end())
553 const Exiv2::Metadatum *item = &*exif->exifIter;
555 return (ExifItem *)item;
557 if (exif->iptcIter != exif->iptcData().end())
559 const Exiv2::Metadatum *item = &*exif->iptcIter;
561 return (ExifItem *)item;
563 if (exif->xmpIter != exif->xmpData().end())
565 const Exiv2::Metadatum *item = &*exif->xmpIter;
567 return (ExifItem *)item;
572 catch (Exiv2::AnyError& e) {
578 ExifItem *exif_get_next_item(ExifData *exif)
581 if (exif->exifIter != exif->exifData().end())
583 const Exiv2::Metadatum *item = &*exif->exifIter;
585 return (ExifItem *)item;
587 if (exif->iptcIter != exif->iptcData().end())
589 const Exiv2::Metadatum *item = &*exif->iptcIter;
591 return (ExifItem *)item;
593 if (exif->xmpIter != exif->xmpData().end())
595 const Exiv2::Metadatum *item = &*exif->xmpIter;
597 return (ExifItem *)item;
601 catch (Exiv2::AnyError& e) {
607 char *exif_item_get_tag_name(ExifItem *item)
610 if (!item) return nullptr;
611 return g_strdup((reinterpret_cast<Exiv2::Metadatum *>(item))->key().c_str());
613 catch (Exiv2::AnyError& e) {
619 guint exif_item_get_tag_id(ExifItem *item)
623 return (reinterpret_cast<Exiv2::Metadatum *>(item))->tag();
625 catch (Exiv2::AnyError& e) {
631 guint exif_item_get_elements(ExifItem *item)
635 return (reinterpret_cast<Exiv2::Metadatum *>(item))->count();
637 catch (Exiv2::AnyError& e) {
643 char *exif_item_get_data(ExifItem *item, guint *data_len)
646 if (!item) return nullptr;
647 auto md = reinterpret_cast<Exiv2::Metadatum *>(item);
648 if (data_len) *data_len = md->size();
649 auto data = static_cast<char *>(g_malloc(md->size()));
650 auto res = md->copy(reinterpret_cast<Exiv2::byte *>(data), Exiv2::littleEndian /* should not matter */);
651 g_assert(res == md->size());
654 catch (Exiv2::AnyError& e) {
660 char *exif_item_get_description(ExifItem *item)
663 if (!item) return nullptr;
664 return utf8_validate_or_convert((reinterpret_cast<Exiv2::Metadatum *>(item))->tagLabel().c_str());
666 catch (std::exception& e) {
672 invalidTypeId, unsignedByte, asciiString, unsignedShort,
673 unsignedLong, unsignedRational, signedByte, undefined,
674 signedShort, signedLong, signedRational, string,
675 date, time, comment, directory,
676 xmpText, xmpAlt, xmpBag, xmpSeq,
680 static guint format_id_trans_tbl [] = {
682 EXIF_FORMAT_BYTE_UNSIGNED,
684 EXIF_FORMAT_SHORT_UNSIGNED,
685 EXIF_FORMAT_LONG_UNSIGNED,
686 EXIF_FORMAT_RATIONAL_UNSIGNED,
688 EXIF_FORMAT_UNDEFINED,
691 EXIF_FORMAT_RATIONAL,
695 EXIF_FORMAT_UNDEFINED,
704 guint exif_item_get_format_id(ExifItem *item)
707 if (!item) return EXIF_FORMAT_UNKNOWN;
708 guint id = (reinterpret_cast<Exiv2::Metadatum *>(item))->typeId();
709 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
710 return format_id_trans_tbl[id];
712 catch (Exiv2::AnyError& e) {
714 return EXIF_FORMAT_UNKNOWN;
718 const char *exif_item_get_format_name(ExifItem *item, gboolean)
721 if (!item) return nullptr;
722 return (reinterpret_cast<Exiv2::Metadatum *>(item))->typeName();
724 catch (Exiv2::AnyError& e) {
731 gchar *exif_item_get_data_as_text(ExifItem *item, ExifData *exif)
734 if (!item) return nullptr;
735 auto metadatum = reinterpret_cast<Exiv2::Metadatum *>(item);
736 return utf8_validate_or_convert(metadatum->print(&exif->exifData()).c_str());
738 catch (Exiv2::AnyError& e) {
743 gchar *exif_item_get_string(ExifItem *item, int idx)
746 if (!item) return nullptr;
747 auto em = reinterpret_cast<Exiv2::Metadatum *>(item);
748 std::string str = em->toString(idx);
749 if (idx == 0 && str.empty()) str = em->toString();
750 if (str.length() > 5 && str.substr(0, 5) == "lang=")
752 std::string::size_type pos = str.find_first_of(' ');
753 if (pos != std::string::npos) str = str.substr(pos+1);
756 return utf8_validate_or_convert(str.c_str());
758 catch (Exiv2::AnyError& e) {
764 gint exif_item_get_integer(ExifItem *item, gint *value)
767 if (!item || exif_item_get_elements(item) == 0) return 0;
769 #if EXIV2_TEST_VERSION(0,28,0)
770 *value = ((Exiv2::Metadatum *)item)->toInt64();
772 *value = (reinterpret_cast<Exiv2::Metadatum *>(item))->toLong();
776 catch (Exiv2::AnyError& e) {
782 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
785 if (!item) return nullptr;
786 if (n >= exif_item_get_elements(item)) return nullptr;
787 Exiv2::Rational v = (reinterpret_cast<Exiv2::Metadatum *>(item))->toRational(n);
788 static ExifRational ret;
791 if (sign) *sign = ((reinterpret_cast<Exiv2::Metadatum *>(item))->typeId() == Exiv2::signedRational);
794 catch (Exiv2::AnyError& e) {
800 gchar *exif_get_tag_description_by_key(const gchar *key)
803 Exiv2::ExifKey ekey(key);
804 return utf8_validate_or_convert(ekey.tagLabel().c_str());
806 catch (Exiv2::AnyError& e) {
808 Exiv2::IptcKey ikey(key);
809 return utf8_validate_or_convert(ikey.tagLabel().c_str());
811 catch (Exiv2::AnyError& e) {
813 Exiv2::XmpKey xkey(key);
814 return utf8_validate_or_convert(xkey.tagLabel().c_str());
816 catch (Exiv2::AnyError& e) {
825 static const AltKey *find_alt_key(const gchar *xmp_key)
827 for (const auto& k : alt_keys)
828 if (strcmp(xmp_key, k.xmp_key) == 0) return &k;
832 static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const GList *values)
835 const GList *work = values;
838 Exiv2::ExifKey ekey(key);
840 auto pos = exif->exifData().findKey(ekey);
841 while (pos != exif->exifData().end())
843 exif->exifData().erase(pos);
844 pos = exif->exifData().findKey(ekey);
849 exif->exifData()[key] = static_cast<gchar *>(work->data);
853 catch (Exiv2::AnyError& e) {
856 Exiv2::IptcKey ekey(key);
857 auto pos = exif->iptcData().findKey(ekey);
858 while (pos != exif->iptcData().end())
860 exif->iptcData().erase(pos);
861 pos = exif->iptcData().findKey(ekey);
866 exif->iptcData()[key] = static_cast<gchar *>(work->data);
870 catch (Exiv2::AnyError& e) {
871 Exiv2::XmpKey ekey(key);
872 auto pos = exif->xmpData().findKey(ekey);
873 while (pos != exif->xmpData().end())
875 exif->xmpData().erase(pos);
876 pos = exif->xmpData().findKey(ekey);
881 exif->xmpData()[key] = static_cast<gchar *>(work->data);
888 catch (Exiv2::AnyError& e) {
894 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
896 gint ret = exif_update_metadata_simple(exif, key, values);
899 !values || /* deleting item */
900 !ret /* writing to the explicitly given xmp tag failed */
903 /* deleted xmp metadatum can't be converted, we have to delete also the corresponding legacy tag */
904 /* if we can't write xmp, update at least the legacy tag */
905 const AltKey *alt_key = find_alt_key(key);
906 if (alt_key && alt_key->iptc_key)
907 ret = exif_update_metadata_simple(exif, alt_key->iptc_key, values);
909 if (alt_key && alt_key->exif_key)
910 ret = exif_update_metadata_simple(exif, alt_key->exif_key, values);
916 static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, MetadataFormat format, const Exiv2::ExifData *metadata)
918 Exiv2::TypeId id = item.typeId();
919 if (format == METADATA_FORMATTED ||
920 id == Exiv2::asciiString ||
921 id == Exiv2::undefined ||
922 id == Exiv2::string ||
925 id == Exiv2::xmpText ||
926 id == Exiv2::langAlt ||
930 /* read as a single entry */
933 if (format == METADATA_FORMATTED)
935 str = item.print(metadata);
936 if (str.length() > 1024)
938 /* truncate very long strings, they cause problems in gui */
945 str = item.toString();
947 if (str.length() > 5 && str.substr(0, 5) == "lang=")
949 std::string::size_type pos = str.find_first_of(' ');
950 if (pos != std::string::npos) str = str.substr(pos+1);
952 list = g_list_append(list, utf8_validate_or_convert(str.c_str()));
957 #if EXIV2_TEST_VERSION(0,28,0)
962 for (i = 0; i < item.count(); i++)
963 list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str()));
968 static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, MetadataFormat format)
970 GList *list = nullptr;
973 Exiv2::ExifKey ekey(key);
974 auto pos = exif->exifData().findKey(ekey);
975 if (pos != exif->exifData().end())
976 list = exif_add_value_to_glist(list, *pos, format, &exif->exifData());
979 catch (Exiv2::AnyError& e) {
981 Exiv2::IptcKey ekey(key);
982 auto pos = exif->iptcData().begin();
983 while (pos != exif->iptcData().end())
985 if (pos->key() == key)
986 list = exif_add_value_to_glist(list, *pos, format, nullptr);
991 catch (Exiv2::AnyError& e) {
992 Exiv2::XmpKey ekey(key);
993 auto pos = exif->xmpData().findKey(ekey);
994 if (pos != exif->xmpData().end())
995 list = exif_add_value_to_glist(list, *pos, format, nullptr);
999 catch (Exiv2::AnyError& e) {
1005 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1007 GList *list = nullptr;
1009 if (!key) return nullptr;
1011 if (format == METADATA_FORMATTED)
1015 text = exif_get_formatted_by_key(exif, key, &key_valid);
1016 if (key_valid) return g_list_append(nullptr, text);
1019 list = exif_get_metadata_simple(exif, key, format);
1021 /* the following code can be ifdefed out as soon as Exiv2 supports it */
1024 const AltKey *alt_key = find_alt_key(key);
1025 if (alt_key && alt_key->iptc_key)
1026 list = exif_get_metadata_simple(exif, alt_key->iptc_key, format);
1032 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
1034 exif->add_jpeg_color_profile(cp_data, cp_length);
1037 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1039 guchar *ret = exif->get_jpeg_color_profile(data_len);
1040 if (ret) return ret;
1042 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1043 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1044 ret = reinterpret_cast<guchar *>(exif_item_get_data(prof_item, data_len));
1048 gchar* exif_get_image_comment(FileData* fd)
1050 if (!fd || !fd->exif)
1051 return g_strdup("");
1053 return g_strdup(fd->exif->image_comment().c_str());
1056 void exif_set_image_comment(FileData* fd, const gchar* comment)
1058 if (!fd || !fd->exif)
1061 fd->exif->set_image_comment(comment ? comment : "");
1065 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1067 if (!exif) return nullptr;
1069 if (!exif->image()) return nullptr;
1071 std::string const path = exif->image()->io().path();
1072 /* given image pathname, first do simple (and fast) file extension test */
1073 gboolean is_raw = filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE);
1075 if (!is_raw && requested_width == 0) return nullptr;
1079 Exiv2::PreviewManager pm(*exif->image());
1081 Exiv2::PreviewPropertiesList list = pm.getPreviewProperties();
1085 Exiv2::PreviewPropertiesList::iterator pos;
1086 auto last = --list.end();
1088 if (requested_width == 0)
1090 pos = last; // the largest
1097 if (pos->width_ >= static_cast<uint32_t>(requested_width) &&
1098 pos->height_ >= static_cast<uint32_t>(requested_height)) break;
1102 // we are not interested in smaller thumbnails in normal image formats - we can use full image instead
1105 if (pos->width_ < static_cast<uint32_t>(requested_width) || pos->height_ < static_cast<uint32_t>(requested_height)) return nullptr;
1109 Exiv2::PreviewImage image = pm.getPreviewImage(*pos);
1111 // Let's not touch data_len until we finish copy.
1112 // Just in case we run into OOM.
1113 size_t img_sz = image.size();
1114 auto* b = new Exiv2::byte[img_sz];
1115 std::copy_n(image.pData(), img_sz, b);
1121 catch (Exiv2::AnyError& e) {
1127 void exif_free_preview(const guchar *buf)
1129 delete[] static_cast<const Exiv2::byte*>(buf);
1132 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */