home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / pygimp / gimpmodule.c next >
Encoding:
C/C++ Source or Header  |  2000-09-29  |  123.8 KB  |  4,666 lines

  1. /* -*- Mode: C; c-basic-offset: 4 -*- */
  2. /*
  3.     Gimp-Python - allows the writing of Gimp plugins in Python.
  4.     Copyright (C) 1997-1999  James Henstridge <james@daa.com.au>
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include "Python.h"
  22. #include "sysmodule.h"
  23. #include "structmember.h"
  24. #include <libgimp/gimp.h>
  25.  
  26. /* maximum bits per pixel ... */
  27. #define MAX_BPP 4
  28.  
  29. /* part of Hans Breuer's pygimp on win32 patch */
  30. #if defined(_MSC_VER)
  31. /* reduce strict checking in glibconfig.h */
  32. # pragma warning(default:4047)
  33. # undef PyObject_HEAD_INIT
  34. # define PyObject_HEAD_INIT(a) \
  35.   1, NULL, /* must be set in init function */
  36. #endif
  37.  
  38. static PyObject *ErrorObject;
  39.  
  40. #ifndef GIMP_CHECK_VERSION
  41. #  define GIMP_CHECK_VERSION(major, minor, micro) \
  42.     (GIMP_MAJOR_VERSION > (major) || \
  43.      (GIMP_MAJOR_VERSION == (major) && GIMP_MINOR_VERSION > (minor)) || \
  44.      (GIMP_MAJOR_VERSION == (major) && GIMP_MINOR_VERSION == (minor) && \
  45.       GIMP_MICRO_VERSION >= (micro)))
  46. #endif
  47.  
  48. #ifndef PG_DEBUG
  49. # define PG_DEBUG 0
  50. #endif
  51.  
  52. /* ----------------------------------------------------- */
  53.  
  54. /* Declarations for objects of type pdb */
  55.  
  56. typedef struct {
  57.     PyObject_HEAD
  58.     /* XXXX Add your own stuff here */
  59. } pdbobject;
  60.  
  61. staticforward PyTypeObject Pdbtype;
  62. #define pdb_check(v) ((v)->ob_type == &Pdbtype)
  63.  
  64.  
  65. /* ---------------------------------------------------------------- */
  66.  
  67. /* Declarations for objects of type pdbFunc */
  68.  
  69. typedef struct {
  70.     PyObject_HEAD
  71.     char *name;
  72.     PyObject *proc_name, *proc_blurb, *proc_help, *proc_author,
  73.     *proc_copyright, *proc_date, *proc_type, *py_params,
  74.     *py_return_vals;
  75.     int nparams, nreturn_vals;
  76.     GimpParamDef *params, *return_vals;
  77. } pfobject;
  78.  
  79. staticforward PyTypeObject Pftype;
  80. #define pf_check(v) ((v)->ob_type == &Pftype)
  81. static pfobject *newpfobject(char *);
  82.  
  83. /* ---------------------------------------------------------------- */
  84.  
  85. /* Declarations for objects of type Image */
  86.  
  87. typedef struct {
  88.     PyObject_HEAD
  89.     gint32 ID;
  90. } imgobject;
  91.  
  92. staticforward PyTypeObject Imgtype;
  93. #define img_check(v) ((v)->ob_type == &Imgtype)
  94. static imgobject *newimgobject(gint32);
  95.  
  96. /* ---------------------------------------------------------------- */
  97.  
  98. /* Declarations for objects of type Display */
  99.  
  100. typedef struct {
  101.     PyObject_HEAD
  102.     gint32 ID;
  103. } dispobject;
  104.  
  105. staticforward PyTypeObject Disptype;
  106. #define disp_check(v) ((v)->ob_type == &Disptype)
  107. static dispobject *newdispobject(gint32);
  108.  
  109. /* ---------------------------------------------------------------- */
  110.  
  111. /* Declarations for objects of type Layer and channel */
  112.  
  113. typedef struct {
  114.     PyObject_HEAD
  115.     gint32 ID;
  116.     GimpDrawable *drawable;
  117. } drwobject, layobject, chnobject;
  118.  
  119. staticforward PyTypeObject Laytype;
  120. #define lay_check(v) ((v)->ob_type == &Laytype)
  121. static layobject *newlayobject(gint32);
  122.  
  123. staticforward PyTypeObject Chntype;
  124. #define chn_check(v) ((v)->ob_type == &Chntype)
  125. static chnobject *newchnobject(gint32);
  126.  
  127. /* The drawable type isn't really a type -- it can be a channel or layer */
  128. #define drw_check(v) ((v)->ob_type == &Laytype || (v)->ob_type == &Chntype)
  129. static drwobject *newdrwobject(GimpDrawable *, gint32);
  130.  
  131. /* ---------------------------------------------------------------- */
  132.  
  133. /* Declarations for objects of type Tile */
  134.  
  135. typedef struct {
  136.     PyObject_HEAD
  137.     GimpTile *tile;
  138.     drwobject *drawable; /* we keep a reference to the drawable */
  139. } tileobject;
  140.  
  141. staticforward PyTypeObject Tiletype;
  142. #define tile_check(v) ((v)->ob_type == &Tiletype)
  143. static tileobject *newtileobject(GimpTile *, drwobject *drw);
  144.  
  145. /* ---------------------------------------------------------------- */
  146.  
  147. /* Declarations for objects of type PixelRegion */
  148.  
  149. typedef struct {
  150.     PyObject_HEAD
  151.     GimpPixelRgn pr;
  152.     drwobject *drawable; /* keep the drawable around */
  153. } probject;
  154.  
  155. staticforward PyTypeObject Prtype;
  156. #define pr_check(v) ((v)->ob_type == &Prtype)
  157. static probject *newprobject(drwobject *drw, int, int, int, int, int, int);
  158.  
  159. /* ---------------------------------------------------------------- */
  160.  
  161. #ifdef GIMP_HAVE_PARASITES
  162. /* Declarations for objects of type GimpParasite */
  163.  
  164. typedef struct {
  165.     PyObject_HEAD
  166.     GimpParasite *para;
  167. } paraobject;
  168.  
  169. staticforward PyTypeObject Paratype;
  170. #define para_check(v) ((v)->ob_type == &Paratype)
  171. static paraobject *newparaobject(GimpParasite *para);
  172.  
  173. #endif
  174. /* ---------------------------------------------------------------- */
  175.  
  176. /* routines to convert between Python tuples and gimp GimpParam's */
  177.  
  178. #if PG_DEBUG > 0
  179.  
  180. static void print_GParam(int nparams, GimpParam *params) {
  181.     int i;
  182.  
  183.     for (i = 0; i < nparams; i++) {
  184.     switch(params[i].type) {
  185.     case GIMP_PDB_INT32:
  186.         fprintf(stderr, "%i. int %i\n", i,
  187.             params[i].data.d_int32);
  188.         break;
  189.     case GIMP_PDB_INT16:
  190.         fprintf(stderr, "%i. int %i\n", i,
  191.             params[i].data.d_int16);
  192.         break;
  193.     case GIMP_PDB_INT8:
  194.         fprintf(stderr, "%i. int %i\n", i,
  195.             params[i].data.d_int8);
  196.         break;
  197.     case GIMP_PDB_FLOAT:
  198.         fprintf(stderr, "%i. float %f\n", i,
  199.             params[i].data.d_float);
  200.         break;
  201.     case GIMP_PDB_STRING:
  202.         fprintf(stderr, "%i. string %s\n", i,
  203.             params[i].data.d_string);
  204.         break;
  205.     case GIMP_PDB_INT32ARRAY:
  206.     case GIMP_PDB_INT16ARRAY:
  207.     case GIMP_PDB_INT8ARRAY:
  208.     case GIMP_PDB_FLOATARRAY:
  209.     case GIMP_PDB_STRINGARRAY:
  210.         fprintf(stderr, "%i. array of type %i %s\n", i,
  211.             params[i].type, params[i].data.d_int32array
  212.             == NULL ? "(null)":"");
  213.         break;
  214.     case GIMP_PDB_STATUS:
  215.         fprintf(stderr, "%i. status %i\n", i,
  216.             params[i].data.d_status);
  217.         break;
  218.     default:
  219.         fprintf(stderr, "%i. other %i\n", i,
  220.             params[i].data.d_int32);
  221.         break;
  222.     }
  223.     }
  224. }
  225.  
  226. #endif
  227.  
  228. static PyObject *
  229. GParam_to_tuple(int nparams, GimpParam *params) {
  230.     PyObject *args, *tmp;
  231.     int i, j, n;
  232.  
  233.     args = PyTuple_New(nparams);
  234.     for (i = 0; i < nparams && params[i].type != GIMP_PDB_END; i++) {
  235.     switch(params[i].type) {
  236.     case GIMP_PDB_INT32:
  237.         PyTuple_SetItem(args, i, PyInt_FromLong(
  238.                             (long) params[i].data.d_int32));
  239.         break;
  240.     case GIMP_PDB_INT16:
  241.         PyTuple_SetItem(args, i, PyInt_FromLong(
  242.                             (long) params[i].data.d_int16));
  243.         break;
  244.     case GIMP_PDB_INT8:
  245.         PyTuple_SetItem(args, i, PyInt_FromLong(
  246.                             (long) params[i].data.d_int8));
  247.         break;
  248.     case GIMP_PDB_FLOAT:
  249.         PyTuple_SetItem(args, i, PyFloat_FromDouble(
  250.                             (double) params[i].data.d_float));
  251.         break;
  252.     case GIMP_PDB_STRING:
  253.         if (params[i].data.d_string == NULL) {
  254.         Py_INCREF(Py_None);
  255.         PyTuple_SetItem(args, i, Py_None);
  256.         } else
  257.         PyTuple_SetItem(args, i,
  258.                 PyString_FromString(
  259.                             params[i].data.d_string));
  260.         break;
  261.  
  262.         /* For these to work, the previous argument must have
  263.          * been an integer
  264.          */
  265.     case GIMP_PDB_INT32ARRAY:
  266.         if (params[i].data.d_int32array == NULL) {
  267.         PyTuple_SetItem(args,i,PyTuple_New(0));
  268.         break;
  269.         }
  270.         if ((tmp=PyTuple_GetItem(args, i-1)) == NULL) {
  271.         Py_DECREF(args);
  272.         return NULL;
  273.         }
  274.         if (!PyInt_Check(tmp)) {
  275.         PyErr_SetString(PyExc_TypeError,
  276.                 "count type must be integer");
  277.         Py_DECREF(args);
  278.         return NULL;
  279.         }
  280.         n = PyInt_AsLong(tmp);
  281.         tmp = PyTuple_New(n);
  282.         for (j = 0; j < n; j++)
  283.         PyTuple_SetItem(tmp, j, PyInt_FromLong(
  284.                                (long)params[i].data.d_int32array[j]));
  285.         PyTuple_SetItem(args, i, tmp);
  286.         break;
  287.     case GIMP_PDB_INT16ARRAY:
  288.         if (params[i].data.d_int16array == NULL) {
  289.         PyTuple_SetItem(args,i,PyTuple_New(0));
  290.         break;
  291.         }
  292.         if ((tmp=PyTuple_GetItem(args, i-1)) == NULL) {
  293.         Py_DECREF(args);
  294.         return NULL;
  295.         }
  296.         if (!PyInt_Check(tmp)) {
  297.         PyErr_SetString(PyExc_TypeError,
  298.                 "count type must be integer");
  299.         Py_DECREF(args);
  300.         return NULL;
  301.         }
  302.         n = PyInt_AsLong(tmp);
  303.         tmp = PyTuple_New(n);
  304.         for (j = 0; j < n; j++)
  305.         PyTuple_SetItem(tmp, j, PyInt_FromLong(
  306.                                (long)params[i].data.d_int16array[j]));
  307.         PyTuple_SetItem(args, i, tmp);
  308.         break;
  309.     case GIMP_PDB_INT8ARRAY:
  310.         if (params[i].data.d_int8array == NULL) {
  311.         PyTuple_SetItem(args,i,PyTuple_New(0));
  312.         break;
  313.         }
  314.         if ((tmp=PyTuple_GetItem(args, i-1)) == NULL) {
  315.         Py_DECREF(args);
  316.         return NULL;
  317.         }
  318.         if (!PyInt_Check(tmp)) {
  319.         PyErr_SetString(PyExc_TypeError,
  320.                 "count type must be integer");
  321.         Py_DECREF(args);
  322.         return NULL;
  323.         }
  324.         n = PyInt_AsLong(tmp);
  325.         tmp = PyTuple_New(n);
  326.         for (j = 0; j < n; j++)
  327.         PyTuple_SetItem(tmp, j, PyInt_FromLong(
  328.                                (long)params[i].data.d_int8array[j]));
  329.         PyTuple_SetItem(args, i, tmp);
  330.         break;
  331.     case GIMP_PDB_FLOATARRAY:
  332.         if (params[i].data.d_floatarray == NULL) {
  333.         PyTuple_SetItem(args,i,PyTuple_New(0));
  334.         break;
  335.         }
  336.         if ((tmp=PyTuple_GetItem(args, i-1)) == NULL) {
  337.         Py_DECREF(args);
  338.         return NULL;
  339.         }
  340.         if (!PyInt_Check(tmp)) {
  341.         PyErr_SetString(PyExc_TypeError,
  342.                 "count type must be integer");
  343.         Py_DECREF(args);
  344.         return NULL;
  345.         }
  346.         n = PyInt_AsLong(tmp);
  347.         tmp = PyTuple_New(n);
  348.         for (j = 0; j < n; j++)
  349.         PyTuple_SetItem(tmp,j,
  350.                 PyFloat_FromDouble((double)
  351.                            params[i].data.d_floatarray[j]));
  352.         PyTuple_SetItem(args, i, tmp);
  353.         break;
  354.     case GIMP_PDB_STRINGARRAY:
  355.         if (params[i].data.d_stringarray == NULL) {
  356.         PyTuple_SetItem(args,i,PyTuple_New(0));
  357.         break;
  358.         }
  359.         if ((tmp=PyTuple_GetItem(args, i-1)) == NULL) {
  360.         Py_DECREF(args);
  361.         return NULL;
  362.         }
  363.         if (!PyInt_Check(tmp)) {
  364.         PyErr_SetString(PyExc_TypeError,
  365.                 "count type must be integer");
  366.         Py_DECREF(args);
  367.         return NULL;
  368.         }
  369.         n = PyInt_AsLong(tmp);
  370.         tmp = PyTuple_New(n);
  371.         for (j = 0; j < n; j++)
  372.         PyTuple_SetItem(tmp,j, params[i].data.d_stringarray[j] ?
  373.             PyString_FromString(params[i].data.d_stringarray[j]) :
  374.             PyString_FromString(""));
  375.         PyTuple_SetItem(args, i, tmp);
  376.         break;
  377.     case GIMP_PDB_COLOR:
  378.         PyTuple_SetItem(args, i, Py_BuildValue("(iii)",
  379.                            (long) params[i].data.d_color.red,
  380.                            (long) params[i].data.d_color.green,
  381.                            (long) params[i].data.d_color.blue));
  382.         break;
  383.     case GIMP_PDB_REGION:
  384.         PyTuple_SetItem(args, i, Py_BuildValue("(iiii)",
  385.                            (long) params[i].data.d_region.x,
  386.                            (long) params[i].data.d_region.y,
  387.                            (long) params[i].data.d_region.width,
  388.                            (long) params[i].data.d_region.height));
  389.         break;
  390.     case GIMP_PDB_DISPLAY:
  391.         PyTuple_SetItem(args, i, (PyObject *)
  392.                 newdispobject(
  393.                       params[i].data.d_display));
  394.         break;
  395.     case GIMP_PDB_IMAGE:
  396.         PyTuple_SetItem(args, i, (PyObject *)
  397.                 newimgobject(params[i].data.d_image));
  398.         break;
  399.     case GIMP_PDB_LAYER:
  400.         PyTuple_SetItem(args, i, (PyObject *)
  401.                 newlayobject(params[i].data.d_layer));
  402.         break;
  403.     case GIMP_PDB_CHANNEL:
  404.         PyTuple_SetItem(args, i, (PyObject *)
  405.                 newchnobject(params[i].data.d_channel));
  406.         break;
  407.     case GIMP_PDB_DRAWABLE:
  408.         PyTuple_SetItem(args, i, (PyObject *)
  409.                 newdrwobject(NULL,
  410.                      params[i].data.d_drawable));
  411.         break;
  412.     case GIMP_PDB_SELECTION:
  413.         PyTuple_SetItem(args, i, (PyObject *)
  414.                 newlayobject(
  415.                      params[i].data.d_selection));
  416.         break;
  417.     case GIMP_PDB_BOUNDARY:
  418.         PyTuple_SetItem(args, i, PyInt_FromLong(
  419.                             params[i].data.d_boundary));
  420.         break;
  421.     case GIMP_PDB_PATH:
  422.         PyTuple_SetItem(args, i, PyInt_FromLong(
  423.                             params[i].data.d_path));
  424.         break;
  425. #ifdef GIMP_HAVE_PARASITES
  426.     case GIMP_PDB_PARASITE:
  427.         PyTuple_SetItem(args, i,
  428.         (PyObject *)newparaobject(gimp_parasite_copy(
  429.                     &(params[i].data.d_parasite))));
  430.         break;
  431. #endif
  432.     case GIMP_PDB_STATUS:
  433.         PyTuple_SetItem(args, i, PyInt_FromLong(
  434.                             params[i].data.d_status));
  435.         break;
  436.     case GIMP_PDB_END:
  437.         break;
  438.     }
  439.     }
  440.     return args;
  441. }
  442.  
  443. static GimpParam *
  444. tuple_to_GParam(PyObject *args, GimpParamDef *ptype, int nparams) {
  445.     PyObject *tuple, *item, *r, *g, *b, *x, *y, *w, *h;
  446.     GimpParam *ret;
  447.     int i, j, len;
  448.     gint32 *i32a; gint16 *i16a; gint8 *i8a; gdouble *fa; gchar **sa;
  449.  
  450.     if (nparams == 0)
  451.     tuple = PyTuple_New(0);
  452.     else if (!PyTuple_Check(args) && nparams == 1)
  453.     tuple = Py_BuildValue("(O)", args);
  454.     else {
  455.     Py_INCREF(args);
  456.     tuple = args;
  457.     }
  458.     if (!PyTuple_Check(tuple)) {
  459.     PyErr_SetString(PyExc_TypeError, "wrong type of parameter");
  460.     return NULL;
  461.     }
  462.  
  463.     if (PyTuple_Size(tuple) != nparams) {
  464.     PyErr_SetString(PyExc_TypeError, "wrong number of parameters");
  465.     return NULL;
  466.     }
  467.  
  468.     ret = g_new(GimpParam, nparams+1);
  469.     for (i = 0; i <= nparams; i++)
  470.     ret[i].type = GIMP_PDB_STATUS;
  471. #define check(expr) if (expr) { \
  472.         PyErr_SetString(PyExc_TypeError, "wrong parameter type"); \
  473.         Py_DECREF(tuple); \
  474.         gimp_destroy_params(ret, nparams); \
  475.         return NULL; \
  476.     }
  477. #define arraycheck(expr, ar) if (expr) { \
  478.         PyErr_SetString(PyExc_TypeError, "subscript of wrong type"); \
  479.         Py_DECREF(tuple); \
  480.         gimp_destroy_params(ret, nparams); \
  481.         g_free(ar); \
  482.         return NULL; \
  483.     }
  484.     for (i = 1; i <= nparams; i++) {
  485.     item = PyTuple_GetItem(tuple, i-1);
  486.     switch (ptype[i-1].type) {
  487.     case GIMP_PDB_INT32:
  488.         check((x = PyNumber_Int(item)) == NULL)
  489.         ret[i].data.d_int32 = PyInt_AsLong(x);
  490.         Py_DECREF(x);
  491.         break;
  492.     case GIMP_PDB_INT16:
  493.         check((x = PyNumber_Int(item)) == NULL)
  494.         ret[i].data.d_int16 = PyInt_AsLong(x);
  495.         Py_DECREF(x);
  496.         break;
  497.     case GIMP_PDB_INT8:
  498.         check((x = PyNumber_Int(item)) == NULL)
  499.         ret[i].data.d_int8 = PyInt_AsLong(x);
  500.         Py_DECREF(x);
  501.         break;
  502.     case GIMP_PDB_FLOAT:
  503.         check((x = PyNumber_Float(item)) == NULL)
  504.         ret[i].data.d_float = PyFloat_AsDouble(x);
  505.         Py_DECREF(x);
  506.         break;
  507.     case GIMP_PDB_STRING:
  508.         check((x = PyObject_Str(item)) == NULL)
  509.         ret[i].data.d_string = g_strdup(
  510.                         PyString_AsString(x));
  511.         Py_DECREF(x);
  512.         break;
  513.     case GIMP_PDB_INT32ARRAY:
  514.         check(!PySequence_Check(item))
  515.         len = PySequence_Length(item);
  516.         i32a = g_new(gint32, len);
  517.         for (j = 0; j < len; j++) {
  518.         x = PySequence_GetItem(item, j);
  519.         arraycheck((y=PyNumber_Int(x))==NULL,
  520.                i32a);
  521.         i32a[j] = PyInt_AsLong(y);
  522.         Py_DECREF(y);
  523.         }
  524.         ret[i].data.d_int32array = i32a;
  525.         break;
  526.     case GIMP_PDB_INT16ARRAY:
  527.         check(!PySequence_Check(item))
  528.         len = PySequence_Length(item);
  529.         i16a = g_new(gint16, len);
  530.         for (j = 0; j < len; j++) {
  531.         x = PySequence_GetItem(item, j);
  532.         arraycheck((y=PyNumber_Int(x))==NULL,
  533.                i16a);
  534.         i16a[j] = PyInt_AsLong(y);
  535.         Py_DECREF(y);
  536.         }
  537.         ret[i].data.d_int16array = i16a;
  538.         break;
  539.     case GIMP_PDB_INT8ARRAY:
  540.         check(!PySequence_Check(item))
  541.         len = PySequence_Length(item);
  542.         i8a = g_new(gint8, len);
  543.         for (j = 0; j < len; j++) {
  544.         x = PySequence_GetItem(item, j);
  545.         arraycheck((y=PyNumber_Int(x))==NULL,
  546.                i8a);
  547.         i8a[j] = PyInt_AsLong(y);
  548.         Py_DECREF(y);
  549.         }
  550.         ret[i].data.d_int8array = i8a;
  551.         break;
  552.     case GIMP_PDB_FLOATARRAY:
  553.         check(!PySequence_Check(item))
  554.         len = PySequence_Length(item);
  555.         fa = g_new(gdouble, len);
  556.         for (j = 0; j < len; j++) {
  557.         x = PySequence_GetItem(item, j);
  558.         arraycheck((y=PyNumber_Float(x))==NULL,
  559.                fa);
  560.         fa[j] = PyFloat_AsDouble(y);
  561.         Py_DECREF(y);
  562.         }
  563.         ret[i].data.d_floatarray = fa;
  564.         break;
  565.     case GIMP_PDB_STRINGARRAY:
  566.         check(!PySequence_Check(item))
  567.         len = PySequence_Length(item);
  568.         sa = g_new(gchar *, len);
  569.         for (j = 0; j < len; j++) {
  570.         x = PySequence_GetItem(item, j);
  571.         arraycheck((y=PyObject_Str(x))==NULL,
  572.                sa);
  573.         sa[j] = g_strdup(PyString_AsString(y));
  574.         Py_DECREF(y);
  575.         }
  576.         ret[i].data.d_stringarray = sa;
  577.         break;
  578.     case GIMP_PDB_COLOR:
  579.         check(!PySequence_Check(item) ||
  580.           PySequence_Length(item) < 3)
  581.         r = PySequence_GetItem(item, 0);
  582.         g = PySequence_GetItem(item, 1);
  583.         b = PySequence_GetItem(item, 2);
  584.         check(!PyInt_Check(r) || !PyInt_Check(g) ||
  585.           !PyInt_Check(b))
  586.         ret[i].data.d_color.red = PyInt_AsLong(r);
  587.         ret[i].data.d_color.green = PyInt_AsLong(g);
  588.         ret[i].data.d_color.blue = PyInt_AsLong(b);
  589.         break;
  590.     case GIMP_PDB_REGION:
  591.         check(!PySequence_Check(item) ||
  592.           PySequence_Length(item) < 4)
  593.         x = PySequence_GetItem(item, 0);
  594.         y = PySequence_GetItem(item, 1);
  595.         w = PySequence_GetItem(item, 2);
  596.         h = PySequence_GetItem(item, 3);
  597.         check(!PyInt_Check(x) || !PyInt_Check(y) ||
  598.           !PyInt_Check(w) || !PyInt_Check(h))
  599.         ret[i].data.d_region.x = PyInt_AsLong(x);
  600.         ret[i].data.d_region.y = PyInt_AsLong(y);
  601.         ret[i].data.d_region.width = PyInt_AsLong(w);
  602.         ret[i].data.d_region.height = PyInt_AsLong(h);
  603.         break;
  604.     case GIMP_PDB_DISPLAY:
  605.         check(!disp_check(item))
  606.         ret[i].data.d_display=((dispobject*)item)->ID;
  607.         break;
  608.     case GIMP_PDB_IMAGE:
  609.         if (item == Py_None) {
  610.         ret[i].data.d_image = -1;
  611.         break;
  612.         }
  613.         check(!img_check(item))
  614.         ret[i].data.d_image=((imgobject*)item)->ID;
  615.         break;
  616.     case GIMP_PDB_LAYER:
  617.         if (item == Py_None) {
  618.         ret[i].data.d_layer = -1;
  619.         break;
  620.         }
  621.         check(!lay_check(item))
  622.         ret[i].data.d_layer=((layobject*)item)->ID;
  623.         break;
  624.     case GIMP_PDB_CHANNEL:
  625.         if (item == Py_None) {
  626.         ret[i].data.d_channel = -1;
  627.         break;
  628.         }
  629.         check(!chn_check(item))
  630.         ret[i].data.d_channel=((chnobject*)item)->ID;
  631.         break;
  632.     case GIMP_PDB_DRAWABLE:
  633.         if (item == Py_None) {
  634.         ret[i].data.d_channel = -1;
  635.         break;
  636.         }
  637.         check(!drw_check(item) && !lay_check(item) && !chn_check(item))
  638.         ret[i].data.d_channel=((drwobject*)item)->ID;
  639.         break;
  640.     case GIMP_PDB_SELECTION:
  641.         check(!lay_check(item))
  642.         ret[i].data.d_selection=((layobject*)item)->ID;
  643.         break;
  644.     case GIMP_PDB_BOUNDARY:
  645.         check(!PyInt_Check(item))
  646.         ret[i].data.d_boundary = PyInt_AsLong(item);
  647.         break;
  648.     case GIMP_PDB_PATH:
  649.         check(!PyInt_Check(item))
  650.         ret[i].data.d_path = PyInt_AsLong(item);
  651.         break;
  652. #ifdef GIMP_HAVE_PARASITES
  653.     case GIMP_PDB_PARASITE:
  654.         /* can't do anything, since size of GimpParasite is not known */
  655.         break;
  656. #endif
  657.     case GIMP_PDB_STATUS:
  658.         check(!PyInt_Check(item))
  659.         ret[i].data.d_status = PyInt_AsLong(item);
  660.         break;
  661.     case GIMP_PDB_END:
  662.         break;
  663.     }
  664. #undef check
  665. #undef arraycheck
  666.     ret[i].type = ptype[i-1].type;
  667.     }
  668.  
  669.     Py_DECREF(tuple);
  670.     return ret;
  671. }
  672.  
  673. /* ---------------------------------------------------------------- */
  674. /* generic number conversions.  Most gimp objects contain an ID field
  675.  * at the start.  This code fragment adds conversion of that ID to
  676.  * an integer, hex, oct or float
  677.  */
  678.  
  679. static PyObject *
  680. gobj_int(self)
  681.      imgobject *self; /* or layobject, drwobject, chnobject ... */
  682. {
  683.     return PyInt_FromLong((long)self->ID);
  684. }
  685.  
  686. static PyObject *
  687. gobj_long(self)
  688.      imgobject *self;
  689. {
  690.     return PyLong_FromLong((long)self->ID);
  691. }
  692.  
  693. static PyObject *
  694. gobj_float(self)
  695.      imgobject *self;
  696. {
  697.     return PyFloat_FromDouble((double)self->ID);
  698. }
  699.  
  700. static PyObject *
  701. gobj_oct(self)
  702.      imgobject *self;
  703. {
  704.     char buf[20];
  705.  
  706.     if (self->ID == 0)
  707.     strcpy(buf, "0");
  708.     else
  709.     sprintf(buf, "0%lo", (unsigned long)self->ID);
  710.     return PyString_FromString(buf);
  711. }
  712.  
  713. static PyObject *
  714. gobj_hex(self)
  715.      imgobject *self;
  716. {
  717.     char buf[20];
  718.  
  719.     sprintf(buf, "0x%lx", (unsigned long)self->ID);
  720.     return PyString_FromString(buf);
  721. }
  722.  
  723. static PyNumberMethods gobj_as_number = {
  724.     (binaryfunc)0, /*nb_add*/
  725.     (binaryfunc)0, /*nb_subtract*/
  726.     (binaryfunc)0, /*nb_multiply*/
  727.     (binaryfunc)0, /*nb_divide*/
  728.     (binaryfunc)0, /*nb_remainder*/
  729.     (binaryfunc)0, /*nb_divmod*/
  730.     (ternaryfunc)0, /*nb_power*/
  731.     (unaryfunc)0, /*nb_negative*/
  732.     (unaryfunc)0, /*nb_positive*/
  733.     (unaryfunc)0, /*nb_absolute*/
  734.     (inquiry)0, /*nb_nonzero*/
  735.     (unaryfunc)0, /*nb_invert*/
  736.     (binaryfunc)0, /*nb_lshift*/
  737.     (binaryfunc)0, /*nb_rshift*/
  738.     (binaryfunc)0, /*nb_and*/
  739.     (binaryfunc)0, /*nb_xor*/
  740.     (binaryfunc)0, /*nb_or*/
  741.     0,              /*nb_coerce*/
  742.     (unaryfunc)gobj_int, /*nb_int*/
  743.     (unaryfunc)gobj_long, /*nb_long*/
  744.     (unaryfunc)gobj_float, /*nb_float*/
  745.     (unaryfunc)gobj_oct, /*nb_oct*/
  746.     (unaryfunc)gobj_hex, /*nb_hex*/
  747. };
  748.  
  749. /* ---------------------------------------------------------------- */
  750.  
  751. static PyObject *
  752. pdb_query(self, args)
  753.      pdbobject *self;
  754.      PyObject *args;
  755. {
  756.     char *n=".*", *b=".*", *h=".*", *a=".*", *c=".*", *d=".*", *t=".*";
  757.     int num, i;
  758.     char **names;
  759.     PyObject *ret;
  760.     if (!PyArg_ParseTuple(args, "|zzzzzzz:pdb.query", &n, &b, &h, &a,
  761.               &c, &d, &t))
  762.     return NULL;
  763.     gimp_procedural_db_query(n, b, h, a, c, d, t, &num, &names);
  764.     ret = PyList_New(num);
  765.     for (i = 0; i < num; i++)
  766.     PyList_SetItem(ret, i, PyString_FromString(names[i]));
  767.     g_free(names);
  768.     return ret;
  769. }
  770.  
  771. static struct PyMethodDef pdb_methods[] = {
  772.     {"query", (PyCFunction)pdb_query, METH_VARARGS},
  773.     {NULL,        NULL}        /* sentinel */
  774. };
  775.  
  776. /* ---------- */
  777.  
  778.  
  779. static pdbobject *
  780. newpdbobject()
  781. {
  782.     pdbobject *self;
  783.     
  784.     self = PyObject_NEW(pdbobject, &Pdbtype);
  785.     if (self == NULL)
  786.     return NULL;
  787.     /* XXXX Add your own initializers here */
  788.     return self;
  789. }
  790.  
  791.  
  792. static void
  793. pdb_dealloc(self)
  794.      pdbobject *self;
  795. {
  796.     /* XXXX Add your own cleanup code here */
  797.     PyMem_DEL(self);
  798. }
  799.  
  800. static PyObject *
  801. pdb_repr(self)
  802.      pdbobject *self;
  803. {
  804.     return PyString_FromString("<Procedural-Database>");
  805. }
  806.  
  807. /* Code to access pdb objects as mappings */
  808.  
  809. static int
  810. pdb_length(self)
  811.      pdbobject *self;
  812. {
  813.     PyErr_SetString(ErrorObject, "Can not size the procedural database.");
  814.     return -1;
  815. }
  816.  
  817. static PyObject *
  818. pdb_subscript(self, key)
  819.      pdbobject *self;
  820.      PyObject *key;
  821. {
  822.     PyObject *r;
  823.  
  824.     if (!PyString_Check(key)) {
  825.     PyErr_SetString(PyExc_TypeError, "Subscript must be a string.");
  826.     return NULL;
  827.     }
  828.     r = (PyObject *)newpfobject(PyString_AsString(key));
  829.     if (r == NULL) {
  830.     PyErr_Clear();
  831.     PyErr_SetObject(PyExc_KeyError, key);
  832.     }
  833.     return r;
  834. }
  835.  
  836. static int
  837. pdb_ass_sub(self, v, w)
  838.      pdbobject *self;
  839.      PyObject *v, *w;
  840. {
  841.     PyErr_SetString(ErrorObject,
  842.             "Use install_procedure to add to the PDB.");
  843.     return -1;
  844. }
  845.  
  846. static PyMappingMethods pdb_as_mapping = {
  847.     (inquiry)pdb_length,        /*mp_length*/
  848.     (binaryfunc)pdb_subscript,        /*mp_subscript*/
  849.     (objobjargproc)pdb_ass_sub,    /*mp_ass_subscript*/
  850. };
  851.  
  852. /* -------------------------------------------------------- */
  853.  
  854. static PyObject *
  855. pdb_getattr(self, name)
  856.      pdbobject *self;
  857.      char *name;
  858. {
  859.     PyObject *r;
  860.  
  861.     r = Py_FindMethod(pdb_methods, (PyObject *)self, name);
  862.     if (r == NULL) {
  863.     PyErr_Clear();
  864.     return (PyObject *)newpfobject(name);
  865.     }
  866.     return r;
  867. }
  868.  
  869. static PyTypeObject Pdbtype = {
  870.     PyObject_HEAD_INIT(&PyType_Type)
  871.     0,                /*ob_size*/
  872.     "pdb",            /*tp_name*/
  873.     sizeof(pdbobject),        /*tp_basicsize*/
  874.     0,                /*tp_itemsize*/
  875.     /* methods */
  876.     (destructor)pdb_dealloc,    /*tp_dealloc*/
  877.     (printfunc)0,        /*tp_print*/
  878.     (getattrfunc)pdb_getattr,    /*tp_getattr*/
  879.     (setattrfunc)0,    /*tp_setattr*/
  880.     (cmpfunc)0,        /*tp_compare*/
  881.     (reprfunc)pdb_repr,        /*tp_repr*/
  882.     0,            /*tp_as_number*/
  883.     0,        /*tp_as_sequence*/
  884.     &pdb_as_mapping,        /*tp_as_mapping*/
  885.     (hashfunc)0,        /*tp_hash*/
  886.     (ternaryfunc)0,        /*tp_call*/
  887.     (reprfunc)0,        /*tp_str*/
  888.  
  889.     /* Space for future expansion */
  890.     0L,0L,0L,0L,
  891.     NULL /* Documentation string */
  892. };
  893.  
  894. /* End of code for pdb objects */
  895. /* -------------------------------------------------------- */
  896.  
  897.  
  898. static pfobject *
  899. newpfobject(name)
  900.      char *name;
  901. {
  902.     pfobject *self;
  903.     char *b,*h,*a,*c,*d;
  904.     int np, nr, i;
  905.     GimpPDBProcType pt;
  906.     GimpParamDef *p, *r;
  907.  
  908.     if (!gimp_procedural_db_proc_info (name, &b, &h, &a, &c, &d, &pt,
  909.                        &np, &nr, &p, &r)) {
  910.     PyErr_SetString(ErrorObject, "procedure not found.");
  911.     return NULL;
  912.     }
  913.  
  914.     self = PyObject_NEW(pfobject, &Pftype);
  915.     if (self == NULL)
  916.     return NULL;
  917.  
  918.     self->name = g_strdup(name);
  919.     self->proc_name = PyString_FromString(name);
  920.     self->proc_blurb = PyString_FromString(b);
  921.     self->proc_help = PyString_FromString(h);
  922.     self->proc_author = PyString_FromString(a);
  923.     self->proc_copyright = PyString_FromString(c);
  924.     self->proc_date = PyString_FromString(d);
  925.     self->proc_type = PyInt_FromLong(pt);
  926.     self->nparams = np;
  927.     self->nreturn_vals = nr;
  928.     self->params = p;
  929.     self->return_vals = r;
  930.  
  931.     self->py_params = PyTuple_New(np);
  932.     for (i = 0; i < np; i++)
  933.     PyTuple_SetItem(self->py_params, i,
  934.             Py_BuildValue("(iss)", p[i].type, p[i].name,
  935.                       p[i].description));
  936.     self->py_return_vals = PyTuple_New(nr);
  937.     for (i = 0; i < nr; i++)
  938.     PyTuple_SetItem(self->py_return_vals, i,
  939.             Py_BuildValue("(iss)", r[i].type, r[i].name,
  940.                       r[i].description));
  941.  
  942.     g_free(b); g_free(h); g_free(a); g_free(c); g_free(d);
  943.     
  944.     return self;
  945. }
  946.  
  947. #ifndef GIMP_HAVE_DESTROY_PARAMDEFS
  948. static void
  949. gimp_destroy_paramdefs (GimpParamDef *paramdefs,
  950.                         int        nparams)
  951. {
  952.     while (nparams--) {
  953.       g_free (paramdefs[nparams].name);
  954.       g_free (paramdefs[nparams].description);
  955.     }
  956.   
  957.     g_free (paramdefs);
  958. }
  959. #endif
  960. static void
  961. pf_dealloc(self)
  962.      pfobject *self;
  963. {
  964.     g_free(self->name);
  965.     Py_DECREF(self->proc_name);
  966.     Py_DECREF(self->proc_blurb);
  967.     Py_DECREF(self->proc_help);
  968.     Py_DECREF(self->proc_author);
  969.     Py_DECREF(self->proc_copyright);
  970.     Py_DECREF(self->proc_date);
  971.     Py_DECREF(self->proc_type);
  972.     Py_DECREF(self->py_params);
  973.     Py_DECREF(self->py_return_vals);
  974.     gimp_destroy_paramdefs(self->params, self->nparams);
  975.     gimp_destroy_paramdefs(self->return_vals, self->nreturn_vals);
  976.     PyMem_DEL(self);
  977. }
  978. #define OFF(x) offsetof(pfobject, x)
  979.  
  980. static struct memberlist pf_memberlist[] = {
  981.     {"proc_name",      T_OBJECT, OFF(proc_name),      RO},
  982.     {"proc_blurb",     T_OBJECT, OFF(proc_blurb),     RO},
  983.     {"proc_help",      T_OBJECT, OFF(proc_help),      RO},
  984.     {"proc_author",    T_OBJECT, OFF(proc_author),    RO},
  985.     {"proc_copyright", T_OBJECT, OFF(proc_copyright), RO},
  986.     {"proc_date",      T_OBJECT, OFF(proc_date),      RO},
  987.     {"proc_type",      T_OBJECT, OFF(proc_type),      RO},
  988.     {"nparams",        T_INT,    OFF(nparams),        RO},
  989.     {"nreturn_vals",   T_INT,    OFF(nreturn_vals),   RO},
  990.     {"params",         T_OBJECT, OFF(py_params),      RO},
  991.     {"return_vals",    T_OBJECT, OFF(py_return_vals), RO},
  992.     {NULL}  /* Sentinel */
  993. };
  994.  
  995. #undef OFF
  996.  
  997. static PyObject *
  998. pf_getattr(self, name)
  999.      pfobject *self;
  1000.      char *name;
  1001. {
  1002.     return PyMember_Get((char *)self, pf_memberlist, name);
  1003. }
  1004.  
  1005. static PyObject *
  1006. pf_repr(self)
  1007.      pfobject *self;
  1008. {
  1009.     PyObject *s;
  1010.  
  1011.     s = PyString_FromString("<pdb function ");
  1012.     PyString_Concat(&s, self->proc_name);
  1013.     PyString_ConcatAndDel(&s, PyString_FromString(">"));
  1014.     return s;
  1015. }
  1016.  
  1017. static PyObject *
  1018. pf_call(self, args, kwargs)
  1019.      pfobject *self;
  1020.      PyObject *args;
  1021.      PyObject *kwargs;
  1022. {
  1023.     GimpParam *params, *ret;
  1024.     int nret;
  1025.     PyObject *t = NULL, *r;
  1026.  
  1027. #if PG_DEBUG > 0
  1028.     fprintf(stderr, "--- %s --- ", PyString_AsString(self->proc_name));
  1029. #endif
  1030.     if (self->nparams > 0 && !strcmp(self->params[0].name, "run_mode")) {
  1031.     params = tuple_to_GParam(args, self->params+1, self->nparams-1);
  1032.     if (params == NULL)
  1033.         return NULL;
  1034.     params[0].type = self->params[0].type;
  1035.     params[0].data.d_int32 = GIMP_RUN_NONINTERACTIVE;
  1036. #if PG_DEBUG > 1
  1037.     print_GParam(self->nparams, params);
  1038. #endif
  1039.     ret = gimp_run_procedure2(self->name, &nret, self->nparams,
  1040.                   params);
  1041.     } else {
  1042.     params = tuple_to_GParam(args, self->params, self->nparams);
  1043.     if (params == NULL)
  1044.         return NULL;
  1045. #if PG_DEBUG > 1
  1046.     print_GParam(self->nparams, params+1);
  1047. #endif
  1048.     ret = gimp_run_procedure2(self->name, &nret, self->nparams,
  1049.                   params+1);
  1050.     }
  1051.     gimp_destroy_params(params, self->nparams);
  1052.     if (!ret) {
  1053.     PyErr_SetString(ErrorObject, "no status returned");
  1054. #if PG_DEBUG >= 1
  1055.     fprintf(stderr, "ret == NULL\n");
  1056. #endif
  1057.     return NULL;
  1058.     }
  1059.     switch(ret[0].data.d_status) {
  1060.     case GIMP_PDB_EXECUTION_ERROR:
  1061. #if PG_DEBUG > 0
  1062.     fprintf(stderr, "execution error\n");
  1063. #endif
  1064.     gimp_destroy_params(ret, nret);
  1065.     PyErr_SetString(PyExc_RuntimeError, "execution error");
  1066.     return NULL;
  1067.     break;
  1068.     case GIMP_PDB_CALLING_ERROR:
  1069. #if PG_DEBUG > 0
  1070.     fprintf(stderr, "calling error\n");
  1071. #endif
  1072.     gimp_destroy_params(ret, nret);
  1073.     PyErr_SetString(PyExc_TypeError, "invalid arguments");
  1074.     return NULL;
  1075.     break;
  1076.     case GIMP_PDB_SUCCESS:
  1077. #if PG_DEBUG > 0
  1078.     fprintf(stderr, "success\n");
  1079. #endif
  1080.     t = GParam_to_tuple(nret-1, ret+1);
  1081.     gimp_destroy_params(ret, nret);
  1082.     if (t == NULL) {
  1083.         PyErr_SetString(ErrorObject,
  1084.                 "couldn't make return value");
  1085.         return NULL;
  1086.     }
  1087.     break;
  1088.     default:
  1089. #if PG_DEBUG > 0
  1090.     fprintf(stderr, "unknown - %i (type %i)\n",
  1091.         ret[0].data.d_status, ret[0].type);
  1092. #endif
  1093.     PyErr_SetString(ErrorObject, "unknown return code.");
  1094.     return NULL;
  1095.     break;
  1096.     }
  1097.     if (PyTuple_Size(t) == 1) {
  1098.     r = PyTuple_GetItem(t, 0);
  1099.     Py_INCREF(r);
  1100.     Py_DECREF(t);
  1101.     return r;
  1102.     }
  1103.     if (PyTuple_Size(t) == 0) {
  1104.     r = Py_None;
  1105.     Py_INCREF(r);
  1106.     Py_DECREF(t);
  1107.     return r;
  1108.     }
  1109.     return t;
  1110. }
  1111.  
  1112.  
  1113. static PyTypeObject Pftype = {
  1114.     PyObject_HEAD_INIT(&PyType_Type)
  1115.     0,                /*ob_size*/
  1116.     "pdbFunc",            /*tp_name*/
  1117.     sizeof(pfobject),        /*tp_basicsize*/
  1118.     0,                /*tp_itemsize*/
  1119.     /* methods */
  1120.     (destructor)pf_dealloc,    /*tp_dealloc*/
  1121.     (printfunc)0,        /*tp_print*/
  1122.     (getattrfunc)pf_getattr,    /*tp_getattr*/
  1123.     (setattrfunc)0,    /*tp_setattr*/
  1124.     (cmpfunc)0,        /*tp_compare*/
  1125.     (reprfunc)pf_repr,        /*tp_repr*/
  1126.     0,            /*tp_as_number*/
  1127.     0,        /*tp_as_sequence*/
  1128.     0,        /*tp_as_mapping*/
  1129.     (hashfunc)0,        /*tp_hash*/
  1130.     (ternaryfunc)pf_call,        /*tp_call*/
  1131.     (reprfunc)0,        /*tp_str*/
  1132.  
  1133.     /* Space for future expansion */
  1134.     0L,0L,0L,0L,
  1135.     NULL /* Documentation string */
  1136. };
  1137.  
  1138. /* End of code for pdbFunc objects */
  1139. /* -------------------------------------------------------- */
  1140.  
  1141.  
  1142. static PyObject *
  1143. img_add_channel(self, args)
  1144.      imgobject *self;
  1145.      PyObject *args;
  1146. {
  1147.     chnobject *chn;
  1148.     int pos;
  1149.     
  1150.     if (!PyArg_ParseTuple(args, "O!i:add_channel", &Chntype, &chn, &pos))
  1151.     return NULL;
  1152.     gimp_image_add_channel(self->ID, chn->ID, pos);
  1153.     Py_INCREF(Py_None);
  1154.     return Py_None;
  1155. }
  1156.  
  1157.  
  1158. static PyObject *
  1159. img_add_layer(self, args)
  1160.      imgobject *self;
  1161.      PyObject *args;
  1162. {
  1163.     layobject *lay;
  1164.     int pos;
  1165.     
  1166.     if (!PyArg_ParseTuple(args, "O!i:add_layer", &Laytype, &lay, &pos))
  1167.     return NULL;
  1168.     gimp_image_add_layer(self->ID, lay->ID, pos);
  1169.     Py_INCREF(Py_None);
  1170.     return Py_None;
  1171. }
  1172.  
  1173.  
  1174. static PyObject *
  1175. img_add_layer_mask(self, args)
  1176.      imgobject *self;
  1177.      PyObject *args;
  1178. {
  1179.     layobject *lay;
  1180.     chnobject *mask;
  1181.     if (!PyArg_ParseTuple(args, "O!O!:add_layer_mask", &Laytype, &lay,
  1182.               &Chntype, &mask))
  1183.     return NULL;
  1184.     gimp_image_add_layer_mask(self->ID, lay->ID, mask->ID);
  1185.     Py_INCREF(Py_None);
  1186.     return Py_None;
  1187. }
  1188.  
  1189.  
  1190. static PyObject *
  1191. img_clean_all(self, args)
  1192.      imgobject *self;
  1193.      PyObject *args;
  1194. {
  1195.     if (!PyArg_ParseTuple(args, ":clean_all"))
  1196.     return NULL;
  1197.     gimp_image_clean_all(self->ID);
  1198.     Py_INCREF(Py_None);
  1199.     return Py_None;
  1200. }
  1201.  
  1202.  
  1203. static PyObject *
  1204. img_disable_undo(self, args)
  1205.      imgobject *self;
  1206.      PyObject *args;
  1207. {
  1208.     /*GimpParam *return_vals;
  1209.     int nreturn_vals;*/
  1210.  
  1211.     if (!PyArg_ParseTuple(args, ":disable_undo"))
  1212.     return NULL;
  1213.     /*return_vals = gimp_run_procedure("gimp_undo_push_group_start",
  1214.                      &nreturn_vals, GIMP_PDB_IMAGE, self->ID,
  1215.                      GIMP_PDB_END);
  1216.     gimp_destroy_params(return_vals, nreturn_vals);*/
  1217.     gimp_image_undo_disable(self->ID);
  1218.     Py_INCREF(Py_None);
  1219.     return Py_None;
  1220. }
  1221.  
  1222.  
  1223. static PyObject *
  1224. img_enable_undo(self, args)
  1225.      imgobject *self;
  1226.      PyObject *args;
  1227. {
  1228.     /*GimpParam *return_vals;
  1229.     int nreturn_vals;*/
  1230.  
  1231.     if (!PyArg_ParseTuple(args, ":enable_undo"))
  1232.     return NULL;
  1233.     /*return_vals = gimp_run_procedure("gimp_undo_push_group_start",
  1234.                      &nreturn_vals, GIMP_PDB_IMAGE, self->ID,
  1235.                      GIMP_PDB_END);
  1236.     gimp_destroy_params(return_vals, nreturn_vals);*/
  1237.     gimp_image_undo_enable(self->ID);
  1238.     Py_INCREF(Py_None);
  1239.     return Py_None;
  1240. }
  1241.  
  1242.  
  1243. static PyObject *
  1244. img_flatten(self, args)
  1245.      imgobject *self;
  1246.      PyObject *args;
  1247. {
  1248.     if (!PyArg_ParseTuple(args, ":flatten"))
  1249.     return NULL;
  1250.     gimp_image_flatten(self->ID);
  1251.     Py_INCREF(Py_None);
  1252.     return Py_None;
  1253. }
  1254.  
  1255.  
  1256. static PyObject *
  1257. img_lower_channel(self, args)
  1258.      imgobject *self;
  1259.      PyObject *args;
  1260. {
  1261.     chnobject *chn;
  1262.     if (!PyArg_ParseTuple(args, "O!:lower_channel", &Chntype, &chn))
  1263.     return NULL;
  1264.     gimp_image_lower_channel(self->ID, chn->ID);
  1265.     Py_INCREF(Py_None);
  1266.     return Py_None;
  1267. }
  1268.  
  1269.  
  1270. static PyObject *
  1271. img_lower_layer(self, args)
  1272.      imgobject *self;
  1273.      PyObject *args;
  1274. {
  1275.     layobject *lay;
  1276.     if (!PyArg_ParseTuple(args, "O!:lower_layer", &Laytype, &lay))
  1277.     return NULL;
  1278.     gimp_image_lower_layer(self->ID, lay->ID);
  1279.     Py_INCREF(Py_None);
  1280.     return Py_None;
  1281. }
  1282.  
  1283.  
  1284. static PyObject *
  1285. img_merge_visible_layers(self, args)
  1286.      imgobject *self;
  1287.      PyObject *args;
  1288. {
  1289.     gint32 id;
  1290.     int merge;
  1291.     if (!PyArg_ParseTuple(args, "i:merge_visible_layers", &merge))
  1292.     return NULL;
  1293.     id = gimp_image_merge_visible_layers(self->ID, merge);
  1294.     if (id == -1) {
  1295.     PyErr_SetString(ErrorObject, "Can't merge layers.");
  1296.     return NULL;
  1297.     }
  1298.     return (PyObject *)newlayobject(id);
  1299. }
  1300.  
  1301.  
  1302. static PyObject *
  1303. img_pick_correlate_layer(self, args)
  1304.      imgobject *self;
  1305.      PyObject *args;
  1306. {
  1307.     int x,y;
  1308.     gint32 id;
  1309.     
  1310.     if (!PyArg_ParseTuple(args, "ii:pick_correlate_layer", &x, &y))
  1311.     return NULL;
  1312.     id = gimp_image_pick_correlate_layer(self->ID, x, y);
  1313.     if (id == -1) {
  1314.     Py_INCREF(Py_None);
  1315.     return Py_None;
  1316.     }
  1317.     return (PyObject *)newlayobject(id);
  1318. }
  1319.  
  1320.  
  1321. static PyObject *
  1322. img_raise_channel(self, args)
  1323.      imgobject *self;
  1324.      PyObject *args;
  1325. {
  1326.     chnobject *chn;
  1327.     
  1328.     if (!PyArg_ParseTuple(args, "O!:raise_channel", &Chntype, &chn))
  1329.     return NULL;
  1330.     gimp_image_raise_channel(self->ID, chn->ID);
  1331.     Py_INCREF(Py_None);
  1332.     return Py_None;
  1333. }
  1334.  
  1335.  
  1336. static PyObject *
  1337. img_raise_layer(self, args)
  1338.      imgobject *self;
  1339.      PyObject *args;
  1340. {
  1341.     layobject *lay;
  1342.     if (!PyArg_ParseTuple(args, "O!:raise_layer", &Laytype, &lay))
  1343.     return NULL;
  1344.     gimp_image_raise_layer(self->ID, lay->ID);
  1345.     Py_INCREF(Py_None);
  1346.     return Py_None;
  1347. }
  1348.  
  1349.  
  1350. static PyObject *
  1351. img_remove_channel(self, args)
  1352.      imgobject *self;
  1353.      PyObject *args;
  1354. {
  1355.     chnobject *chn;
  1356.     if (!PyArg_ParseTuple(args, "O!:remove_channel", &Chntype, &chn))
  1357.     return NULL;
  1358.     gimp_image_remove_channel(self->ID, chn->ID);
  1359.     Py_INCREF(Py_None);
  1360.     return Py_None;
  1361. }
  1362.  
  1363.  
  1364. static PyObject *
  1365. img_remove_layer(self, args)
  1366.      imgobject *self;
  1367.      PyObject *args;
  1368. {
  1369.     layobject *lay;
  1370.     if (!PyArg_ParseTuple(args, "O!:remove_layer", &Laytype, &lay))
  1371.     return NULL;
  1372.     gimp_image_remove_layer(self->ID, lay->ID);
  1373.     Py_INCREF(Py_None);
  1374.     return Py_None;
  1375. }
  1376.  
  1377.  
  1378. static PyObject *
  1379. img_remove_layer_mask(self, args)
  1380.      imgobject *self;
  1381.      PyObject *args;
  1382. {
  1383.     layobject *lay;
  1384.     int mode;
  1385.     if (!PyArg_ParseTuple(args, "O!i:remove_layer_mask", &Laytype, &lay,
  1386.               &mode))
  1387.     return NULL;
  1388.     gimp_image_remove_layer_mask(self->ID, lay->ID, mode);
  1389.     Py_INCREF(Py_None);
  1390.     return Py_None;
  1391. }
  1392.  
  1393.  
  1394. static PyObject *
  1395. img_resize(self, args)
  1396.      imgobject *self;
  1397.      PyObject *args;
  1398. {
  1399.     unsigned int new_w, new_h;
  1400.     int offs_x, offs_y;
  1401.     if (!PyArg_ParseTuple(args, "iiii:resize", &new_w, &new_h,
  1402.               &offs_x, &offs_y))
  1403.     return NULL;
  1404.     gimp_image_resize(self->ID, new_w, new_h, offs_x, offs_y);
  1405.     Py_INCREF(Py_None);
  1406.     return Py_None;
  1407. }
  1408.  
  1409.  
  1410. static PyObject *
  1411. img_get_component_active(self, args)
  1412.      imgobject *self;
  1413.      PyObject *args;
  1414. {
  1415.     int comp;
  1416.     if (!PyArg_ParseTuple(args, "i:get_component_active", &comp))
  1417.     return NULL;
  1418.     return PyInt_FromLong((long)
  1419.               gimp_image_get_component_active(self->ID, comp));
  1420. }
  1421.  
  1422.  
  1423. static PyObject *
  1424. img_get_component_visible(self, args)
  1425.      imgobject *self;
  1426.      PyObject *args;
  1427. {
  1428.     int comp;
  1429.     
  1430.     if (!PyArg_ParseTuple(args, "i:get_component_visible", &comp))
  1431.     return NULL;
  1432.     
  1433.     return PyInt_FromLong((long)
  1434.               gimp_image_get_component_visible(self->ID, comp));
  1435. }
  1436.  
  1437.  
  1438. static PyObject *
  1439. img_set_component_active(self, args)
  1440.      imgobject *self;
  1441.      PyObject *args;
  1442. {
  1443.     int comp, a;
  1444.     if (!PyArg_ParseTuple(args, "ii:set_component_active", &comp, &a))
  1445.     return NULL;
  1446.     gimp_image_set_component_active(self->ID, comp, a);
  1447.     Py_INCREF(Py_None);
  1448.     return Py_None;
  1449. }
  1450.  
  1451.  
  1452. static PyObject *
  1453. img_set_component_visible(self, args)
  1454.      imgobject *self;
  1455.      PyObject *args;
  1456. {
  1457.     int comp, v;
  1458.     if (!PyArg_ParseTuple(args, "ii:set_component_visible", &comp, &v))
  1459.     return NULL;
  1460.     gimp_image_set_component_visible(self->ID, comp, v);
  1461.     Py_INCREF(Py_None);
  1462.     return Py_None;
  1463. }
  1464.  
  1465. #ifdef GIMP_HAVE_PARASITES
  1466. static PyObject *
  1467. img_parasite_find(self, args)
  1468.      imgobject *self;
  1469.      PyObject *args;
  1470. {
  1471.     char *name;
  1472.     if (!PyArg_ParseTuple(args, "s:parasite_find", &name))
  1473.     return NULL;
  1474.     return (PyObject *)newparaobject(gimp_image_parasite_find(self->ID, name));
  1475. }
  1476.  
  1477. static PyObject *
  1478. img_parasite_attach(self, args)
  1479.      imgobject *self;
  1480.      PyObject *args;
  1481. {
  1482.     paraobject *parasite;
  1483.     if (!PyArg_ParseTuple(args, "O!:parasite_attach", &Paratype, ¶site))
  1484.     return NULL;
  1485.     gimp_image_parasite_attach(self->ID, parasite->para);
  1486.     Py_INCREF(Py_None);
  1487.     return Py_None;
  1488. }
  1489.  
  1490. static PyObject *
  1491. img_attach_new_parasite(self, args)
  1492.      imgobject *self;
  1493.      PyObject *args;
  1494. {
  1495.     char *name, *data;
  1496.     int flags, size;
  1497.     if (!PyArg_ParseTuple(args, "sis#:attach_new_parasite", &name, &flags,
  1498.               &data, &size))
  1499.     return NULL;
  1500.     gimp_image_attach_new_parasite(self->ID, name, flags, size, data);
  1501.     Py_INCREF(Py_None);
  1502.     return Py_None;
  1503. }
  1504.  
  1505. static PyObject *
  1506. img_parasite_detach(self, args)
  1507.      imgobject *self;
  1508.      PyObject *args;
  1509. {
  1510.     char *name;
  1511.     if (!PyArg_ParseTuple(args, "s:parasite_detach", &name))
  1512.     return NULL;
  1513.     gimp_image_parasite_detach(self->ID, name);
  1514.     Py_INCREF(Py_None);
  1515.     return Py_None;
  1516. }
  1517.  
  1518. #endif
  1519. #ifdef GIMP_HAVE_RESOLUTION_INFO
  1520. /* gimp_image_set_resolution
  1521.  * gimp_image_get_resolution
  1522.  * gimp_set_unit
  1523.  * gimp_get_unit
  1524.  */
  1525. #endif
  1526. #if GIMP_CHECK_VERSION(1,1,0)
  1527. static PyObject *
  1528. img_get_layer_by_tattoo(self, args)
  1529.      imgobject *self;
  1530.      PyObject *args;
  1531. {
  1532.     int tattoo;
  1533.     if (!PyArg_ParseTuple(args, "i:get_layer_by_tattoo", &tattoo))
  1534.     return NULL;
  1535.     return (PyObject *)newlayobject(gimp_image_get_layer_by_tattoo(self->ID,
  1536.                                    tattoo));
  1537. }
  1538.  
  1539. static PyObject *
  1540. img_get_channel_by_tattoo(self, args)
  1541.      imgobject *self;
  1542.      PyObject *args;
  1543. {
  1544.     int tattoo;
  1545.     if (!PyArg_ParseTuple(args, "i:get_channel_by_tattoo", &tattoo))
  1546.     return NULL;
  1547.     return (PyObject *)newchnobject(gimp_image_get_channel_by_tattoo(self->ID,
  1548.                                      tattoo));
  1549. }
  1550.  
  1551. static PyObject *
  1552. img_add_hguide(self, args)
  1553.      imgobject *self;
  1554.      PyObject *args;
  1555. {
  1556.     int ypos;
  1557.     if (!PyArg_ParseTuple(args, "i:add_hguide", &ypos))
  1558.     return NULL;
  1559.     gimp_image_add_hguide(self->ID, ypos);
  1560.     Py_INCREF(Py_None);
  1561.     return Py_None;
  1562. }
  1563.  
  1564. static PyObject *
  1565. img_add_vguide(self, args)
  1566.      imgobject *self;
  1567.      PyObject *args;
  1568. {
  1569.     int xpos;
  1570.     if (!PyArg_ParseTuple(args, "i:add_vguide", &xpos))
  1571.     return NULL;
  1572.     gimp_image_add_vguide(self->ID, xpos);
  1573.     Py_INCREF(Py_None);
  1574.     return Py_None;
  1575. }
  1576.  
  1577. static PyObject *
  1578. img_delete_guide(self, args)
  1579.      imgobject *self;
  1580.      PyObject *args;
  1581. {
  1582.     int guide;
  1583.     if (!PyArg_ParseTuple(args, "i:delete_guide", &guide))
  1584.     return NULL;
  1585.     gimp_image_delete_guide(self->ID, guide);
  1586.     Py_INCREF(Py_None);
  1587.     return Py_None;
  1588. }
  1589.  
  1590. static PyObject *
  1591. img_find_next_guide(self, args)
  1592.      imgobject *self;
  1593.      PyObject *args;
  1594. {
  1595.     int guide;
  1596.     if (!PyArg_ParseTuple(args, "i:find_next_guide", &guide))
  1597.     return NULL;
  1598.     return PyInt_FromLong(gimp_image_find_next_guide(self->ID, guide));
  1599. }
  1600.  
  1601. static PyObject *
  1602. img_get_guide_orientation(self, args)
  1603.      imgobject *self;
  1604.      PyObject *args;
  1605. {
  1606.     int guide;
  1607.     if (!PyArg_ParseTuple(args, "i:get_guide_orientation", &guide))
  1608.     return NULL;
  1609.     return PyInt_FromLong(gimp_image_get_guide_orientation(self->ID, guide));
  1610. }
  1611.  
  1612. static PyObject *
  1613. img_get_guide_position(self, args)
  1614.      imgobject *self;
  1615.      PyObject *args;
  1616. {
  1617.     int guide;
  1618.     if (!PyArg_ParseTuple(args, "i:get_guide_position", &guide))
  1619.     return NULL;
  1620.     return PyInt_FromLong(gimp_image_get_guide_position(self->ID, guide));
  1621. }
  1622. #endif
  1623.  
  1624. static struct PyMethodDef img_methods[] = {
  1625.     {"add_channel",    (PyCFunction)img_add_channel,    METH_VARARGS},
  1626.     {"add_layer",    (PyCFunction)img_add_layer,    METH_VARARGS},
  1627.     {"add_layer_mask",    (PyCFunction)img_add_layer_mask,    METH_VARARGS},
  1628.     {"clean_all",    (PyCFunction)img_clean_all,    METH_VARARGS},
  1629.     {"disable_undo",    (PyCFunction)img_disable_undo,    METH_VARARGS},
  1630.     {"enable_undo",    (PyCFunction)img_enable_undo,    METH_VARARGS},
  1631.     {"flatten",    (PyCFunction)img_flatten,    METH_VARARGS},
  1632.     {"lower_channel",    (PyCFunction)img_lower_channel,    METH_VARARGS},
  1633.     {"lower_layer",    (PyCFunction)img_lower_layer,    METH_VARARGS},
  1634.     {"merge_visible_layers",    (PyCFunction)img_merge_visible_layers,    METH_VARARGS},
  1635.     {"pick_correlate_layer",    (PyCFunction)img_pick_correlate_layer,    METH_VARARGS},
  1636.     {"raise_channel",    (PyCFunction)img_raise_channel,    METH_VARARGS},
  1637.     {"raise_layer",    (PyCFunction)img_raise_layer,    METH_VARARGS},
  1638.     {"remove_channel",    (PyCFunction)img_remove_channel,    METH_VARARGS},
  1639.     {"remove_layer",    (PyCFunction)img_remove_layer,    METH_VARARGS},
  1640.     {"remove_layer_mask",    (PyCFunction)img_remove_layer_mask,    METH_VARARGS},
  1641.     {"resize",    (PyCFunction)img_resize,    METH_VARARGS},
  1642.     {"get_component_active",    (PyCFunction)img_get_component_active,    METH_VARARGS},
  1643.     {"get_component_visible",    (PyCFunction)img_get_component_visible,    METH_VARARGS},
  1644.     {"set_component_active",    (PyCFunction)img_set_component_active,    METH_VARARGS},
  1645.     {"set_component_visible",    (PyCFunction)img_set_component_visible,    METH_VARARGS},
  1646. #ifdef GIMP_HAVE_PARASITES
  1647.     {"parasite_find",       (PyCFunction)img_parasite_find,      METH_VARARGS},
  1648.     {"parasite_attach",     (PyCFunction)img_parasite_attach,    METH_VARARGS},
  1649.     {"attach_new_parasite", (PyCFunction)img_attach_new_parasite,METH_VARARGS},
  1650.     {"parasite_detach",     (PyCFunction)img_parasite_detach,    METH_VARARGS},
  1651. #endif
  1652. #if GIMP_CHECK_VERSION(1,1,0)
  1653.     {"get_layer_by_tattoo",(PyCFunction)img_get_layer_by_tattoo,METH_VARARGS},
  1654.     {"get_channel_by_tattoo",(PyCFunction)img_get_channel_by_tattoo,METH_VARARGS},
  1655.     {"add_hguide", (PyCFunction)img_add_hguide, METH_VARARGS},
  1656.     {"add_vguide", (PyCFunction)img_add_vguide, METH_VARARGS},
  1657.     {"delete_guide", (PyCFunction)img_delete_guide, METH_VARARGS},
  1658.     {"find_next_guide", (PyCFunction)img_find_next_guide, METH_VARARGS},
  1659.     {"get_guide_orientation",(PyCFunction)img_get_guide_orientation,METH_VARARGS},
  1660.     {"get_guide_position", (PyCFunction)img_get_guide_position, METH_VARARGS},
  1661. #endif
  1662.     {NULL,        NULL}        /* sentinel */
  1663. };
  1664.  
  1665. /* ---------- */
  1666.  
  1667.  
  1668. static imgobject *
  1669. newimgobject(gint32 ID)
  1670. {
  1671.     imgobject *self;
  1672.  
  1673.     if (ID == -1) {
  1674.     Py_INCREF(Py_None);
  1675.     return (imgobject *)Py_None;
  1676.     }
  1677.     self = PyObject_NEW(imgobject, &Imgtype);
  1678.     if (self == NULL)
  1679.     return NULL;
  1680.     self->ID = ID;
  1681.     return self;
  1682. }
  1683.  
  1684.  
  1685. static void
  1686. img_dealloc(self)
  1687.      imgobject *self;
  1688. {
  1689.     /* XXXX Add your own cleanup code here */
  1690.     PyMem_DEL(self);
  1691. }
  1692.  
  1693. static PyObject *
  1694. img_getattr(self, name)
  1695.      imgobject *self;
  1696.      char *name;
  1697. {
  1698.     gint32 *a, id;
  1699.     guchar *c;
  1700.     int n, i;
  1701.     PyObject *ret;
  1702.     if (!strcmp(name, "__members__"))
  1703.     return Py_BuildValue(
  1704. #ifdef GIMP_HAVE_RESOLUTION_INFO
  1705.                  "[ssssssssssss]",
  1706. #else
  1707.                  "[ssssssssss]",
  1708. #endif
  1709.                  "ID", "active_channel",
  1710.                  "active_layer", "base_type", "channels", "cmap",
  1711.                  "filename", "floating_selection",
  1712.                  "height", "layers",
  1713. #ifdef GIMP_HAVE_RESOLUTION_INFO
  1714.                  "resolution",
  1715. #endif
  1716.                  "selection",
  1717. #ifdef GIMP_HAVE_RESOLUTION_INFO
  1718.                  "unit",
  1719. #endif
  1720.                  "width");
  1721.     if (!strcmp(name, "ID"))
  1722.     return PyInt_FromLong(self->ID);
  1723.     if (!strcmp(name, "active_channel")) {
  1724.     id = gimp_image_get_active_channel(self->ID);
  1725.     if (id == -1) {
  1726.         Py_INCREF(Py_None);
  1727.         return Py_None;
  1728.     }
  1729.     return (PyObject *)newchnobject(id);
  1730.     }
  1731.     if (!strcmp(name, "active_layer")) {
  1732.     id = gimp_image_get_active_layer(self->ID);
  1733.     if (id == -1) {
  1734.         Py_INCREF(Py_None);
  1735.         return Py_None;
  1736.     }
  1737.     return (PyObject *)newlayobject(id);
  1738.     }
  1739.     if (!strcmp(name, "base_type"))
  1740.     return PyInt_FromLong(gimp_image_base_type(self->ID));
  1741.     if (!strcmp(name, "channels")) {
  1742.     a = gimp_image_get_channels(self->ID, &n);
  1743.     ret = PyList_New(n);
  1744.     for (i = 0; i < n; i++)
  1745.         PyList_SetItem(ret, i, (PyObject *)newchnobject(a[i]));
  1746.     return ret;
  1747.     }
  1748.     if (!strcmp(name, "cmap")) {
  1749.     c = gimp_image_get_cmap(self->ID, &n);
  1750.     return PyString_FromStringAndSize(c, n * 3);
  1751.     }
  1752.     if (!strcmp(name, "filename"))
  1753.     return PyString_FromString(gimp_image_get_filename(self->ID));
  1754.     if (!strcmp(name, "floating_selection")) {
  1755.     id = gimp_image_floating_selection(self->ID);
  1756.     if (id == -1) {
  1757.         Py_INCREF(Py_None);
  1758.         return Py_None;
  1759.     }
  1760.     return (PyObject *)newlayobject(id);
  1761.     }
  1762.     if (!strcmp(name, "layers")) {
  1763.     a = gimp_image_get_layers(self->ID, &n);
  1764.     ret = PyList_New(n);
  1765.     for (i = 0; i < n; i++)
  1766.         PyList_SetItem(ret, i, (PyObject *)newlayobject(a[i]));
  1767.     return ret;
  1768.     }
  1769.     if (!strcmp(name, "selection"))
  1770.     return (PyObject *)newchnobject(
  1771.                     gimp_image_get_selection(self->ID));
  1772.     if (!strcmp(name, "height"))
  1773.     return PyInt_FromLong(gimp_image_height(self->ID));
  1774.     if (!strcmp(name, "width"))
  1775.     return PyInt_FromLong(gimp_image_width(self->ID));
  1776.  
  1777. #ifdef GIMP_HAVE_RESOLUTION_INFO
  1778.     if (!strcmp(name, "resolution")) {
  1779.     double xres, yres;
  1780.     gimp_image_get_resolution(self->ID, &xres, &yres);
  1781.     return Py_BuildValue("(dd)", xres, yres);
  1782.     }
  1783.     if (!strcmp(name, "unit"))
  1784.     return PyInt_FromLong(gimp_image_get_unit(self->ID));
  1785. #endif
  1786.         
  1787.     return Py_FindMethod(img_methods, (PyObject *)self, name);
  1788. }
  1789.  
  1790. static int
  1791. img_setattr(self, name, v)
  1792.      imgobject *self;
  1793.      char *name;
  1794.      PyObject *v;
  1795. {
  1796.     if (v == NULL) {
  1797.     PyErr_SetString(PyExc_TypeError, "can not delete attributes.");
  1798.     return -1;
  1799.     }
  1800.     if (!strcmp(name, "active_channel")) {
  1801.     if (!chn_check(v)) {
  1802.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  1803.         return -1;
  1804.     }
  1805.     gimp_image_set_active_channel(self->ID, ((chnobject *)v)->ID);
  1806.     return 0;
  1807.     }
  1808.     if (!strcmp(name, "active_layer")) {
  1809.     if (!lay_check(v)) {
  1810.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  1811.         return -1;
  1812.     }
  1813.     gimp_image_set_active_layer(self->ID, ((layobject *)v)->ID);
  1814.     return 0;
  1815.     }
  1816.     if (!strcmp(name, "cmap")) {
  1817.     if (!PyString_Check(v)) {
  1818.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  1819.         return -1;
  1820.     }
  1821.     gimp_image_set_cmap(self->ID, PyString_AsString(v),
  1822.                 PyString_Size(v) / 3);
  1823.     return 0;
  1824.     }
  1825.     if (!strcmp(name, "filename")) {
  1826.     if (!PyString_Check(v)) {
  1827.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  1828.         return -1;
  1829.     }
  1830.     gimp_image_set_filename(self->ID, PyString_AsString(v));
  1831.     return 0;
  1832.     }
  1833. #ifdef GIMP_HAVE_RESOLUTION_INFO
  1834.     if (!strcmp(name, "resolution")) {
  1835.     PyObject *xo, *yo;
  1836.     if (!PySequence_Check(v) ||
  1837.         !PyFloat_Check(xo = PySequence_GetItem(v, 0)) ||
  1838.         !PyFloat_Check(yo = PySequence_GetItem(v, 1))) {
  1839.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  1840.         return -1;
  1841.     }
  1842.     gimp_image_set_resolution(self->ID, PyFloat_AsDouble(xo),
  1843.                   PyFloat_AsDouble(yo));
  1844.     }
  1845.     if (!strcmp(name, "unit")) {
  1846.     if (!PyInt_Check(v)) {
  1847.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  1848.         return -1;
  1849.     }
  1850.     gimp_image_set_unit(self->ID, PyInt_AsLong(v));
  1851.     }
  1852. #endif
  1853.     if (!strcmp(name, "channels") || !strcmp(name, "layers") ||
  1854.     !strcmp(name, "selection") || !strcmp(name, "height") ||
  1855.     !strcmp(name, "base_type") || !strcmp(name, "width") ||
  1856.     !strcmp(name, "floating_selection") || !strcmp(name, "ID")) {
  1857.     PyErr_SetString(PyExc_TypeError, "read-only attribute.");
  1858.     return -1;
  1859.     }
  1860.         
  1861.     return -1;
  1862. }
  1863.  
  1864. static PyObject *
  1865. img_repr(self)
  1866.      imgobject *self;
  1867. {
  1868.     PyObject *s;
  1869.  
  1870.     s = PyString_FromString("<image ");
  1871.     PyString_ConcatAndDel(&s, PyString_FromString(
  1872.                           gimp_image_get_filename(self->ID)));
  1873.     PyString_ConcatAndDel(&s, PyString_FromString(">"));
  1874.     return s;
  1875. }
  1876.  
  1877. static int
  1878. img_cmp(self, other)
  1879.      imgobject *self, *other;
  1880. {
  1881.     return self->ID - other->ID;
  1882. }
  1883.  
  1884. static PyTypeObject Imgtype = {
  1885.     PyObject_HEAD_INIT(&PyType_Type)
  1886.     0,                /*ob_size*/
  1887.     "Image",            /*tp_name*/
  1888.     sizeof(imgobject),        /*tp_basicsize*/
  1889.     0,                /*tp_itemsize*/
  1890.     /* methods */
  1891.     (destructor)img_dealloc,    /*tp_dealloc*/
  1892.     (printfunc)0,        /*tp_print*/
  1893.     (getattrfunc)img_getattr,    /*tp_getattr*/
  1894.     (setattrfunc)img_setattr,    /*tp_setattr*/
  1895.     (cmpfunc)img_cmp,        /*tp_compare*/
  1896.     (reprfunc)img_repr,        /*tp_repr*/
  1897.     &gobj_as_number,            /*tp_as_number*/
  1898.     0,        /*tp_as_sequence*/
  1899.     0,        /*tp_as_mapping*/
  1900.     (hashfunc)0,        /*tp_hash*/
  1901.     (ternaryfunc)0,        /*tp_call*/
  1902.     (reprfunc)0,        /*tp_str*/
  1903.  
  1904.     /* Space for future expansion */
  1905.     0L,0L,0L,0L,
  1906.     NULL /* Documentation string */
  1907. };
  1908.  
  1909. /* End of code for Image objects */
  1910. /* -------------------------------------------------------- */
  1911.  
  1912.  
  1913. static struct PyMethodDef disp_methods[] = {
  1914.  
  1915.     {NULL,        NULL}        /* sentinel */
  1916. };
  1917.  
  1918. /* ---------- */
  1919.  
  1920.  
  1921. static dispobject *
  1922. newdispobject(gint32 ID)
  1923. {
  1924.     dispobject *self;
  1925.     
  1926.     self = PyObject_NEW(dispobject, &Disptype);
  1927.     if (self == NULL)
  1928.     return NULL;
  1929.     self->ID = ID;
  1930.     return self;
  1931. }
  1932.  
  1933.  
  1934. static void
  1935. disp_dealloc(self)
  1936.      dispobject *self;
  1937. {
  1938.     PyMem_DEL(self);
  1939. }
  1940.  
  1941. static PyObject *
  1942. disp_getattr(self, name)
  1943.      dispobject *self;
  1944.      char *name;
  1945. {
  1946.     return Py_FindMethod(disp_methods, (PyObject *)self, name);
  1947. }
  1948.  
  1949. static PyObject *
  1950. disp_repr(self)
  1951.      dispobject *self;
  1952. {
  1953.     PyObject *s;
  1954.  
  1955.     s = PyString_FromString("<display>");
  1956.     return s;
  1957. }
  1958.  
  1959. static PyTypeObject Disptype = {
  1960.     PyObject_HEAD_INIT(&PyType_Type)
  1961.     0,                /*ob_size*/
  1962.     "Display",            /*tp_name*/
  1963.     sizeof(dispobject),        /*tp_basicsize*/
  1964.     0,                /*tp_itemsize*/
  1965.     /* methods */
  1966.     (destructor)disp_dealloc,    /*tp_dealloc*/
  1967.     (printfunc)0,        /*tp_print*/
  1968.     (getattrfunc)disp_getattr,    /*tp_getattr*/
  1969.     (setattrfunc)0,    /*tp_setattr*/
  1970.     (cmpfunc)0,        /*tp_compare*/
  1971.     (reprfunc)disp_repr,        /*tp_repr*/
  1972.     0,            /*tp_as_number*/
  1973.     0,        /*tp_as_sequence*/
  1974.     0,        /*tp_as_mapping*/
  1975.     (hashfunc)0,        /*tp_hash*/
  1976.     (ternaryfunc)0,        /*tp_call*/
  1977.     (reprfunc)0,        /*tp_str*/
  1978.  
  1979.     /* Space for future expansion */
  1980.     0L,0L,0L,0L,
  1981.     NULL /* Documentation string */
  1982. };
  1983.  
  1984. /* End of code for Display objects */
  1985. /* -------------------------------------------------------- */
  1986.  
  1987.  
  1988. static void
  1989. ensure_drawable(self)
  1990.      drwobject *self;
  1991. {
  1992.     if (!self->drawable)
  1993.     self->drawable = gimp_drawable_get(self->ID);
  1994. }
  1995.  
  1996. static PyObject *
  1997. drw_flush(self, args)
  1998.      drwobject *self;
  1999.      PyObject *args;
  2000. {
  2001.     if (!PyArg_ParseTuple(args, ":flush"))
  2002.     return NULL;
  2003.     ensure_drawable(self);
  2004.     gimp_drawable_flush(self->drawable);
  2005.     Py_INCREF(Py_None);
  2006.     return Py_None;
  2007. }
  2008.  
  2009.  
  2010. static PyObject *
  2011. drw_update(self, args)
  2012.      drwobject *self;
  2013.      PyObject *args;
  2014. {
  2015.     int x, y;
  2016.     unsigned int w, h;
  2017.     if (!PyArg_ParseTuple(args, "iiii:update", &x, &y, &w, &h))
  2018.     return NULL;
  2019.     gimp_drawable_update(self->ID, x, y, w, h);
  2020.     Py_INCREF(Py_None);
  2021.     return Py_None;
  2022. }
  2023.  
  2024.  
  2025. static PyObject *
  2026. drw_merge_shadow(self, args)
  2027.      drwobject *self;
  2028.      PyObject *args;
  2029. {
  2030.     int u;
  2031.     if (!PyArg_ParseTuple(args, "i:merge_shadow", &u))
  2032.     return NULL;
  2033.     gimp_drawable_merge_shadow(self->ID, u);
  2034.     Py_INCREF(Py_None);
  2035.     return Py_None;
  2036. }
  2037.  
  2038.  
  2039. static PyObject *
  2040. drw_fill(self, args)
  2041.      drwobject *self;
  2042.      PyObject *args;
  2043. {
  2044.     int f;
  2045.     if (!PyArg_ParseTuple(args, "i:fill", &f))
  2046.     return NULL;
  2047.     gimp_drawable_fill(self->ID, f);
  2048.     Py_INCREF(Py_None);
  2049.     return Py_None;
  2050. }
  2051.  
  2052.  
  2053. static PyObject *
  2054. drw_get_tile(self, args)
  2055.      drwobject *self;
  2056.      PyObject *args;
  2057. {
  2058.     GimpTile *t;
  2059.     int shadow, r, c;
  2060.     if (!PyArg_ParseTuple(args, "iii:get_tile", &shadow, &r, &c))
  2061.     return NULL;
  2062.     ensure_drawable(self);
  2063.     t = gimp_drawable_get_tile(self->drawable, shadow, r, c);
  2064.     return (PyObject *)newtileobject(t, self);
  2065. }
  2066.  
  2067. static PyObject *
  2068. drw_get_tile2(self, args)
  2069.      drwobject *self;
  2070.      PyObject *args;
  2071. {
  2072.     GimpTile *t;
  2073.     int shadow, x, y;
  2074.     if (!PyArg_ParseTuple(args, "iii:get_tile2", &shadow, &x ,&y))
  2075.     return NULL;
  2076.     ensure_drawable(self);
  2077.     t = gimp_drawable_get_tile2(self->drawable, shadow, x, y);
  2078.     return (PyObject *)newtileobject(t, self);
  2079. }
  2080.  
  2081. static PyObject *
  2082. drw_get_pixel_rgn(self, args)
  2083.      drwobject *self;
  2084.      PyObject *args;
  2085. {
  2086.     int x, y, w, h, dirty = 1, shadow = 0;
  2087.     if (!PyArg_ParseTuple(args, "iiii|ii:get_pixel_rgn", &x,&y,
  2088.               &w,&h, &dirty,&shadow))
  2089.     return NULL;
  2090.     ensure_drawable(self);
  2091.     return (PyObject *)newprobject(self, x, y, w, h,
  2092.                    dirty, shadow);
  2093. }
  2094.  
  2095. #ifdef GIMP_HAVE_PARASITES
  2096. static PyObject *
  2097. drw_parasite_find(self, args)
  2098.      drwobject *self;
  2099.      PyObject *args;
  2100. {
  2101.     char *name;
  2102.     if (!PyArg_ParseTuple(args, "s:parasite_find", &name))
  2103.     return NULL;
  2104.     return (PyObject *)newparaobject(gimp_drawable_parasite_find(self->ID,
  2105.                                  name));
  2106. }
  2107.  
  2108. static PyObject *
  2109. drw_parasite_attach(self, args)
  2110.      drwobject *self;
  2111.      PyObject *args;
  2112. {
  2113.     paraobject *parasite;
  2114.     if (!PyArg_ParseTuple(args, "O!:parasite_attach", &Paratype, ¶site))
  2115.     return NULL;
  2116.     gimp_drawable_parasite_attach(self->ID, parasite->para);
  2117.     Py_INCREF(Py_None);
  2118.     return Py_None;
  2119. }
  2120.  
  2121. static PyObject *
  2122. drw_attach_new_parasite(self, args)
  2123.      drwobject *self;
  2124.      PyObject *args;
  2125. {
  2126.     char *name, *data;
  2127.     int flags, size;
  2128.     if (!PyArg_ParseTuple(args, "sis#:attach_new_parasite", &name, &flags,
  2129.               &data, &size))
  2130.     return NULL;
  2131.     gimp_drawable_attach_new_parasite(self->ID, name, flags, size, data);
  2132.     Py_INCREF(Py_None);
  2133.     return Py_None;
  2134. }
  2135.  
  2136. static PyObject *
  2137. drw_parasite_detach(self, args)
  2138.      drwobject *self;
  2139.      PyObject *args;
  2140. {
  2141.     char *name;
  2142.     if (!PyArg_ParseTuple(args, "s:detach_parasite", &name))
  2143.     return NULL;
  2144.     gimp_drawable_parasite_detach(self->ID, name);
  2145.     Py_INCREF(Py_None);
  2146.     return Py_None;
  2147. }
  2148. #endif
  2149.  
  2150. /* for inclusion with the methods of layer and channel objects */
  2151. #ifdef GIMP_HAVE_PARASITES
  2152. #define drw_methods() \
  2153.     {"flush",    (PyCFunction)drw_flush,    METH_VARARGS}, \
  2154.     {"update",    (PyCFunction)drw_update,    METH_VARARGS}, \
  2155.     {"merge_shadow",    (PyCFunction)drw_merge_shadow,    METH_VARARGS}, \
  2156.     {"fill",    (PyCFunction)drw_fill,    METH_VARARGS}, \
  2157.     {"get_tile",    (PyCFunction)drw_get_tile,    METH_VARARGS}, \
  2158.     {"get_tile2",    (PyCFunction)drw_get_tile2,    METH_VARARGS}, \
  2159.     {"get_pixel_rgn", (PyCFunction)drw_get_pixel_rgn, METH_VARARGS}, \
  2160.     {"parasite_find",       (PyCFunction)drw_parasite_find, METH_VARARGS}, \
  2161.     {"parasite_attach",     (PyCFunction)drw_parasite_attach, METH_VARARGS},\
  2162.     {"attach_new_parasite",(PyCFunction)drw_attach_new_parasite,METH_VARARGS},\
  2163.     {"parasite_detach",     (PyCFunction)drw_parasite_detach, METH_VARARGS}
  2164. #else
  2165. #define drw_methods() \
  2166.     {"flush",    (PyCFunction)drw_flush,    METH_VARARGS}, \
  2167.     {"update",    (PyCFunction)drw_update,    METH_VARARGS}, \
  2168.     {"merge_shadow",    (PyCFunction)drw_merge_shadow,    METH_VARARGS}, \
  2169.     {"fill",    (PyCFunction)drw_fill,    METH_VARARGS}, \
  2170.     {"get_tile",    (PyCFunction)drw_get_tile,    METH_VARARGS}, \
  2171.     {"get_tile2",    (PyCFunction)drw_get_tile2,    METH_VARARGS}, \
  2172.     {"get_pixel_rgn", (PyCFunction)drw_get_pixel_rgn, METH_VARARGS}
  2173. #endif
  2174. /* ---------- */
  2175.  
  2176.  
  2177. static drwobject *
  2178. newdrwobject(GimpDrawable *d, gint32 ID)
  2179. {
  2180.     drwobject *self;
  2181.  
  2182.     if (d == NULL && ID == -1) {
  2183.     Py_INCREF(Py_None);
  2184.     return (drwobject *)Py_None;
  2185.     }
  2186.     if (d != NULL)
  2187.     ID = d->id;
  2188.     /* create the appropriate object type */
  2189.     if (gimp_drawable_is_layer(ID))
  2190.     self = newlayobject(ID);
  2191.     else
  2192.     self = newchnobject(ID);
  2193.  
  2194.     if (self == NULL)
  2195.     return NULL;
  2196.  
  2197.     self->drawable = d;
  2198.  
  2199.     return self;
  2200. }
  2201.  
  2202. /* End of code for Drawable objects */
  2203. /* -------------------------------------------------------- */
  2204.  
  2205.  
  2206. static PyObject *
  2207. lay_copy(self, args)
  2208.      layobject *self;
  2209.      PyObject *args;
  2210. {
  2211.     int add_alpha = 0, nreturn_vals;
  2212.     GimpParam *return_vals;
  2213.     gint32 id;
  2214.  
  2215.     /* start of long convoluted (working) layer_copy */
  2216.     if (!PyArg_ParseTuple(args, "|i:copy", &add_alpha))
  2217.     return NULL;
  2218.  
  2219.     return_vals = gimp_run_procedure("gimp_layer_copy",
  2220.                      &nreturn_vals, 
  2221.                      GIMP_PDB_LAYER, self->ID,
  2222.                      GIMP_PDB_INT32, add_alpha,
  2223.                      GIMP_PDB_END);
  2224.     if (return_vals[0].data.d_status != GIMP_PDB_SUCCESS) {
  2225.     PyErr_SetString(ErrorObject, "can't create new layer");
  2226.     return NULL;
  2227.     }    
  2228.     id = return_vals[1].data.d_layer;
  2229.     gimp_destroy_params(return_vals, nreturn_vals);
  2230.     return (PyObject *)newlayobject(id);
  2231.  
  2232.     /* This simple version of the code doesn't seem to work */
  2233.     /* return (PyObject *)newlayobject(gimp_layer_copy(self->ID));*/
  2234. }
  2235.  
  2236.  
  2237. static PyObject *
  2238. lay_add_alpha(self, args)
  2239.      layobject *self;
  2240.      PyObject *args;
  2241. {
  2242.     if (!PyArg_ParseTuple(args, ":add_alpha"))
  2243.     return NULL;
  2244.     gimp_layer_add_alpha(self->ID);
  2245.     Py_INCREF(Py_None);
  2246.     return Py_None;
  2247. }
  2248.  
  2249.  
  2250. static PyObject *
  2251. lay_create_mask(self, args)
  2252.      layobject *self;
  2253.      PyObject *args;
  2254. {
  2255.     int type;
  2256.  
  2257.     if (!PyArg_ParseTuple(args, "i:create_mask", &type))
  2258.     return NULL;
  2259.     return (PyObject *)newchnobject(gimp_layer_create_mask(self->ID,type));
  2260. }
  2261.  
  2262.  
  2263. static PyObject *
  2264. lay_resize(self, args)
  2265.      layobject *self;
  2266.      PyObject *args;
  2267. {
  2268.     unsigned int new_h, new_w;
  2269.     int offs_x, offs_y;
  2270.     if (!PyArg_ParseTuple(args, "iiii:resize", &new_w, &new_h,
  2271.               &offs_x, &offs_y))
  2272.     return NULL;
  2273.     gimp_layer_resize(self->ID, new_w, new_h, offs_x, offs_y);
  2274.     Py_INCREF(Py_None);
  2275.     return Py_None;
  2276. }
  2277.  
  2278.  
  2279. static PyObject *
  2280. lay_scale(self, args)
  2281.      layobject *self;
  2282.      PyObject *args;
  2283. {
  2284.     unsigned int new_w, new_h;
  2285.     int local_origin;
  2286.     if (!PyArg_ParseTuple(args, "iii:scale", &new_w, &new_h,
  2287.               &local_origin))
  2288.     return NULL;
  2289.     gimp_layer_scale(self->ID, new_w, new_h, local_origin);
  2290.     Py_INCREF(Py_None);
  2291.     return Py_None;
  2292. }
  2293.  
  2294.  
  2295. static PyObject *
  2296. lay_translate(self, args)
  2297.      layobject *self;
  2298.      PyObject *args;
  2299. {
  2300.     int offs_x, offs_y;
  2301.     if (!PyArg_ParseTuple(args, "ii:translate", &offs_x, &offs_y))
  2302.     return NULL;
  2303.     gimp_layer_translate(self->ID, offs_x, offs_y);
  2304.     Py_INCREF(Py_None);
  2305.     return Py_None;
  2306. }
  2307.  
  2308.  
  2309. static PyObject *
  2310. lay_set_offsets(self, args)
  2311.      layobject *self;
  2312.      PyObject *args;
  2313. {
  2314.     int offs_x, offs_y;
  2315.     if (!PyArg_ParseTuple(args, "ii:set_offsets", &offs_x, &offs_y))
  2316.     return NULL;
  2317.     gimp_layer_set_offsets(self->ID, offs_x, offs_y);
  2318.     Py_INCREF(Py_None);
  2319.     return Py_None;
  2320. }
  2321.  
  2322. #if GIMP_CHECK_VERSION(1,1,0)
  2323. static PyObject *
  2324. lay_get_tattoo(self, args)
  2325.      layobject *self;
  2326.      PyObject *args;
  2327. {
  2328.     if (!PyArg_ParseTuple(args, ":get_tattoo"))
  2329.     return NULL;
  2330.     return PyInt_FromLong(gimp_layer_get_tattoo(self->ID));
  2331. }
  2332. #endif
  2333.  
  2334. static struct PyMethodDef lay_methods[] = {
  2335.     {"copy",    (PyCFunction)lay_copy,    METH_VARARGS},
  2336.     {"add_alpha",    (PyCFunction)lay_add_alpha,    METH_VARARGS},
  2337.     {"create_mask",    (PyCFunction)lay_create_mask,    METH_VARARGS},
  2338.     {"resize",    (PyCFunction)lay_resize,    METH_VARARGS},
  2339.     {"scale",    (PyCFunction)lay_scale,    METH_VARARGS},
  2340.     {"translate",    (PyCFunction)lay_translate,    METH_VARARGS},
  2341.     {"set_offsets",    (PyCFunction)lay_set_offsets,    METH_VARARGS},
  2342. #if GIMP_CHECK_VERSION(1,1,0)
  2343.     {"get_tattoo",      (PyCFunction)lay_get_tattoo,    METH_VARARGS},
  2344. #endif
  2345.  
  2346.     drw_methods(), 
  2347.     {NULL,        NULL}        /* sentinel */
  2348. };
  2349.  
  2350. /* ---------- */
  2351.  
  2352.  
  2353. static layobject *
  2354. newlayobject(gint32 ID)
  2355. {
  2356.     layobject *self;
  2357.  
  2358.     if (ID == -1) {
  2359.     Py_INCREF(Py_None);
  2360.     return (layobject *)Py_None;
  2361.     }
  2362.     self = PyObject_NEW(layobject, &Laytype);
  2363.     if (self == NULL)
  2364.     return NULL;
  2365.     self->ID = ID;
  2366.     self->drawable = NULL;
  2367.     return self;
  2368. }
  2369.  
  2370.  
  2371. static void
  2372. lay_dealloc(self)
  2373.      layobject *self;
  2374. {
  2375.     if (self->drawable)
  2376.     gimp_drawable_detach(self->drawable);
  2377.     PyMem_DEL(self);
  2378. }
  2379.  
  2380. static PyObject *
  2381. lay_getattr(self, name)
  2382.      layobject *self;
  2383.      char *name;
  2384. {
  2385.     gint32 id;
  2386.     
  2387.     if (!strcmp(name, "__members__"))
  2388.     return Py_BuildValue("[ssssssssssssssssssss]", "ID", "apply_mask",
  2389.                  "bpp", "edit_mask", "has_alpha", "height",
  2390.                  "image", "is_color", "is_colour",
  2391.                  "is_floating_selection", "is_gray", "is_grey",
  2392.                  "is_indexed", "is_rgb", "mask", "mask_bounds",
  2393.                  "mode", "name", "offsets", "opacity",
  2394.                  "preserve_transparency", "show_mask", "type",
  2395.                  "visible", "width");
  2396.     if (!strcmp(name, "ID"))
  2397.     return PyInt_FromLong(self->ID);
  2398.     if (!strcmp(name, "bpp"))
  2399.     return PyInt_FromLong((long) gimp_drawable_bpp(self->ID));
  2400.     if (!strcmp(name, "has_alpha"))
  2401.     return PyInt_FromLong(gimp_drawable_has_alpha(self->ID));
  2402.     if (!strcmp(name, "height"))
  2403.     return PyInt_FromLong((long) gimp_drawable_height(self->ID));
  2404.     if (!strcmp(name, "image")) {
  2405.     id = gimp_layer_get_image_id(self->ID);
  2406.     if (id == -1) {
  2407.         Py_INCREF(Py_None);
  2408.         return Py_None;
  2409.     }
  2410.     return (PyObject *)newimgobject(id);
  2411.     }
  2412.     if (!strcmp(name, "is_color") || !strcmp(name, "is_colour") ||
  2413.     !strcmp(name, "is_rgb"))
  2414.     return PyInt_FromLong(gimp_drawable_is_rgb(self->ID));
  2415.     if (!strcmp(name, "is_floating_selection"))
  2416.     return PyInt_FromLong(
  2417.                   gimp_layer_is_floating_selection(self->ID));
  2418.     if (!strcmp(name, "is_gray") || !strcmp(name, "is_grey"))
  2419.     return PyInt_FromLong(gimp_drawable_is_gray(self->ID));
  2420.     if (!strcmp(name, "is_indexed"))
  2421.     return PyInt_FromLong(gimp_drawable_is_indexed(self->ID));
  2422.     if (!strcmp(name, "mask")) {
  2423.     id = gimp_layer_get_mask_id(self->ID);
  2424.     if (id == -1) {
  2425.         Py_INCREF(Py_None);
  2426.         return Py_None;
  2427.     }
  2428.     return (PyObject *)newchnobject(id);
  2429.     }
  2430.     if (!strcmp(name, "mask_bounds")) {
  2431.     gint x1, y1, x2, y2;
  2432.     gimp_drawable_mask_bounds(self->ID, &x1, &y1, &x2, &y2);
  2433.     return Py_BuildValue("(iiii)", x1, y1, x2, y2);
  2434.     }
  2435.     if (!strcmp(name, "apply_mask"))
  2436.     return PyInt_FromLong((long) gimp_layer_get_apply_mask(
  2437.                                    self->ID));
  2438.     if (!strcmp(name, "edit_mask"))
  2439.     return PyInt_FromLong((long) gimp_layer_get_edit_mask(
  2440.                                   self->ID));
  2441.     if (!strcmp(name, "mode"))
  2442.     return PyInt_FromLong((long) gimp_layer_get_mode(self->ID));
  2443.     if (!strcmp(name, "name"))
  2444.     return PyString_FromString(gimp_layer_get_name(self->ID));
  2445.     if (!strcmp(name, "offsets")) {
  2446.     gint x, y;
  2447.     gimp_drawable_offsets(self->ID, &x, &y);
  2448.     return Py_BuildValue("(ii)", x, y);
  2449.     }
  2450.     if (!strcmp(name, "opacity"))
  2451.     return PyFloat_FromDouble((double) gimp_layer_get_opacity(
  2452.                                   self->ID));
  2453.     if (!strcmp(name, "preserve_transparency"))
  2454.     return PyInt_FromLong((long)
  2455.                   gimp_layer_get_preserve_transparency(self->ID));
  2456.     if (!strcmp(name, "show_mask"))
  2457.     return PyInt_FromLong((long) gimp_layer_get_show_mask(
  2458.                                   self->ID));
  2459.     if (!strcmp(name, "type"))
  2460.     return PyInt_FromLong((long) gimp_drawable_type(self->ID));
  2461.     if (!strcmp(name, "visible"))
  2462.     return PyInt_FromLong((long) gimp_layer_get_visible(self->ID));
  2463.     if (!strcmp(name, "width"))
  2464.     return PyInt_FromLong((long) gimp_drawable_width(self->ID));
  2465.     return Py_FindMethod(lay_methods, (PyObject *)self, name);
  2466. }
  2467.  
  2468. static int
  2469. lay_setattr(self, name, v)
  2470.      layobject *self;
  2471.      char *name;
  2472.      PyObject *v;
  2473. {
  2474.     if (v == NULL) {
  2475.     PyErr_SetString(PyExc_TypeError, "can not delete attributes.");
  2476.     return -1;
  2477.     }
  2478.     /* Set attribute 'name' to value 'v'. v==NULL means delete */
  2479.     if (!strcmp(name, "apply_mask")) {
  2480.     if (!PyInt_Check(v)) {
  2481.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2482.         return -1;
  2483.     }
  2484.     gimp_layer_set_apply_mask(self->ID, PyInt_AsLong(v));
  2485.     return 0;
  2486.     }
  2487.     if (!strcmp(name, "edit_mask")) {
  2488.     if (!PyInt_Check(v)) {
  2489.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2490.         return -1;
  2491.     }
  2492.     gimp_layer_set_edit_mask(self->ID, PyInt_AsLong(v));
  2493.     return 0;
  2494.     }
  2495.     if (!strcmp(name, "mode")) {
  2496.     if (!PyInt_Check(v)) {
  2497.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2498.         return -1;
  2499.     }
  2500.     gimp_layer_set_mode(self->ID, (GimpLayerModeEffects)PyInt_AsLong(v));
  2501.     return 0;
  2502.     }
  2503.     if (!strcmp(name, "name")) {
  2504.     if (!PyString_Check(v)) {
  2505.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2506.         return -1;
  2507.     }
  2508.     gimp_layer_set_name(self->ID, PyString_AsString(v));
  2509.     return 0;
  2510.     }
  2511.     if (!strcmp(name, "opacity")) {
  2512.     if (!PyFloat_Check(v)) {
  2513.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2514.         return -1;
  2515.     }
  2516.     gimp_layer_set_opacity(self->ID, PyFloat_AsDouble(v));
  2517.     return 0;
  2518.     }
  2519.     if (!strcmp(name, "preserve_transparency")) {
  2520.     if (!PyInt_Check(v)) {
  2521.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2522.         return -1;
  2523.     }
  2524.     gimp_layer_set_preserve_transparency(self->ID,
  2525.                          PyInt_AsLong(v));
  2526.     return 0;
  2527.     }
  2528.     if (!strcmp(name, "show_mask")) {
  2529.     if (!PyInt_Check(v)) {
  2530.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2531.         return -1;
  2532.     }
  2533.     gimp_layer_set_show_mask(self->ID, PyInt_AsLong(v));
  2534.     return 0;
  2535.     }
  2536.     if (!strcmp(name, "visible")) {
  2537.     if (!PyInt_Check(v)) {
  2538.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2539.         return -1;
  2540.     }
  2541.     gimp_layer_set_visible(self->ID, PyInt_AsLong(v));
  2542.     return 0;
  2543.     }
  2544.     if (!strcmp(name, "ID") ||
  2545.     !strcmp(name, "bpp") || !strcmp(name, "height") ||
  2546.     !strcmp(name, "image") || !strcmp(name, "mask") ||
  2547.     !strcmp(name, "type") || !strcmp(name, "width") ||
  2548.     !strcmp(name, "is_floating_selection") ||
  2549.     !strcmp(name, "offsets") || !strcmp(name, "mask_bounds") ||
  2550.     !strcmp(name, "has_alpha") || !strcmp(name, "is_color") ||
  2551.     !strcmp(name, "is_colour") || !strcmp(name, "is_rgb") ||
  2552.     !strcmp(name, "is_gray") || !strcmp(name, "is_grey") ||
  2553.     !strcmp(name, "is_indexed") || !strcmp(name, "__members__")) {
  2554.     PyErr_SetString(PyExc_TypeError, "read-only attribute.");
  2555.     return -1;
  2556.     }
  2557.     return -1;
  2558. }
  2559.  
  2560. static PyObject *
  2561. lay_repr(self)
  2562.      layobject *self;
  2563. {
  2564.     PyObject *s;
  2565.  
  2566.     s = PyString_FromString("<layer ");
  2567.     PyString_ConcatAndDel(&s, PyString_FromString(
  2568.                           gimp_layer_get_name(self->ID)));
  2569.     PyString_ConcatAndDel(&s, PyString_FromString(">"));
  2570.     return s;
  2571. }
  2572.  
  2573. static int
  2574. lay_cmp(self, other)
  2575.      layobject *self, *other;
  2576. {
  2577.     return self->ID - other->ID;
  2578. }
  2579.  
  2580. static PyTypeObject Laytype = {
  2581.     PyObject_HEAD_INIT(&PyType_Type)
  2582.     0,                /*ob_size*/
  2583.     "Layer",            /*tp_name*/
  2584.     sizeof(layobject),        /*tp_basicsize*/
  2585.     0,                /*tp_itemsize*/
  2586.     /* methods */
  2587.     (destructor)lay_dealloc,    /*tp_dealloc*/
  2588.     (printfunc)0,        /*tp_print*/
  2589.     (getattrfunc)lay_getattr,    /*tp_getattr*/
  2590.     (setattrfunc)lay_setattr,    /*tp_setattr*/
  2591.     (cmpfunc)lay_cmp,        /*tp_compare*/
  2592.     (reprfunc)lay_repr,        /*tp_repr*/
  2593.     &gobj_as_number,            /*tp_as_number*/
  2594.     0,        /*tp_as_sequence*/
  2595.     0,        /*tp_as_mapping*/
  2596.     (hashfunc)0,        /*tp_hash*/
  2597.     (ternaryfunc)0,        /*tp_call*/
  2598.     (reprfunc)0,        /*tp_str*/
  2599.  
  2600.     /* Space for future expansion */
  2601.     0L,0L,0L,0L,
  2602.     NULL /* Documentation string */
  2603. };
  2604.  
  2605. /* End of code for Layer objects */
  2606. /* -------------------------------------------------------- */
  2607.  
  2608.  
  2609. static PyObject *
  2610. chn_copy(self, args)
  2611.      chnobject *self;
  2612.      PyObject *args;
  2613. {
  2614.     gint32 id;
  2615.     
  2616.     if (!PyArg_ParseTuple(args, ":copy"))
  2617.     return NULL;
  2618.     id = gimp_channel_copy(self->ID);
  2619.     if (id == -1) {
  2620.     PyErr_SetString(ErrorObject, "can't copy channel");
  2621.     return NULL;
  2622.     }
  2623.     return (PyObject *)newchnobject(id);
  2624. }
  2625.  
  2626. #if GIMP_CHECK_VERSION(1,1,0)
  2627. static PyObject *
  2628. chn_get_tattoo(self, args)
  2629.      chnobject *self;
  2630.      PyObject *args;
  2631. {
  2632.     if (!PyArg_ParseTuple(args, ":get_tattoo"))
  2633.     return NULL;
  2634.     return PyInt_FromLong(gimp_channel_get_tattoo(self->ID));
  2635. }
  2636. #endif
  2637.  
  2638. static struct PyMethodDef chn_methods[] = {
  2639.     {"copy",    (PyCFunction)chn_copy,    METH_VARARGS},
  2640. #if GIMP_CHECK_VERSION(1,1,0)
  2641.     {"get_tattoo", (PyCFunction)chn_get_tattoo, METH_VARARGS},
  2642. #endif
  2643.     drw_methods(),
  2644.     {NULL,        NULL}        /* sentinel */
  2645. };
  2646.  
  2647. /* ---------- */
  2648.  
  2649.  
  2650. static chnobject *
  2651. newchnobject(gint32 ID)
  2652. {
  2653.     chnobject *self;
  2654.  
  2655.     if (ID == -1) {
  2656.     Py_INCREF(Py_None);
  2657.     return (chnobject *)Py_None;
  2658.     }
  2659.     self = PyObject_NEW(chnobject, &Chntype);
  2660.     if (self == NULL)
  2661.     return NULL;
  2662.     self->ID = ID;
  2663.     self->drawable = NULL;
  2664.     return self;
  2665. }
  2666.  
  2667. static void
  2668. chn_dealloc(self)
  2669.      chnobject *self;
  2670. {
  2671.     if (self->drawable)
  2672.     gimp_drawable_detach(self->drawable);
  2673.     PyMem_DEL(self);
  2674. }
  2675.  
  2676. static PyObject *
  2677. chn_getattr(self, name)
  2678.      chnobject *self;
  2679.      char *name;
  2680. {
  2681.     unsigned char r, g, b;
  2682.     gint32 id;
  2683.     
  2684.     if (!strcmp(name, "__members__"))
  2685.     return Py_BuildValue("[ssssssssssssssssssssssss]", "ID",
  2686.                  "bpp", "color", "colour", "has_alpha", "height",
  2687.                  "image", "is_color", "is_colour", "is_gray",
  2688.                  "is_grey", "is_indexed", "is_layer_mask",
  2689.                  "is_rgb", "layer", "layer_mask", "mask_bounds",
  2690.                  "name", "offsets", "opacity", "show_masked",
  2691.                  "type", "visible", "width");
  2692.     if (!strcmp(name, "ID"))
  2693.     return PyInt_FromLong(self->ID);
  2694.     if (!strcmp(name, "bpp"))
  2695.     return PyInt_FromLong(gimp_drawable_bpp(self->ID));
  2696.     if (!strcmp(name, "color") || !strcmp(name, "colour")) {
  2697.     gimp_channel_get_color(self->ID, &r, &g, &b);
  2698.     return Py_BuildValue("(iii)", (long)r, (long)g, (long)b);
  2699.     }
  2700.     if (!strcmp(name, "has_alpha"))
  2701.     return PyInt_FromLong(gimp_drawable_has_alpha(self->ID));
  2702.     if (!strcmp(name, "height"))
  2703.     return PyInt_FromLong((long)gimp_drawable_height(self->ID));
  2704.     if (!strcmp(name, "image")) {
  2705.     id = gimp_channel_get_image_id(self->ID);
  2706.     if (id == -1) {
  2707.         Py_INCREF(Py_None);
  2708.         return Py_None;
  2709.     }
  2710.     return (PyObject *)newimgobject(id);
  2711.     }
  2712.     if (!strcmp(name, "is_color") || !strcmp(name, "is_colour") ||
  2713.     !strcmp(name, "is_rgb"))
  2714.     return PyInt_FromLong(gimp_drawable_is_rgb(self->ID));
  2715.     if (!strcmp(name, "is_gray") || !strcmp(name, "is_grey"))
  2716.     return PyInt_FromLong(gimp_drawable_is_gray(self->ID));
  2717.     if (!strcmp(name, "is_indexed"))
  2718.     return PyInt_FromLong(gimp_drawable_is_indexed(self->ID));
  2719.     if (!strcmp(name, "layer")) {
  2720.     /* id = gimp_channel_get_layer_id(self->ID); */
  2721.     /* It isn't quite clear what that was supposed to achieve, but
  2722.        the gimp_channel_get_layer_id call no longer exists, which
  2723.        was breaking everything that tried to load gimpmodule.so.
  2724.  
  2725.        With no-one apparently maintaing it, the options seem to be:
  2726.         A) remove this "layer" attribute entirely, as it doesn't
  2727.            seem to make much sense.
  2728.             B) Just return the channel ID.
  2729.        
  2730.         The latter seems more conservative, so that's what I'll do.
  2731.         -- acapnotic@users.sourceforge.net (08/09/2000) */
  2732.     id = self->ID;
  2733.     if (id == -1) {
  2734.         Py_INCREF(Py_None);
  2735.         return Py_None;
  2736.     }
  2737.     return (PyObject *)newlayobject(id);
  2738.     }
  2739.     if (!strcmp(name, "layer_mask") || !strcmp(name, "is_layer_mask"))
  2740.     return PyInt_FromLong(gimp_drawable_is_layer_mask(self->ID));
  2741.     if (!strcmp(name, "mask_bounds")) {
  2742.     gint x1, y1, x2, y2;
  2743.     gimp_drawable_mask_bounds(self->ID, &x1, &y1, &x2, &y2);
  2744.     return Py_BuildValue("(iiii)", x1, y1, x2, y2);
  2745.     }
  2746.     if (!strcmp(name, "name"))
  2747.     return PyString_FromString(gimp_channel_get_name(self->ID));
  2748.     if (!strcmp(name, "offsets")) {
  2749.     gint x, y;
  2750.     gimp_drawable_offsets(self->ID, &x, &y);
  2751.     return Py_BuildValue("(ii)", x, y);
  2752.     }
  2753.     if (!strcmp(name, "opacity"))
  2754.     return PyFloat_FromDouble(gimp_channel_get_opacity(self->ID));
  2755.     if (!strcmp(name, "show_masked"))
  2756.     return PyInt_FromLong(gimp_channel_get_show_masked(self->ID));
  2757.     if (!strcmp(name, "type"))
  2758.     return PyInt_FromLong(gimp_drawable_type(self->ID));
  2759.     if (!strcmp(name, "visible"))
  2760.     return PyInt_FromLong(gimp_channel_get_visible(self->ID));
  2761.     if (!strcmp(name, "width"))
  2762.     return PyInt_FromLong(gimp_drawable_width(self->ID));
  2763.     return Py_FindMethod(chn_methods, (PyObject *)self, name);
  2764. }
  2765.  
  2766. static int
  2767. chn_setattr(self, name, v)
  2768.      chnobject *self;
  2769.      char *name;
  2770.      PyObject *v;
  2771. {
  2772.     PyObject *r, *g, *b;
  2773.     if (v == NULL) {
  2774.     PyErr_SetString(PyExc_TypeError, "can not delete attributes.");
  2775.     return -1;
  2776.     }
  2777.     if (!strcmp(name, "color") || !strcmp(name, "colour")) {
  2778.     if (!PySequence_Check(v) || PySequence_Length(v) < 3) {
  2779.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2780.         return -1;
  2781.     }
  2782.     r = PySequence_GetItem(v, 0);
  2783.     g = PySequence_GetItem(v, 1);
  2784.     b = PySequence_GetItem(v, 2);
  2785.     if (!PyInt_Check(r) || !PyInt_Check(g) || !PyInt_Check(b)) {
  2786.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2787.         Py_DECREF(r); Py_DECREF(g); Py_DECREF(b);
  2788.         return -1;
  2789.     }
  2790.     gimp_channel_set_color(self->ID, PyInt_AsLong(r),
  2791.                    PyInt_AsLong(g), PyInt_AsLong(b));
  2792.     Py_DECREF(r); Py_DECREF(g); Py_DECREF(b);
  2793.     return 0;
  2794.     }
  2795.     if (!strcmp(name, "name")) {
  2796.     if (!PyString_Check(v)) {
  2797.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2798.         return -1;
  2799.     }
  2800.     gimp_channel_set_name(self->ID, PyString_AsString(v));
  2801.     return 0;
  2802.     }
  2803.     if (!strcmp(name, "opacity")) {
  2804.     if (!PyFloat_Check(v)) {
  2805.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2806.         return -1;
  2807.     }
  2808.     gimp_channel_set_opacity(self->ID, PyFloat_AsDouble(v));
  2809.     return 0;
  2810.     }
  2811.     /*    if (!strcmp(name, "show_masked")) {
  2812.     if (!PyInt_Check(v)) {
  2813.     PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2814.     return -1;
  2815.     }
  2816.     gimp_channel_set_show_masked(self->ID, PyInt_AsLong(v));
  2817.     return 0;
  2818.     }
  2819.     */    if (!strcmp(name, "visible")) {
  2820.     if (!PyInt_Check(v)) {
  2821.         PyErr_SetString(PyExc_TypeError, "type mis-match.");
  2822.         return -1;
  2823.     }
  2824.     gimp_channel_set_visible(self->ID, PyInt_AsLong(v));
  2825.     return 0;
  2826.     }
  2827.     if (!strcmp(name, "height") || !strcmp(name, "image") ||
  2828.     !strcmp(name, "layer") || !strcmp(name, "width") ||
  2829.     !strcmp(name, "ID") || !strcmp(name, "bpp") ||
  2830.     !strcmp(name, "layer_mask") || !strcmp(name, "mask_bounds") ||
  2831.     !strcmp(name, "offsets") || !strcmp(name, "type") ||
  2832.     !strcmp(name, "has_alpha") || !strcmp(name, "is_color") ||
  2833.     !strcmp(name, "is_colour") || !strcmp(name, "is_rgb") ||
  2834.     !strcmp(name, "is_gray") || !strcmp(name, "is_grey") ||
  2835.     !strcmp(name, "is_indexed") || !strcmp(name, "is_layer_mask") ||
  2836.     !strcmp(name, "__members__")) {
  2837.     PyErr_SetString(PyExc_TypeError, "read-only attribute.");
  2838.     return -1;
  2839.     }
  2840.     return -1;
  2841. }
  2842.  
  2843. static PyObject *
  2844. chn_repr(self)
  2845.      chnobject *self;
  2846. {
  2847.     PyObject *s;
  2848.  
  2849.     s = PyString_FromString("<channel ");
  2850.     PyString_ConcatAndDel(&s, PyString_FromString(gimp_channel_get_name(
  2851.                                     self->ID)));
  2852.     PyString_ConcatAndDel(&s, PyString_FromString(">"));
  2853.     return s;
  2854. }
  2855.  
  2856. static int
  2857. chn_cmp(self, other)
  2858.      chnobject *self, *other;
  2859. {
  2860.     return self->ID - other->ID;
  2861. }
  2862.  
  2863. static PyTypeObject Chntype = {
  2864.     PyObject_HEAD_INIT(&PyType_Type)
  2865.     0,                /*ob_size*/
  2866.     "Channel",            /*tp_name*/
  2867.     sizeof(chnobject),        /*tp_basicsize*/
  2868.     0,                /*tp_itemsize*/
  2869.     /* methods */
  2870.     (destructor)chn_dealloc,    /*tp_dealloc*/
  2871.     (printfunc)0,        /*tp_print*/
  2872.     (getattrfunc)chn_getattr,    /*tp_getattr*/
  2873.     (setattrfunc)chn_setattr,    /*tp_setattr*/
  2874.     (cmpfunc)chn_cmp,        /*tp_compare*/
  2875.     (reprfunc)chn_repr,        /*tp_repr*/
  2876.     &gobj_as_number,            /*tp_as_number*/
  2877.     0,        /*tp_as_sequence*/
  2878.     0,        /*tp_as_mapping*/
  2879.     (hashfunc)0,        /*tp_hash*/
  2880.     (ternaryfunc)0,        /*tp_call*/
  2881.     (reprfunc)0,        /*tp_str*/
  2882.  
  2883.     /* Space for future expansion */
  2884.     0L,0L,0L,0L,
  2885.     NULL /* Documentation string */
  2886. };
  2887.  
  2888. /* End of code for Channel objects */
  2889. /* -------------------------------------------------------- */
  2890.  
  2891.  
  2892. static PyObject *
  2893. tile_flush(self, args)
  2894.      tileobject *self;
  2895.      PyObject *args;
  2896. {
  2897.     if (!PyArg_ParseTuple(args, ":flush"))
  2898.     return NULL;
  2899.     gimp_tile_flush(self->tile);
  2900.     Py_INCREF(Py_None);
  2901.     return Py_None;
  2902. }
  2903.  
  2904.  
  2905. static struct PyMethodDef tile_methods[] = {
  2906.     {"flush",    (PyCFunction)tile_flush,    METH_VARARGS},
  2907.  
  2908.     {NULL,        NULL}        /* sentinel */
  2909. };
  2910.  
  2911. /* ---------- */
  2912.  
  2913.  
  2914. static tileobject *
  2915. newtileobject(t, drw)
  2916.      GimpTile *t;
  2917.      drwobject *drw;
  2918. {
  2919.     tileobject *self;
  2920.     
  2921.     self = PyObject_NEW(tileobject, &Tiletype);
  2922.     if (self == NULL)
  2923.     return NULL;
  2924.     gimp_tile_ref(t);
  2925.     self->tile = t;
  2926.  
  2927.     Py_INCREF(drw);
  2928.     self->drawable = drw;
  2929.  
  2930.     return self;
  2931. }
  2932.  
  2933.  
  2934. static void
  2935. tile_dealloc(self)
  2936.      tileobject *self;
  2937. {
  2938.     gimp_tile_unref(self->tile, FALSE);
  2939.     Py_DECREF(self->drawable);
  2940.     PyMem_DEL(self);
  2941. }
  2942.  
  2943. #define OFF(x) offsetof(GimpTile, x)
  2944.  
  2945. static struct memberlist tile_memberlist[] = {
  2946.     {"ewidth",    T_UINT,        OFF(ewidth),    RO},
  2947.     {"eheight",    T_UINT,        OFF(eheight),    RO},
  2948.     {"bpp",        T_UINT,        OFF(bpp),    RO},
  2949.     {"ref_count",    T_USHORT,    OFF(ref_count),    RO},
  2950.     {"dirty",    T_INT,        0,    RO},
  2951.     {"shadow",    T_INT,        0,    RO},
  2952.     {"drawable",    T_INT,        OFF(drawable),    RO},
  2953.     {NULL} /* Sentinel */
  2954. };
  2955.  
  2956. #undef OFF
  2957.  
  2958. static PyObject *
  2959. tile_getattr(self, name)
  2960.      tileobject *self;
  2961.      char *name;
  2962. {
  2963.     PyObject *rv;
  2964.  
  2965.     if (!strcmp(name, "dirty"))
  2966.     return PyInt_FromLong(self->tile->dirty);
  2967.     if (!strcmp(name, "shadow"))
  2968.     return PyInt_FromLong(self->tile->shadow);
  2969.     if (!strcmp(name, "drawable"))
  2970.     return (PyObject *)newdrwobject(self->tile->drawable, 0);
  2971.     rv = PyMember_Get((char *)self->tile, tile_memberlist, name);
  2972.     if (rv)
  2973.     return rv;
  2974.     PyErr_Clear();
  2975.     return Py_FindMethod(tile_methods, (PyObject *)self, name);
  2976. }
  2977.  
  2978. static PyObject *
  2979. tile_repr(self)
  2980.      tileobject *self;
  2981. {
  2982.     PyObject *s;
  2983.     if (self->tile->shadow)
  2984.     s = PyString_FromString("<shadow tile for drawable ");
  2985.     else
  2986.     s = PyString_FromString("<tile for drawable ");
  2987.     PyString_ConcatAndDel(&s, PyString_FromString(gimp_drawable_name(
  2988.                                      self->tile->drawable->id)));
  2989.     PyString_ConcatAndDel(&s, PyString_FromString(">"));
  2990.     return s;
  2991. }
  2992.  
  2993. static int
  2994. tile_length(self)
  2995.      tileobject *self;
  2996. {
  2997.     return self->tile->ewidth * self->tile->eheight;
  2998. }
  2999.  
  3000. static PyObject *
  3001. tile_subscript(self, sub)
  3002.      tileobject *self;
  3003.      PyObject *sub;
  3004. {
  3005.     GimpTile *tile = self->tile;
  3006.     int bpp = tile->bpp;
  3007.     long x, y;
  3008.  
  3009.     if (PyInt_Check(sub)) {
  3010.     x = PyInt_AsLong(sub);
  3011.     if (x < 0 || x >= tile->ewidth * tile->eheight) {
  3012.         PyErr_SetString(PyExc_IndexError,"index out of range");
  3013.         return NULL;
  3014.     }
  3015.     return PyString_FromStringAndSize(tile->data + bpp * x, bpp);
  3016.     }
  3017.     if (PyTuple_Check(sub)) {
  3018.     if (!PyArg_ParseTuple(sub, "ll", &x, &y))
  3019.         return NULL;
  3020.     if (x < 0 || y < 0 || x >= tile->ewidth || y>=tile->eheight) {
  3021.         PyErr_SetString(PyExc_IndexError,"index out of range");
  3022.         return NULL;
  3023.     }
  3024.     return PyString_FromStringAndSize(tile->data + bpp * (x +
  3025.                                   y * tile->ewidth), bpp);
  3026.     }
  3027.     PyErr_SetString(PyExc_TypeError, "tile subscript not int or 2-tuple");
  3028.     return NULL;
  3029. }
  3030.  
  3031. static int
  3032. tile_ass_sub(self, v, w)
  3033.      tileobject *self;
  3034.      PyObject *v, *w;
  3035. {
  3036.     GimpTile *tile = self->tile;
  3037.     int bpp = tile->bpp, i;
  3038.     long x, y;
  3039.     char *pix, *data;
  3040.     
  3041.     if (w == NULL) {
  3042.     PyErr_SetString(PyExc_TypeError,
  3043.             "can not delete pixels in tile");
  3044.     return -1;
  3045.     }
  3046.     if (!PyString_Check(w) && PyString_Size(w) == bpp) {
  3047.     PyErr_SetString(PyExc_TypeError, "invalid subscript");
  3048.     return -1;
  3049.     }
  3050.     pix = PyString_AsString(w);
  3051.     if (PyInt_Check(v)) {
  3052.     x = PyInt_AsLong(v);
  3053.     if (x < 0 || x >= tile->ewidth * tile->eheight) {
  3054.         PyErr_SetString(PyExc_IndexError,"index out of range");
  3055.         return -1;
  3056.     }
  3057.     data = tile->data + x * bpp;
  3058.     for (i = 0; i < bpp; i++)
  3059.         data[i] = pix[i];
  3060.     tile->dirty = TRUE;
  3061.     return 0;
  3062.     }
  3063.     if (PyTuple_Check(v)) {
  3064.     if (!PyArg_ParseTuple(v, "ll", &x, &y))
  3065.         return -1;
  3066.     if (x < 0 || y < 0 || x >= tile->ewidth || y>=tile->eheight) {
  3067.         PyErr_SetString(PyExc_IndexError,"index out of range");
  3068.         return -1;
  3069.     }
  3070.     data = tile->data + bpp * (x + y * tile->ewidth);
  3071.     for (i = 0; i < bpp; i++)
  3072.         data[i] = pix[i];
  3073.     tile->dirty = TRUE;
  3074.     return 0;
  3075.     }
  3076.     PyErr_SetString(PyExc_TypeError, "tile subscript not int or 2-tuple");
  3077.     return -1;
  3078. }
  3079.  
  3080. static PyMappingMethods tile_as_mapping = {
  3081.     (inquiry)tile_length, /*length*/
  3082.     (binaryfunc)tile_subscript, /*subscript*/
  3083.     (objobjargproc)tile_ass_sub, /*ass_sub*/
  3084. };
  3085.  
  3086. static PyTypeObject Tiletype = {
  3087.     PyObject_HEAD_INIT(&PyType_Type)
  3088.     0,                /*ob_size*/
  3089.     "Tile",            /*tp_name*/
  3090.     sizeof(tileobject),        /*tp_basicsize*/
  3091.     0,                /*tp_itemsize*/
  3092.     /* methods */
  3093.     (destructor)tile_dealloc,    /*tp_dealloc*/
  3094.     (printfunc)0,        /*tp_print*/
  3095.     (getattrfunc)tile_getattr,    /*tp_getattr*/
  3096.     (setattrfunc)0,    /*tp_setattr*/
  3097.     (cmpfunc)0,        /*tp_compare*/
  3098.     (reprfunc)tile_repr,        /*tp_repr*/
  3099.     0,            /*tp_as_number*/
  3100.     0,        /*tp_as_sequence*/
  3101.     &tile_as_mapping,        /*tp_as_mapping*/
  3102.     (hashfunc)0,        /*tp_hash*/
  3103.     (ternaryfunc)0,        /*tp_call*/
  3104.     (reprfunc)0,        /*tp_str*/
  3105.  
  3106.     /* Space for future expansion */
  3107.     0L,0L,0L,0L,
  3108.     NULL /* Documentation string */
  3109. };
  3110.  
  3111. /* End of code for Tile objects */
  3112. /* -------------------------------------------------------- */
  3113.  
  3114.  
  3115. static PyObject *
  3116. pr_resize(self, args)
  3117.      probject *self;
  3118.      PyObject *args;
  3119. {
  3120.     int x, y, w, h;
  3121.     if (!PyArg_ParseTuple(args, "iiii:resize", &x, &y, &w, &h))
  3122.     return NULL;
  3123.     gimp_pixel_rgn_resize(&(self->pr), x, y, w, h);
  3124.     Py_INCREF(Py_None);
  3125.     return Py_None;
  3126. }
  3127.  
  3128.  
  3129.  
  3130. static struct PyMethodDef pr_methods[] = {
  3131.     {"resize",    (PyCFunction)pr_resize,    METH_VARARGS},
  3132.  
  3133.     {NULL,        NULL}        /* sentinel */
  3134. };
  3135.  
  3136. /* ---------- */
  3137.  
  3138.  
  3139. static probject *
  3140. newprobject(drawable, x, y, width, height, dirty, shadow)
  3141.      drwobject *drawable;
  3142.      int x, y, width, height, dirty, shadow;
  3143. {
  3144.     probject *self;
  3145.     
  3146.     self = PyObject_NEW(probject, &Prtype);
  3147.     if (self == NULL)
  3148.     return NULL;
  3149.     gimp_pixel_rgn_init(&(self->pr), drawable->drawable, x, y, width, height,
  3150.             dirty, shadow);
  3151.     self->drawable = drawable;
  3152.     Py_INCREF(drawable);
  3153.     return self;
  3154. }
  3155.  
  3156.  
  3157. static void
  3158. pr_dealloc(self)
  3159.      probject *self;
  3160. {
  3161.     Py_DECREF(self->drawable);
  3162.     PyMem_DEL(self);
  3163. }
  3164.  
  3165. /* Code to access pr objects as mappings */
  3166.  
  3167. static int
  3168. pr_length(self)
  3169.      probject *self;
  3170. {
  3171.     PyErr_SetString(ErrorObject, "Can't get size of pixel region.");
  3172.     return -1;
  3173. }
  3174.  
  3175. static PyObject *
  3176. pr_subscript(self, key)
  3177.      probject *self;
  3178.      PyObject *key;
  3179. {
  3180.     GimpPixelRgn *pr = &(self->pr);
  3181.     int bpp = pr->bpp;
  3182.     PyObject *x, *y;
  3183.     int x1, y1, x2, y2, xs, ys;
  3184.  
  3185.     if (!PyTuple_Check(key) || PyTuple_Size(key) != 2) {
  3186.     PyErr_SetString(PyExc_TypeError,"subscript must be a 2-tuple.");
  3187.     return NULL;
  3188.     }
  3189.     if (!PyArg_ParseTuple(key, "OO", &x, &y))
  3190.     return NULL;
  3191.     if (PyInt_Check(x)) {
  3192.     x1 = PyInt_AsLong(x);
  3193.     if (pr->x > x1 || x1 >= pr->x + pr->w) {
  3194.         PyErr_SetString(PyExc_IndexError,
  3195.                 "x subscript out of range");
  3196.         return NULL;
  3197.     }
  3198.     if (PyInt_Check(y)) {
  3199.         char buf[MAX_BPP];
  3200.             
  3201.         y1 = PyInt_AsLong(y);
  3202.         if (pr->y > y1 || y1 >= pr->y + pr->h) {
  3203.         PyErr_SetString(PyExc_IndexError,
  3204.                 "y subscript out of range");
  3205.         return NULL;
  3206.         }
  3207.         gimp_pixel_rgn_get_pixel(pr, buf, x1, y1);
  3208.         return PyString_FromStringAndSize(buf, bpp);
  3209.     } else if (PySlice_Check(y))
  3210.         if (PySlice_GetIndices((PySliceObject *)y,
  3211.                    pr->y + pr->h, &y1, &y2, &ys) ||
  3212.         (y1 != 0 && pr->y > y1) ||
  3213.         pr->y > y2 || ys != 1) {
  3214.         PyErr_SetString(PyExc_IndexError,
  3215.                 "invalid y slice");
  3216.         return NULL;
  3217.         } else {
  3218.         gchar *buf = g_new(gchar, bpp * (y2 - y1));
  3219.         PyObject *ret;
  3220.                 
  3221.         if (y1 == 0) y1 = pr->y;
  3222.         gimp_pixel_rgn_get_col(pr, buf, x1, y1, y2-y1);
  3223.         ret = PyString_FromStringAndSize(buf, bpp * (y2 - y1));
  3224.         g_free(buf);
  3225.         return ret;
  3226.         }
  3227.     else {
  3228.         PyErr_SetString(PyExc_TypeError,"invalid y subscript");
  3229.         return NULL;
  3230.     }
  3231.     } else if (PySlice_Check(x)) {
  3232.     if (PySlice_GetIndices((PySliceObject *)x, pr->x + pr->w,
  3233.                    &x1, &x2, &xs) || (x1 != 0 && pr->x > x1) ||
  3234.         pr->x > x2 || xs != 1) {
  3235.         PyErr_SetString(PyExc_IndexError, "invalid x slice");
  3236.         return NULL;
  3237.     }
  3238.     if (x1 == 0) x1 = pr->x;
  3239.     if (PyInt_Check(y)) {
  3240.         char *buf;
  3241.         PyObject *ret;
  3242.             
  3243.         y1 = PyInt_AsLong(y);
  3244.         if (pr->y > y1 || y1 >= pr->y + pr->h) {
  3245.         PyErr_SetString(PyExc_IndexError,
  3246.                 "y subscript out of range");
  3247.         return NULL;
  3248.         }
  3249.         buf = g_new(gchar, bpp * (x2 - x1));
  3250.         gimp_pixel_rgn_get_row(pr, buf, x1, y1, x2 - x1);
  3251.         ret = PyString_FromStringAndSize(buf, bpp * (x2-x1));
  3252.         g_free(buf);
  3253.         return ret;
  3254.     } else if (PySlice_Check(y))
  3255.         if (PySlice_GetIndices((PySliceObject *)y,
  3256.                    pr->y + pr->h, &y1, &y2, &ys) ||
  3257.         (y1 != 0 && pr->y) > y1 ||
  3258.         pr->y > y2 || ys != 1) {
  3259.         PyErr_SetString(PyExc_IndexError,
  3260.                 "invalid y slice");
  3261.         return NULL;
  3262.         } else {
  3263.         gchar *buf = g_new(gchar, bpp * (x2 - x1) * (y2 - y1));
  3264.         PyObject *ret;
  3265.                 
  3266.         if (y1 == 0) y1 = pr->y;
  3267.         gimp_pixel_rgn_get_rect(pr, buf, x1, y1,
  3268.                     x2 - x1, y2 - y1);
  3269.         ret = PyString_FromStringAndSize(buf, bpp * (x2-x1) * (y2-y1));
  3270.         g_free(buf);
  3271.         return ret;
  3272.         }
  3273.     else {
  3274.         PyErr_SetString(PyExc_TypeError,"invalid y subscript");
  3275.         return NULL;
  3276.     }
  3277.     } else {
  3278.     PyErr_SetString(PyExc_TypeError, "invalid x subscript");
  3279.     return NULL;
  3280.     }
  3281. }
  3282.  
  3283. static int
  3284. pr_ass_sub(self, v, w)
  3285.      probject *self;
  3286.      PyObject *v, *w;
  3287. {
  3288.     GimpPixelRgn *pr = &(self->pr);
  3289.     int bpp = pr->bpp;
  3290.     PyObject *x, *y;
  3291.     char *buf;
  3292.     int len, x1, x2, xs, y1, y2, ys;
  3293.     
  3294.     if (w == NULL) {
  3295.     PyErr_SetString(PyExc_TypeError, "can't delete subscripts.");
  3296.     return -1;
  3297.     }
  3298.     if (!PyString_Check(w)) {
  3299.     PyErr_SetString(PyExc_TypeError,
  3300.             "must assign string to subscript");
  3301.     return -1;
  3302.     }
  3303.     if (!PyTuple_Check(v) || PyTuple_Size(v) != 2) {
  3304.     PyErr_SetString(PyExc_TypeError,"subscript must be a 2-tuple");
  3305.     return -1;
  3306.     }
  3307.     if (!PyArg_ParseTuple(v, "OO", &x, &y))
  3308.     return -1;
  3309.     buf = PyString_AsString(w);
  3310.     len = PyString_Size(w);
  3311.     if (PyInt_Check(x)) {
  3312.     x1 = PyInt_AsLong(x);
  3313.     if (pr->x > x1 || x1 >= pr->x + pr->w) {
  3314.         PyErr_SetString(PyExc_IndexError,
  3315.                 "x subscript out of range");
  3316.         return -1;
  3317.     }
  3318.     if (PyInt_Check(y)) {
  3319.         y1 = PyInt_AsLong(y);
  3320.         if (pr->y > y1 || y1 >= pr->y + pr->h) {
  3321.         PyErr_SetString(PyExc_IndexError,
  3322.                 "y subscript out of range");
  3323.         return -1;
  3324.         }
  3325.         if (len != bpp) {
  3326.         PyErr_SetString(PyExc_TypeError,
  3327.                 "string is wrong length");
  3328.         return -1;
  3329.         }
  3330.         gimp_pixel_rgn_set_pixel(pr, buf, x1, y1);
  3331.         return 0;
  3332.     } else if (PySlice_Check(y)) {
  3333.         if (PySlice_GetIndices((PySliceObject *)y,
  3334.                    pr->y + pr->h, &y1, &y2, &ys) ||
  3335.         (y1 != 0 && pr->y > y1) ||
  3336.         pr->y > y2 || ys != 1) {
  3337.         PyErr_SetString(PyExc_IndexError,
  3338.                 "invalid y slice");
  3339.         return -1;
  3340.         }
  3341.         if (y1 == 0) y1 = pr->y;
  3342.         if (len != bpp * (y2 - y1)) {
  3343.         PyErr_SetString(PyExc_TypeError,
  3344.                 "string is wrong length");
  3345.         return -1;
  3346.         }
  3347.         gimp_pixel_rgn_set_col(pr, buf, x1, y1, y2 - y1);
  3348.         return 0;
  3349.     } else {
  3350.         PyErr_SetString(PyExc_IndexError,"invalid y subscript");
  3351.         return -1;
  3352.     }
  3353.     } else if (PySlice_Check(x)) {
  3354.     if (PySlice_GetIndices((PySliceObject *)x, pr->x + pr->w,
  3355.                    &x1, &x2, &xs) || (x1 != 0 && pr->x > x1) ||
  3356.         pr->x > x2 || xs != 1) {
  3357.         PyErr_SetString(PyExc_IndexError, "invalid x slice");
  3358.         return -1;
  3359.     }
  3360.     if (x1 == 0) x1 = pr->x;
  3361.     if (PyInt_Check(y)) {
  3362.         y1 = PyInt_AsLong(y);
  3363.         if (pr->y > y1 || y1 >= pr->y + pr->h) {
  3364.         PyErr_SetString(PyExc_IndexError,
  3365.                 "y subscript out of range");
  3366.         return -1;
  3367.         }
  3368.         if (len != bpp * (x2 - x1)) {
  3369.         PyErr_SetString(PyExc_TypeError,
  3370.                 "string is wrong length");
  3371.         return -1;
  3372.         }
  3373.         gimp_pixel_rgn_set_row(pr, buf, x1, y1, x2 - x1);
  3374.         return 0;
  3375.     } else if (PySlice_Check(y)) {
  3376.         if (PySlice_GetIndices((PySliceObject *)y,
  3377.                    pr->y + pr->h, &y1, &y2, &ys) ||
  3378.         (y1 != 0 && pr->y > y1) ||
  3379.         pr->y > y2 || ys != 1) {
  3380.         PyErr_SetString(PyExc_IndexError,
  3381.                 "invalid y slice");
  3382.         return -1;
  3383.         }
  3384.         if (y1 == 0) y1 = pr->y;
  3385.         if (len != bpp * (x2 - x1) * (y2 - y1)) {
  3386.         PyErr_SetString(PyExc_TypeError,
  3387.                 "string is wrong length");
  3388.         return -1;
  3389.         }
  3390.         gimp_pixel_rgn_set_rect(pr, buf, x1, y1, x2-x1, y2-y1);
  3391.         return 0;
  3392.     } else {
  3393.         PyErr_SetString(PyExc_TypeError,"invalid y subscript");
  3394.         return -1;
  3395.     }
  3396.     } else {
  3397.     PyErr_SetString(PyExc_TypeError, "invalid x subscript");
  3398.     return -1;
  3399.     }
  3400.     return -1;
  3401. }
  3402.  
  3403. static PyMappingMethods pr_as_mapping = {
  3404.     (inquiry)pr_length,        /*mp_length*/
  3405.     (binaryfunc)pr_subscript,        /*mp_subscript*/
  3406.     (objobjargproc)pr_ass_sub,    /*mp_ass_subscript*/
  3407. };
  3408.  
  3409. /* -------------------------------------------------------- */
  3410.  
  3411. #define OFF(x) offsetof(GimpPixelRgn, x)
  3412.  
  3413. static struct memberlist pr_memberlist[] = {
  3414.     {"drawable",  T_INT,  OFF(drawable),  RO},
  3415.     {"bpp",       T_UINT, OFF(bpp),       RO},
  3416.     {"rowstride", T_UINT, OFF(rowstride), RO},
  3417.     {"x",         T_UINT, OFF(x),         RO},
  3418.     {"y",         T_UINT, OFF(y),         RO},
  3419.     {"w",         T_UINT, OFF(w),         RO},
  3420.     {"h",         T_UINT, OFF(h),         RO},
  3421.     {"dirty",     T_UINT, 0 /*bitfield*/, RO},
  3422.     {"shadow",    T_UINT, 0 /*bitfield*/, RO}
  3423. };
  3424.  
  3425. #undef OFF
  3426.  
  3427. static PyObject *
  3428. pr_getattr(self, name)
  3429.      probject *self;
  3430.      char *name;
  3431. {
  3432.     PyObject *rv;
  3433.  
  3434.     if (!strcmp(name, "drawable"))
  3435.     return (PyObject *)newdrwobject(self->pr.drawable, 0);
  3436.     if (!strcmp(name, "dirty"))
  3437.     return PyInt_FromLong(self->pr.dirty);
  3438.     if (!strcmp(name, "shadow"))
  3439.     return PyInt_FromLong(self->pr.shadow);
  3440.     rv = PyMember_Get((char *)&(self->pr), pr_memberlist, name);
  3441.     if (rv)
  3442.     return rv;
  3443.     PyErr_Clear();
  3444.     return Py_FindMethod(pr_methods, (PyObject *)self, name);
  3445. }
  3446.  
  3447. static PyObject *
  3448. pr_repr(self)
  3449.      probject *self;
  3450. {
  3451.     PyObject *s;
  3452.     s = PyString_FromString("<pixel region for drawable ");
  3453.     PyString_ConcatAndDel(&s, PyString_FromString(gimp_drawable_name(
  3454.                                      self->pr.drawable->id)));
  3455.     PyString_ConcatAndDel(&s, PyString_FromString(">"));
  3456.     return s;
  3457. }
  3458.  
  3459. static PyTypeObject Prtype = {
  3460.     PyObject_HEAD_INIT(&PyType_Type)
  3461.     0,                /*ob_size*/
  3462.     "PixelRegion",            /*tp_name*/
  3463.     sizeof(probject),        /*tp_basicsize*/
  3464.     0,                /*tp_itemsize*/
  3465.     /* methods */
  3466.     (destructor)pr_dealloc,    /*tp_dealloc*/
  3467.     (printfunc)0,        /*tp_print*/
  3468.     (getattrfunc)pr_getattr,    /*tp_getattr*/
  3469.     (setattrfunc)0,    /*tp_setattr*/
  3470.     (cmpfunc)0,        /*tp_compare*/
  3471.     (reprfunc)pr_repr,        /*tp_repr*/
  3472.     0,            /*tp_as_number*/
  3473.     0,        /*tp_as_sequence*/
  3474.     &pr_as_mapping,        /*tp_as_mapping*/
  3475.     (hashfunc)0,        /*tp_hash*/
  3476.     (ternaryfunc)0,        /*tp_call*/
  3477.     (reprfunc)0,        /*tp_str*/
  3478.  
  3479.     /* Space for future expansion */
  3480.     0L,0L,0L,0L,
  3481.     NULL /* Documentation string */
  3482. };
  3483.  
  3484. /* End of code for PixelRegion objects */
  3485. /* -------------------------------------------------------- */
  3486.  
  3487. #ifdef GIMP_HAVE_PARASITES
  3488. static PyObject *
  3489. para_copy(self, args)
  3490.      paraobject *self;
  3491.      PyObject *args;
  3492. {
  3493.     if (!PyArg_ParseTuple(args, ":copy"))
  3494.     return NULL;
  3495.     return (PyObject *)newparaobject(gimp_parasite_copy(self->para));
  3496. }
  3497.  
  3498. static PyObject *
  3499. para_is_type(self, args)
  3500.      paraobject *self;
  3501.      PyObject *args;
  3502. {
  3503.     char *name;
  3504.     if (!PyArg_ParseTuple(args, "s:is_type", &name))
  3505.     return NULL;
  3506.     return PyInt_FromLong(gimp_parasite_is_type(self->para, name));
  3507. }
  3508.  
  3509. static PyObject *
  3510. para_has_flag(self, args)
  3511.      paraobject *self;
  3512.      PyObject *args;
  3513. {
  3514.     int flag;
  3515.     if (!PyArg_ParseTuple(args, "i:has_flag", &flag))
  3516.     return NULL;
  3517.     return PyInt_FromLong(gimp_parasite_has_flag(self->para, flag));
  3518. }
  3519.  
  3520.  
  3521.  
  3522. static struct PyMethodDef para_methods[] = {
  3523.     {"copy",    (PyCFunction)para_copy,    METH_VARARGS},
  3524.     {"is_type",    (PyCFunction)para_is_type,    METH_VARARGS},
  3525.     {"has_flag",(PyCFunction)para_has_flag,    METH_VARARGS},
  3526.  
  3527.     {NULL,        NULL}        /* sentinel */
  3528. };
  3529.  
  3530. static paraobject *
  3531. newparaobject(para)
  3532.      GimpParasite *para;
  3533. {
  3534.     paraobject *self;
  3535.  
  3536.     if (!para) {
  3537.     Py_INCREF(Py_None);
  3538.     return (paraobject *)Py_None;
  3539.     }
  3540.     self = PyObject_NEW(paraobject, &Paratype);
  3541.     if (self == NULL)
  3542.     return NULL;
  3543.     self->para = para;
  3544.     return self;
  3545. }
  3546.  
  3547.  
  3548. static void
  3549. para_dealloc(self)
  3550.      paraobject *self;
  3551. {
  3552.     gimp_parasite_free(self->para);
  3553.     PyMem_DEL(self);
  3554. }
  3555.  
  3556. static PyObject *
  3557. para_getattr(self, name)
  3558.      paraobject *self;
  3559.      char *name;
  3560. {
  3561.     if (!strcmp(name, "__members__")) {
  3562. #if GIMP_CHECK_VERSION(1,1,5)
  3563.     return Py_BuildValue("[sssss]", "data", "flags", "is_persistent",
  3564.                  "is_undoable", "name");
  3565. #else
  3566.     return Py_BuildValue("[ssss]", "data", "flags", "is_persistent",
  3567.                  "name");
  3568. #endif
  3569.     }
  3570.     if (!strcmp(name, "is_persistent"))
  3571.     return PyInt_FromLong(gimp_parasite_is_persistent(self->para));
  3572. #if GIMP_CHECK_VERSION(1,1,5)
  3573.     if (!strcmp(name, "is_undoable"))
  3574.     return PyInt_FromLong(gimp_parasite_is_undoable(self->para));
  3575. #endif
  3576.     if (!strcmp(name, "flags"))
  3577.     return PyInt_FromLong(gimp_parasite_flags(self->para));
  3578.     if (!strcmp(name, "name"))
  3579.     return PyString_FromString(gimp_parasite_name(self->para));
  3580.     if (!strcmp(name, "data"))
  3581.     return PyString_FromStringAndSize(gimp_parasite_data(self->para),
  3582.                       gimp_parasite_data_size(self->para));
  3583.     return Py_FindMethod(para_methods, (PyObject *)self, name);
  3584. }
  3585.  
  3586. static PyObject *
  3587. para_repr(self)
  3588.      paraobject *self;
  3589. {
  3590.     PyObject *s;
  3591.     s = PyString_FromString("<parasite ");
  3592.     PyString_ConcatAndDel(&s, PyString_FromString(gimp_parasite_name(self->para)));
  3593.     PyString_ConcatAndDel(&s, PyString_FromString(">"));
  3594.     return s;
  3595. }
  3596.  
  3597. static PyObject *
  3598. para_str(self)
  3599.      paraobject *self;
  3600. {
  3601.     return PyString_FromStringAndSize(gimp_parasite_data(self->para),
  3602.                       gimp_parasite_data_size(self->para));
  3603. }
  3604.  
  3605. static PyTypeObject Paratype = {
  3606.     PyObject_HEAD_INIT(&PyType_Type)
  3607.     0,                /*ob_size*/
  3608.     "GimpParasite",            /*tp_name*/
  3609.     sizeof(paraobject),        /*tp_basicsize*/
  3610.     0,                /*tp_itemsize*/
  3611.     /* methods */
  3612.     (destructor)para_dealloc,    /*tp_dealloc*/
  3613.     (printfunc)0,        /*tp_print*/
  3614.     (getattrfunc)para_getattr,    /*tp_getattr*/
  3615.     (setattrfunc)0,    /*tp_setattr*/
  3616.     (cmpfunc)0,        /*tp_compare*/
  3617.     (reprfunc)para_repr,        /*tp_repr*/
  3618.     0,            /*tp_as_number*/
  3619.     0,        /*tp_as_sequence*/
  3620.     0,        /*tp_as_mapping*/
  3621.     (hashfunc)0,        /*tp_hash*/
  3622.     (ternaryfunc)0,        /*tp_call*/
  3623.     (reprfunc)para_str,        /*tp_str*/
  3624.  
  3625.     /* Space for future expansion */
  3626.     0L,0L,0L,0L,
  3627.     NULL /* Documentation string */
  3628. };
  3629.  
  3630. /* End of code for GimpParasite objects */
  3631. /* -------------------------------------------------------- */
  3632. #endif
  3633.  
  3634. GimpPlugInInfo PLUG_IN_INFO = {
  3635.     NULL, /* init_proc */
  3636.     NULL, /* quit_proc */
  3637.     NULL, /* query_proc */
  3638.     NULL  /* run_proc */
  3639. };
  3640.  
  3641. static PyObject *callbacks[] = {
  3642.     NULL, NULL, NULL, NULL
  3643. };
  3644.  
  3645. static void pygimp_init_proc() {
  3646.     PyObject *r;
  3647.     r = PyObject_CallFunction(callbacks[0], "()");
  3648.     if (!r) {
  3649.     PyErr_Print();
  3650.     PyErr_Clear();
  3651.     return;
  3652.     }
  3653.     Py_DECREF(r);
  3654. }
  3655.  
  3656. static void pygimp_quit_proc() {
  3657.     PyObject *r;
  3658.     r = PyObject_CallFunction(callbacks[1], "()");
  3659.     if (!r) {
  3660.     PyErr_Print();
  3661.     PyErr_Clear();
  3662.     return;
  3663.     }
  3664.     Py_DECREF(r);
  3665. }
  3666.  
  3667. static void pygimp_query_proc() {
  3668.     PyObject *r;
  3669.     r = PyObject_CallFunction(callbacks[2], "()");
  3670.     if (!r) {
  3671.     PyErr_Print();
  3672.     PyErr_Clear();
  3673.     return;
  3674.     }
  3675.     Py_DECREF(r);
  3676. }
  3677.  
  3678. static void pygimp_run_proc(char *name, int nparams, GimpParam *params,
  3679.                 int *nreturn_vals, GimpParam **return_vals) {
  3680.     PyObject *args, *ret;
  3681.     GimpParamDef *pd, *rv;
  3682.     GimpPDBProcType t;
  3683.     char *b, *h, *a, *c, *d;
  3684.     int np, nrv;
  3685.  
  3686.     gimp_procedural_db_proc_info(name, &b, &h, &a, &c, &d, &t, &np, &nrv,
  3687.              &pd, &rv);
  3688.     g_free(b); g_free(h); g_free(a); g_free(c); g_free(d); g_free(pd);
  3689.  
  3690. #if PG_DEBUG > 0
  3691.     fprintf(stderr, "Params for %s:", name);
  3692.     print_GParam(nparams, params);
  3693. #endif
  3694.     args = GParam_to_tuple(nparams, params);
  3695.     if (args == NULL) {
  3696.     PyErr_Clear();
  3697.     *nreturn_vals = 1;
  3698.     *return_vals = g_new(GimpParam, 1);
  3699.     (*return_vals)[0].type = GIMP_PDB_STATUS;
  3700.     (*return_vals)[0].data.d_status = GIMP_PDB_CALLING_ERROR;
  3701.     return;
  3702.     }
  3703.     ret = PyObject_CallFunction(callbacks[3], "(sO)", name, args);
  3704.     Py_DECREF(args);
  3705.     if (ret == NULL) {
  3706.     PyErr_Print();
  3707.     PyErr_Clear();
  3708.     *nreturn_vals = 1;
  3709.     *return_vals = g_new(GimpParam, 1);
  3710.     (*return_vals)[0].type = GIMP_PDB_STATUS;
  3711.     (*return_vals)[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
  3712.     return;
  3713.     }
  3714.     *return_vals = tuple_to_GParam(ret, rv, nrv);
  3715.     g_free(rv);
  3716.     if (*return_vals == NULL) {
  3717.     PyErr_Clear();
  3718.     *nreturn_vals = 1;
  3719.     *return_vals = g_new(GimpParam, 1);
  3720.     (*return_vals)[0].type = GIMP_PDB_STATUS;
  3721.     (*return_vals)[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
  3722.     return;
  3723.     }
  3724.     Py_DECREF(ret);
  3725.     *nreturn_vals = nrv + 1;
  3726.     (*return_vals)[0].type = GIMP_PDB_STATUS;
  3727.     (*return_vals)[0].data.d_status = GIMP_PDB_SUCCESS;
  3728. }
  3729.  
  3730. static PyObject *
  3731. gimp_Main(self, args)
  3732.      PyObject *self;    /* Not used */
  3733.      PyObject *args;
  3734. {
  3735.     PyObject *av;
  3736.     int argc, i;
  3737.     char **argv;
  3738.     PyObject *ip, *qp, *query, *rp;
  3739.     
  3740.     if (!PyArg_ParseTuple(args, "OOOO:main", &ip, &qp, &query, &rp))
  3741.     return NULL;
  3742.  
  3743. #define Arg_Check(v) (PyCallable_Check(v) || (v) == Py_None)
  3744.  
  3745.     if (!Arg_Check(ip) || !Arg_Check(qp) || !Arg_Check(query) ||
  3746.     !Arg_Check(rp)) {
  3747.     PyErr_SetString(ErrorObject, "arguments must be callable.");
  3748.     return NULL;
  3749.     }
  3750.     if (query == Py_None) {
  3751.     PyErr_SetString(ErrorObject, "a query procedure must be provided.");
  3752.     return NULL;
  3753.     }
  3754.  
  3755.     if (ip != Py_None) {
  3756.     callbacks[0] = ip;
  3757.     PLUG_IN_INFO.init_proc = pygimp_init_proc;
  3758.     }
  3759.     if (qp != Py_None) {
  3760.     callbacks[1] = qp;
  3761.     PLUG_IN_INFO.quit_proc = pygimp_quit_proc;
  3762.     }
  3763.     if (query != Py_None) {
  3764.     callbacks[2] = query;
  3765.     PLUG_IN_INFO.query_proc = pygimp_query_proc;
  3766.     }
  3767.     if (rp != Py_None) {
  3768.     callbacks[3] = rp;
  3769.     PLUG_IN_INFO.run_proc = pygimp_run_proc;
  3770.     }
  3771.  
  3772.     av = PySys_GetObject("argv");
  3773.  
  3774.     argc = PyList_Size(av);
  3775.     argv = g_new(char *, argc);
  3776.  
  3777.     for (i = 0; i < argc; i++)
  3778.     argv[i] = g_strdup(PyString_AsString(PyList_GetItem(av, i)));
  3779.  
  3780. #ifdef G_OS_WIN32
  3781.     {
  3782.     extern void set_gimp_PLUG_IN_INFO_PTR(GimpPlugInInfo *);
  3783.     set_gimp_PLUG_IN_INFO_PTR(&PLUG_IN_INFO);
  3784.     }
  3785. #endif
  3786.  
  3787.     gimp_main(argc, argv);
  3788.  
  3789.     if (argv != NULL) {
  3790.     for (i = 0; i < argc; i++)
  3791.         if (argv[i] != NULL)
  3792.         g_free(argv[i]);
  3793.     g_free(argv);
  3794.     }
  3795.  
  3796.     Py_INCREF(Py_None);
  3797.     return Py_None;
  3798. }
  3799.  
  3800. static PyObject *
  3801. gimp_Quit(self, args)
  3802.      PyObject *self;    /* Not used */
  3803.      PyObject *args;
  3804. {
  3805.  
  3806.     if (!PyArg_ParseTuple(args, ":quit"))
  3807.     return NULL;
  3808.     gimp_quit();
  3809.     Py_INCREF(Py_None);
  3810.     return Py_None;
  3811. }
  3812.  
  3813. static PyObject *
  3814. gimp_Set_data(self, args)
  3815.      PyObject *self;    /* Not used */
  3816.      PyObject *args;
  3817. {
  3818.     char *id, *data;
  3819.     int bytes, nreturn_vals;
  3820.     GimpParam *return_vals;
  3821.  
  3822.     if (!PyArg_ParseTuple(args, "ss#:set_data", &id, &data, &bytes))
  3823.     return NULL;
  3824.     return_vals = gimp_run_procedure("gimp_procedural_db_set_data",
  3825.                      &nreturn_vals, GIMP_PDB_STRING, id, GIMP_PDB_INT32, bytes,
  3826.                      GIMP_PDB_INT8ARRAY, data, GIMP_PDB_END);
  3827.     if (return_vals[0].data.d_status != GIMP_PDB_SUCCESS) {
  3828.     PyErr_SetString(ErrorObject, "error occurred while storing");
  3829.     return NULL;
  3830.     }
  3831.     gimp_destroy_params(return_vals, nreturn_vals);
  3832.     Py_INCREF(Py_None);
  3833.     return Py_None;
  3834. }
  3835.  
  3836. static PyObject *
  3837. gimp_Get_data(self, args)
  3838.      PyObject *self;    /* Not used */
  3839.      PyObject *args;
  3840. {
  3841.     char *id;
  3842.     int nreturn_vals;
  3843.     GimpParam *return_vals;
  3844.     PyObject *s;
  3845.  
  3846.     if (!PyArg_ParseTuple(args, "s:get_data", &id))
  3847.     return NULL;
  3848.  
  3849.     return_vals = gimp_run_procedure("gimp_procedural_db_get_data",
  3850.                      &nreturn_vals, GIMP_PDB_STRING, id, GIMP_PDB_END);
  3851.  
  3852.     if (return_vals[0].data.d_status != GIMP_PDB_SUCCESS) {
  3853.     PyErr_SetString(ErrorObject, "no data for id");
  3854.     return NULL;
  3855.     }
  3856.     s = PyString_FromStringAndSize((char *)return_vals[2].data.d_int8array,
  3857.                    return_vals[1].data.d_int32);
  3858.     gimp_destroy_params(return_vals, nreturn_vals);
  3859.     return s;
  3860. }
  3861.  
  3862. static PyObject *
  3863. gimp_Progress_init(self, args)
  3864.      PyObject *self;    /* Not used */
  3865.      PyObject *args;
  3866. {
  3867.     char *msg = NULL;
  3868.     if (!PyArg_ParseTuple(args, "|s:progress_init", &msg))
  3869.     return NULL;
  3870.     gimp_progress_init(msg);
  3871.     Py_INCREF(Py_None);
  3872.     return Py_None;
  3873. }
  3874.  
  3875. static PyObject *
  3876. gimp_Progress_update(self, args)
  3877.      PyObject *self;    /* Not used */
  3878.      PyObject *args;
  3879. {
  3880.     double p;
  3881.     if (!PyArg_ParseTuple(args, "d:progress_update", &p))
  3882.     return NULL;
  3883.     gimp_progress_update(p);
  3884.     Py_INCREF(Py_None);
  3885.     return Py_None;
  3886. }
  3887.  
  3888. static PyObject *
  3889. gimp_Query_images(self, args)
  3890.      PyObject *self;    /* Not used */
  3891.      PyObject *args;
  3892. {
  3893.     gint32 *imgs;
  3894.     int nimgs, i;
  3895.     PyObject *ret;
  3896.     if (!PyArg_ParseTuple(args, ":query_images"))
  3897.     return NULL;
  3898.     imgs = gimp_image_list(&nimgs);
  3899.     ret = PyList_New(nimgs);
  3900.     for (i = 0; i < nimgs; i++)
  3901.     PyList_SetItem(ret, i, (PyObject *)newimgobject(imgs[i]));
  3902.     return ret;
  3903. }
  3904.  
  3905. static PyObject *
  3906. gimp_Install_procedure(self, args)
  3907.      PyObject *self;    /* Not used */
  3908.      PyObject *args;
  3909. {
  3910.     char *name, *blurb, *help, *author, *copyright, *date, *menu_path,
  3911.     *image_types, *n, *d;
  3912.     GimpParamDef *params, *return_vals;
  3913.     int type, nparams, nreturn_vals, i;
  3914.     PyObject *pars, *rets;
  3915.  
  3916.     if (!PyArg_ParseTuple(args, "sssssszziOO:install_procedure",
  3917.               &name, &blurb, &help,
  3918.               &author, ©right, &date, &menu_path, &image_types,
  3919.               &type, &pars, &rets))
  3920.     return NULL;
  3921.     if (!PySequence_Check(pars) || !PySequence_Check(rets)) {
  3922.     PyErr_SetString(PyExc_TypeError,
  3923.             "last two args must be sequences");
  3924.     return NULL;
  3925.     }
  3926.     nparams = PySequence_Length(pars);
  3927.     nreturn_vals = PySequence_Length(rets);
  3928.     params = g_new(GimpParamDef, nparams);
  3929.     for (i = 0; i < nparams; i++) {
  3930.     if (!PyArg_ParseTuple(PySequence_GetItem(pars, i), "iss",
  3931.                   &(params[i].type), &n, &d)) {
  3932.         g_free(params);
  3933.         return NULL;
  3934.     }
  3935.     params[i].name = g_strdup(n);
  3936.     params[i].description = g_strdup(d);
  3937.     }
  3938.     return_vals = g_new(GimpParamDef, nreturn_vals);
  3939.     for (i = 0; i < nreturn_vals; i++) {
  3940.     if (!PyArg_ParseTuple(PySequence_GetItem(rets, i), "iss",
  3941.                   &(return_vals[i].type), &n, &d)) {
  3942.         g_free(params); g_free(return_vals);
  3943.         return NULL;
  3944.     }
  3945.     return_vals[i].name = g_strdup(n);
  3946.     return_vals[i].description = g_strdup(d);
  3947.     }
  3948.     gimp_install_procedure(name, blurb, help, author, copyright, date,
  3949.                menu_path, image_types, type, nparams, nreturn_vals, params,
  3950.                return_vals);
  3951.     Py_INCREF(Py_None);
  3952.     return Py_None;
  3953. }
  3954.  
  3955. static PyObject *
  3956. gimp_Install_temp_proc(self, args)
  3957.      PyObject *self;    /* Not used */
  3958.      PyObject *args;
  3959. {
  3960.     char *name, *blurb, *help, *author, *copyright, *date, *menu_path,
  3961.     *image_types, *n, *d;
  3962.     GimpParamDef *params, *return_vals;
  3963.     int type, nparams, nreturn_vals, i;
  3964.     PyObject *pars, *rets;
  3965.  
  3966.     if (!PyArg_ParseTuple(args, "sssssszziOO:install_temp_proc",
  3967.               &name, &blurb, &help,
  3968.               &author, ©right, &date, &menu_path, &image_types,
  3969.               &type, &pars, &rets))
  3970.     return NULL;
  3971.     if (!PySequence_Check(pars) || !PySequence_Check(rets)) {
  3972.     PyErr_SetString(PyExc_TypeError,
  3973.             "last two args must be sequences");
  3974.     return NULL;
  3975.     }
  3976.     nparams = PySequence_Length(pars);
  3977.     nreturn_vals = PySequence_Length(rets);
  3978.     params = g_new(GimpParamDef, nparams);
  3979.     for (i = 0; i < nparams; i++) {
  3980.     if (!PyArg_ParseTuple(PySequence_GetItem(pars, i), "iss",
  3981.                   &(params[i].type), &n, &d)) {
  3982.         g_free(params);
  3983.         return NULL;
  3984.     }
  3985.     params[i].name = g_strdup(n);
  3986.     params[i].description = g_strdup(d);
  3987.     }
  3988.     return_vals = g_new(GimpParamDef, nreturn_vals);
  3989.     for (i = 0; i < nreturn_vals; i++) {
  3990.     if (!PyArg_ParseTuple(PySequence_GetItem(rets, i), "iss",
  3991.                   &(return_vals[i].type), &n, &d)) {
  3992.         g_free(params); g_free(return_vals);
  3993.         return NULL;
  3994.     }
  3995.     return_vals[i].name = g_strdup(n);
  3996.     return_vals[i].description = g_strdup(d);
  3997.     }
  3998.     gimp_install_temp_proc(name, blurb, help, author, copyright, date,
  3999.                menu_path, image_types, type, nparams, nreturn_vals, params,
  4000.                return_vals, pygimp_run_proc);
  4001.     Py_INCREF(Py_None);
  4002.     return Py_None;
  4003. }
  4004.  
  4005. static PyObject *
  4006. gimp_Uninstall_temp_proc(self, args)
  4007.      PyObject *self;    /* Not used */
  4008.      PyObject *args;
  4009. {
  4010.     char *name;
  4011.  
  4012.     if (!PyArg_ParseTuple(args, "s:uninstall_temp_proc", &name))
  4013.     return NULL;
  4014.     gimp_uninstall_temp_proc(name);
  4015.     Py_INCREF(Py_None);
  4016.     return Py_None;
  4017. }
  4018.  
  4019. static PyObject *
  4020. gimp_Register_magic_load_handler(self, args)
  4021.      PyObject *self;    /* Not used */
  4022.      PyObject *args;
  4023. {
  4024.     char *name, *extensions, *prefixes, *magics;
  4025.     if (!PyArg_ParseTuple(args, "ssss:register_magic_load_handler",
  4026.               &name, &extensions, &prefixes, &magics))
  4027.     return NULL;
  4028.     gimp_register_magic_load_handler(name, extensions, prefixes, magics);
  4029.     Py_INCREF(Py_None);
  4030.     return Py_None;
  4031. }
  4032.  
  4033. static PyObject *
  4034. gimp_Register_load_handler(self, args)
  4035.      PyObject *self;    /* Not used */
  4036.      PyObject *args;
  4037. {
  4038.     char *name, *extensions, *prefixes;
  4039.     if (!PyArg_ParseTuple(args, "sss:register_load_handler",
  4040.               &name, &extensions, &prefixes))
  4041.     return NULL;
  4042.     gimp_register_load_handler(name, extensions, prefixes);
  4043.     Py_INCREF(Py_None);
  4044.     return Py_None;
  4045. }
  4046.  
  4047. static PyObject *
  4048. gimp_Register_save_handler(self, args)
  4049.      PyObject *self;    /* Not used */
  4050.      PyObject *args;
  4051. {
  4052.     char *name, *extensions, *prefixes;
  4053.     if (!PyArg_ParseTuple(args, "sss:register_save_handler",
  4054.               &name, &extensions, &prefixes))
  4055.     return NULL;
  4056.     gimp_register_save_handler(name, extensions, prefixes);
  4057.     Py_INCREF(Py_None);
  4058.     return Py_None;
  4059. }
  4060.  
  4061. static PyObject *
  4062. gimp_Gamma(self, args)
  4063.      PyObject *self;    /* Not used */
  4064.      PyObject *args;
  4065. {
  4066.     if (!PyArg_ParseTuple(args, ":gamma"))
  4067.     return NULL;
  4068.     return PyFloat_FromDouble(gimp_gamma());
  4069. }
  4070.  
  4071. static PyObject *
  4072. gimp_Install_cmap(self, args)
  4073.      PyObject *self;    /* Not used */
  4074.      PyObject *args;
  4075. {
  4076.     if (!PyArg_ParseTuple(args, ":install_cmap"))
  4077.     return NULL;
  4078.     return PyInt_FromLong(gimp_install_cmap());
  4079. }
  4080.  
  4081. static PyObject *
  4082. gimp_Use_xshm(self, args)
  4083.      PyObject *self;    /* Not used */
  4084.      PyObject *args;
  4085. {
  4086.     if (!PyArg_ParseTuple(args, ":use_xshm"))
  4087.     return NULL;
  4088.     return PyInt_FromLong(gimp_use_xshm());
  4089. }
  4090.  
  4091. static PyObject *
  4092. gimp_Color_cube(self, args)
  4093.      PyObject *self;    /* Not used */
  4094.      PyObject *args;
  4095. {
  4096.     if (!PyArg_ParseTuple(args, ":color_cube"))
  4097.     return NULL;
  4098.     return PyString_FromString(gimp_color_cube());
  4099. }
  4100.  
  4101. static PyObject *
  4102. gimp_Gtkrc(self, args)
  4103.      PyObject *self;    /* Not used */
  4104.      PyObject *args;
  4105. {
  4106.     if (!PyArg_ParseTuple(args, ":gtkrc"))
  4107.     return NULL;
  4108.     return PyString_FromString(gimp_gtkrc());
  4109. }
  4110.  
  4111. static PyObject *
  4112. gimp_Get_background(self, args)
  4113.      PyObject *self;    /* Not used */
  4114.      PyObject *args;
  4115. {
  4116.     guchar r, g, b;
  4117.     if (!PyArg_ParseTuple(args, ":get_background"))
  4118.     return NULL;
  4119.     gimp_palette_get_background(&r, &g, &b);
  4120.     return Py_BuildValue("(iii)", (int)r, (int)g, (int)b);
  4121. }
  4122.  
  4123. static PyObject *
  4124. gimp_Get_foreground(self, args)
  4125.      PyObject *self;    /* Not used */
  4126.      PyObject *args;
  4127. {
  4128.     guchar r, g, b;
  4129.     if (!PyArg_ParseTuple(args, ":get_foreground"))
  4130.     return NULL;
  4131.     gimp_palette_get_foreground(&r, &g, &b);
  4132.     return Py_BuildValue("(iii)", (int)r, (int)g, (int)b);
  4133. }
  4134.  
  4135. static PyObject *
  4136. gimp_Set_background(self, args)
  4137.      PyObject *self;    /* Not used */
  4138.      PyObject *args;
  4139. {
  4140.     int r, g, b;
  4141.     if (!PyArg_ParseTuple(args, "(iii):set_background", &r, &g, &b)) {
  4142.     PyErr_Clear();
  4143.     if (!PyArg_ParseTuple(args, "iii:set_background", &r, &g, &b))
  4144.         return NULL;
  4145.     }
  4146.     gimp_palette_set_background(r,g,b);
  4147.     Py_INCREF(Py_None);
  4148.     return Py_None;
  4149. }
  4150.  
  4151. static PyObject *
  4152. gimp_Set_foreground(self, args)
  4153.      PyObject *self;    /* Not used */
  4154.      PyObject *args;
  4155. {
  4156.     int r, g, b;
  4157.     if (!PyArg_ParseTuple(args, "(iii):set_foreground", &r, &g, &b)) {
  4158.     PyErr_Clear();
  4159.     if (!PyArg_ParseTuple(args, "iii:set_foreground", &r, &g, &b))
  4160.         return NULL;
  4161.     }
  4162.     gimp_palette_set_foreground(r,g,b);
  4163.     Py_INCREF(Py_None);
  4164.     return Py_None;
  4165. }
  4166.  
  4167. static PyObject *
  4168. gimp_Gradients_get_list(self, args)
  4169.      PyObject *self;    /* Not used */
  4170.      PyObject *args;
  4171. {
  4172.     char **list;
  4173.     int num, i;
  4174.     PyObject *ret;
  4175.     if (!PyArg_ParseTuple(args, ":gradients_get_list"))
  4176.     return NULL;
  4177.     list = gimp_gradients_get_list(&num);
  4178.     ret = PyList_New(num);
  4179.     for (i = 0; i < num; i++)
  4180.     PyList_SetItem(ret, i, PyString_FromString(list[i]));
  4181.     g_free(list);
  4182.     return ret;
  4183. }
  4184.  
  4185. static PyObject *
  4186. gimp_Gradients_get_active(self, args)
  4187.      PyObject *self;    /* Not used */
  4188.      PyObject *args;
  4189. {
  4190.     if (!PyArg_ParseTuple(args, ":gradients_get_active"))
  4191.     return NULL;
  4192.     return PyString_FromString(gimp_gradients_get_active());
  4193. }
  4194.  
  4195. static PyObject *
  4196. gimp_Gradients_set_active(self, args)
  4197.      PyObject *self;    /* Not used */
  4198.      PyObject *args;
  4199. {
  4200.     char *actv;
  4201.     if (!PyArg_ParseTuple(args, "s:gradients_set_active", &actv))
  4202.     return NULL;
  4203.     gimp_gradients_set_active(actv);
  4204.     Py_INCREF(Py_None);
  4205.     return Py_None;
  4206. }
  4207.  
  4208. static PyObject *
  4209. gimp_Gradients_sample_uniform(self, args)
  4210.      PyObject *self;    /* Not used */
  4211.      PyObject *args;
  4212. {
  4213.     int num, i, j;
  4214.     double *samp;
  4215.     PyObject *ret;
  4216.     if (!PyArg_ParseTuple(args, "i:gradients_sample_uniform", &num))
  4217.     return NULL;
  4218.     samp = gimp_gradients_sample_uniform(num);
  4219.     ret = PyList_New(num);
  4220.     for (i = 0, j = 0; i < num; i++, j += 4)
  4221.     PyList_SetItem(ret, i, Py_BuildValue("(dddd)", samp[j],
  4222.                          samp[j+1], samp[j+2], samp[j+3]));
  4223.     g_free(samp);
  4224.     return ret;
  4225. }
  4226.  
  4227. static PyObject *
  4228. gimp_Gradients_sample_custom(self, args)
  4229.      PyObject *self;    /* Not used */
  4230.      PyObject *args;
  4231. {
  4232.     int num, i, j;
  4233.     double *pos, *samp;
  4234.     PyObject *ret, *item;
  4235.     if (!PyArg_ParseTuple(args, "O:gradients_sample_custom", &ret))
  4236.     return NULL;
  4237.     if (!PySequence_Check(ret)) {
  4238.     PyErr_SetString(PyExc_TypeError,
  4239.             "second arg must be a sequence");
  4240.     return NULL;
  4241.     }
  4242.     num = PySequence_Length(ret);
  4243.     pos = g_new(gdouble, num);
  4244.     for (i = 0; i < num; i++) {
  4245.     item = PySequence_GetItem(ret, i);
  4246.     if (!PyFloat_Check(item)) {
  4247.         PyErr_SetString(PyExc_TypeError,
  4248.                 "second arg must be a sequence of floats");
  4249.         g_free(pos);
  4250.         return NULL;
  4251.     }
  4252.     pos[i] = PyFloat_AsDouble(item);
  4253.     }
  4254.     samp = gimp_gradients_sample_custom(num, pos);
  4255.     g_free(pos);
  4256.     ret = PyList_New(num);
  4257.     for (i = 0, j = 0; i < num; i++, j += 4)
  4258.     PyList_SetItem(ret, i, Py_BuildValue("(dddd)", samp[j],
  4259.                          samp[j+1], samp[j+2], samp[j+3]));
  4260.     g_free(samp);
  4261.     return ret;
  4262. }
  4263.  
  4264.  
  4265. static PyObject *
  4266. gimp_image(self, args)
  4267.      PyObject *self, *args;
  4268. {
  4269.     unsigned int width, height;
  4270.     GimpImageBaseType type;
  4271.  
  4272.     if (!PyArg_ParseTuple(args, "iii:image", &width, &height, &type))
  4273.     return NULL;
  4274.     return (PyObject *)newimgobject(gimp_image_new(width, height, type));
  4275. }
  4276.  
  4277.  
  4278. static PyObject *
  4279. gimp_layer(self, args)
  4280.      PyObject *self, *args;
  4281. {
  4282.     imgobject *img;
  4283.     char *name;
  4284.     unsigned int width, height;
  4285.     GimpImageType type;
  4286.     double opacity;
  4287.     GimpLayerModeEffects mode;
  4288.     
  4289.  
  4290.     if (!PyArg_ParseTuple(args, "O!siiidi:layer", &Imgtype, &img, &name,
  4291.               &width, &height, &type, &opacity, &mode))
  4292.     return NULL;
  4293.     return (PyObject *)newlayobject(gimp_layer_new(img->ID, name, width,
  4294.                            height, type, opacity, mode));
  4295. }
  4296.  
  4297.  
  4298. static PyObject *
  4299. gimp_channel(self, args)
  4300.      PyObject *self, *args;
  4301. {
  4302.     imgobject *img;
  4303.     char *name;
  4304.     unsigned int width, height, r, g, b;
  4305.     double opacity;
  4306.     unsigned char colour[3];
  4307.  
  4308.     if (!PyArg_ParseTuple(args, "O!siid(iii):channel", &Imgtype, &img,
  4309.               &name, &width, &height, &opacity, &r, &g, &b))
  4310.     return NULL;
  4311.     colour[0] = r & 0xff;
  4312.     colour[1] = g & 0xff;
  4313.     colour[2] = b & 0xff;
  4314.     return (PyObject *)newchnobject(gimp_channel_new(img->ID, name,
  4315.                              width, height, opacity, colour));
  4316. }
  4317.  
  4318.  
  4319. static PyObject *
  4320. gimp_display(self, args)
  4321.      PyObject *self, *args;
  4322. {
  4323.     imgobject *img;
  4324.  
  4325.     if (!PyArg_ParseTuple(args, "O!:display", &Imgtype, &img))
  4326.     return NULL;
  4327.     return (PyObject *)newdispobject(gimp_display_new(img->ID));
  4328. }
  4329.  
  4330.  
  4331. static PyObject *
  4332. gimp_delete(self, args)
  4333.      PyObject *self, *args;
  4334. {
  4335.     imgobject *img;
  4336.  
  4337.     if (!PyArg_ParseTuple(args, "O:delete", &img))
  4338.     return NULL;
  4339.     if (img_check(img)) gimp_image_delete(img->ID);
  4340.     else if (lay_check(img)) gimp_layer_delete(img->ID);
  4341.     else if (chn_check(img)) gimp_channel_delete(img->ID);
  4342.     else if (disp_check(img)) gimp_display_delete(img->ID);
  4343.     Py_INCREF(Py_None);
  4344.     return Py_None;
  4345. }
  4346.  
  4347.  
  4348. static PyObject *
  4349. gimp_Displays_flush(self, args)
  4350.      PyObject *self, *args;
  4351. {
  4352.     if (!PyArg_ParseTuple(args, ":flush"))
  4353.     return NULL;
  4354.     gimp_displays_flush();
  4355.     Py_INCREF(Py_None);
  4356.     return Py_None;
  4357. }
  4358.  
  4359.  
  4360. static PyObject *
  4361. gimp_Tile_cache_size(self, args)
  4362.      PyObject *self, *args;
  4363. {
  4364.     unsigned long k;
  4365.     if (!PyArg_ParseTuple(args, "l:tile_cache_size", &k))
  4366.     return NULL;
  4367.     gimp_tile_cache_size(k);
  4368.     Py_INCREF(Py_None);
  4369.     return Py_None;
  4370. }
  4371.  
  4372.  
  4373. static PyObject *
  4374. gimp_Tile_cache_ntiles(self, args)
  4375.      PyObject *self, *args;
  4376. {
  4377.     unsigned long n;
  4378.     if (!PyArg_ParseTuple(args, "l:tile_cache_ntiles", &n))
  4379.     return NULL;
  4380.     gimp_tile_cache_ntiles(n);
  4381.     Py_INCREF(Py_None);
  4382.     return Py_None;
  4383. }
  4384.  
  4385.  
  4386. static PyObject *
  4387. gimp_Tile_width(self, args)
  4388.      PyObject *self, *args;
  4389. {
  4390.     if (PyArg_ParseTuple(args, ":tile_width"))
  4391.     return NULL;
  4392.     return PyInt_FromLong(gimp_tile_width());
  4393. }
  4394.  
  4395.  
  4396. static PyObject *
  4397. gimp_Tile_height(self, args)
  4398.      PyObject *self, *args;
  4399. {
  4400.     if (PyArg_ParseTuple(args, ":tile_height"))
  4401.     return NULL;
  4402.     return PyInt_FromLong(gimp_tile_height());
  4403. }
  4404.  
  4405. void gimp_extension_ack     (void);
  4406. void gimp_extension_process (guint timeout);
  4407.  
  4408. static PyObject *
  4409. gimp_Extension_ack(self, args)
  4410.      PyObject *self, *args;
  4411. {
  4412.     if (!PyArg_ParseTuple(args, ":extension_ack"))
  4413.     return NULL;
  4414.     gimp_extension_ack();
  4415.     Py_INCREF(Py_None);
  4416.     return Py_None;
  4417. }
  4418.  
  4419. static PyObject *
  4420. gimp_Extension_process(self, args)
  4421.      PyObject *self, *args;
  4422. {
  4423.     int timeout;
  4424.  
  4425.     if (!PyArg_ParseTuple(args, "i:extension_process", &timeout))
  4426.     return NULL;
  4427.     gimp_extension_process(timeout);
  4428.     Py_INCREF(Py_None);
  4429.     return Py_None;
  4430. }
  4431.  
  4432. #ifdef GIMP_HAVE_PARASITES
  4433. static PyObject *
  4434. new_parasite(self, args)
  4435.      PyObject *self, *args;
  4436. {
  4437.     char *name, *data;
  4438.     int flags, size;
  4439.     if (!PyArg_ParseTuple(args, "sis#:parasite", &name, &flags,
  4440.               &data, &size))
  4441.     return NULL;
  4442.     return (PyObject *)newparaobject(gimp_parasite_new(name, flags, size, data));
  4443. }
  4444.  
  4445. static PyObject *
  4446. gimp_Parasite_find(self, args)
  4447.      PyObject *self, *args;
  4448. {
  4449.     char *name;
  4450.     if (!PyArg_ParseTuple(args, "s:parasite_find", &name))
  4451.     return NULL;
  4452.     return (PyObject *)newparaobject(gimp_parasite_find(name));
  4453. }
  4454.  
  4455. static PyObject *
  4456. gimp_Parasite_attach(self, args)
  4457.      PyObject *self, *args;
  4458. {
  4459.     paraobject *parasite;
  4460.     if (!PyArg_ParseTuple(args, "O!:parasite_attach", &Paratype, ¶site))
  4461.     return NULL;
  4462.     gimp_parasite_attach(parasite->para);
  4463.     Py_INCREF(Py_None);
  4464.     return Py_None;
  4465. }
  4466.  
  4467. static PyObject *
  4468. gimp_Attach_new_parasite(self, args)
  4469.      PyObject *self, *args;
  4470. {
  4471.     char *name, *data;
  4472.     int flags, size;
  4473.     if (!PyArg_ParseTuple(args, "sis#:attach_new_parasite", &name, &flags,
  4474.               &data, &size))
  4475.     return NULL;
  4476.     gimp_attach_new_parasite(name, flags, size, data);
  4477.     Py_INCREF(Py_None);
  4478.     return Py_None;
  4479. }
  4480.  
  4481. static PyObject *
  4482. gimp_Parasite_detach(self, args)
  4483.      PyObject *self, *args;
  4484. {
  4485.     char *name;
  4486.     if (!PyArg_ParseTuple(args, "s:parasite_detach", &name))
  4487.     return NULL;
  4488.     gimp_parasite_detach(name);
  4489.     Py_INCREF(Py_None);
  4490.     return Py_None;
  4491. }
  4492. #endif
  4493.  
  4494. #ifdef GIMP_HAVE_DEFAULT_DISPLAY
  4495. static PyObject *
  4496. gimp_Default_display(self, args)
  4497.      PyObject *self, *args;
  4498. {
  4499.     if (!PyArg_ParseTuple(args, ":default_display"))
  4500.     return NULL;
  4501.     return (PyObject *)newdispobject(gimp_default_display());
  4502. }
  4503. #endif
  4504.  
  4505. static PyObject *
  4506. id2image(self, args)
  4507.      PyObject *self, *args;
  4508. {
  4509.     int id;
  4510.     if (!PyArg_ParseTuple(args, "i:_id2image", &id))
  4511.     return NULL;
  4512.     if (id >= 0)
  4513.     return (PyObject *)newimgobject(id);
  4514.     Py_INCREF(Py_None);
  4515.     return Py_None;
  4516. }
  4517.  
  4518. static PyObject *
  4519. id2drawable(self, args)
  4520.      PyObject *self, *args;
  4521. {
  4522.     int id;
  4523.     if (!PyArg_ParseTuple(args, "i:_id2drawable", &id))
  4524.     return NULL;
  4525.     if (id >= 0)
  4526.     return (PyObject *)newdrwobject(NULL, id);
  4527.     Py_INCREF(Py_None);
  4528.     return Py_None;
  4529. }
  4530.  
  4531. static PyObject *
  4532. id2display(self, args)
  4533.      PyObject *self, *args;
  4534. {
  4535.     int id;
  4536.     if (!PyArg_ParseTuple(args, "i:_id2display", &id))
  4537.     return NULL;
  4538.     if (id >= 0)
  4539.     return (PyObject *)newdispobject(id);
  4540.     Py_INCREF(Py_None);
  4541.     return Py_None;
  4542. }
  4543.  
  4544. /* List of methods defined in the module */
  4545.  
  4546. static struct PyMethodDef gimp_methods[] = {
  4547.     {"main",    (PyCFunction)gimp_Main,    METH_VARARGS},
  4548.     {"quit",    (PyCFunction)gimp_Quit,    METH_VARARGS},
  4549.     {"set_data",    (PyCFunction)gimp_Set_data,    METH_VARARGS},
  4550.     {"get_data",    (PyCFunction)gimp_Get_data,    METH_VARARGS},
  4551.     {"progress_init",    (PyCFunction)gimp_Progress_init,    METH_VARARGS},
  4552.     {"progress_update",    (PyCFunction)gimp_Progress_update,    METH_VARARGS},
  4553.     {"query_images",    (PyCFunction)gimp_Query_images,    METH_VARARGS},
  4554.     {"install_procedure",    (PyCFunction)gimp_Install_procedure,    METH_VARARGS},
  4555.     {"install_temp_proc",    (PyCFunction)gimp_Install_temp_proc,    METH_VARARGS},
  4556.     {"uninstall_temp_proc",    (PyCFunction)gimp_Uninstall_temp_proc,    METH_VARARGS},
  4557.     {"register_magic_load_handler",    (PyCFunction)gimp_Register_magic_load_handler,    METH_VARARGS},
  4558.     {"register_load_handler",    (PyCFunction)gimp_Register_load_handler,    METH_VARARGS},
  4559.     {"register_save_handler",    (PyCFunction)gimp_Register_save_handler,    METH_VARARGS},
  4560.     {"gamma",    (PyCFunction)gimp_Gamma,    METH_VARARGS},
  4561.     {"install_cmap",    (PyCFunction)gimp_Install_cmap,    METH_VARARGS},
  4562.     {"use_xshm",    (PyCFunction)gimp_Use_xshm,    METH_VARARGS},
  4563.     {"color_cube",    (PyCFunction)gimp_Color_cube,    METH_VARARGS},
  4564.     {"colour_cube", (PyCFunction)gimp_Color_cube,  METH_VARARGS},
  4565.     {"gtkrc",    (PyCFunction)gimp_Gtkrc,    METH_VARARGS},
  4566.     {"get_background",    (PyCFunction)gimp_Get_background,    METH_VARARGS},
  4567.     {"get_foreground",    (PyCFunction)gimp_Get_foreground,    METH_VARARGS},
  4568.     {"set_background",    (PyCFunction)gimp_Set_background,    METH_VARARGS},
  4569.     {"set_foreground",    (PyCFunction)gimp_Set_foreground,    METH_VARARGS},
  4570.     {"gradients_get_list",    (PyCFunction)gimp_Gradients_get_list,    METH_VARARGS},
  4571.     {"gradients_get_active",    (PyCFunction)gimp_Gradients_get_active,    METH_VARARGS},
  4572.     {"gradients_set_active",    (PyCFunction)gimp_Gradients_set_active,    METH_VARARGS},
  4573.     {"gradients_sample_uniform",    (PyCFunction)gimp_Gradients_sample_uniform,    METH_VARARGS},
  4574.     {"gradients_sample_custom",    (PyCFunction)gimp_Gradients_sample_custom,    METH_VARARGS},
  4575.     {"image", (PyCFunction)gimp_image, METH_VARARGS},
  4576.     {"layer", (PyCFunction)gimp_layer, METH_VARARGS},
  4577.     {"channel", (PyCFunction)gimp_channel, METH_VARARGS},
  4578.     {"display", (PyCFunction)gimp_display, METH_VARARGS},
  4579.     {"delete", (PyCFunction)gimp_delete, METH_VARARGS},
  4580.     {"displays_flush", (PyCFunction)gimp_Displays_flush, METH_VARARGS},
  4581.     {"tile_cache_size", (PyCFunction)gimp_Tile_cache_size, METH_VARARGS},
  4582.     {"tile_cache_ntiles", (PyCFunction)gimp_Tile_cache_ntiles, METH_VARARGS},
  4583.     {"tile_width", (PyCFunction)gimp_Tile_width, METH_VARARGS},
  4584.     {"tile_height", (PyCFunction)gimp_Tile_height, METH_VARARGS},
  4585.     {"extension_ack", (PyCFunction)gimp_Extension_ack, METH_VARARGS},
  4586.     {"extension_process", (PyCFunction)gimp_Extension_process, METH_VARARGS},
  4587. #ifdef GIMP_HAVE_PARASITES
  4588.     {"parasite",           (PyCFunction)new_parasite,            METH_VARARGS},
  4589.     {"parasite_find",      (PyCFunction)gimp_Parasite_find,      METH_VARARGS},
  4590.     {"parasite_attach",    (PyCFunction)gimp_Parasite_attach,    METH_VARARGS},
  4591.     {"attach_new_parasite",(PyCFunction)gimp_Attach_new_parasite,METH_VARARGS},
  4592.     {"parasite_detach",    (PyCFunction)gimp_Parasite_detach,    METH_VARARGS},
  4593. #endif
  4594. #ifdef GIMP_HAVE_DEFAULT_DISPLAY
  4595.     {"default_display",  (PyCFunction)gimp_Default_display,  METH_VARARGS},
  4596. #endif
  4597.     {"_id2image", (PyCFunction)id2image, METH_VARARGS},
  4598.     {"_id2drawable", (PyCFunction)id2drawable, METH_VARARGS},
  4599.     {"_id2display", (PyCFunction)id2display, METH_VARARGS},
  4600.     {NULL,     (PyCFunction)NULL, 0, NULL}        /* sentinel */
  4601. };
  4602.  
  4603.  
  4604. /* Initialization function for the module (*must* be called initgimp) */
  4605.  
  4606. static char gimp_module_documentation[] = 
  4607. "This module provides interfaces to allow you to write gimp plugins"
  4608. ;
  4609.  
  4610. void
  4611. initgimp()
  4612. {
  4613.     PyObject *m, *d;
  4614.     PyObject *i;
  4615.  
  4616. #if defined (_MSC_VER)
  4617.     /* see: Python FAQ 3.24 "Initializer not a constant." */
  4618.     Pdbtype.ob_type = &PyType_Type;
  4619.     Pftype.ob_type = &PyType_Type;
  4620.     Imgtype.ob_type = &PyType_Type;
  4621.     Disptype.ob_type = &PyType_Type;
  4622.     Laytype.ob_type = &PyType_Type;
  4623.     Chntype.ob_type = &PyType_Type;
  4624.     Tiletype.ob_type = &PyType_Type;
  4625.     Prtype.ob_type = &PyType_Type;
  4626.     Paratype.ob_type = &PyType_Type;
  4627. #endif
  4628.  
  4629.     /* Create the module and add the functions */
  4630.     m = Py_InitModule4("gimp", gimp_methods,
  4631.                gimp_module_documentation,
  4632.                (PyObject*)NULL,PYTHON_API_VERSION);
  4633.  
  4634.     /* Add some symbolic constants to the module */
  4635.     d = PyModule_GetDict(m);
  4636.     ErrorObject = PyString_FromString("gimp.error");
  4637.     PyDict_SetItemString(d, "error", ErrorObject);
  4638.  
  4639.     PyDict_SetItemString(d, "pdb", (PyObject *)newpdbobject());
  4640.  
  4641.     /* export the types used in gimpmodule */
  4642.     PyDict_SetItemString(d, "ImageType", (PyObject *)&Imgtype);
  4643.     PyDict_SetItemString(d, "LayerType", (PyObject *)&Laytype);
  4644.     PyDict_SetItemString(d, "ChannelType", (PyObject *)&Chntype);
  4645.     PyDict_SetItemString(d, "DisplayType", (PyObject *)&Disptype);
  4646.     PyDict_SetItemString(d, "TileType", (PyObject *)&Tiletype);
  4647.     PyDict_SetItemString(d, "PixelRegionType", (PyObject *)&Prtype);
  4648. #ifdef GIMP_HAVE_PARASITES
  4649.     PyDict_SetItemString(d, "ParasiteType", (PyObject *)&Paratype);
  4650. #endif
  4651.  
  4652.     PyDict_SetItemString(d, "major_version",
  4653.              i=PyInt_FromLong(gimp_major_version));
  4654.     Py_DECREF(i);
  4655.     PyDict_SetItemString(d, "minor_version",
  4656.              i=PyInt_FromLong(gimp_minor_version));
  4657.     Py_DECREF(i);
  4658.     PyDict_SetItemString(d, "micro_version",
  4659.              i=PyInt_FromLong(gimp_micro_version));
  4660.     Py_DECREF(i);
  4661.     
  4662.     /* Check for errors */
  4663.     if (PyErr_Occurred())
  4664.     Py_FatalError("can't initialize module gimp");
  4665. }
  4666.