summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2011-07-02 13:53:10 +0100
committerChris Wilson <chris@chris-wilson.co.uk>2011-07-02 13:53:10 +0100
commit3e9ed76f78cbe62193adf1fc5dd6b3a165ca8a91 (patch)
tree4b338131de68b04dccddff0eb3a98e08dda5f70e
parent42c4abee1677ed1109ac4c5f02de731ecda5b696 (diff)
Share the common gdk-pixbuf to cairo_image_surface routine
-rw-r--r--demo.c78
-rw-r--r--demo.h7
-rw-r--r--slideshow-demo.c79
-rw-r--r--spiral-demo.c79
4 files changed, 84 insertions, 159 deletions
diff --git a/demo.c b/demo.c
index f69babe..9bbaedf 100644
--- a/demo.c
+++ b/demo.c
@@ -72,3 +72,81 @@ struct device *device_open(int argc, char **argv)
return device;
}
+cairo_surface_t *
+_cairo_image_surface_create_from_pixbuf(const GdkPixbuf *pixbuf)
+{
+ 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);
+ int cairo_stride;
+ guchar *cairo_pixels;
+ cairo_format_t format;
+ cairo_surface_t *surface;
+ int j;
+
+ if (n_channels == 3)
+ format = CAIRO_FORMAT_RGB24;
+ else
+ format = CAIRO_FORMAT_ARGB32;
+
+ surface = cairo_image_surface_create(format, width, height);
+ if (cairo_surface_status(surface))
+ return surface;
+
+ cairo_stride = cairo_image_surface_get_stride (surface);
+ cairo_pixels = cairo_image_surface_get_data(surface);
+
+ 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 + 0x7f; 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 += cairo_stride;
+ }
+
+ return surface;
+}
+
diff --git a/demo.h b/demo.h
index aecba36..48707ce 100644
--- a/demo.h
+++ b/demo.h
@@ -1,6 +1,8 @@
-#include <cairo.h>
#include <stdint.h>
+#include <cairo.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
+
struct device;
struct framebuffer;
struct slide;
@@ -56,3 +58,6 @@ struct device *drm_open (int argc, char **argv);
#else
static inline struct device *drm_open (int argc, char **argv) { return 0; }
#endif
+
+cairo_surface_t *
+_cairo_image_surface_create_from_pixbuf(const GdkPixbuf *pixbuf);
diff --git a/slideshow-demo.c b/slideshow-demo.c
index d5746d3..1689d6e 100644
--- a/slideshow-demo.c
+++ b/slideshow-demo.c
@@ -7,7 +7,6 @@
#include <string.h>
#include <math.h>
-#include <gdk-pixbuf/gdk-pixbuf.h>
#include "demo.h"
static struct source {
@@ -60,84 +59,6 @@ fps_draw (cairo_t *cr, const char *name,
cairo_show_text (cr, buf);
}
-static cairo_surface_t *
-_cairo_image_surface_create_from_pixbuf(const GdkPixbuf *pixbuf)
-{
- 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);
- int cairo_stride;
- guchar *cairo_pixels;
- cairo_format_t format;
- cairo_surface_t *surface;
- int j;
-
- if (n_channels == 3)
- format = CAIRO_FORMAT_RGB24;
- else
- format = CAIRO_FORMAT_ARGB32;
-
- surface = cairo_image_surface_create(format, width, height);
- if (cairo_surface_status(surface))
- return surface;
-
- cairo_stride = cairo_image_surface_get_stride (surface);
- cairo_pixels = cairo_image_surface_get_data(surface);
-
- 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 + 0x7f; 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 += cairo_stride;
- }
-
- return surface;
-}
-
static int load_sources_file(const char *filename,
struct device *device,
cairo_surface_t *target)
diff --git a/spiral-demo.c b/spiral-demo.c
index 8fe7ff9..b6977c6 100644
--- a/spiral-demo.c
+++ b/spiral-demo.c
@@ -7,7 +7,6 @@
#include <string.h>
#include <math.h>
-#include <gdk-pixbuf/gdk-pixbuf.h>
#include "demo.h"
static struct source{
@@ -59,84 +58,6 @@ fps_draw (cairo_t *cr, const char *name,
cairo_show_text (cr, buf);
}
-static cairo_surface_t *
-_cairo_image_surface_create_from_pixbuf(const GdkPixbuf *pixbuf)
-{
- 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);
- int cairo_stride;
- guchar *cairo_pixels;
- cairo_format_t format;
- cairo_surface_t *surface;
- int j;
-
- if (n_channels == 3)
- format = CAIRO_FORMAT_RGB24;
- else
- format = CAIRO_FORMAT_ARGB32;
-
- surface = cairo_image_surface_create(format, width, height);
- if (cairo_surface_status(surface))
- return surface;
-
- cairo_stride = cairo_image_surface_get_stride (surface);
- cairo_pixels = cairo_image_surface_get_data(surface);
-
- 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 + 0x7f; 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 += cairo_stride;
- }
-
- return surface;
-}
-
static int load_sources_file(const char *filename, cairo_surface_t *target)
{
GdkPixbuf *pb;