~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to source/blender/python/api2_2x/Image.c

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2005-11-06 12:40:03 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051106124003-3pgs7tcg5rox96xg
Tags: 2.37a-1.1
* Non-maintainer upload.
* Split out parts of 01_SConstruct_debian.dpatch again: root_build_dir
  really needs to get adjusted before the clean target runs - closes: #333958,
  see #288882 for reference

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* 
 
2
 * $Id: Image.c,v 1.29 2005/05/22 18:43:29 stiv Exp $
2
3
 *
3
4
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4
5
 *
17
18
 *
18
19
 * You should have received a copy of the GNU General Public License
19
20
 * along with this program; if not, write to the Free Software Foundation,
20
 
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
21
 * Inc., 59 Temple Place - Suite 330, Boston, MA        02111-1307, USA.
21
22
 *
22
23
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23
24
 * All rights reserved.
24
25
 *
25
26
 * This is a new part of Blender.
26
27
 *
27
 
 * Contributor(s): Willian P. Germano
 
28
 * Contributor(s): Willian P. Germano, Campbell Barton, Joilnen B. Leite,
 
29
 * Austin Benesh
28
30
 *
29
31
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
30
32
*/
31
33
 
 
34
#include <BDR_drawmesh.h>       /* free_realtime_image */
32
35
#include <BKE_main.h>
33
36
#include <BKE_global.h>
34
37
#include <BKE_library.h>
35
38
#include <BKE_image.h>
 
39
#include <BIF_drawimage.h>
36
40
#include <BLI_blenlib.h>
37
 
#include <IMB_imbuf_types.h> /* for the IB_rect define */
 
41
#include <DNA_space_types.h>    /* FILE_MAXDIR = 160 */
 
42
#include <IMB_imbuf_types.h>    /* for the IB_rect define */
 
43
#include <BIF_gl.h>
38
44
#include "gen_utils.h"
39
45
 
40
46
#include "Image.h"
41
47
 
42
48
/*****************************************************************************/
43
 
/* Python BPy_Image defaults:                                                */
 
49
/* Python BPy_Image defaults:                                                                                                                                                                                            */
44
50
/*****************************************************************************/
45
 
#define EXPP_IMAGE_REP      1
46
 
#define EXPP_IMAGE_REP_MIN  1
 
51
#define EXPP_IMAGE_REP                  1
 
52
#define EXPP_IMAGE_REP_MIN      1
47
53
#define EXPP_IMAGE_REP_MAX 16
48
54
 
49
55
 
52
58
/************************/
53
59
 
54
60
/*****************************************************************************/
55
 
/* Python API function prototypes for the Image module.                      */
56
 
/*****************************************************************************/
57
 
static PyObject *M_Image_New (PyObject *self, PyObject *args,
58
 
                PyObject *keywords);
59
 
static PyObject *M_Image_Get (PyObject *self, PyObject *args);
60
 
static PyObject *M_Image_Load (PyObject *self, PyObject *args);
61
 
 
62
 
/*****************************************************************************/
63
 
/* The following string definitions are used for documentation strings.      */
64
 
/* In Python these will be written to the console when doing a               */
65
 
/* Blender.Image.__doc__                                                     */
66
 
/*****************************************************************************/
67
 
static char M_Image_doc[] =
68
 
"The Blender Image module\n\n";
69
 
 
70
 
static char M_Image_New_doc[] =
71
 
"() - return a new Image object -- unimplemented";
 
61
/* Python API function prototypes for the Image module.  */
 
62
/*****************************************************************************/
 
63
/*static PyObject *M_Image_New( PyObject * self, PyObject * args,
 
64
                              PyObject * keywords );*/
 
65
static PyObject *M_Image_Get( PyObject * self, PyObject * args );
 
66
static PyObject *M_Image_Load( PyObject * self, PyObject * args );
 
67
 
 
68
/*****************************************************************************/
 
69
/* The following string definitions are used for documentation strings.  */
 
70
/* In Python these will be written to the console when doing a           */
 
71
/* Blender.Image.__doc__                 */
 
72
/*****************************************************************************/
 
73
static char M_Image_doc[] = "The Blender Image module\n\n";
 
74
 
 
75
/*static char M_Image_New_doc[] =
 
76
        "() - return a new Image object -- unimplemented";*/
72
77
 
73
78
static char M_Image_Get_doc[] =
74
 
"(name) - return the image with the name 'name', \
 
79
        "(name) - return the image with the name 'name', \
75
80
returns None if not found.\n If 'name' is not specified, \
76
81
it returns a list of all images in the\ncurrent scene.";
77
82
 
78
83
static char M_Image_Load_doc[] =
79
 
"(filename) - return image from file filename as Image Object, \
 
84
        "(filename) - return image from file filename as Image Object, \
80
85
returns None if not found.\n";
81
86
 
82
87
/*****************************************************************************/
83
 
/* Python method structure definition for Blender.Image module:              */
 
88
/* Python method structure definition for Blender.Image module:          */
84
89
/*****************************************************************************/
85
90
struct PyMethodDef M_Image_methods[] = {
86
 
  {"New",(PyCFunction)M_Image_New, METH_VARARGS|METH_KEYWORDS,
87
 
          M_Image_New_doc},
88
 
  {"Get",         M_Image_Get,         METH_VARARGS, M_Image_Get_doc},
89
 
  {"get",         M_Image_Get,         METH_VARARGS, M_Image_Get_doc},
90
 
  {"Load",        M_Image_Load,        METH_VARARGS, M_Image_Load_doc},
91
 
  {NULL, NULL, 0, NULL}
 
91
        /*{"New", ( PyCFunction ) M_Image_New, METH_VARARGS | METH_KEYWORDS,
 
92
           M_Image_New_doc}, */
 
93
        {"Get", M_Image_Get, METH_VARARGS, M_Image_Get_doc},
 
94
        {"get", M_Image_Get, METH_VARARGS, M_Image_Get_doc},
 
95
        {"Load", M_Image_Load, METH_VARARGS, M_Image_Load_doc},
 
96
        {NULL, NULL, 0, NULL}
92
97
};
93
98
 
94
99
/*****************************************************************************/
95
 
/* Function:              M_Image_New                                        */
96
 
/* Python equivalent:     Blender.Image.New                                  */
97
 
/*****************************************************************************/
98
 
static PyObject *M_Image_New(PyObject *self, PyObject *args, PyObject *keywords)
99
 
{
100
 
  printf ("In Image_New() - unimplemented in 2.25\n");
101
 
 
102
 
  Py_INCREF(Py_None);
103
 
  return Py_None;
104
 
}
105
 
 
106
 
/*****************************************************************************/
107
 
/* Function:              M_Image_Get                                        */
108
 
/* Python equivalent:     Blender.Image.Get                                  */
109
 
/* Description:           Receives a string and returns the image object     */
110
 
/*                        whose name matches the string.  If no argument is  */
111
 
/*                        passed in, a list of all image names in the        */
112
 
/*                        current scene is returned.                         */
113
 
/*****************************************************************************/
114
 
static PyObject *M_Image_Get(PyObject *self, PyObject *args)
115
 
