#include "../rfxswf.h"
#include "../log.h"
#include "./pyutils.h"
-#include "./tag.h"
+#include "./tags.h"
#include "./taglist.h"
+#include "./primitives.h"
+#include "./action.h"
/*
TODO:
typedef struct {
PyObject_HEAD
- SWF swf; //swf.firstTag ist not used
+ SWF swf; //swf.firstTag is not used
PyObject*taglist;
char*filename;
} SWFObject;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|idOs",
kwlist, &version, &framerate,
- &obbox, filename))
+ &obbox, &filename))
return NULL;
- if (!PyArg_Parse(obbox, "(iiii)", &bbox.xmin, &bbox.ymin, &bbox.xmax, &bbox.ymax))
- return NULL;
+ if(obbox) {
+ if (!PY_CHECK_TYPE(obbox, &BBoxClass)) {
+ obbox = f_BBox(0, obbox, 0);
+ if(!obbox)
+ return NULL;
+ }
+ bbox = bbox_getSRECT(obbox);
+ }
memset(&swf->swf, 0, sizeof(SWF));
if(filename)
return (PyObject*)swf;
}
//----------------------------------------------------------------------------
-static PyObject* f_load(PyObject* self, PyObject* args)
+static PyObject* f_load(PyObject* self, PyObject* args, PyObject* kwargs)
{
- char* filename;
+ static char *kwlist1[] = {"filename", NULL};
+ static char *kwlist2[] = {"data", NULL};
+ char* filename = 0;
+ char* data = 0;
+ int len = 0;
SWFObject* swf;
int fi;
- if (!PyArg_ParseTuple(args,"s:load", &filename))
- return NULL;
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist1, &filename)) {
+ PyErr_Clear();
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#", kwlist2, &data, &len)) {
+ PyErr_Clear();
+ PyArg_ParseTupleAndKeywords(args, kwargs, "s:load", kwlist1, &filename);
+ return 0;
+ }
+ }
swf = PyObject_New(SWFObject, &SWFClass);
mylog("+%08x(%d) f_load\n", (int)swf, swf->ob_refcnt);
memset(&swf->swf, 0, sizeof(SWF));
- swf->filename = strdup(filename);
- if(!filename) {
- PyErr_SetString(PyExc_Exception, setError("Couldn't open file %s", filename));
- return 0;
- }
- fi = open(filename,O_RDONLY|O_BINARY);
- if (fi<0) {
- PyErr_SetString(PyExc_Exception, setError("Couldn't open file %s", filename));
- return 0;
- }
- if(swf_ReadSWF(fi,&swf->swf)<0) {
- close(fi);
- PyErr_SetString(PyExc_Exception, setError("%s is not a valid SWF file or contains errors",filename));
- return 0;
+ if(filename) {
+ if(!filename) {
+ PyErr_SetString(PyExc_Exception, setError("Couldn't open file %s", filename));
+ return 0;
+ }
+ swf->filename = strdup(filename);
+ fi = open(filename,O_RDONLY|O_BINARY);
+ if (fi<0) {
+ return PY_ERROR("Couldn't open file %s", filename);
+ }
+ if(swf_ReadSWF(fi,&swf->swf)<0) {
+ close(fi);
+ return PY_ERROR("%s is not a valid SWF file or contains errors",filename);
+ }
+ close(fi);
+ } else {
+ struct reader_t r;
+ reader_init_memreader(&r, data, len);
+ swf->filename = 0;
+ if(swf_ReadSWF2(&r, &swf->swf)<0) {
+ return PY_ERROR("<data> is not a valid SWF file or contains errors");
+ }
+ r.dealloc(&r);
}
- close(fi);
+ swf_FoldAll(&swf->swf);
swf->taglist = taglist_new2(swf->swf.firstTag);
if(swf->taglist == NULL) {
return NULL;
}
+
+ swf_FreeTags(&swf->swf);
swf->swf.firstTag = 0;
return (PyObject*)swf;
int fi;
char*filename = 0;
int compress = 0;
-
+
if(!self)
return NULL;
swf->firstTag = taglist_getTAGs(swfo->taglist);
+ /*if(!swf->firstTag)
+ return NULL;*/
+
// fix the file, in case it is empty or not terminated properly
{
TAG*tag = swf->firstTag;
if(!tag)
tag = swf->firstTag = swf_InsertTag(0,ST_END);
while(tag && tag->next) {
- mylog(" tag:%08x\n", tag);
tag = tag->next;
}
if(tag->id != ST_END) {
}
}
close(fi);
-
+
+ swf_FreeTags(swf);
+ /*{ TAG * t = swf->firstTag;
+ while (t)
+ {
+ mylog("tag: %08x\n",t);
+ mylog(" id: %d (%s)\n", t->id, swf_TagGetName(t));
+ mylog(" data: %08x (%d bytes)\n", t->data, t->len);
+ mylog(" next: %08x\n", t->next);
+ TAG * tnew = t->next;
+ mylog("->free data\n");
+ if (t->data) free(t->data);
+ mylog("->free tag\n");
+ free(t);
+ t = tnew;
+ }
+ }*/
swf->firstTag = 0;
+ mylog(" %08x(%d) f_save filename=%s done\n", (int)self, self->ob_refcnt, filename);
+
return PY_NONE;
}
//----------------------------------------------------------------------------
if(!self || !PyArg_ParseTuple(args,""))
return NULL;
swf->swf.firstTag = taglist_getTAGs(swf->taglist);
+ if(!swf->swf.firstTag)
+ return NULL;
swf_WriteCGI(&swf->swf);
+ swf_FreeTags(&swf->swf);
swf->swf.firstTag = 0;
return PY_NONE;
}
static PyObject* swf_getattr(PyObject * self, char* a)
{
SWFObject*swf = (SWFObject*)self;
- PyObject* ret;
+ PyObject* ret = 0;
if(!strcmp(a, "fps")) {
double fps = swf->swf.frameRate/256.0;
mylog(" %08x(%d) swf_getattr %s = %s\n", (int)self, self->ob_refcnt, a, filename);
return Py_BuildValue("s", filename);
} else if(!strcmp(a, "bbox")) {
- int xmin,ymin,xmax,ymax;
- xmin = swf->swf.movieSize.xmin;
- ymin = swf->swf.movieSize.ymin;
- xmax = swf->swf.movieSize.xmax;
- ymax = swf->swf.movieSize.ymax;
- mylog(" %08x(%d) swf_getattr %s = (%d,%d,%d,%d)\n", (int)self, self->ob_refcnt, a, xmin,ymin,xmax,ymax);
- return Py_BuildValue("(iiii)", xmin, ymin, xmax, ymax);
+ return f_BBox2(swf->swf.movieSize);
} else if(!strcmp(a, "tags")) {
PyObject*ret = (PyObject*)(swf->taglist);
Py_INCREF(ret);
mylog(" %08x(%d) swf_getattr %s = %08x(%d)\n", (int)self, self->ob_refcnt, a, ret, ret->ob_refcnt);
return ret;
+ } else if(!strcmp(a, "filesize")) {
+ int s = swf->swf.fileSize;
+ return Py_BuildValue("i", s);
+ } else if(!strcmp(a, "width")) {
+ int w = (swf->swf.movieSize.xmax - swf->swf.movieSize.xmin) / 20;
+ return Py_BuildValue("i", w);
+ } else if(!strcmp(a, "height")) {
+ int h = (swf->swf.movieSize.ymax - swf->swf.movieSize.ymin) / 20;
+ return Py_BuildValue("i", h);
+ } else if(!strcmp(a, "framecount")) {
+ int f = swf->swf.frameCount;
+ return Py_BuildValue("i", f);
}
ret = Py_FindMethod(swf_functions, self, a);
mylog(" %08x(%d) swf_setattr %s = %s\n", (int)self, self->ob_refcnt, a, filename);
return 0;
} else if(!strcmp(a, "bbox")) {
- int xmin=0,ymin=0,xmax=0,ymax=0;
- if (!PyArg_Parse(o, "(iiii)", &xmin, &ymin, &xmax, &ymax))
- goto err;
+ PyObject *obbox = o;
+ if (!PY_CHECK_TYPE(obbox, &BBoxClass)) {
+ obbox = f_BBox(0, o, 0);
+ if(!obbox)
+ return 1;
+ }
+ SRECT bbox = bbox_getSRECT(obbox);
- swf->swf.movieSize.xmin = xmin;
- swf->swf.movieSize.ymin = ymin;
- swf->swf.movieSize.xmax = xmax;
- swf->swf.movieSize.ymax = ymax;
- mylog(" %08x(%d) swf_setattr %s = (%d,%d,%d,%d)\n", (int)self, self->ob_refcnt, a, xmin,ymin,xmax,ymax);
+ swf->swf.movieSize = bbox;
+ mylog(" %08x(%d) swf_setattr %s = (%d,%d,%d,%d)\n", (int)self, self->ob_refcnt, a, bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax);
return 0;
} else if(!strcmp(a, "tags")) {
PyObject* taglist;
- /*if (!PyArg_Parse(o, "O!", &TagListClass, &taglist));
- goto err;*/
- // TODO: check if it's really a taglist
taglist = o;
+ PY_ASSERT_TYPE(taglist,&TagListClass);
Py_DECREF(swf->taglist);
swf->taglist = taglist;
Py_INCREF(swf->taglist);
static PyMethodDef SWFMethods[] =
{
/* SWF creation*/
- {"load", f_load, METH_VARARGS, "Load a SWF from disc."},
+ {"load", (PyCFunction)f_load, METH_KEYWORDS, "Load a SWF from disc."},
{"create", (PyCFunction)f_create, METH_KEYWORDS, "Create a new SWF from scratch."},
{0,0,0,0}
// save is a member function
{
int _verbose = 0;
static char *kwlist[] = {"verbosity", NULL};
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &verbose))
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &_verbose))
return NULL;
setVerbosity(_verbose);
{"verbose", (PyCFunction)module_verbose, METH_KEYWORDS, "Set the module verbosity"},
{0,0,0,0}
};
-
+
void initSWF(void)
{
PyObject*module;
PyMethodDef* primitive_methods = primitive_getMethods();
- PyMethodDef* tag_methods = tag_getMethods();
+ PyMethodDef* tag_methods = tags_getMethods();
PyMethodDef* action_methods = action_getMethods();
PyMethodDef* swf_methods = swf_getMethods();
all_methods = addMethods(all_methods, LoggingMethods);
module = Py_InitModule("SWF", all_methods);
+
+ /* Python doesn't copy the PyMethodDef struct, so we need
+ to keep it around */
+ // free(all_methods)
}