~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: 2007-05-17 11:47:59 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20070517114759-yp4ybrnhp2u7pk66
Tags: 2.44-1
* New upstream release.
* Drop debian/patches/01_64bits_stupidity, not needed anymore: as of this
  version blender is 64 bits safe again. Adjust README.Debian accordingly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* 
2
 
 * $Id: Image.c,v 1.62 2006/12/27 05:04:17 campbellbarton Exp $
 
2
 * $Id: Image.c,v 1.76 2007/05/03 15:10:44 bebraw Exp $
3
3
 *
4
4
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5
5
 *
44
44
#include "IMB_imbuf_types.h"    /* for the IB_rect define */
45
45
#include "BIF_gl.h"
46
46
#include "gen_utils.h"
 
47
#include "gen_library.h"
47
48
#include "BKE_packedFile.h"
48
49
#include "DNA_packedFile_types.h"
49
50
#include "BKE_icons.h"
62
63
#define EXPP_IMAGE_REP_MAX 16
63
64
 
64
65
 
 
66
enum img_consts {
 
67
        EXPP_IMAGE_ATTR_XREP = 0,
 
68
        EXPP_IMAGE_ATTR_YREP,
 
69
        EXPP_IMAGE_ATTR_START,
 
70
        EXPP_IMAGE_ATTR_END,
 
71
        EXPP_IMAGE_ATTR_SPEED,
 
72
        EXPP_IMAGE_ATTR_BINDCODE,
 
73
        EXPP_IMAGE_ATTR_SOURCE,
 
74
};
 
75
 
65
76
/************************/
66
77
/*** The Image Module ***/
67
78
/************************/
78
89
/*****************************************************************************/
79
90
/* Python BPy_Image methods declarations:        */
80
91
/*****************************************************************************/
81
 
static PyObject *Image_getName( BPy_Image * self );
82
92
static PyObject *Image_getFilename( BPy_Image * self );
83
93
static PyObject *Image_getSize( BPy_Image * self );
84
94
static PyObject *Image_getDepth( BPy_Image * self );
88
98
static PyObject *Image_getStart( BPy_Image * self );
89
99
static PyObject *Image_getEnd( BPy_Image * self );
90
100
static PyObject *Image_getSpeed( BPy_Image * self );
91
 
static PyObject *Image_setName( BPy_Image * self, PyObject * args );
92
 
static PyObject *Image_setFilename( BPy_Image * self, PyObject * args );
 
101
static int Image_setFilename( BPy_Image * self, PyObject * args );
 
102
static PyObject *Image_oldsetFilename( BPy_Image * self, PyObject * args );
93
103
static PyObject *Image_setXRep( BPy_Image * self, PyObject * args );
94
104
static PyObject *Image_setYRep( BPy_Image * self, PyObject * args );
95
105
static PyObject *Image_setStart( BPy_Image * self, PyObject * args );
127
137
         "() - Get maximum x & y coordinates of current image as [x, y]"},
128
138
        {"getMinXY", ( PyCFunction ) Image_getMinXY, METH_NOARGS,
129
139
         "() - Get minimun x & y coordinates of image as [x, y]"},
130
 
        {"getName", ( PyCFunction ) Image_getName, METH_NOARGS,
 
140
        {"getName", ( PyCFunction ) GenericLib_getName, METH_NOARGS,
131
141
         "() - Return Image object name"},
132
142
        {"getFilename", ( PyCFunction ) Image_getFilename, METH_NOARGS,
133
143
         "() - Return Image object filename"},
155
165
        {"glFree", ( PyCFunction ) Image_glFree, METH_NOARGS,
156
166
         "() - Free the image data from OpenGL texture memory only,\n\
157
167
                see also image.glLoad()."},
158
 
        {"setName", ( PyCFunction ) Image_setName, METH_VARARGS,
 
168
        {"setName", ( PyCFunction ) GenericLib_setName_with_method, METH_VARARGS,
159
169
         "(str) - Change Image object name"},
160
 
        {"setFilename", ( PyCFunction ) Image_setFilename, METH_VARARGS,
 
170
        {"setFilename", ( PyCFunction ) Image_oldsetFilename, METH_VARARGS,
161
171
         "(str) - Change Image file name"},
162
172
        {"setXRep", ( PyCFunction ) Image_setXRep, METH_VARARGS,
163
173
         "(int) - Change Image object x repetition value"},
225
235
{
226
236
        int width, height, depth;
227
237
        char *name;
 
238
        float color[] = {0, 0, 0, 1};
228
239
        Image *image;
229
240
        if( !PyArg_ParseTuple( args, "siii", &name, &width, &height, &depth ) )
230
241
                return ( EXPP_ReturnPyObjError( PyExc_TypeError,
232
243
        if (width > 5000 || height > 5000 || width < 1 || height < 1)
233
244
                return ( EXPP_ReturnPyObjError( PyExc_TypeError,
234
245
                                        "Image width and height must be between 1 and 5000" ) );
235
 
        image = BKE_add_image_size(width, height, name, 0);
 
246
        image = BKE_add_image_size(width, height, name, 0, color);
236
247
        if( !image )
237
248
                return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
238
249
                                                "couldn't create PyObject Image_Type" ) );
532
543
        pixel[index + 2] = ( char ) ( p[2] * 255.0 );
533
544
        pixel[index + 3] = ( char ) ( p[3] * 255.0 );
534
545
 
 
546
        ibuf->userflags |= IB_BITMAPDIRTY;
535
547
        Py_RETURN_NONE;
536
548
}
537
549
 
587
599
        pixel[index + 1] = ( char ) p[1];
588
600
        pixel[index + 2] = ( char ) p[2];
589
601
        pixel[index + 3] = ( char ) p[3];
590
 
 
 
602
        
 
603
        ibuf->userflags |= IB_BITMAPDIRTY;
591
604
        Py_RETURN_NONE;
592
605
}
593
606
 
661
674
static PyObject *Image_pack( BPy_Image * self )
662
675
{
663
676
        Image *image = self->image;     
664
 
        char expandpath[FILE_MAXDIR + FILE_MAXFILE];
665
 
        BLI_strncpy(expandpath, image->name, FILE_MAXDIR+FILE_MAXFILE);
666
 
        BLI_convertstringcode(expandpath, G.sce, 1);
667
 
 
668
 
        if (image->packedfile )
669
 
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
670
 
                                              "image alredy packed" );
671
 
        if (!BLI_exists(expandpath))
672
 
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
673
 
                                              "image path does not exist" );
674
 
                
675
 
        image->packedfile = newPackedFile(image->name);
 
677
        ImBuf *ibuf = BKE_image_get_ibuf(image, NULL);
 
678
        
 
679
        if( !ibuf || !ibuf->rect )      /* didn't work */
 
680
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
681
                                      "couldn't load image data in Blender" );
 
682
        
 
683
        if (image->packedfile ) { /* RePack? */
 
684
                if (ibuf->userflags & IB_BITMAPDIRTY)
 
685
                        BKE_image_memorypack(image);
 
686
        } else { /* Pack for the first time */
 
687
                if (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))
 
688
                        BKE_image_memorypack(image);
 
689
                else
 
690
                        image->packedfile = newPackedFile(image->name);
 
691
        }
676
692
        Py_RETURN_NONE;
677
693
}
678
694
 
679
695
 
680
696
static PyObject *Image_makeCurrent( BPy_Image * self )
681
697
{
 
698
#if 0   /* add back in when bpy becomes "official" */
 
699
        static char warning = 1;
 
700
        if( warning ) {
 
701
                printf("image.makeCurrent() deprecated!\n\t use 'bpy.images.active = image instead'\n");
 
702
                --warning;
 
703
        }
 
704
#endif
 
705
        
682
706
        if (!G.sima)
683
707
                Py_RETURN_FALSE;
684
708
        
700
724
        Py_RETURN_NONE;         /*  normal return, image saved */
701
725
}
702
726
 
 
727
static PyObject *M_Image_SourceDict( void )
 
728
{
 
729
        PyObject *Dict = PyConstant_New(  );
 
730
        if( Dict ) {
 
731
                BPy_constant *d = ( BPy_constant * ) Dict;
 
732
                PyConstant_Insert(d, "STILL", PyInt_FromLong(IMA_SRC_FILE));
 
733
                PyConstant_Insert(d, "MOVIE", PyInt_FromLong(IMA_SRC_MOVIE));
 
734
                PyConstant_Insert(d, "SEQUENCE", PyInt_FromLong(IMA_SRC_SEQUENCE));
 
735
                PyConstant_Insert(d, "GENERATED", PyInt_FromLong(IMA_SRC_GENERATED));
 
736
        }
 
737
        return Dict;
 
738
}
 
739
 
703
740
/*****************************************************************************/
704
741
/* Function:            Image_Init       */
705
742
/*****************************************************************************/
706
743
PyObject *Image_Init( void )
707
744
{
708
745
        PyObject *submodule;
 
746
        PyObject *Sources = M_Image_SourceDict( );
709
747
 
710
 
        Image_Type.ob_type = &PyType_Type;
 
748
        if( PyType_Ready( &Image_Type ) < 0 )
 
749
                return NULL;
711
750
 
712
751
        submodule =
713
752
                Py_InitModule3( "Blender.Image", M_Image_methods,
714
753
                                M_Image_doc );
715
754
 
716
 
        return ( submodule );
 
755
        if( Sources )
 
756
                PyModule_AddObject( submodule, "Sources", Sources );
 
757
 
 
758
        return submodule;
717
759
}
718
760
 
719
761
/*****************************************************************************/
720
762
/* Python Image_Type callback function prototypes:       */
721
763
/*****************************************************************************/
722
 
static void Image_dealloc( BPy_Image * self );
723
 
static int Image_setAttr( BPy_Image * self, char *name, PyObject * v );
724
764
static int Image_compare( BPy_Image * a, BPy_Image * b );
725
 
static PyObject *Image_getAttr( BPy_Image * self, char *name );
726
765
static PyObject *Image_repr( BPy_Image * self );
727
766
 
728
767
/*****************************************************************************/
729
 
/* Python Image_Type structure definition:   */
730
 
/*****************************************************************************/
731
 
PyTypeObject Image_Type = {
732
 
        PyObject_HEAD_INIT( NULL ) /*     required macro. ( no comma needed )  */ 
733
 
        0,      /* ob_size */
734
 
        "Blender Image",        /* tp_name */
735
 
        sizeof( BPy_Image ),    /* tp_basicsize */
736
 
        0,                      /* tp_itemsize */
737
 
        /* methods */
738
 
        ( destructor ) Image_dealloc,   /* tp_dealloc */
739
 
        0,                      /* tp_print */
740
 
        ( getattrfunc ) Image_getAttr,  /* tp_getattr */
741
 
        ( setattrfunc ) Image_setAttr,  /* tp_setattr */
742
 
        ( cmpfunc ) Image_compare,      /* tp_compare */
743
 
        ( reprfunc ) Image_repr,        /* tp_repr */
744
 
        0,                      /* tp_as_number */
745
 
        0,                      /* tp_as_sequence */
746
 
        0,                      /* tp_as_mapping */
747
 
        0,                      /* tp_as_hash */
748
 
        0, 0, 0, 0, 0, 0,
749
 
        0,                      /* tp_doc */
750
 
        0, 0, 0, 0, 0, 0,
751
 
        BPy_Image_methods,      /* tp_methods */
752
 
        0,                      /* tp_members */
753
 
        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 */
754
 
};
755
 
 
756
 
/*****************************************************************************/
757
 
/* Function:            Image_dealloc            */
758
 
/* Description: This is a callback function for the BPy_Image type. It is  */
759
 
/*              the destructor function.         */
760
 
/*****************************************************************************/
761
 
static void Image_dealloc( BPy_Image * self )
762
 
{
763
 
        PyObject_DEL( self );
764
 
}
765
 
 
766
 
/*****************************************************************************/
767
768
/* Function:            Image_CreatePyObject     */
768
769
/* Description: This function will create a new BPy_Image from an existing  */
769
770
/*              Blender image structure.         */
782
783
}
783
784
 
784
785
/*****************************************************************************/
785
 
/* Function:            Image_CheckPyObject      */
786
 
/* Description: This function returns true when the given PyObject is of the */
787
 
/*              type Image. Otherwise it will return false.      */
788
 
/*****************************************************************************/
789
 
int Image_CheckPyObject( PyObject * pyobj )
790
 
{
791
 
        return ( pyobj->ob_type == &Image_Type );
792
 
}
793
 
 
794
 
/*****************************************************************************/
795
786
/* Function:    Image_FromPyObject       */
796
787
/* Description: Returns the Blender Image associated with this object    */
797
788
/*****************************************************************************/
800
791
        return ( ( BPy_Image * ) pyobj )->image;
801
792
}
802
793
 
803
 
/*****************************************************************************/
804
 
/* Python BPy_Image methods:             */
805
 
/*****************************************************************************/
806
 
static PyObject *Image_getName( BPy_Image * self )
807
 
{
808
 
        PyObject *attr = PyString_FromString( self->image->id.name + 2 );
809
 
 
810
 
        if( attr )
811
 
                return attr;
812
 
 
813
 
        return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
814
 
                                        "couldn't get Image.name attribute" ) );
815
 
}
816
 
 
817
794
static PyObject *Image_getFilename( BPy_Image * self )
818
795
{
819
796
        PyObject *attr = PyString_FromString( self->image->name );
839
816
        
840
817
        PyList_SetItem( attr, 0, PyInt_FromLong(ibuf->x));
841
818
        PyList_SetItem( attr, 1, PyInt_FromLong(ibuf->y));
842
 
        return attr;    
843
 
 
844
 
 
 
819
        return attr;
845
820
}
846
821
 
847
822
static PyObject *Image_getDepth( BPy_Image * self )
986
961
        return PyLong_FromUnsignedLong( image->bindcode );
987
962
}
988
963
 
989
 
static PyObject *Image_setName( BPy_Image * self, PyObject * args )
990
 
{
991
 
        char *name;
992
 
 
993
 
        if( !PyArg_ParseTuple( args, "s", &name ) )
994
 
                return ( EXPP_ReturnPyObjError( PyExc_TypeError,
995
 
                                                "expected string argument" ) );
996
 
 
997
 
        rename_id( &self->image->id, name );
998
 
 
999
 
        Py_RETURN_NONE;
1000
 
}
1001
 
 
1002
 
static PyObject *Image_setFilename( BPy_Image * self, PyObject * args )
1003
 
{
1004
 
        char *name;
1005
 
        int namelen = 0;
 
964
static int Image_setFilename( BPy_Image * self, PyObject * value )
 
965
{
 
966
        char *name;
 
967
 
 
968
        name = PyString_AsString(value);
 
969
 
 
970
        if( !name )
 
971
                return ( EXPP_ReturnIntError( PyExc_TypeError,
 
972
                                                "expected a string argument" ) );
1006
973
 
1007
974
        /* max len is FILE_MAXDIR == 160, FILE_MAXFILE == 80 chars like done in DNA_image_types.h */
1008
 
 
1009
 
        if( !PyArg_ParseTuple( args, "s#", &name, &namelen ) )
1010
 
                return ( EXPP_ReturnPyObjError( PyExc_TypeError,
1011
 
                                                "expected a string argument" ) );
1012
 
 
1013
 
        if( namelen >= FILE_MAXDIR + FILE_MAXFILE )
1014
 
                return ( EXPP_ReturnPyObjError( PyExc_TypeError,
 
975
        if( strlen(name) >= FILE_MAXDIR + FILE_MAXFILE )
 
976
                return ( EXPP_ReturnIntError( PyExc_TypeError,
1015
977
                                                "string argument is limited to 240 chars at most" ) );
1016
978
 
1017
 
        PyOS_snprintf( self->image->name, (FILE_MAXDIR + FILE_MAXFILE) * sizeof( char ), "%s",
1018
 
                       name );
 
979
        strcpy( self->image->name, name );
 
980
        return 0;
 
981
}
1019
982
 
1020
 
        Py_RETURN_NONE;
 
983
static PyObject *Image_oldsetFilename( BPy_Image * self, PyObject * args )
 
984
{
 
985
        return EXPP_setterWrapper( (void *)self, args, (setter)Image_setFilename );
1021
986
}
1022
987
 
1023
988
static PyObject *Image_setXRep( BPy_Image * self, PyObject * args )
1107
1072
        Py_RETURN_NONE;
1108
1073
}
1109
1074
 
1110
 
/*****************************************************************************/
1111
 
/* Function:            Image_getAttr            */
1112
 
/* Description: This is a callback function for the BPy_Image type. It is */
1113
 
/*              the function that accesses BPy_Image member variables and   */
1114
 
/*              methods.         */
1115
 
/*****************************************************************************/
1116
 
static PyObject *Image_getAttr( BPy_Image * self, char *name )
1117
 
{
1118
 
        PyObject *attr = Py_None;
1119
 
 
1120
 
        if( strcmp( name, "name" ) == 0 )
1121
 
                attr = PyString_FromString( self->image->id.name + 2 );
1122
 
        else if ( strcmp( name, "properties" ) == 0 )
1123
 
                return BPy_Wrap_IDProperty( (ID*)self->image, IDP_GetProperties((ID*)self->image, 1), NULL );
1124
 
        else if( strcmp( name, "filename" ) == 0 )
1125
 
                attr = PyString_FromString( self->image->name );
1126
 
        else if( strcmp( name, "size" ) == 0 )
1127
 
                return Image_getSize( self );
1128
 
        else if( strcmp( name, "depth" ) == 0 )
1129
 
                return Image_getDepth( self );
1130
 
        else if( strcmp( name, "xrep" ) == 0 )
1131
 
                attr = PyInt_FromLong( self->image->xrep );
1132
 
        else if( strcmp( name, "yrep" ) == 0 )
1133
 
                attr = PyInt_FromLong( self->image->yrep );
1134
 
        else if( strcmp( name, "start" ) == 0 )
1135
 
                attr = PyInt_FromLong( self->image->twsta );
1136
 
        else if( strcmp( name, "end" ) == 0 )
1137
 
                attr = PyInt_FromLong( self->image->twend );
1138
 
        else if( strcmp( name, "speed" ) == 0 )
1139
 
                attr = PyInt_FromLong( self->image->animspeed );
1140
 
        else if( strcmp( name, "packed" ) == 0 ) {
1141
 
                if (self->image->packedfile) attr = Py_True;
1142
 
                else attr = Py_False;
1143
 
                EXPP_incr_ret(attr);
1144
 
        } else if( strcmp( name, "has_data" ) == 0 ) {
1145
 
                if (self->image->ibufs.first) attr = Py_True;
1146
 
                else attr = Py_False;
1147
 
                EXPP_incr_ret(attr);
1148
 
        } else if( strcmp( name, "fields" ) == 0 ) {
1149
 
                if (self->image->flag & IMA_FIELDS) attr = Py_True;
1150
 
                else attr = Py_False;
1151
 
                EXPP_incr_ret(attr);
1152
 
        } else if( strcmp( name, "fields_odd" ) == 0 ) {
1153
 
                if (self->image->flag & IMA_STD_FIELD) attr = Py_True;
1154
 
                else attr = Py_False;
1155
 
                EXPP_incr_ret(attr);
1156
 
        } else if( strcmp( name, "antialias" ) == 0 ) {
1157
 
                if (self->image->flag & IMA_ANTIALI) attr = Py_True;
1158
 
                else attr = Py_False;
1159
 
                EXPP_incr_ret(attr);
1160
 
                
1161
 
        } else if( strcmp( name, "bindcode" ) == 0 )
1162
 
                attr = PyInt_FromLong( self->image->bindcode );
1163
 
        else if( strcmp( name, "users" ) == 0 )
1164
 
                attr = PyInt_FromLong( self->image->id.us );
1165
 
        else if( strcmp( name, "__members__" ) == 0 )
1166
 
                attr = Py_BuildValue( "[s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s]",
1167
 
                                      "name", "filename", "size", "depth",
1168
 
                                      "xrep", "yrep", "start", "end",
1169
 
                                      "speed", "packed", "has_data"
1170
 
                                      "fields", "odd", "antialias",
1171
 
                                          "bindcode", "users" );
1172
 
 
1173
 
        if( !attr )
1174
 
                return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
1175
 
                                                "couldn't create PyObject" ) );
1176
 
 
1177
 
        if( attr != Py_None )
1178
 
                return attr;    /* attribute found, return its value */
1179
 
 
1180
 
        /* not an attribute, search the methods table */
1181
 
        return Py_FindMethod( BPy_Image_methods, ( PyObject * ) self, name );
1182
 
}
1183
 
 
1184
 
/*****************************************************************************/
1185
 
/* Function:            Image_setAttr            */
1186
 
/* Description: This is a callback function for the BPy_Image type. It is the*/
1187
 
/*              function that changes Image object members values. If this  */
1188
 
/*              data is linked to a Blender Image, it also gets updated.  */
1189
 
/*****************************************************************************/
1190
 
static int Image_setAttr( BPy_Image * self, char *name, PyObject * value )
1191
 
{
1192
 
        PyObject *valtuple;
1193
 
        PyObject *error = NULL;
1194
 
 
1195
 
/* We're playing a trick on the Python API users here.  Even if they use
1196
 
 * Image.member = val instead of Image.setMember(value), we end up using the
1197
 
 * function anyway, since it already has error checking, clamps to the right
1198
 
 * interval and updates the Blender Image structure when necessary. */
1199
 
 
1200
 
        valtuple = Py_BuildValue( "(O)", value );       /*the set* functions expect a tuple */
1201
 
 
1202
 
        if( !valtuple )
1203
 
                return EXPP_ReturnIntError( PyExc_MemoryError,
1204
 
                                            "ImageSetAttr: couldn't create PyTuple" );
1205
 
 
1206
 
        if( strcmp( name, "name" ) == 0 )
1207
 
                error = Image_setName( self, valtuple );
1208
 
        else if( strcmp( name, "filename" ) == 0 )
1209
 
                error = Image_setFilename( self, valtuple );
1210
 
        else if( strcmp( name, "xrep" ) == 0 )
1211
 
                error = Image_setXRep( self, valtuple );
1212
 
        else if( strcmp( name, "yrep" ) == 0 )
1213
 
                error = Image_setYRep( self, valtuple );
1214
 
        else if( strcmp( name, "start" ) == 0 )
1215
 
                error = Image_setStart( self, valtuple );
1216
 
        else if( strcmp( name, "end" ) == 0 )
1217
 
                error = Image_setEnd( self, valtuple );
1218
 
        else if( strcmp( name, "speed" ) == 0 )
1219
 
                error = Image_setSpeed( self, valtuple );
1220
 
        
1221
 
        else if( strcmp( name, "fields" ) == 0 ) {
1222
 
                int param = PyObject_IsTrue( value );
1223
 
                if( param == -1 )
1224
 
                        return EXPP_ReturnIntError( PyExc_TypeError,
1225
 
                                "expected true/false argument" );
1226
 
                
1227
 
                if (param) self->image->flag |= IMA_FIELDS;
1228
 
                else self->image->flag &= ~IMA_FIELDS;
1229
 
                Py_INCREF( Py_None );
1230
 
                error = Py_None;
1231
 
        } else if( strcmp( name, "fields_odd" ) == 0 ) {
1232
 
                int param = PyObject_IsTrue( value );
1233
 
                if( param == -1 )
1234
 
                        return EXPP_ReturnIntError( PyExc_TypeError,
1235
 
                                "expected true/false argument" );
1236
 
                
1237
 
                if (param) self->image->flag |= IMA_STD_FIELD;
1238
 
                else self->image->flag &= ~IMA_STD_FIELD;
1239
 
                Py_INCREF( Py_None );
1240
 
                error = Py_None;
1241
 
        } else if( strcmp( name, "antialias" ) == 0 ) {
1242
 
                int param = PyObject_IsTrue( value );
1243
 
                if( param == -1 )
1244
 
                        return EXPP_ReturnIntError( PyExc_TypeError,
1245
 
                                "expected true/false argument" );
1246
 
                
1247
 
                if (param) self->image->flag |= IMA_ANTIALI;
1248
 
                else self->image->flag &= ~IMA_ANTIALI;
1249
 
                Py_INCREF( Py_None );
1250
 
                error = Py_None;
1251
 
        } else {                        /* Error: no such member in the Image object structure */
1252
 
                /*Py_DECREF( value ); borrowed ref, no need to decref */
1253
 
                Py_DECREF( valtuple );
1254
 
                return ( EXPP_ReturnIntError( PyExc_KeyError,
1255
 
                                              "attribute not found or immutable" ) );
1256
 
        }
1257
 
 
1258
 
        Py_DECREF( valtuple );
1259
 
 
1260
 
        if( error != Py_None )
1261
 
                return -1;
1262
 
 
1263
 
        Py_DECREF( Py_None );   /* incref'ed by the called set* function */
1264
 
        return 0;               /* normal exit */
1265
 
}
1266
1075
 
