Use util_clip_triangle() in pan_item_tri_new()
[geeqie.git] / src / exiv2.cc
index ca70401..e42f1af 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include <config.h>
-
-#ifdef HAVE_EXIV2
+#include "exif.h"
 
-// Don't include the <exiv2/version.hpp> file directly
-// Early Exiv2 versions didn't have version.hpp and the macros.
-#include <exiv2/exiv2.hpp>
-#include <iostream>
+#include <algorithm>
+#include <cstdint>
+#include <cstring>
+#include <exception>
+#include <list>
+#include <memory>
 #include <string>
+#include <utility>
+#include <vector>
 
-// EXIV2_TEST_VERSION is defined in Exiv2 0.15 and newer.
-#ifdef EXIV2_VERSION
-#ifndef EXIV2_TEST_VERSION
-#define EXIV2_TEST_VERSION(major,minor,patch) \
-       ( EXIV2_VERSION >= EXIV2_MAKE_VERSION(major,minor,patch) )
-#endif
-#else
-#define EXIV2_TEST_VERSION(major,minor,patch) (false)
+#include <config.h>
+
+#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"
+
+struct ExifItem;
+
 #if EXIV2_TEST_VERSION(0,27,0)
 #define HAVE_EXIV2_ERROR_CODE
 #endif
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-
 #if EXIV2_TEST_VERSION(0,27,0)
 #define EXV_PACKAGE "exiv2"
 #endif
 
-#include <glib.h>
-
-#include "main.h"
-#include "exif.h"
-
-#include "filefilter.h"
-#include "ui-fileops.h"
-
-#include "misc.h"
-
 #if EXIV2_TEST_VERSION(0,28,0)
 #define AnyError Error
 #define AutoPtr UniquePtr
@@ -75,7 +69,7 @@ struct AltKey
 };
 
 /* this is a list of keys that should be converted, even with the older Exiv2 which does not support it directly */