{
116
 
  char  *name = NULL;
117
 
  Image *img_iter;
118
 
 
119
 
        if (!PyArg_ParseTuple(args, "|s", &name))
120
 
    return (EXPP_ReturnPyObjError (PyExc_TypeError,
121
 
            "expected string argument (or nothing)"));
122
 
 
123
 
  img_iter = G.main->image.first;
124
 
 
125
 
        if (name) { /* (name) - Search image by name */
126
 
 
127
 
    BPy_Image *wanted_image = NULL;
128
 
 
129
 
    while ((img_iter) && (wanted_image == NULL)) {
130
 
      if (strcmp (name, img_iter->id.name+2) == 0) {
131
 
        wanted_image = (BPy_Image *)PyObject_NEW(BPy_Image, &Image_Type);
132
 
                                if (wanted_image) wanted_image->image = img_iter;
133
 
      }
134
 
      img_iter = img_iter->id.next;
135
 
    }
136
 
 
137
 
    if (wanted_image == NULL) { /* Requested image doesn't exist */
138
 
      char error_msg[64];
139
 
      PyOS_snprintf(error_msg, sizeof(error_msg),
140
 
                      "Image \"%s\" not found", name);
141
 
      return (EXPP_ReturnPyObjError (PyExc_NameError, error_msg));
142
 
    }
143
 
 
144
 
    return (PyObject *)wanted_image;
145
 
        }
146
 
 
147
 
        else { /* () - return a list of all images in the scene */
148
 
    int index = 0;
149
 
    PyObject *img_list, *pyobj;
150
 
 
151
 
    img_list = PyList_New (BLI_countlist (&(G.main->image)));
152
 
 
153
 
    if (img_list == NULL)
154
 
      return (PythonReturnErrorObject (PyExc_MemoryError,
155
 
              "couldn't create PyList"));
156
 
 
157
 
                while (img_iter) {
158
 
      pyobj = Image_CreatePyObject (img_iter);
159
 
 
160
 
                        if (!pyobj)
161
 
                                return (PythonReturnErrorObject (PyExc_MemoryError,
162
 
                                                                        "couldn't create PyObject"));
163
 
 
164
 
                        PyList_SET_ITEM (img_list, index, pyobj);
165
 
 
166
 
      img_iter = img_iter->id.next;
167
 
      index++;
168
 
                }
169
 
 
170
 
                return (img_list);
171
 
        }
172
 
}
173
 
 
174
 
/*****************************************************************************/
175
 
/* Function:              M_Image_Load                                       */
176
 
/* Python equivalent:     Blender.Image.Load                                 */
177
 
/* Description:           Receives a string and returns the image object     */
178
 
/*                        whose filename matches the string.                 */
179
 
/*****************************************************************************/
180
 
static PyObject *M_Image_Load(PyObject *self, PyObject *args)
181
 
{
182
 
  char    *fname;
183
 
  Image   *img_ptr;
184
 
  BPy_Image *img;
185
 
 
186
 
  if (!PyArg_ParseTuple(args, "s", &fname))
187
 
    return (EXPP_ReturnPyObjError (PyExc_TypeError,
188
 
            "expected string argument"));
189
 
 
190
 
  img = (BPy_Image *)PyObject_NEW(BPy_Image, &Image_Type);
191
 
 
192
 
  if (!img)
193
 
    return (EXPP_ReturnPyObjError (PyExc_MemoryError,
194
 
            "couldn't create PyObject Image_Type"));
195
 
 
196
 
  img_ptr = add_image(fname);
197
 
  if (!img_ptr)
198
 
    return (EXPP_ReturnPyObjError (PyExc_IOError,
199
 
            "couldn't load image"));
200
 
 
201
 
  img->image = img_ptr;
202
 
 
203
 
  return (PyObject *)img;
204
 
}
205
 
 
206
 
/*****************************************************************************/
207
 
/* Function:              Image_Init                                         */
208
 
/*****************************************************************************/
209
 
PyObject *Image_Init (void)
210
 
{
211
 
  PyObject  *submodule;
212
 
 
213
 
  Image_Type.ob_type = &PyType_Type;
214
 
 
215
 
  submodule = Py_InitModule3("Blender.Image", M_Image_methods, M_Image_doc);
216
 
 
217
 
  return (submodule);
 
100
/* Function:    M_Image_New      (unimplemented) */
 
101
/* Python equivalent:           Blender.Image.New    */
 
102
/*****************************************************************************/
 
103
/*static PyObject *M_Image_New( PyObject * self, PyObject * args,
 
104
        PyObject * keywords )
 
105
{
 
106
        Py_INCREF( Py_None );
 
107
        return Py_None;
 
108
}
 
109
*/
 
110
/*****************************************************************************/
 
111
/* Function:            M_Image_Get      */
 
112
/* Python equivalent:   Blender.Image.Get   */
 
113
/* Description:         Receives a string and returns the image object   */
 
114
/*                      whose name matches the string.  If no argument is  */
 
115
/*                      passed in, a list of all image names in the      */
 
116
/*                      current scene is returned.                       */
 
117
/*****************************************************************************/
 
118
static PyObject *M_Image_Get( PyObject * self, PyObject * args )
 
119
{
 
120
        char *name = NULL;
 
121
        Image *img_iter;
 
122
 
 
123
        if( !PyArg_ParseTuple( args, "|s", &name ) )
 
124
                return ( EXPP_ReturnPyObjError( PyExc_TypeError,
 
125
                                                "expected string argument (or nothing)" ) );
 
126
 
 
127
        img_iter = G.main->image.first;
 
128
 
 
129
        if( name ) {            /* (name) - Search image by name */
 
130
 
 
131
                BPy_Image *wanted_image = NULL;
 
132
 
 
133
                while( ( img_iter ) && ( wanted_image == NULL ) ) {
 
134
                        if( strcmp( name, img_iter->id.name + 2 ) == 0 ) {
 
135
                                wanted_image = ( BPy_Image * )
 
136
                                        PyObject_NEW( BPy_Image, &Image_Type );
 
137
                                if( wanted_image )
 
138
                                        wanted_image->image = img_iter;
 
139
                        }
 
140
                        img_iter = img_iter->id.next;
 
141
                }
 
142
 
 
143
                if( wanted_image == NULL ) {    /* Requested image doesn't exist */
 
144
                        char error_msg[64];
 
145
                        PyOS_snprintf( error_msg, sizeof( error_msg ),
 
146
                                       "Image \"%s\" not found", name );
 
147
                        return ( EXPP_ReturnPyObjError
 
148
                                 ( PyExc_NameError, error_msg ) );
 
149
                }
 
150
 
 
151
                return ( PyObject * ) wanted_image;
 
152
        }
 
153
 
 
154
        else {                  /* () - return a list of all images in the scene */
 
155
                int index = 0;
 
156
                PyObject *img_list, *pyobj;
 
157
 
 
158
                img_list = PyList_New( BLI_countlist( &( G.main->image ) ) );
 
159
 
 
160
                if( img_list == NULL )
 
161
                        return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
 
162
                                                        "couldn't create PyList" ) );
 
163
 
 
164
                while( img_iter ) {
 
165
                        pyobj = Image_CreatePyObject( img_iter );
 
166
 
 
167
                        if( !pyobj )
 
168
                                return ( EXPP_ReturnPyObjError
 
169
                                         ( PyExc_MemoryError,
 
170
                                           "couldn't create PyObject" ) );
 
171
 
 
172
                        PyList_SET_ITEM( img_list, index, pyobj );
 
173
 
 
174
                        img_iter = img_iter->id.next;
 
175
                        index++;
 
176
                }
 
177
 
 
178
                return ( img_list );
 
179
        }
 
