|
2 | 2 | # documentation -- you have to know how to use the agg c++ API to use |
3 | 3 | # it |
4 | 4 | import matplotlib.agg as agg |
| 5 | +from math import pi |
5 | 6 |
|
| 7 | +## Define some colors |
| 8 | +red = agg.rgba8(255,0,0,255) |
| 9 | +blue = agg.rgba8(0,0,255,255) |
| 10 | +green = agg.rgba8(0,255,0,255) |
| 11 | +black = agg.rgba8(0,0,0,255) |
| 12 | +white = agg.rgba8(255,255,255,255) |
| 13 | +yellow = agg.rgba8(192,192,255,255) |
| 14 | + |
| 15 | +## Create the rendering buffer, rasterizer, etc |
6 | 16 | width, height = 600,400 |
7 | 17 | stride = width*4 |
8 | 18 | buffer = agg.buffer(width, height, stride) |
9 | 19 |
|
10 | 20 | rbuf = agg.rendering_buffer() |
11 | 21 | rbuf.attachb(buffer) |
12 | 22 |
|
13 | | -red = agg.rgba8(255,0,0,255) |
14 | | -blue = agg.rgba8(0,0,255,255) |
15 | | -green = agg.rgba8(0,255,0,255) |
16 | | -black = agg.rgba8(0,0,0,255) |
17 | | -white = agg.rgba8(255,255,255,255) |
18 | | -yellow = agg.rgba8(192,192,255,255) |
| 23 | +pf = agg.pixel_format_rgba(rbuf) |
| 24 | +rbase = agg.renderer_base_rgba(pf) |
| 25 | +rbase.clear_rgba8(blue) |
19 | 26 |
|
| 27 | +renderer = agg.renderer_scanline_aa_solid_rgba(rbase); |
| 28 | +renderer.color_rgba8( red ) |
| 29 | +rasterizer = agg.rasterizer_scanline_aa() |
| 30 | +scanline = agg.scanline_p8() |
20 | 31 |
|
| 32 | +## A polygon path |
21 | 33 | path = agg.path_storage() |
22 | 34 | path.move_to(10,10) |
23 | 35 | path.line_to(100,100) |
24 | 36 | path.line_to(200,200) |
25 | 37 | path.line_to(100,200) |
26 | 38 | path.close_polygon() |
27 | 39 |
|
| 40 | +# stroke it |
28 | 41 | stroke = agg.conv_stroke_path(path) |
29 | 42 | stroke.width(3.0) |
| 43 | +rasterizer.add_path(stroke) |
| 44 | +agg.render_scanlines_rgba(rasterizer, scanline, renderer); |
30 | 45 |
|
31 | | -pf = agg.pixel_format_rgba(rbuf) |
32 | | -rbase = agg.renderer_base_rgba(pf) |
33 | | -rbase.clear_rgba8(blue) |
| 46 | +## A curved path |
| 47 | +path = agg.path_storage() |
| 48 | +path.move_to(200,10) |
| 49 | +path.line_to(350,50) |
| 50 | +path.curve3(150,200) |
| 51 | +path.curve3(100,70) |
| 52 | +path.close_polygon() |
| 53 | +curve = agg.conv_curve_path(path) |
34 | 54 |
|
35 | | -renderer = agg.renderer_scanline_aa_solid_rgba(rbase); |
36 | | -renderer.color_rgba8( red ) |
| 55 | +# fill it |
| 56 | +rasterizer.add_path(curve) |
| 57 | +renderer.color_rgba8( green ) |
| 58 | +agg.render_scanlines_rgba(rasterizer, scanline, renderer); |
37 | 59 |
|
38 | | -rasterizer = agg.rasterizer_scanline_aa() |
| 60 | +# and stroke it |
| 61 | +stroke = agg.conv_stroke_curve(curve) |
| 62 | +stroke.width(5.0) |
39 | 63 | rasterizer.add_path(stroke) |
| 64 | +renderer.color_rgba8( yellow ) |
| 65 | +agg.render_scanlines_rgba(rasterizer, scanline, renderer); |
40 | 66 |
|
41 | | -scanline = agg.scanline_p8() |
| 67 | +## Transforming a path |
| 68 | +path = agg.path_storage() |
| 69 | +path.move_to(0,0) |
| 70 | +path.line_to(1,0) |
| 71 | +path.line_to(1,1) |
| 72 | +path.line_to(0,1) |
| 73 | +path.close_polygon() |
| 74 | + |
| 75 | +rotation = agg.trans_affine_rotation(pi/4) |
| 76 | +scaling = agg.trans_affine_scaling(30,30) |
| 77 | +translation = agg.trans_affine_translation(300,300) |
| 78 | +trans = rotation*scaling*translation |
| 79 | + |
| 80 | +transpath = agg.conv_transform_path(path, trans) |
| 81 | +stroke = agg.conv_stroke_transpath(transpath) |
| 82 | +stroke.width(2.0) |
| 83 | +rasterizer.add_path(stroke) |
| 84 | +renderer.color_rgba8( black ) |
| 85 | +agg.render_scanlines_rgba(rasterizer, scanline, renderer); |
| 86 | + |
| 87 | +## Converting a transformed path to a curve |
| 88 | +path = agg.path_storage() |
| 89 | +path.move_to(0,0) |
| 90 | +path.curve3(1,0) |
| 91 | +path.curve3(1,1) |
| 92 | +path.curve3(0,1) |
| 93 | +path.close_polygon() |
42 | 94 |
|
| 95 | +rotation = agg.trans_affine_rotation(pi/4) |
| 96 | +scaling = agg.trans_affine_scaling(30,30) |
| 97 | +translation = agg.trans_affine_translation(300,250) |
| 98 | +trans = rotation*scaling*translation |
| 99 | + |
| 100 | +transpath = agg.conv_transform_path(path, trans) |
| 101 | +curvetrans = agg.conv_curve_trans(transpath) |
| 102 | +stroke = agg.conv_stroke_curvetrans(curvetrans) |
| 103 | +stroke.width(2.0) |
| 104 | +rasterizer.add_path(stroke) |
| 105 | +renderer.color_rgba8( white ) |
43 | 106 | agg.render_scanlines_rgba(rasterizer, scanline, renderer); |
44 | 107 |
|
| 108 | +## Copy a rectangle from the buffer the rectangle defined by |
| 109 | +## x0,y0->x1,y1 and paste it at xdest, ydest |
| 110 | +x0, y0 = 10, 50 |
| 111 | +x1, y1 = 110, 190 |
| 112 | +xdest, ydest = 350, 200 |
| 113 | + |
| 114 | + |
| 115 | +widthr, heightr = x1-x0, y1-y0 |
| 116 | +strider = widthr*4 |
| 117 | +copybuffer = agg.buffer(widthr, heightr, strider) |
| 118 | + |
| 119 | +rbufcopy = agg.rendering_buffer() |
| 120 | +rbufcopy.attachb(copybuffer) |
| 121 | +pfcopy = agg.pixel_format_rgba(rbufcopy) |
| 122 | +rbasecopy = agg.renderer_base_rgba(pfcopy) |
| 123 | + |
| 124 | +rect = agg.rect(x0, y0, x1, y1) |
| 125 | +print rect.is_valid() |
| 126 | +rectp = agg.rectPtr(rect) |
| 127 | +#print dir(rbasecopy) |
| 128 | + |
| 129 | +# agg is funny about the arguments to copy from; the last 2 args are |
| 130 | +# dx, dy. If the src and dest buffers are the same size and you omit |
| 131 | +# the dx and dy args, the position of the copy in the dest buffer is |
| 132 | +# the same as in the src. Since our dest buffer is smaller than our |
| 133 | +# src buffer, we have to offset the location by -x0, -y0 |
| 134 | +rbasecopy.copy_from(rbuf, rect, -x0, -y0); |
| 135 | + |
| 136 | +# paste the rectangle at a new location xdest, ydest |
| 137 | +rbase.copy_from(rbufcopy, None, xdest, ydest); |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +## Display it with PIL |
45 | 142 | s = buffer.to_string() |
46 | 143 | print len(s) |
47 | 144 | import Image |
|
0 commit comments