__author__ = 'Joel Wright'

import pygtk
pygtk.require('2.0')
import gtk
import math
import cairo
import struct

class DrawingAreaExample(gtk.Window):
    def __init__(self):
        super(DrawingAreaExample, self).__init__()

        self.set_title("Drawing Area Example")
        self.resize(300,400)
        self.set_position(gtk.WIN_POS_CENTER)
        self.connect("destroy", gtk.main_quit)

        self.surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 300, 300)
        self.draw_to_surface()

        area = gtk.DrawingArea()
        area.set_size_request(300, 300)
        area.connect("expose-event", self.expose)

        show_button = gtk.Button("dump")
        show_button.connect("clicked", self.dump_pixmap)

        fixed = gtk.Fixed()
        fixed.put(area, 0, 0)
        fixed.put(show_button, 20, 320)

        self.add(fixed)

        self.show_all()

    def expose(self, widget, event):

        cr = widget.window.cairo_create()
        cr.set_source_surface(self.surface, 0, 0)
        cr.paint()

    def draw_to_surface(self):

        cr = cairo.Context(self.surface)

        cr.set_line_width(9)
        cr.set_source_rgb(0.7, 0.2, 0.0)

        w = self.surface.get_width()
        h = self.surface.get_height()

        cr.translate(w/2, h/2)
        cr.arc(0, 0, 50, 0, 2*math.pi)
        cr.stroke_preserve()

        cr.set_source_rgb(0.3, 0.4, 0.6)
        cr.fill()

    def dump_pixmap(self, widget):
        d = self.surface.get_data()
        self.print_buffer(d)

    def print_buffer(self, data):
        coord = 0
        while coord < len(data):
            b = struct.unpack('B',data[coord])
            g = struct.unpack('B',data[coord+1])
            r = struct.unpack('B',data[coord+2])
            coord += 4
            print '(%d,%d,%d)' % (r[0], g[0], b[0])


DrawingAreaExample()
gtk.main()
@@ -1544,17 +1544,92 @@
     
-coord = 0
+s = %22%22%0A        coord = 0%0A        row = 1%0A        stride = self.surface.get_stride()%0A
 %0A   
@@ -1838,21 +1838,163 @@
         
-print
+if (coord / row) %3E stride:%0A                row += 1%0A                s += '(%25d,%25d,%25d)%5Cn' %25 (r%5B0%5D, g%5B0%5D, b%5B0%5D)%0A            else:%0A                s +=
  '(%25d,%25d
@@ -1997,16 +1997,17 @@
 d,%25d,%25d)
+ 
 ' %25 (r%5B0
@@ -2022,16 +2022,33 @@
  b%5B0%5D)%0A%0A
+        print s%0A%0A
 %0ADrawing

Improve output of pixel data.