1267
1076
/*****************************************************************************/
1268
1077
/* Function:    Image_compare                    */
1287
1096
        return PyString_FromFormat( "[Image \"%s\"]",
1288
1097
                                    self->image->id.name + 2 );
1289
1098
}
 
1099
 
 
1100
static PyObject *Image_getPacked(BPy_Image *self, void *closure)
 
1101
{
 
1102
        if (self->image->packedfile)
 
1103
                Py_RETURN_TRUE;
 
1104
        else
 
1105
                Py_RETURN_FALSE;
 
1106
}
 
1107
 
 
1108
static PyObject *Image_hasData(BPy_Image *self, void *closure)
 
1109
{
 
1110
        if (self->image->ibufs.first)
 
1111
                Py_RETURN_TRUE;
 
1112
        else
 
1113
                Py_RETURN_FALSE;
 
1114
}
 
1115
 
 
1116
static PyObject *Image_getFlag(BPy_Image *self, void *flag)
 
1117
{
 
1118
        if (self->image->flag & (int)flag)
 
1119
                Py_RETURN_TRUE;
 
1120
        else
 
1121
                Py_RETURN_FALSE;
 
1122
                
 
1123
}
 
1124
 
 
1125
static PyObject *Image_getFlagTpage(BPy_Image *self, void *flag)
 
1126
{
 
1127
        if (self->image->tpageflag & (int)flag)
 
1128
                Py_RETURN_TRUE;
 
1129
        else
 
1130
                Py_RETURN_FALSE;
 
1131
                
 
1132
}
 
1133
 
 
1134
static int Image_setSource( BPy_Image *self, PyObject *args)
 
1135
{
 
1136
    PyObject* integer = PyNumber_Int( args );
 
1137
        short value;
 
1138
 
 
1139
        if( !integer )
 
1140
                return EXPP_ReturnIntError( PyExc_TypeError,
 
1141
                                "expected integer argument" );
 
1142
 
 
1143
        value = ( short )PyInt_AS_LONG( integer );
 
1144
        Py_DECREF( integer );
 
1145
 
 
1146
        if( value < IMA_SRC_FILE || value > IMA_SRC_GENERATED )
 
1147
                return EXPP_ReturnIntError( PyExc_ValueError,
 
1148
                                "expected integer argument in range 1-4" );
 
1149
 
 
1150
        self->image->source = value;
 
1151
        return 0;
 
1152
}
 
1153
 
 
1154
static int Image_setFlag(BPy_Image *self, PyObject *value, void *flag)
 
1155
{
 
1156
        if ( PyObject_IsTrue(value) )
 
1157
                self->image->flag |= (int)flag;
 
1158
        else
 
1159
                self->image->flag &= ~(int)flag;
 
1160
        return 0;
 
1161
}
 
1162
 
 
1163
static int Image_setFlagTpage(BPy_Image *self, PyObject *value, void *flag)
 
1164
{
 
1165
        if ( PyObject_IsTrue(value) )
 
1166
                self->image->tpageflag |= (int)flag;
 
1167
        else
 
1168
                self->image->tpageflag &= ~(int)flag;
 
1169
        return 0;
 
1170
}
 
1171
 
 
1172
/*
 
1173
 * get integer attributes
 
1174
 */
 
1175
static PyObject *getIntAttr( BPy_Image *self, void *type )
 
1176
{
 
1177
        PyObject *attr = NULL;
 
1178
        int param;
 
1179
        struct Image *image = self->image;
 
1180
 
 
1181
        switch( (int)type ) {
 
1182
        case EXPP_IMAGE_ATTR_XREP:
 
1183
                param = image->xrep;
 
1184
                break;
 
1185
        case EXPP_IMAGE_ATTR_YREP:
 
1186
                param = image->xrep;
 
1187
                break;
 
1188
        case EXPP_IMAGE_ATTR_START:
 
1189
                param = image->twsta;
 
1190
                break;  
 
1191
        case EXPP_IMAGE_ATTR_END:
 
1192
                param = image->twend;
 
1193
                break;
 
1194
        case EXPP_IMAGE_ATTR_SPEED:
 
1195
                param = image->animspeed;
 
1196
                break;
 
1197
        case EXPP_IMAGE_ATTR_BINDCODE:
 
1198
                param = image->bindcode;
 
1199
                break;
 
1200
        case EXPP_IMAGE_ATTR_SOURCE:
 
1201
                param = image->source;
 
1202
                break;
 
1203
        default:
 
1204
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
1205
                                "undefined type in getIntAttr" );
 
1206
        }
 
1207
 
 
1208
        attr = PyInt_FromLong( param );
 
1209
        
 
1210
        if( attr )
 
1211
                return attr;
 