180
}
 
181
 
 
182
/*****************************************************************************/
 
183
/* Function:    M_Image_Load             */
 
184
/* Python equivalent:   Blender.Image.Load   */
 
185
/* Description:         Receives a string and returns the image object   */
 
186
/*                      whose filename matches the string.               */
 
187
/*****************************************************************************/
 
188
static PyObject *M_Image_Load( PyObject * self, PyObject * args )
 
189
{
 
190
        char *fname;
 
191
        Image *img_ptr;
 
192
        BPy_Image *img;
 
193
 
 
194
        if( !PyArg_ParseTuple( args, "s", &fname ) )
 
195
                return ( EXPP_ReturnPyObjError( PyExc_TypeError,
 
196
                                                "expected string argument" ) );
 
197
 
 
198
        img = ( BPy_Image * ) PyObject_NEW( BPy_Image, &Image_Type );
 
199
 
 
200
        if( !img )
 
201
                return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
 
202
                                                "couldn't create PyObject Image_Type" ) );
 
203
 
 
204
        img_ptr = add_image( fname );
 
205
        if( !img_ptr )
 
206
                return ( EXPP_ReturnPyObjError( PyExc_IOError,
 
207
                                                "couldn't load image" ) );
 
208
 
 
209
        img->image = img_ptr;
 
210
 
 
211
        return ( PyObject * ) img;
 
212
}
 
213
 
 
214
 
 
215
/**
 
216
 * getPixelF( x, y )
 
217
 *  returns float list of pixel colors in rgba order.
 
218
 *  returned values are floats normalized to 0.0 - 1.0.
 
219
 *  blender images are all 4x8 bit at the moment apr-2005
 
220
 */
 
221
 
 
222
static PyObject *Image_getPixelF( BPy_Image * self, PyObject * args )
 
223
{
 
224
 
 
225
        PyObject *attr;
 
226
        Image *image = self->image;
 
227
        char *pixel;    /* image data */
 
228
        int index;              /* offset into image data */
 
229
        int x = 0;
 
230
        int y = 0;
 
231
        int pixel_size = 4;     /* each pixel is 4 x 8-bits packed in unsigned int */
 
232
 
 
233
        if( !PyArg_ParseTuple( args, "ii", &x, &y ) )
 
234
                return EXPP_ReturnPyObjError( PyExc_TypeError,
 
235
                                              "expected 2 integers" );
 
236
 
 
237
        if( !image->ibuf || !image->ibuf->rect )        /* if no image data available */
 
238
                load_image( image, IB_rect, "", 0 );    /* loading it */
 
239
 
 
240
        if( !image->ibuf || !image->ibuf->rect )        /* didn't work */
 
241
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
242
                                              "couldn't load image data in Blender" );
 
243
 
 
244
        if( image->ibuf->type == 1 )    /* bitplane image */
 
245
                return EXPP_ReturnPyObjError( PyExc_TypeError,
 
246
                                              "unsupported bitplane image format" );
 
247
 
 
248
        if( x > ( image->ibuf->x - 1 )
 
249
            || y > ( image->ibuf->y - 1 )
 
250
            || x < image->ibuf->xorig || y < image->ibuf->yorig )
 
251
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
252
                                              "x or y is out of range" );
 
253
 
 
254
        /* 
 
255
           assumption: from looking at source, skipx is often not set,
 
256
           so we calc ourselves
 
257
         */
 
258
 
 
259
        index = ( x + y * image->ibuf->x ) * pixel_size;
 
260
 
 
261
        pixel = ( char * ) image->ibuf->rect;
 
262
        attr = Py_BuildValue( "[f,f,f,f]",
 
263
                              ( ( float ) pixel[index] ) / 255.0,
 
264
                              ( ( float ) pixel[index + 1] ) / 255.0,
 
265
                              ( ( float ) pixel[index + 2] ) / 255.0,
 
266
                              ( ( float ) pixel[index + 3] / 255.0 ) );
 
267
 
 
268
        if( attr )  /* normal return */
 
269
                return attr;
 
270
 
 
271
        return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
272
                                      "couldn't get pixel colors" );
 
273
}
 
274
 
 
275
 
 
276
static PyObject *Image_getMaxXY( BPy_Image * self )
 
277
{
 
278
        Image *image = self->image;
 
279
        PyObject *attr;
 
280
 
 
281
        if( !image->ibuf || !image->ibuf->rect )        /* if no image data available */
 
282
                load_image( image, IB_rect, "", 0 );    /* loading it */
 
283
 
 
284
        if( !image->ibuf || !image->ibuf->rect )        /* didn't work */
 
285
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
286
                                              "couldn't load image data in Blender" );
 
287
 
 
288
        attr = Py_BuildValue( "[i,i]", image->ibuf->x, image->ibuf->y );
 
289
        if( attr )
 
290
                return attr;
 
291
        return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
292
                                      "could not determine max x or y" );
 
293
 
 
294
}
 
295
 
 
296
/*****************************************************************************/
 
297
/* Function:            Image_Init       */
 
298
/*****************************************************************************/
 
299
PyObject *Image_Init( void )
 
300
{
 
301
        PyObject *submodule;
 
302
 
 
303
        Image_Type.ob_type = &PyType_Type;
 
304
 
 
305
        submodule =
 
306
                Py_InitModule3( "Blender.Image", M_Image_methods,
 
307
                                M_Image_doc );
 
308
 
 
309
        return ( submodule );
218
310
}
219
311
 
220
312
/************************/
222
314
/************************/
223
315
 
224
316
/*****************************************************************************/
225
 
/* Python BPy_Image methods declarations:                                    */
 
317
/* Python BPy_Image methods declarations:        */
226
318
/*****************************************************************************/
227
 
static PyObject *Image_getName(BPy_Image *self);
228
 
static PyObject *Image_getFilename(BPy_Image *self);
229
 
static PyObject *Image_getSize(BPy_Image *self);
230
 
static PyObject *Image_getDepth(BPy_Image *self);
231
 
static PyObject *Image_getXRep(BPy_Image *self);
232
 
static PyObject *Image_getYRep(BPy_Image *self);
233
 
static PyObject *Image_setName(BPy_Image *self, PyObject *args);
234
 
static PyObject *Image_setXRep(BPy_Image *self, PyObject *args);
235
 
static PyObject *Image_setYRep(BPy_Image *self, PyObject *args);
 
319
static PyObject *Image_getName( BPy_Image * self );
 
320
static PyObject *Image_getFilename( BPy_Image * self );
 
321
static PyObject *Image_getSize( BPy_Image * self );
 
322
static PyObject *Image_getDepth( BPy_Image * self );
 
323
static PyObject *Image_getXRep( BPy_Image * self );
 
324
static PyObject *Image_getYRep( BPy_Image * self );
 
325
static PyObject *Image_getBindCode( BPy_Image * self );
 
326
static PyObject *Image_setName( BPy_Image * self, PyObject * args );
 
327
static PyObject *Image_setFilename( BPy_Image * self, PyObject * args );
 
328
static PyObject *Image_setXRep( BPy_Image * self, PyObject * args );
 
329
static PyObject *Image_setYRep( BPy_Image * self, PyObject * args );
 
330
static PyObject *Image_reload( BPy_Image * self );
 
