From 198adc6861a9c012830c643269d73a879fcc5157 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Wed, 02 Dec 2009 17:08:31 +0000 Subject: Add sugar_cairo_surface_from_pixbuf to sugar-misc.c --- 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 +#include + +cairo_surface_t* sugar_cairo_surface_from_pixbuf (GdkPixbuf *pixbuf); + +#endif /* __SUGAR_MISC_H__ */ -- cgit v0.9.1