diff options
author | Steve Chaplin <> | 2012-04-21 21:40:53 +0800 |
---|---|---|
committer | Steve Chaplin <> | 2012-04-21 21:40:53 +0800 |
commit | a2d54c73e2d50159fef593d112e1c1dccb8b4222 (patch) | |
tree | 6a158862c8798f6f47aef264f4e17071df25e470 /test/isurface_get_data.py | |
parent | 0c5c297b67381f341e089e5a26c511807fa9290f (diff) |
Implement ImageSurface.get_data(), using some code from Paul Colomiets.
bug #44935.
Diffstat (limited to 'test/isurface_get_data.py')
-rwxr-xr-x | test/isurface_get_data.py | 113 |
1 files changed, 76 insertions, 37 deletions
diff --git a/test/isurface_get_data.py b/test/isurface_get_data.py index f2662d4..6875042 100755 --- a/test/isurface_get_data.py +++ b/test/isurface_get_data.py @@ -5,46 +5,85 @@ Test ImageSurface.get_data() import tempfile import cairo -import numpy if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS): raise SystemExit ('cairo was not compiled with ImageSurface and PNG support') w, h = 128, 128 -surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) -ctx = cairo.Context(surface) - -ctx.set_source_rgb(1, 1, 1) # white -ctx.set_operator(cairo.OPERATOR_SOURCE) -ctx.paint() - -# 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() - -ctx.set_source_rgb(0, 0, 0) # black -ctx.set_line_width(15) -ctx.stroke() -_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png') -surface.write_to_png(outFileName) -print "see %s output file" % outFileName - -# 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) - -a = numpy.ndarray(shape=(w,h,4), dtype=numpy.uint8, buffer=buf) - -# draw a vertical line -a[:,40,0] = 255 # byte 0 is blue on little-endian systems -a[:,40,1] = 0 -a[:,40,2] = 0 -_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png') -surface.write_to_png(outFileName) -print "see %s output file" % outFileName +def create_surface(): + "create black triangle on white background" + surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) + ctx = cairo.Context(surface) + + ctx.set_source_rgb(1, 1, 1) # white + ctx.set_operator(cairo.OPERATOR_SOURCE) + ctx.paint() + + # 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() + + ctx.set_source_rgb(0, 0, 0) # black + ctx.set_line_width(15) + ctx.stroke() + return surface + + +def test_python_buffer(): + "get_data() and modify data using Python" + surface = create_surface() + _, f1 = tempfile.mkstemp(prefix='pycairo_', suffix='.png') + surface.write_to_png(f1) + + buf = surface.get_data() + stride = surface.get_stride() + for i in range(h): + offset = i * stride + 120 + buf[offset] = b'\xFF' + buf[offset + 1] = b'\x00' + buf[offset + 2] = b'\x00' + + _, f2 = tempfile.mkstemp(prefix='pycairo_', suffix='.png') + surface.write_to_png(f2) + print("""\ +test_python_buffer: + original data: %s + modified data: %s +""" % (f1, f2)) + + +def test_numpy_and_python_buffer(): + "get_data() and modify data using numpy" + try: + import numpy + except: + print("numpy not installed") + return + + surface = create_surface() + _, f1 = tempfile.mkstemp(prefix='pycairo_', suffix='.png') + surface.write_to_png(f1) + + buf = surface.get_data() + + a = numpy.ndarray(shape=(w,h,4), dtype=numpy.uint8, buffer=buf) + + # draw a vertical line + a[:,40,0] = 255 # byte 0 is blue on little-endian systems + a[:,40,1] = 0 + a[:,40,2] = 0 + + _, f2 = tempfile.mkstemp(prefix='pycairo_', suffix='.png') + surface.write_to_png(f2) + print("""\ +test_numpy_and_python_buffer: + original data: %s + modified data: %s +""" % (f1, f2)) + + +test_python_buffer() +test_numpy_and_python_buffer() |