331
static PyObject *Image_glLoad( BPy_Image * self );
 
332
static PyObject *Image_glFree( BPy_Image * self );
 
333
static PyObject *Image_getPixelF( BPy_Image * self, PyObject * args );
 
334
static PyObject *Image_getMaxXY( BPy_Image * self );
236
335
 
237
336
/*****************************************************************************/
238
 
/* Python BPy_Image methods table:                                           */
 
337
/* Python BPy_Image methods table:       */
239
338
/*****************************************************************************/
240
339
static PyMethodDef BPy_Image_methods[] = {
241
 
 /* name, method, flags, doc */
242
 
  {"getName", (PyCFunction)Image_getName, METH_NOARGS,
243
 
          "() - Return Image object name"},
244
 
  {"getFilename", (PyCFunction)Image_getFilename, METH_NOARGS,
245
 
          "() - Return Image object filename"},
246
 
  {"getSize", (PyCFunction)Image_getSize, METH_NOARGS,
247
 
          "() - Return Image object [width, height] dimension in pixels"},
248
 
  {"getDepth", (PyCFunction)Image_getDepth, METH_NOARGS,
249
 
          "() - Return Image object pixel depth"},
250
 
  {"getXRep", (PyCFunction)Image_getXRep, METH_NOARGS,
251
 
          "() - Return Image object x repetition value"},
252
 
  {"getYRep", (PyCFunction)Image_getYRep, METH_NOARGS,
253
 
          "() - Return Image object y repetition value"},
254
 
  {"setName", (PyCFunction)Image_setName, METH_VARARGS,
255
 
          "(str) - Change Image object name"},
256
 
  {"setXRep", (PyCFunction)Image_setXRep, METH_VARARGS,
257
 
          "(int) - Change Image object x repetition value"},
258
 
  {"setYRep", (PyCFunction)Image_setYRep, METH_VARARGS,
259
 
          "(int) - Change Image object y repetition value"},
260
 
  {0}
261
 
};
262
 
 
263
 
/*****************************************************************************/
264
 
/* Python Image_Type callback function prototypes:                           */
265
 
/*****************************************************************************/
266
 
static void Image_dealloc (BPy_Image *self);
267
 
static int Image_setAttr (BPy_Image *self, char *name, PyObject *v);
268
 
static int Image_compare (BPy_Image *a, BPy_Image *b);
269
 
static PyObject *Image_getAttr (BPy_Image *self, char *name);
270
 
static PyObject *Image_repr (BPy_Image *self);
271
 
 
272
 
/*****************************************************************************/
273
 
/* Python Image_Type structure definition:                                   */
274
 
/*****************************************************************************/
275
 
PyTypeObject Image_Type =
276
 
{
277
 
  PyObject_HEAD_INIT(NULL)
278
 
  0,                                     /* ob_size */
279
 
  "Blender Image",                       /* tp_name */
280
 
  sizeof (BPy_Image),                    /* tp_basicsize */
281
 
  0,                                     /* tp_itemsize */
282
 
  /* methods */
283
 
  (destructor)Image_dealloc,             /* tp_dealloc */
284
 
  0,                                     /* tp_print */
285
 
  (getattrfunc)Image_getAttr,            /* tp_getattr */
286
 
  (setattrfunc)Image_setAttr,            /* tp_setattr */
287
 
  (cmpfunc)Image_compare,                /* tp_compare */
288
 
  (reprfunc)Image_repr,                  /* tp_repr */
289
 
  0,                                     /* tp_as_number */
290
 
  0,                                     /* tp_as_sequence */
291
 
  0,                                     /* tp_as_mapping */
292
 
  0,                                     /* tp_as_hash */
293
 
  0,0,0,0,0,0,
294
 
  0,                                     /* tp_doc */ 
295
 
  0,0,0,0,0,0,
296
 
  BPy_Image_methods,                       /* tp_methods */
297
 
  0,                                     /* tp_members */
298
 
};
299
 
 
300
 
/*****************************************************************************/
301
 
/* Function:    Image_dealloc                                                */
302
 
/* Description: This is a callback function for the BPy_Image type. It is    */
303
 
/*              the destructor function.                                     */
304
 
/*****************************************************************************/
305
 
static void Image_dealloc (BPy_Image *self)
306
 
{
307
 
  PyObject_DEL (self);
 
340
        /* name, method, flags, doc */
 
341
        {"getPixelF", ( PyCFunction ) Image_getPixelF, METH_VARARGS,
 
342
         "(float, float) - Get colors of specified pixel as [r,g,b,a]"},
 
343
        {"getMaxXY", ( PyCFunction ) Image_getMaxXY, METH_VARARGS,
 
344
         "() - Get maximum x & y coordinates of current image as [x, y]"},
 
345
        {"getName", ( PyCFunction ) Image_getName, METH_NOARGS,
 
346
         "() - Return Image object name"},
 
347
        {"getFilename", ( PyCFunction ) Image_getFilename, METH_NOARGS,
 
348
         "() - Return Image object filename"},
 
349
        {"getSize", ( PyCFunction ) Image_getSize, METH_NOARGS,
 
350
         "() - Return Image object [width, height] dimension in pixels"},
 
351
        {"getDepth", ( PyCFunction ) Image_getDepth, METH_NOARGS,
 
352
         "() - Return Image object pixel depth"},
 
353
        {"getXRep", ( PyCFunction ) Image_getXRep, METH_NOARGS,
 
354
         "() - Return Image object x repetition value"},
 
355
        {"getYRep", ( PyCFunction ) Image_getYRep, METH_NOARGS,
 
356
         "() - Return Image object y repetition value"},
 
357
        {"getBindCode", ( PyCFunction ) Image_getBindCode, METH_NOARGS,
 
358
         "() - Return Image object's bind code value"},
 
359
        {"reload", ( PyCFunction ) Image_reload, METH_NOARGS,
 
360
         "() - Reload the image from the filesystem"},
 
361
        {"glLoad", ( PyCFunction ) Image_glLoad, METH_NOARGS,
 
362
         "() - Load the image data in OpenGL texture memory.\n\
 
363
        The bindcode (int) is returned."},
 
364
        {"glFree", ( PyCFunction ) Image_glFree, METH_NOARGS,
 
365
         "() - Free the image data from OpenGL texture memory only,\n\
 
366
                see also image.glLoad()."},
 
367
        {"setName", ( PyCFunction ) Image_setName, METH_VARARGS,
 
368
         "(str) - Change Image object name"},
 
369
        {"setFilename", ( PyCFunction ) Image_setFilename, METH_VARARGS,
 
370
         "(str) - Change Image file name"},
 
371
        {"setXRep", ( PyCFunction ) Image_setXRep, METH_VARARGS,
 
372
         "(int) - Change Image object x repetition value"},
 
373
        {"setYRep", ( PyCFunction ) Image_setYRep, METH_VARARGS,
 
374
         "(int) - Change Image object y repetition value"},
 
375
        {NULL, NULL, 0, NULL}
 
376
};
 
377
 
 
378
/*****************************************************************************/
 
379
/* Python Image_Type callback function prototypes:       */
 
380
/*****************************************************************************/
 
381
static void Image_dealloc( BPy_Image * self );
 
382
static int Image_setAttr( BPy_Image * self, char *name, PyObject * v );
 
383
static int Image_compare( BPy_Image * a, BPy_Image * b );
 
384
static PyObject *Image_getAttr( BPy_Image * self, char *name );
 
385
static PyObject *Image_repr( BPy_Image * self );
 
386
 
 
387
/*****************************************************************************/
 
388
/* Python Image_Type structure definition:   */
 
389
/*****************************************************************************/
 
390
PyTypeObject Image_Type = {
 
391
        PyObject_HEAD_INIT( NULL )  /*     required macro. ( no comma needed )  */
 
392
        0,      /* ob_size */
 
393
        "Blender Image",        /* tp_name */
 
394
        sizeof( BPy_Image ),    /* tp_basicsize */
 
395
        0,                      /* tp_itemsize */
 
396
        /* methods */
 
397
        ( destructor ) Image_dealloc,   /* tp_dealloc */
 
398
        0,                      /* tp_print */
 
399
        ( getattrfunc ) Image_getAttr,  /* tp_getattr */
 
400
        ( setattrfunc ) Image_setAttr,  /* tp_setattr */
 
401
        ( cmpfunc ) Image_compare,      /* tp_compare */
 
402
        ( reprfunc ) Image_repr,        /* tp_repr */
 
403
        0,                      /* tp_as_number */
 
404
        0,                      /* tp_as_sequence */
 
405
        0,                      /* tp_as_mapping */
 
406
        0,                      /* tp_as_hash */
 
407
        0, 0, 0, 0, 0, 0,
 
408
        0,                      /* tp_doc */
 
409
        0, 0, 0, 0, 0, 0,
 
410
        BPy_Image_methods,      /* tp_methods */
 
411
        0,                      /* tp_members */
 
412
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      /* up to tp_del, to avoid a warning */
 
413
};
 
414
 
 
415
/*****************************************************************************/
 
416
/* Function:            Image_dealloc            */
 
417
/* Description: This is a callback function for the BPy_Image type. It is  */
 
418
/*              the destructor function.         */
 
419
/*****************************************************************************/
 
420
static void Image_dealloc( BPy_Image * self )
 
421
{
 
422
        PyObject_DEL( self );
308
423
}
309
424
 
310
425
/*****************************************************************************/
311
 
/* Function:    Image_CreatePyObject                                         */
312
 
/* Description: This function will create a new BPy_Image from an existing   */
313
 
/*              Blender image structure.                                     */
 
426
/* Function:            Image_CreatePyObject     */
 
427
/* Description: This function will create a new BPy_Image from an existing  */
 
428
/*              Blender image structure.         */
314
429
/*****************************************************************************/
315
 
PyObject *Image_CreatePyObject (Image *image)
 
430
PyObject *Image_CreatePyObject( Image * image )
316
431
{
317
432
        BPy_Image *py_img;
318
433
 
319
 
        py_img = (BPy_Image *)PyObject_NEW (BPy_Image, &Image_Type);
 
434
        py_img = ( BPy_Image * ) PyObject_NEW( BPy_Image, &Image_Type );
320
435
 
321
 
        if (!py_img)
322
 
                return EXPP_ReturnPyObjError (PyExc_MemoryError,
323
 
                                                "couldn't create BPy_Image object");
 
436
        if( !py_img )
 
437
                return EXPP_ReturnPyObjError( PyExc_MemoryError,
 
438
                                              "couldn't create BPy_Image object" );
324
439
 
325
440
        py_img->image = image;
326
441
 
327
 
        return (PyObject *)py_img;
 
442
        return ( PyObject * ) py_img;
328
443
}
329
444
 
330
445
/*****************************************************************************/
331
 
/* Function:    Image_CheckPyObject                                          */
 
446
/* Function:            Image_CheckPyObject      */
332
447
/* Description: This function returns true when the given PyObject is of the */
333
 
/*              type Image. Otherwise it will return false.                  */
334
 
/*****************************************************************************/
335
 
int Image_CheckPyObject (PyObject *pyobj)
336
 
{
337
 
        return (pyobj->ob_type == &Image_Type);
338
 
}
339
 
 
340
 
/*****************************************************************************/
341
 
/* Function:    Image_FromPyObject                                           */
342
 
/* Description: Returns the Blender Image associated with this object        */
343
 
/*****************************************************************************/
344
 
Image *Image_FromPyObject (PyObject *pyobj)
345
 
{
346
 
        return ((BPy_Image *)pyobj)->image;
347
 
}
348
 
 
349
 
/*****************************************************************************/
350
 
/* Python BPy_Image methods:                                                 */
351
 
/*****************************************************************************/
352
 
static PyObject *Image_getName(BPy_Image *self)
353
 
{
354
 
  PyObject *attr = PyString_FromString(self->image->id.name+2);
355
 
 
356
 
  if (attr) return attr;
357
 
 
358
 
  return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
359
 
          "couldn't get Image.name attribute"));
360
 
}
361
 
 
362
 
static PyObject *Image_getFilename(BPy_Image *self)
363
 
{
364
 
  PyObject *attr = PyString_FromString(self->image->name);
365
 
 
366
 
  if (attr) return attr;
367
 
 
368
 
  return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
369
 
          "couldn't get Image.filename attribute"));
370
 
}
371
 
 
372
 
static PyObject *Image_getSize(BPy_Image *self)
373
 
{
374
 
        PyObject *attr;
375
 
        Image *image = self->image;
376
 
 
377
 
        if (!image->ibuf) /* if no image data available */
378
 
                load_image(image, IB_rect, "", 0); /* loading it */
379
 
 
380
 
        if (!image->ibuf) /* didn't work */
381
 
                return EXPP_ReturnPyObjError (PyExc_RuntimeError,
382
 
                                        "couldn't load image data in Blender");
383
 
 
384
 
        attr = Py_BuildValue("[hh]", image->ibuf->x, image->ibuf->y);
385
 
 
386
 
        if (attr) return attr;
387
 
 
388
 
  return EXPP_ReturnPyObjError (PyExc_RuntimeError,
389
 
          "couldn't get Image.size attribute");
390
 
}
391
 
 
392
 
static PyObject *Image_getDepth(BPy_Image *self)
393
 
{
394
 
        PyObject *attr;
395
 
        Image *image = self->image;
396
 
 
397
 
        if (!image->ibuf) /* if no image data available */
398
 
                load_image(image, IB_rect, "", 0); /* loading it */
399
 
 
400
 
        if (!image->ibuf) /* didn't work */
401
 
                return EXPP_ReturnPyObjError (PyExc_RuntimeError,
402
 
                                        "couldn't load image data in Blender");
403
 
 
404
 
        attr = Py_BuildValue("h", image->ibuf->depth);
405
 
 
406
 
        if (attr) return attr;
407
 
 
408
 
  return EXPP_ReturnPyObjError (PyExc_RuntimeError,
409
 
          "couldn't get Image.depth attribute");
410
 
}
411
 
 
412
 
 
413
 
static PyObject *Image_getXRep(BPy_Image *self)
414
 
{
415
 
  PyObject *attr = PyInt_FromLong(self->image->xrep);
416
 
 
417
 
  if (attr) return attr;
418
 
 
419
 
  return EXPP_ReturnPyObjError (PyExc_RuntimeError,
420
 
          "couldn't get Image.xrep attribute");
421
 
}
422
 
 
423
 
static PyObject *Image_getYRep(BPy_Image *self)
424
 
