3 * Copyright (C) 2008 - 2009 The Geeqie Team
5 * Author: Vladimir Nadvornik
7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
16 #include <exiv2/image.hpp>
17 #include <exiv2/exif.hpp>
20 // EXIV2_TEST_VERSION is defined in Exiv2 0.15 and newer.
21 #ifndef EXIV2_TEST_VERSION
22 # define EXIV2_TEST_VERSION(major,minor,patch) \
23 ( EXIV2_VERSION >= EXIV2_MAKE_VERSION(major,minor,patch) )
27 #include <sys/types.h>
33 #if !EXIV2_TEST_VERSION(0,17,90)
34 #include <exiv2/tiffparser.hpp>
35 #include <exiv2/tiffcomposite.hpp>
36 #include <exiv2/tiffvisitor.hpp>
37 #include <exiv2/tiffimage.hpp>
38 #include <exiv2/cr2image.hpp>
39 #include <exiv2/crwimage.hpp>
40 #if EXIV2_TEST_VERSION(0,16,0)
41 #include <exiv2/orfimage.hpp>
43 #if EXIV2_TEST_VERSION(0,13,0)
44 #include <exiv2/rafimage.hpp>
46 #include <exiv2/futils.hpp>
48 #include <exiv2/preview.hpp>
51 #if EXIV2_TEST_VERSION(0,17,0)
52 #include <exiv2/convert.hpp>
53 #include <exiv2/xmpsidecar.hpp>
62 #include "filefilter.h"
63 #include "ui_fileops.h"
68 typedef struct _AltKey AltKey;
73 const gchar *exif_key;
74 const gchar *iptc_key;
77 /* this is a list of keys that should be converted, even with the older Exiv2 which does not support it directly */
78 static const AltKey alt_keys[] = {
79 {"Xmp.tiff.Orientation", "Exif.Image.Orientation", NULL},
80 {"Xmp.dc.title", NULL, "Iptc.Application2.ObjectName" },
81 {"Xmp.photoshop.Urgency", NULL, "Iptc.Application2.Urgency" },
82 {"Xmp.photoshop.Category", NULL, "Iptc.Application2.Category" },
83 {"Xmp.photoshop.SupplementalCategory", NULL, "Iptc.Application2.SuppCategory" },
84 {"Xmp.dc.subject", NULL, "Iptc.Application2.Keywords" },
85 {"Xmp.iptc.Location", NULL, "Iptc.Application2.LocationName" },
86 {"Xmp.photoshop.Instruction", NULL, "Iptc.Application2.SpecialInstructions" },
87 {"Xmp.photoshop.DateCreated", NULL, "Iptc.Application2.DateCreated" },
88 {"Xmp.dc.creator", NULL, "Iptc.Application2.Byline" },
89 {"Xmp.photoshop.AuthorsPosition", NULL, "Iptc.Application2.BylineTitle" },
90 {"Xmp.photoshop.City", NULL, "Iptc.Application2.City" },
91 {"Xmp.photoshop.State", NULL, "Iptc.Application2.ProvinceState" },
92 {"Xmp.iptc.CountryCode", NULL, "Iptc.Application2.CountryCode" },
93 {"Xmp.photoshop.Country", NULL, "Iptc.Application2.CountryName" },
94 {"Xmp.photoshop.TransmissionReference", NULL, "Iptc.Application2.TransmissionReference"},
95 {"Xmp.photoshop.Headline", NULL, "Iptc.Application2.Headline" },
96 {"Xmp.photoshop.Credit", NULL, "Iptc.Application2.Credit" },
97 {"Xmp.photoshop.Source", NULL, "Iptc.Application2.Source" },
98 {"Xmp.dc.rights", NULL, "Iptc.Application2.Copyright" },
99 {"Xmp.dc.description", NULL, "Iptc.Application2.Caption" },
100 {"Xmp.photoshop.CaptionWriter", NULL, "Iptc.Application2.Writer" },
104 static void debug_exception(Exiv2::AnyError& e)
106 gchar *str = g_locale_from_utf8(e.what(), -1, NULL, NULL, NULL);
107 DEBUG_1("Exiv2: %s", str);
113 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
114 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
115 #if EXIV2_TEST_VERSION(0,16,0)
116 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
123 virtual void writeMetadata(gchar *path = NULL)
125 g_critical("Unsupported method of writing metadata");
128 virtual ExifData *original()
133 virtual Exiv2::Image *image() = 0;
135 virtual Exiv2::ExifData &exifData() = 0;
137 virtual Exiv2::IptcData &iptcData() = 0;
139 #if EXIV2_TEST_VERSION(0,16,0)
140 virtual Exiv2::XmpData &xmpData() = 0;
143 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) = 0;
145 virtual guchar *get_jpeg_color_profile(guint *data_len) = 0;
148 // This allows read-only access to the original metadata
149 struct _ExifDataOriginal : public _ExifData
152 Exiv2::Image::AutoPtr image_;
154 /* the icc profile in jpeg is not technically exif - store it here */
155 unsigned char *cp_data_;
159 Exiv2::ExifData emptyExifData_;
160 Exiv2::IptcData emptyIptcData_;
161 #if EXIV2_TEST_VERSION(0,16,0)
162 Exiv2::XmpData emptyXmpData_;
166 _ExifDataOriginal(Exiv2::Image::AutoPtr image)
174 _ExifDataOriginal(gchar *path)
180 gchar *pathl = path_from_utf8(path);
183 image_ = Exiv2::ImageFactory::open(pathl);
184 // g_assert (image.get() != 0);
185 image_->readMetadata();
187 #if EXIV2_TEST_VERSION(0,16,0)
188 if (image_->mimeType() == "application/rdf+xml")
190 //Exiv2 sidecar converts xmp to exif and iptc, we don't want it.
191 image_->clearExifData();
192 image_->clearIptcData();
196 #if EXIV2_TEST_VERSION(0,14,0)
197 if (image_->mimeType() == "image/jpeg")
199 /* try to get jpeg color profile */
200 Exiv2::BasicIo &io = image_->io();
201 gint open = io.isopen();
202 if (!open) io.open();
205 unsigned char *mapped = (unsigned char*)io.mmap();
206 if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
209 if (!open) io.close();
213 catch (Exiv2::AnyError& e)
220 virtual ~_ExifDataOriginal()
222 if (cp_data_) g_free(cp_data_);
225 virtual Exiv2::Image *image()
227 if (!valid_) return NULL;
231 virtual Exiv2::ExifData &exifData ()
233 if (!valid_) return emptyExifData_;
234 return image_->exifData();
237 virtual Exiv2::IptcData &iptcData ()
239 if (!valid_) return emptyIptcData_;
240 return image_->iptcData();
243 #if EXIV2_TEST_VERSION(0,16,0)
244 virtual Exiv2::XmpData &xmpData ()
246 if (!valid_) return emptyXmpData_;
247 return image_->xmpData();
251 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
253 if (cp_data_) g_free(cp_data_);
255 cp_length_ = cp_length;
258 virtual guchar *get_jpeg_color_profile(guint *data_len)
262 if (data_len) *data_len = cp_length_;
263 return (unsigned char *) g_memdup(cp_data_, cp_length_);
270 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data);
273 // This allows read-write access to the metadata
274 struct _ExifDataProcessed : public _ExifData
277 _ExifDataOriginal *imageData_;
278 _ExifDataOriginal *sidecarData_;
280 Exiv2::ExifData exifData_;
281 Exiv2::IptcData iptcData_;
282 #if EXIV2_TEST_VERSION(0,16,0)
283 Exiv2::XmpData xmpData_;
287 _ExifDataProcessed(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
289 imageData_ = new _ExifDataOriginal(path);
291 #if EXIV2_TEST_VERSION(0,16,0)
294 sidecarData_ = new _ExifDataOriginal(sidecar_path);
295 xmpData_ = sidecarData_->xmpData();
299 xmpData_ = imageData_->xmpData();
303 exifData_ = imageData_->exifData();
304 iptcData_ = imageData_->iptcData();
305 #if EXIV2_TEST_VERSION(0,17,0)
306 syncExifWithXmp(exifData_, xmpData_);
310 g_hash_table_foreach(modified_xmp, _ExifDataProcessed_update_xmp, this);
314 virtual ~_ExifDataProcessed()
316 if (imageData_) delete imageData_;
317 if (sidecarData_) delete sidecarData_;
320 virtual ExifData *original()
325 virtual void writeMetadata(gchar *path = NULL)
329 #if EXIV2_TEST_VERSION(0,17,0)
330 if (options->metadata.save_legacy_IPTC)
331 copyXmpToIptc(xmpData_, iptcData_);
335 copyXmpToExif(xmpData_, exifData_);
337 Exiv2::Image *image = imageData_->image();
339 if (!image) Exiv2::Error(21);
340 image->setExifData(exifData_);
341 image->setIptcData(iptcData_);
342 #if EXIV2_TEST_VERSION(0,16,0)
343 image->setXmpData(xmpData_);
345 image->writeMetadata();
349 #if EXIV2_TEST_VERSION(0,17,0)
350 gchar *pathl = path_from_utf8(path);;
352 Exiv2::Image::AutoPtr sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
356 sidecar->setXmpData(xmpData_);
357 sidecar->writeMetadata();
359 throw Exiv2::Error(3, "xmp");
364 virtual Exiv2::Image *image()
366 return imageData_->image();
369 virtual Exiv2::ExifData &exifData ()
374 virtual Exiv2::IptcData &iptcData ()
379 #if EXIV2_TEST_VERSION(0,16,0)
380 virtual Exiv2::XmpData &xmpData ()
386 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
388 imageData_->add_jpeg_color_profile(cp_data, cp_length);
391 virtual guchar *get_jpeg_color_profile(guint *data_len)
393 return imageData_->get_jpeg_color_profile(data_len);
405 #ifdef EXV_ENABLE_NLS
406 bind_textdomain_codeset (EXV_PACKAGE, "UTF-8");
412 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data)
414 exif_update_metadata((ExifData *)data, (gchar *)key, (GList *)value);
417 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
419 DEBUG_1("exif read %s, sidecar: %s", path, sidecar_path ? sidecar_path : "-");
421 return new _ExifDataProcessed(path, sidecar_path, modified_xmp);
423 catch (Exiv2::AnyError& e) {
430 gboolean exif_write(ExifData *exif)
433 exif->writeMetadata();
436 catch (Exiv2::AnyError& e) {
442 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
445 exif->writeMetadata(path);
448 catch (Exiv2::AnyError& e) {
456 void exif_free(ExifData *exif)
459 g_assert(dynamic_cast<_ExifDataProcessed *>(exif)); // this should not be called on ExifDataOriginal
463 ExifData *exif_get_original(ExifData *exif)
465 return exif->original();
469 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
472 Exiv2::Metadatum *item = NULL;
474 Exiv2::ExifKey ekey(key);
475 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
476 if (pos == exif->exifData().end()) return NULL;
479 catch (Exiv2::AnyError& e) {
481 Exiv2::IptcKey ekey(key);
482 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
483 if (pos == exif->iptcData().end()) return NULL;
486 catch (Exiv2::AnyError& e) {
487 #if EXIV2_TEST_VERSION(0,16,0)
488 Exiv2::XmpKey ekey(key);
489 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
490 if (pos == exif->xmpData().end()) return NULL;
495 return (ExifItem *)item;
497 catch (Exiv2::AnyError& e) {
503 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
506 Exiv2::Metadatum *item = NULL;
508 Exiv2::ExifKey ekey(key);
509 exif->exifData().add(ekey, NULL);
510 Exiv2::ExifData::iterator pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
514 catch (Exiv2::AnyError& e) {
516 Exiv2::IptcKey ekey(key);
517 exif->iptcData().add(ekey, NULL);
518 Exiv2::IptcData::iterator pos = exif->iptcData().end();
522 catch (Exiv2::AnyError& e) {
523 #if EXIV2_TEST_VERSION(0,16,0)
524 Exiv2::XmpKey ekey(key);
525 exif->xmpData().add(ekey, NULL);
526 Exiv2::XmpData::iterator pos = exif->xmpData().end();
532 return (ExifItem *)item;
534 catch (Exiv2::AnyError& e) {
541 ExifItem *exif_get_first_item(ExifData *exif)
544 exif->exifIter = exif->exifData().begin();
545 exif->iptcIter = exif->iptcData().begin();
546 #if EXIV2_TEST_VERSION(0,16,0)
547 exif->xmpIter = exif->xmpData().begin();
549 if (exif->exifIter != exif->exifData().end())
551 const Exiv2::Metadatum *item = &*exif->exifIter;
553 return (ExifItem *)item;
555 if (exif->iptcIter != exif->iptcData().end())
557 const Exiv2::Metadatum *item = &*exif->iptcIter;
559 return (ExifItem *)item;
561 #if EXIV2_TEST_VERSION(0,16,0)
562 if (exif->xmpIter != exif->xmpData().end())
564 const Exiv2::Metadatum *item = &*exif->xmpIter;
566 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 EXIV2_TEST_VERSION(0,16,0)
594 if (exif->xmpIter != exif->xmpData().end())
596 const Exiv2::Metadatum *item = &*exif->xmpIter;
598 return (ExifItem *)item;
603 catch (Exiv2::AnyError& e) {
609 char *exif_item_get_tag_name(ExifItem *item)
612 if (!item) return NULL;
613 return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
615 catch (Exiv2::AnyError& e) {
621 guint exif_item_get_tag_id(ExifItem *item)
625 return ((Exiv2::Metadatum *)item)->tag();
627 catch (Exiv2::AnyError& e) {
633 guint exif_item_get_elements(ExifItem *item)
637 return ((Exiv2::Metadatum *)item)->count();
639 catch (Exiv2::AnyError& e) {
645 char *exif_item_get_data(ExifItem *item, guint *data_len)
649 Exiv2::Metadatum *md = (Exiv2::Metadatum *)item;
650 if (data_len) *data_len = md->size();
651 char *data = (char *)g_malloc(md->size());
652 long res = md->copy((Exiv2::byte *)data, Exiv2::littleEndian /* should not matter */);
653 g_assert(res == md->size());
656 catch (Exiv2::AnyError& e) {
662 char *exif_item_get_description(ExifItem *item)
665 if (!item) return NULL;
666 return utf8_validate_or_convert(((Exiv2::Metadatum *)item)->tagLabel().c_str());
668 catch (std::exception& e) {
669 // debug_exception(e);
675 invalidTypeId, unsignedByte, asciiString, unsignedShort,
676 unsignedLong, unsignedRational, signedByte, undefined,
677 signedShort, signedLong, signedRational, string,
678 date, time, comment, directory,
679 xmpText, xmpAlt, xmpBag, xmpSeq,
683 static guint format_id_trans_tbl [] = {
685 EXIF_FORMAT_BYTE_UNSIGNED,
687 EXIF_FORMAT_SHORT_UNSIGNED,
688 EXIF_FORMAT_LONG_UNSIGNED,
689 EXIF_FORMAT_RATIONAL_UNSIGNED,
691 EXIF_FORMAT_UNDEFINED,
694 EXIF_FORMAT_RATIONAL,
698 EXIF_FORMAT_UNDEFINED,
707 guint exif_item_get_format_id(ExifItem *item)
710 if (!item) return EXIF_FORMAT_UNKNOWN;
711 guint id = ((Exiv2::Metadatum *)item)->typeId();
712 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
713 return format_id_trans_tbl[id];
715 catch (Exiv2::AnyError& e) {
717 return EXIF_FORMAT_UNKNOWN;
721 const char *exif_item_get_format_name(ExifItem *item, gboolean brief)
724 if (!item) return NULL;
725 return ((Exiv2::Metadatum *)item)->typeName();
727 catch (Exiv2::AnyError& e) {
734 gchar *exif_item_get_data_as_text(ExifItem *item)
737 if (!item) return NULL;
738 Exiv2::Metadatum *metadatum = (Exiv2::Metadatum *)item;
739 #if EXIV2_TEST_VERSION(0,17,0)
740 return utf8_validate_or_convert(metadatum->print().c_str());
742 std::stringstream str;
743 Exiv2::Exifdatum *exifdatum;
744 Exiv2::Iptcdatum *iptcdatum;
745 #if EXIV2_TEST_VERSION(0,16,0)
746 Exiv2::Xmpdatum *xmpdatum;
748 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(metadatum)))
750 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(metadatum)))
752 #if EXIV2_TEST_VERSION(0,16,0)
753 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(metadatum)))
757 return utf8_validate_or_convert(str.str().c_str());
760 catch (Exiv2::AnyError& e) {
765 gchar *exif_item_get_string(ExifItem *item, int idx)
768 if (!item) return NULL;
769 Exiv2::Metadatum *em = (Exiv2::Metadatum *)item;
770 #if EXIV2_TEST_VERSION(0,16,0)
771 std::string str = em->toString(idx);
773 std::string str = em->toString(); // FIXME
775 if (idx == 0 && str == "") str = em->toString();
776 if (str.length() > 5 && str.substr(0, 5) == "lang=")
778 std::string::size_type pos = str.find_first_of(' ');
779 if (pos != std::string::npos) str = str.substr(pos+1);
782 return utf8_validate_or_convert(str.c_str());
784 catch (Exiv2::AnyError& e) {
790 gint exif_item_get_integer(ExifItem *item, gint *value)
794 *value = ((Exiv2::Metadatum *)item)->toLong();
797 catch (Exiv2::AnyError& e) {
803 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
806 if (!item) return NULL;
807 if (n >= exif_item_get_elements(item)) return NULL;
808 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational(n);
809 static ExifRational ret;
812 if (sign) *sign = (((Exiv2::Metadatum *)item)->typeId() == Exiv2::signedRational);
815 catch (Exiv2::AnyError& e) {
821 gchar *exif_get_tag_description_by_key(const gchar *key)
824 Exiv2::ExifKey ekey(key);
825 return utf8_validate_or_convert(ekey.tagLabel().c_str());
827 catch (Exiv2::AnyError& e) {
829 Exiv2::IptcKey ikey(key);
830 return utf8_validate_or_convert(ikey.tagLabel().c_str());
832 catch (Exiv2::AnyError& e) {
834 #if EXIV2_TEST_VERSION(0,16,0)
835 Exiv2::XmpKey xkey(key);
836 return utf8_validate_or_convert(xkey.tagLabel().c_str());
839 catch (Exiv2::AnyError& e) {
848 static const AltKey *find_alt_key(const gchar *xmp_key)
852 while (alt_keys[i].xmp_key)
854 if (strcmp(xmp_key, alt_keys[i].xmp_key) == 0) return &alt_keys[i];
860 static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const GList *values)
863 const GList *work = values;
866 Exiv2::ExifKey ekey(key);
868 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
869 while (pos != exif->exifData().end())
871 exif->exifData().erase(pos);
872 pos = exif->exifData().findKey(ekey);
877 exif->exifData()[key] = (gchar *)work->data;
881 catch (Exiv2::AnyError& e) {
882 #if EXIV2_TEST_VERSION(0,16,0)
886 Exiv2::IptcKey ekey(key);
887 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
888 while (pos != exif->iptcData().end())
890 exif->iptcData().erase(pos);
891 pos = exif->iptcData().findKey(ekey);
896 exif->iptcData()[key] = (gchar *)work->data;
900 #if EXIV2_TEST_VERSION(0,16,0)
901 catch (Exiv2::AnyError& e) {
902 Exiv2::XmpKey ekey(key);
903 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
904 while (pos != exif->xmpData().end())
906 exif->xmpData().erase(pos);
907 pos = exif->xmpData().findKey(ekey);
912 exif->xmpData()[key] = (gchar *)work->data;
920 catch (Exiv2::AnyError& e) {
926 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
928 gint ret = exif_update_metadata_simple(exif, key, values);
931 #if !EXIV2_TEST_VERSION(0,17,0)
932 TRUE || /* no conversion support */
934 !values || /* deleting item */
935 !ret /* writing to the explicitely given xmp tag failed */
938 /* deleted xmp metadatum can't be converted, we have to delete also the corresponding legacy tag */
939 /* if we can't write xmp, update at least the legacy tag */
940 const AltKey *alt_key = find_alt_key(key);
941 if (alt_key && alt_key->iptc_key)
942 ret = exif_update_metadata_simple(exif, alt_key->iptc_key, values);
944 if (alt_key && alt_key->exif_key)
945 ret = exif_update_metadata_simple(exif, alt_key->exif_key, values);
951 static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, MetadataFormat format, const Exiv2::ExifData *metadata)
953 #if EXIV2_TEST_VERSION(0,16,0)
954 Exiv2::TypeId id = item.typeId();
955 if (format == METADATA_FORMATTED ||
956 id == Exiv2::asciiString ||
957 id == Exiv2::undefined ||
958 id == Exiv2::string ||
961 id == Exiv2::xmpText ||
962 id == Exiv2::langAlt ||
967 /* read as a single entry */
970 if (format == METADATA_FORMATTED)
972 #if EXIV2_TEST_VERSION(0,17,0)
974 #if EXIV2_TEST_VERSION(0,18,0)
979 std::stringstream stream;
980 Exiv2::Exifdatum *exifdatum;
981 Exiv2::Iptcdatum *iptcdatum;
982 #if EXIV2_TEST_VERSION(0,16,0)
983 Exiv2::Xmpdatum *xmpdatum;
985 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(metadatum)))
986 stream << *exifdatum;
987 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(metadatum)))
988 stream << *iptcdatum;
989 #if EXIV2_TEST_VERSION(0,16,0)
990 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(metadatum)))
995 if (str.length() > 1024)
997 /* truncate very long strings, they cause problems in gui */
1004 str = item.toString();
1005 if (str.length() > 5 && str.substr(0, 5) == "lang=")
1007 std::string::size_type pos = str.find_first_of(' ');
1008 if (pos != std::string::npos) str = str.substr(pos+1);
1011 list = g_list_append(list, utf8_validate_or_convert(str.c_str()));
1012 #if EXIV2_TEST_VERSION(0,16,0)
1016 /* read as a list */
1018 for (i = 0; i < item.count(); i++)
1019 list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str()));
1025 static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, MetadataFormat format)
1030 Exiv2::ExifKey ekey(key);
1031 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
1032 if (pos != exif->exifData().end())
1033 list = exif_add_value_to_glist(list, *pos, format, &exif->exifData());
1036 catch (Exiv2::AnyError& e) {
1038 Exiv2::IptcKey ekey(key);
1039 Exiv2::IptcData::iterator pos = exif->iptcData().begin();
1040 while (pos != exif->iptcData().end())
1042 if (pos->key() == key)
1043 list = exif_add_value_to_glist(list, *pos, format, NULL);
1048 catch (Exiv2::AnyError& e) {
1049 #if EXIV2_TEST_VERSION(0,16,0)
1050 Exiv2::XmpKey ekey(key);
1051 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
1052 if (pos != exif->xmpData().end())
1053 list = exif_add_value_to_glist(list, *pos, format, NULL);
1058 catch (Exiv2::AnyError& e) {
1064 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1068 if (!key) return NULL;
1070 if (format == METADATA_FORMATTED)
1074 text = exif_get_formatted_by_key(exif, key, &key_valid);
1075 if (key_valid) return g_list_append(NULL, text);
1078 list = exif_get_metadata_simple(exif, key, format);
1080 /* the following code can be ifdefed out as soon as Exiv2 supports it */
1083 const AltKey *alt_key = find_alt_key(key);
1084 if (alt_key && alt_key->iptc_key)
1085 list = exif_get_metadata_simple(exif, alt_key->iptc_key, format);
1087 #if !EXIV2_TEST_VERSION(0,17,0)
1088 /* with older Exiv2 versions exif is not synced */
1089 if (!list && alt_key && alt_key->exif_key)
1090 list = exif_get_metadata_simple(exif, alt_key->exif_key, format);
1097 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
1099 exif->add_jpeg_color_profile(cp_data, cp_length);
1102 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1104 guchar *ret = exif->get_jpeg_color_profile(data_len);
1105 if (ret) return ret;
1107 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1108 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1109 ret = (guchar *)exif_item_get_data(prof_item, data_len);
1113 #if EXIV2_TEST_VERSION(0,17,90)
1115 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1117 if (!exif) return NULL;
1119 if (!exif->image()) return NULL;
1121 const char* path = exif->image()->io().path().c_str();
1122 /* given image pathname, first do simple (and fast) file extension test */
1123 gboolean is_raw = filter_file_class(path, FORMAT_CLASS_RAWIMAGE);
1125 if (!is_raw && requested_width == 0) return NULL;
1129 Exiv2::PreviewManager pm(*exif->image());
1131 Exiv2::PreviewPropertiesList list = pm.getPreviewProperties();
1135 Exiv2::PreviewPropertiesList::iterator pos;
1136 Exiv2::PreviewPropertiesList::iterator last = --list.end();
1138 if (requested_width == 0)
1140 pos = last; // the largest
1147 if (pos->width_ >= (uint32_t)requested_width &&
1148 pos->height_ >= (uint32_t)requested_height) break;
1152 // we are not interested in smaller thumbnails in normal image formats - we can use full image instead
1155 if (pos->width_ < (uint32_t)requested_width || pos->height_ < (uint32_t)requested_height) return NULL;
1159 Exiv2::PreviewImage image = pm.getPreviewImage(*pos);
1161 Exiv2::DataBuf buf = image.copy();
1162 std::pair<Exiv2::byte*, long> p = buf.release();
1164 *data_len = p.second;
1169 catch (Exiv2::AnyError& e) {
1175 void exif_free_preview(guchar *buf)
1177 delete[] (Exiv2::byte*)buf;
1182 #if !EXIV2_TEST_VERSION(0,17,90)
1184 /* This is a dirty hack to support raw file preview, bassed on
1185 tiffparse.cpp from Exiv2 examples */
1190 RawFile(Exiv2::BasicIo &io);
1193 const Exiv2::Value *find(uint16_t tag, uint16_t group);
1195 unsigned long preview_offset();
1199 Exiv2::TiffComponent::AutoPtr rootDir;
1200 Exiv2::BasicIo &io_;
1201 const Exiv2::byte *map_data;
1203 unsigned long offset;
1206 typedef struct _UnmapData UnmapData;
1214 static GList *exif_unmap_list = 0;
1216 extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1218 unsigned long offset;
1220 if (!exif) return NULL;
1221 if (!exif->image()) return NULL;
1223 const char* path = exif->image()->io().path().c_str();
1225 /* given image pathname, first do simple (and fast) file extension test */
1226 if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return NULL;
1235 RawFile rf(exif->image()->io());
1236 offset = rf.preview_offset();
1237 DEBUG_1("%s: offset %lu", path, offset);
1239 fd = open(path, O_RDONLY);
1245 if (fstat(fd, &st) == -1)
1250 map_len = st.st_size;
1251 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1253 if (map_data == MAP_FAILED)
1257 *data_len = map_len - offset;
1258 ud = g_new(UnmapData, 1);
1259 ud->ptr = map_data + offset;
1260 ud->map_data = map_data;
1261 ud->map_len = map_len;
1263 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1267 catch (Exiv2::AnyError& e) {
1274 void exif_free_preview(guchar *buf)
1276 GList *work = exif_unmap_list;
1280 UnmapData *ud = (UnmapData *)work->data;
1283 munmap(ud->map_data, ud->map_len);
1284 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1290 g_assert_not_reached();
1293 using namespace Exiv2;
1295 RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
1299 if (fstat(fd, &st) == -1)
1303 map_len = st.st_size;
1304 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1305 if (map_data == MAP_FAILED)
1310 if (io.open() != 0) {
1311 throw Error(9, io.path(), strError());
1314 map_data = io.mmap();
1315 map_len = io.size();
1318 type = Exiv2::ImageFactory::getType(map_data, map_len);
1320 #if EXIV2_TEST_VERSION(0,16,0)
1321 TiffHeaderBase *tiffHeader = NULL;
1323 TiffHeade2 *tiffHeader = NULL;
1325 Cr2Header *cr2Header = NULL;
1328 case Exiv2::ImageType::tiff:
1329 tiffHeader = new TiffHeade2();
1331 case Exiv2::ImageType::cr2:
1332 cr2Header = new Cr2Header();
1334 #if EXIV2_TEST_VERSION(0,16,0)
1335 case Exiv2::ImageType::orf:
1336 tiffHeader = new OrfHeader();
1339 #if EXIV2_TEST_VERSION(0,13,0)
1340 case Exiv2::ImageType::raf:
1341 if (map_len < 84 + 4) throw Error(14);
1342 offset = getULong(map_data + 84, bigEndian);
1345 case Exiv2::ImageType::crw:
1347 // Parse the image, starting with a CIFF header component
1348 Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
1349 parseTree->read(map_data, map_len);
1350 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
1351 if (entry) offset = entry->pData() - map_data;
1356 throw Error(3, "RAW");
1359 // process tiff-like formats
1361 TiffCompFactoryFct createFct = TiffCreator::create;
1363 rootDir = createFct(Tag::root, Group::none);
1364 if (0 == rootDir.get()) {
1365 throw Error(1, "No root element defined in TIFF structure");
1370 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
1371 #if EXIV2_TEST_VERSION(0,16,0)
1372 rootDir->setStart(map_data + tiffHeader->offset());
1374 rootDir->setStart(map_data + tiffHeader->ifdOffset());
1380 rootDir->setStart(map_data + cr2Header->offset());
1383 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
1385 TiffReader reader(map_data,
1390 rootDir->accept(reader);
1398 RawFile::~RawFile(void)
1404 const Value * RawFile::find(uint16_t tag, uint16_t group)
1406 TiffFinder finder(tag, group);
1407 rootDir->accept(finder);
1408 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
1411 DEBUG_1("(tag: %04x %04x) ", tag, group);
1412 return te->pValue();
1418 unsigned long RawFile::preview_offset(void)
1421 if (offset) return offset;
1423 if (type == Exiv2::ImageType::cr2)
1425 val = find(0x111, Group::ifd0);
1426 if (val) return val->toLong();
1431 val = find(0x201, Group::sub0_0);
1432 if (val) return val->toLong();
1434 val = find(0x201, Group::ifd0);
1435 if (val) return val->toLong();
1437 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
1438 if (val) return val->toLong();
1440 val = find(0x111, Group::sub0_1); // dng
1441 if (val) return val->toLong();
1452 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */