8000 added gtkgd ext and gd win32 files · matplotlib/matplotlib@a9672ea · GitHub
[go: up one dir, main page]

Skip to content

Commit a9672ea

Browse files
committed
added gtkgd ext and gd win32 files
svn path=/trunk/matplotlib/; revision=125
1 parent 93f79b4 commit a9672ea

File tree

6 files changed

+181
-4
lines changed

6 files changed

+181
-4
lines changed

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,5 @@ matplotlib/backends/backend_gtkgd.py
155155
matplotlib/backends/backend_ps.py
156156
matplotlib/backends/backend_template.py
157157
matplotlib/backends/backend_wx.py
158+
src/_matplotlibc.c
158159
test/test.py

MANIFEST.in

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
include API_CHANGES CHANGELOG KNOWN_BUGS GOALS INSTALL INTERACTIVE LICENSE Makefile TODO MANIFEST.in
1+
include LICENSE API_CHANGES CHANGELOG KNOWN_BUGS GOALS INSTALL
2+
include INTERACTIVE TODO
3+
include Makefile MANIFEST.in MANIFEST
4+
include __init__.py setupext.py setup.py
25
include examples/README
36
include examples/*.py
47
include examples/*.glade
58
include examples/data/*
69
include fonts/afm/*
710
include fonts/ttf/*
811
include images/*
12+
include test/*.py
13+
include test/README
14+

examples/simple_plot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from matplotlib.backends.backend_gtk import show_xvfb
12
from matplotlib.matlab import *
23

34
t = arange(0.0, 1.0, 0.002)
@@ -11,5 +12,5 @@
1112

1213
#grid(True)
1314
#set(gca(), 'xticks', (0,.2,.7))
14-
#savefig('test2')
15-
show()
15+
#savefig('test2', dpi=600)
16+
show_xvfb()

setup.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22
import sys,os
33

44
import glob
5+
from setupext import build_gtkgd
6+
57

68
data = []
79
data.extend(glob.glob('fonts/afm/*.afm'))
810
data.extend(glob.glob('fonts/ttf/*.ttf'))
911
data.extend(glob.glob('images/*.xpm'))
1012

13+
ext_modules = []
14+
15+
if 0: # how do I add '--with-gtkgd' flag checking?
16+
build_gtkgd(ext_modules)
17+
18+
1119
setup(name="matplotlib",
12-
version= '0.50e',
20+
version= '0.50f',
1321
description = "Matlab style python plotting package",
1422
author = "John D. Hunter",
1523
author_email="jdhunter@ace.bsd.uchicago.edu",
@@ -22,5 +30,6 @@
2230
""",
2331
packages=['matplotlib', 'matplotlib/backends'],
2432
platforms='any',
33+
ext_modules = ext_modules,
2534
data_files=[('share/matplotlib', data)],
2635
)

setupext.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
"""
3+
Some helper functions for building the C extensions
4+
"""
5+
import sys, os
6+
from distutils.core import Extension
7+
def getoutput(s):
8+
'get the output of a system command'
9+
return os.popen(s).read().strip()
10+
11+
12+
def add_pygtk_flags(module):
13+
'Add the module flags to build extensions which use gtk'
14+
pygtkIncludes = getoutput('pkg-config --cflags-only-I pygtk-2.0').split()
15+
gtkIncludes = getoutput('pkg-config --cflags-only-I gtk+-2.0').split()
16+
includes = pygtkIncludes + gtkIncludes
17+
include_dirs = [include[2:] for include in includes]
18+
19+
20+
pygtkLinker = getoutput('pkg-config --libs pygtk-2.0').split()
21+
gtkLinker = getoutput('pkg-config --libs gtk+-2.0').split()
22+
linkerFlags = pygtkLinker + gtkLinker
23+
24+
libraries = [flag[2:] for flag in linkerFlags if flag.startswith('-l')]
25+
26+
library_dirs = [flag[2:] for dir in linkerFlags if flag.startswith('-L')]
27+
28+
extra_link_args = [flag for flag in linkerFlags if not
29+
(flag.startswith('-l') or flag.startswith('-L'))]
30+
31+
module.include_dirs.extend(include_dirs)
32+
module.libraries.extend(libraries)
33+
module.library_dirs.extend(library_dirs)
34+
module.extra_link_args.extend(extra_link_args)
35+
36+
37+
def add_gd_flags(module):
38+
'Add the module flags to build extensions which use gd'
39+
module.libraries.append('gd')
40+
41+
42+
def build_gtkgd(ext_modules):
43+
module = Extension('matplotlib._gtkgd',
44+
['src/_gtkgd.c'],
45+
library_dirs = [],
46+
libraries = [],
47+
include_dirs = [],
48+
extra_link_args = [],
49+
)
50+
add_pygtk_flags(module)
51+
add_gd_flags(module)
52+
ext_modules.append(module)

src/_gtkgd.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/* Helper functions for converting a gd image to a gtk drawable
2+
efficiently
3+
*/
4+
#include <Python.h>
5+
#include <gd.h>
6+
#include <pygobject.h>
7+
#include <pygtk/pygtk.h>
8+
#include <string.h>
9+
#include <errno.h>
10+
#include <stdio.h>
11+
12+
13+
static PyObject *ErrorObject;
14+
static PyTypeObject *PyGObject_Type=NULL;
15+
16+
typedef struct i_o {
17+
PyObject_HEAD
18+
gdImagePtr imagedata;
19+
int multiplier_x,origin_x;
20+
int multiplier_y,origin_y;
21+
struct i_o *current_brush;
22+
struct i_o *current_tile;
23+
} imageobject;
24+
25+
26+
static PyObject *
27+
_gd_to_gtk_drawable(PyObject *self, PyObject *args) {
28+
int x1, y1, x2, y2, w, h, i, j, c, r, g, b;
29+
PyObject *im = NULL;
30+
imageobject *imo = NULL;
31+
gdImagePtr imData;
32+
PyGObject *py_drawable = NULL;
33+
GdkDrawable *drawable = NULL;
34+
GdkGC* gc = NULL;
35+
GdkColor color;
36+
GdkColormap* cmap = NULL;
37+
38+
if (!PyArg_ParseTuple(args, "O!O", PyGObject_Type, &py_drawable, &im))
39+
return NULL;
40+
41+
drawable = GDK_DRAWABLE(py_drawable->obj);
42+
gc = gdk_gc_new(drawable);
43+
cmap = gdk_window_get_colormap(drawable);
44+
imo = PyObject_GetAttrString(im, "_image");
45+
46+
imData = imo->imagedata;
47+
w = gdImageSX(imData);
48+
h = gdImageSY(imData);
49+
50+
//printf("cmap size: %u\n", cmap->size);
51+
for (i=0; i<w; ++i)
52+
for (j=0; j<h; ++j) {
53+
c = gdImageGetPixel(imData, i, j);
54+
55+
color.red = 256*imData->red[c];
56+
color.green = 256*imData->green[c];
57+
color.blue = 256*imData->blue[c];
58+
gdk_colormap_alloc_color(cmap, &color, 1, 1);
59+
60+
gdk_gc_set_foreground(gc, &color);
61+
//what should I compare color.pixel against here for failure
62+
gdk_draw_point(drawable, gc, i, j);
63+
64+
}
65+
66+
67+
gdImageGetClip(imo->imagedata, &x1, &y1, &x2, &y2);
68+
return Py_BuildValue("(ii)(ii)", x1, y1, x2, y2);
69+
70+
}
71+
72+
73+
74+
75+
76+
static struct PyMethodDef _gtkgd_methods[] = {
77+
{"gd_to_gtk_drawable", (PyCFunction)_gd_to_gtk_drawable, METH_VARARGS,
78+
"Draw to a gtk drawable from a gd image."},
79+
{NULL, NULL} /* sentinel */
80+
};
81+
82+
83+
84+
DL_EXPORT(void) init_gtkgd(void)
85+
{
86+
PyObject *module, *d;
87+
88+
89+
90+
init_pygobject();
91+
init_pygtk();
92+
/* Create the module and add the functions */
93+
Py_InitModule("_gtkgd", _gtkgd_methods);
94+
module = PyImport_ImportModule("gobject");
95+
if (module) {
96+
PyGObject_Type =
97+
(PyTypeObject*)PyObject_GetAttrString(module, "GObject");
98+
Py_DECREF(module);
99+
}
100+
/* Add some symbolic constants to the module */
101+
d = PyModule_GetDict(module);
102+
ErrorObject = PyString_FromString("_gtkgd.error");
103+
PyDict_SetItemString(d, "error", ErrorObject);
104+
105+
/* Check for errors */
106+
if (PyErr_Occurred())
107+
Py_FatalError("can't initialize module _gtkgd");
108+
}

0 commit comments

Comments
 (0)
0