/*
- * Geeqie
- * Copyright (C) 2008 The Geeqie Team
+ * Copyright (C) 2008 - 2016 The Geeqie Team
*
* Author: Vladimir Nadvornik
*
- * This software is released under the GNU General Public License (GNU GPL).
- * Please read the included file COPYING for more information.
- * This software comes with no warranty of any kind, use at your own risk!
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "config.h"
+#include "exif.h"
-#ifdef HAVE_EXIV2
+#include <algorithm>
+#include <cstdint>
+#include <cstring>
+#include <exception>
+#include <list>
+#include <memory>
+#include <string>
+#include <utility>
+#include <vector>
-#include <exiv2/image.hpp>
-#include <exiv2/exif.hpp>
-#include <iostream>
+#include <config.h>
-// EXIV2_TEST_VERSION is defined in Exiv2 0.15 and newer.
-#ifndef EXIV2_TEST_VERSION
-# define EXIV2_TEST_VERSION(major,minor,patch) \
- ( EXIV2_VERSION >= EXIV2_MAKE_VERSION(major,minor,patch) )
+#include <exiv2/exiv2.hpp>
+#include <glib.h>
+#ifdef ENABLE_NLS
+# include <libintl.h>
#endif
+#include "debug.h"
+#include "filedata.h"
+#include "filefilter.h"
+#include "misc.h"
+#include "options.h"
+#include "typedefs.h"
+#include "ui-fileops.h"
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-
-#if !EXIV2_TEST_VERSION(0,17,90)
-#include <exiv2/tiffparser.hpp>
-#include <exiv2/tiffcomposite.hpp>
-#include <exiv2/tiffvisitor.hpp>
-#include <exiv2/tiffimage.hpp>
-#include <exiv2/cr2image.hpp>
-#include <exiv2/crwimage.hpp>
-#if EXIV2_TEST_VERSION(0,16,0)
-#include <exiv2/orfimage.hpp>
-#endif
-#if EXIV2_TEST_VERSION(0,13,0)
-#include <exiv2/rafimage.hpp>
+struct ExifItem;
+
+#if EXIV2_TEST_VERSION(0,27,0)
+#define HAVE_EXIV2_ERROR_CODE
#endif
-#include <exiv2/futils.hpp>
-#else
-#include <exiv2/preview.hpp>
+
+#if EXIV2_TEST_VERSION(0,27,0)
+#define EXV_PACKAGE "exiv2"
#endif
+#if EXIV2_TEST_VERSION(0,28,0)
+#define AnyError Error
+#define AutoPtr UniquePtr
+#endif
-extern "C" {
-#include <glib.h>
+struct AltKey
+{
+ const gchar *xmp_key;
+ const gchar *exif_key;
+ const gchar *iptc_key;
+};
-#include "main.h"
-#include "exif.h"
+/* this is a list of keys that should be converted, even with the older Exiv2 which does not support it directly */
+static constexpr AltKey alt_keys[] = {
+ {"Xmp.tiff.Orientation", "Exif.Image.Orientation", nullptr},
+ {"Xmp.dc.title", nullptr, "Iptc.Application2.ObjectName" },
+ {"Xmp.photoshop.Urgency", nullptr, "Iptc.Application2.Urgency" },
+ {"Xmp.photoshop.Category", nullptr, "Iptc.Application2.Category" },
+ {"Xmp.photoshop.SupplementalCategory", nullptr, "Iptc.Application2.SuppCategory" },
+ {"Xmp.dc.subject", nullptr, "Iptc.Application2.Keywords" },
+ {"Xmp.iptc.Location", nullptr, "Iptc.Application2.LocationName" },
+ {"Xmp.photoshop.Instruction", nullptr, "Iptc.Application2.SpecialInstructions" },
+ {"Xmp.photoshop.DateCreated", nullptr, "Iptc.Application2.DateCreated" },
+ {"Xmp.dc.creator", nullptr, "Iptc.Application2.Byline" },
+ {"Xmp.photoshop.AuthorsPosition", nullptr, "Iptc.Application2.BylineTitle" },
+ {"Xmp.photoshop.City", nullptr, "Iptc.Application2.City" },
+ {"Xmp.photoshop.State", nullptr, "Iptc.Application2.ProvinceState" },
+ {"Xmp.iptc.CountryCode", nullptr, "Iptc.Application2.CountryCode" },
+ {"Xmp.photoshop.Country", nullptr, "Iptc.Application2.CountryName" },
+ {"Xmp.photoshop.TransmissionReference", nullptr, "Iptc.Application2.TransmissionReference"},
+ {"Xmp.photoshop.Headline", nullptr, "Iptc.Application2.Headline" },
+ {"Xmp.photoshop.Credit", nullptr, "Iptc.Application2.Credit" },
+ {"Xmp.photoshop.Source", nullptr, "Iptc.Application2.Source" },
+ {"Xmp.dc.rights", nullptr, "Iptc.Application2.Copyright" },
+ {"Xmp.dc.description", nullptr, "Iptc.Application2.Caption" },
+ {"Xmp.photoshop.CaptionWriter", nullptr, "Iptc.Application2.Writer" },
+ };
-#include "filefilter.h"
-#include "ui_fileops.h"
+static void _debug_exception(const char* file,
+ int line,
+ const char* func,
+ Exiv2::AnyError& e)
+{
+ gchar *str = g_locale_from_utf8(e.what(), -1, nullptr, nullptr, nullptr);
+ DEBUG_1("%s:%d:%s:Exiv2: %s", file, line, func, str);
+ g_free(str);
}
-struct _ExifData
+#define debug_exception(e) _debug_exception(__FILE__, __LINE__, __func__, e)
+
+struct ExifData
{
- Exiv2::Image::AutoPtr image;
- Exiv2::Image::AutoPtr sidecar;
Exiv2::ExifData::const_iterator exifIter; /* for exif_get_next_item */
Exiv2::IptcData::const_iterator iptcIter; /* for exif_get_next_item */
-#if EXIV2_TEST_VERSION(0,16,0)
Exiv2::XmpData::const_iterator xmpIter; /* for exif_get_next_item */
-#endif
- bool have_sidecar;
+
+ virtual ~ExifData() = default;
+
+ virtual void writeMetadata(gchar * = nullptr)
+ {
+ g_critical("Unsupported method of writing metadata");
+ }
+
+ virtual ExifData *original()
+ {
+ return nullptr;
+ }
+
+ virtual Exiv2::Image *image() = 0;
+
+ virtual Exiv2::ExifData &exifData() = 0;
+
+ virtual Exiv2::IptcData &iptcData() = 0;
+
+ virtual Exiv2::XmpData &xmpData() = 0;
+
+ virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) = 0;
+
+ virtual guchar *get_jpeg_color_profile(guint *data_len) = 0;
+
+ virtual std::string image_comment() const = 0;
+
+ virtual void set_image_comment(const std::string& comment) = 0;
+};
+
+// This allows read-only access to the original metadata
+struct ExifDataOriginal : public ExifData
+{
+protected:
+ Exiv2::Image::AutoPtr image_;
/* the icc profile in jpeg is not technically exif - store it here */
- unsigned char *cp_data;
- guint cp_length;
+ unsigned char *cp_data_;
+ guint cp_length_;
+ gboolean valid_;
+ gchar *pathl_;
+
+ Exiv2::ExifData emptyExifData_;
+ Exiv2::IptcData emptyIptcData_;
+ Exiv2::XmpData emptyXmpData_;
+
+public:
+ ExifDataOriginal(Exiv2::Image::AutoPtr image)
+ {
+ cp_data_ = nullptr;
+ cp_length_ = 0;
+ image_ = std::move(image);
+ valid_ = TRUE;
+ }
- _ExifData(gchar *path, gchar *sidecar_path)
+ ExifDataOriginal(gchar *path)
{
- have_sidecar = false;
- cp_data = NULL;
- cp_length = 0;
- gchar *pathl = path_from_utf8(path);
- image = Exiv2::ImageFactory::open(pathl);
- g_free(pathl);
-// g_assert (image.get() != 0);
- image->readMetadata();
-
-#if EXIV2_TEST_VERSION(0,16,0)
- DEBUG_2("xmp count %li", image->xmpData().count());
- if (sidecar_path && image->xmpData().empty())
+ cp_data_ = nullptr;
+ cp_length_ = 0;
+ valid_ = TRUE;
+
+ pathl_ = path_from_utf8(path);
+ try
+ {
+ image_ = Exiv2::ImageFactory::open(pathl_);
+ image_->readMetadata();
+
+ if (image_->mimeType() == "application/rdf+xml")
+ {
+ //Exiv2 sidecar converts xmp to exif and iptc, we don't want it.
+ image_->clearExifData();
+ image_->clearIptcData();
+ }
+
+ if (image_->mimeType() == "image/jpeg")
+ {
+ /* try to get jpeg color profile */
+ Exiv2::BasicIo &io = image_->io();
+ gint open = io.isopen();
+ if (!open) io.open();
+ if (io.isopen())
+ {
+ auto mapped = static_cast<unsigned char*>(io.mmap());
+ if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
+ io.munmap();
+ }
+ if (!open) io.close();
+ }
+ }
+ catch (Exiv2::AnyError& e)
{
- gchar *sidecar_pathl = path_from_utf8(sidecar_path);
- sidecar = Exiv2::ImageFactory::open(sidecar_pathl);
- g_free(sidecar_pathl);
- sidecar->readMetadata();
- have_sidecar = sidecar->good();
- DEBUG_2("sidecar xmp count %li", sidecar->xmpData().count());
+ valid_ = FALSE;
}
+ }
+
+ ~ExifDataOriginal() override
+ {
+ if (cp_data_) g_free(cp_data_);
+ if (pathl_) g_free(pathl_);
+ }
+
+ Exiv2::Image *image() override
+ {
+ if (!valid_) return nullptr;
+ return image_.get();
+ }
+
+ Exiv2::ExifData &exifData () override
+ {
+ if (!valid_) return emptyExifData_;
+ return image_->exifData();
+ }
+
+ Exiv2::IptcData &iptcData () override
+ {
+ if (!valid_) return emptyIptcData_;
+ return image_->iptcData();
+ }
+
+ Exiv2::XmpData &xmpData () override
+ {
+ if (!valid_) return emptyXmpData_;
+ return image_->xmpData();
+ }
+
+ void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) override
+ {
+ if (cp_data_) g_free(cp_data_);
+ cp_data_ = cp_data;
+ cp_length_ = cp_length;
+ }
+ guchar *get_jpeg_color_profile(guint *data_len) override
+ {
+ if (cp_data_)
+ {
+ if (data_len) *data_len = cp_length_;
+#if GLIB_CHECK_VERSION(2,68,0)
+ return static_cast<unsigned char *>(g_memdup2(cp_data_, cp_length_));
+#else
+ return static_cast<unsigned char *>(g_memdup(cp_data_, cp_length_));
#endif
-#if EXIV2_TEST_VERSION(0,14,0)
- if (image->mimeType() == std::string("image/jpeg"))
+ }
+ return nullptr;
+ }
+
+ std::string image_comment() const override
+ {
+ return image_.get() ? image_->comment() : "";
+ }
+
+ void set_image_comment(const std::string& comment) override
+ {
+ if (image_.get())
+ image_->setComment(comment);
+ }
+};
+
+static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data);
+
+// This allows read-write access to the metadata
+struct ExifDataProcessed : public ExifData
+{
+protected:
+ std::unique_ptr<ExifDataOriginal> imageData_;
+ std::unique_ptr<ExifDataOriginal> sidecarData_;
+
+ Exiv2::ExifData exifData_;
+ Exiv2::IptcData iptcData_;
+ Exiv2::XmpData xmpData_;
+
+public:
+ ExifDataProcessed(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
+ {
+ imageData_ = std::make_unique<ExifDataOriginal>(path);
+ sidecarData_ = nullptr;
+ if (sidecar_path)
{
- /* try to get jpeg color profile */
- Exiv2::BasicIo &io = image->io();
- gint open = io.isopen();
- if (!open) io.open();
- unsigned char *mapped = (unsigned char*)io.mmap();
- if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
- io.munmap();
- if (!open) io.close();
+ sidecarData_ = std::make_unique<ExifDataOriginal>(sidecar_path);
+ xmpData_ = sidecarData_->xmpData();
}
+ else
+ {
+ xmpData_ = imageData_->xmpData();
+ }
+
+ exifData_ = imageData_->exifData();
+ iptcData_ = imageData_->iptcData();
+ try
+ {
+ syncExifWithXmp(exifData_, xmpData_);
+ }
+ catch (...)
+ {
+ DEBUG_1("Exiv2: Catching bug\n");
+ }
+ if (modified_xmp)
+ {
+ g_hash_table_foreach(modified_xmp, _ExifDataProcessed_update_xmp, this);
+ }
+ }
+
+ ExifData *original() override
+ {
+ return imageData_.get();
+ }
+
+ void writeMetadata(gchar *path = nullptr) override
+ {
+ if (!path)
+ {
+ if (options->metadata.save_legacy_IPTC)
+ copyXmpToIptc(xmpData_, iptcData_);
+ else
+ iptcData_.clear();
+
+ copyXmpToExif(xmpData_, exifData_);
+ Exiv2::Image *image = imageData_->image();
+
+ if (!image)
+#ifdef HAVE_EXIV2_ERROR_CODE
+# if EXIV2_TEST_VERSION(0,28,0)
+ throw Exiv2::Error(Exiv2::ErrorCode::kerInputDataReadFailed);
+# else
+ throw Exiv2::Error(Exiv2::kerInputDataReadFailed);
+# endif
+#else
+ throw Exiv2::Error(21);
#endif
+ image->setExifData(exifData_);
+ image->setIptcData(iptcData_);
+ image->setXmpData(xmpData_);
+ image->writeMetadata();
+ }
+ else
+ {
+ gchar *pathl = path_from_utf8(path);;
+
+ auto sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
+
+ g_free(pathl);
+
+ sidecar->setXmpData(xmpData_);
+ sidecar->writeMetadata();
+ }
+ }
+
+ Exiv2::Image *image() override
+ {
+ return imageData_->image();
}
-
- ~_ExifData()
+
+ Exiv2::ExifData &exifData () override
{
- if (cp_data) g_free(cp_data);
+ return exifData_;
}
-
- void writeMetadata()
+
+ Exiv2::IptcData &iptcData () override
{
- if (have_sidecar) sidecar->writeMetadata();
- image->writeMetadata();
+ return iptcData_;
}
-
- Exiv2::ExifData &exifData ()
+
+ Exiv2::XmpData &xmpData () override
{
- return image->exifData();
+ return xmpData_;
}
- Exiv2::IptcData &iptcData ()
+ void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) override
{
- return image->iptcData();
+ imageData_->add_jpeg_color_profile(cp_data, cp_length);
}
-#if EXIV2_TEST_VERSION(0,16,0)
- Exiv2::XmpData &xmpData ()
+ guchar *get_jpeg_color_profile(guint *data_len) override
{
- return have_sidecar ? sidecar->xmpData() : image->xmpData();
+ return imageData_->get_jpeg_color_profile(data_len);
+ }
+
+ std::string image_comment() const override
+ {
+ return imageData_->image_comment();
}
-#endif
+ void set_image_comment(const std::string& comment) override
+ {
+ imageData_->set_image_comment(comment);
+ }
};
-extern "C" {
-ExifData *exif_read(gchar *path, gchar *sidecar_path)
+
+
+
+
+void exif_init()
+{
+#ifdef EXV_ENABLE_NLS
+ bind_textdomain_codeset (EXV_PACKAGE, "UTF-8");
+#endif
+
+#ifdef EXV_ENABLE_BMFF
+ Exiv2::enableBMFF(TRUE);
+#endif
+}
+
+
+
+static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data)
+{
+ exif_update_metadata(static_cast<ExifData *>(data), static_cast<gchar *>(key), static_cast<GList *>(value));
+}
+
+ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
{
DEBUG_1("exif read %s, sidecar: %s", path, sidecar_path ? sidecar_path : "-");
try {
- return new ExifData(path, sidecar_path);
+ return new ExifDataProcessed(path, sidecar_path, modified_xmp);
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
- return NULL;
+ debug_exception(e);
+ return nullptr;
}
-
+
}
-int exif_write(ExifData *exif)
+gboolean exif_write(ExifData *exif)
{
try {
exif->writeMetadata();
- return 1;
+ return TRUE;
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
- return 0;
+ debug_exception(e);
+ return FALSE;
+ }
+}
+
+gboolean exif_write_sidecar(ExifData *exif, gchar *path)
+{
+ try {
+ exif->writeMetadata(path);
+ return TRUE;
}
-
+ catch (Exiv2::AnyError& e) {
+ debug_exception(e);
+ return FALSE;
+ }
+
}
void exif_free(ExifData *exif)
{
-
+ if (!exif) return;
+ g_assert(dynamic_cast<ExifDataProcessed *>(exif)); // this should not be called on ExifDataOriginal
delete exif;
}
+ExifData *exif_get_original(ExifData *exif)
+{
+ return exif->original();
+}
+
+
ExifItem *exif_get_item(ExifData *exif, const gchar *key)
{
try {
- Exiv2::Metadatum *item = NULL;
+ Exiv2::Metadatum *item = nullptr;
try {
Exiv2::ExifKey ekey(key);
- Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
- if (pos == exif->exifData().end()) return NULL;
+ auto pos = exif->exifData().findKey(ekey);
+ if (pos == exif->exifData().end()) return nullptr;
item = &*pos;
}
catch (Exiv2::AnyError& e) {
try {
Exiv2::IptcKey ekey(key);
- Exiv2::IptcData::iterator pos = exif->iptcData().findKey(ekey);
- if (pos == exif->iptcData().end()) return NULL;
+ auto pos = exif->iptcData().findKey(ekey);
+ if (pos == exif->iptcData().end()) return nullptr;
item = &*pos;
}
catch (Exiv2::AnyError& e) {
-#if EXIV2_TEST_VERSION(0,16,0)
Exiv2::XmpKey ekey(key);
- Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
- if (pos == exif->xmpData().end()) return NULL;
+ auto pos = exif->xmpData().findKey(ekey);
+ if (pos == exif->xmpData().end()) return nullptr;
item = &*pos;
-#endif
}
}
- return (ExifItem *)item;
+ return reinterpret_cast<ExifItem *>(item);
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
- return NULL;
+ debug_exception(e);
+ return nullptr;
}
}
ExifItem *exif_add_item(ExifData *exif, const gchar *key)
{
try {
- Exiv2::Metadatum *item = NULL;
+ Exiv2::Metadatum *item = nullptr;
try {
Exiv2::ExifKey ekey(key);
- exif->exifData().add(ekey, NULL);
- Exiv2::ExifData::iterator pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
+ exif->exifData().add(ekey, nullptr);
+ auto pos = exif->exifData().end(); // a hack, there should be a better way to get the currently added item
pos--;
item = &*pos;
}
catch (Exiv2::AnyError& e) {
try {
Exiv2::IptcKey ekey(key);
- exif->iptcData().add(ekey, NULL);
- Exiv2::IptcData::iterator pos = exif->iptcData().end();
+ exif->iptcData().add(ekey, nullptr);
+ auto pos = exif->iptcData().end();
pos--;
item = &*pos;
}
catch (Exiv2::AnyError& e) {
-#if EXIV2_TEST_VERSION(0,16,0)
Exiv2::XmpKey ekey(key);
- exif->xmpData().add(ekey, NULL);
- Exiv2::XmpData::iterator pos = exif->xmpData().end();
+ exif->xmpData().add(ekey, nullptr);
+ auto pos = exif->xmpData().end();
pos--;
item = &*pos;
-#endif
}
}
- return (ExifItem *)item;
+ return reinterpret_cast<ExifItem *>(item);
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
- return NULL;
+ debug_exception(e);
+ return nullptr;
}
}
try {
exif->exifIter = exif->exifData().begin();
exif->iptcIter = exif->iptcData().begin();
-#if EXIV2_TEST_VERSION(0,16,0)
exif->xmpIter = exif->xmpData().begin();
-#endif
if (exif->exifIter != exif->exifData().end())
{
const Exiv2::Metadatum *item = &*exif->exifIter;
exif->iptcIter++;
return (ExifItem *)item;
}
-#if EXIV2_TEST_VERSION(0,16,0)
if (exif->xmpIter != exif->xmpData().end())
{
const Exiv2::Metadatum *item = &*exif->xmpIter;
exif->xmpIter++;
return (ExifItem *)item;
}
-#endif
- return NULL;
-
+ return nullptr;
+
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
- return NULL;
+ debug_exception(e);
+ return nullptr;
}
}
exif->iptcIter++;
return (ExifItem *)item;
}
-#if EXIV2_TEST_VERSION(0,16,0)
if (exif->xmpIter != exif->xmpData().end())
{
const Exiv2::Metadatum *item = &*exif->xmpIter;
exif->xmpIter++;
return (ExifItem *)item;
}
-#endif
- return NULL;
+ return nullptr;
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
- return NULL;
+ debug_exception(e);
+ return nullptr;
}
}
char *exif_item_get_tag_name(ExifItem *item)
{
try {
- if (!item) return NULL;
- return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
+ if (!item) return nullptr;
+ return g_strdup((reinterpret_cast<Exiv2::Metadatum *>(item))->key().c_str());
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
- return NULL;
+ debug_exception(e);
+ return nullptr;
}
}
{
try {
if (!item) return 0;
- return ((Exiv2::Metadatum *)item)->tag();
+ return (reinterpret_cast<Exiv2::Metadatum *>(item))->tag();
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
+ debug_exception(e);
return 0;
}
}
{
try {
if (!item) return 0;
- return ((Exiv2::Metadatum *)item)->count();
+ return (reinterpret_cast<Exiv2::Metadatum *>(item))->count();
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
+ debug_exception(e);
return 0;
}
}
char *exif_item_get_data(ExifItem *item, guint *data_len)
{
try {
- if (!item) return 0;
- Exiv2::Metadatum *md = (Exiv2::Metadatum *)item;
+ if (!item) return nullptr;
+ auto md = reinterpret_cast<Exiv2::Metadatum *>(item);
if (data_len) *data_len = md->size();
- char *data = (char *)g_malloc(md->size());
- long res = md->copy((Exiv2::byte *)data, Exiv2::littleEndian /* should not matter */);
+ auto data = static_cast<char *>(g_malloc(md->size()));
+ auto res = md->copy(reinterpret_cast<Exiv2::byte *>(data), Exiv2::littleEndian /* should not matter */);
g_assert(res == md->size());
return data;
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
- return NULL;
+ debug_exception(e);
+ return nullptr;
}
}
char *exif_item_get_description(ExifItem *item)
{
try {
- if (!item) return NULL;
- return g_locale_to_utf8(((Exiv2::Metadatum *)item)->tagLabel().c_str(), -1, NULL, NULL, NULL);
+ if (!item) return nullptr;
+ return utf8_validate_or_convert((reinterpret_cast<Exiv2::Metadatum *>(item))->tagLabel().c_str());
}
catch (std::exception& e) {
-// std::cout << "Caught Exiv2 exception '" << e << "'\n";
- return NULL;
+ return nullptr;
}
}
EXIF_FORMAT_STRING,
EXIF_FORMAT_STRING
};
-
-
+
+
guint exif_item_get_format_id(ExifItem *item)
{
try {
if (!item) return EXIF_FORMAT_UNKNOWN;
- guint id = ((Exiv2::Metadatum *)item)->typeId();
+ guint id = (reinterpret_cast<Exiv2::Metadatum *>(item))->typeId();
if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
return format_id_trans_tbl[id];
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
+ debug_exception(e);
return EXIF_FORMAT_UNKNOWN;
}
}
-const char *exif_item_get_format_name(ExifItem *item, gint brief)
+const char *exif_item_get_format_name(ExifItem *item, gboolean)
{
try {
- if (!item) return NULL;
- return ((Exiv2::Metadatum *)item)->typeName();
+ if (!item) return nullptr;
+ return (reinterpret_cast<Exiv2::Metadatum *>(item))->typeName();
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
- return NULL;
+ debug_exception(e);
+ return nullptr;
}
}
-gchar *exif_item_get_data_as_text(ExifItem *item)
+gchar *exif_item_get_data_as_text(ExifItem *item, ExifData *exif)
{
try {
- if (!item) return NULL;
- Exiv2::Metadatum *metadatum = (Exiv2::Metadatum *)item;
-#if EXIV2_TEST_VERSION(0,17,0)
- return g_locale_to_utf8(metadatum->print().c_str(), -1, NULL, NULL, NULL);
-#else
- std::stringstream str;
- Exiv2::Exifdatum *exifdatum;
- Exiv2::Iptcdatum *iptcdatum;
-#if EXIV2_TEST_VERSION(0,16,0)
- Exiv2::Xmpdatum *xmpdatum;
-#endif
- if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(metadatum)))
- str << *exifdatum;
- else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(metadatum)))
- str << *iptcdatum;
-#if EXIV2_TEST_VERSION(0,16,0)
- else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(metadatum)))
- str << *xmpdatum;
-#endif
-
- return g_locale_to_utf8(str.str().c_str(), -1, NULL, NULL, NULL);
-#endif
+ if (!item) return nullptr;
+ auto metadatum = reinterpret_cast<Exiv2::Metadatum *>(item);
+ return utf8_validate_or_convert(metadatum->print(&exif->exifData()).c_str());
}
catch (Exiv2::AnyError& e) {
- return NULL;
+ return nullptr;
}
}
gchar *exif_item_get_string(ExifItem *item, int idx)
{
try {
- if (!item) return NULL;
- Exiv2::Metadatum *em = (Exiv2::Metadatum *)item;
-#if EXIV2_TEST_VERSION(0,16,0)
+ if (!item) return nullptr;
+ auto em = reinterpret_cast<Exiv2::Metadatum *>(item);
std::string str = em->toString(idx);
-#else
- std::string str = em->toString(); // FIXME
-#endif
- if (idx == 0 && str == "") str = em->toString();
+ if (idx == 0 && str.empty()) str = em->toString();
if (str.length() > 5 && str.substr(0, 5) == "lang=")
{
std::string::size_type pos = str.find_first_of(' ');
if (pos != std::string::npos) str = str.substr(pos+1);
}
-// return g_locale_to_utf8(str.c_str(), -1, NULL, NULL, NULL); // FIXME
- return g_strdup(str.c_str());
+ return utf8_validate_or_convert(str.c_str());
}
catch (Exiv2::AnyError& e) {
- return NULL;
+ return nullptr;
}
}
gint exif_item_get_integer(ExifItem *item, gint *value)
{
try {
- if (!item) return 0;
- *value = ((Exiv2::Metadatum *)item)->toLong();
+ if (!item || exif_item_get_elements(item) == 0) return 0;
+
+#if EXIV2_TEST_VERSION(0,28,0)
+ *value = ((Exiv2::Metadatum *)item)->toInt64();
+#else
+ *value = (reinterpret_cast<Exiv2::Metadatum *>(item))->toLong();
+#endif
return 1;
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
+ debug_exception(e);
return 0;
}
}
-ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, gint n)
+ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
{
try {
- if (!item) return NULL;
- if (n >= exif_item_get_elements(item)) return NULL;
- Exiv2::Rational v = ((Exiv2::Metadatum *)item)->toRational(n);
+ if (!item) return nullptr;
+ if (n >= exif_item_get_elements(item)) return nullptr;
+ Exiv2::Rational v = (reinterpret_cast<Exiv2::Metadatum *>(item))->toRational(n);
static ExifRational ret;
ret.num = v.first;
ret.den = v.second;
- if (sign) *sign = (((Exiv2::Metadatum *)item)->typeId() == Exiv2::signedRational);
+ if (sign) *sign = ((reinterpret_cast<Exiv2::Metadatum *>(item))->typeId() == Exiv2::signedRational);
return &ret;
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
- return NULL;
+ debug_exception(e);
+ return nullptr;
}
}
{
try {
Exiv2::ExifKey ekey(key);
- return g_locale_to_utf8(Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ()), -1, NULL, NULL, NULL);
+ return utf8_validate_or_convert(ekey.tagLabel().c_str());
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
- return NULL;
+ try {
+ Exiv2::IptcKey ikey(key);
+ return utf8_validate_or_convert(ikey.tagLabel().c_str());
+ }
+ catch (Exiv2::AnyError& e) {
+ try {
+ Exiv2::XmpKey xkey(key);
+ return utf8_validate_or_convert(xkey.tagLabel().c_str());
+ }
+ catch (Exiv2::AnyError& e) {
+ debug_exception(e);
+ return nullptr;
+ }
+ }
}
+ return nullptr;
}
-int exif_item_set_string(ExifItem *item, const char *str)
+static const AltKey *find_alt_key(const gchar *xmp_key)
{
- try {
- if (!item) return 0;
- ((Exiv2::Metadatum *)item)->setValue(std::string(str));
- return 1;
- }
- catch (Exiv2::AnyError& e) {
- return 0;
- }
+ for (const auto& k : alt_keys)
+ if (strcmp(xmp_key, k.xmp_key) == 0) return &k;
+ return nullptr;
}
-int exif_item_delete(ExifData *exif, ExifItem *item)
+static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const GList *values)
{
try {
- if (!item) return 0;
- for (Exiv2::ExifData::iterator i = exif->exifData().begin(); i != exif->exifData().end(); ++i) {
- if (((Exiv2::Metadatum *)item) == &*i) {
- i = exif->exifData().erase(i);
- return 1;
- }
+ const GList *work = values;
+
+ try {
+ Exiv2::ExifKey ekey(key);
+
+ auto pos = exif->exifData().findKey(ekey);
+ while (pos != exif->exifData().end())
+ {
+ exif->exifData().erase(pos);
+ pos = exif->exifData().findKey(ekey);
+ }
+
+ while (work)
+ {
+ exif->exifData()[key] = static_cast<gchar *>(work->data);
+ work = work->next;
+ }
}
- for (Exiv2::IptcData::iterator i = exif->iptcData().begin(); i != exif->iptcData().end(); ++i) {
- if (((Exiv2::Metadatum *)item) == &*i) {
- i = exif->iptcData().erase(i);
- return 1;
+ catch (Exiv2::AnyError& e) {
+ try
+ {
+ Exiv2::IptcKey ekey(key);
+ auto pos = exif->iptcData().findKey(ekey);
+ while (pos != exif->iptcData().end())
+ {
+ exif->iptcData().erase(pos);
+ pos = exif->iptcData().findKey(ekey);
+ }
+
+ while (work)
+ {
+ exif->iptcData()[key] = static_cast<gchar *>(work->data);
+ work = work->next;
+ }
}
- }
-#if EXIV2_TEST_VERSION(0,16,0)
- for (Exiv2::XmpData::iterator i = exif->xmpData().begin(); i != exif->xmpData().end(); ++i) {
- if (((Exiv2::Metadatum *)item) == &*i) {
- i = exif->xmpData().erase(i);
- return 1;
+ catch (Exiv2::AnyError& e) {
+ Exiv2::XmpKey ekey(key);
+ auto pos = exif->xmpData().findKey(ekey);
+ while (pos != exif->xmpData().end())
+ {
+ exif->xmpData().erase(pos);
+ pos = exif->xmpData().findKey(ekey);
+ }
+
+ while (work)
+ {
+ exif->xmpData()[key] = static_cast<gchar *>(work->data);
+ work = work->next;
+ }
}
}
-#endif
- return 0;
+ return 1;
}
catch (Exiv2::AnyError& e) {
+ debug_exception(e);
return 0;
}
}
-void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
+gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
{
- if (exif->cp_data) g_free(exif->cp_data);
- exif->cp_data = cp_data;
- exif->cp_length =cp_length;
-}
+ gint ret = exif_update_metadata_simple(exif, key, values);
-guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
-{
- if (exif->cp_data)
+ if (
+ !values || /* deleting item */
+ !ret /* writing to the explicitly given xmp tag failed */
+ )
{
- if (data_len) *data_len = exif->cp_length;
- return (unsigned char *) g_memdup(exif->cp_data, exif->cp_length);
+ /* deleted xmp metadatum can't be converted, we have to delete also the corresponding legacy tag */
+ /* if we can't write xmp, update at least the legacy tag */
+ const AltKey *alt_key = find_alt_key(key);
+ if (alt_key && alt_key->iptc_key)
+ ret = exif_update_metadata_simple(exif, alt_key->iptc_key, values);
+
+ if (alt_key && alt_key->exif_key)
+ ret = exif_update_metadata_simple(exif, alt_key->exif_key, values);
}
- ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
- if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
- return (unsigned char *) exif_item_get_data(prof_item, data_len);
- return NULL;
+ return ret;
}
-#if EXIV2_TEST_VERSION(0,17,90)
-guchar *exif_get_preview(ExifData *exif, guint *data_len)
+static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, MetadataFormat format, const Exiv2::ExifData *metadata)
{
- if (!exif) return NULL;
-
- const char* path = exif->image->io().path().c_str();
- /* given image pathname, first do simple (and fast) file extension test */
- if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return NULL;
-
- try {
-
- Exiv2::PreviewImageLoader loader(*exif->image);
-
- Exiv2::PreviewPropertiesList list = loader.getPreviewPropertiesList();
+ Exiv2::TypeId id = item.typeId();
+ if (format == METADATA_FORMATTED ||
+ id == Exiv2::asciiString ||
+ id == Exiv2::undefined ||
+ id == Exiv2::string ||
+ id == Exiv2::date ||
+ id == Exiv2::time ||
+ id == Exiv2::xmpText ||
+ id == Exiv2::langAlt ||
+ id == Exiv2::comment
+ )
+ {
+ /* read as a single entry */
+ std::string str;
- if (!list.empty())
+ if (format == METADATA_FORMATTED)
{
- Exiv2::PreviewPropertiesList::iterator pos = --list.end();
-
- Exiv2::PreviewImage image = loader.getPreviewImage(*pos);
-
- Exiv2::DataBuf &buf = image.data();
- std::pair<Exiv2::byte*, long> p = buf.release();
-
- *data_len = p.second;
- return p.first;
+ str = item.print(metadata);
+ if (str.length() > 1024)
+ {
+ /* truncate very long strings, they cause problems in gui */
+ str.erase(1024);
+ str.append("...");
+ }
}
- return NULL;
- }
- catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
- return NULL;
- }
-}
-
-void exif_free_preview(guchar *buf)
-{
- delete[] (Exiv2::byte*)buf;
-}
+ else
+ {
+ str = item.toString();
+ }
+ if (str.length() > 5 && str.substr(0, 5) == "lang=")
+ {
+ std::string::size_type pos = str.find_first_of(' ');
+ if (pos != std::string::npos) str = str.substr(pos+1);
+ }
+ list = g_list_append(list, utf8_validate_or_convert(str.c_str()));
+ }
+ else
+ {
+ /* read as a list */
+#if EXIV2_TEST_VERSION(0,28,0)
+ size_t i;
+#else
+ long i;
#endif
-
+ for (i = 0; i < item.count(); i++)
+ list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str()));
+ }
+ return list;
}
-#if !EXIV2_TEST_VERSION(0,17,90)
-/* This is a dirty hack to support raw file preview, bassed on
-tiffparse.cpp from Exiv2 examples */
-
-class RawFile {
- public:
-
- RawFile(Exiv2::BasicIo &io);
- ~RawFile();
-
- const Exiv2::Value *find(uint16_t tag, uint16_t group);
-
- unsigned long preview_offset();
-
- private:
- int type;
- Exiv2::TiffComponent::AutoPtr rootDir;
- Exiv2::BasicIo &io_;
- const Exiv2::byte *map_data;
- size_t map_len;
- unsigned long offset;
-};
-
-typedef struct _UnmapData UnmapData;
-struct _UnmapData
+static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, MetadataFormat format)
{
- guchar *ptr;
- guchar *map_data;
- size_t map_len;
-};
-
-static GList *exif_unmap_list = 0;
-
-extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len)
-{
- unsigned long offset;
-
- if (!exif) return NULL;
- const char* path = exif->image->io().path().c_str();
-
- /* given image pathname, first do simple (and fast) file extension test */
- if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return NULL;
-
+ GList *list = nullptr;
try {
- struct stat st;
- guchar *map_data;
- size_t map_len;
- UnmapData *ud;
- int fd;
-
- RawFile rf(exif->image->io());
- offset = rf.preview_offset();
- DEBUG_1("%s: offset %lu", path, offset);
-
- fd = open(path, O_RDONLY);
- if (fd == -1)
- {
- return NULL;
- }
+ try {
+ Exiv2::ExifKey ekey(key);
+ auto pos = exif->exifData().findKey(ekey);
+ if (pos != exif->exifData().end())
+ list = exif_add_value_to_glist(list, *pos, format, &exif->exifData());
+
+ }
+ catch (Exiv2::AnyError& e) {
+ try {
+ Exiv2::IptcKey ekey(key);
+ auto pos = exif->iptcData().begin();
+ while (pos != exif->iptcData().end())
+ {
+ if (pos->key() == key)
+ list = exif_add_value_to_glist(list, *pos, format, nullptr);
+ ++pos;
+ }
- if (fstat(fd, &st) == -1)
- {
- close(fd);
- return NULL;
}
- map_len = st.st_size;
- map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
- close(fd);
- if (map_data == MAP_FAILED)
- {
- return NULL;
+ catch (Exiv2::AnyError& e) {
+ Exiv2::XmpKey ekey(key);
+ auto pos = exif->xmpData().findKey(ekey);
+ if (pos != exif->xmpData().end())
+ list = exif_add_value_to_glist(list, *pos, format, nullptr);
}
- *data_len = map_len - offset;
- ud = g_new(UnmapData, 1);
- ud->ptr = map_data + offset;
- ud->map_data = map_data;
- ud->map_len = map_len;
-
- exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
- return ud->ptr;
-
+ }
}
catch (Exiv2::AnyError& e) {
- std::cout << "Caught Exiv2 exception '" << e << "'\n";
+ debug_exception(e);
}
- return NULL;
-
+ return list;
}
-void exif_free_preview(guchar *buf)
+GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
{
- GList *work = exif_unmap_list;
-
- while (work)
- {
- UnmapData *ud = (UnmapData *)work->data;
- if (ud->ptr == buf)
- {
- munmap(ud->map_data, ud->map_len);
- exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
- g_free(ud);
- return;
- }
- work = work->next;
- }
- g_assert_not_reached();
-}
+ GList *list = nullptr;
-using namespace Exiv2;
+ if (!key) return nullptr;
-RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
-{
-/*
- struct stat st;
- if (fstat(fd, &st) == -1)
+ if (format == METADATA_FORMATTED)
{
- throw Error(14);
+ gchar *text;
+ gint key_valid;
+ text = exif_get_formatted_by_key(exif, key, &key_valid);
+ if (key_valid) return g_list_append(nullptr, text);
}
- map_len = st.st_size;
- map_data = (Exiv2::byte *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
- if (map_data == MAP_FAILED)
- {
- throw Error(14);
- }
-*/
- if (io.open() != 0) {
- throw Error(9, io.path(), strError());
- }
-
- map_data = io.mmap();
- map_len = io.size();
-
-
- type = Exiv2::ImageFactory::getType(map_data, map_len);
-
-#if EXIV2_TEST_VERSION(0,16,0)
- TiffHeaderBase *tiffHeader = NULL;
-#else
- TiffHeade2 *tiffHeader = NULL;
-#endif
- Cr2Header *cr2Header = NULL;
-
- switch (type) {
- case Exiv2::ImageType::tiff:
- tiffHeader = new TiffHeade2();
- break;
- case Exiv2::ImageType::cr2:
- cr2Header = new Cr2Header();
- break;
-#if EXIV2_TEST_VERSION(0,16,0)
- case Exiv2::ImageType::orf:
- tiffHeader = new OrfHeader();
- break;
-#endif
-#if EXIV2_TEST_VERSION(0,13,0)
- case Exiv2::ImageType::raf:
- if (map_len < 84 + 4) throw Error(14);
- offset = getULong(map_data + 84, bigEndian);
- return;
-#endif
- case Exiv2::ImageType::crw:
- {
- // Parse the image, starting with a CIFF header component
- Exiv2::CiffHeader::AutoPtr parseTree(new Exiv2::CiffHeader);
- parseTree->read(map_data, map_len);
- CiffComponent *entry = parseTree->findComponent(0x2007, 0);
- if (entry) offset = entry->pData() - map_data;
- return;
- }
-
- default:
- throw Error(3, "RAW");
- }
-
- // process tiff-like formats
- TiffCompFactoryFct createFct = TiffCreator::create;
+ list = exif_get_metadata_simple(exif, key, format);
- rootDir = createFct(Tag::root, Group::none);
- if (0 == rootDir.get()) {
- throw Error(1, "No root element defined in TIFF structure");
- }
-
- if (tiffHeader)
- {
- if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
-#if EXIV2_TEST_VERSION(0,16,0)
- rootDir->setStart(map_data + tiffHeader->offset());
-#else
- rootDir->setStart(map_data + tiffHeader->ifdOffset());
-#endif
- }
-
- if (cr2Header)
+ /* the following code can be ifdefed out as soon as Exiv2 supports it */
+ if (!list)
{
- rootDir->setStart(map_data + cr2Header->offset());
+ const AltKey *alt_key = find_alt_key(key);
+ if (alt_key && alt_key->iptc_key)
+ list = exif_get_metadata_simple(exif, alt_key->iptc_key, format);
}
-
- TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
+ return list;
+}
- TiffReader reader(map_data,
- map_len,
- rootDir.get(),
- state);
- rootDir->accept(reader);
-
- if (tiffHeader)
- delete tiffHeader;
- if (cr2Header)
- delete cr2Header;
+void exif_add_jpeg_color_profile(ExifData *exif, unsigned char *cp_data, guint cp_length)
+{
+ exif->add_jpeg_color_profile(cp_data, cp_length);
}
-RawFile::~RawFile(void)
+guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
{
- io_.munmap();
- io_.close();
+ guchar *ret = exif->get_jpeg_color_profile(data_len);
+ if (ret) return ret;
+
+ ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
+ if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
+ ret = reinterpret_cast<guchar *>(exif_item_get_data(prof_item, data_len));
+ return ret;
}
-const Value * RawFile::find(uint16_t tag, uint16_t group)
+gchar* exif_get_image_comment(FileData* fd)
{
- TiffFinder finder(tag, group);
- rootDir->accept(finder);
- TiffEntryBase* te = dynamic_cast<TiffEntryBase*>(finder.result());
- if (te)
- {
- DEBUG_1("(tag: %04x %04x) ", tag, group);
- return te->pValue();
- }
- else
- return NULL;
+ if (!fd || !fd->exif)
+ return g_strdup("");
+
+ return g_strdup(fd->exif->image_comment().c_str());
}
-unsigned long RawFile::preview_offset(void)
+void exif_set_image_comment(FileData* fd, const gchar* comment)
{
- const Value *val;
- if (offset) return offset;
-
- if (type == Exiv2::ImageType::cr2)
- {
- val = find(0x111, Group::ifd0);
- if (val) return val->toLong();
+ if (!fd || !fd->exif)
+ return;
- return 0;
- }
-
- val = find(0x201, Group::sub0_0);
- if (val) return val->toLong();
+ fd->exif->set_image_comment(comment ? comment : "");
+}
- val = find(0x201, Group::ifd0);
- if (val) return val->toLong();
- val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
- if (val) return val->toLong();
+guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
+{
+ if (!exif) return nullptr;
- val = find(0x111, Group::sub0_1); // dng
- if (val) return val->toLong();
+ if (!exif->image()) return nullptr;
- return 0;
-}
+ std::string const path = exif->image()->io().path();
+ /* given image pathname, first do simple (and fast) file extension test */
+ gboolean is_raw = filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE);
+ if (!is_raw && requested_width == 0) return nullptr;
-#endif
+ try {
+ Exiv2::PreviewManager pm(*exif->image());
-#endif
-/* HAVE_EXIV2 */
+ Exiv2::PreviewPropertiesList list = pm.getPreviewProperties();
+
+ if (!list.empty())
+ {
+ Exiv2::PreviewPropertiesList::iterator pos;
+ auto last = --list.end();
+
+ if (requested_width == 0)
+ {
+ pos = last; // the largest
+ }
+ else
+ {
+ pos = list.begin();
+ while (pos != last)
+ {
+ if (pos->width_ >= static_cast<uint32_t>(requested_width) &&
+ pos->height_ >= static_cast<uint32_t>(requested_height)) break;
+ ++pos;
+ }
+
+ // we are not interested in smaller thumbnails in normal image formats - we can use full image instead
+ if (!is_raw)
+ {
+ if (pos->width_ < static_cast<uint32_t>(requested_width) || pos->height_ < static_cast<uint32_t>(requested_height)) return nullptr;
+ }
+ }
+
+ Exiv2::PreviewImage image = pm.getPreviewImage(*pos);
+
+ // Let's not touch data_len until we finish copy.
+ // Just in case we run into OOM.
+ size_t img_sz = image.size();
+ auto* b = new Exiv2::byte[img_sz];
+ std::copy_n(image.pData(), img_sz, b);
+ *data_len = img_sz;
+ return b;
+ }
+ return nullptr;
+ }
+ catch (Exiv2::AnyError& e) {
+ debug_exception(e);
+ return nullptr;
+ }
+}
+
+void exif_free_preview(const guchar *buf)
+{
+ delete[] static_cast<const Exiv2::byte*>(buf);
+}
+
+/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */