3 Python wrapper for librfxswf- module core.
5 Part of the swftools package.
7 Copyright (c) 2003 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
25 #include "../rfxswf.h"
27 #include "./pyutils.h"
29 #include "./taglist.h"
33 1) taglist is rfxswflib's linked list. It should maybe implemented as Python
34 list, which would, however, mean that we would have to convert the list
35 back and forth for the following functions:
36 load, save, writeCGI, unfoldAll, foldAll, optimizeOrder
37 2) taglist should have an ID handler. Every time a tag is inserted, it's ID
38 is stored in a lookup list.
42 //-------------------------- Types -------------------------------------------
44 staticforward PyTypeObject SWFClass;
50 SWF swf; //swf.firstTag ist not used
56 //----------------------------------------------------------------------------
57 static PyObject* f_create(PyObject* self, PyObject* args, PyObject* kwargs)
59 static char *kwlist[] = {"version", "fps", "bbox", "name", NULL};
62 double framerate = 25;
64 SRECT bbox = {0,0,0,0};
67 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|idOs",
68 kwlist, &version, &framerate,
72 if (!PyArg_Parse(obbox, "(iiii)", &bbox.xmin, &bbox.ymin, &bbox.xmax, &bbox.ymax))
75 swf = PyObject_New(SWFObject, &SWFClass);
76 memset(&swf->swf, 0, sizeof(SWF));
78 swf->filename = strdup(filename);
82 swf->swf.fileVersion = version;
83 swf->swf.frameRate = (int)(framerate*0x100);
84 swf->swf.movieSize = bbox;
85 swf->taglist = taglist_new();
87 if(swf->swf.fileVersion>=6)
88 swf->swf.compressed = 1;
90 mylog("create %08x -> %08x\n", (int)self, (int)swf);
91 return (PyObject*)swf;
93 //----------------------------------------------------------------------------
94 static PyObject* f_load(PyObject* self, PyObject* args)
100 if (!PyArg_ParseTuple(args,"s:load", &filename))
103 swf = PyObject_New(SWFObject, &SWFClass);
104 memset(&swf->swf, 0, sizeof(SWF));
105 swf->filename = strdup(filename);
108 PyErr_SetString(PyExc_Exception, setError("Couldn't open file %s", filename));
111 fi = open(filename,O_RDONLY|O_BINARY);
113 PyErr_SetString(PyExc_Exception, setError("Couldn't open file %s", filename));
116 if(swf_ReadSWF(fi,&swf->swf)<0) {
118 PyErr_SetString(PyExc_Exception, setError("%s is not a valid SWF file or contains errors",filename));
122 swf->swf.firstTag = 0;
124 swf->taglist = taglist_new2(swf->swf.firstTag);
126 mylog("load %08x -> %08x\n", (int)self, (int)swf);
127 return (PyObject*)swf;
129 //----------------------------------------------------------------------------
130 static PyObject * swf_save(PyObject* self, PyObject* args, PyObject* kwargs)
132 static char *kwlist[] = {"name", "compress", NULL};
142 swfo = (SWFObject*)self;
145 filename = swfo->filename;
147 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|si", kwlist, &filename, &compress))
150 // keyword arg compress (=1) forces compression
154 swf->firstTag = taglist_getTAGs(swfo->taglist);
156 // fix the file, in case it is empty or not terminated properly
158 TAG*tag = swf->firstTag;
160 tag = swf->firstTag = swf_InsertTag(0,ST_END);
161 while(tag && tag->next)
163 if(tag->id != ST_END) {
164 tag = swf_InsertTag(tag,ST_END);
168 fi = open(filename, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
170 PyErr_SetString(PyExc_Exception, setError("couldn't create output file %s", filename));
173 if(swf->compressed) {
174 if(swf_WriteSWC(fi, swf)<0) {
176 PyErr_SetString(PyExc_Exception, setError("WriteSWC() failed."));
180 if(swf_WriteSWF(fi, swf)<0) {
182 PyErr_SetString(PyExc_Exception, setError("WriteSWC() failed."));
192 //----------------------------------------------------------------------------
193 static PyObject * swf_writeCGI(PyObject* self, PyObject* args)
195 SWFObject*swf = (SWFObject*)self;
196 if(!self || !PyArg_ParseTuple(args,""))
198 swf->swf.firstTag = taglist_getTAGs(swf->taglist);
199 swf_WriteCGI(&swf->swf);
200 swf->swf.firstTag = 0;
203 //----------------------------------------------------------------------------
205 //TODO: void swf_Relocate(SWF*swf, char*bitmap); // bitmap is 65536 bytes, bitmap[a]==0 means id a is free
207 static PyMethodDef swf_functions[] =
208 {{"save", (PyCFunction)swf_save, METH_KEYWORDS, "Save SWF to disk"},
209 {"writeCGI", (PyCFunction)swf_writeCGI, METH_VARARGS, "print SWF as CGI to stdout"},
210 {NULL, NULL, 0, NULL}
213 //----------------------------------------------------------------------------
214 static void swf_dealloc(PyObject* self)
218 swfo = (SWFObject*)self;
220 mylog("swf_dealloc %08x(%d)\n", (int)self, self->ob_refcnt);
222 free(swfo->filename);
225 Py_DECREF(swfo->taglist);
229 //----------------------------------------------------------------------------
230 static int swf_print(PyObject * self, FILE *fi, int flags) //flags&Py_PRINT_RAW
232 SWFObject*swf = (SWFObject*)self;
233 swf_DumpHeader(fi, &swf->swf);
234 //void swf_DumpSWF(FILE * f,SWF*swf);
235 mylog("print %08x(%d)\n", (int)self, self->ob_refcnt);
238 //----------------------------------------------------------------------------
239 static PyObject* swf_getattr(PyObject * self, char* a)
241 SWFObject*swf = (SWFObject*)self;
244 if(!strcmp(a, "fps")) {
245 double fps = swf->swf.frameRate/256.0;
246 mylog("swf_getattr %08x(%d) %s = %f\n", (int)self, self->ob_refcnt, a, fps);
247 return Py_BuildValue("d", fps);
248 } else if(!strcmp(a, "version")) {
249 int version = swf->swf.fileVersion;;
250 mylog("swf_getattr %08x(%d) %s = %d\n", (int)self, self->ob_refcnt, a, version);
251 return Py_BuildValue("i", version);
252 } else if(!strcmp(a, "name")) {
253 char*filename = swf->filename;
254 mylog("swf_getattr %08x(%d) %s = %s\n", (int)self, self->ob_refcnt, a, filename);
255 return Py_BuildValue("s", filename);
256 } else if(!strcmp(a, "bbox")) {
257 int xmin,ymin,xmax,ymax;
258 xmin = swf->swf.movieSize.xmin;
259 ymin = swf->swf.movieSize.ymin;
260 xmax = swf->swf.movieSize.xmax;
261 ymax = swf->swf.movieSize.ymax;
262 mylog("swf_getattr %08x(%d) %s = (%d,%d,%d,%d)\n", (int)self, self->ob_refcnt, a, xmin,ymin,xmax,ymax);
263 return Py_BuildValue("(iiii)", xmin, ymin, xmax, ymax);
264 } else if(!strcmp(a, "tags")) {
265 PyObject*ret = (PyObject*)(swf->taglist);
267 mylog("swf_getattr %08x(%d) %s = %08x(%d)\n", (int)self, self->ob_refcnt, a, ret, ret->ob_refcnt);
271 ret = Py_FindMethod(swf_functions, self, a);
272 mylog("swf_getattr %08x(%d) %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
275 //----------------------------------------------------------------------------
276 static int swf_setattr(PyObject * self, char* a, PyObject * o)
278 SWFObject*swf = (SWFObject*)self;
279 if(!strcmp(a, "fps")) {
281 if (!PyArg_Parse(o, "d", &fps))
283 swf->swf.frameRate = (int)(fps*0x100);
284 mylog("swf_setattr %08x(%d) %s = %f\n", (int)self, self->ob_refcnt, a, fps);
286 } else if(!strcmp(a, "version")) {
288 if (!PyArg_Parse(o, "i", &version))
290 swf->swf.fileVersion = version;
291 mylog("swf_setattr %08x(%d) %s = %d\n", (int)self, self->ob_refcnt, a, version);
293 } else if(!strcmp(a, "name")) {
295 if (!PyArg_Parse(o, "s", &filename))
298 free(swf->filename);swf->filename=0;
300 swf->filename = strdup(filename);
301 mylog("swf_setattr %08x(%d) %s = %s\n", (int)self, self->ob_refcnt, a, filename);
303 } else if(!strcmp(a, "bbox")) {
304 int xmin=0,ymin=0,xmax=0,ymax=0;
305 if (!PyArg_Parse(o, "(iiii)", &xmin, &ymin, &xmax, &ymax))
308 swf->swf.movieSize.xmin = xmin;
309 swf->swf.movieSize.ymin = ymin;
310 swf->swf.movieSize.xmax = xmax;
311 swf->swf.movieSize.ymax = ymax;
312 mylog("swf_setattr %08x(%d) %s = (%d,%d,%d,%d)\n", (int)self, self->ob_refcnt, a, xmin,ymin,xmax,ymax);
314 } else if(!strcmp(a, "tags")) {
316 /*if (!PyArg_Parse(o, "O!", &TagListClass, &taglist));
318 // TODO: check if it's really a taglist
320 Py_DECREF(swf->taglist);
321 swf->taglist = taglist;
322 Py_INCREF(swf->taglist);
323 mylog("swf_setattr %08x(%d) %s = %08x\n", (int)self, self->ob_refcnt, a, swf->taglist);
327 mylog("swf_setattr %08x(%d) %s = ? (%08x)\n", (int)self, self->ob_refcnt, a, o);
331 //----------------------------------------------------------------------------
332 static PyTypeObject SWFClass =
334 PyObject_HEAD_INIT(NULL)
337 tp_basicsize: sizeof(SWFObject),
339 tp_dealloc: swf_dealloc,
341 tp_getattr: swf_getattr,
342 tp_setattr: swf_setattr,
344 //----------------------------------------------------------------------------
346 static PyMethodDef SWFMethods[] =
349 {"load", f_load, METH_VARARGS, "Load a SWF from disc."},
350 {"create", (PyCFunction)f_create, METH_KEYWORDS, "Create a new SWF from scratch."},
352 // save is a member function
354 PyMethodDef* swf_getMethods()
356 SWFClass.ob_type = &PyType_Type;
360 // =============================================================================
362 #include "primitives.h"
367 static PyObject* module_verbose(PyObject* self, PyObject* args)
369 if (!PyArg_ParseTuple(args,"i", &verbose))
371 return Py_BuildValue("s", 0);
374 static PyMethodDef LoggingMethods[] =
376 /* Module functions */
377 {"verbose", module_verbose, METH_VARARGS, "Set the module verbosity"},
383 PyMethodDef* primitive_methods = primitive_getMethods();
384 PyMethodDef* tag_methods = tag_getMethods();
385 PyMethodDef* action_methods = action_getMethods();
386 PyMethodDef* swf_methods = swf_getMethods();
388 initLog("test.log",8,0,0,0,0);
390 PyMethodDef* all_methods = 0;
391 all_methods = addMethods(all_methods, primitive_methods);
392 all_methods = addMethods(all_methods, tag_methods);
393 all_methods = addMethods(all_methods, action_methods);
394 all_methods = addMethods(all_methods, swf_methods);
396 all_methods = addMethods(all_methods, LoggingMethods);
398 (void)Py_InitModule("SWF", all_methods);