1212
 
 
1213
        return EXPP_ReturnPyObjError( PyExc_MemoryError,
 
1214
                                "PyInt_FromLong() failed!" );
 
1215
}
 
1216
 
 
1217
 
 
1218
/*
 
1219
 * set integer attributes which require clamping
 
1220
 */
 
1221
 
 
1222
static int setIntAttrClamp( BPy_Image *self, PyObject *value, void *type )
 
1223
{
 
1224
        void *param;
 
1225
        struct Image *image = self->image;
 
1226
        int min, max, size;
 
1227
 
 
1228
        switch( (int)type ) {
 
1229
        case EXPP_IMAGE_ATTR_XREP:
 
1230
                min = EXPP_IMAGE_REP_MIN;
 
1231
                max = EXPP_IMAGE_REP_MAX;
 
1232
                size = 'h';
 
1233
                param = (void *)&image->xrep;
 
1234
                break;
 
1235
        case EXPP_IMAGE_ATTR_YREP:
 
1236
                min = EXPP_IMAGE_REP_MIN;
 
1237
                max = EXPP_IMAGE_REP_MAX;
 
1238
                size = 'h';
 
1239
                param = (void *)&image->yrep;
 
1240
                break;
 
1241
        case EXPP_IMAGE_ATTR_START:
 
1242
                min = 0;
 
1243
                max = 128;
 
1244
                size = 'h';
 
1245
                param = (void *)&image->twsta;
 
1246
                break;
 
1247
        case EXPP_IMAGE_ATTR_END:
 
1248
                min = 0;
 
1249
                max = 128;
 
1250
                size = 'h';
 
1251
                param = (void *)&image->twend;
 
1252
                break;
 
1253
        case EXPP_IMAGE_ATTR_SPEED:
 
1254
                min = 0;
 
1255
                max = 100;
 
1256
                size = 'h';
 
1257
                param = (void *)&image->animspeed;
 
1258
                break;
 
1259
        default:
 
1260
                return EXPP_ReturnIntError( PyExc_RuntimeError,
 
1261
                                "undefined type in setIntAttrClamp");
 
1262
        }
 
1263
        return EXPP_setIValueClamped( value, param, min, max, size );
 
1264
}
 
1265
 
 
1266
/*****************************************************************************/
 
1267
/* Python attributes get/set structure:                                      */
 
1268
/*****************************************************************************/
 