{
425
 
  PyObject *attr = PyInt_FromLong(self->image->yrep);
426
 
 
427
 
  if (attr) return attr;
428
 
 
429
 
  return EXPP_ReturnPyObjError (PyExc_RuntimeError,
430
 
          "couldn't get Image.yrep attribute");
431
 
}
432
 
 
433
 
static PyObject *Image_setName(BPy_Image *self, PyObject *args)
434
 
{
435
 
  char *name;
436
 
  char buf[21];
437
 
 
438
 
  if (!PyArg_ParseTuple(args, "s", &name))
439
 
    return (EXPP_ReturnPyObjError (PyExc_TypeError,
440
 
            "expected string argument"));
441
 
  
442
 
  PyOS_snprintf(buf, sizeof(buf), "%s", name);
443
 
  
444
 
  rename_id(&self->image->id, buf);
445
 
 
446
 
  Py_INCREF(Py_None);
447
 
  return Py_None;
448
 
}
449
 
 
450
 
static PyObject *Image_setXRep(BPy_Image *self, PyObject *args)
451
 
{
452
 
  short value;
453
 
 
454
 
  if (!PyArg_ParseTuple(args, "h", &value))
455
 
    return (EXPP_ReturnPyObjError (PyExc_TypeError,
456
 
            "expected int argument in [1,16]"));
457
 
 
458
 
  if (value >= EXPP_IMAGE_REP_MIN || value <= EXPP_IMAGE_REP_MAX)
459
 
    self->image->xrep = value;
460
 
  else
461
 
    return (EXPP_ReturnPyObjError (PyExc_ValueError,
462
 
            "expected int argument in [1,16]"));
463
 
 
464
 
  Py_INCREF(Py_None);
465
 
  return Py_None;
466
 
}
467
 
 
468
 
static PyObject *Image_setYRep(BPy_Image *self, PyObject *args)
469
 
{
470
 
  short value;
471
 
 
472
 
  if (!PyArg_ParseTuple(args, "h", &value))
473
 
    return (EXPP_ReturnPyObjError (PyExc_TypeError,
474
 
            "expected int argument in [1,16]"));
475
 
 
476
 
  if (value >= EXPP_IMAGE_REP_MIN || value <= EXPP_IMAGE_REP_MAX)
477
 
    self->image->yrep = value;
478
 
  else
479
 
    return (EXPP_ReturnPyObjError (PyExc_ValueError,
480
 
            "expected int argument in [1,16]"));
481
 
 
482
 
  Py_INCREF(Py_None);
483
 
  return Py_None;
484
 
}
485
 
 
486
 
/*****************************************************************************/
487
 
/* Function:    Image_getAttr                                                */
488
 
/* Description: This is a callback function for the BPy_Image type. It is    */
489
 
/*              the function that accesses BPy_Image member variables and    */
490
 
/*              methods.                                                     */
491
 
/*****************************************************************************/
492
 
static PyObject *Image_getAttr (BPy_Image *self, char *name)
493
 
{
494
 
  PyObject *attr = Py_None;
495
 
 
496
 
  if (strcmp(name, "name") == 0)
497
 
    attr = PyString_FromString(self->image->id.name+2);
498
 
  else if (strcmp(name, "filename") == 0)
499
 
    attr = PyString_FromString(self->image->name);
500
 
  else if (strcmp(name, "size") == 0)
501
 
    attr = Image_getSize(self);
502
 
  else if (strcmp(name, "depth") == 0)
503
 
    attr = Image_getDepth(self);
504
 
  else if (strcmp(name, "xrep") == 0)
505
 
    attr = PyInt_FromLong(self->image->xrep);
506
 
  else if (strcmp(name, "yrep") == 0)
507
 
    attr = PyInt_FromLong(self->image->yrep);
508
 
 
509
 
  else if (strcmp(name, "__members__") == 0)
510
 
    attr = Py_BuildValue("[s,s,s,s,s,s]",
511
 
                    "name", "filename", "size", "depth", "xrep", "yrep");
512
 
 
513
 
  if (!attr)
514
 
    return (EXPP_ReturnPyObjError (PyExc_MemoryError,
515
 
                            "couldn't create PyObject"));
516
 
 
517
 
  if (attr != Py_None) return attr; /* attribute found, return its value */
518
 
 
519
 
  /* not an attribute, search the methods table */
520
 
  return Py_FindMethod(BPy_Image_methods, (PyObject *)self, name);
521
 
}
522
 
 
523
 
/*****************************************************************************/
524
 
/* Function:    Image_setAttr                                                */
 
448
/*              type Image. Otherwise it will return false.      */
 
449
/*****************************************************************************/
 
450
int Image_CheckPyObject( PyObject * pyobj )
 
451
{
 
452
        return ( pyobj->ob_type == &Image_Type );
 
453
}
 
454
 
 
455
/*****************************************************************************/
 
456
/* Function:    Image_FromPyObject       */
 
457
/* Description: Returns the Blender Image associated with this object    */
 
458
/*****************************************************************************/
 
459
Image *Image_FromPyObject( PyObject * pyobj )
 
460
{
 
461
        return ( ( BPy_Image * ) pyobj )->image;
 
462
}
 
463
 
 
464
/*****************************************************************************/
 
465
/* Python BPy_Image methods:             */
 
466
/*****************************************************************************/
 
467
static PyObject *Image_getName( BPy_Image * self )
 
468
{
 
469
        PyObject *attr = PyString_FromString( self->image->id.name + 2 );
 
470
 
 
471
        if( attr )
 
472
                return attr;
 
473
 
 
474
        return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
475
                                        "couldn't get Image.name attribute" ) );
 
476
}
 
477
 
 
478
static PyObject *Image_getFilename( BPy_Image * self )
 
479
{
 
480
        PyObject *attr = PyString_FromString( self->image->name );
 
481
 
 
482
        if( attr )
 
483
                return attr;
 
484
 
 
485
        return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
486
                                        "couldn't get Image.filename attribute" ) );
 
487
}
 
488
 
 
489
static PyObject *Image_getSize( BPy_Image * self )
 
490
{
 
491
        PyObject *attr;
 
492
        Image *image = self->image;
 
493
 
 
494
        if( !image->ibuf )      /* if no image data available */
 
495
                load_image( image, IB_rect, "", 0 );    /* loading it */
 
496
 
 
497
        if( !image->ibuf )      /* didn't work */
 
498
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
499
                                              "couldn't load image data in Blender" );
 
500
 
 
501
        attr = Py_BuildValue( "[hh]", image->ibuf->x, image->ibuf->y );
 
502
 
 
503
        if( attr )
 
504
                return attr;
 
505
 
 
506
        return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
507
                                      "couldn't get Image.size attribute" );
 
508
}
 
509
 
 
510
static PyObject *Image_getDepth( BPy_Image * self )
 
511
{
 
512
        PyObject *attr;
 
513
        Image *image = self->image;
 
514
 
 
515
        if( !image->ibuf )      /* if no image data available */
 
516
                load_image( image, IB_rect, "", 0 );    /* loading it */
 
517
 
 
518
        if( !image->ibuf )      /* didn't work */
 
519
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
520
                                              "couldn't load image data in Blender" );
 
521
 
 
522
        attr = Py_BuildValue( "h", image->ibuf->depth );
 
523
 
 
524
        if( attr )
 
525
                return attr;
 
526
 
 
527
        return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
528
                                      "couldn't get Image.depth attribute" );
 
529
}
 
530
 
 
531
 
 
532
static PyObject *Image_getXRep( BPy_Image * self )
 
533
{
 
534
        PyObject *attr = PyInt_FromLong( self->image->xrep );
 
535
 
 
536
        if( attr )
 
537
                return attr;
 
538
 
 
539
        return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
540
                                      "couldn't get Image.xrep attribute" );
 
541
}
 
542
 
 
543
static PyObject *Image_getYRep( BPy_Image * self )
 
544
{
 
545
        PyObject *attr = PyInt_FromLong( self->image->yrep );
 
546
 
 
547
        if( attr )
 
548
                return attr;
 
549
 
 
550
        return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
551
                                      "couldn't get Image.yrep attribute" );
 
552
}
 
553
 
 
554
static PyObject *Image_getBindCode( BPy_Image * self )
 
555
{
 
556
        PyObject *attr = PyLong_FromUnsignedLong( self->image->bindcode );
 
557
 
 
558
        if( attr )
 
559
                return attr;
 
560
 
 
561
        return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
562
                                      "couldn't get Image.bindcode attribute" );
 
563
}
 
564
 
 
565
static PyObject *Image_reload( BPy_Image * self )
 
566
{
 
567
        Image *img = self->image;
 
568
 
 
569
        free_image_buffers( img );      /* force read again */
 
570
        img->ok = 1;
 
571
        if( G.sima )
 
572
                image_changed( G.sima, 0 );
 
573
 
 
574
        Py_INCREF( Py_None );
 
575
        return Py_None;
 
576
}
 
577
 
 
578
static PyObject *Image_glFree( BPy_Image * self )
 
579
{
 
580
        Image *img = self->image;
 
581
 
 
582
        free_realtime_image( img );
 
583
        return EXPP_incr_ret( Py_None );
 
584
}
 
585
 
 
586
static PyObject *Image_glLoad( BPy_Image * self )
 
587
{
 
588
        Image *img = self->image;
 
589
        unsigned int *bind = &img->bindcode;
 
590
 
 
591
        if( *bind == 0 ) {
 
592
 
 
593
                if( !img->ibuf )        /* if no image data is available */
 
594
                        load_image( img, IB_rect, "", 0 );      /* loading it */
 
595
 
 
596
                if( !img->ibuf )        /* didn't work */
 
597
                        return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
598
                                                      "couldn't load image data in Blender" );
 
599
 
 
600
                glGenTextures( 1, ( GLuint * ) bind );
 
601
                glBindTexture( GL_TEXTURE_2D, *bind );
 
602
 
 
603
                gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, img->ibuf->x,
 
604
                                   img->ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE,
 
605
                                   img->ibuf->rect );
 
606
                glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
 
607
                                 GL_LINEAR_MIPMAP_NEAREST );
 
608
                glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
 
609
                                 GL_LINEAR );
 
610
                glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
 
611
 
 
612
                glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, img->ibuf->x,
 
613
                              img->ibuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE,
 
614
                              img->ibuf->rect );
 
615
        }
 
616
 
 
617
        return PyLong_FromUnsignedLong( img->bindcode );
 
618
}
 
619
 
 
620
static PyObject *Image_setName( BPy_Image * self, PyObject * args )
 
621
{
 
622
        char *name;
 
623
        char buf[21];
 
624
 
 
625
        if( !PyArg_ParseTuple( args, "s", &name ) )
 
626
                return ( EXPP_ReturnPyObjError( PyExc_TypeError,
 
627
                                                "expected string argument" ) );
 
628
 
 
629
        PyOS_snprintf( buf, sizeof( buf ), "%s", name );
 
630
 
 
631
        rename_id( &self->image->id, buf );
 
632
 
 
633
        Py_INCREF( Py_None );
 
634
        return Py_None;
 
635
}
 
636
 
 
637
static PyObject *Image_setFilename( BPy_Image * self, PyObject * args )
 
638
{
 
639
        char *name;
 
640
        int namelen = 0;
 
641
 
 
642
        /* max len is FILE_MAXDIR = 160 chars like done in DNA_image_types.h */
 
643
 
 
644
        if( !PyArg_ParseTuple( args, "s#", &name, &namelen ) )
 
645
                return ( EXPP_ReturnPyObjError( PyExc_TypeError,
 
646
                                                "expected a string argument" ) );
 
647
 
 
648
        if( namelen >= FILE_MAXDIR )
 
649
                return ( EXPP_ReturnPyObjError( PyExc_TypeError,
 
650
                                                "string argument is limited to 160 chars at most" ) );
 
651
 
 
652
        PyOS_snprintf( self->image->name, FILE_MAXDIR * sizeof( char ), "%s",
 
653
                       name );
 
654
 
 
655
        Py_INCREF( Py_None );
 
656
        return Py_None;
 
657
}
 
658
 
 
659
static PyObject *Image_setXRep( BPy_Image * self, PyObject * args )
 
660
{
 
661
        short value;
 
662
 
 
663
        if( !PyArg_ParseTuple( args, "h", &value ) )
 
664
                return ( EXPP_ReturnPyObjError( PyExc_TypeError,
 
665
                                                "expected int argument in [1,16]" ) );
 
666
 
 
667
        if( value >= EXPP_IMAGE_REP_MIN || value <= EXPP_IMAGE_REP_MAX )
 
668
                self->image->xrep = value;
 
669
        else
 
670
                return ( EXPP_ReturnPyObjError( PyExc_ValueError,
 
671
                                                "expected int argument in [1,16]" ) );
 
672
 
 
673
        Py_INCREF( Py_None );
 
674
        return Py_None;
 
675
}
 
676
 
 
677
static PyObject *Image_setYRep( BPy_Image * self, PyObject * args )
 
678
{
 
679
        short value;
 
680
 
 
681
        if( !PyArg_ParseTuple( args, "h", &value ) )
 
682
                return ( EXPP_ReturnPyObjError( PyExc_TypeError,
 
683
                                                "expected int argument in [1,16]" ) );
 
684
 
 
685
        if( value >= EXPP_IMAGE_REP_MIN || value <= EXPP_IMAGE_REP_MAX )
 
686
                self->image->yrep = value;
 
687
        else
 
688
                return ( EXPP_ReturnPyObjError( PyExc_ValueError,
 
689
                                                "expected int argument in [1,16]" ) );
 
690
 
 
691
        Py_INCREF( Py_None );
 
692
        return Py_None;
 
693
}
 
694
 
 
695
/*****************************************************************************/
 
696
/* Function:            Image_getAttr            */
 
697
/* Description: This is a callback function for the BPy_Image type. It is */
 
698
/*              the function that accesses BPy_Image member variables and   */
 
699
/*              methods.         */
 
700
/*****************************************************************************/
 
701
static PyObject *Image_getAttr( BPy_Image * self, char *name )
 
