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;
163 // This allows read-only access to the original metadata
164 struct _ExifDataOriginal : public _ExifData
167 Exiv2::Image::AutoPtr image_;
169 /* the icc profile in jpeg is not technically exif - store it here */
170 unsigned char *cp_data_;
175 Exiv2::ExifData emptyExifData_;
176 Exiv2::IptcData emptyIptcData_;
177 #if EXIV2_TEST_VERSION(0,16,0)
178 Exiv2::XmpData emptyXmpData_;
182 _ExifDataOriginal(Exiv2::Image::AutoPtr image)
190 _ExifDataOriginal(gchar *path)
196 pathl_ = path_from_utf8(path);
199 image_ = Exiv2::ImageFactory::open(pathl_);
200 // g_assert (image.get() != 0);
201 image_->readMetadata();
203 #if EXIV2_TEST_VERSION(0,16,0)
204 if (image_->mimeType() == "application/rdf+xml")
206 //Exiv2 sidecar converts xmp to exif and iptc, we don't want it.
207 image_->clearExifData();
208 image_->clearIptcData();
212 #if EXIV2_TEST_VERSION(0,14,0)
213 if (image_->mimeType() == "image/jpeg")
215 /* try to get jpeg color profile */
216 Exiv2::BasicIo &io = image_->io();
217 gint open = io.isopen();
218 if (!open) io.open();
221 unsigned char *mapped = (unsigned char*)io.mmap();
222 if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
225 if (!open) io.close();
229 catch (Exiv2::AnyError& e)
235 virtual ~_ExifDataOriginal()
237 if (cp_data_) g_free(cp_data_);
238 if (pathl_) g_free(pathl_);
241 virtual Exiv2::Image *image()
243 if (!valid_) return NULL;
247 virtual Exiv2::ExifData &exifData ()
249 if (!valid_) return emptyExifData_;
250 return image_->exifData();
253 virtual Exiv2::IptcData &iptcData ()
255 if (!valid_) return emptyIptcData_;
256 return image_->iptcData();
259 #if EXIV2_TEST_VERSION(0,16,0)
260 virtual Exiv2::XmpData &xmpData ()
262 if (!valid_) return emptyXmpData_;
263 return image_->xmpData();
267 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
269 if (cp_data_) g_free(cp_data_);
271 cp_length_ = cp_length;
274 virtual guchar *get_jpeg_color_profile(guint *data_len)
278 if (data_len) *data_len = cp_length_;
279 return (unsigned char *) g_memdup(cp_data_, cp_length_);
286 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data);
289 // This allows read-write access to the metadata
290 struct _ExifDataProcessed : public _ExifData
293 _ExifDataOriginal *imageData_;
294 _ExifDataOriginal *sidecarData_;
296 Exiv2::ExifData exifData_;
297 Exiv2::IptcData iptcData_;
298 #if EXIV2_TEST_VERSION(0,16,0)
299 Exiv2::XmpData xmpData_;
303 _ExifDataProcessed(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
305 imageData_ = new _ExifDataOriginal(path);
307 #if EXIV2_TEST_VERSION(0,16,0)
310 sidecarData_ = new _ExifDataOriginal(sidecar_path);
311 xmpData_ = sidecarData_->xmpData();
315 xmpData_ = imageData_->xmpData();
319 exifData_ = imageData_->exifData();
320 iptcData_ = imageData_->iptcData();
321 #if EXIV2_TEST_VERSION(0,17,0)
324 syncExifWithXmp(exifData_, xmpData_);
328 DEBUG_1("Exiv2: Catching bug\n");
333 g_hash_table_foreach(modified_xmp, _ExifDataProcessed_update_xmp, this);
337 virtual ~_ExifDataProcessed()
339 if (imageData_) delete imageData_;
340 if (sidecarData_) delete sidecarData_;
343 virtual ExifData *original()
348 virtual void writeMetadata(gchar *path = NULL)
352 #if EXIV2_TEST_VERSION(0,17,0)
353 if (options->metadata.save_legacy_IPTC)
354 copyXmpToIptc(xmpData_, iptcData_);
358 copyXmpToExif(xmpData_, exifData_);
360 Exiv2::Image *image = imageData_->image();
362 if (!image) Exiv2::Error(21);
363 image->setExifData(exifData_);
364 image->setIptcData(iptcData_);
365 #if EXIV2_TEST_VERSION(0,16,0)
366 image->setXmpData(xmpData_);
368 image->writeMetadata();
372 #if EXIV2_TEST_VERSION(0,17,0)
373 gchar *pathl = path_from_utf8(path);;
375 Exiv2::Image::AutoPtr sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
379 sidecar->setXmpData(xmpData_);
380 sidecar->writeMetadata();
382 throw Exiv2::Error(3, "xmp");
387 virtual Exiv2::Image *image()
389 return imageData_->image();
392 virtual Exiv2::ExifData &exifData ()
397 virtual Exiv2::IptcData &iptcData ()
402 #if EXIV2_TEST_VERSION(0,16,0)
403 virtual Exiv2::XmpData &xmpData ()
409 virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length)
411 imageData_->add_jpeg_color_profile(cp_data, cp_length);
414 virtual guchar *get_jpeg_color_profile(guint *data_len)
416 return imageData_->get_jpeg_color_profile(data_len);
428 #ifdef EXV_ENABLE_NLS
429 bind_textdomain_codeset (EXV_PACKAGE, "UTF-8");
435 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data)
437 exif_update_metadata((ExifData *)data, (gchar *)key, (GList *)value);
440 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
442 DEBUG_1("exif read %s, sidecar: %s", path, sidecar_path ? sidecar_path : "-");
444 return new _ExifDataProcessed(path, sidecar_path, modified_xmp);
446 catch (Exiv2::AnyError& e) {
453 gboolean exif_write(ExifData *exif)
456 exif->writeMetadata();
459 catch (Exiv2::AnyError& e) {
465 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
468 exif->writeMetadata(path);
471 catch (Exiv2::AnyError& e) {
479 void exif_free(ExifData *exif)
482 g_assert(dynamic_cast<_ExifDataProcessed *>(exif)); // this should not be called on ExifDataOriginal
486 ExifData *exif_get_original(ExifData *exif)
488 return exif->original();
492 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
495 Exiv2::Metadatum *item = NULL;
497 Exiv2::ExifKey ekey(key);
498 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
499 if (pos == exif->exifData().end()) return NULL;
502 catch (Exiv2::AnyError& e) {
504 Exiv2::IptcKey ekey(key);
505 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
506 if (pos == exif->iptcData().end()) return NULL;
509 catch (Exiv2::AnyError& e) {
510 #if EXIV2_TEST_VERSION(0,16,0)
511 Exiv2::XmpKey ekey(key);
512 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
513 if (pos == exif->xmpData().end()) return NULL;
518 return (ExifItem *)item;
520 catch (Exiv2::AnyError& e) {
526 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
529 Exiv2::Metadatum *item = NULL;
531 Exiv2::ExifKey ekey(key);
532 exif->exifData().add(ekey, NULL);
533 Exiv2::ExifData::iterator pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
537 catch (Exiv2::AnyError& e) {
539 Exiv2::IptcKey ekey(key);
540 exif->iptcData().add(ekey, NULL);
541 Exiv2::IptcData::iterator pos = exif->iptcData().end();
545 catch (Exiv2::AnyError& e) {
546 #if EXIV2_TEST_VERSION(0,16,0)
547 Exiv2::XmpKey ekey(key);
548 exif->xmpData().add(ekey, NULL);
549 Exiv2::XmpData::iterator pos = exif->xmpData().end();
555 return (ExifItem *)item;
557 catch (Exiv2::AnyError& e) {
564 ExifItem *exif_get_first_item(ExifData *exif)
567 exif->exifIter = exif->exifData().begin();
568 exif->iptcIter = exif->iptcData().begin();
569 #if EXIV2_TEST_VERSION(0,16,0)
570 exif->xmpIter = exif->xmpData().begin();
572 if (exif->exifIter != exif->exifData().end())
574 const Exiv2::Metadatum *item = &*exif->exifIter;
576 return (ExifItem *)item;
578 if (exif->iptcIter != exif->iptcData().end())
580 const Exiv2::Metadatum *item = &*exif->iptcIter;
582 return (ExifItem *)item;
584 #if EXIV2_TEST_VERSION(0,16,0)
585 if (exif->xmpIter != exif->xmpData().end())
587 const Exiv2::Metadatum *item = &*exif->xmpIter;
589 return (ExifItem *)item;
595 catch (Exiv2::AnyError& e) {
601 ExifItem *exif_get_next_item(ExifData *exif)
604 if (exif->exifIter != exif->exifData().end())
606 const Exiv2::Metadatum *item = &*exif->exifIter;
608 return (ExifItem *)item;
610 if (exif->iptcIter != exif->iptcData().end())
612 const Exiv2::Metadatum *item = &*exif->iptcIter;
614 return (ExifItem *)item;
616 #if EXIV2_TEST_VERSION(0,16,0)
617 if (exif->xmpIter != exif->xmpData().end())
619 const Exiv2::Metadatum *item = &*exif->xmpIter;
621 return (ExifItem *)item;
626 catch (Exiv2::AnyError& e) {
632 char *exif_item_get_tag_name(ExifItem *item)
635 if (!item) return NULL;
636 return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
638 catch (Exiv2::AnyError& e) {
644 guint exif_item_get_tag_id(ExifItem *item)
648 return ((Exiv2::Metadatum *)item)->tag();
650 catch (Exiv2::AnyError& e) {
656 guint exif_item_get_elements(ExifItem *item)
660 return ((Exiv2::Metadatum *)item)->count();
662 catch (Exiv2::AnyError& e) {
668 char *exif_item_get_data(ExifItem *item, guint *data_len)
672 Exiv2::Metadatum *md = (Exiv2::Metadatum *)item;
673 if (data_len) *data_len = md->size();
674 char *data = (char *)g_malloc(md->size());
675 long res = md->copy((Exiv2::byte *)data, Exiv2::littleEndian /* should not matter */);
676 g_assert(res == md->size());
679 catch (Exiv2::AnyError& e) {
685 char *exif_item_get_description(ExifItem *item)
688 if (!item) return NULL;
689 return utf8_validate_or_convert(((Exiv2::Metadatum *)item)->tagLabel().c_str());
691 catch (std::exception& e) {
692 // debug_exception(e);
698 invalidTypeId, unsignedByte, asciiString, unsignedShort,
699 unsignedLong, unsignedRational, signedByte, undefined,
700 signedShort, signedLong, signedRational, string,
701 date, time, comment, directory,
702 xmpText, xmpAlt, xmpBag, xmpSeq,
706 static guint format_id_trans_tbl [] = {
708 EXIF_FORMAT_BYTE_UNSIGNED,
710 EXIF_FORMAT_SHORT_UNSIGNED,
711 EXIF_FORMAT_LONG_UNSIGNED,
712 EXIF_FORMAT_RATIONAL_UNSIGNED,
714 EXIF_FORMAT_UNDEFINED,
717 EXIF_FORMAT_RATIONAL,
721 EXIF_FORMAT_UNDEFINED,
730 guint exif_item_get_format_id(ExifItem *item)
733 if (!item) return EXIF_FORMAT_UNKNOWN;
734 guint id = ((Exiv2::Metadatum *)item)->typeId();
735 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
736 return format_id_trans_tbl[id];
738 catch (Exiv2::AnyError& e) {
740 return EXIF_FORMAT_UNKNOWN;
744 const char *exif_item_get_format_name(ExifItem *item, gboolean brief)
747 if (!item) return NULL;
748 return ((Exiv2::Metadatum *)item)->typeName();
750 catch (Exiv2::AnyError& e) {
757 gchar *exif_item_get_data_as_text(ExifItem *item)
760 if (!item) return NULL;
761 Exiv2::Metadatum *metadatum = (Exiv2::Metadatum *)item;
762 #if EXIV2_TEST_VERSION(0,17,0)
763 return utf8_validate_or_convert(metadatum->print().c_str());
765 std::stringstream str;
766 Exiv2::Exifdatum *exifdatum;
767 Exiv2::Iptcdatum *iptcdatum;
768 #if EXIV2_TEST_VERSION(0,16,0)
769 Exiv2::Xmpdatum *xmpdatum;
771 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(metadatum)))
773 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(metadatum)))
775 #if EXIV2_TEST_VERSION(0,16,0)
776 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(metadatum)))
780 return utf8_validate_or_convert(str.str().c_str());
783 catch (Exiv2::AnyError& e) {
788 gchar *exif_item_get_string(ExifItem *item, int idx)
791 if (!item) return NULL;
792 Exiv2::Metadatum *em = (Exiv2::Metadatum *)item;
793 #if EXIV2_TEST_VERSION(0,16,0)
794 std::string str = em->toString(idx);
796 std::string str = em->toString(); // FIXME
798 if (idx == 0 && str == "") str = em->toString();
799 if (str.length() > 5 && str.substr(0, 5) == "lang=")
801 std::string::size_type pos = str.find_first_of(' ');
802 if (pos != std::string::npos) str = str.substr(pos+1);
805 return utf8_validate_or_convert(str.c_str());
807 catch (Exiv2::AnyError& e) {
813 gint exif_item_get_integer(ExifItem *item, gint *value)
816 if (!item || exif_item_get_elements(item) == 0) return 0;
817 *value = ((Exiv2::Metadatum *)item)->toLong();
820 catch (Exiv2::AnyError& e) {
826 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
829 if (!item) return NULL;
830 if (n >= exif_item_get_elements(item)) return NULL;
831 Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational(n);
832 static ExifRational ret;
835 if (sign) *sign = (((Exiv2::Metadatum *)item)->typeId() == Exiv2::signedRational);
838 catch (Exiv2::AnyError& e) {
844 gchar *exif_get_tag_description_by_key(const gchar *key)
847 Exiv2::ExifKey ekey(key);
848 return utf8_validate_or_convert(ekey.tagLabel().c_str());
850 catch (Exiv2::AnyError& e) {
852 Exiv2::IptcKey ikey(key);
853 return utf8_validate_or_convert(ikey.tagLabel().c_str());
855 catch (Exiv2::AnyError& e) {
857 #if EXIV2_TEST_VERSION(0,16,0)
858 Exiv2::XmpKey xkey(key);
859 return utf8_validate_or_convert(xkey.tagLabel().c_str());
862 catch (Exiv2::AnyError& e) {
871 static const AltKey *find_alt_key(const gchar *xmp_key)
875 while (alt_keys[i].xmp_key)
877 if (strcmp(xmp_key, alt_keys[i].xmp_key) == 0) return &alt_keys[i];
883 static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const GList *values)
886 const GList *work = values;
889 Exiv2::ExifKey ekey(key);
891 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
892 while (pos != exif->exifData().end())
894 exif->exifData().erase(pos);
895 pos = exif->exifData().findKey(ekey);
900 exif->exifData()[key] = (gchar *)work->data;
904 catch (Exiv2::AnyError& e) {
905 #if EXIV2_TEST_VERSION(0,16,0)
909 Exiv2::IptcKey ekey(key);
910 Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
911 while (pos != exif->iptcData().end())
913 exif->iptcData().erase(pos);
914 pos = exif->iptcData().findKey(ekey);
919 exif->iptcData()[key] = (gchar *)work->data;
923 #if EXIV2_TEST_VERSION(0,16,0)
924 catch (Exiv2::AnyError& e) {
925 Exiv2::XmpKey ekey(key);
926 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
927 while (pos != exif->xmpData().end())
929 exif->xmpData().erase(pos);
930 pos = exif->xmpData().findKey(ekey);
935 exif->xmpData()[key] = (gchar *)work->data;
943 catch (Exiv2::AnyError& e) {
949 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
951 gint ret = exif_update_metadata_simple(exif, key, values);
954 #if !EXIV2_TEST_VERSION(0,17,0)
955 TRUE || /* no conversion support */
957 !values || /* deleting item */
958 !ret /* writing to the explicitely given xmp tag failed */
961 /* deleted xmp metadatum can't be converted, we have to delete also the corresponding legacy tag */
962 /* if we can't write xmp, update at least the legacy tag */
963 const AltKey *alt_key = find_alt_key(key);
964 if (alt_key && alt_key->iptc_key)
965 ret = exif_update_metadata_simple(exif, alt_key->iptc_key, values);
967 if (alt_key && alt_key->exif_key)
968 ret = exif_update_metadata_simple(exif, alt_key->exif_key, values);
974 static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, MetadataFormat format, const Exiv2::ExifData *metadata)
976 #if EXIV2_TEST_VERSION(0,16,0)
977 Exiv2::TypeId id = item.typeId();
978 if (format == METADATA_FORMATTED ||
979 id == Exiv2::asciiString ||
980 id == Exiv2::undefined ||
981 id == Exiv2::string ||
984 id == Exiv2::xmpText ||
985 id == Exiv2::langAlt ||
990 /* read as a single entry */
993 if (format == METADATA_FORMATTED)
995 #if EXIV2_TEST_VERSION(0,17,0)
997 #if EXIV2_TEST_VERSION(0,18,0)
1002 std::stringstream stream;
1003 Exiv2::Exifdatum *exifdatum;
1004 Exiv2::Iptcdatum *iptcdatum;
1005 #if EXIV2_TEST_VERSION(0,16,0)
1006 Exiv2::Xmpdatum *xmpdatum;
1008 if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(&item)))
1009 stream << *exifdatum;
1010 else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(&item)))
1011 stream << *iptcdatum;
1012 #if EXIV2_TEST_VERSION(0,16,0)
1013 else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(&item)))
1014 stream << *xmpdatum;
1018 if (str.length() > 1024)
1020 /* truncate very long strings, they cause problems in gui */
1027 str = item.toString();
1029 if (str.length() > 5 && str.substr(0, 5) == "lang=")
1031 std::string::size_type pos = str.find_first_of(' ');
1032 if (pos != std::string::npos) str = str.substr(pos+1);
1034 list = g_list_append(list, utf8_validate_or_convert(str.c_str()));
1035 #if EXIV2_TEST_VERSION(0,16,0)
1039 /* read as a list */
1041 for (i = 0; i < item.count(); i++)
1042 list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str()));
1048 static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, MetadataFormat format)
1053 Exiv2::ExifKey ekey(key);
1054 Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
1055 if (pos != exif->exifData().end())
1056 list = exif_add_value_to_glist(list, *pos, format, &exif->exifData());
1059 catch (Exiv2::AnyError& e) {
1061 Exiv2::IptcKey ekey(key);
1062 Exiv2::IptcData::iterator pos = exif->iptcData().begin();
1063 while (pos != exif->iptcData().end())
1065 if (pos->key() == key)
1066 list = exif_add_value_to_glist(list, *pos, format, NULL);
1071 catch (Exiv2::AnyError& e) {
1072 #if EXIV2_TEST_VERSION(0,16,0)
1073 Exiv2::XmpKey ekey(key);
1074 Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
1075 if (pos != exif->xmpData().end())
1076 list = exif_add_value_to_glist(list, *pos, format, NULL);
1081 catch (Exiv2::AnyError& e) {
1087 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1091 if (!key) return NULL;
1093 if (format == METADATA_FORMATTED)
1097 text = exif_get_formatted_by_key(exif, key, &key_valid);
1098 if (key_valid) return g_list_append(NULL, text);
1101 list = exif_get_metadata_simple(exif, key, format);
1103 /* the following code can be ifdefed out as soon as Exiv2 supports it */
1106 const AltKey *alt_key = find_alt_key(key);
1107 if (alt_key && alt_key->iptc_key)
1108 list = exif_get_metadata_simple(exif, alt_key->iptc_key, format);
1110 #if !EXIV2_TEST_VERSION(0,17,0)
1111 /* with older Exiv2 versions exif is not synced */
1112 if (!list && alt_key && alt_key->exif_key)
1113 list = exif_get_metadata_simple(exif, alt_key->exif_key, format);
1120 void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
1122 exif->add_jpeg_color_profile(cp_data, cp_length);
1125 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1127 guchar *ret = exif->get_jpeg_color_profile(data_len);
1128 if (ret) return ret;
1130 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1131 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1132 ret = (guchar *)exif_item_get_data(prof_item, data_len);
1136 #if EXIV2_TEST_VERSION(0,17,90)
1138 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1140 if (!exif) return NULL;
1142 if (!exif->image()) return NULL;
1144 std::string const path = exif->image()->io().path();
1145 /* given image pathname, first do simple (and fast) file extension test */
1146 gboolean is_raw = filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE);
1148 if (!is_raw && requested_width == 0) return NULL;
1152 Exiv2::PreviewManager pm(*exif->image());
1154 Exiv2::PreviewPropertiesList list = pm.getPreviewProperties();
1158 Exiv2::PreviewPropertiesList::iterator pos;
1159 Exiv2::PreviewPropertiesList::iterator last = --list.end();
1161 if (requested_width == 0)
1163 pos = last; // the largest
1170 if (pos->width_ >= (uint32_t)requested_width &&
1171 pos->height_ >= (uint32_t)requested_height) break;
1175 // we are not interested in smaller thumbnails in normal image formats - we can use full image instead
1178 if (pos->width_ < (uint32_t)requested_width || pos->height_ < (uint32_t)requested_height) return NULL;
1182 Exiv2::PreviewImage image = pm.getPreviewImage(*pos);
1184 Exiv2::DataBuf buf = image.copy();
1185 std::pair<Exiv2::byte*, long> p = buf.release();
1187 *data_len = p.second;
1192 catch (Exiv2::AnyError& e) {
1198 void exif_free_preview(guchar *buf)
1200 delete[] (Exiv2::byte*)buf;
1205 #if !EXIV2_TEST_VERSION(0,17,90)
1207 /* This is a dirty hack to support raw file preview, bassed on
1208 tiffparse.cpp from Exiv2 examples */
1213 RawFile(Exiv2::BasicIo &io);
1216 const Exiv2::Value *find(uint16_t tag, uint16_t group);
1218 unsigned long preview_offset();
1222 Exiv2::TiffComponent::AutoPtr rootDir;
1223 Exiv2::BasicIo &io_;
1224 const Exiv2::byte *map_data;
1226 unsigned long offset;
1229 typedef struct _UnmapData UnmapData;
1237 static GList *exif_unmap_list = 0;
1239 extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1241 unsigned long offset;
1243 if (!exif) return NULL;
1244 if (!exif->image()) return NULL;
1246 std::string const path = exif->image()->io().path();
1248 /* given image pathname, first do simple (and fast) file extension test */
1249 if (!filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE)) return NULL;
1258 RawFile rf(exif->image()->io());
1259 offset = rf.preview_offset();
1260 DEBUG_1("%s: offset %lu", path.c_str(), offset);
1262 fd = open(path.c_str(), O_RDONLY);
1268 if (fstat(fd, &st) == -1)
1273 map_len = st.st_size;
1274 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1276 if (map_data == MAP_FAILED)
1280 *data_len = map_len - offset;
1281 ud = g_new(UnmapData, 1);
1282 ud->ptr = map_data + offset;
1283 ud->map_data = map_data;
1284 ud->map_len = map_len;
1286 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1290 catch (Exiv2::AnyError& e) {
1297 void exif_free_preview(guchar *buf)
1299 GList *work = exif_unmap_list;
1303 UnmapData *ud = (UnmapData *)work->data;
1306 munmap(ud->map_data, ud->map_len);
1307 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1313 g_assert_not_reached();
1316 using namespace Exiv2;
1318 RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
1322 if (fstat(fd, &st) == -1)
1326 map_len = st.st_size;
1327 map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1328 if (map_data == MAP_FAILED)
1333 if (io.open() != 0) {
1334 throw Error(9, io.path(), strError());
1337 map_data = io.mmap();
1338 map_len = io.size();
1341 type = Exiv2::ImageFactory::getType(map_data, map_len);
1343 #if EXIV2_TEST_VERSION(0,16,0)
1344 TiffHeaderBase *tiffHeader = NULL;
1346 TiffHeade2 *tiffHeader = NULL;
1348 Cr2Header *cr2Header = NULL;
1351 case Exiv2::ImageType::tiff:
1352 tiffHeader = new TiffHeade2();
1354 case Exiv2::ImageType::cr2:
1355 cr2Header = new Cr2Header();
1357 #if EXIV2_TEST_VERSION(0,16,0)
1358 case Exiv2::ImageType::orf:
1359 tiffHeader = new OrfHeader();
1362 #if EXIV2_TEST_VERSION(0,13,0)
1363 case Exiv2::ImageType::raf:
1364 if (map_len < 84 + 4) throw Error(14);
1365 offset = getULong(map_data + 84, bigEndian);
1368 case Exiv2::ImageType::crw:
1370 // Parse the image, starting with a CIFF header component
1371 Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
1372 parseTree->read(map_data, map_len);
1373 CiffComponent *entry = parseTree->findComponent(0x2007, 0);
1374 if (entry) offset = entry->pData() - map_data;
1379 throw Error(3, "RAW");
1382 // process tiff-like formats
1384 TiffCompFactoryFct createFct = TiffCreator::create;
1386 rootDir = createFct(Tag::root, Group::none);
1387 if (0 == rootDir.get()) {
1388 throw Error(1, "No root element defined in TIFF structure");
1393 if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
1394 #if EXIV2_TEST_VERSION(0,16,0)
1395 rootDir->setStart(map_data + tiffHeader->offset());
1397 rootDir->setStart(map_data + tiffHeader->ifdOffset());
1403 rootDir->setStart(map_data + cr2Header->offset());
1406 TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
1408 TiffReader reader(map_data,
1413 rootDir->accept(reader);
1421 RawFile::~RawFile(void)
1427 const Value * RawFile::find(uint16_t tag, uint16_t group)
1429 TiffFinder finder(tag, group);
1430 rootDir->accept(finder);
1431 TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
1434 DEBUG_1("(tag: %04x %04x) ", tag, group);
1435 return te->pValue();
1441 unsigned long RawFile::preview_offset(void)
1444 if (offset) return offset;
1446 if (type == Exiv2::ImageType::cr2)
1448 val = find(0x111, Group::ifd0);
1449 if (val) return val->toLong();
1454 val = find(0x201, Group::sub0_0);
1455 if (val) return val->toLong();
1457 val = find(0x201, Group::ifd0);
1458 if (val) return val->toLong();
1460 val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
1461 if (val) return val->toLong();
1463 val = find(0x111, Group::sub0_1); // dng
1464 if (val) return val->toLong();
1475 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */