[niepce] importer: provision getting the orientaiton of the preview with gphoto2



commit 0a164c5196bfab98fbb38d725e758fef070f3bb8
Author: Hubert Figuière <hub figuiere net>
Date:   Mon May 29 21:01:57 2017 -0400

    importer: provision getting the orientaiton of the preview with gphoto2

 src/engine/importer/cameraimporter.cpp |    9 +-----
 src/fwk/toolkit/gphoto.cpp             |   44 +++++++++++++++++++++++++++++--
 src/fwk/toolkit/gphoto.hpp             |   10 +++---
 src/fwk/utils/exempi.cpp               |    1 -
 src/fwk/utils/exempi.hpp               |    2 +-
 src/fwk/utils/files.cpp                |   19 +++++++++++++-
 src/fwk/utils/files.hpp                |    9 +++---
 7 files changed, 72 insertions(+), 22 deletions(-)
---
diff --git a/src/engine/importer/cameraimporter.cpp b/src/engine/importer/cameraimporter.cpp
index da18e2e..077f767 100644
--- a/src/engine/importer/cameraimporter.cpp
+++ b/src/engine/importer/cameraimporter.cpp
@@ -106,13 +106,8 @@ bool CameraImporter::do_import(const std::string & source,
 {
     // XXX we shouldn't have to do that.
     list_source_content(source, [this, importer] (auto file_list) {
-            char* tmp_dir = g_dir_make_tmp("niepce-camera-import-XXXXXX", nullptr);
-            if (!tmp_dir) {
-                return false;
-            }
-            std::string tmp_dir_path = tmp_dir;
-            g_free(tmp_dir);
-            tmp_dir = nullptr;
+            auto tmp_dir_path = fwk::make_tmp_dir("niepce-camera-import-XXXXXX");
+            // XXX check we don't return an empty string.
 
             for (auto file: file_list) {
                 auto imported_camera_file =
diff --git a/src/fwk/toolkit/gphoto.cpp b/src/fwk/toolkit/gphoto.cpp
index 1fbb702..6bdf5f8 100644
--- a/src/fwk/toolkit/gphoto.cpp
+++ b/src/fwk/toolkit/gphoto.cpp
@@ -28,10 +28,15 @@
 #include <gphoto2-context.h>
 #include <gphoto2-port-result.h>
 
+#include <glibmm/miscutils.h>
+
 #include <gdkmm/pixbufloader.h>
 
 #include "fwk/base/debug.hpp"
+#include "fwk/utils/exempi.hpp"
+#include "fwk/utils/files.hpp"
 #include "fwk/utils/pathutils.hpp"
+#include "fwk/toolkit/gdkutils.hpp"
 #include "fwk/toolkit/thumbnail.hpp"
 #include "gphoto.hpp"
 
@@ -186,6 +191,11 @@ bool GpCamera::open()
     if (m_priv->camera) {
         close();
     }
+
+    if (m_temp_dir_path.empty()) {
+        m_temp_dir_path = fwk::make_tmp_dir("niepce-gphoto-XXXXXX");
+    }
+
     gp_camera_new(&m_priv->camera);
 
     CameraAbilities abilities;
@@ -285,10 +295,9 @@ fwk::Thumbnail GpCamera::get_preview(const std::string& path) const
     std::string name = fwk::path_basename(path);
 
     gp::CameraFilePtr file = gp::file_new();
-    int result = gp_camera_file_get (m_priv->camera, folder.c_str(), name.c_str(),
+    int result = gp_camera_file_get(m_priv->camera, folder.c_str(), name.c_str(),
                                      GP_FILE_TYPE_PREVIEW, file.get(),
                                      m_priv->context);
-    DBG_OUT("file_get %s %d", path.c_str(), result);
     if (result >= 0) {
         const char *fd;
         unsigned long fs;
@@ -297,7 +306,36 @@ fwk::Thumbnail GpCamera::get_preview(const std::string& path) const
         Glib::RefPtr<Gdk::PixbufLoader> loader = Gdk::PixbufLoader::create();
         loader->write(reinterpret_cast<const guint8*>(fd), fs);
         loader->close();
-        thumbnail = fwk::Thumbnail(loader->get_pixbuf());
+
+        auto pix = loader->get_pixbuf();
+#if 0
+        result = gp_camera_file_get(m_priv->camera, folder.c_str(), name.c_str(),
+                                    GP_FILE_TYPE_EXIF, file.get(),
+                                    m_priv->context);
+        if (result >= 0) {
+            const char *exifdata;
+            unsigned long exifsize;
+            int32_t orientation = 0;
+
+            gp_file_get_data_and_size(file.get(), &exifdata, &exifsize);
+            std::string exif_path = Glib::build_filename(m_temp_dir_path, name);
+            FILE* exif_file = fopen(exif_path.c_str(), "w");
+            fwrite(exifdata + 10, 1, exifsize - 10, exif_file);
+            fclose(exif_file);
+
+            DBG_OUT("exif block size %lu", exifsize);
+            fwk::XmpMeta xmp(exif_path, false);
+            DBG_OUT("xmp open exif %s", exif_path.c_str());
+            if (xmp.isOk()) {
+                orientation = xmp.orientation();
+            } else {
+                ERR_OUT("xmp is not ok");
+            }
+            pix = fwk::gdkpixbuf_exif_rotate(pix, orientation);
+            unlink(exif_path.c_str());
+        }
+#endif
+        thumbnail = fwk::Thumbnail(pix);
     }
 
     return thumbnail;
diff --git a/src/fwk/toolkit/gphoto.hpp b/src/fwk/toolkit/gphoto.hpp
index 7e3d6d9..b50d70e 100644
--- a/src/fwk/toolkit/gphoto.hpp
+++ b/src/fwk/toolkit/gphoto.hpp
@@ -37,14 +37,13 @@ namespace fwk {
 class Thumbnail;
 
 namespace gp {
+typedef std::unique_ptr<CameraFile, decltype(&gp_file_unref)> CameraFilePtr;
 
-    typedef std::unique_ptr<CameraFile, decltype(&gp_file_unref)> CameraFilePtr;
+CameraFilePtr file_new();
 
-    CameraFilePtr file_new();
+typedef std::unique_ptr<CameraList, decltype(&gp_list_unref)> CameraListPtr;
 
-    typedef std::unique_ptr<CameraList, decltype(&gp_list_unref)> CameraListPtr;
-
-    CameraListPtr list_new();
+CameraListPtr list_new();
 }
 /** Describe a gphoto device: model + port (path)
  */
@@ -119,6 +118,7 @@ private:
                          std::list<std::pair<std::string, std::string>>& files) const;
     class Priv;
     GpDevicePtr m_device;
+    std::string m_temp_dir_path;
     Priv* m_priv;
 };
 
diff --git a/src/fwk/utils/exempi.cpp b/src/fwk/utils/exempi.cpp
index 6573898..608b7e8 100644
--- a/src/fwk/utils/exempi.cpp
+++ b/src/fwk/utils/exempi.cpp
@@ -78,7 +78,6 @@ XmpMeta::XmpMeta()
     m_xmp = xmp_new_empty();
 }
 
-
 /** @param file the path to the file to open
  * @param sidecar_only we only want the sidecar.
  * It will locate the XMP sidecar for the file.
diff --git a/src/fwk/utils/exempi.hpp b/src/fwk/utils/exempi.hpp
index 7e4ac12..8a7385c 100644
--- a/src/fwk/utils/exempi.hpp
+++ b/src/fwk/utils/exempi.hpp
@@ -108,7 +108,7 @@ public:
     NON_COPYABLE(XmpMeta);
 
     XmpMeta();
-    XmpMeta(const std::string & for_file, bool sidecar_only);
+    XmpMeta(const std::string& for_file, bool sidecar_only);
     virtual ~XmpMeta();
 
     bool isOk() const
diff --git a/src/fwk/utils/files.cpp b/src/fwk/utils/files.cpp
index 6a26247..878e50a 100644
--- a/src/fwk/utils/files.cpp
+++ b/src/fwk/utils/files.cpp
@@ -1,7 +1,8 @@
+/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode:nil; -*- */
 /*
  * niepce - fwk/utils/files.cpp
  *
- * Copyright (C) 2007-2013 Hubert Figuiere
+ * Copyright (C) 2007-2017 Hubert Figuiere
  *
  * 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
@@ -29,6 +30,22 @@
 
 namespace fwk {
 
+std::string make_tmp_dir(const std::string& base)
+{
+    GError *err = nullptr;
+    char* tmp_dir = g_dir_make_tmp(base.c_str(), &err);
+    if (!tmp_dir) {
+        if (err) {
+            ERR_OUT("g_dir_mak_tmp(%s) failed: %s", base.c_str(), err->message);
+            g_error_free(err);
+        }
+        return "";
+    }
+    std::string tmp_dir_path = tmp_dir;
+    g_free(tmp_dir);
+    return tmp_dir_path;
+}
+
        bool filter_none(const Glib::RefPtr<Gio::FileInfo> & )
        {
                return true;
diff --git a/src/fwk/utils/files.hpp b/src/fwk/utils/files.hpp
index aa33469..fc6c901 100644
--- a/src/fwk/utils/files.hpp
+++ b/src/fwk/utils/files.hpp
@@ -1,7 +1,8 @@
+/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode:nil; -*- */
 /*
  * niepce - fwk/utils/files.hpp
  *
- * Copyright (C) 2007-2013 Hubert Figuiere
+ * Copyright (C) 2007-2017 Hubert Figuiere
  *
  * 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
@@ -17,9 +18,6 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-
-
-
 #ifndef __UTILS_FILES_H__
 #define __UTILS_FILES_H__
 
@@ -33,6 +31,9 @@
 
 namespace fwk {
 
+/** wrapper around g_dir_make_tmp() */
+std::string make_tmp_dir(const std::string& base);
+
        bool filter_none(const Glib::RefPtr<Gio::FileInfo> & file);
   bool filter_ext(const Glib::RefPtr<Gio::FileInfo> & file,
                  const std::string & ext);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]