plug-ins/pygimp/gimpmodule.c plug-ins/pygimp/pygimp-pdb.c

2007-12-30  Manish Singh  <yosh@gimp.org>

        * plug-ins/pygimp/gimpmodule.c
        * plug-ins/pygimp/pygimp-pdb.c
        * plug-ins/pygimp/pygimp-colors.c
        * plug-ins/pygimp/pygimpcolor-api.h
        * plug-ins/pygimp/pygimpcolor.h: Move pygimp_rgb_from_pyobject into
        the gimpcolor module, and make it handle translation of sequences
        into colors too. Make the GimpParam translation code use it.

        * plug-ins/pygimp/gimpcolormodule.c: Initial wrapping of
        gimp_bilinear color functions.

        * plug-ins/pygimp/gimpenumsmodule.c
        * plug-ins/pygimp/gimpmodule.c
        * plug-ins/pygimp/pygimp.h
        * plug-ins/pygimp/pygimp-tile.c: Basic wrapping of GimpPixelFetcher.

svn path=/trunk/; revision=24481
This commit is contained in:
Manish Singh
2007-12-30 15:25:13 +00:00
committed by Manish Singh
parent af3009347a
commit 43f8239668
10 changed files with 393 additions and 83 deletions

View File

@ -1,3 +1,21 @@
2007-12-30 Manish Singh <yosh@gimp.org>
* plug-ins/pygimp/gimpmodule.c
* plug-ins/pygimp/pygimp-pdb.c
* plug-ins/pygimp/pygimp-colors.c
* plug-ins/pygimp/pygimpcolor-api.h
* plug-ins/pygimp/pygimpcolor.h: Move pygimp_rgb_from_pyobject into
the gimpcolor module, and make it handle translation of sequences
into colors too. Make the GimpParam translation code use it.
* plug-ins/pygimp/gimpcolormodule.c: Initial wrapping of
gimp_bilinear color functions.
* plug-ins/pygimp/gimpenumsmodule.c
* plug-ins/pygimp/gimpmodule.c
* plug-ins/pygimp/pygimp.h
* plug-ins/pygimp/pygimp-tile.c: Basic wrapping of GimpPixelFetcher.
2007-12-30 Øyvind Kolås <pippin@gimp.org>
* app/gegl/gimpoperationtilesink.c: specify that this operation does

View File

