b1cb0c67cb89aa7efd517bbae4a3992fc283df9a
[swftools.git] / lib / python / primitives.c
1 /* primitives.c
2
3    Python wrapper for librfxswf- primitive objects (implementation)
4
5    Part of the swftools package.
6
7    Copyright (c) 2003 Matthias Kramm <kramm@quiss.org>
8  
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
22
23 #include <Python.h>
24 #undef HAVE_STAT
25 #include "../rfxswf.h"
26 #include "../log.h"
27 #include "./pyutils.h"
28 #include "primitives.h"
29
30 //----------------------------------------------------------------------------
31 typedef struct {
32     PyObject_HEAD
33     RGBA rgba;
34 } ColorObject;
35
36 PyObject* f_Color(PyObject* self, PyObject* args, PyObject* kwargs)
37 {
38     static char *kwlist[] = {"r", "g", "b", "a", NULL};
39     ColorObject* color;
40     int r=0,g=0,b=0,a=255;
41     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iii|i", kwlist, &r,&g,&b,&a))
42         return NULL;
43     color = PyObject_New(ColorObject, &ColorClass);
44     color->rgba.r = r;
45     color->rgba.g = g;
46     color->rgba.b = b;
47     color->rgba.a = a;
48     return (PyObject*)color;
49 }
50 static PyObject* color_getattr(PyObject * self, char* a)
51 {
52     ColorObject*color = (ColorObject*)self;
53     if(!strcmp(a, "r")) {
54         return Py_BuildValue("r", color->rgba.r);
55     } else if(!strcmp(a, "g")) {
56         return Py_BuildValue("g", color->rgba.g);
57     } else if(!strcmp(a, "b")) {
58         return Py_BuildValue("b", color->rgba.b);
59     } else if(!strcmp(a, "a")) {
60         return Py_BuildValue("a", color->rgba.a);
61     }
62     return NULL;
63 }
64 static int color_setattr(PyObject * self, char* attr, PyObject* o)
65 {
66     ColorObject*color = (ColorObject*)self;
67     if(!strcmp(attr, "r")) {
68         if (!PyArg_Parse(o, "d", &color->rgba.r)) goto err;
69         return 0;
70     } else if(!strcmp(attr, "g")) {
71         if (!PyArg_Parse(o, "d", &color->rgba.g)) goto err;
72         return 0;
73     } else if(!strcmp(attr, "b")) {
74         if (!PyArg_Parse(o, "d", &color->rgba.b)) goto err;
75         return 0;
76     } else if(!strcmp(attr, "a")) {
77         if (!PyArg_Parse(o, "d", &color->rgba.a)) goto err;
78         return 0;
79     } 
80 err:
81     mylog("swf_setattr %08x(%d) %s = ? (%08x)\n", (int)self, self->ob_refcnt, attr, o);
82     return 1;
83 }
84 RGBA color_getRGBA(PyObject*self)
85 {
86     ColorObject*color = 0;
87     if (!PyArg_Parse(self, "O!", &ColorClass, &color)) {
88         RGBA dummy;
89         memset(&dummy, 0, sizeof(dummy));
90         mylog("Error: wrong type for function color_getRGBA");
91         return dummy;
92     }
93     return color->rgba;
94 }
95 PyTypeObject ColorClass = 
96 {
97     PyObject_HEAD_INIT(NULL)
98     0,
99     tp_name: "Color",
100     tp_basicsize: sizeof(ColorObject),
101     tp_itemsize: 0,
102     tp_dealloc: dummy_dealloc,
103     tp_print: 0,
104     tp_getattr: color_getattr,
105     tp_setattr: color_setattr,
106 };
107 //----------------------------------------------------------------------------
108 typedef struct {
109     PyObject_HEAD
110     SRECT bbox;
111 } BBoxObject;
112
113 PyObject* f_BBox(PyObject* self, PyObject* args, PyObject* kwargs)
114 {
115     static char *kwlist[] = {"xmin", "ymin", "xmax", "ymax", NULL};
116     BBoxObject* bbox;
117     SRECT box;
118     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iiii", kwlist, 
119                 &box.xmin,
120                 &box.ymin,
121                 &box.xmax,
122                 &box.ymax));
123         return NULL;
124     bbox = PyObject_New(BBoxObject, &BBoxClass);
125     bbox->bbox = box;
126     return (PyObject*)bbox;
127 }
128 static PyObject* bbox_getattr(PyObject * self, char* a)
129 {
130     BBoxObject*bbox = (BBoxObject*)self;
131     if(!strcmp(a, "xmin")) {
132         return Py_BuildValue("i", bbox->bbox.xmin);
133     } else if(!strcmp(a, "ymin")) {
134         return Py_BuildValue("i", bbox->bbox.ymin);
135     } else if(!strcmp(a, "xmax")) {
136         return Py_BuildValue("i", bbox->bbox.xmax);
137     } else if(!strcmp(a, "ymax")) {
138         return Py_BuildValue("i", bbox->bbox.ymax);
139     }
140     return NULL;
141 }
142 static int bbox_setattr(PyObject * self, char* a, PyObject* o)
143 {
144     BBoxObject*bbox= (BBoxObject*)self;
145     if(!strcmp(a, "xmin")) {
146         if (!PyArg_Parse(o, "i", &bbox->bbox.xmin)) goto err;
147         return 0;
148     } else if(!strcmp(a, "ymin")) {
149         if (!PyArg_Parse(o, "i", &bbox->bbox.ymin)) goto err;
150         return 0;
151     } else if(!strcmp(a, "xmax")) {
152         if (!PyArg_Parse(o, "i", &bbox->bbox.xmax)) goto err;
153         return 0;
154     } else if(!strcmp(a, "ymax")) {
155         if (!PyArg_Parse(o, "i", &bbox->bbox.ymax)) goto err;
156         return 0;
157     } 
158 err:
159     mylog("swf_setattr %08x(%d) %s = ? (%08x)\n", (int)self, self->ob_refcnt, a, o);
160     return 1;
161 }
162 SRECT bbox_getBBox(PyObject*self)
163 {
164     BBoxObject*bbox= 0;
165     if (!PyArg_Parse(self, "O!", &BBoxClass, &bbox)) {
166         SRECT dummy;
167         memset(&dummy, 0, sizeof(dummy));
168         mylog("Error: wrong type for function color_getRGBA");
169         return dummy;
170     }
171     return bbox->bbox;
172 }
173 PyTypeObject BBoxClass = 
174 {
175     PyObject_HEAD_INIT(NULL)
176     0,
177     tp_name: "BBox",
178     tp_basicsize: sizeof(BBoxObject),
179     tp_itemsize: 0,
180     tp_dealloc: dummy_dealloc,
181     tp_print: 0,
182     tp_getattr: bbox_getattr,
183     tp_setattr: bbox_setattr,
184 };
185 SRECT bbox_getBBox(PyObject*self);
186 //----------------------------------------------------------------------------
187 typedef struct {
188     PyObject_HEAD
189     MATRIX matrix;
190 } MatrixObject;
191
192 PyObject* f_Matrix(PyObject* _self, PyObject* args, PyObject* kwargs)
193 {
194     PyObject*self = (PyObject*)PyObject_New(MatrixObject, &MatrixClass);
195     MatrixObject*matrix = (MatrixObject*)self;
196     mylog("+%08x(%d) f_Matrix", self, self->ob_refcnt);
197     static char *kwlist[] = {"x", "y", "scale", "rotate", NULL};
198     float x=0,y=0,scale=1.0,rotate=0;
199     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ffff", kwlist, &x,&y,&scale,&rotate))
200         return NULL;
201     mylog(" %08x(%d) f_Matrix: x=%f y=%f scale=%f rotate=%f", self, self->ob_refcnt, x,y,scale,rotate);
202     swf_GetMatrix(0, &matrix->matrix);
203     matrix->matrix.tx = (int)(x*20);
204     matrix->matrix.ty = (int)(y*20);
205     matrix->matrix.sx = (int)(scale*65536);
206     matrix->matrix.sy = (int)(scale*65536);
207     /* TODO: rotate */
208     return self;
209 }
210 static PyObject* matrix_getattr(PyObject * self, char* a)
211 {
212     PY_ASSERT_TYPE(self,&MatrixClass);
213     return NULL;
214 }
215 static int matrix_setattr(PyObject * self, char* a, PyObject* o)
216 {
217     PY_ASSERT_TYPE(self,&MatrixClass);
218     return 0;
219 }
220 MATRIX matrix_getMatrix(PyObject*self)
221 {
222     mylog(" %08x(%d) matrix_getMatrix", self, self->ob_refcnt);
223     PY_ASSERT_TYPE(self,&MatrixClass);
224     MatrixObject*matrix = (MatrixObject*)self;
225     return matrix->matrix;
226 }
227 PyTypeObject MatrixClass = 
228 {
229     PyObject_HEAD_INIT(NULL)
230     0,
231     tp_name: "Matrix",
232     tp_basicsize: sizeof(MatrixObject),
233     tp_itemsize: 0,
234     tp_dealloc: dummy_dealloc,
235     tp_print: 0,
236     tp_getattr: matrix_getattr,
237     tp_setattr: matrix_setattr,
238     tp_compare: 0,
239     tp_repr: 0,
240     tp_as_number: 0,
241     tp_as_sequence: 0,
242     tp_as_mapping: 0,
243     tp_hash: 0,     // dict(x)
244     tp_call: 0,     // x()
245     tp_str: 0       // str(x)
246 };
247 //----------------------------------------------------------------------------
248 typedef struct {
249     PyObject_HEAD
250     CXFORM cxform;
251 } CXFormObject;
252
253 PyObject* f_ColorTransform(PyObject* self, PyObject* args, PyObject* kwargs)
254 {
255     return NULL;
256 }
257 static PyObject* colortransform_getattr(PyObject * self, char* a)
258 {
259     return NULL;
260 }
261 static int colortransform_setattr(PyObject * self, char* a, PyObject* o)
262 {
263     return 0;
264 }
265 CXFORM colortransform_getCXForm(PyObject*self)
266 {
267     CXFormObject*cxform= 0;
268     if (!PyArg_Parse(self, "O!", &CXFormClass, &cxform)) {
269         CXFORM dummy;
270         memset(&dummy, 0, sizeof(dummy));
271         mylog("Error: wrong type for function color_getRGBA");
272         return dummy;
273     }
274     return cxform->cxform;
275 }
276 PyTypeObject CXFormClass = 
277 {
278     PyObject_HEAD_INIT(NULL)
279     0,
280     tp_name: "ColorTransform",
281     tp_basicsize: sizeof(CXFormObject),
282     tp_itemsize: 0,
283     tp_dealloc: dummy_dealloc,
284     tp_print: 0,
285     tp_getattr: colortransform_getattr,
286     tp_setattr: colortransform_setattr,
287 };
288 //----------------------------------------------------------------------------
289 typedef struct {
290     PyObject_HEAD
291     GRADIENT gradient;
292 } GradientObject;
293
294 PyObject* f_Gradient(PyObject* self, PyObject* args, PyObject* kwargs)
295 {
296     return NULL;
297 }
298 static PyObject* gradient_getattr(PyObject * self, char* a)
299 {
300     return NULL;
301 }
302 static int gradient_setattr(PyObject * self, char* a, PyObject* o)
303 {
304     return 0;
305 }
306 GRADIENT colortransform_getGradient(PyObject*self)
307 {
308     GradientObject*gradient = 0;
309     if (!PyArg_Parse(self, "O!", &gradient, &gradient)) {
310         GRADIENT dummy;
311         memset(&dummy, 0, sizeof(dummy));
312         mylog("Error: wrong type for function color_getRGBA");
313         return dummy;
314     }
315     return gradient->gradient;
316 }
317 PyTypeObject GradientClass = 
318 {
319     PyObject_HEAD_INIT(NULL)
320     0,
321     tp_name: "Gradient",
322     tp_basicsize: sizeof(GradientObject),
323     tp_itemsize: 0,
324     tp_dealloc: dummy_dealloc,
325     tp_print: 0,
326     tp_getattr: gradient_getattr,
327     tp_setattr: gradient_setattr,
328 };
329 //----------------------------------------------------------------------------
330
331 static PyMethodDef primitive_methods[] = 
332 {
333     {"Color", (PyCFunction)f_Color, METH_KEYWORDS, "Create a new color object."},
334     {"Gradient", (PyCFunction)f_Gradient, METH_KEYWORDS, "Create a new gradient object."},
335     {"ColorTransform", (PyCFunction)f_ColorTransform, METH_KEYWORDS, "Create a new colortransform object."},
336     {"Matrix", (PyCFunction)f_Matrix, METH_KEYWORDS, "Create a new matrix object."},
337     {"BBox", (PyCFunction)f_BBox, METH_KEYWORDS, "Create a new bounding box object."},
338     {NULL, NULL, 0, NULL}
339 };
340
341 PyMethodDef* primitive_getMethods()
342 {
343     GradientClass.ob_type = &PyType_Type;
344     CXFormClass.ob_type = &PyType_Type;
345     BBoxClass.ob_type = &PyType_Type;
346     MatrixClass.ob_type = &PyType_Type;
347     return primitive_methods;
348 }
349
350