[gegl-gtk/pixbuf-loader] TEMP: add skeleton gdkpixbuf loader



commit b709c64e8f965a00366c5e9f7cf1112573358a8b
Author: Jon Nordby <jononor gmail com>
Date:   Fri Aug 31 23:26:03 2012 +0200

    TEMP: add skeleton gdkpixbuf loader

 Makefile.am                 |    1 +
 configure.ac                |   19 +++++
 gdkpixbufloader/Makefile.am |    9 +++
 gdkpixbufloader/io-gegl.c   |  157 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 186 insertions(+), 0 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 01a767f..954cdf5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -5,6 +5,7 @@ ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
 SUBDIRS=\
        gegl-gtk \
        operations \
+       gdkpixbufloader \
        examples \
        tests
 
diff --git a/configure.ac b/configure.ac
index 535ad48..27cbe3a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -241,6 +241,24 @@ AC_SUBST(CAIRO_GOBJECT_CFLAGS)
 AC_SUBST(CAIRO_GOBJECT_LIBS)
 
 ########################
+# Gdk Pixbuf
+########################
+PKG_CHECK_MODULES(GDKPIXBUF, glib-2.0 gmodule-2.0 gdk-pixbuf-2.0)
+
+# FIXME:
+# Ideally the pkg config file for gdk-pixbuf would have these paths set
+# There are also environment variables to take into account
+# For now, they are party hardcoded.
+AC_MSG_CHECKING(pixbufloader module directory)
+AC_SUBST(PIXBUFMODULEDIR, `pkg-config gdk-pixbuf-2.0 --variable=libdir`/gtk-2.0/2.10.0/loaders)
+AC_MSG_RESULT($PIXBUFMODULEDIR)
+
+AC_MSG_CHECKING(pixbufloader module file)
+#WRONG: target is x86_64-unknown-linux-gnu, should be x86_64-redhat-linux-gnu
+AC_SUBST(PIXBUFMODULEFILE, /etc/gtk-2.0/$target/gdk-pixbuf-loaders) 
+AC_MSG_RESULT($PIXBUFMODULEFILE)
+
+########################
 # Check GObject Introspection
 ########################
 AC_ARG_ENABLE([introspection],
@@ -348,6 +366,7 @@ AC_OUTPUT([
 Makefile
 gegl-gtk/Makefile
 operations/Makefile
+gdkpixbufloader/Makefile
 examples/Makefile
 examples/c/Makefile
 examples/vala/Makefile
diff --git a/gdkpixbufloader/Makefile.am b/gdkpixbufloader/Makefile.am
new file mode 100644
index 0000000..37573a7
--- /dev/null
+++ b/gdkpixbufloader/Makefile.am
@@ -0,0 +1,9 @@
+pixbufmoduledir = $(PIXBUFMODULEDIR)
+pixbufmodule_LTLIBRARIES = io-gegl.la
+
+io_gegl_la_SOURCES = io-gegl.c
+io_gegl_la_LDFLAGS = -export_dynamic -avoid-version -module -no-undefined
+io_gegl_la_LIBADD = $(GDKPIXBUF_LIBS) $(GEGL_LIBS)
+io_gegl_la_CPPFLAGS = $(GDKPIXBUF_CFLAGS) $(GEGL_CFLAGS)
+
+
diff --git a/gdkpixbufloader/io-gegl.c b/gdkpixbufloader/io-gegl.c
new file mode 100644
index 0000000..5188ac0
--- /dev/null
+++ b/gdkpixbufloader/io-gegl.c
@@ -0,0 +1,157 @@
+/* This file is part of GEGL-GTK
+ *
+ * GEGL-GTK is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * GEGL-GTK 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GEGL-GTK; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (C) 2012 Jon Nordby <jononor gmail com>
+ */
+
+/* References
+       http://library.gnome.org/devel/gdk-pixbuf/unstable/GdkPixbufLoader.html
+       http://library.gnome.org/devel/gdk-pixbuf/stable/gdk-pixbuf-Module-Interface.html
+*/
+
+#define GDK_PIXBUF_ENABLE_BACKEND
+
+#include <glib.h>
+#include <gmodule.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
+
+#include <gegl.h>
+
+static GQuark error_domain;
+
+
+/* FIXME: use gio instead */
+int write_to_temporary_file(FILE *infile, char* filename, GError **error) {
+       FILE *outfile = fopen(filename, "w");
+       if (!outfile) {
+               *error = g_error_new(error_domain, 1, "Could not open temporary file.");
+/*             close(filename);*/
+/*             return -1;*/
+       }
+       int i=0;
+
+       while (1) {
+               unsigned char c = fgetc(infile);
+               if (c == EOF) {
+                       break;
+               }
+               if (fputc(c, outfile) == EOF) {
+                       *error = g_error_new(error_domain, 2, "Error during write to temporary file.");
+                       close(filename);
+                       return -1;
+               }
+               i++;
+/*             if (i % 100000) printf("100K");*/
+               if (i == 0) printf("overflow");
+       }
+
+       close(filename);
+       return 0;
+}
+
+/* Load a fully rendered OpenRaster image */
+static GdkPixbuf*
+ora_image_load (FILE *f, GError **error) {
+
+#if 0
+       GdkPixbuf *pixbuf = NULL;
+       GdkColorspace colorspace = GDK_COLORSPACE_RGB;
+       gboolean has_alpha = TRUE;
+       int bits_per_sample = 8;
+       guchar *data = NULL;
+       int width = -1;
+       int height = -1;
+       int rowstride = -1;
+       ORA ora = NULL;
+       int retval = ORA_OK;
+
+       error_domain = g_quark_from_string("io-ora");
+
+       /* HACKY: write file to a temporary, on-disk file, then pass that filename
+        * to libora. This is done because libora does not provide a way to 
+        * read the document from a FILE or memory buffer. */
+       char tmp_filename[L_tmpnam];
+       tmpnam(tmp_filename);
+
+       printf("%s", tmp_filename);
+
+       retval = write_to_temporary_file(f, tmp_filename, error);
+       if (retval) {
+               // remove(tmp_filename);
+               return NULL;
+       }
+
+       retval = ora_open(tmp_filename, ORA_FILE_READ, &ora);
+       if (!ora) {
+               *error = g_error_new(error_domain, retval, "libora could not open the OpenRaster document");
+               remove(tmp_filename);
+               return NULL;
+       }
+
+       ora_get_document_size(ora, &width, &height);
+       ora_render_document(ora, &data);
+       ora_close(ora);
+       remove(tmp_filename);
+
+       /* FIXME: attach destroy function to free data */
+       rowstride = width*4;
+       pixbuf = gdk_pixbuf_new_from_data(data, colorspace, has_alpha, bits_per_sample,
+                                                                               width, height, rowstride,
+                                                                               NULL, NULL);
+
+       return pixbuf;
+       
+#endif
+    return NULL;
+}
+
+/* Register plugin */
+#define MODULE_ENTRY(function) G_MODULE_EXPORT void function
+
+MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
+{
+        module->load = ora_image_load;
+}
+
+MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
+{
+       /* FIXME: set correctly
+       OpenRaster files are ZIP archives, but have a file called mimetype STORED
+       in the beginning of the archive. So "mimetypeimage/openraster" should exist,
+       starting on the 18th byte
+       { "mimetypeimage/openraster", "", 100 }, */
+
+    static GdkPixbufModulePattern signature[] = {
+               { "PK", NULL, 80 },
+               { NULL, NULL, 0 }
+    };
+       static gchar * mime_types[] = {
+               "image/openraster",
+               NULL
+       };
+       static gchar * extensions[] = {
+               "ora",
+               NULL
+       };
+
+       info->name = "ora";
+    info->signature = signature;
+       info->description = "OpenRaster";
+       info->mime_types = mime_types;
+       info->extensions = extensions;
+       info->flags = 0;
+       info->license = "LGPL";
+}
+


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