1269
static PyGetSetDef BPy_Image_getseters[] = {
 
1270
        GENERIC_LIB_GETSETATTR,
 
1271
        {"filename", (getter)Image_getFilename, (setter)Image_setFilename,
 
1272
         "image path", NULL},
 
1273
        /* readonly */
 
1274
        {"depth", (getter)Image_getDepth, (setter)NULL,
 
1275
         "image depth", NULL},
 
1276
        {"size", (getter)Image_getSize, (setter)NULL,
 
1277
         "image size", NULL},
 
1278
        {"packed", (getter)Image_getPacked, (setter)NULL,
 
1279
         "image packed state", NULL },
 
1280
        {"has_data", (getter)Image_hasData, (setter)NULL,
 
1281
         "is image data loaded", NULL },
 
1282
        /* ints */
 
1283
        {"xrep", (getter)getIntAttr, (setter)setIntAttrClamp,
 
1284
         "image xrep", (void *)EXPP_IMAGE_ATTR_XREP },
 
1285
        {"yrep", (getter)getIntAttr, (setter)setIntAttrClamp,
 
1286
         "image yrep", (void *)EXPP_IMAGE_ATTR_YREP },
 
1287
        {"start", (getter)getIntAttr, (setter)setIntAttrClamp,
 
1288
         "image start frame", (void *)EXPP_IMAGE_ATTR_START },
 
1289
        {"end", (getter)getIntAttr, (setter)setIntAttrClamp,
 
1290
         "image end frame", (void *)EXPP_IMAGE_ATTR_END },
 
1291
        {"speed", (getter)getIntAttr, (setter)setIntAttrClamp,
 
1292
         "image end frame", (void *)EXPP_IMAGE_ATTR_SPEED },
 
1293
        {"bindcode", (getter)getIntAttr, (setter)NULL,
 
1294
         "openGL bindcode", (void *)EXPP_IMAGE_ATTR_BINDCODE },
 
1295
        {"source", (getter)getIntAttr, (setter)Image_setSource,
 
1296
         "image source type", (void *)EXPP_IMAGE_ATTR_SOURCE },
 
1297
        /* flags */
 
1298
        {"fields", (getter)Image_getFlag, (setter)Image_setFlag,
 
1299
         "image fields toggle", (void *)IMA_FIELDS },
 
1300
        {"fields_odd", (getter)Image_getFlag, (setter)Image_setFlag,
 
1301
         "image odd fields toggle", (void *)IMA_STD_FIELD },
 
1302
        {"antialias", (getter)Image_getFlag, (setter)Image_setFlag,
 
1303
         "image antialiasing toggle", (void *)IMA_ANTIALI },
 
1304
        {"reflect", (getter)Image_getFlag, (setter)Image_setFlag,
 
1305
         "image reflect toggle", (void *)IMA_REFLECT },
 
1306
        {"clampX", (getter)Image_getFlagTpage, (setter)Image_setFlagTpage,
 
1307
         "disable tiling on the X axis", (void *)IMA_CLAMP_U },
 
1308
        {"clampY", (getter)Image_getFlagTpage, (setter)Image_setFlagTpage,
 
1309
         "disable tiling on the Y axis", (void *)IMA_CLAMP_V },
 
1310
        {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
 
1311
};
 
1312
 
 
1313
 
 
1314
/*****************************************************************************/
 
1315
/* Python Image_Type structure definition:   */
 
1316
/*****************************************************************************/
 
1317
PyTypeObject Image_Type = {
 
1318
        PyObject_HEAD_INIT( NULL ) /*     required macro. ( no comma needed )  */ 
 
1319
        0,      /* ob_size */
 
1320
        "Blender Image",        /* tp_name */
 
1321
        sizeof( BPy_Image ),    /* tp_basicsize */
 
1322
        0,                      /* tp_itemsize */
 
1323
        /* methods */
 
1324
        NULL,   /* tp_dealloc */
 
1325
        0,              /* tp_print */
 
1326
        NULL,   /* tp_getattr */
 
1327
        NULL,   /* tp_setattr */
 
1328
        ( cmpfunc ) Image_compare,      /* tp_compare */
 
1329
        ( reprfunc ) Image_repr,        /* tp_repr */
 
1330
 
 
1331
        /* Method suites for standard classes */
 
1332
 
 
1333
        NULL,                       /* PyNumberMethods *tp_as_number; */
 
1334
        NULL,                       /* PySequenceMethods *tp_as_sequence; */
 
1335
        NULL,                       /* PyMappingMethods *tp_as_mapping; */
 
1336
 
 
1337
        /* More standard operations (here for binary compatibility) */
 
1338
 
 
1339
        ( hashfunc ) GenericLib_hash,   /* hashfunc tp_hash; */
 
1340
        NULL,                       /* ternaryfunc tp_call; */
 
1341
        NULL,                       /* reprfunc tp_str; */
 
1342
        NULL,                       /* getattrofunc tp_getattro; */
 
1343
        NULL,                       /* setattrofunc tp_setattro; */
 
1344
 
 
1345
        /* Functions to access object as input/output buffer */
 
1346
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
 
1347
 
 
1348
  /*** Flags to define presence of optional/expanded features ***/
 
1349
        Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
 
1350
 
 
1351
        NULL,                       /*  char *tp_doc;  Documentation string */
 
1352
  /*** Assigned meaning in release 2.0 ***/
 
1353
        /* call function for all accessible objects */
 
1354
        NULL,                       /* traverseproc tp_traverse; */
 
1355
 
 
1356
        /* delete references to contained objects */
 
1357
        NULL,                       /* inquiry tp_clear; */
 
1358
 
 
1359
  /***  Assigned meaning in release 2.1 ***/
 
1360
  /*** rich comparisons ***/
 
1361
        NULL,                       /* richcmpfunc tp_richcompare; */
 
1362
 
 
1363
  /***  weak reference enabler ***/
 
1364
        0,                          /* long tp_weaklistoffset; */
 
1365
 
 
1366
  /*** Added in release 2.2 ***/
 
1367
        /*   Iterators */
 
1368
        NULL,                       /* getiterfunc tp_iter; */
 
1369
        NULL,                       /* iternextfunc tp_iternext; */
 
1370
 
 
1371
  /*** Attribute descriptor and subclassing stuff ***/
 
1372
        BPy_Image_methods,           /* struct PyMethodDef *tp_methods; */
 
1373
        NULL,                       /* struct PyMemberDef *tp_members; */
 
1374
        BPy_Image_getseters,         /* struct PyGetSetDef *tp_getset; */
 
1375
        NULL,                       /* struct _typeobject *tp_base; */
 
1376
        NULL,                       /* PyObject *tp_dict; */
 
1377
        NULL,                       /* descrgetfunc tp_descr_get; */
 
1378
        NULL,                       /* descrsetfunc tp_descr_set; */
 
1379
        0,                          /* long tp_dictoffset; */
 
1380
        NULL,                       /* initproc tp_init; */
 
1381
        NULL,                       /* allocfunc tp_alloc; */
 
1382
        NULL,                       /* newfunc tp_new; */
 
1383
        /*  Low-level free-memory routine */
 
1384
        NULL,                       /* freefunc tp_free;  */
 
1385
        /* For PyObject_IS_GC */
 
1386
        NULL,                       /* inquiry tp_is_gc;  */
 
1387
        NULL,                       /* PyObject *tp_bases; */
 
1388
        /* method resolution order */
 
1389
        NULL,                       /* PyObject *tp_mro;  */
 
1390
        NULL,                       /* PyObject *tp_cache; */
 
1391
        NULL,                       /* PyObject *tp_subclasses; */
 
1392
        NULL,                       /* PyObject *tp_weaklist; */
 
1393
        NULL
 
1394
};