Copyright (C) 1998-2004 Ljubomir Milanovic & Horst Wagner.
/-------> PNG: g2_attach(id_PNG,.. ----------------------- g2_plot---> | Virtual device: id |--------> X11: g2_attach(id_X11,... ----------------------- \-------> PS: g2_attach(id_PS,...
If you don't need or like the concept of virtual devices, simply ignore it.
g2 uses the gd library by Thomas Boutell to generate PNG and JPEG files. This package is freeware (however, not GPL) and can be downloaded at http://www.boutell.com/gd/. Linux users might prefer to install a pre-compiled gd rpm package which should be available at your local RedHat mirror site. NT users should install the gd source package in a subdirectory named "gd" which should be located in the same directory as the g2 subdirectory (but not in the g2 directory itself). Otherwise, file locations for gd must be modified in the g2 project workspace. Unix and VMS users will have to build and install gd according to the instructions found in the gd distribution.
gzip -dc g2-xxxx.tar.gz | tar xvf -./configuremake dependmake make install, or copy libg2.a/so and g2.h, g2_X11.h, g2_PS.h, g2_gd.h and g2_FIG.h to the default locations for library and include filescd to demo directory and run make perl Makefile.PLmakemake testmake installmake to build g2 Python modulemake demo to test g2 Python modulemake install to install g2 Python module (you must be root)setup.py "compile options" "link options" installlibg2.so, and you are unwilling or unable to do an install, you need to tell the g2 Python module where to look for it, either with ldconfig, or with the LD_LIBRARY_PATH environment variablemms to compile library (descrip.mms file is supplied)mms in demo directory to compile demo applications
#include <g2.h> #include <g2_PS.h> main() { int id; id = g2_open_PS("rect.ps", g2_A4, g2_PS_land); g2_rectangle(id, 20, 20, 150, 150); g2_close(id); }
You want to draw a PNG file instead of a PostScript file ? Replace the PS header file with
#include <g2_gd.h>
and replace the call to g2_open_PS() with
id = g2_open_gd("rect.png", 300, 200, g2_gd_png);
You want to draw to a PNG file and a PostScript file with one plot command ?
Here we use the concept of virtual devices. Open a PNG and a PostScript device, then open a virtual device and attach both the PNG and PostScript device to the virtual device. Plot commands to the virtual device will be issued to both the PNG and the PostScript device. You can attach and detach further devices at any time.
#include <g2.h> #include <g2_PS.h> #include <g2_gd.h> main() { int id_PS,id_PNG,id; id_PS = g2_open_PS("rect.ps", g2_A4, g2_PS_land); id_PNG = g2_open_gd("rect.png", 300, 200, g2_gd_png); id = g2_open_vd(); g2_attach(id, id_PS); g2_attach(id, id_PNG); g2_rectangle(id, 20, 20, 150, 150); g2_circle(id, 50, 60, 100); g2_close(id); }
Note: closing a virtual device automatically closes all attached devices.
REAL CALL #define in C (e.g. g2_A4) do not work. Get corresponding values from the appropriate header files.A short Fortran example:
program demo
real d,color
d=g2_open_PS('demo_f.ps', 4.0, 1.0)
call g2_plot(d, 50.0, 50.0)
call g2_string(d, 25.0, 75.0, 'TEST ')
color=g2_ink(d, 1.0, 0.0, 0.0)
write (6,*) color
call g2_pen(d, color)
call g2_circle(d, 20.0, 20.0, 10.0)
call g2_flush(d)
call g2_close(d)
stop
end
use G2; $d = newX11 G2::Device(100,100); $d->circle(10, 10, 20); $d->string(20, 40, "Hello World"); print "\nDone.\n[Enter]\n"; getc(STDIN); $d->close()
The creator functions are newX11, newGIF, newPS, etc. and accept the same arguments as the open functions in the C version. See the Perl documentation (perldoc G2) for more details and the test.pl script for a more extensive example.
g2_open_ functions. Here is a simple Python script:
import sys from g2 import * X11 = g2_open_X11(822, 575) PS = g2_open_PS('foo.ps', g2_A4, g2_PS_land) graph = g2_open_vd() graph.g2_attach(X11) graph.g2_attach(PS) graph.g2_line(30, 30, 90, 90) graph.g2_circle(60, 60, 30) X11.g2_pen(X11.g2_ink(.75, .2, 0)) graph.g2_polygon([60, 30, 30, 60, 60, 90, 90, 60]) graph.g2_set_dash([20, 12]) sqrts = [100, 100, 225, 150, 400, 200, 625, 250] graph.g2_poly_line(sqrts) graph.g2_image(640, 252, [[2, 4, 6],[3, 6, 9],[4, 8, 12]]) graph.g2_flush() print 'Done.\n[Enter]' sys.stdin.read(1) graph.g2_close()
In C, many functions expect a pointer to a buffer of double's and an int stating the number of points in this buffer. In Python, these functions are passed just a list of floats. You need not specify the number of points: Python knows the length of the list.
Full documentation, including sample code, is available from the interactive Python prompt:
$ python
>>> import g2
>>> help(g2)
Here functions with a Python specific form (e.g. g2_query_pointer()) are marked as such.
or visit the g2 home page on: http://g2.sourceforge.net/
1.4.6