sprite support.
[swftools.git] / lib / python / SWF.c
index d8ff006..9afe7de 100644 (file)
@@ -25,8 +25,9 @@
 #include "../rfxswf.h"
 #include "../log.h"
 #include "./pyutils.h"
-#include "./tag.h"
+#include "./tags.h"
 #include "./taglist.h"
+#include "./primitives.h"
 
 /*
 TODO:
@@ -72,8 +73,12 @@ static PyObject* f_create(PyObject* self, PyObject* args, PyObject* kwargs)
                &obbox, filename))
        return NULL;
 
-    if (!PyArg_Parse(obbox, "(iiii)", &bbox.xmin, &bbox.ymin, &bbox.xmax, &bbox.ymax))
-       return NULL;
+    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)
@@ -106,12 +111,12 @@ static PyObject* f_load(PyObject* self, PyObject* args)
     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;
     }
+    swf->filename = strdup(filename);
     fi = open(filename,O_RDONLY|O_BINARY);
     if (fi<0) { 
         PyErr_SetString(PyExc_Exception, setError("Couldn't open file %s", filename));
@@ -123,11 +128,14 @@ static PyObject* f_load(PyObject* self, PyObject* args)
        return 0;
     }
     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;
@@ -141,7 +149,7 @@ static PyObject * swf_save(PyObject* self, PyObject* args, PyObject* kwargs)
     int fi;
     char*filename = 0;
     int compress = 0;
-
+    
     if(!self)
        return NULL;
 
@@ -160,6 +168,8 @@ static PyObject * swf_save(PyObject* self, PyObject* args, PyObject* kwargs)
        swf->compressed = 1;
     
     swf->firstTag = taglist_getTAGs(swfo->taglist);
+    if(!swf->firstTag)
+       return NULL;
 
     // fix the file, in case it is empty or not terminated properly
     {
@@ -167,7 +177,6 @@ static PyObject * swf_save(PyObject* self, PyObject* args, PyObject* kwargs)
        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) {
@@ -194,9 +203,27 @@ static PyObject * swf_save(PyObject* self, PyObject* args, PyObject* kwargs)
            }
     }
     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;
 }
 //----------------------------------------------------------------------------
@@ -206,7 +233,10 @@ static PyObject * swf_writeCGI(PyObject* self, PyObject* args)
     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;
 }
@@ -323,10 +353,8 @@ static int swf_setattr(PyObject * self, char* a, PyObject * o)
        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);
@@ -391,12 +419,12 @@ static PyMethodDef LoggingMethods[] =
     {"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();
 
@@ -409,4 +437,9 @@ void initSWF(void)
     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) 
 }
+