[cairo-commit] pycairo/test isurface_get_data.py,1.1,1.2

Steve Chaplin commit at pdx.freedesktop.org
Sun Nov 26 17:17:46 PST 2006


Committed by: stevech1097

Update of /cvs/cairo/pycairo/test
In directory kemper:/tmp/cvs-serv11288/test

Modified Files:
	isurface_get_data.py 
Log Message:
'SC'

Index: isurface_get_data.py
===================================================================
RCS file: /cvs/cairo/pycairo/test/isurface_get_data.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- isurface_get_data.py	20 Nov 2006 11:21:00 -0000	1.1
+++ isurface_get_data.py	27 Nov 2006 01:17:42 -0000	1.2
@@ -4,43 +4,41 @@
 """
 
 import cairo
+import numpy
 
 filename_base = "/tmp/file"
+w, h = 128, 128
 
-# create an test image and save as PNG
-width, height = 300, 200
-surface1 = cairo.ImageSurface (cairo.FORMAT_ARGB32, width, height)
-ctx = cairo.Context (surface1)
-ctx.scale (width, height)
+surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, w, h)
+ctx = cairo.Context (surface)
 
-ctx.set_source_rgb (1, 1, 1)
+ctx.set_source_rgb (1, 1, 1)  # white
 ctx.set_operator (cairo.OPERATOR_SOURCE)
 ctx.paint()
 
-ctx.move_to (0.0, 0.0)
-ctx.line_to (1.0, 1.0)
-ctx.set_line_width (0.05)
-ctx.set_source_rgb (0, 0, 1)
-ctx.stroke()
-
-ctx.select_font_face ("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
-ctx.set_font_size (0.08)
-ctx.move_to (0.05, 0.5)
-ctx.set_source_rgb (0.0, 0.0, 0.0)
-ctx.show_text ("get_data test")
-surface1.write_to_png (filename_base + "get_data_test1.png")
+# Draw out the triangle using absolute coordinates
+ctx.move_to (w/2, h/3)
+ctx.line_to (2*w/3, 2*h/3)
+ctx.rel_line_to (-1*w/3, 0)
+ctx.close_path()
 
-# get data from the Surface and use to create a new Surface
-buf = surface1.get_data()
+ctx.set_source_rgb (0, 0, 0)  # black
+ctx.set_line_width(15)
+ctx.stroke()
+surface.write_to_png (filename_base + "get_data_test1.png")
 
+# modify surface using numpy
+buf = surface.get_data()
 # alternative which should work (?) but reports
 # TypeError: buffer is read-only
 # - is a Python bug?
 #buf = buffer (surface1)
 
-surface2 = cairo.ImageSurface.create_for_data (
-    buf, cairo.FORMAT_ARGB32, surface1.get_width(), surface1.get_height(),
-    surface1.get_stride()
-    )
-surface2.write_to_png (filename_base + "get_data_test2.png")
-# need to visually check the two files created are the same.
+a = numpy.frombuffer (buf, numpy.uint8)
+a.shape = (w, h, 4)
+
+# draw a vertical line
+a[:,40,0] = 255  # byte 0 is blue on little-endian systems
+a[:,40,1] = 0
+a[:,40,2] = 0
+surface.write_to_png (filename_base + "get_data_test2.png")



More information about the cairo-commit mailing list