-static const AltKey alt_keys[] = {
+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"             },
@@ -98,7 +92,6 @@ static const AltKey alt_keys[] = {
        {"Xmp.dc.rights",                       nullptr,                                "Iptc.Application2.Copyright"           },
        {"Xmp.dc.description",                  nullptr,                                "Iptc.Application2.Caption"             },
        {"Xmp.photoshop.CaptionWriter",         nullptr,                                "Iptc.Application2.Writer"              },
-       {nullptr, nullptr, nullptr}
        };
 
 static void _debug_exception(const char* file,
@@ -117,13 +110,11 @@ struct ExifData
 {
        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
 
        virtual ~ExifData() = default;
 
-       virtual void writeMetadata(gchar *UNUSED(path) = nullptr)
+       virtual void writeMetadata(gchar * = nullptr)
        {
                g_critical("Unsupported method of writing metadata");
        }
@@ -139,9 +130,7 @@ struct ExifData
 
        virtual Exiv2::IptcData &iptcData() = 0;
 
-#if EXIV2_TEST_VERSION(0,16,0)
        virtual Exiv2::XmpData &xmpData() = 0;
-#endif
 
        virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) = 0;
 
@@ -166,9 +155,7 @@ protected:
 
        Exiv2::ExifData emptyExifData_;
        Exiv2::IptcData emptyIptcData_;
-#if EXIV2_TEST_VERSION(0,16,0)
        Exiv2::XmpData emptyXmpData_;
-#endif
 
 public:
        ExifDataOriginal(Exiv2::Image::AutoPtr image)
@@ -189,19 +176,15 @@ public:
                try
                        {
                        image_ = Exiv2::ImageFactory::open(pathl_);
-//                     g_assert (image.get() != 0);
                        image_->readMetadata();
 
-#if EXIV2_TEST_VERSION(0,16,0)
                        if (image_->mimeType() == "application/rdf+xml")
                                {
                                //Exiv2 sidecar converts xmp to exif and iptc, we don't want it.
                                image_->clearExifData();
                                image_->clearIptcData();
                                }
-#endif
 
-#if EXIV2_TEST_VERSION(0,14,0)
                        if (image_->mimeType() == "image/jpeg")
                                {
                                /* try to get jpeg color profile */
@@ -216,7 +199,6 @@ public:
                                        }
                                if (!open) io.close();
                                }
-#endif
                        }
                catch (Exiv2::AnyError& e)
                        {
@@ -248,13 +230,11 @@ public:
                return image_->iptcData();
        }
 
-#if EXIV2_TEST_VERSION(0,16,0)
        Exiv2::XmpData &xmpData () override
        {
                if (!valid_) return emptyXmpData_;
                return image_->xmpData();
        }
-#endif
 
        void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) override
        {
@@ -300,16 +280,13 @@ protected:
 
        Exiv2::ExifData exifData_;
        Exiv2::IptcData iptcData_;
-#if EXIV2_TEST_VERSION(0,16,0)
        Exiv2::XmpData xmpData_;
-#endif
 
 public:
        ExifDataProcessed(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
        {
                imageData_ = std::make_unique<ExifDataOriginal>(path);
                sidecarData_ = nullptr;
-#if EXIV2_TEST_VERSION(0,16,0)
                if (sidecar_path)
                        {
                        sidecarData_ = std::make_unique<ExifDataOriginal>(sidecar_path);
@@ -320,10 +297,8 @@ public:
                        xmpData_ = imageData_->xmpData();
                        }
 
-#endif
                exifData_ = imageData_->exifData();
                iptcData_ = imageData_->iptcData();
-#if EXIV2_TEST_VERSION(0,17,0)
                try
                        {
                        syncExifWithXmp(exifData_, xmpData_);
@@ -332,7 +307,6 @@ public:
                        {
                        DEBUG_1("Exiv2: Catching bug\n");
                        }
-#endif
                if (modified_xmp)
                        {
                        g_hash_table_foreach(modified_xmp, _ExifDataProcessed_update_xmp, this);
@@ -348,35 +322,31 @@ public:
        {
                if (!path)
                        {
-#if EXIV2_TEST_VERSION(0,17,0)
                        if (options->metadata.save_legacy_IPTC)
                                copyXmpToIptc(xmpData_, iptcData_);
                        else
                                iptcData_.clear();
 
                        copyXmpToExif(xmpData_, exifData_);
-#endif
                        Exiv2::Image *image = imageData_->image();
 
+                       if (!image)
 #ifdef HAVE_EXIV2_ERROR_CODE
-#if EXIV2_TEST_VERSION(0,28,0)
-            if (!image) throw Exiv2::Error(Exiv2::ErrorCode::kerInputDataReadFailed);
+#  if EXIV2_TEST_VERSION(0,28,0)
+                               throw Exiv2::Error(Exiv2::ErrorCode::kerInputDataReadFailed);
+#  else
+                               throw Exiv2::Error(Exiv2::kerInputDataReadFailed);
+#  endif
 #else
-                       if (!image) throw Exiv2::Error(Exiv2::kerInputDataReadFailed);
-#endif
-#else
-                       if (!image) throw Exiv2::Error(21);
+                               throw Exiv2::Error(21);
 #endif
                        image->setExifData(exifData_);
                        image->setIptcData(iptcData_);
-#if EXIV2_TEST_VERSION(0,16,0)
                        image->setXmpData(xmpData_);
-#endif
                        image->writeMetadata();
                        }
                else
                        {
-#if EXIV2_TEST_VERSION(0,17,0)
                        gchar *pathl = path_from_utf8(path);;
 
                        auto sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
@@ -385,13 +355,6 @@ public:
 
                        sidecar->setXmpData(xmpData_);
                        sidecar->writeMetadata();
-#else
-#ifdef HAVE_EXIV2_ERROR_CODE
-                       throw Exiv2::Error(Exiv2::kerNotAnImage, "xmp");
-#else
-                       throw Exiv2::Error(3, "xmp");
-#endif
-#endif
                        }
        }
 
@@ -410,12 +373,10 @@ public:
                return iptcData_;
        }
 
-#if EXIV2_TEST_VERSION(0,16,0)
        Exiv2::XmpData &xmpData () override
        {
                return xmpData_;
        }
-#endif
 
        void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) override
        {
@@ -531,12 +492,10 @@ ExifItem *exif_get_item(ExifData *exif, const gchar *key)
                                item = &*pos;
                        }
                        catch (Exiv2::AnyError& e) {
-#if EXIV2_TEST_VERSION(0,16,0)
                                Exiv2::XmpKey ekey(key);
                                auto pos = exif->xmpData().findKey(ekey);
                                if (pos == exif->xmpData().end()) return nullptr;
                                item = &*pos;
-#endif
                        }
                }
                return reinterpret_cast<ExifItem *>(item);
@@ -567,13 +526,11 @@ ExifItem *exif_add_item(ExifData *exif, const gchar *key)
                                item = &*pos;
                        }
                        catch (Exiv2::AnyError& e) {
-#if EXIV2_TEST_VERSION(0,16,0)
                                Exiv2::XmpKey ekey(key);
                                exif->xmpData().add(ekey, nullptr);
                                auto pos = exif->xmpData().end();
                                pos--;
                                item = &*pos;
-#endif
                        }
                }
                return reinterpret_cast<ExifItem *>(item);
