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>
29 // EXIV2_TEST_VERSION is defined in Exiv2 0.15 and newer.
30 #ifndef EXIV2_TEST_VERSION
31 # define EXIV2_TEST_VERSION(major,minor,patch) \
32 ( EXIV2_VERSION >= EXIV2_MAKE_VERSION(major,minor,patch) )
36 #include <sys/types.h>
42 #if !EXIV2_TEST_VERSION(0,17,90)
43 #include <exiv2/tiffparser.hpp>
44 #include <exiv2/tiffcomposite.hpp>
45 #include <exiv2/tiffvisitor.hpp>
46 #include <exiv2/tiffimage.hpp>
47 #include <exiv2/cr2image.hpp>
48 #include <exiv2/crwimage.hpp>
49 #if EXIV2_TEST_VERSION(0,16,0)
50 #include <exiv2/orfimage.hpp>
52 #if EXIV2_TEST_VERSION(0,13,0)
53 #include <exiv2/rafimage.hpp>
55 #include <exiv2/futils.hpp>
57 #include <exiv2/preview.hpp>
60 #if EXIV2_TEST_VERSION(0,17,0)
61 #include <exiv2/convert.hpp>
62 #include <exiv2/xmpsidecar.hpp>
71 #include "filefilter.h"
72 #include "ui_fileops.h"
77 typedef struct _AltKey AltKey;
82 const gchar *exif_key;
83 const gchar *iptc_key;
86 /* this is a list of keys that should be converted, even with the older Exiv2 which does not support it directly */
87 static const AltKey alt_keys[] = {
88 {"Xmp.tiff.Orientation", "Exif.Image.Orientation", NULL},
89 {"Xmp.dc.title", NULL, "Iptc.Application2.ObjectName" },
90 {"Xmp.photoshop.Urgency", NULL, "Iptc.Application2.Urgency" },
91 {"Xmp.photoshop.Category", NULL, "Iptc.Application2.Category" },
92 {"Xmp.photoshop.SupplementalCategory", NULL, "Iptc.Application2.SuppCategory" },
93 {"Xmp.dc.subject", NULL, "Iptc.Application2.Keywords" },
94 {"Xmp.iptc.Location", NULL, "Iptc.Application2.LocationName" },
95 {"Xmp.photoshop.Instruction", NULL, "Iptc.Application2.SpecialInstructions" },
96 {"Xmp.photoshop.DateCreated", NULL, "Iptc.Application2.DateCreated" },
97 {"Xmp.dc.creator", NULL, "Iptc.Application2.Byline" },
98 {"Xmp.photoshop.AuthorsPosition", NULL, "Iptc.Application2.BylineTitle" },
99 {"Xmp.photoshop.City", NULL, "Iptc.Application2.City" },
100 {"Xmp.photoshop.State", NULL, "Iptc.Application2.ProvinceState" },
101 {"Xmp.iptc.CountryCode", NULL, "Iptc.Application2.CountryCode" },
102 {"Xmp.photoshop.Country", NULL, "Iptc.Application2.CountryName" },
103 {"Xmp.photoshop.TransmissionReference", NULL, "Iptc.Application2.TransmissionReference"},
104 {"Xmp.photoshop.Headline", NULL, "Iptc.Application2.Headline" },
105 {"Xmp.photoshop.Credit", NULL, "Iptc.Application2.Credit" },
106 {"Xmp.photoshop.Source", NULL, "Iptc.Application2.Source" },
107 {"Xmp.dc.rights", NULL, "Iptc.Application2.Copyright" },
108 {"Xmp.dc.description", NULL, "Iptc.Application2.Caption" },
109 {"Xmp.photoshop.CaptionWriter", NULL, "Iptc.Application2.Writer" },
113 static void _debug_exception(const char* file,
118 gchar *str = g_locale_from_utf8(e.what(), -1, NULL, NULL, NULL);
119 DEBUG_1("%s:%d:%s:Exiv2: %s", file, line, func, str);
123 #define debug_exception(e) _debug_exception(__FILE__, __LINE__, __func__, e)
127 Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
128 Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
129 #if EXIV2_TEST_VERSION(0,16,0)
130 Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
137 virtual void writeMetadata(gchar *path = NULL)
139 g_critical("Unsupported method of writing metadata");
142 virtual ExifData *original()
147 virtual Exiv2::Image *image() = 0;
149 virtual Exiv2::ExifData &exifData() = 0;
151 virtual Exiv2::IptcData &iptcData() = 0;
153 #if EXIV2_TEST_VERSION(0,16,0)
154 virtual Exiv2::XmpData &xmpData() = 0;
157 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) = 0;
159 virtual guchar *get_jpeg_color_profile(guint *data_len) = 0;
162 // This allows read-only access to the original metadata
163 struct _ExifDataOriginal : public _ExifData
166 Exiv2::Image::AutoPtr image_;
168 /* the icc profile in jpeg is not technically exif - store it here */
169 unsigned char *cp_data_;
174 Exiv2::ExifData emptyExifData_;
175 Exiv2::IptcData emptyIptcData_;
176 #if EXIV2_TEST_VERSION(0,16,0)
177 Exiv2::XmpData emptyXmpData_;
181 _ExifDataOriginal(Exiv2::Image::AutoPtr image)
189 _ExifDataOriginal(gchar *path)
195 pathl_ = path_from_utf8(path);
198 image_ = Exiv2::ImageFactory::open(pathl_);
199 // g_assert (image.get() != 0);
200 image_->readMetadata();
202 #if EXIV2_TEST_VERSION(0,16,0)
203 if (image_->mimeType() == "application/rdf+xml")
205 //Exiv2 sidecar converts xmp to exif and iptc, we don't want it.
206 image_->clearExifData();
207 image_->clearIptcData();
211 #if EXIV2_TEST_VERSION(0,14,0)
212 if (image_->mimeType() == "image/jpeg")
214 /* try to get jpeg color profile */
215 Exiv2::BasicIo &io = image_->io();
216 gint open = io.isopen();
217 if (!open) io.open();
220 unsigned char *mapped = (unsigned char*)io.mmap();
221 if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
224 if (!open) io.close();
228 catch (Exiv2::AnyError& e)
234 virtual ~_ExifDataOriginal()
236 if (cp_data_) g_free(cp_data_);
237 if (pathl_) g_free(pathl_);
240 virtual Exiv2::Image *image()
242 if (!valid_) return NULL;
246 virtual Exiv2::ExifData &exifData ()
248 if (!valid_) return emptyExifData_;
249 return image_->exifData();
252 virtual Exiv2::IptcData &iptcData ()
254 if (!valid_) return emptyIptcData_;
255 return image_->iptcData();
258 #if EXIV2_TEST_VERSION(0,16,0)
259 virtual Exiv2::XmpData &xmpData ()
261 if (!valid_) return emptyXmpData_;
262 return image_->xmpData();
266 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
268 if (cp_data_) g_free(cp_data_);
270 cp_length_ = cp_length;
273 virtual guchar *get_jpeg_color_profile(guint *data_len)
277 if (data_len) *data_len = cp_length_;
278 return (unsigned char *) g_memdup(cp_data_, cp_length_);
285 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data);
288 // This allows read-write access to the metadata
289 struct _ExifDataProcessed : public _ExifData
292 _ExifDataOriginal *imageData_;
293 _ExifDataOriginal *sidecarData_;
295 Exiv2::ExifData exifData_;
296 Exiv2::IptcData iptcData_;
297 #if EXIV2_TEST_VERSION(0,16,0)
298 Exiv2::XmpData xmpData_;
302 _ExifDataProcessed(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
304 imageData_ = new _ExifDataOriginal(path);
306 #if EXIV2_TEST_VERSION(0,16,0)
309 sidecarData_ = new _ExifDataOriginal(sidecar_path);
310 xmpData_ = sidecarData_->xmpData();
314 xmpData_ = imageData_->xmpData();
318 exifData_ = imageData_->exifData();
319 iptcData_ = imageData_->iptcData();
320 #if EXIV2_TEST_VERSION(0,17,0)
323 syncExifWithXmp(exifData_, xmpData_);
327 DEBUG_1("Exiv2: Catching bug\n");
332 g_hash_table_foreach(modified_xmp, _ExifDataProcessed_update_xmp, this);
336 virtual ~_ExifDataProcessed()
338 if (imageData_) delete imageData_;
339 if (sidecarData_) delete sidecarData_;
342 virtual ExifData *original()
347 virtual void writeMetadata(gchar *path = NULL)
351 #if EXIV2_TEST_VERSION(0,17,0)
352 if (options->metadata.save_legacy_IPTC)
353 copyXmpToIptc(xmpData_, iptcData_);
357 copyXmpToExif(xmpData_, exifData_);
359 Exiv2::Image *image = imageData_->image();
361 if (!image) Exiv2::Error(21);
362 image->setExifData(exifData_);
363 image->setIptcData(iptcData_);
364 #if EXIV2_TEST_VERSION(0,16,0)
365 image->setXmpData(xmpData_);
367 image->writeMetadata();
371 #if EXIV2_TEST_VERSION(0,17,0)
372 gchar *pathl = path_from_utf8(path);;
374 Exiv2::Image::AutoPtr sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
378 sidecar->setXmpData(xmpData_);
379 sidecar->writeMetadata();
381 throw Exiv2::Error(3, "xmp");
386 virtual Exiv2::Image *image()
388 return imageData_->image();
391 virtual Exiv2::ExifData &exifData ()
396 virtual Exiv2::IptcData &iptcData ()
401 #if EXIV2_TEST_VERSION(0,16,0)
402 virtual Exiv2::XmpData &xmpData ()
408 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
410 imageData_->add_jpeg_color_profile(cp_data, cp_length);
413 virtual guchar *get_jpeg_color_profile(guint *data_len)
415 return imageData_->get_jpeg_color_profile(data_len);
427 #ifdef EXV_ENABLE_NLS
428 bind_textdomain_codeset (EXV_PACKAGE, "UTF-8");
434 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data)
436 exif_update_metadata((ExifData *)data, (gchar *)key, (GList *)value);
439 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
441 DEBUG_1("exif read %s, sidecar: %s", path, sidecar_path ? sidecar_path : "-");
443 return new _ExifDataProcessed(path, sidecar_path, modified_xmp);
445 catch (Exiv2::AnyError& e) {
452 gboolean exif_write(ExifData *exif)
455 exif->writeMetadata();
458 catch (Exiv2::AnyError& e) {
464 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
467 exif->writeMetadata(path);
470 catch (Exiv2::AnyError& e) {
478 void exif_free(ExifData *exif)
481 g_assert(dynamic_cast<_ExifDataProcessed *>(exif)); // this should not be called on ExifDataOriginal
485 ExifData *exif_get_original(ExifData *exif)
487 return exif->original();
491 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
494 Exiv2::Metadatum *item = NULL;
496 Exiv2::ExifKey ekey(key);
497 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
498 if (pos == exif->exifData().end()) return NULL;
501 catch (Exiv2::AnyError& e) {
503 Exiv2::IptcKey ekey(key);
504 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
505 if (pos == exif->iptcData().end()) return NULL;
508 catch (Exiv2::AnyError& e) {
509 #if EXIV2_TEST_VERSION(0,16,0)
510 Exiv2::XmpKey ekey(key);
511 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
512 if (pos == exif->xmpData().end()) return NULL;
517 return (ExifItem *)item;
519 catch (Exiv2::AnyError& e) {
525 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
528 Exiv2::Metadatum *item = NULL;
530 Exiv2::ExifKey ekey(key);
531 exif->exifData().add(ekey, NULL);
532 Exiv2::ExifData::iterator pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
536 catch (Exiv2::AnyError& e) {
538 Exiv2::IptcKey ekey(key);
539 exif->iptcData().add(ekey, NULL);
540 Exiv2::IptcData::iterator pos = exif->iptcData().end();
544 catch (Exiv2::AnyError& e) {
545 #if EXIV2_TEST_VERSION(0,16,0)
546 Exiv2::XmpKey ekey(key);
547 exif->xmpData().add(ekey, NULL);
548 Exiv2::XmpData::iterator pos = exif->xmpData().end();
554 return (ExifItem *)item;
556 catch (Exiv2::AnyError& e) {
563 ExifItem *exif_get_first_item(ExifData *exif)
566 exif->exifIter = exif->exifData().begin();
567 exif->iptcIter = exif->iptcData().begin();
568 #if EXIV2_TEST_VERSION(0,16,0)
569 exif->xmpIter = exif->xmpData().begin();
571 if (exif->exifIter != exif->exifData().end())
573 const Exiv2::Metadatum *item = &*exif->exifIter;
575 return (ExifItem *)item;
577 if (exif->iptcIter != exif->iptcData().end())
579 const Exiv2::Metadatum *item = &*exif->iptcIter;
581 return (ExifItem *)item;
583 #if EXIV2_TEST_VERSION(0,16,0)
584 if (exif->xmpIter != exif->xmpData().end())
586 const Exiv2::Metadatum *item = &*exif->xmpIter;
588 return (ExifItem *)item;
594 catch (Exiv2::AnyError& e) {
600 ExifItem *exif_get_next_item(ExifData *exif)
603 if (exif->exifIter != exif->exifData().end())
605 const Exiv2::Metadatum *item = &*exif->exifIter;
607 return (ExifItem *)item;
609 if (exif->iptcIter != exif->iptcData().end())
611 const Exiv2::Metadatum *item = &*exif->iptcIter;
613 return (ExifItem *)item;
615 #if EXIV2_TEST_VERSION(0,16,0)
616 if (exif->xmpIter != exif->xmpData().end())
618 const Exiv2::Metadatum *item = &*exif->xmpIter;
620 return (ExifItem *)item;
625 catch (Exiv2::AnyError& e) {
631 char *exif_item_get_tag_name(ExifItem *item)
634 if (!item) return NULL;
635 return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
637 catch (Exiv2::AnyError& e) {
643 guint exif_item_get_tag_id(ExifItem *item)
647 return ((Exiv2::Metadatum *)item)->tag();
649 catch (Exiv2::AnyError& e) {
655 guint exif_item_get_elements(ExifItem *item)
659 return ((Exiv2::Metadatum *)item)->count();
661 catch (Exiv2::AnyError& e) {
667 char *exif_item_get_data(ExifItem *item, guint *data_len)
671 Exiv2::Metadatum *md = (Exiv2::Metadatum *)item;
672 if (data_len) *data_len = md->size();
673 char *data = (char *)g_malloc(md->size());
674 long res = md->copy((Exiv2::byte *)data, Exiv2::littleEndian /* should not matter */);
675 g_assert(res == md->size());
678 catch (Exiv2::AnyError& e) {
684 char *exif_item_get_description(ExifItem *item)
687 if (!item) return NULL;
688 return utf8_validate_or_convert(((Exiv2::Metadatum *)item)->tagLabel().c_str());
690 catch (std::exception& e) {
691 // debug_exception(e);
697 invalidTypeId, unsignedByte, asciiString, unsignedShort,
698 unsignedLong, unsignedRational, signedByte, undefined,
699 signedShort, signedLong, signedRational, string,
700 date, time, comment, directory,
701 xmpText, xmpAlt, xmpBag, xmpSeq,
705 static guint format_id_trans_tbl [] = {
707 EXIF_FORMAT_BYTE_UNSIGNED,
709 EXIF_FORMAT_SHORT_UNSIGNED,
710 EXIF_FORMAT_LONG_UNSIGNED,
711 EXIF_FORMAT_RATIONAL_UNSIGNED,
713 EXIF_FORMAT_UNDEFINED,
716 EXIF_FORMAT_RATIONAL,
720 EXIF_FORMAT_UNDEFINED,
729 guint exif_item_get_format_id(ExifItem *item)
732 if (!item) return EXIF_FORMAT_UNKNOWN;
733 guint id = ((Exiv2::Metadatum *)item)->typeId();
734 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
735 return format_id_trans_tbl[id];
737 catch (Exiv2::AnyError& e) {
739 return EXIF_FORMAT_UNKNOWN;
743 const char *exif_item_get_format_name(ExifItem *item, gboolean brief)
746 if (!item) return NULL;
747 return ((Exiv2::Metadatum *)item)->typeName();
749 catch (Exiv2::AnyError& e) {
756 gchar *exif_item_get_data_as_text(ExifItem *item)
759 if (!item) return NULL;
760 Exiv2::Metadatum *metadatum = (Exiv2::Metadatum *)item;
761 #if EXIV2_TEST_VERSION(0,17,0)
762 return utf8_validate_or_convert(metadatum->print().c_str());
764 std::stringstream str;
765 Exiv2::Exifdatum *exifdatum;
766 Exiv2::Iptcdatum *iptcdatum;
767 #if EXIV2_TEST_VERSION(0,16,0)
768 Exiv2::Xmpdatum *xmpdatum;
770 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(metadatum)))
772 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(metadatum)))
774 #if EXIV2_TEST_VERSION(0,16,0)
775 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(metadatum)))
779 return utf8_validate_or_convert(str.str().c_str());
782 catch (Exiv2::AnyError& e) {
787 gchar *exif_item_get_string(ExifItem *item, int idx)
790 if (!item) return NULL;
791 Exiv2::Metadatum *em = (Exiv2::Metadatum *)item;
792 #if EXIV2_TEST_VERSION(0,16,0)
793 std::string str = em->toString(idx);
795 std::string str = em->toString(); // FIXME
797 if (idx == 0 && str == "") str = em->toString();
798 if (str.length() > 5 && str.substr(0, 5) == "lang=")
800 std::string::size_type pos = str.find_first_of(' ');
801 if (pos != std::string::npos) str = str.substr(pos+1);
804 return utf8_validate_or_convert(str.c_str());
806 catch (Exiv2::AnyError& e) {
812 gint exif_item_get_integer(ExifItem *item, gint *value)
815 if (!item || exif_item_get_elements(item) == 0) return 0;
816 *value = ((Exiv2::Metadatum *)item)->toLong();
819 catch (Exiv2::AnyError& e) {
825 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
828 if (!item) return NULL;
829 if (n >= exif_item_get_elements(item)) return NULL;
830 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational(n);
831 static ExifRational ret;
834 if (sign) *sign = (((Exiv2::Metadatum *)item)->typeId() == Exiv2::signedRational);
837 catch (Exiv2::AnyError& e) {
843 gchar *exif_get_tag_description_by_key(const gchar *key)
846 Exiv2::ExifKey ekey(key);
847 return utf8_validate_or_convert(ekey.tagLabel().c_str());
849 catch (Exiv2::AnyError& e) {
851 Exiv2::IptcKey ikey(key);
852 return utf8_validate_or_convert(ikey.tagLabel().c_str());
854 catch (Exiv2::AnyError& e) {
856 #if EXIV2_TEST_VERSION(0,16,0)
857 Exiv2::XmpKey xkey(key);
858 return utf8_validate_or_convert(xkey.tagLabel().c_str());
861 catch (Exiv2::AnyError& e) {
870 static const AltKey *find_alt_key(const gchar *xmp_key)
874 while (alt_keys[i].xmp_key)
876 if (strcmp(xmp_key, alt_keys[i].xmp_key) == 0) return &alt_keys[i];
882 static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const GList *values)
885 const GList *work = values;
888 Exiv2::ExifKey ekey(key);
890 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
891 while (pos != exif->exifData().end())
893 exif->exifData().erase(pos);
894 pos = exif->exifData().findKey(ekey);
899 exif->exifData()[key] = (gchar *)work->data;
903 catch (Exiv2::AnyError& e) {
904 #if EXIV2_TEST_VERSION(0,16,0)
908 Exiv2::IptcKey ekey(key);
909 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
910 while (pos != exif->iptcData().end())
912 exif->iptcData().erase(pos);
913 pos = exif->iptcData().findKey(ekey);
918 exif->iptcData()[key] = (gchar *)work->data;
922 #if EXIV2_TEST_VERSION(0,16,0)
923 catch (Exiv2::AnyError& e) {
924 Exiv2::XmpKey ekey(key);
925 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
926 while (pos != exif->xmpData().end())
928 exif->xmpData().erase(pos);
929 pos = exif->xmpData().findKey(ekey);
934 exif->xmpData()[key] = (gchar *)work->data;
942 catch (Exiv2::AnyError& e) {
948 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
950 gint ret = exif_update_metadata_simple(exif, key, values);
953 #if !EXIV2_TEST_VERSION(0,17,0)
954 TRUE || /* no conversion support */
956 !values || /* deleting item */
957 !ret /* writing to the explicitely given xmp tag failed */
960 /* deleted xmp metadatum can't be converted, we have to delete also the corresponding legacy tag */
961 /* if we can't write xmp, update at least the legacy tag */
962 const AltKey *alt_key = find_alt_key(key);
963 if (alt_key && alt_key->iptc_key)
964 ret = exif_update_metadata_simple(exif, alt_key->iptc_key, values);
966 if (alt_key && alt_key->exif_key)
967 ret = exif_update_metadata_simple(exif, alt_key->exif_key, values);
973 static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, MetadataFormat format, const Exiv2::ExifData *metadata)
975 #if EXIV2_TEST_VERSION(0,16,0)
976 Exiv2::TypeId id = item.typeId();
977 if (format == METADATA_FORMATTED ||
978 id == Exiv2::asciiString ||
979 id == Exiv2::undefined ||
980 id == Exiv2::string ||
983 id == Exiv2::xmpText ||
984 id == Exiv2::langAlt ||
989 /* read as a single entry */
992 if (format == METADATA_FORMATTED)
994 #if EXIV2_TEST_VERSION(0,17,0)
996 #if EXIV2_TEST_VERSION(0,18,0)
1001 std::stringstream stream;
1002 Exiv2::Exifdatum *exifdatum;
1003 Exiv2::Iptcdatum *iptcdatum;
1004 #if EXIV2_TEST_VERSION(0,16,0)
1005 Exiv2::Xmpdatum *xmpdatum;
1007 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(&item)))
1008 stream << *exifdatum;
1009 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(&item)))
1010 stream << *iptcdatum;
1011 #if EXIV2_TEST_VERSION(0,16,0)
1012 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(&item)))
1013 stream << *xmpdatum;
1017 if (str.length() > 1024)
1019 /* truncate very long strings, they cause problems in gui */
1026 str = item.toString();
1028 if (str.length() > 5 && str.substr(0, 5) == "lang=")
1030 std::string::size_type pos = str.find_first_of(' ');
1031 if (pos != std::string::npos) str = str.substr(pos+1);
1033 list = g_list_append(list, utf8_validate_or_convert(str.c_str()));
1034 #if EXIV2_TEST_VERSION(0,16,0)
1038 /* read as a list */
1040 for (i = 0; i < item.count(); i++)
1041 list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str()));
1047 static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, MetadataFormat format)
1052 Exiv2::ExifKey ekey(key);
1053 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
1054 if (pos != exif->exifData().end())
1055 list = exif_add_value_to_glist(list, *pos, format, &exif->exifData());
1058 catch (Exiv2::AnyError& e) {
1060 Exiv2::IptcKey ekey(key);
1061 Exiv2::IptcData::iterator pos = exif->iptcData().begin();
1062 while (pos != exif->iptcData().end())
1064 if (pos->key() == key)
1065 list = exif_add_value_to_glist(list, *pos, format, NULL);
1070 catch (Exiv2::AnyError& e) {
1071 #if EXIV2_TEST_VERSION(0,16,0)
1072 Exiv2::XmpKey ekey(key);
1073 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
1074 if (pos != exif->xmpData().end())
1075 list = exif_add_value_to_glist(list, *pos, format, NULL);
1080 catch (Exiv2::AnyError& e) {
1086 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1090 if (!key) return NULL;
1092 if (format == METADATA_FORMATTED)
1096 text = exif_get_formatted_by_key(exif, key, &key_valid);
1097 if (key_valid) return g_list_append(NULL, text);
1100 list = exif_get_metadata_simple(exif, key, format);
1102 /* the following code can be ifdefed out as soon as Exiv2 supports it */
1105 const AltKey *alt_key = find_alt_key(key);
1106 if (alt_key && alt_key->iptc_key)
1107 list = exif_get_metadata_simple(exif, alt_key->iptc_key, format);
1109 #if !EXIV2_TEST_VERSION(0,17,0)
1110 /* with older Exiv2 versions exif is not synced */
1111 if (!list && alt_key && alt_key->exif_key)
1112 list = exif_get_metadata_simple(exif, alt_key->exif_key, format);
1119 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
1121 exif->add_jpeg_color_profile(cp_data, cp_length);
1124 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1126 guchar *ret = exif->get_jpeg_color_profile(data_len);
1127 if (ret) return ret;
1129 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1130 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1131 ret = (guchar *)exif_item_get_data(prof_item, data_len);
1135 #if EXIV2_TEST_VERSION(0,17,90)
1137 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1139 if (!exif) return NULL;
1141 if (!exif->image()) return NULL;
1143 /* given image pathname, first do simple (and fast) file extension test */
1144 gboolean is_raw = filter_file_class(exif->image()->io().path().c_str(), FORMAT_CLASS_RAWIMAGE);
1146 if (!is_raw && requested_width == 0) return NULL;
1150 Exiv2::PreviewManager pm(*exif->image());
1152 Exiv2::PreviewPropertiesList list = pm.getPreviewProperties();
1156 Exiv2::PreviewPropertiesList::iterator pos;
1157 Exiv2::PreviewPropertiesList::iterator last = --list.end();
1159 if (requested_width == 0)
1161 pos = last; // the largest
1168 if (pos->width_ >= (uint32_t)requested_width &&
1169 pos->height_ >= (uint32_t)requested_height) break;
1173 // we are not interested in smaller thumbnails in normal image formats - we can use full image instead
1176 if (pos->width_ < (uint32_t)requested_width || pos->height_ < (uint32_t)requested_height) return NULL;
1180 Exiv2::PreviewImage image = pm.getPreviewImage(*pos);
1182 Exiv2::DataBuf buf = image.copy();
1183 std::pair<Exiv2::byte*, long> p = buf.release();
1185 *data_len = p.second;
1190 catch (Exiv2::AnyError& e) {
1196 void exif_free_preview(guchar *buf)
1198 delete[] (Exiv2::byte*)buf;
1203 #if !EXIV2_TEST_VERSION(0,17,90)
1205 /* This is a dirty hack to support raw file preview, bassed on
1206 tiffparse.cpp from Exiv2 examples */
1211 RawFile(Exiv2::BasicIo &io);
1214 const Exiv2::Value *find(uint16_t tag, uint16_t group);
1216 unsigned long preview_offset();
1220 Exiv2::TiffComponent::AutoPtr rootDir;
1221 Exiv2::BasicIo &io_;
1222 const Exiv2::byte *map_data;
1224 unsigned long offset;
1227 typedef struct _UnmapData UnmapData;
1235 static GList *exif_unmap_list = 0;
1237 extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1239 unsigned long offset;
1241 if (!exif) return NULL;
1242 if (!exif->image()) return NULL;
1244 const char* path = exif->image()->io().path().c_str();
1246 /* given image pathname, first do simple (and fast) file extension test */
1247 if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return NULL;
1256 RawFile rf(exif->image()->io());
1257 offset = rf.preview_offset();
1258 DEBUG_1("%s: offset %lu", path, offset);
1260 fd = open(path, O_RDONLY);
1266 if (fstat(fd, &st) == -1)
1271 map_len = st.st_size;
1272 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1274 if (map_data == MAP_FAILED)
1278 *data_len = map_len - offset;
1279 ud = g_new(UnmapData, 1);
1280 ud->ptr = map_data + offset;
1281 ud->map_data = map_data;
1282 ud->map_len = map_len;
1284 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1288 catch (Exiv2::AnyError& e) {
1295 void exif_free_preview(guchar *buf)
1297 GList *work = exif_unmap_list;
1301 UnmapData *ud = (UnmapData *)work->data;
1304 munmap(ud->map_data, ud->map_len);
1305 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1311 g_assert_not_reached();
1314 using namespace Exiv2;
1316 RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
1320 if (fstat(fd, &st) == -1)
1324 map_len = st.st_size;
1325 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1326 if (map_data == MAP_FAILED)
1331 if (io.open() != 0) {
1332 throw Error(9, io.path(), strError());
1335 map_data = io.mmap();
1336 map_len = io.size();
1339 type = Exiv2::ImageFactory::getType(map_data, map_len);
1341 #if EXIV2_TEST_VERSION(0,16,0)
1342 TiffHeaderBase *tiffHeader = NULL;
1344 TiffHeade2 *tiffHeader = NULL;
1346 Cr2Header *cr2Header = NULL;
1349 case Exiv2::ImageType::tiff:
1350 tiffHeader = new TiffHeade2();
1352 case Exiv2::ImageType::cr2:
1353 cr2Header = new Cr2Header();
1355 #if EXIV2_TEST_VERSION(0,16,0)
1356 case Exiv2::ImageType::orf:
1357 tiffHeader = new OrfHeader();
1360 #if EXIV2_TEST_VERSION(0,13,0)
1361 case Exiv2::ImageType::raf:
1362 if (map_len < 84 + 4) throw Error(14);
1363 offset = getULong(map_data + 84, bigEndian);
1366 case Exiv2::ImageType::crw:
1368 // Parse the image, starting with a CIFF header component
1369 Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
1370 parseTree->read(map_data, map_len);
1371 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
1372 if (entry) offset = entry->pData() - map_data;
1377 throw Error(3, "RAW");
1380 // process tiff-like formats
1382 TiffCompFactoryFct createFct = TiffCreator::create;
1384 rootDir = createFct(Tag::root, Group::none);
1385 if (0 == rootDir.get()) {
1386 throw Error(1, "No root element defined in TIFF structure");
1391 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
1392 #if EXIV2_TEST_VERSION(0,16,0)
1393 rootDir->setStart(map_data + tiffHeader->offset());
1395 rootDir->setStart(map_data + tiffHeader->ifdOffset());
1401 rootDir->setStart(map_data + cr2Header->offset());
1404 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
1406 TiffReader reader(map_data,
1411 rootDir->accept(reader);
1419 RawFile::~RawFile(void)
1425 const Value * RawFile::find(uint16_t tag, uint16_t group)
1427 TiffFinder finder(tag, group);
1428 rootDir->accept(finder);
1429 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
1432 DEBUG_1("(tag: %04x %04x) ", tag, group);
1433 return te->pValue();
1439 unsigned long RawFile::preview_offset(void)
1442 if (offset) return offset;
1444 if (type == Exiv2::ImageType::cr2)
1446 val = find(0x111, Group::ifd0);
1447 if (val) return val->toLong();
1452 val = find(0x201, Group::sub0_0);
1453 if (val) return val->toLong();
1455 val = find(0x201, Group::ifd0);
1456 if (val) return val->toLong();
1458 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
1459 if (val) return val->toLong();
1461 val = find(0x111, Group::sub0_1); // dng
1462 if (val) return val->toLong();
1473 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */