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.
37 #include <exiv2/exiv2.hpp>
45 #include "filefilter.h"
49 #include "ui-fileops.h"
53 #if EXIV2_TEST_VERSION(0,27,0)
54 #define HAVE_EXIV2_ERROR_CODE
57 #if EXIV2_TEST_VERSION(0,27,0)
58 #define EXV_PACKAGE "exiv2"
61 #if EXIV2_TEST_VERSION(0,28,0)
62 #define AnyError Error
63 #define AutoPtr UniquePtr
69 const gchar *exif_key;
70 const gchar *iptc_key;
73 /* this is a list of keys that should be converted, even with the older Exiv2 which does not support it directly */
74 static constexpr AltKey alt_keys[] = {
75 {"Xmp.tiff.Orientation", "Exif.Image.Orientation", nullptr},
76 {"Xmp.dc.title", nullptr, "Iptc.Application2.ObjectName" },
77 {"Xmp.photoshop.Urgency", nullptr, "Iptc.Application2.Urgency" },
78 {"Xmp.photoshop.Category", nullptr, "Iptc.Application2.Category" },
79 {"Xmp.photoshop.SupplementalCategory", nullptr, "Iptc.Application2.SuppCategory" },
80 {"Xmp.dc.subject", nullptr, "Iptc.Application2.Keywords" },
81 {"Xmp.iptc.Location", nullptr, "Iptc.Application2.LocationName" },
82 {"Xmp.photoshop.Instruction", nullptr, "Iptc.Application2.SpecialInstructions" },
83 {"Xmp.photoshop.DateCreated", nullptr, "Iptc.Application2.DateCreated" },
84 {"Xmp.dc.creator", nullptr, "Iptc.Application2.Byline" },
85 {"Xmp.photoshop.AuthorsPosition", nullptr, "Iptc.Application2.BylineTitle" },
86 {"Xmp.photoshop.City", nullptr, "Iptc.Application2.City" },
87 {"Xmp.photoshop.State", nullptr, "Iptc.Application2.ProvinceState" },
88 {"Xmp.iptc.CountryCode", nullptr, "Iptc.Application2.CountryCode" },
89 {"Xmp.photoshop.Country", nullptr, "Iptc.Application2.CountryName" },
90 {"Xmp.photoshop.TransmissionReference", nullptr, "Iptc.Application2.TransmissionReference"},
91 {"Xmp.photoshop.Headline", nullptr, "Iptc.Application2.Headline" },
92 {"Xmp.photoshop.Credit", nullptr, "Iptc.Application2.Credit" },
93 {"Xmp.photoshop.Source", nullptr, "Iptc.Application2.Source" },
94 {"Xmp.dc.rights", nullptr, "Iptc.Application2.Copyright" },
95 {"Xmp.dc.description", nullptr, "Iptc.Application2.Caption" },
96 {"Xmp.photoshop.CaptionWriter", nullptr, "Iptc.Application2.Writer" },
99 static void _debug_exception(const char* file,
104 gchar *str = g_locale_from_utf8(e.what(), -1, nullptr, nullptr, nullptr);
105 DEBUG_1("%s:%d:%s:Exiv2: %s", file, line, func, str);
109 #define debug_exception(e) _debug_exception(__FILE__, __LINE__, __func__, e)
113 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
114 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
115 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
117 virtual ~ExifData() = default;
119 virtual void writeMetadata(gchar * = nullptr)
121 g_critical("Unsupported method of writing metadata");
124 virtual ExifData *original()
129 virtual Exiv2::Image *image() = 0;
131 virtual Exiv2::ExifData &exifData() = 0;
133 virtual Exiv2::IptcData &iptcData() = 0;
135 virtual Exiv2::XmpData &xmpData() = 0;
137 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) = 0;
139 virtual guchar *get_jpeg_color_profile(guint *data_len) = 0;
141 virtual std::string image_comment() const = 0;
143 virtual void set_image_comment(const std::string& comment) = 0;
146 // This allows read-only access to the original metadata
147 struct ExifDataOriginal : public ExifData
150 Exiv2::Image::AutoPtr image_;
152 /* the icc profile in jpeg is not technically exif - store it here */
153 unsigned char *cp_data_;
158 Exiv2::ExifData emptyExifData_;
159 Exiv2::IptcData emptyIptcData_;
160 Exiv2::XmpData emptyXmpData_;
163 ExifDataOriginal(Exiv2::Image::AutoPtr image)
167 image_ = std::move(image);
171 ExifDataOriginal(gchar *path)
177 pathl_ = path_from_utf8(path);
180 image_ = Exiv2::ImageFactory::open(pathl_);
181 image_->readMetadata();
183 if (image_->mimeType() == "application/rdf+xml")
185 //Exiv2 sidecar converts xmp to exif and iptc, we don't want it.
186 image_->clearExifData();
187 image_->clearIptcData();
190 if (image_->mimeType() == "image/jpeg")
192 /* try to get jpeg color profile */
193 Exiv2::BasicIo &io = image_->io();
194 gint open = io.isopen();
195 if (!open) io.open();
198 auto mapped = static_cast<unsigned char*>(io.mmap());
199 if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
202 if (!open) io.close();
205 catch (Exiv2::AnyError& e)
211 ~ExifDataOriginal() override
213 if (cp_data_) g_free(cp_data_);
214 if (pathl_) g_free(pathl_);
217 Exiv2::Image *image() override
219 if (!valid_) return nullptr;
223 Exiv2::ExifData &exifData () override
225 if (!valid_) return emptyExifData_;
226 return image_->exifData();
229 Exiv2::IptcData &iptcData () override
231 if (!valid_) return emptyIptcData_;
232 return image_->iptcData();
235 Exiv2::XmpData &xmpData () override
237 if (!valid_) return emptyXmpData_;
238 return image_->xmpData();
241 void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) override
243 if (cp_data_) g_free(cp_data_);
245 cp_length_ = cp_length;
248 guchar *get_jpeg_color_profile(guint *data_len) override
252 if (data_len) *data_len = cp_length_;
253 #if GLIB_CHECK_VERSION(2,68,0)
254 return static_cast<unsigned char *>(g_memdup2(cp_data_, cp_length_));
256 return static_cast<unsigned char *>(g_memdup(cp_data_, cp_length_));
262 std::string image_comment() const override
264 return image_.get() ? image_->comment() : "";
267 void set_image_comment(const std::string& comment) override
270 image_->setComment(comment);
274 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data);
276 // This allows read-write access to the metadata
277 struct ExifDataProcessed : public ExifData
280 std::unique_ptr<ExifDataOriginal> imageData_;
281 std::unique_ptr<ExifDataOriginal> sidecarData_;
283 Exiv2::ExifData exifData_;
284 Exiv2::IptcData iptcData_;
285 Exiv2::XmpData xmpData_;
288 ExifDataProcessed(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
290 imageData_ = std::make_unique<ExifDataOriginal>(path);
291 sidecarData_ = nullptr;
294 sidecarData_ = std::make_unique<ExifDataOriginal>(sidecar_path);
295 xmpData_ = sidecarData_->xmpData();
299 xmpData_ = imageData_->xmpData();
302 exifData_ = imageData_->exifData();
303 iptcData_ = imageData_->iptcData();
306 syncExifWithXmp(exifData_, xmpData_);
310 DEBUG_1("Exiv2: Catching bug\n");
314 g_hash_table_foreach(modified_xmp, _ExifDataProcessed_update_xmp, this);
318 ExifData *original() override
320 return imageData_.get();
323 void writeMetadata(gchar *path = nullptr) override
327 if (options->metadata.save_legacy_IPTC)
328 copyXmpToIptc(xmpData_, iptcData_);
332 copyXmpToExif(xmpData_, exifData_);
333 Exiv2::Image *image = imageData_->image();
335 #ifdef HAVE_EXIV2_ERROR_CODE
336 #if EXIV2_TEST_VERSION(0,28,0)
337 if (!image) throw Exiv2::Error(Exiv2::ErrorCode::kerInputDataReadFailed);
339 if (!image) throw Exiv2::Error(Exiv2::kerInputDataReadFailed);
342 if (!image) throw Exiv2::Error(21);
344 image->setExifData(exifData_);
345 image->setIptcData(iptcData_);
346 image->setXmpData(xmpData_);
347 image->writeMetadata();
351 gchar *pathl = path_from_utf8(path);;
353 auto sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
357 sidecar->setXmpData(xmpData_);
358 sidecar->writeMetadata();
362 Exiv2::Image *image() override
364 return imageData_->image();
367 Exiv2::ExifData &exifData () override
372 Exiv2::IptcData &iptcData () override
377 Exiv2::XmpData &xmpData () override
382 void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) override
384 imageData_->add_jpeg_color_profile(cp_data, cp_length);
387 guchar *get_jpeg_color_profile(guint *data_len) override
389 return imageData_->get_jpeg_color_profile(data_len);
392 std::string image_comment() const override
394 return imageData_->image_comment();
397 void set_image_comment(const std::string& comment) override
399 imageData_->set_image_comment(comment);
410 #ifdef EXV_ENABLE_NLS
411 bind_textdomain_codeset (EXV_PACKAGE, "UTF-8");
414 #ifdef EXV_ENABLE_BMFF
415 Exiv2::enableBMFF(TRUE);
421 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data)
423 exif_update_metadata(static_cast<ExifData *>(data), static_cast<gchar *>(key), static_cast<GList *>(value));
426 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
428 DEBUG_1("exif read %s, sidecar: %s", path, sidecar_path ? sidecar_path : "-");
430 return new ExifDataProcessed(path, sidecar_path, modified_xmp);
432 catch (Exiv2::AnyError& e) {
439 gboolean exif_write(ExifData *exif)
442 exif->writeMetadata();
445 catch (Exiv2::AnyError& e) {
451 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
454 exif->writeMetadata(path);
457 catch (Exiv2::AnyError& e) {
465 void exif_free(ExifData *exif)
468 g_assert(dynamic_cast<ExifDataProcessed *>(exif)); // this should not be called on ExifDataOriginal
472 ExifData *exif_get_original(ExifData *exif)
474 return exif->original();
478 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
481 Exiv2::Metadatum *item = nullptr;
483 Exiv2::ExifKey ekey(key);
484 auto pos = exif->exifData().findKey(ekey);
485 if (pos == exif->exifData().end()) return nullptr;
488 catch (Exiv2::AnyError& e) {
490 Exiv2::IptcKey ekey(key);
491 auto pos = exif->iptcData().findKey(ekey);
492 if (pos == exif->iptcData().end()) return nullptr;
495 catch (Exiv2::AnyError& e) {
496 Exiv2::XmpKey ekey(key);
497 auto pos = exif->xmpData().findKey(ekey);
498 if (pos == exif->xmpData().end()) return nullptr;
502 return reinterpret_cast<ExifItem *>(item);
504 catch (Exiv2::AnyError& e) {
510 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
513 Exiv2::Metadatum *item = nullptr;
515 Exiv2::ExifKey ekey(key);
516 exif->exifData().add(ekey, nullptr);
517 auto pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
521 catch (Exiv2::AnyError& e) {
523 Exiv2::IptcKey ekey(key);
524 exif->iptcData().add(ekey, nullptr);
525 auto pos = exif->iptcData().end();
529 catch (Exiv2::AnyError& e) {
530 Exiv2::XmpKey ekey(key);
531 exif->xmpData().add(ekey, nullptr);
532 auto pos = exif->xmpData().end();
537 return reinterpret_cast<ExifItem *>(item);
539 catch (Exiv2::AnyError& e) {
546 ExifItem *exif_get_first_item(ExifData *exif)
549 exif->exifIter = exif->exifData().begin();
550 exif->iptcIter = exif->iptcData().begin();
551 exif->xmpIter = exif->xmpData().begin();
552 if (exif->exifIter != exif->exifData().end())
554 const Exiv2::Metadatum *item = &*exif->exifIter;
556 return (ExifItem *)item;
558 if (exif->iptcIter != exif->iptcData().end())
560 const Exiv2::Metadatum *item = &*exif->iptcIter;
562 return (ExifItem *)item;
564 if (exif->xmpIter != exif->xmpData().end())
566 const Exiv2::Metadatum *item = &*exif->xmpIter;
568 return (ExifItem *)item;
573 catch (Exiv2::AnyError& e) {
579 ExifItem *exif_get_next_item(ExifData *exif)
582 if (exif->exifIter != exif->exifData().end())
584 const Exiv2::Metadatum *item = &*exif->exifIter;
586 return (ExifItem *)item;
588 if (exif->iptcIter != exif->iptcData().end())
590 const Exiv2::Metadatum *item = &*exif->iptcIter;
592 return (ExifItem *)item;
594 if (exif->xmpIter != exif->xmpData().end())
596 const Exiv2::Metadatum *item = &*exif->xmpIter;
598 return (ExifItem *)item;
602 catch (Exiv2::AnyError& e) {
608 char *exif_item_get_tag_name(ExifItem *item)
611 if (!item) return nullptr;
612 return g_strdup((reinterpret_cast<Exiv2::Metadatum *>(item))->key().c_str());
614 catch (Exiv2::AnyError& e) {
620 guint exif_item_get_tag_id(ExifItem *item)
624 return (reinterpret_cast<Exiv2::Metadatum *>(item))->tag();
626 catch (Exiv2::AnyError& e) {
632 guint exif_item_get_elements(ExifItem *item)
636 return (reinterpret_cast<Exiv2::Metadatum *>(item))->count();
638 catch (Exiv2::AnyError& e) {
644 char *exif_item_get_data(ExifItem *item, guint *data_len)
647 if (!item) return nullptr;
648 auto md = reinterpret_cast<Exiv2::Metadatum *>(item);
649 if (data_len) *data_len = md->size();
650 auto data = static_cast<char *>(g_malloc(md->size()));
651 long res = md->copy(reinterpret_cast<Exiv2::byte *>(data), Exiv2::littleEndian /* should not matter */);
652 g_assert(res == md->size());
655 catch (Exiv2::AnyError& e) {
661 char *exif_item_get_description(ExifItem *item)
664 if (!item) return nullptr;
665 return utf8_validate_or_convert((reinterpret_cast<Exiv2::Metadatum *>(item))->tagLabel().c_str());
667 catch (std::exception& e) {
673 invalidTypeId, unsignedByte, asciiString, unsignedShort,
674 unsignedLong, unsignedRational, signedByte, undefined,
675 signedShort, signedLong, signedRational, string,
676 date, time, comment, directory,
677 xmpText, xmpAlt, xmpBag, xmpSeq,
681 static guint format_id_trans_tbl [] = {
683 EXIF_FORMAT_BYTE_UNSIGNED,
685 EXIF_FORMAT_SHORT_UNSIGNED,
686 EXIF_FORMAT_LONG_UNSIGNED,
687 EXIF_FORMAT_RATIONAL_UNSIGNED,
689 EXIF_FORMAT_UNDEFINED,
692 EXIF_FORMAT_RATIONAL,
696 EXIF_FORMAT_UNDEFINED,
705 guint exif_item_get_format_id(ExifItem *item)
708 if (!item) return EXIF_FORMAT_UNKNOWN;
709 guint id = (reinterpret_cast<Exiv2::Metadatum *>(item))->typeId();
710 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
711 return format_id_trans_tbl[id];
713 catch (Exiv2::AnyError& e) {
715 return EXIF_FORMAT_UNKNOWN;
719 const char *exif_item_get_format_name(ExifItem *item, gboolean)
722 if (!item) return nullptr;
723 return (reinterpret_cast<Exiv2::Metadatum *>(item))->typeName();
725 catch (Exiv2::AnyError& e) {
732 gchar *exif_item_get_data_as_text(ExifItem *item, ExifData *exif)
735 if (!item) return nullptr;
736 auto metadatum = reinterpret_cast<Exiv2::Metadatum *>(item);
737 return utf8_validate_or_convert(metadatum->print(&exif->exifData()).c_str());
739 catch (Exiv2::AnyError& e) {
744 gchar *exif_item_get_string(ExifItem *item, int idx)
747 if (!item) return nullptr;
748 auto em = reinterpret_cast<Exiv2::Metadatum *>(item);
749 std::string str = em->toString(idx);
750 if (idx == 0 && str.empty()) str = em->toString();
751 if (str.length() > 5 && str.substr(0, 5) == "lang=")
753 std::string::size_type pos = str.find_first_of(' ');
754 if (pos != std::string::npos) str = str.substr(pos+1);
757 return utf8_validate_or_convert(str.c_str());
759 catch (Exiv2::AnyError& e) {
765 gint exif_item_get_integer(ExifItem *item, gint *value)
768 if (!item || exif_item_get_elements(item) == 0) return 0;
770 #if EXIV2_TEST_VERSION(0,28,0)
771 *value = ((Exiv2::Metadatum *)item)->toInt64();
773 *value = (reinterpret_cast<Exiv2::Metadatum *>(item))->toLong();
777 catch (Exiv2::AnyError& e) {
783 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
786 if (!item) return nullptr;
787 if (n >= exif_item_get_elements(item)) return nullptr;
788 Exiv2::Rational v = (reinterpret_cast<Exiv2::Metadatum *>(item))->toRational(n);
789 static ExifRational ret;
792 if (sign) *sign = ((reinterpret_cast<Exiv2::Metadatum *>(item))->typeId() == Exiv2::signedRational);
795 catch (Exiv2::AnyError& e) {
801 gchar *exif_get_tag_description_by_key(const gchar *key)
804 Exiv2::ExifKey ekey(key);
805 return utf8_validate_or_convert(ekey.tagLabel().c_str());
807 catch (Exiv2::AnyError& e) {
809 Exiv2::IptcKey ikey(key);
810 return utf8_validate_or_convert(ikey.tagLabel().c_str());
812 catch (Exiv2::AnyError& e) {
814 Exiv2::XmpKey xkey(key);
815 return utf8_validate_or_convert(xkey.tagLabel().c_str());
817 catch (Exiv2::AnyError& e) {
826 static const AltKey *find_alt_key(const gchar *xmp_key)
828 for (const auto& k : alt_keys)
829 if (strcmp(xmp_key, k.xmp_key) == 0) return &k;
833 static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const GList *values)
836 const GList *work = values;
839 Exiv2::ExifKey ekey(key);
841 auto pos = exif->exifData().findKey(ekey);
842 while (pos != exif->exifData().end())
844 exif->exifData().erase(pos);
845 pos = exif->exifData().findKey(ekey);
850 exif->exifData()[key] = static_cast<gchar *>(work->data);
854 catch (Exiv2::AnyError& e) {
857 Exiv2::IptcKey ekey(key);
858 auto pos = exif->iptcData().findKey(ekey);
859 while (pos != exif->iptcData().end())
861 exif->iptcData().erase(pos);
862 pos = exif->iptcData().findKey(ekey);
867 exif->iptcData()[key] = static_cast<gchar *>(work->data);
871 catch (Exiv2::AnyError& e) {
872 Exiv2::XmpKey ekey(key);
873 auto pos = exif->xmpData().findKey(ekey);
874 while (pos != exif->xmpData().end())
876 exif->xmpData().erase(pos);
877 pos = exif->xmpData().findKey(ekey);
882 exif->xmpData()[key] = static_cast<gchar *>(work->data);
889 catch (Exiv2::AnyError& e) {
895 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
897 gint ret = exif_update_metadata_simple(exif, key, values);
900 !values || /* deleting item */
901 !ret /* writing to the explicitly given xmp tag failed */
904 /* deleted xmp metadatum can't be converted, we have to delete also the corresponding legacy tag */
905 /* if we can't write xmp, update at least the legacy tag */
906 const AltKey *alt_key = find_alt_key(key);
907 if (alt_key && alt_key->iptc_key)
908 ret = exif_update_metadata_simple(exif, alt_key->iptc_key, values);
910 if (alt_key && alt_key->exif_key)
911 ret = exif_update_metadata_simple(exif, alt_key->exif_key, values);
917 static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, MetadataFormat format, const Exiv2::ExifData *metadata)
919 Exiv2::TypeId id = item.typeId();
920 if (format == METADATA_FORMATTED ||
921 id == Exiv2::asciiString ||
922 id == Exiv2::undefined ||
923 id == Exiv2::string ||
926 id == Exiv2::xmpText ||
927 id == Exiv2::langAlt ||
931 /* read as a single entry */
934 if (format == METADATA_FORMATTED)
936 str = item.print(metadata);
937 if (str.length() > 1024)
939 /* truncate very long strings, they cause problems in gui */
946 str = item.toString();
948 if (str.length() > 5 && str.substr(0, 5) == "lang=")
950 std::string::size_type pos = str.find_first_of(' ');
951 if (pos != std::string::npos) str = str.substr(pos+1);
953 list = g_list_append(list, utf8_validate_or_convert(str.c_str()));
959 for (i = 0; i < item.count(); i++)
960 list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str()));
965 static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, MetadataFormat format)
967 GList *list = nullptr;
970 Exiv2::ExifKey ekey(key);
971 auto pos = exif->exifData().findKey(ekey);
972 if (pos != exif->exifData().end())
973 list = exif_add_value_to_glist(list, *pos, format, &exif->exifData());
976 catch (Exiv2::AnyError& e) {
978 Exiv2::IptcKey ekey(key);
979 auto pos = exif->iptcData().begin();
980 while (pos != exif->iptcData().end())
982 if (pos->key() == key)
983 list = exif_add_value_to_glist(list, *pos, format, nullptr);
988 catch (Exiv2::AnyError& e) {
989 Exiv2::XmpKey ekey(key);
990 auto pos = exif->xmpData().findKey(ekey);
991 if (pos != exif->xmpData().end())
992 list = exif_add_value_to_glist(list, *pos, format, nullptr);
996 catch (Exiv2::AnyError& e) {
1002 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1004 GList *list = nullptr;
1006 if (!key) return nullptr;
1008 if (format == METADATA_FORMATTED)
1012 text = exif_get_formatted_by_key(exif, key, &key_valid);
1013 if (key_valid) return g_list_append(nullptr, text);
1016 list = exif_get_metadata_simple(exif, key, format);
1018 /* the following code can be ifdefed out as soon as Exiv2 supports it */
1021 const AltKey *alt_key = find_alt_key(key);
1022 if (alt_key && alt_key->iptc_key)
1023 list = exif_get_metadata_simple(exif, alt_key->iptc_key, format);
1029 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
1031 exif->add_jpeg_color_profile(cp_data, cp_length);
1034 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1036 guchar *ret = exif->get_jpeg_color_profile(data_len);
1037 if (ret) return ret;
1039 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1040 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1041 ret = reinterpret_cast<guchar *>(exif_item_get_data(prof_item, data_len));
1045 gchar* exif_get_image_comment(FileData* fd)
1047 if (!fd || !fd->exif)
1048 return g_strdup("");
1050 return g_strdup(fd->exif->image_comment().c_str());
1053 void exif_set_image_comment(FileData* fd, const gchar* comment)
1055 if (!fd || !fd->exif)
1058 fd->exif->set_image_comment(comment ? comment : "");
1062 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1064 if (!exif) return nullptr;
1066 if (!exif->image()) return nullptr;
1068 std::string const path = exif->image()->io().path();
1069 /* given image pathname, first do simple (and fast) file extension test */
1070 gboolean is_raw = filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE);
1072 if (!is_raw && requested_width == 0) return nullptr;
1076 Exiv2::PreviewManager pm(*exif->image());
1078 Exiv2::PreviewPropertiesList list = pm.getPreviewProperties();
1082 Exiv2::PreviewPropertiesList::iterator pos;
1083 auto last = --list.end();
1085 if (requested_width == 0)
1087 pos = last; // the largest
1094 if (pos->width_ >= static_cast<uint32_t>(requested_width) &&
1095 pos->height_ >= static_cast<uint32_t>(requested_height)) break;
1099 // we are not interested in smaller thumbnails in normal image formats - we can use full image instead
1102 if (pos->width_ < static_cast<uint32_t>(requested_width) || pos->height_ < static_cast<uint32_t>(requested_height)) return nullptr;
1106 Exiv2::PreviewImage image = pm.getPreviewImage(*pos);
1108 // Let's not touch data_len until we finish copy.
1109 // Just in case we run into OOM.
1110 size_t img_sz = image.size();
1111 auto* b = new Exiv2::byte[img_sz];
1112 std::copy_n(image.pData(), img_sz, b);
1118 catch (Exiv2::AnyError& e) {
1124 void exif_free_preview(const guchar *buf)
1126 delete[] static_cast<const Exiv2::byte*>(buf);
1129 #endif /* HAVE_EXIV2 */
1130 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */