|
| 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