@@ -590,9 +547,7 @@ ExifItem *exif_get_first_item(ExifData *exif)
        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;
@@ -605,14 +560,12 @@ ExifItem *exif_get_first_item(ExifData *exif)
                        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 nullptr;
 
        }
@@ -637,14 +590,12 @@ ExifItem *exif_get_next_item(ExifData *exif)
                        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 nullptr;
        }
        catch (Exiv2::AnyError& e) {
@@ -696,7 +647,7 @@ char *exif_item_get_data(ExifItem *item, guint *data_len)
                auto md = reinterpret_cast<Exiv2::Metadatum *>(item);
                if (data_len) *data_len = md->size();
                auto data = static_cast<char *>(g_malloc(md->size()));
-               long res = md->copy(reinterpret_cast<Exiv2::byte *>(data), Exiv2::littleEndian /* should not matter */);
+               auto res = md->copy(reinterpret_cast<Exiv2::byte *>(data), Exiv2::littleEndian /* should not matter */);
                g_assert(res == md->size());
                return data;
        }
@@ -713,7 +664,6 @@ char *exif_item_get_description(ExifItem *item)
                return utf8_validate_or_convert((reinterpret_cast<Exiv2::Metadatum *>(item))->tagLabel().c_str());
        }
        catch (std::exception& e) {
-//             debug_exception(e);
                return nullptr;
        }
 }
@@ -765,7 +715,7 @@ guint exif_item_get_format_id(ExifItem *item)
        }
 }
 