702
{
 
703
        PyObject *attr = Py_None;
 
704
 
 
705
        if( strcmp( name, "name" ) == 0 )
 
706
                attr = PyString_FromString( self->image->id.name + 2 );
 
707
        else if( strcmp( name, "filename" ) == 0 )
 
708
                attr = PyString_FromString( self->image->name );
 
709
        else if( strcmp( name, "size" ) == 0 )
 
710
                attr = Image_getSize( self );
 
711
        else if( strcmp( name, "depth" ) == 0 )
 
712
                attr = Image_getDepth( self );
 
713
        else if( strcmp( name, "xrep" ) == 0 )
 
714
                attr = PyInt_FromLong( self->image->xrep );
 
715
        else if( strcmp( name, "yrep" ) == 0 )
 
716
                attr = PyInt_FromLong( self->image->yrep );
 
717
        else if( strcmp( name, "bindcode" ) == 0 )
 
718
                attr = PyInt_FromLong( self->image->bindcode );
 
719
        else if( strcmp( name, "users" ) == 0 )
 
720
                attr = PyInt_FromLong( self->image->id.us );
 
721
        else if( strcmp( name, "__members__" ) == 0 )
 
722
                attr = Py_BuildValue( "[s,s,s,s,s,s,s,s]",
 
723
                                      "name", "filename", "size", "depth",
 
724
                                      "xrep", "yrep", "bindcode", "users" );
 
725
 
 
726
        if( !attr )
 
727
                return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
 
728
                                                "couldn't create PyObject" ) );
 
729
 
 
730
        if( attr != Py_None )
 
731
                return attr;    /* attribute found, return its value */
 
732
 
 
733
        /* not an attribute, search the methods table */
 
734
        return Py_FindMethod( BPy_Image_methods, ( PyObject * ) self, name );
 
735
}
 
736
 
 
737
/*****************************************************************************/
 
738
/* Function:            Image_setAttr            */
525
739
/* Description: This is a callback function for the BPy_Image type. It is the*/
526
 
/*              function that changes Image object members values. If this   */
527
 
/*              data is linked to a Blender Image, it also gets updated.     */
 
740
/*              function that changes Image object members values. If this  */
 
741
/*              data is linked to a Blender Image, it also gets updated.  */
528
742
/*****************************************************************************/
529
 
static int Image_setAttr (BPy_Image *self, char *name, PyObject *value)
 
743
static int Image_setAttr( BPy_Image * self, char *name, PyObject * value )
530
744
{
531
 
  PyObject *valtuple; 
532
 
  PyObject *error = NULL;
 
745
        PyObject *valtuple;
 
746
        PyObject *error = NULL;
533
747
 
534
 
/* We're playing a trick on the Python API users here.  Even if they use
 
748
/* We're playing a trick on the Python API users here.  Even if they use
535
749
 * Image.member = val instead of Image.setMember(value), we end up using the
536
750
 * function anyway, since it already has error checking, clamps to the right
537
751
 * interval and updates the Blender Image structure when necessary. */
538
752
 
539
 
  valtuple = Py_BuildValue("(O)", value); /*the set* functions expect a tuple*/
540
 
 
541
 
  if (!valtuple)
542
 
    return EXPP_ReturnIntError(PyExc_MemoryError,
543
 
                  "ImageSetAttr: couldn't create PyTuple");
544
 
 
545
 
  if (strcmp (name, "name") == 0)
546
 
    error = Image_setName (self, valtuple);
547
 
  else if (strcmp (name, "xrep") == 0)
548
 
    error = Image_setXRep (self, valtuple);
549
 
  else if (strcmp (name, "yrep") == 0)
550
 
    error = Image_setYRep (self, valtuple);
551
 
  else { /* Error: no such member in the Image object structure */
552
 
    Py_DECREF(value);
553
 
    Py_DECREF(valtuple);
554
 
    return (EXPP_ReturnIntError (PyExc_KeyError,
555
 
            "attribute not found or immutable"));
556
 
  }
557
 
 
558
 
  Py_DECREF(valtuple);
559
 
 
560
 
  if (error != Py_None) return -1;
561
 
 
562
 
  Py_DECREF(Py_None); /* incref'ed by the called set* function */
563
 
  return 0; /* normal exit */
 
753
        valtuple = Py_BuildValue( "(O)", value );       /*the set* functions expect a tuple */
 
754
 
 
755
        if( !valtuple )
 
756
                return EXPP_ReturnIntError( PyExc_MemoryError,
 
757
                                            "ImageSetAttr: couldn't create PyTuple" );
 
758
 
 
759
        if( strcmp( name, "name" ) == 0 )
 
760
                error = Image_setName( self, valtuple );
 
761
        if( strcmp( name, "filename" ) == 0 )
 
762
                error = Image_setFilename( self, valtuple );
 
763
        else if( strcmp( name, "xrep" ) == 0 )
 
764
                error = Image_setXRep( self, valtuple );
 
765
        else if( strcmp( name, "yrep" ) == 0 )
 
766
                error = Image_setYRep( self, valtuple );
 
767
        else {                  /* Error: no such member in the Image object structure */
 
768
                Py_DECREF( value );
 
769
                Py_DECREF( valtuple );
 
770
                return ( EXPP_ReturnIntError( PyExc_KeyError,
 
771
                                              "attribute not found or immutable" ) );
 
772
        }
 
773
 
 
774
        Py_DECREF( valtuple );
 
775
 
 
776
        if( error != Py_None )
 
777
                return -1;
 
778
 
 
779
        Py_DECREF( Py_None );   /* incref'ed by the called set* function */
 
780
        return 0;               /* normal exit */
564
781
}
565
782
 
566
783
/*****************************************************************************/
567
 
/* Function:    Image_compare                                                */
568
 
/* Description: This is a callback function for the BPy_Image type. It       */
569
 
/*              compares two Image_Type objects. Only the "==" and "!="      */
570
 
/*              comparisons are meaninful. Returns 0 for equality and -1 if  */
571
 
/*              they don't point to the same Blender Image struct.           */
572
 
/*              In Python it becomes 1 if they are equal, 0 otherwise.       */
 
784
/* Function:    Image_compare                    */
 
785
/* Description: This is a callback function for the BPy_Image type. It   */
 
786
/*              compares two Image_Type objects. Only the "==" and "!="  */
 
787
/*              comparisons are meaninful. Returns 0 for equality and -1 if  */
 
788
/*              they don't point to the same Blender Image struct.       */
 
789
/*              In Python it becomes 1 if they are equal, 0 otherwise.   */
573
790
/*****************************************************************************/
574
 
static int Image_compare (BPy_Image *a, BPy_Image *b)
 
791
static int Image_compare( BPy_Image * a, BPy_Image * b )
575
792
{
576
793
        Image *pa = a->image, *pb = b->image;
577
 
        return (pa == pb) ? 0:-1;
 
794
        return ( pa == pb ) ? 0 : -1;
578
795
}
579
796
 
580
797
/*****************************************************************************/
581
 
/* Function:    Image_repr                                                   */
582
 
/* Description: This is a callback function for the BPy_Image type. It       */
583
 
/*              builds a meaninful string to represent image objects.        */
 
798
/* Function:    Image_repr       */
 
799
/* Description: This is a callback function for the BPy_Image type. It   */
 
800
/*              builds a meaninful string to represent image objects.    */
584
801
/*****************************************************************************/
585
 
static PyObject *Image_repr (BPy_Image *self)
 
802
static PyObject *Image_repr( BPy_Image * self )
586
803
{
587
 
  return PyString_FromFormat("[Image \"%s\"]", self->image->id.name+2);
 
804
        return PyString_FromFormat( "[Image \"%s\"]",
 
805
                                    self->image->id.name + 2 );
588
806
}