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 #include <exiv2/image.hpp>
26 #include <exiv2/exif.hpp>
30 // EXIV2_TEST_VERSION is defined in Exiv2 0.15 and newer.
31 #ifndef EXIV2_TEST_VERSION
32 # define EXIV2_TEST_VERSION(major,minor,patch) \
33 ( EXIV2_VERSION >= EXIV2_MAKE_VERSION(major,minor,patch) )
37 #include <sys/types.h>
43 #if !EXIV2_TEST_VERSION(0,17,90)
44 #include <exiv2/tiffparser.hpp>
45 #include <exiv2/tiffcomposite.hpp>
46 #include <exiv2/tiffvisitor.hpp>
47 #include <exiv2/tiffimage.hpp>
48 #include <exiv2/cr2image.hpp>
49 #include <exiv2/crwimage.hpp>
50 #if EXIV2_TEST_VERSION(0,16,0)
51 #include <exiv2/orfimage.hpp>
53 #if EXIV2_TEST_VERSION(0,13,0)
54 #include <exiv2/rafimage.hpp>
56 #include <exiv2/futils.hpp>
58 #include <exiv2/preview.hpp>
61 #if EXIV2_TEST_VERSION(0,17,0)
62 #include <exiv2/convert.hpp>
63 #include <exiv2/xmpsidecar.hpp>
72 #include "filefilter.h"
73 #include "ui_fileops.h"
78 typedef struct _AltKey AltKey;
83 const gchar *exif_key;
84 const gchar *iptc_key;
87 /* this is a list of keys that should be converted, even with the older Exiv2 which does not support it directly */
88 static const AltKey alt_keys[] = {
89 {"Xmp.tiff.Orientation", "Exif.Image.Orientation", NULL},
90 {"Xmp.dc.title", NULL, "Iptc.Application2.ObjectName" },
91 {"Xmp.photoshop.Urgency", NULL, "Iptc.Application2.Urgency" },
92 {"Xmp.photoshop.Category", NULL, "Iptc.Application2.Category" },
93 {"Xmp.photoshop.SupplementalCategory", NULL, "Iptc.Application2.SuppCategory" },
94 {"Xmp.dc.subject", NULL, "Iptc.Application2.Keywords" },
95 {"Xmp.iptc.Location", NULL, "Iptc.Application2.LocationName" },
96 {"Xmp.photoshop.Instruction", NULL, "Iptc.Application2.SpecialInstructions" },
97 {"Xmp.photoshop.DateCreated", NULL, "Iptc.Application2.DateCreated" },
98 {"Xmp.dc.creator", NULL, "Iptc.Application2.Byline" },
99 {"Xmp.photoshop.AuthorsPosition", NULL, "Iptc.Application2.BylineTitle" },
100 {"Xmp.photoshop.City", NULL, "Iptc.Application2.City" },
101 {"Xmp.photoshop.State", NULL, "Iptc.Application2.ProvinceState" },
102 {"Xmp.iptc.CountryCode", NULL, "Iptc.Application2.CountryCode" },
103 {"Xmp.photoshop.Country", NULL, "Iptc.Application2.CountryName" },
104 {"Xmp.photoshop.TransmissionReference", NULL, "Iptc.Application2.TransmissionReference"},
105 {"Xmp.photoshop.Headline", NULL, "Iptc.Application2.Headline" },
106 {"Xmp.photoshop.Credit", NULL, "Iptc.Application2.Credit" },
107 {"Xmp.photoshop.Source", NULL, "Iptc.Application2.Source" },
108 {"Xmp.dc.rights", NULL, "Iptc.Application2.Copyright" },
109 {"Xmp.dc.description", NULL, "Iptc.Application2.Caption" },
110 {"Xmp.photoshop.CaptionWriter", NULL, "Iptc.Application2.Writer" },
114 static void _debug_exception(const char* file,
119 gchar *str = g_locale_from_utf8(e.what(), -1, NULL, NULL, NULL);
120 DEBUG_1("%s:%d:%s:Exiv2: %s", file, line, func, str);
124 #define debug_exception(e) _debug_exception(__FILE__, __LINE__, __func__, e)
128 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
129 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
130 #if EXIV2_TEST_VERSION(0,16,0)
131 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
138 virtual void writeMetadata(gchar *path = NULL)
140 g_critical("Unsupported method of writing metadata");
143 virtual ExifData *original()
148 virtual Exiv2::Image *image() = 0;
150 virtual Exiv2::ExifData &exifData() = 0;
152 virtual Exiv2::IptcData &iptcData() = 0;
154 #if EXIV2_TEST_VERSION(0,16,0)
155 virtual Exiv2::XmpData &xmpData() = 0;
158 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) = 0;
160 virtual guchar *get_jpeg_color_profile(guint *data_len) = 0;
162 virtual std::string image_comment() const = 0;
164 virtual void set_image_comment(const std::string& comment) = 0;
167 // This allows read-only access to the original metadata
168 struct _ExifDataOriginal : public _ExifData
171 Exiv2::Image::AutoPtr image_;
173 /* the icc profile in jpeg is not technically exif - store it here */
174 unsigned char *cp_data_;
179 Exiv2::ExifData emptyExifData_;
180 Exiv2::IptcData emptyIptcData_;
181 #if EXIV2_TEST_VERSION(0,16,0)
182 Exiv2::XmpData emptyXmpData_;
186 _ExifDataOriginal(Exiv2::Image::AutoPtr image)
194 _ExifDataOriginal(gchar *path)
200 pathl_ = path_from_utf8(path);
203 image_ = Exiv2::ImageFactory::open(pathl_);
204 // g_assert (image.get() != 0);
205 image_->readMetadata();
207 #if EXIV2_TEST_VERSION(0,16,0)
208 if (image_->mimeType() == "application/rdf+xml")
210 //Exiv2 sidecar converts xmp to exif and iptc, we don't want it.
211 image_->clearExifData();
212 image_->clearIptcData();
216 #if EXIV2_TEST_VERSION(0,14,0)
217 if (image_->mimeType() == "image/jpeg")
219 /* try to get jpeg color profile */
220 Exiv2::BasicIo &io = image_->io();
221 gint open = io.isopen();
222 if (!open) io.open();
225 unsigned char *mapped = (unsigned char*)io.mmap();
226 if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
229 if (!open) io.close();
233 catch (Exiv2::AnyError& e)
239 virtual ~_ExifDataOriginal()
241 if (cp_data_) g_free(cp_data_);
242 if (pathl_) g_free(pathl_);
245 virtual Exiv2::Image *image()
247 if (!valid_) return NULL;
251 virtual Exiv2::ExifData &exifData ()
253 if (!valid_) return emptyExifData_;
254 return image_->exifData();
257 virtual Exiv2::IptcData &iptcData ()
259 if (!valid_) return emptyIptcData_;
260 return image_->iptcData();
263 #if EXIV2_TEST_VERSION(0,16,0)
264 virtual Exiv2::XmpData &xmpData ()
266 if (!valid_) return emptyXmpData_;
267 return image_->xmpData();
271 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
273 if (cp_data_) g_free(cp_data_);
275 cp_length_ = cp_length;
278 virtual guchar *get_jpeg_color_profile(guint *data_len)
282 if (data_len) *data_len = cp_length_;
283 return (unsigned char *) g_memdup(cp_data_, cp_length_);
288 virtual std::string image_comment() const
290 return image_.get() ? image_->comment() : "";
293 virtual void set_image_comment(const std::string& comment)
296 image_->setComment(comment);
301 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data);
304 // This allows read-write access to the metadata
305 struct _ExifDataProcessed : public _ExifData
308 _ExifDataOriginal *imageData_;
309 _ExifDataOriginal *sidecarData_;
311 Exiv2::ExifData exifData_;
312 Exiv2::IptcData iptcData_;
313 #if EXIV2_TEST_VERSION(0,16,0)
314 Exiv2::XmpData xmpData_;
318 _ExifDataProcessed(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
320 imageData_ = new _ExifDataOriginal(path);
322 #if EXIV2_TEST_VERSION(0,16,0)
325 sidecarData_ = new _ExifDataOriginal(sidecar_path);
326 xmpData_ = sidecarData_->xmpData();
330 xmpData_ = imageData_->xmpData();
334 exifData_ = imageData_->exifData();
335 iptcData_ = imageData_->iptcData();
336 #if EXIV2_TEST_VERSION(0,17,0)
339 syncExifWithXmp(exifData_, xmpData_);
343 DEBUG_1("Exiv2: Catching bug\n");
348 g_hash_table_foreach(modified_xmp, _ExifDataProcessed_update_xmp, this);
352 virtual ~_ExifDataProcessed()
354 if (imageData_) delete imageData_;
355 if (sidecarData_) delete sidecarData_;
358 virtual ExifData *original()
363 virtual void writeMetadata(gchar *path = NULL)
367 #if EXIV2_TEST_VERSION(0,17,0)
368 if (options->metadata.save_legacy_IPTC)
369 copyXmpToIptc(xmpData_, iptcData_);
373 copyXmpToExif(xmpData_, exifData_);
375 Exiv2::Image *image = imageData_->image();
377 if (!image) Exiv2::Error(21);
378 image->setExifData(exifData_);
379 image->setIptcData(iptcData_);
380 #if EXIV2_TEST_VERSION(0,16,0)
381 image->setXmpData(xmpData_);
383 image->writeMetadata();
387 #if EXIV2_TEST_VERSION(0,17,0)
388 gchar *pathl = path_from_utf8(path);;
390 Exiv2::Image::AutoPtr sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
394 sidecar->setXmpData(xmpData_);
395 sidecar->writeMetadata();
397 throw Exiv2::Error(3, "xmp");
402 virtual Exiv2::Image *image()
404 return imageData_->image();
407 virtual Exiv2::ExifData &exifData ()
412 virtual Exiv2::IptcData &iptcData ()
417 #if EXIV2_TEST_VERSION(0,16,0)
418 virtual Exiv2::XmpData &xmpData ()
424 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
426 imageData_->add_jpeg_color_profile(cp_data, cp_length);
429 virtual guchar *get_jpeg_color_profile(guint *data_len)
431 return imageData_->get_jpeg_color_profile(data_len);
434 virtual std::string image_comment() const
436 return imageData_->image_comment();
439 virtual void set_image_comment(const std::string& comment)
441 imageData_->set_image_comment(comment);
453 #ifdef EXV_ENABLE_NLS
454 bind_textdomain_codeset (EXV_PACKAGE, "UTF-8");
460 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data)
462 exif_update_metadata((ExifData *)data, (gchar *)key, (GList *)value);
465 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
467 DEBUG_1("exif read %s, sidecar: %s", path, sidecar_path ? sidecar_path : "-");
469 return new _ExifDataProcessed(path, sidecar_path, modified_xmp);
471 catch (Exiv2::AnyError& e) {
478 gboolean exif_write(ExifData *exif)
481 exif->writeMetadata();
484 catch (Exiv2::AnyError& e) {
490 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
493 exif->writeMetadata(path);
496 catch (Exiv2::AnyError& e) {
504 void exif_free(ExifData *exif)
507 g_assert(dynamic_cast<_ExifDataProcessed *>(exif)); // this should not be called on ExifDataOriginal
511 ExifData *exif_get_original(ExifData *exif)
513 return exif->original();
517 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
520 Exiv2::Metadatum *item = NULL;
522 Exiv2::ExifKey ekey(key);
523 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
524 if (pos == exif->exifData().end()) return NULL;
527 catch (Exiv2::AnyError& e) {
529 Exiv2::IptcKey ekey(key);
530 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
531 if (pos == exif->iptcData().end()) return NULL;
534 catch (Exiv2::AnyError& e) {
535 #if EXIV2_TEST_VERSION(0,16,0)
536 Exiv2::XmpKey ekey(key);
537 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
538 if (pos == exif->xmpData().end()) return NULL;
543 return (ExifItem *)item;
545 catch (Exiv2::AnyError& e) {
551 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
554 Exiv2::Metadatum *item = NULL;
556 Exiv2::ExifKey ekey(key);
557 exif->exifData().add(ekey, NULL);
558 Exiv2::ExifData::iterator pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
562 catch (Exiv2::AnyError& e) {
564 Exiv2::IptcKey ekey(key);
565 exif->iptcData().add(ekey, NULL);
566 Exiv2::IptcData::iterator pos = exif->iptcData().end();
570 catch (Exiv2::AnyError& e) {
571 #if EXIV2_TEST_VERSION(0,16,0)
572 Exiv2::XmpKey ekey(key);
573 exif->xmpData().add(ekey, NULL);
574 Exiv2::XmpData::iterator pos = exif->xmpData().end();
580 return (ExifItem *)item;
582 catch (Exiv2::AnyError& e) {
589 ExifItem *exif_get_first_item(ExifData *exif)
592 exif->exifIter = exif->exifData().begin();
593 exif->iptcIter = exif->iptcData().begin();
594 #if EXIV2_TEST_VERSION(0,16,0)
595 exif->xmpIter = exif->xmpData().begin();
597 if (exif->exifIter != exif->exifData().end())
599 const Exiv2::Metadatum *item = &*exif->exifIter;
601 return (ExifItem *)item;
603 if (exif->iptcIter != exif->iptcData().end())
605 const Exiv2::Metadatum *item = &*exif->iptcIter;
607 return (ExifItem *)item;
609 #if EXIV2_TEST_VERSION(0,16,0)
610 if (exif->xmpIter != exif->xmpData().end())
612 const Exiv2::Metadatum *item = &*exif->xmpIter;
614 return (ExifItem *)item;
620 catch (Exiv2::AnyError& e) {
626 ExifItem *exif_get_next_item(ExifData *exif)
629 if (exif->exifIter != exif->exifData().end())
631 const Exiv2::Metadatum *item = &*exif->exifIter;
633 return (ExifItem *)item;
635 if (exif->iptcIter != exif->iptcData().end())
637 const Exiv2::Metadatum *item = &*exif->iptcIter;
639 return (ExifItem *)item;
641 #if EXIV2_TEST_VERSION(0,16,0)
642 if (exif->xmpIter != exif->xmpData().end())
644 const Exiv2::Metadatum *item = &*exif->xmpIter;
646 return (ExifItem *)item;
651 catch (Exiv2::AnyError& e) {
657 char *exif_item_get_tag_name(ExifItem *item)
660 if (!item) return NULL;
661 return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
663 catch (Exiv2::AnyError& e) {
669 guint exif_item_get_tag_id(ExifItem *item)
673 return ((Exiv2::Metadatum *)item)->tag();
675 catch (Exiv2::AnyError& e) {
681 guint exif_item_get_elements(ExifItem *item)
685 return ((Exiv2::Metadatum *)item)->count();
687 catch (Exiv2::AnyError& e) {
693 char *exif_item_get_data(ExifItem *item, guint *data_len)
697 Exiv2::Metadatum *md = (Exiv2::Metadatum *)item;
698 if (data_len) *data_len = md->size();
699 char *data = (char *)g_malloc(md->size());
700 long res = md->copy((Exiv2::byte *)data, Exiv2::littleEndian /* should not matter */);
701 g_assert(res == md->size());
704 catch (Exiv2::AnyError& e) {
710 char *exif_item_get_description(ExifItem *item)
713 if (!item) return NULL;
714 return utf8_validate_or_convert(((Exiv2::Metadatum *)item)->tagLabel().c_str());
716 catch (std::exception& e) {
717 // debug_exception(e);
723 invalidTypeId, unsignedByte, asciiString, unsignedShort,
724 unsignedLong, unsignedRational, signedByte, undefined,
725 signedShort, signedLong, signedRational, string,
726 date, time, comment, directory,
727 xmpText, xmpAlt, xmpBag, xmpSeq,
731 static guint format_id_trans_tbl [] = {
733 EXIF_FORMAT_BYTE_UNSIGNED,
735 EXIF_FORMAT_SHORT_UNSIGNED,
736 EXIF_FORMAT_LONG_UNSIGNED,
737 EXIF_FORMAT_RATIONAL_UNSIGNED,
739 EXIF_FORMAT_UNDEFINED,
742 EXIF_FORMAT_RATIONAL,
746 EXIF_FORMAT_UNDEFINED,
755 guint exif_item_get_format_id(ExifItem *item)
758 if (!item) return EXIF_FORMAT_UNKNOWN;
759 guint id = ((Exiv2::Metadatum *)item)->typeId();
760 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
761 return format_id_trans_tbl[id];
763 catch (Exiv2::AnyError& e) {
765 return EXIF_FORMAT_UNKNOWN;
769 const char *exif_item_get_format_name(ExifItem *item, gboolean brief)
772 if (!item) return NULL;
773 return ((Exiv2::Metadatum *)item)->typeName();
775 catch (Exiv2::AnyError& e) {
782 gchar *exif_item_get_data_as_text(ExifItem *item)
785 if (!item) return NULL;
786 Exiv2::Metadatum *metadatum = (Exiv2::Metadatum *)item;
787 #if EXIV2_TEST_VERSION(0,17,0)
788 return utf8_validate_or_convert(metadatum->print().c_str());
790 std::stringstream str;
791 Exiv2::Exifdatum *exifdatum;
792 Exiv2::Iptcdatum *iptcdatum;
793 #if EXIV2_TEST_VERSION(0,16,0)
794 Exiv2::Xmpdatum *xmpdatum;
796 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(metadatum)))
798 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(metadatum)))
800 #if EXIV2_TEST_VERSION(0,16,0)
801 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(metadatum)))
805 return utf8_validate_or_convert(str.str().c_str());
808 catch (Exiv2::AnyError& e) {
813 gchar *exif_item_get_string(ExifItem *item, int idx)
816 if (!item) return NULL;
817 Exiv2::Metadatum *em = (Exiv2::Metadatum *)item;
818 #if EXIV2_TEST_VERSION(0,16,0)
819 std::string str = em->toString(idx);
821 std::string str = em->toString(); // FIXME
823 if (idx == 0 && str == "") str = em->toString();
824 if (str.length() > 5 && str.substr(0, 5) == "lang=")
826 std::string::size_type pos = str.find_first_of(' ');
827 if (pos != std::string::npos) str = str.substr(pos+1);
830 return utf8_validate_or_convert(str.c_str());
832 catch (Exiv2::AnyError& e) {
838 gint exif_item_get_integer(ExifItem *item, gint *value)
841 if (!item || exif_item_get_elements(item) == 0) return 0;
842 *value = ((Exiv2::Metadatum *)item)->toLong();
845 catch (Exiv2::AnyError& e) {
851 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
854 if (!item) return NULL;
855 if (n >= exif_item_get_elements(item)) return NULL;
856 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational(n);
857 static ExifRational ret;
860 if (sign) *sign = (((Exiv2::Metadatum *)item)->typeId() == Exiv2::signedRational);
863 catch (Exiv2::AnyError& e) {
869 gchar *exif_get_tag_description_by_key(const gchar *key)
872 Exiv2::ExifKey ekey(key);
873 return utf8_validate_or_convert(ekey.tagLabel().c_str());
875 catch (Exiv2::AnyError& e) {
877 Exiv2::IptcKey ikey(key);
878 return utf8_validate_or_convert(ikey.tagLabel().c_str());
880 catch (Exiv2::AnyError& e) {
882 #if EXIV2_TEST_VERSION(0,16,0)
883 Exiv2::XmpKey xkey(key);
884 return utf8_validate_or_convert(xkey.tagLabel().c_str());
887 catch (Exiv2::AnyError& e) {
896 static const AltKey *find_alt_key(const gchar *xmp_key)
900 while (alt_keys[i].xmp_key)
902 if (strcmp(xmp_key, alt_keys[i].xmp_key) == 0) return &alt_keys[i];
908 static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const GList *values)
911 const GList *work = values;
914 Exiv2::ExifKey ekey(key);
916 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
917 while (pos != exif->exifData().end())
919 exif->exifData().erase(pos);
920 pos = exif->exifData().findKey(ekey);
925 exif->exifData()[key] = (gchar *)work->data;
929 catch (Exiv2::AnyError& e) {
930 #if EXIV2_TEST_VERSION(0,16,0)
934 Exiv2::IptcKey ekey(key);
935 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
936 while (pos != exif->iptcData().end())
938 exif->iptcData().erase(pos);
939 pos = exif->iptcData().findKey(ekey);
944 exif->iptcData()[key] = (gchar *)work->data;
948 #if EXIV2_TEST_VERSION(0,16,0)
949 catch (Exiv2::AnyError& e) {
950 Exiv2::XmpKey ekey(key);
951 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
952 while (pos != exif->xmpData().end())
954 exif->xmpData().erase(pos);
955 pos = exif->xmpData().findKey(ekey);
960 exif->xmpData()[key] = (gchar *)work->data;
968 catch (Exiv2::AnyError& e) {
974 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
976 gint ret = exif_update_metadata_simple(exif, key, values);
979 #if !EXIV2_TEST_VERSION(0,17,0)
980 TRUE || /* no conversion support */
982 !values || /* deleting item */
983 !ret /* writing to the explicitely given xmp tag failed */
986 /* deleted xmp metadatum can't be converted, we have to delete also the corresponding legacy tag */
987 /* if we can't write xmp, update at least the legacy tag */
988 const AltKey *alt_key = find_alt_key(key);
989 if (alt_key && alt_key->iptc_key)
990 ret = exif_update_metadata_simple(exif, alt_key->iptc_key, values);
992 if (alt_key && alt_key->exif_key)
993 ret = exif_update_metadata_simple(exif, alt_key->exif_key, values);
999 static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, MetadataFormat format, const Exiv2::ExifData *metadata)
1001 #if EXIV2_TEST_VERSION(0,16,0)
1002 Exiv2::TypeId id = item.typeId();
1003 if (format == METADATA_FORMATTED ||
1004 id == Exiv2::asciiString ||
1005 id == Exiv2::undefined ||
1006 id == Exiv2::string ||
1007 id == Exiv2::date ||
1008 id == Exiv2::time ||
1009 id == Exiv2::xmpText ||
1010 id == Exiv2::langAlt ||
1011 id == Exiv2::comment
1015 /* read as a single entry */
1018 if (format == METADATA_FORMATTED)
1020 #if EXIV2_TEST_VERSION(0,17,0)
1022 #if EXIV2_TEST_VERSION(0,18,0)
1027 std::stringstream stream;
1028 Exiv2::Exifdatum *exifdatum;
1029 Exiv2::Iptcdatum *iptcdatum;
1030 #if EXIV2_TEST_VERSION(0,16,0)
1031 Exiv2::Xmpdatum *xmpdatum;
1033 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(&item)))
1034 stream << *exifdatum;
1035 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(&item)))
1036 stream << *iptcdatum;
1037 #if EXIV2_TEST_VERSION(0,16,0)
1038 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(&item)))
1039 stream << *xmpdatum;
1043 if (str.length() > 1024)
1045 /* truncate very long strings, they cause problems in gui */
1052 str = item.toString();
1054 if (str.length() > 5 && str.substr(0, 5) == "lang=")
1056 std::string::size_type pos = str.find_first_of(' ');
1057 if (pos != std::string::npos) str = str.substr(pos+1);
1059 list = g_list_append(list, utf8_validate_or_convert(str.c_str()));
1060 #if EXIV2_TEST_VERSION(0,16,0)
1064 /* read as a list */
1066 for (i = 0; i < item.count(); i++)
1067 list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str()));
1073 static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, MetadataFormat format)
1078 Exiv2::ExifKey ekey(key);
1079 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
1080 if (pos != exif->exifData().end())
1081 list = exif_add_value_to_glist(list, *pos, format, &exif->exifData());
1084 catch (Exiv2::AnyError& e) {
1086 Exiv2::IptcKey ekey(key);
1087 Exiv2::IptcData::iterator pos = exif->iptcData().begin();
1088 while (pos != exif->iptcData().end())
1090 if (pos->key() == key)
1091 list = exif_add_value_to_glist(list, *pos, format, NULL);
1096 catch (Exiv2::AnyError& e) {
1097 #if EXIV2_TEST_VERSION(0,16,0)
1098 Exiv2::XmpKey ekey(key);
1099 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
1100 if (pos != exif->xmpData().end())
1101 list = exif_add_value_to_glist(list, *pos, format, NULL);
1106 catch (Exiv2::AnyError& e) {
1112 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1116 if (!key) return NULL;
1118 if (format == METADATA_FORMATTED)
1122 text = exif_get_formatted_by_key(exif, key, &key_valid);
1123 if (key_valid) return g_list_append(NULL, text);
1126 list = exif_get_metadata_simple(exif, key, format);
1128 /* the following code can be ifdefed out as soon as Exiv2 supports it */
1131 const AltKey *alt_key = find_alt_key(key);
1132 if (alt_key && alt_key->iptc_key)
1133 list = exif_get_metadata_simple(exif, alt_key->iptc_key, format);
1135 #if !EXIV2_TEST_VERSION(0,17,0)
1136 /* with older Exiv2 versions exif is not synced */
1137 if (!list && alt_key && alt_key->exif_key)
1138 list = exif_get_metadata_simple(exif, alt_key->exif_key, format);
1145 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
1147 exif->add_jpeg_color_profile(cp_data, cp_length);
1150 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1152 guchar *ret = exif->get_jpeg_color_profile(data_len);
1153 if (ret) return ret;
1155 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1156 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1157 ret = (guchar *)exif_item_get_data(prof_item, data_len);
1161 gchar* exif_get_image_comment(FileData* fd)
1163 if (!fd || !fd->exif)
1164 return g_strdup("");
1166 return g_strdup(fd->exif->image_comment().c_str());
1169 void exif_set_image_comment(FileData* fd, const gchar* comment)
1171 if (!fd || !fd->exif)
1174 fd->exif->set_image_comment(comment ? comment : "");
1178 #if EXIV2_TEST_VERSION(0,17,90)
1180 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1182 if (!exif) return NULL;
1184 if (!exif->image()) return NULL;
1186 std::string const path = exif->image()->io().path();
1187 /* given image pathname, first do simple (and fast) file extension test */
1188 gboolean is_raw = filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE);
1190 if (!is_raw && requested_width == 0) return NULL;
1194 Exiv2::PreviewManager pm(*exif->image());
1196 Exiv2::PreviewPropertiesList list = pm.getPreviewProperties();
1200 Exiv2::PreviewPropertiesList::iterator pos;
1201 Exiv2::PreviewPropertiesList::iterator last = --list.end();
1203 if (requested_width == 0)
1205 pos = last; // the largest
1212 if (pos->width_ >= (uint32_t)requested_width &&
1213 pos->height_ >= (uint32_t)requested_height) break;
1217 // we are not interested in smaller thumbnails in normal image formats - we can use full image instead
1220 if (pos->width_ < (uint32_t)requested_width || pos->height_ < (uint32_t)requested_height) return NULL;
1224 Exiv2::PreviewImage image = pm.getPreviewImage(*pos);
1226 Exiv2::DataBuf buf = image.copy();
1227 std::pair<Exiv2::byte*, long> p = buf.release();
1229 *data_len = p.second;
1234 catch (Exiv2::AnyError& e) {
1240 void exif_free_preview(guchar *buf)
1242 delete[] (Exiv2::byte*)buf;
1247 #if !EXIV2_TEST_VERSION(0,17,90)
1249 /* This is a dirty hack to support raw file preview, bassed on
1250 tiffparse.cpp from Exiv2 examples */
1255 RawFile(Exiv2::BasicIo &io);
1258 const Exiv2::Value *find(uint16_t tag, uint16_t group);
1260 unsigned long preview_offset();
1264 Exiv2::TiffComponent::AutoPtr rootDir;
1265 Exiv2::BasicIo &io_;
1266 const Exiv2::byte *map_data;
1268 unsigned long offset;
1271 typedef struct _UnmapData UnmapData;
1279 static GList *exif_unmap_list = 0;
1281 extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1283 unsigned long offset;
1285 if (!exif) return NULL;
1286 if (!exif->image()) return NULL;
1288 std::string const path = exif->image()->io().path();
1290 /* given image pathname, first do simple (and fast) file extension test */
1291 if (!filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE)) return NULL;
1300 RawFile rf(exif->image()->io());
1301 offset = rf.preview_offset();
1302 DEBUG_1("%s: offset %lu", path.c_str(), offset);
1304 fd = open(path.c_str(), O_RDONLY);
1310 if (fstat(fd, &st) == -1)
1315 map_len = st.st_size;
1316 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1318 if (map_data == MAP_FAILED)
1322 *data_len = map_len - offset;
1323 ud = g_new(UnmapData, 1);
1324 ud->ptr = map_data + offset;
1325 ud->map_data = map_data;
1326 ud->map_len = map_len;
1328 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1332 catch (Exiv2::AnyError& e) {
1339 void exif_free_preview(guchar *buf)
1341 GList *work = exif_unmap_list;
1345 UnmapData *ud = (UnmapData *)work->data;
1348 munmap(ud->map_data, ud->map_len);
1349 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1355 g_assert_not_reached();
1358 using namespace Exiv2;
1360 RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
1364 if (fstat(fd, &st) == -1)
1368 map_len = st.st_size;
1369 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1370 if (map_data == MAP_FAILED)
1375 if (io.open() != 0) {
1376 throw Error(9, io.path(), strError());
1379 map_data = io.mmap();
1380 map_len = io.size();
1383 type = Exiv2::ImageFactory::getType(map_data, map_len);
1385 #if EXIV2_TEST_VERSION(0,16,0)
1386 TiffHeaderBase *tiffHeader = NULL;
1388 TiffHeade2 *tiffHeader = NULL;
1390 Cr2Header *cr2Header = NULL;
1393 case Exiv2::ImageType::tiff:
1394 tiffHeader = new TiffHeade2();
1396 case Exiv2::ImageType::cr2:
1397 cr2Header = new Cr2Header();
1399 #if EXIV2_TEST_VERSION(0,16,0)
1400 case Exiv2::ImageType::orf:
1401 tiffHeader = new OrfHeader();
1404 #if EXIV2_TEST_VERSION(0,13,0)
1405 case Exiv2::ImageType::raf:
1406 if (map_len < 84 + 4) throw Error(14);
1407 offset = getULong(map_data + 84, bigEndian);
1410 case Exiv2::ImageType::crw:
1412 // Parse the image, starting with a CIFF header component
1413 Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
1414 parseTree->read(map_data, map_len);
1415 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
1416 if (entry) offset = entry->pData() - map_data;
1421 throw Error(3, "RAW");
1424 // process tiff-like formats
1426 TiffCompFactoryFct createFct = TiffCreator::create;
1428 rootDir = createFct(Tag::root, Group::none);
1429 if (0 == rootDir.get()) {
1430 throw Error(1, "No root element defined in TIFF structure");
1435 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
1436 #if EXIV2_TEST_VERSION(0,16,0)
1437 rootDir->setStart(map_data + tiffHeader->offset());
1439 rootDir->setStart(map_data + tiffHeader->ifdOffset());
1445 rootDir->setStart(map_data + cr2Header->offset());
1448 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
1450 TiffReader reader(map_data,
1455 rootDir->accept(reader);
1463 RawFile::~RawFile(void)
1469 const Value * RawFile::find(uint16_t tag, uint16_t group)
1471 TiffFinder finder(tag, group);
1472 rootDir->accept(finder);
1473 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
1476 DEBUG_1("(tag: %04x %04x) ", tag, group);
1477 return te->pValue();
1483 unsigned long RawFile::preview_offset(void)
1486 if (offset) return offset;
1488 if (type == Exiv2::ImageType::cr2)
1490 val = find(0x111, Group::ifd0);
1491 if (val) return val->toLong();
1496 val = find(0x201, Group::sub0_0);
1497 if (val) return val->toLong();
1499 val = find(0x201, Group::ifd0);
1500 if (val) return val->toLong();
1502 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
1503 if (val) return val->toLong();
1505 val = find(0x111, Group::sub0_1); // dng
1506 if (val) return val->toLong();
1517 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */