From 79f5678feeb16910c27ac6c24e93ca0f8b479c8d Mon Sep 17 00:00:00 2001 From: C. Scott Ananian Date: Thu, 10 Nov 2011 23:33:09 +0000 Subject: Fix 'read pixel' operation under GTK3 (now faster in GTK2 as well). --- diff --git a/TurtleArt/tacanvas.py b/TurtleArt/tacanvas.py index ac20092..8b294a9 100644 --- a/TurtleArt/tacanvas.py +++ b/TurtleArt/tacanvas.py @@ -700,16 +700,12 @@ class TurtleGraphics: h = self.tw.turtle_canvas.get_height() if x < 0 or x > (w - 1) or y < 0 or y > (h - 1): return(-1, -1, -1, -1) - # Map the cairo surface onto a pixmap - pixmap = gtk.gdk.Pixmap(None, w, h, 24) - cr = pixmap.cairo_create() - cr.set_source_surface(self.tw.turtle_canvas, 0, 0) - cr.paint() + # Map the cairo surface onto a pixbuf + pixbuf = Gdk.pixbuf_get_from_surface(self.tw.turtle_canvas, + x, y, 1, 1) # Read the pixel - pixel = pixmap.get_image(x, y, 1, 1).get_pixel(0, 0) - return(int((pixel & 0xFF0000) >> 16), - int((pixel & 0x00FF00) >> 8), - int((pixel & 0x0000FF) >> 0), 0) + pixels = pixbuf.get_pixels() + return (ord(pixels[0]), ord(pixels[1]), ord(pixels[2]), 0) else: return(-1, -1, -1, -1) diff --git a/util/gtkcompat.py b/util/gtkcompat.py index c188a13..87622b8 100644 --- a/util/gtkcompat.py +++ b/util/gtkcompat.py @@ -29,6 +29,7 @@ except ValueError, ImportError: import gconf import pango as Pango import pangocairo as PangoCairo + import cairo Gdk = Gtk.gdk Rectangle = Gdk.Rectangle @@ -42,6 +43,16 @@ except ValueError, ImportError: Gdk.cairo_set_source_pixbuf = \ lambda cr, img, x, y: cr.set_source_pixbuf(img, x, y) + def gdk_pixbuf_get_from_surface(surface, x, y, w, h): + pixmap = Gdk.Pixmap(None, w, h, 24) + cr = pixmap.cairo_create() + cr.set_source_surface(surface, -x, -y) + cr.rectangle(0,0,w,h); + cr.set_operator(cairo.OPERATOR_SOURCE) + cr.fill() + cmap = Gdk.colormap_get_system() + return Gdk.pixbuf_get_from_drawable(None, pixmap, cmap, 0, 0, 0, 0, w, h) + Gdk.pixbuf_get_from_surface = gdk_pixbuf_get_from_surface class GdkEventMask: EXPOSURE_MASK = Gdk.EXPOSURE_MASK -- cgit v0.9.1