fixed graphics bug
[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 PyObject* f_Color(PyObject* self, PyObject* args, PyObject* kwargs)
31 {
32     static char *kwlist[] = {"r", "g", "b", "a", NULL};
33     ColorObject* color;
34     int r=0,g=0,b=0,a=255;
35     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iii|i", kwlist, &r,&g,&b,&a))
36         return NULL;
37     color = PyObject_New(ColorObject, &ColorClass);
38     color->rgba.r = r;
39     color->rgba.g = g;
40     color->rgba.b = b;
41     color->rgba.a = a;
42     return (PyObject*)color;
43 }
44 static PyObject* color_getattr(PyObject * self, char* a)
45 {
46     ColorObject*color = (ColorObject*)self;
47     if(!strcmp(a, "r")) {
48         return Py_BuildValue("r", color->rgba.r);
49     } else if(!strcmp(a, "g")) {
50         return Py_BuildValue("g", color->rgba.g);
51     } else if(!strcmp(a, "b")) {
52         return Py_BuildValue("b", color->rgba.b);
53     } else if(!strcmp(a, "a")) {
54         return Py_BuildValue("a", color->rgba.a);
55     }
56     return NULL;
57 }
58 static int color_setattr(PyObject * self, char* attr, PyObject* o)
59 {
60     ColorObject*color = (ColorObject*)self;
61     if(!strcmp(attr, "r")) {
62         if (!PyArg_Parse(o, "d", &color->rgba.r)) goto err;
63         return 0;
64     } else if(!strcmp(attr, "g")) {
65         if (!PyArg_Parse(o, "d", &color->rgba.g)) goto err;
66         return 0;
67     } else if(!strcmp(attr, "b")) {
68         if (!PyArg_Parse(o, "d", &color->rgba.b)) goto err;
69         return 0;
70     } else if(!strcmp(attr, "a")) {
71         if (!PyArg_Parse(o, "d", &color->rgba.a)) goto err;
72         return 0;
73     } 
74 err:
75     mylog("swf_setattr %08x(%d) %s = ? (%08x)\n", (int)self, self->ob_refcnt, attr, o);
76     return 1;
77 }
78 //----------------------------------------------------------------------------
79 PyObject* f_BBox(PyObject* self, PyObject* args, PyObject* kwargs)
80 {
81     static char *kwlist[] = {"xmin", "ymin", "xmax", "ymax", NULL};
82     BBoxObject* bbox;
83     SRECT box;
84     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iiii", kwlist, 
85                 &box.xmin,
86                 &box.ymin,
87                 &box.xmax,
88                 &box.ymax));
89         return NULL;
90     bbox = PyObject_New(BBoxObject, &BBoxClass);
91     bbox->bbox = box;
92     return (PyObject*)bbox;
93 }
94 static PyObject* bbox_getattr(PyObject * self, char* a)
95 {
96     BBoxObject*bbox = (BBoxObject*)self;
97     if(!strcmp(a, "xmin")) {
98         return Py_BuildValue("i", bbox->bbox.xmin);
99     } else if(!strcmp(a, "ymin")) {
100         return Py_BuildValue("i", bbox->bbox.ymin);
101     } else if(!strcmp(a, "xmax")) {
102         return Py_BuildValue("i", bbox->bbox.xmax);
103     } else if(!strcmp(a, "ymax")) {
104         return Py_BuildValue("i", bbox->bbox.ymax);
105     }
106     return NULL;
107 }
108 static int bbox_setattr(PyObject * self, char* a, PyObject* o)
109 {
110     BBoxObject*bbox= (BBoxObject*)self;
111     if(!strcmp(a, "xmin")) {
112         if (!PyArg_Parse(o, "i", &bbox->bbox.xmin)) goto err;
113         return 0;
114     } else if(!strcmp(a, "ymin")) {
115         if (!PyArg_Parse(o, "i", &bbox->bbox.ymin)) goto err;
116         return 0;
117     } else if(!strcmp(a, "xmax")) {
118         if (!PyArg_Parse(o, "i", &bbox->bbox.xmax)) goto err;
119         return 0;
120     } else if(!strcmp(a, "ymax")) {
121         if (!PyArg_Parse(o, "i", &bbox->bbox.ymax)) goto err;
122         return 0;
123     } 
124 err:
125     mylog("swf_setattr %08x(%d) %s = ? (%08x)\n", (int)self, self->ob_refcnt, a, o);
126     return 1;
127 }
128 //----------------------------------------------------------------------------
129 PyObject* f_Matrix(PyObject* self, PyObject* args, PyObject* kwargs)
130 {
131     return NULL;
132 }
133 static PyObject* matrix_getattr(PyObject * self, char* a)
134 {
135     return NULL;
136 }
137 static int matrix_setattr(PyObject * self, char* a, PyObject* o)
138 {
139     return 0;
140 }
141 //----------------------------------------------------------------------------
142 PyObject* f_ColorTransform(PyObject* self, PyObject* args, PyObject* kwargs)
143 {
144     return NULL;
145 }
146 static PyObject* colortransform_getattr(PyObject * self, char* a)
147 {
148     return NULL;
149 }
150 static int colortransform_setattr(PyObject * self, char* a, PyObject* o)
151 {
152     return 0;
153 }
154 //----------------------------------------------------------------------------
155 PyObject* f_Gradient(PyObject* self, PyObject* args, PyObject* kwargs)
156 {
157     return NULL;
158 }
159 static PyObject* gradient_getattr(PyObject * self, char* a)
160 {
161     return NULL;
162 }
163 static int gradient_setattr(PyObject * self, char* a, PyObject* o)
164 {
165     return 0;
166 }
167 //----------------------------------------------------------------------------
168
169 PyTypeObject ColorClass = 
170 {
171     PyObject_HEAD_INIT(NULL)
172     0,
173     tp_name: "Color",
174     tp_basicsize: sizeof(ColorObject),
175     tp_itemsize: 0,
176     tp_dealloc: dummy_dealloc,
177     tp_print: 0,
178     tp_getattr: color_getattr,
179     tp_setattr: color_setattr,
180 };
181 PyTypeObject BBoxClass = 
182 {
183     PyObject_HEAD_INIT(NULL)
184     0,
185     tp_name: "BBox",
186     tp_basicsize: sizeof(BBoxObject),
187     tp_itemsize: 0,
188     tp_dealloc: dummy_dealloc,
189     tp_print: 0,
190     tp_getattr: bbox_getattr,
191     tp_setattr: bbox_setattr,
192 };
193 PyTypeObject GradientClass = 
194 {
195     PyObject_HEAD_INIT(NULL)
196     0,
197     tp_name: "Gradient",
198     tp_basicsize: sizeof(GradientObject),
199     tp_itemsize: 0,
200     tp_dealloc: dummy_dealloc,
201     tp_print: 0,
202     tp_getattr: gradient_getattr,
203     tp_setattr: gradient_setattr,
204 };
205 PyTypeObject CXFormClass = 
206 {
207     PyObject_HEAD_INIT(NULL)
208     0,
209     tp_name: "ColorTransform",
210     tp_basicsize: sizeof(CXFormObject),
211     tp_itemsize: 0,
212     tp_dealloc: dummy_dealloc,
213     tp_print: 0,
214     tp_getattr: colortransform_getattr,
215     tp_setattr: colortransform_setattr,
216 };
217 PyTypeObject MatrixClass = 
218 {
219     PyObject_HEAD_INIT(NULL)
220     0,
221     tp_name: "Matrix",
222     tp_basicsize: sizeof(MatrixObject),
223     tp_itemsize: 0,
224     tp_dealloc: dummy_dealloc,
225     tp_print: 0,
226     tp_getattr: matrix_getattr,
227     tp_setattr: matrix_setattr,
228     tp_compare: 0,
229     tp_repr: 0,
230     tp_as_number: 0,
231     tp_as_sequence: 0,
232     tp_as_mapping: 0,
233     tp_hash: 0,     // dict(x)
234     tp_call: 0,     // x()
235     tp_str: 0       // str(x)
236 };