@ -148,67 +148,95 @@ cleanup:
return dict;
}
#if 0
static PyObject *
pygimp_bilinear(PyObject *self, PyObject *args, PyObject *kwargs)
{
gdouble x, y;
gdouble values[4];
PyObject *py_values;
static char *kwlist[] = { "x", "y", "values", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"dd(dddd):bilinear", kwlist,
&x, &y,
&values[0], &values[1],
&values[2], &values[3]))
"ddO:bilinear", kwlist,
&x, &y, &py_values))
return NULL;
return PyFloat_FromDouble(gimp_bilinear(x, y, values));
}
static PyObject *
pygimp_bilinear_8(PyObject *self, PyObject *args, PyObject *kwargs)
{
gdouble x, y;
char *values;
int len;
guchar r;
static char *kwlist[] = { "x", "y", "values", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"dds#:bilinear_8", kwlist,
&x, &y, &values, &len))
return NULL;
if (len != 4) {
PyErr_SetString(PyExc_ValueError,
"string must be exactly 4 bytes long");
return NULL;
if (PyString_Check(py_values)) {
if (PyString_Size(py_values) == 4) {
guchar ret;
ret = gimp_bilinear_8(x, y, (guchar *)PyString_AsString(py_values));
return PyString_FromStringAndSize((char *)&ret, 1);
}
} else if (PySequence_Check(py_values)) {
if (PySequence_Size(py_values) == 4) {
int i;
for (i = 0; i < 4; i++) {
PyObject *v;
v = PySequence_GetItem(py_values, i);
values[i] = PyFloat_AsDouble(v);
Py_DECREF(v);
}
return PyFloat_FromDouble(gimp_bilinear(x, y, values));
}
}
r = gimp_bilinear_8(x, y, values);
return PyString_FromStringAndSize(&r, 1);
PyErr_SetString(PyExc_TypeError, "values is not a sequence of 4 items");
return NULL;
}
static PyObject *
pygimp_bilinear_32(PyObject *self, PyObject *args, PyObject *kwargs)
pygimp_bilinear_color(PyObject *self, PyObject *args, PyObject *kwargs, gboolean with_alpha)
{
gdouble x, y;
guint32 values[4];
GimpRGB values[4];
GimpRGB rgb;
PyObject *py_values, *v;
int i, success;
static char *kwlist[] = { "x", "y", "values", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"dd(IIII):bilinear_32",
kwlist,
&x, &y,
&values[0], &values[1],
&values[2], &values[3]))
return NULL;
with_alpha ? "ddO:bilinear_rgba"
: "ddO:bilinear_rgb",
kwlist,
&x, &y, &py_values))
return NULL;
return PyInt_FromLong(gimp_bilinear_32(x, y, values));
if (!PySequence_Check(py_values) || PySequence_Size(py_values) != 4) {
PyErr_SetString(PyExc_TypeError, "values is not a sequence of 4 items");
return NULL;
}
for (i = 0; i < 4; i++) {
v = PySequence_GetItem(py_values, i);
success = pygimp_rgb_from_pyobject(v, &values[i]);
Py_DECREF(v);
if (!success) {
PyErr_Format(PyExc_TypeError, "values[%d] is not a GimpRGB", i);
return NULL;
}
}
if (with_alpha)
rgb = gimp_bilinear_rgba(x, y, values);
else
rgb = gimp_bilinear_rgb(x, y, values);
return pygimp_rgb_new(&rgb);
}
static PyObject *
pygimp_bilinear_rgb(PyObject *self, PyObject *args, PyObject *kwargs)
{
return pygimp_bilinear_color(self, args, kwargs, FALSE);
}
static PyObject *
pygimp_bilinear_rgba(PyObject *self, PyObject *args, PyObject *kwargs)
{
return pygimp_bilinear_color(self, args, kwargs, TRUE);
}
#if 0
static PyObject *
pygimp_bilinear_pixels_8(PyObject *self, PyObject *args, PyObject *kwargs)
{
@ -334,11 +362,11 @@ static struct PyMethodDef gimpcolor_methods[] = {
{"rgb_parse_hex", (PyCFunction)pygimp_rgb_parse_hex, METH_VARARGS | METH_KEYWORDS},
{"rgb_parse_css", (PyCFunction)pygimp_rgb_parse_css, METH_VARARGS | METH_KEYWORDS},
{"rgb_names", (PyCFunction)pygimp_rgb_list_names, METH_NOARGS},
#if 0
{"bilinear", (PyCFunction)pygimp_bilinear, METH_VARARGS | METH_KEYWORDS},
{"bilinear_8", (PyCFunction)pygimp_bilinear_8, METH_VARARGS | METH_KEYWORDS},
{"bilinear_32", (PyCFunction)pygimp_bilinear_32, METH_VARARGS | METH_KEYWORDS},
//{"bilinear_pixels_8", (PyCFunction)pygimp_bilinear_pixels_8, METH_VARARGS | METH_KEYWORDS},
{"bilinear_rgb", (PyCFunction)pygimp_bilinear_rgb, METH_VARARGS | METH_KEYWORDS},
{"bilinear_rgba", (PyCFunction)pygimp_bilinear_rgba, METH_VARARGS | METH_KEYWORDS},
#if 0
{"bilinear_pixels_8", (PyCFunction)pygimp_bilinear_pixels_8, METH_VARARGS | METH_KEYWORDS},
{"adaptive_supersample_area", (PyCFunction)pygimp_adaptive_supersample_area, METH_VARARGS | METH_KEYWORDS},
#endif
{NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
@ -353,7 +381,8 @@ static struct _PyGimpColor_Functions pygimpcolor_api_functions = {
&PyGimpHSL_Type,
pygimp_hsl_new,
&PyGimpCMYK_Type,
pygimp_cmyk_new
pygimp_cmyk_new,
pygimp_rgb_from_pyobject
};

View File

@ -75,6 +75,17 @@ add_misc_enums(PyObject *m)
PyModule_AddObject(m, "MAX_MEMSIZE",
PyLong_FromUnsignedLongLong(GIMP_MAX_MEMSIZE));
PyModule_AddIntConstant(m, "PIXEL_FETCHER_EDGE_NONE",
GIMP_PIXEL_FETCHER_EDGE_NONE);
PyModule_AddIntConstant(m, "PIXEL_FETCHER_EDGE_WRAP",
GIMP_PIXEL_FETCHER_EDGE_WRAP);
PyModule_AddIntConstant(m, "PIXEL_FETCHER_EDGE_SMEAR",
GIMP_PIXEL_FETCHER_EDGE_SMEAR);
PyModule_AddIntConstant(m, "PIXEL_FETCHER_EDGE_BLACK",
GIMP_PIXEL_FETCHER_EDGE_BLACK);
PyModule_AddIntConstant(m, "PIXEL_FETCHER_EDGE_BACKGROUND",
GIMP_PIXEL_FETCHER_EDGE_BACKGROUND);
}
static void

View File

@ -830,30 +830,6 @@ pygimp_get_foreground(PyObject *self)
return pygimp_rgb_new(&rgb);
}
static gboolean
pygimp_rgb_from_pyobject(PyObject *object, GimpRGB *color)
{
g_return_val_if_fail(color != NULL, FALSE);
if (pygimp_rgb_check (object)) {
*color = *pyg_boxed_get(object, GimpRGB);
return TRUE;
}
if (PyString_Check (object)) {
if (gimp_rgb_parse_css (color, PyString_AsString(object), -1)) {
return TRUE;
} else {
PyErr_Clear();
PyErr_SetString(PyExc_TypeError, "unable to parse color string");
return FALSE;
}
}
PyErr_Clear();
PyErr_SetString(PyExc_TypeError, "could not convert to GimpRGB");
return FALSE;
}
static PyObject *
pygimp_set_background(PyObject *self, PyObject *args)
{
@ -1895,6 +1871,12 @@ initgimp(void)
if (PyType_Ready(&PyGimpVectors_Type) < 0)
return;
PyGimpPixelFetcher_Type.ob_type = &PyType_Type;
PyGimpPixelFetcher_Type.tp_alloc = PyType_GenericAlloc;
PyGimpPixelFetcher_Type.tp_new = PyType_GenericNew;
if (PyType_Ready(&PyGimpPixelFetcher_Type) < 0)
return;
pygimp_init_pygobject();
init_pygimpcolor();
@ -1949,6 +1931,9 @@ initgimp(void)
Py_INCREF(&PyGimpVectors_Type);
PyModule_AddObject(m, "Vectors", (PyObject *)&PyGimpVectors_Type);
Py_INCREF(&PyGimpPixelFetcher_Type);
PyModule_AddObject(m, "PixelFetcher", (PyObject *)&PyGimpPixelFetcher_Type);
/* for other modules */
pygimp_api_functions.pygimp_error = pygimp_error;

View File

@ -2355,3 +2355,51 @@ pygimp_cmyk_new(const GimpCMYK *cmyk)
{
return pyg_boxed_new(GIMP_TYPE_CMYK, (gpointer)cmyk, TRUE, TRUE);
}
int
pygimp_rgb_from_pyobject(PyObject *object, GimpRGB *color)
{
g_return_val_if_fail(color != NULL, FALSE);
if (pygimp_rgb_check(object)) {
*color = *pyg_boxed_get(object, GimpRGB);
return 1;
} else if (PyString_Check(object)) {
if (gimp_rgb_parse_css (color, PyString_AsString(object), -1)) {
return 1;
} else {
PyErr_SetString(PyExc_TypeError, "unable to parse color string");
return 0;
}
} else if (PySequence_Check(object)) {
GimpRGB rgb;
PyObject *r, *g, *b, *a = NULL;
if (!PyArg_ParseTuple(object, "OOO|O", &r, &g, &b, &a))
return 0;
#define SET_MEMBER(m) G_STMT_START { \
if (PyInt_Check(m)) \
rgb.m = (double) PyInt_AS_LONG(m) / 255.0; \
else if (PyFloat_Check(m)) \
rgb.m = PyFloat_AS_DOUBLE(m); \
else { \
PyErr_SetString(PyExc_TypeError, \
#m " must be an int or a float"); \
return -1; \
} \
} G_STMT_END
SET_MEMBER(r);
SET_MEMBER(g);
SET_MEMBER(b);
if (a)
SET_MEMBER(a);
else
rgb.a = 1.0;
}
PyErr_SetString(PyExc_TypeError, "could not convert to GimpRGB");
return 0;
}

View File

@ -311,7 +311,7 @@ pygimp_param_to_tuple(int nparams, const GimpParam *params)
GimpParam *
pygimp_param_from_tuple(PyObject *args, const GimpParamDef *ptype, int nparams)
{
PyObject *tuple, *item, *r, *g, *b, *x, *y, *w, *h;
PyObject *tuple, *item, *x, *y, *w, *h;
GimpParam *ret;
int i, j, len;
gint32 *i32a; gint16 *i16a; guint8 *i8a; gdouble *fa; gchar **sa;
@ -455,23 +455,15 @@ pygimp_param_from_tuple(PyObject *args, const GimpParamDef *ptype, int nparams)
break;
case GIMP_PDB_COLOR:
{
GimpRGB *rgb, tmprgb;
GimpRGB rgb;
if (!pygimp_rgb_check(item)) {
check(!PySequence_Check(item) ||
PySequence_Length(item) < 3);
r = PySequence_GetItem(item, 0);
g = PySequence_GetItem(item, 1);
b = PySequence_GetItem(item, 2);
check(!PyInt_Check(r) || !PyInt_Check(g) ||
!PyInt_Check(b));
gimp_rgba_set_uchar(&tmprgb, PyInt_AsLong(r),
PyInt_AsLong(g), PyInt_AsLong(b), 255);
rgb = &tmprgb;
} else {
rgb = pyg_boxed_get(item, GimpRGB);
if (!pygimp_rgb_from_pyobject(item, &rgb)) {
Py_DECREF(tuple);
gimp_destroy_params(ret, nparams);
return NULL;
}
ret[i].data.d_color = *rgb;
ret[i].data.d_color = rgb;
}
break;
case GIMP_PDB_REGION:

View File

@ -22,6 +22,10 @@
#endif
#include "pygimp.h"
#define NO_IMPORT_PYGIMPCOLOR
#include "pygimpcolor-api.h"
#include <structmember.h>
/* maximum bits per pixel ... */
@ -697,3 +701,208 @@ PyTypeObject PyGimpPixelRgn_Type = {
(allocfunc)0, /* tp_alloc */
(newfunc)0, /* tp_new */
};
static PyObject *
pf_get_pixel(PyGimpPixelFetcher *self, PyObject *args, PyObject *kwargs)
{
int x, y;
guchar pixel[4];
static char *kwlist[] = { "x", "y", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"ii:get_pixel", kwlist,
&x, &y))
return NULL;
gimp_pixel_fetcher_get_pixel(self->pf, x, y, pixel);
return PyString_FromStringAndSize((char *)pixel, self->bpp);
}
static PyObject *
pf_put_pixel(PyGimpPixelFetcher *self, PyObject *args, PyObject *kwargs)
{
int x, y, len;
guchar *pixel;
static char *kwlist[] = { "x", "y", "pixel", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"iis#:put_pixel", kwlist,
&x, &y, &pixel, &len))
return NULL;
if (len != self->bpp) {
PyErr_Format(PyExc_TypeError, "pixel must be %d bpp", self->bpp);
return NULL;
}
gimp_pixel_fetcher_put_pixel(self->pf, x, y, pixel);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef pf_methods[] = {
{"get_pixel", (PyCFunction)pf_get_pixel, METH_VARARGS | METH_KEYWORDS},
{"put_pixel", (PyCFunction)pf_put_pixel, METH_VARARGS | METH_KEYWORDS},
{NULL, NULL}
};
static PyObject *
pf_get_bg_color(PyGimpPixelFetcher *self, void *closure)
{
return pygimp_rgb_new(&self->bg_color);
}
static int
pf_set_bg_color(PyGimpPixelFetcher *self, PyObject *value, void *closure)
{
if (value == NULL) {
PyErr_SetString(PyExc_TypeError, "cannot delete bg_color");
return -1;
}
if (!pygimp_rgb_from_pyobject(value, &self->bg_color))
return -1;
gimp_pixel_fetcher_set_bg_color(self->pf, &self->bg_color);
return 0;
}
static PyObject *
pf_get_edge_mode(PyGimpPixelFetcher *self, void *closure)
{
return PyInt_FromLong(self->edge_mode);
}
static int
pf_set_edge_mode(PyGimpPixelFetcher *self, PyObject *value, void *closure)
{
if (value == NULL) {
PyErr_SetString(PyExc_TypeError, "cannot delete edge_mode");
return -1;
}
if (!PyInt_Check(value)) {
PyErr_SetString(PyExc_TypeError, "type mismatch");
return -1;
}
self->edge_mode = PyInt_AsLong(value);
gimp_pixel_fetcher_set_edge_mode(self->pf, self->edge_mode);
return 0;
}
static PyGetSetDef pf_getsets[] = {
{ "bg_color", (getter)pf_get_bg_color, (setter)pf_set_bg_color },
{ "edge_mode", (getter)pf_get_edge_mode, (setter)pf_set_edge_mode },
{ NULL, (getter)0, (setter)0 }
};
static void
pf_dealloc(PyGimpPixelFetcher *self)
{
gimp_pixel_fetcher_destroy(self->pf);
Py_DECREF(self->drawable);
PyObject_DEL(self);
}
static PyObject *
pf_repr(PyGimpPixelFetcher *self)
{
PyObject *s;
char *name;
name = gimp_drawable_get_name(self->drawable->drawable->drawable_id);
if (self->shadow)
s = PyString_FromFormat("<gimp.PixelFetcher for drawable '%s' (shadow)>", name);
else
s = PyString_FromFormat("<gimp.PixelFetcher for drawable '%s'>", name);
g_free(name);
return s;
}
static int
pf_init(PyGimpPixelFetcher *self, PyObject *args, PyObject *kwargs)
{
PyGimpDrawable *drw;
gboolean shadow = FALSE;
GimpRGB bg_color = { 0.0, 0.0, 0.0, 1.0 };
GimpPixelFetcherEdgeMode edge_mode = GIMP_PIXEL_FETCHER_EDGE_NONE;
static char *kwlist[] = { "drawable", "shadow", "bg_color", "edge_mode",
NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O!|iO&i:gimp.PixelFetcher.__init__",
kwlist,
&PyGimpDrawable_Type, &drw, &shadow,
pygimp_rgb_from_pyobject, &bg_color,
&edge_mode))
return -1;
self->pf = gimp_pixel_fetcher_new(drw->drawable, shadow);
Py_INCREF(drw);
self->drawable = drw;
self->shadow = shadow;
self->bg_color = bg_color;
self->edge_mode = edge_mode;
self->bpp = gimp_drawable_bpp(drw->drawable->drawable_id);
gimp_pixel_fetcher_set_bg_color(self->pf, &bg_color);
gimp_pixel_fetcher_set_edge_mode(self->pf, edge_mode);
return 0;
}
PyTypeObject PyGimpPixelFetcher_Type = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"gimp.PixelFetcher", /* tp_name */
sizeof(PyGimpPixelFetcher), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)pf_dealloc, /* tp_dealloc */
(printfunc)0, /* tp_print */
(getattrfunc)0, /* tp_getattr */
(setattrfunc)0, /* tp_setattr */
(cmpfunc)0, /* tp_compare */
(reprfunc)pf_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc)0, /* tp_hash */
(ternaryfunc)0, /* tp_call */
(reprfunc)0, /* tp_str */
(getattrofunc)0, /* tp_getattro */
(setattrofunc)0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
NULL, /* Documentation string */
(traverseproc)0, /* tp_traverse */
(inquiry)0, /* tp_clear */
(richcmpfunc)0, /* tp_richcompare */
0, /* tp_weaklistoffset */
(getiterfunc)0, /* tp_iter */
(iternextfunc)0, /* tp_iternext */
pf_methods, /* tp_methods */
0, /* tp_members */
pf_getsets, /* tp_getset */
(PyTypeObject *)0, /* tp_base */
(PyObject *)0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)pf_init, /* tp_init */
(allocfunc)0, /* tp_alloc */
(newfunc)0, /* tp_new */
};

