Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomeu Vizoso <tomeu@sugarlabs.org>2009-12-02 17:08:31 (GMT)
committer Tomeu Vizoso <tomeu.vizoso@collabora.co.uk>2010-08-18 18:08:32 (GMT)
commit198adc6861a9c012830c643269d73a879fcc5157 (patch)
treebf6db9958f02da71a4309529eb966878368bfb20
parente83f0907beafd1dbd721c3f732b54235a53c8191 (diff)
Add sugar_cairo_surface_from_pixbuf to sugar-misc.c
-rw-r--r--src/sugar/Makefile.am3
-rw-r--r--src/sugar/sugar-misc.c107
-rw-r--r--src/sugar/sugar-misc.h28
3 files changed, 138 insertions, 0 deletions
diff --git a/src/sugar/Makefile.am b/src/sugar/Makefile.am
index 75bf407..47ae2a1 100644
--- a/src/sugar/Makefile.am
+++ b/src/sugar/Makefile.am
@@ -24,6 +24,7 @@ public_headers = \
sugar-key-grabber.h \
sugar-menu.h \
sugar-grid.h \
+ sugar-misc.h \
sexy-icon-entry.h \
eggsmclient.h \
acme-volume.h \
@@ -55,6 +56,7 @@ libsugarext_la_SOURCES = \
sugar-address-entry.c \
sugar-grid.c \
sugar-key-grabber.c \
+ sugar-misc.c \
sugar-menu.c
BUILT_SOURCES = \
@@ -72,6 +74,7 @@ sugar-marshal.h: sugar-marshal.list
SugarExt-1.0.gir: $(INTROSPECTION_SCANNER) libsugarext.la
$(INTROSPECTION_SCANNER) -v \
--namespace SugarExt --nsversion=1.0 \
+ --strip-prefix=sugar \
--include=Gtk-2.0 \
--library=libsugarext.la \
--library asound \
diff --git a/src/sugar/sugar-misc.c b/src/sugar/sugar-misc.c
new file mode 100644
index 0000000..1eb1b7e
--- /dev/null
+++ b/src/sugar/sugar-misc.c
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2009 Tomeu Vizoso
+ *
+ * This library 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 2 of the License, or (at your option) any later version.
+ *
+ * This library 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 this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "sugar-misc.h"
+
+cairo_surface_t*
+sugar_cairo_surface_from_pixbuf (GdkPixbuf *pixbuf)
+{
+/* Ripped from GooCanvas */
+ gint width = gdk_pixbuf_get_width (pixbuf);
+ gint height = gdk_pixbuf_get_height (pixbuf);
+ guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
+ int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
+ int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
+ guchar *cairo_pixels;
+ cairo_format_t format;
+ cairo_surface_t *surface;
+ static const cairo_user_data_key_t key;
+ int j;
+
+ if (n_channels == 3)
+ format = CAIRO_FORMAT_RGB24;
+ else
+ format = CAIRO_FORMAT_ARGB32;
+
+ cairo_pixels = g_malloc (4 * width * height);
+ surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
+ format,
+ width, height, 4 * width);
+ cairo_surface_set_user_data (surface, &key,
+ cairo_pixels, (cairo_destroy_func_t)g_free);
+
+ for (j = height; j; j--)
+ {
+ guchar *p = gdk_pixels;
+ guchar *q = cairo_pixels;
+
+ if (n_channels == 3)
+ {
+ guchar *end = p + 3 * width;
+
+ while (p < end)
+ {
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
+ q[0] = p[2];
+ q[1] = p[1];
+ q[2] = p[0];
+#else
+ q[1] = p[0];
+ q[2] = p[1];
+ q[3] = p[2];
+#endif
+ p += 3;
+ q += 4;
+ }
+ }
+ else
+ {
+ guchar *end = p + 4 * width;
+ guint t1,t2,t3;
+
+#define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
+
+ while (p < end)
+ {
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
+ MULT(q[0], p[2], p[3], t1);
+ MULT(q[1], p[1], p[3], t2);
+ MULT(q[2], p[0], p[3], t3);
+ q[3] = p[3];
+#else
+ q[0] = p[3];
+ MULT(q[1], p[0], p[3], t1);
+ MULT(q[2], p[1], p[3], t2);
+ MULT(q[3], p[2], p[3], t3);
+#endif
+
+ p += 4;
+ q += 4;
+ }
+
+#undef MULT
+ }
+
+ gdk_pixels += gdk_rowstride;
+ cairo_pixels += 4 * width;
+ }
+
+ return surface;
+}
+
diff --git a/src/sugar/sugar-misc.h b/src/sugar/sugar-misc.h
new file mode 100644
index 0000000..3d08fc7
--- /dev/null
+++ b/src/sugar/sugar-misc.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2009 Tomeu Vizoso
+ *
+ * This library 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 2 of the License, or (at your option) any later version.
+ *
+ * This library 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 this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __SUGAR_MISC_H__
+#define __SUGAR_MISC_H__
+
+#include <gdk/gdk.h>
+#include <cairo.h>
+
+cairo_surface_t* sugar_cairo_surface_from_pixbuf (GdkPixbuf *pixbuf);
+
+#endif /* __SUGAR_MISC_H__ */