From 0a47b1c62c929ccff2a9f803455b94d9024ac051 Mon Sep 17 00:00:00 2001 From: Alex Merry Date: Fri, 14 Dec 2012 12:26:35 +0000 Subject: Factor out pixbufFromSurface from style selectors It's silly to have two copies of the same code in two different files. --- tikzit/src/gtk/EdgeStyleSelector.m | 41 +++----------------------------------- tikzit/src/gtk/NodeStyleSelector.m | 41 +++----------------------------------- tikzit/src/gtk/gtkhelpers.h | 4 ++++ tikzit/src/gtk/gtkhelpers.m | 35 ++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 76 deletions(-) diff --git a/tikzit/src/gtk/EdgeStyleSelector.m b/tikzit/src/gtk/EdgeStyleSelector.m index 8c4e0df..6257b95 100644 --- a/tikzit/src/gtk/EdgeStyleSelector.m +++ b/tikzit/src/gtk/EdgeStyleSelector.m @@ -26,6 +26,8 @@ #import "ShapeNames.h" #import "StyleManager.h" +#import "gtkhelpers.h" + #import // {{{ Internal interfaces @@ -56,7 +58,6 @@ enum { - (void) clearModel; - (cairo_surface_t*) createEdgeIconSurface; - (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style; -- (GdkPixbuf*) pixbufFromSurface:(cairo_surface_t*)surface; - (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style usingSurface:(cairo_surface_t*)surface; - (void) addStyle:(EdgeStyle*)style; - (void) observeStyle:(EdgeStyle*)style; @@ -307,42 +308,6 @@ enum { return pixbuf; } -// Bring on GTK+3 and gdk_pixbuf_get_from_surface() -- (GdkPixbuf*) pixbufFromSurface:(cairo_surface_t*)surface { - cairo_surface_flush (surface); - - int width = cairo_image_surface_get_width (surface); - int height = cairo_image_surface_get_height (surface); - int stride = cairo_image_surface_get_stride (surface); - unsigned char *data = cairo_image_surface_get_data (surface); - - GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, - TRUE, - 8, - width, - height); - unsigned char *pbdata = gdk_pixbuf_get_pixels (pixbuf); - int pbstride = gdk_pixbuf_get_rowstride (pixbuf); - - for (int y = 0; y < height; ++y) { - uint32_t *line = (uint32_t*)(data + y*stride); - unsigned char *pbline = pbdata + (y*pbstride); - for (int x = 0; x < width; ++x) { - uint32_t pixel = *(line + x); - unsigned char *pbpixel = pbline + (x*4); - // NB: We should un-pre-mult the alpha here. - // However, in our world, alpha is always - // on or off, so it doesn't really matter - pbpixel[3] = ((pixel & 0xff000000) >> 24); - pbpixel[0] = ((pixel & 0x00ff0000) >> 16); - pbpixel[1] = ((pixel & 0x0000ff00) >> 8); - pbpixel[2] = (pixel & 0x000000ff); - } - } - - return pixbuf; -} - - (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style usingSurface:(cairo_surface_t*)surface { Transformer *transformer = [Transformer defaultTransformer]; [transformer setFlippedAboutXAxis:YES]; @@ -364,7 +329,7 @@ enum { [e renderBasicEdgeInContext:context withTransformer:transformer selected:NO]; [context release]; - return [self pixbufFromSurface:surface]; + return pixbuf_get_from_surface (surface); } - (void) addStyle:(EdgeStyle*)style usingSurface:(cairo_surface_t*)surface { diff --git a/tikzit/src/gtk/NodeStyleSelector.m b/tikzit/src/gtk/NodeStyleSelector.m index 4fd3a5e..d44f446 100644 --- a/tikzit/src/gtk/NodeStyleSelector.m +++ b/tikzit/src/gtk/NodeStyleSelector.m @@ -23,6 +23,8 @@ #import "ShapeNames.h" #import "StyleManager.h" +#import "gtkhelpers.h" + #import // {{{ Internal interfaces @@ -52,7 +54,6 @@ enum { @interface NodeStyleSelector (Private) - (cairo_surface_t*) createNodeIconSurface; - (GdkPixbuf*) pixbufOfNodeInStyle:(NodeStyle*)style; -- (GdkPixbuf*) pixbufFromSurface:(cairo_surface_t*)surface; - (GdkPixbuf*) pixbufOfNodeInStyle:(NodeStyle*)style usingSurface:(cairo_surface_t*)surface; - (void) addStyle:(NodeStyle*)style; - (void) observeStyle:(NodeStyle*)style; @@ -285,42 +286,6 @@ enum { return pixbuf; } -// Bring on GTK+3 and gdk_pixbuf_get_from_surface() -- (GdkPixbuf*) pixbufFromSurface:(cairo_surface_t*)surface { - cairo_surface_flush (surface); - - int width = cairo_image_surface_get_width (surface); - int height = cairo_image_surface_get_height (surface); - int stride = cairo_image_surface_get_stride (surface); - unsigned char *data = cairo_image_surface_get_data (surface); - - GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, - TRUE, - 8, - width, - height); - unsigned char *pbdata = gdk_pixbuf_get_pixels (pixbuf); - int pbstride = gdk_pixbuf_get_rowstride (pixbuf); - - for (int y = 0; y < height; ++y) { - uint32_t *line = (uint32_t*)(data + y*stride); - unsigned char *pbline = pbdata + (y*pbstride); - for (int x = 0; x < width; ++x) { - uint32_t pixel = *(line + x); - unsigned char *pbpixel = pbline + (x*4); - // NB: We should un-pre-mult the alpha here. - // However, in our world, alpha is always - // on or off, so it doesn't really matter - pbpixel[3] = ((pixel & 0xff000000) >> 24); - pbpixel[0] = ((pixel & 0x00ff0000) >> 16); - pbpixel[1] = ((pixel & 0x0000ff00) >> 8); - pbpixel[2] = (pixel & 0x000000ff); - } - } - - return pixbuf; -} - - (GdkPixbuf*) pixbufOfNodeInStyle:(NodeStyle*)style usingSurface:(cairo_surface_t*)surface { Shape *shape = [Shape shapeForName:[style shapeName]]; @@ -342,7 +307,7 @@ enum { andFillWithColor:[[style fillColorRGB] rColor]]; [context release]; - return [self pixbufFromSurface:surface]; + return pixbuf_get_from_surface (surface); } - (void) addStyle:(NodeStyle*)style usingSurface:(cairo_surface_t*)surface { diff --git a/tikzit/src/gtk/gtkhelpers.h b/tikzit/src/gtk/gtkhelpers.h index 418d234..a84e02b 100644 --- a/tikzit/src/gtk/gtkhelpers.h +++ b/tikzit/src/gtk/gtkhelpers.h @@ -22,6 +22,7 @@ // #import "TZFoundation.h" #include +#import void gtk_table_adjust_attach (GtkTable *table, GtkWidget *widget, @@ -46,4 +47,7 @@ gint tz_hijack_key_press (GtkWindow *win, GdkEventKey *event, gpointer user_data); +// Equivalent of GTK+3's gdk_pixbuf_get_from_surface() +GdkPixbuf * pixbuf_get_from_surface(cairo_surface_t *surface); + // vim:ft=objc:sts=2:sw=2:et diff --git a/tikzit/src/gtk/gtkhelpers.m b/tikzit/src/gtk/gtkhelpers.m index 3e4ab8a..cc21622 100644 --- a/tikzit/src/gtk/gtkhelpers.m +++ b/tikzit/src/gtk/gtkhelpers.m @@ -239,4 +239,39 @@ tz_hijack_key_press (GtkWindow *win, return FALSE; } +GdkPixbuf * pixbuf_get_from_surface(cairo_surface_t *surface) { + cairo_surface_flush (surface); + + int width = cairo_image_surface_get_width (surface); + int height = cairo_image_surface_get_height (surface); + int stride = cairo_image_surface_get_stride (surface); + unsigned char *data = cairo_image_surface_get_data (surface); + + GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, + TRUE, + 8, + width, + height); + unsigned char *pbdata = gdk_pixbuf_get_pixels (pixbuf); + int pbstride = gdk_pixbuf_get_rowstride (pixbuf); + + for (int y = 0; y < height; ++y) { + uint32_t *line = (uint32_t*)(data + y*stride); + unsigned char *pbline = pbdata + (y*pbstride); + for (int x = 0; x < width; ++x) { + uint32_t pixel = *(line + x); + unsigned char *pbpixel = pbline + (x*4); + // NB: We should un-pre-mult the alpha here. + // However, in our world, alpha is always + // on or off, so it doesn't really matter + pbpixel[3] = ((pixel & 0xff000000) >> 24); + pbpixel[0] = ((pixel & 0x00ff0000) >> 16); + pbpixel[1] = ((pixel & 0x0000ff00) >> 8); + pbpixel[2] = (pixel & 0x000000ff); + } + } + + return pixbuf; +} + // vim:ft=objc:ts=8:et:sts=4:sw=4 -- cgit v1.2.3