View File

@ -107,6 +107,19 @@ PyObject *pygimp_vectors_new(gint32 vectors_ID);
extern PyTypeObject PyGimpVectorsStroke_Type;
extern PyTypeObject PyGimpVectorsBezierStroke_Type;
typedef struct {
PyObject_HEAD
GimpPixelFetcher *pf;
PyGimpDrawable *drawable; /* keep the drawable around */
gboolean shadow;
GimpRGB bg_color;
GimpPixelFetcherEdgeMode edge_mode;
int bpp;
} PyGimpPixelFetcher;
extern PyTypeObject PyGimpPixelFetcher_Type;
#define pygimp_pixel_fetcher_check(v) (PyObject_TypeCheck(v, &PyGimpPixelFetcher_Type))
G_END_DECLS
#endif

View File

@ -34,6 +34,7 @@ struct _PyGimpColor_Functions {
PyObject *(* hsl_new)(const GimpHSL *hsl);
PyTypeObject *CMYK_Type;
PyObject *(* cmyk_new)(const GimpCMYK *cmyk);
int (* rgb_from_pyobject)(PyObject *object, GimpRGB *color);
};
#ifndef _INSIDE_PYGIMPCOLOR_
@ -59,6 +60,8 @@ struct _PyGimpColor_Functions *_PyGimpColor_API;
#define pygimp_hsl_new (_PyGimpColor_API->hsl_new)
#define pygimp_cmyk_new (_PyGimpColor_API->cmyk_new)
#define pygimp_rgb_from_pyobject (_PyGimpColor_API->rgb_from_pyobject)
#define init_pygimpcolor() G_STMT_START { \
PyObject *gimpcolormodule = PyImport_ImportModule("gimpcolor"); \
if (gimpcolormodule != NULL) { \

View File

@ -46,6 +46,8 @@ extern PyTypeObject PyGimpCMYK_Type;
#define pygimp_cmyk_check(v) (pyg_boxed_check((v), GIMP_TYPE_CMYK))
PyObject *pygimp_cmyk_new(const GimpCMYK *cmyk);
int pygimp_rgb_from_pyobject(PyObject *object, GimpRGB *color);
G_END_DECLS
#endif