-const char *exif_item_get_format_name(ExifItem *item, gboolean UNUSED(brief))
+const char *exif_item_get_format_name(ExifItem *item, gboolean)
 {
        try {
                if (!item) return nullptr;
@@ -783,26 +733,7 @@ gchar *exif_item_get_data_as_text(ExifItem *item, ExifData *exif)
        try {
                if (!item) return nullptr;
                auto metadatum = reinterpret_cast<Exiv2::Metadatum *>(item);
-#if EXIV2_TEST_VERSION(0,17,0)
                return utf8_validate_or_convert(metadatum->print(&exif->exifData()).c_str());
-#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 utf8_validate_or_convert(str.str().c_str());
-#endif
        }
        catch (Exiv2::AnyError& e) {
                return nullptr;
@@ -814,11 +745,7 @@ gchar *exif_item_get_string(ExifItem *item, int idx)
        try {
                if (!item) return nullptr;
                auto em = reinterpret_cast<Exiv2::Metadatum *>(item);
-#if EXIV2_TEST_VERSION(0,16,0)
                std::string str = em->toString(idx);
-#else
-               std::string str = em->toString(); /**< @FIXME ?? */
-#endif
                if (idx == 0 && str.empty()) str = em->toString();
                if (str.length() > 5 && str.substr(0, 5) == "lang=")
                        {
@@ -883,10 +810,8 @@ gchar *exif_get_tag_description_by_key(const gchar *key)
                }
                catch (Exiv2::AnyError& e) {
                        try {
-#if EXIV2_TEST_VERSION(0,16,0)
                                Exiv2::XmpKey xkey(key);
                                return utf8_validate_or_convert(xkey.tagLabel().c_str());
-#endif
                        }
                        catch (Exiv2::AnyError& e) {
                                debug_exception(e);
@@ -899,13 +824,8 @@ gchar *exif_get_tag_description_by_key(const gchar *key)
 
 static const AltKey *find_alt_key(const gchar *xmp_key)
 {
-       gint i = 0;
-
-       while (alt_keys[i].xmp_key)
-               {
-               if (strcmp(xmp_key, alt_keys[i].xmp_key) == 0) return &alt_keys[i];
-               i++;
-               }
+       for (const auto& k : alt_keys)
+               if (strcmp(xmp_key, k.xmp_key) == 0) return &k;
        return nullptr;
 }
 
@@ -931,9 +851,7 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
                                }
                }
                catch (Exiv2::AnyError& e) {
-#if EXIV2_TEST_VERSION(0,16,0)
                        try
-#endif
                        {
                                Exiv2::IptcKey ekey(key);
                                auto pos = exif->iptcData().findKey(ekey);
@@ -949,7 +867,6 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
                                        work = work->next;
                                        }
                        }
-#if EXIV2_TEST_VERSION(0,16,0)
                        catch (Exiv2::AnyError& e) {
                                Exiv2::XmpKey ekey(key);
                                auto pos = exif->xmpData().findKey(ekey);
@@ -965,7 +882,6 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
                                        work = work->next;
                                        }
                        }
-#endif
                }
                return 1;
        }
@@ -980,9 +896,6 @@ gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
        gint ret = exif_update_metadata_simple(exif, key, values);
 
        if (
-#if !EXIV2_TEST_VERSION(0,17,0)
-           TRUE || /* no conversion support */
-#endif
            !values || /* deleting item */
            !ret  /* writing to the explicitly given xmp tag failed */
            )
@@ -1002,7 +915,6 @@ gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
 
 static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, MetadataFormat format, const Exiv2::ExifData *metadata)
 {
-#if EXIV2_TEST_VERSION(0,16,0)
        Exiv2::TypeId id = item.typeId();
        if (format == METADATA_FORMATTED ||
            id == Exiv2::asciiString ||
@@ -1015,35 +927,12 @@ static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, Metad
            id == Exiv2::comment
            )
                {
-#endif
                /* read as a single entry */
                std::string str;
 
                if (format == METADATA_FORMATTED)
                        {
-#if EXIV2_TEST_VERSION(0,17,0)
-                       str = item.print(
-#if EXIV2_TEST_VERSION(0,18,0)
-                                       metadata
-#endif
-                                       );
-#else
-                       std::stringstream stream;
-                       Exiv2::Exifdatum *exifdatum;
-                       Exiv2::Iptcdatum *iptcdatum;
-#if EXIV2_TEST_VERSION(0,16,0)
-                       Exiv2::Xmpdatum *xmpdatum;
-#endif
-                       if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(&item)))
-                               stream << *exifdatum;
-                       else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(&item)))
-                               stream << *iptcdatum;
-#if EXIV2_TEST_VERSION(0,16,0)
-                       else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(&item)))
-                               stream << *xmpdatum;
-#endif
-                       str = stream.str();
-#endif
+                       str = item.print(metadata);
                        if (str.length() > 1024)
                                {
                                /* truncate very long strings, they cause problems in gui */
@@ -1061,16 +950,18 @@ static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, Metad
                        if (pos != std::string::npos) str = str.substr(pos+1);
                        }
                list = g_list_append(list, utf8_validate_or_convert(str.c_str()));
-#if EXIV2_TEST_VERSION(0,16,0)
                }
        else
                {
                /* read as a list */
-               gint i;
+#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()));
                }
-#endif
        return list;
 }
 
@@ -1098,12 +989,10 @@ static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, Metadat
 
                        }
                        catch (Exiv2::AnyError& e) {
-#if EXIV2_TEST_VERSION(0,16,0)
                                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);
-#endif
                        }
                }
        }
@@ -1135,12 +1024,6 @@ GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format
                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);
-
-#if !EXIV2_TEST_VERSION(0,17,0)
-               /* with older Exiv2 versions exif is not synced */
-               if (!list && alt_key && alt_key->exif_key)
-                       list = exif_get_metadata_simple(exif, alt_key->exif_key, format);
-#endif
                }
        return list;
 }
@@ -1179,8 +1062,6 @@ void exif_set_image_comment(FileData* fd, const gchar* comment)
 }
 
 
-#if EXIV2_TEST_VERSION(0,17,90)
-
 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
 {
        if (!exif) return nullptr;
@@ -1227,19 +1108,13 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
 
                        Exiv2::PreviewImage image = pm.getPreviewImage(*pos);
 
-                       Exiv2::DataBuf buf = image.copy();
-
-#if EXIV2_TEST_VERSION(0,28,0)
-                       *data_len = buf.size();
-                       auto b = buf.data();
-                       buf.reset();
-                       return b;
-#else
-                       std::pair<Exiv2::byte*, long> p = buf.release();
-
-                       *data_len = p.second;
-                       return p.first;
-#endif
+                       // 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;
        }
