[niepce] Remove dependency on libopenraw-gnome



commit 49ce506349fc099916127fb6e3faa3c42e9a62d3
Author: Hubert Figuière <hub figuiere net>
Date:   Sat May 11 19:47:42 2019 -0400

    Remove dependency on libopenraw-gnome

 configure.ac                  |   2 +-
 src/fwk/toolkit/thumbnail.cpp | 157 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 154 insertions(+), 5 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 59743f3..1ccc5e0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -65,7 +65,7 @@ PKG_CHECK_MODULES(GPHOTO, [libgphoto2 >= $LIBGPHOTO_VERSION libgphoto2_port])
 
 dnl niepce
 PKG_CHECK_MODULES(BABL, babl)
-PKG_CHECK_MODULES(OPENRAW, libopenraw-gnome-0.1 >= $LIBOPENRAW_VERSION)
+PKG_CHECK_MODULES(OPENRAW, libopenraw-0.1 >= $LIBOPENRAW_VERSION)
 AC_SUBST(OPENRAW_CFLAGS)
 AC_SUBST(OPENRAW_LIBS)
 
diff --git a/src/fwk/toolkit/thumbnail.cpp b/src/fwk/toolkit/thumbnail.cpp
index b6199b3..8ff1f0d 100644
--- a/src/fwk/toolkit/thumbnail.cpp
+++ b/src/fwk/toolkit/thumbnail.cpp
@@ -2,7 +2,7 @@
 /*
  * niepce - fwk/toolkit/thumbnail.cpp
  *
- * Copyright (C) 2017 Hubert Figuière
+ * Copyright (C) 2017-2019 Hubert Figuière
  *
  * 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
@@ -21,7 +21,7 @@
 #include "thumbnail.hpp"
 
 #include <glibmm/miscutils.h>
-#include <libopenraw-gnome/gdkpixbuf.h>
+#include <libopenraw/libopenraw.h>
 
 #include "fwk/base/debug.hpp"
 #include "fwk/toolkit/mimetype.hpp"
@@ -30,6 +30,155 @@
 
 namespace fwk {
 
+namespace {
+
+static void pixbuf_free(guchar* data, gpointer u)
+{
+    (void)u;
+    free(data);
+}
+
+/**
+ * Returns a retained GdkPixbuf
+ */
+static GdkPixbuf* rotate_pixbuf(GdkPixbuf* tmp, int32_t orientation)
+{
+    GdkPixbuf* pixbuf = nullptr;
+    switch (orientation) {
+    case 0:
+    case 1:
+        pixbuf = (GdkPixbuf*)g_object_ref(tmp);
+        break;
+    case 2:
+        pixbuf = gdk_pixbuf_flip(tmp, TRUE);
+        break;
+    case 3:
+        pixbuf = gdk_pixbuf_rotate_simple(tmp, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
+        break;
+    case 4: {
+        GdkPixbuf* rotated =
+            gdk_pixbuf_rotate_simple(tmp, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
+        pixbuf = gdk_pixbuf_flip(rotated, TRUE);
+        g_object_unref(rotated);
+        break;
+    }
+    case 5: {
+        GdkPixbuf* rotated =
+            gdk_pixbuf_rotate_simple(tmp, GDK_PIXBUF_ROTATE_CLOCKWISE);
+        pixbuf = gdk_pixbuf_flip(rotated, FALSE);
+        g_object_unref(rotated);
+        break;
+    }
+    case 6:
+        pixbuf = gdk_pixbuf_rotate_simple(tmp, GDK_PIXBUF_ROTATE_CLOCKWISE);
+        break;
+    case 7: {
+        GdkPixbuf* rotated =
+            gdk_pixbuf_rotate_simple(tmp, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
+        pixbuf = gdk_pixbuf_flip(rotated, FALSE);
+        g_object_unref(rotated);
+        break;
+    }
+    case 8:
+        pixbuf =
+            gdk_pixbuf_rotate_simple(tmp, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
+        break;
+    default:
+        break;
+    }
+    return pixbuf;
+}
+
+static GdkPixbuf* thumbnail_to_pixbuf(ORThumbnailRef thumbnail,
+                                      int32_t orientation)
+{
+    GdkPixbuf* tmp = nullptr;
+    GdkPixbuf* pixbuf = nullptr;
+
+    const guchar* buf;
+    or_data_type format = or_thumbnail_format(thumbnail);
+    buf = (const guchar*)or_thumbnail_data(thumbnail);
+
+    switch (format) {
+    case OR_DATA_TYPE_PIXMAP_8RGB: {
+        uint32_t x, y;
+        size_t buf_size;
+        guchar* data;
+
+        buf_size = or_thumbnail_data_size(thumbnail);
+        data = (guchar*)malloc(buf_size);
+        memcpy(data, buf, buf_size);
+        or_thumbnail_dimensions(thumbnail, &x, &y);
+
+        tmp = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, FALSE, 8, x, y,
+                                       x * 3, pixbuf_free, nullptr);
+        break;
+    }
+    case OR_DATA_TYPE_JPEG:
+    case OR_DATA_TYPE_TIFF:
+    case OR_DATA_TYPE_PNG: {
+        GdkPixbufLoader* loader = nullptr;
+        size_t count = or_thumbnail_data_size(thumbnail);
+        loader = gdk_pixbuf_loader_new();
+        if (loader != nullptr) {
+            GError* error = nullptr;
+            if (!gdk_pixbuf_loader_write(loader, buf, count, &error) && error) {
+                ERR_OUT("loader write error: %s", error->message);
+                g_error_free(error);
+                error = nullptr;
+            }
+            if (!gdk_pixbuf_loader_close(loader, &error) && error) {
+                ERR_OUT("loader close error: %s", error->message);
+                g_error_free(error);
+            }
+            tmp = gdk_pixbuf_loader_get_pixbuf(loader);
+            g_object_ref(tmp);
+            g_object_unref(loader);
+        }
+        break;
+    }
+    default:
+        break;
+    }
+    pixbuf = rotate_pixbuf(tmp, orientation);
+    g_object_unref(tmp);
+    return pixbuf;
+}
+
+static GdkPixbuf* gdkpixbuf_extract_rotated_thumbnail(const char* path,
+                                                      uint32_t preferred_size)
+{
+    ORRawFileRef rf;
+    int32_t orientation = 0;
+    GdkPixbuf* pixbuf = nullptr;
+    or_error err = OR_ERROR_NONE;
+    ORThumbnailRef thumbnail = nullptr;
+    gboolean rotate = TRUE;
+
+    rf = or_rawfile_new(path, OR_RAWFILE_TYPE_UNKNOWN);
+    if (rf) {
+        if (rotate) {
+            orientation = or_rawfile_get_orientation(rf);
+        }
+        thumbnail = or_thumbnail_new();
+        err = or_rawfile_get_thumbnail(rf, preferred_size, thumbnail);
+        if (err == OR_ERROR_NONE) {
+            pixbuf = thumbnail_to_pixbuf(thumbnail, orientation);
+        } else {
+            ERR_OUT("or_get_extract_thumbnail() failed with %d.", err);
+        }
+        err = or_thumbnail_release(thumbnail);
+        if (err != OR_ERROR_NONE) {
+            ERR_OUT("or_thumbnail_release() failed with %d", err);
+        }
+        or_rawfile_release(rf);
+    }
+
+    return pixbuf;
+}
+
+}
+
 Thumbnail::Thumbnail(const Glib::RefPtr<Gdk::Pixbuf>& pixbuf)
     : m_pixbuf(pixbuf)
 {
@@ -78,8 +227,8 @@ Thumbnail Thumbnail::thumbnail_file(const std::string& filename,
             ERR_OUT("exception thumbnailing image %s", e.what().c_str());
         }
     } else {
-        GdkPixbuf *pixbuf = or_gdkpixbuf_extract_rotated_thumbnail(filename.c_str(),
-                                                                   std::min(w, h));
+        GdkPixbuf *pixbuf = gdkpixbuf_extract_rotated_thumbnail(filename.c_str(),
+                                                                std::min(w, h));
         if(pixbuf) {
             pix = Glib::wrap(pixbuf, true); // take ownership
             if((w < pix->get_width()) || (h < pix->get_height())) {


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