@@ -1249,293 +1124,9 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
        }
 }
 
-void exif_free_preview(guchar *buf)
+void exif_free_preview(const guchar *buf)
 {
-       delete[] static_cast<Exiv2::byte*>(buf);
+       delete[] static_cast<const Exiv2::byte*>(buf);
 }
-#endif
-
-#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;
-};
-
-struct UnmapData
-{
-       guchar *ptr;
-       guchar *map_data;
-       size_t map_len;
-};
-
-static GList *exif_unmap_list = 0;
-
-guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
-{
-       unsigned long offset;
-
-       if (!exif) return NULL;
-       if (!exif->image()) return NULL;
-
-       std::string const path = exif->image()->io().path();
-
-       /* given image pathname, first do simple (and fast) file extension test */
-       if (!filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE)) return NULL;
 
-       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.c_str(), offset);
-
-               fd = open(path.c_str(), O_RDONLY);
-               if (fd == -1)
-                       {
-                       return NULL;
-                       }
-
-               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;
-                       }
-               *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) {
-               debug_exception(e);
-       }
-       return NULL;
-
-}
-
-void exif_free_preview(guchar *buf)
-{
-       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();
-}
-
-using namespace Exiv2;
-
-RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
-{
-/*
-       struct stat st;
-       if (fstat(fd, &st) == -1)
-               {
-               throw Error(14);
-               }
-       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)
-       std::unique_ptr<TiffHeaderBase> tiffHeader;
-#else
-       std::unique_ptr<TiffHeade2> tiffHeader;
-#endif
-       std::unique_ptr<Cr2Header> cr2Header;
-
-       switch (type) {
-               case Exiv2::ImageType::tiff:
-                       tiffHeader = std::make_unique<TiffHeade2>();
-                       break;
-               case Exiv2::ImageType::cr2:
-                       cr2Header = std::make_unique<Cr2Header>();
-                       break;
-#if EXIV2_TEST_VERSION(0,16,0)
-               case Exiv2::ImageType::orf:
-                       tiffHeader = std::make_unique<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
-                       auto parseTree = std::make_unique<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;
-
-       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)
-               {
-               rootDir->setStart(map_data + cr2Header->offset());
-               }
-
-       TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
-
-       TiffReader reader(map_data,
-                         map_len,
-                         rootDir.get(),
-                         state);
-
-       rootDir->accept(reader);
-}
-
-RawFile::~RawFile(void)
-{
-       io_.munmap();
-       io_.close();
-}
-
-const Value * RawFile::find(uint16_t tag, uint16_t group)
-{
-       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;
-}
-
-unsigned long RawFile::preview_offset(void)
-{
-       const Value *val;
-       if (offset) return offset;
-
-       if (type == Exiv2::ImageType::cr2)
-               {
-               val = find(0x111, Group::ifd0);
-#if EXIV2_TEST_VERSION(0,28,0)
-               if (val) return val->toInt64();
-#else
-               if (val) return val->tolong();
-#endif
-               return 0;
-               }
-
-       val = find(0x201, Group::sub0_0);
-#if EXIV2_TEST_VERSION(0,28,0)
-       if (val) return val->toInt64();
-#else
-       if (val) return val->tolong();
-#endif
-
-       val = find(0x201, Group::ifd0);
-#if EXIV2_TEST_VERSION(0,28,0)
-       if (val) return val->toInt64();
-#else
-       if (val) return val->tolong();
-#endif
-
-       val = find(0x201, Group::ignr); // for PEF files, originally it was probably ifd2
-#if EXIV2_TEST_VERSION(0,28,0)
-       if (val) return val->toInt64();
-#else
-       if (val) return val->tolong();
-#endif
-
-       val = find(0x111, Group::sub0_1); // dng
-#if EXIV2_TEST_VERSION(0,28,0)
-       if (val) return val->toInt64();
-#else
-       if (val) return val->tolong();
-#endif
-
-       return 0;
-}
-
-
-#endif
-
-
-#endif
-/* HAVE_EXIV2 */
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */