From: kramm Date: Fri, 16 Apr 2004 17:51:21 +0000 (+0000) Subject: fixed tag organisation, started video stuff X-Git-Tag: release-0-6-0~254 X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=commitdiff_plain;h=3477d799207e51e1cde8d1d8792ae838cc1f2835 fixed tag organisation, started video stuff --- diff --git a/lib/python/Makefile.in b/lib/python/Makefile.in index 66c8d51..312c92e 100644 --- a/lib/python/Makefile.in +++ b/lib/python/Makefile.in @@ -17,6 +17,8 @@ taglist.o: taglist.c taglist.h tagmap.h tag.h $(C) -Wall -Wno-unused -fPIC $(PYTHON_INCLUDES) -c taglist.c -o taglist.o tag.o: tag.c tag.h tagmap.h $(C) -Wall -Wno-unused -fPIC $(PYTHON_INCLUDES) -c tag.c -o tag.o +tags.o: tags.c tags.h tagmap.h tag.h + $(C) -Wall -Wno-unused -fPIC $(PYTHON_INCLUDES) -c tags.c -o tags.o action.o: action.c action.h $(C) -Wall -Wno-unused -fPIC $(PYTHON_INCLUDES) -c action.c -o action.o pyutils.o: pyutils.c pyutils.h @@ -25,14 +27,14 @@ pyutils.o: pyutils.c pyutils.h SWF.o: SWF.c pyutils.h primitives.h taglist.h $(C) -Wall -Wno-unused -fPIC $(PYTHON_INCLUDES) -c SWF.c -o SWF.o -SWF.so: SWF.o taglist.o tag.o tagmap.o action.o primitives.o pyutils.o ../librfxswf.a Makefile - $(L) -g -shared SWF.o tagmap.o taglist.o tag.o action.o primitives.o pyutils.o -o SWF.so ../librfxswf.a $(PYTHON_LIB) -ljpeg -lz -lt1 -lfreetype +SWF.so: SWF.o taglist.o tag.o tags.o tagmap.o action.o primitives.o pyutils.o ../librfxswf.a Makefile + $(L) -g -shared SWF.o tagmap.o taglist.o tag.o tags.o action.o primitives.o pyutils.o -o SWF.so ../librfxswf.a $(PYTHON_LIB) -ljpeg -lz -lt1 -lfreetype mypython: mypython.c Makefile - $(L) -g $(PYTHON_INCLUDES) mypython.c -o mypython $(PYTHON_LIB) -ldl -lm -lpthread -lutil + $(L) -Wno-redundant-decls -g $(PYTHON_INCLUDES) mypython.c -o mypython $(PYTHON_LIB) -ldl -lm -lpthread -lutil -swfpython: swfpython.c Makefile primitives.o tagmap.o taglist.o tag.o action.o pyutils.o SWF.o - $(L) -g $(PYTHON_INCLUDES) swfpython.c -o swfpython $(PYTHON_LIB) -ldl -lm -lpthread -lutil ../librfxswf.a $(PYTHON_LIB) -ljpeg -lz -lt1 -lfreetype +swfpython: swfpython.c Makefile primitives.o tagmap.o taglist.o tag.o tags.o action.o pyutils.o SWF.o + $(L) -Wno-redundant-decls -g $(PYTHON_INCLUDES) swfpython.c -o swfpython $(PYTHON_LIB) -ldl -lm -lpthread -lutil ../librfxswf.a $(PYTHON_LIB) -ljpeg -lz -lt1 -lfreetype testmem: testmem.c Makefile $(L) $(PYTHON_INCLUDES) testmem.c -o testmem $(PYTHON_LIB) -ldl -lm -lpthread -lutil @@ -49,6 +51,8 @@ test2: SWF.so test3: SWF.so ./test_simple.py +test4: SWF.so + ./test_movie.py clean: rm -f *.o *.so mypython swfpython diff --git a/lib/python/SWF.c b/lib/python/SWF.c index ef7d49e..aa2c7f3 100644 --- a/lib/python/SWF.c +++ b/lib/python/SWF.c @@ -25,7 +25,7 @@ #include "../rfxswf.h" #include "../log.h" #include "./pyutils.h" -#include "./tag.h" +#include "./tags.h" #include "./taglist.h" /* @@ -417,7 +417,7 @@ 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(); diff --git a/lib/python/mypython.c b/lib/python/mypython.c index cac8b4d..951586b 100644 --- a/lib/python/mypython.c +++ b/lib/python/mypython.c @@ -1,6 +1,3 @@ -#include -#include -#include #include int main(int argn, char*argv[]) diff --git a/lib/python/primitives.h b/lib/python/primitives.h index e215344..47647e6 100644 --- a/lib/python/primitives.h +++ b/lib/python/primitives.h @@ -26,6 +26,8 @@ #undef HAVE_STAT #include +#include "../rfxswf.h" + /* exported to allow casting to this type */ extern PyTypeObject ColorClass; extern PyTypeObject BBoxClass; diff --git a/lib/python/tag.c b/lib/python/tag.c index 9d69de2..13b4863 100644 --- a/lib/python/tag.c +++ b/lib/python/tag.c @@ -10,382 +10,84 @@ //---------------------------------------------------------------------------- -struct _TagObject; - -typedef struct _tag_internals -{ - int size; - int (*parse)(struct _TagObject*); - int (*fillTAG)(struct _TagObject*); - void (*dealloc)(struct _TagObject*); - void*data; - PyMethodDef* tagfunctions; -} tag_internals_t; - typedef struct _TagObject { PyObject_HEAD - - PyObject* tagmap; - TAG*tag; tag_internals_t internals; } TagObject; //---------------------------------------------------------------------------- - -PyObject* tag_new(tag_internals_t*tag_internals); - -typedef struct _font_internal -{ - SWFFONT* font; -} font_internal_t; -staticforward tag_internals_t font_tag; - -static int font_parse(TagObject*self) -{ - font_internal_t*font = (font_internal_t*)self->internals.data; - /* TODO */ - PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet")); - return 0; -} -static void font_dealloc(TagObject*self) -{ - font_internal_t*font = (font_internal_t*)self->internals.data; - if(font->font) { - swf_FontFree(font->font); - font->font = 0; - } -} -static int font_fillTAG(TagObject*self) -{ - font_internal_t*fi = (font_internal_t*)self->internals.data; - if(self->tag) - return 1; - self->tag = swf_InsertTag(0, ST_DEFINEFONT2); - swf_FontSetDefine2(self->tag, fi->font); - return 1; -} -static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs) -{ - static char *kwlist[] = {"filename", NULL}; - char*filename = 0; - TagObject*tag; - SWFFONT* font; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &filename)) - return NULL; - - font = swf_LoadFont(filename); - if(!font) { - PyErr_SetString(PyExc_Exception, setError("Could not load %s", filename)); - return NULL; - } - - tag = (TagObject*)tag_new(&font_tag); - font_internal_t*fi = (font_internal_t*)tag->internals.data; - font->id = 0; - fi->font = font; - return (PyObject*)tag; -} -static SWFFONT* font_getSWFFONT(PyObject*self) -{ - PY_ASSERT_TYPE(self, &TagClass); - TagObject*tag = (TagObject*)self; - font_internal_t*fi = (font_internal_t*)tag->internals.data; - return fi->font; -} -static tag_internals_t font_tag = -{ - size: sizeof(font_internal_t), - parse: font_parse, - fillTAG: font_fillTAG, - dealloc: font_dealloc, - tagfunctions: 0 -}; -//---------------------------------------------------------------------------- - -typedef struct _placeobject_internal -{ - SWFPLACEOBJECT* po; -} placeobject_internal_t; -staticforward tag_internals_t placeobject_tag; - -static void po_dealloc(TagObject*self) -{ - placeobject_internal_t*pi = (placeobject_internal_t*)self->internals.data; - if(pi->po) { - swf_PlaceObjectFree(pi->po); - pi->po = 0; - } -} -static int po_parse(TagObject*self) -{ - placeobject_internal_t*pi = (placeobject_internal_t*)self->internals.data; - /* TODO */ - PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet")); - return 0; -} -static int po_fillTAG(TagObject*self) -{ - placeobject_internal_t*pi = (placeobject_internal_t*)self->internals.data; - self->tag = swf_InsertTag(0, ST_PLACEOBJECT2); - swf_SetPlaceObject(self->tag, pi->po); - return 1; -} -static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs) -{ - static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL}; - TagObject*tag; - - TagObject*character = 0; - int depth; - int clipdepth = 0; - PyObject*matrix = 0; - PyObject*cxform = 0; - PyObject*action = 0; - int ratio = 0; - char* name = 0; - SWFPLACEOBJECT* po; - po = malloc(sizeof(SWFPLACEOBJECT)); - memset(po, 0, sizeof(SWFPLACEOBJECT)); - - swf_GetPlaceObject(0, po); - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!i|O!O!isiO!", kwlist, - &TagClass, &character, - &depth, - &MatrixClass, &matrix, - &CXFormClass, &cxform, - &ratio, - &name, - &clipdepth, - &ActionClass, &action - )) - return NULL; - po->depth = depth; - po->id = /*ID*/ 0; - po->clipdepth = clipdepth; - po->ratio = ratio; - po->name = name; - if(clipdepth) po->clipdepth = clipdepth; - if(matrix) po->matrix = matrix_getMatrix(matrix); - if(cxform) po->cxform = colortransform_getCXForm(cxform); - if(action) po->actions = action_getAction(action); - - tag = (TagObject*)tag_new(&placeobject_tag); - placeobject_internal_t*pi = (placeobject_internal_t*)tag->internals.data; - pi->po = po; - pi->po->id = tagmap_add(tag->tagmap,(PyObject*)character); - - mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt); - - return (PyObject*)tag; -} -static tag_internals_t placeobject_tag = -{ - size: sizeof(placeobject_internal_t), - parse: po_parse, - fillTAG: po_fillTAG, - dealloc: po_dealloc, - tagfunctions: 0 -}; -//---------------------------------------------------------------------------- -staticforward tag_internals_t bgcolor_tag; -static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other) -{ - TagObject*tag = (TagObject*)self; - int r,g,b; - r = tag->tag->data[0]; - g = tag->tag->data[1]; - b = tag->tag->data[2]; - return Py_BuildValue("(iii)", r,g,b); -} -static PyMethodDef setbgcolor_methods[] = -{{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"}, - {NULL, NULL, 0, NULL} -}; -static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs) -{ - static char *kwlist[] = {"color", NULL}; - int r=0,g=0,b=0; - TagObject*tag; - PyObject*color; - - tag = (TagObject*)tag_new(&bgcolor_tag); - - /* 1st try- copy constructor */ - if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) { - /* 2nd try- color's contructor */ - color = f_Color(NULL, args, kwargs); - } - if(!color) - return NULL; - - tag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR); - RGBA rgba = color_getRGBA(color); - swf_SetU8(tag->tag, rgba.r); - swf_SetU8(tag->tag, rgba.g); - swf_SetU8(tag->tag, rgba.b); - mylog(" %08x(%d) SetBackgroundColor(%02x,%02x,%02x) (colorobj=%08x(%d))\n", (int)tag, tag->ob_refcnt, rgba.r, rgba.g, rgba.b, color, color->ob_refcnt); - return (PyObject*)tag; -} -static tag_internals_t bgcolor_tag = -{ - size: 0, - parse: 0, - fillTAG: 0, - dealloc: 0, - tagfunctions: setbgcolor_methods -}; -//---------------------------------------------------------------------------- -staticforward tag_internals_t protect_tag; -static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs) -{ - static char *kwlist[] = {"password", NULL}; - char*password = 0; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password)) - return NULL; - - TagObject*tag = (TagObject*)tag_new(&protect_tag); - tag->tag = swf_InsertTag(0, ST_PROTECT); - if(password) { - swf_SetPassword(tag->tag, password); - } - mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt); - return (PyObject*)tag; -} -static tag_internals_t protect_tag = -{ - size: 0, - parse: 0, - fillTAG: 0, - dealloc: 0, - tagfunctions: 0 -}; -//---------------------------------------------------------------------------- -staticforward tag_internals_t text_tag; - -typedef struct _text_internal // not used yet -{ - int font_id; - char*text; - SWFFONT* swffont; - RGBA rgba; - int size; -} text_internal_t; -staticforward tag_internals_t placeobject_tag; - -static int text_fillTAG(TagObject*self) //not used yet -{ - text_internal_t*ti = (text_internal_t*)self->internals.data; - self->tag= swf_InsertTag(0, ST_DEFINETEXT2); - swf_SetU16(self->tag, /*ID*/0); - SRECT r = swf_SetDefineText(self->tag, ti->swffont, &ti->rgba, ti->text, ti->size); - return 0; -} -static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs) -{ - static char *kwlist[] = {"font", "text", "size", "color", NULL}; - TagObject*tag = 0; - char*text = 0; - int textlen = 0; - PyObject*unicode16; - PyObject*unicode8; - int size = 0; - RGBA rgba = {255,0,0,0}; - PyObject*color = 0; - PyObject*font = 0; - SRECT r; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!u#i|O!", kwlist, &TagClass, &font, &text, &textlen, &size, &ColorClass, &color)) - return NULL; - - unicode16 = PyUnicode_DecodeUTF16(text, textlen*2, NULL, NULL); - unicode8 = PyUnicode_AsUTF8String(unicode16); - text = PyString_AS_STRING(unicode8); - - if(color) - rgba = color_getRGBA(color); - - mylog("DefineText: text = %s", text); - - tag = (TagObject*)tag_new(&text_tag); - text_internal_t*ti = (text_internal_t*)tag->internals.data; - - SWFFONT* swffont = font_getSWFFONT(font); - int font_id = tagmap_add(tag->tagmap, font); // add dependency on font - swffont->id = font_id; // for swf_SetDefineText - - /* todo: remove, store data in ti */ - tag->tag= swf_InsertTag(0, ST_DEFINETEXT2); - swf_SetU16(tag->tag, /*ID*/0); - r = swf_SetDefineText(tag->tag, swffont, &rgba, text, size); - mylog("+%08x(%d) DefineText tag=%08x \n", (int)tag, tag->ob_refcnt); - - //Py_DECREF(unicode16); - //Py_DECREF(unicode8); - //free(text); - - return (PyObject*)tag; -} -static tag_internals_t text_tag = -{ - size: sizeof(text_internal_t), - parse: 0, - fillTAG: text_fillTAG, - dealloc: 0, - tagfunctions: 0 -}; -//---------------------------------------------------------------------------- static PyMethodDef generic_methods[] = { {NULL, NULL, 0, NULL} }; static tag_internals_t generic_tag = { - size: 0, parse: 0, dealloc: 0, fillTAG: 0, - tagfunctions: generic_methods + tagfunctions: generic_methods, + datasize: 0, }; //---------------------------------------------------------------------------- -struct tag_parser { +static struct tag_parser { int id; tag_internals_t*spec; -} tag_parsers[] = { - {ST_PLACEOBJECT,&placeobject_tag}, - {ST_PLACEOBJECT2,&placeobject_tag}, - {ST_SETBACKGROUNDCOLOR,&bgcolor_tag}, - {ST_DEFINEFONT,&font_tag}, - {ST_PROTECT,&protect_tag}, - {ST_DEFINETEXT,&text_tag}, - {-1,0} + struct tag_parser* next; +} tag_parsers[1024]; +static char parsers_initialized = 0; + +void register_tag(int id, tag_internals_t*spec) +{ + assert(id>=0 && id<1024); + if(!parsers_initialized) { + memset(tag_parsers, 0, sizeof(tag_parsers)); + parsers_initialized = 1; + } + tag_parsers[id].id = id; + tag_parsers[id].spec = spec; }; + +static tag_internals_t* get_parser(int id) +{ + if(parsers_initialized<2) { + int t; + struct tag_parser*last = &tag_parsers[0]; + for(t=0;t<1024;t++) { + if(tag_parsers[t].spec) { + last->next = &tag_parsers[t]; + last = &tag_parsers[t]; + } + } + parsers_initialized = 2; + } + assert(id>=0 && id<1024); + return tag_parsers[id].spec; +} + //---------------------------------------------------------------------------- static void tag_dealloc(PyObject * self) { TagObject*tag = (TagObject*)self; - if(tag->tag) - mylog("-%08x(%d) tag_dealoc [%s]\n", (int)self, self->ob_refcnt, swf_TagGetName(tag->tag)); + if(tag->internals.tag) + mylog("-%08x(%d) tag_dealoc [%s]\n", (int)self, self->ob_refcnt, swf_TagGetName(tag->internals.tag)); else mylog("-%08x(%d) tag_dealoc [?]\n", (int)self, self->ob_refcnt); if(tag->internals.dealloc) { if(!tag->internals.data) mylog("-%08x(%d) tag_dealoc: Warning: calling dealloc without any data(?)\n", (int)self, self->ob_refcnt); - tag->internals.dealloc(tag); + tag->internals.dealloc(&tag->internals); } if(tag->internals.data) { free(tag->internals.data); tag->internals.data = 0; } - if(tag->tag) { - swf_DeleteTag(tag->tag); - tag->tag = 0; + if(tag->internals.tag) { + swf_DeleteTag(tag->internals.tag); + tag->internals.tag = 0; } - Py_DECREF(tag->tagmap); - tag->tagmap = 0; + Py_DECREF(tag->internals.tagmap); + tag->internals.tagmap = 0; PyObject_Del(self); } //---------------------------------------------------------------------------- @@ -402,16 +104,16 @@ static PyMethodDef common_tagfunctions[] = static int fillTAG(PyObject*self) { TagObject*tag = (TagObject*)self; - if(tag->tag) + if(tag->internals.tag) return 1; if(!tag->internals.fillTAG) { PyErr_SetString(PyExc_Exception, setError("No way to fill TAG with data")); return 0; } - if(!tag->internals.fillTAG(tag)) { + if(!tag->internals.fillTAG(&tag->internals)) { return 0; // pass through exception } - if(!tag->tag) { + if(!tag->internals.tag) { PyErr_SetString(PyExc_Exception, setError("Couldn't fill tag")); return 0; } @@ -424,15 +126,15 @@ static PyObject* tag_getattr(PyObject * self, char* a) int t; /* -- fields -- */ - if(!strcmp(a, "id")) { + if(!strcmp(a, "tagid")) { if(!fillTAG(self)) return 0; - return Py_BuildValue("i", tag->tag->id); + return Py_BuildValue("i", tag->internals.tag->id); } if(!strcmp(a, "name")) { if(!fillTAG(self)) return 0; - char* name = swf_TagGetName(tag->tag); + char* name = swf_TagGetName(tag->internals.tag); return Py_BuildValue("s", name); } @@ -459,14 +161,14 @@ PyObject* tag_new(tag_internals_t*tag_internals) TagObject*tag = PyObject_New(TagObject, &TagClass); mylog("+%08x(%d) tag_new\n", (int)tag, tag->ob_refcnt); memcpy(&tag->internals, tag_internals, sizeof(tag_internals_t)); - if(tag->internals.size) { - tag->internals.data = malloc(tag->internals.size); - memset(tag->internals.data , 0, tag->internals.size); + if(tag->internals.datasize) { + tag->internals.data = malloc(tag->internals.datasize); + memset(tag->internals.data , 0, tag->internals.datasize); } else { tag->internals.data = 0; } - tag->tag = 0; - tag->tagmap = tagmap_new(); + tag->internals.tag = 0; + tag->internals.tagmap = tagmap_new(); return (PyObject*)tag; } @@ -475,7 +177,7 @@ PyObject* tag_new2(TAG*t, PyObject* tagmap) TagObject*tag = PyObject_New(TagObject, &TagClass); mylog("+%08x(%d) tag_new2 tag=%08x id=%d (%s)\n", (int)tag, tag->ob_refcnt, t, t->id, swf_TagGetName(t)); - tag->tagmap = tagmap_new(); + PyObject*mytagmap = tagmap_new(); int num = swf_GetNumUsedIDs(t); if(num) { // tag has dependencies @@ -491,32 +193,29 @@ PyObject* tag_new2(TAG*t, PyObject* tagmap) } //mylog("+%08x(%d) tag_new2 handling id %d at %d/%d\n", (int)tag, tag->ob_refcnt, id, positions[i], t->len); //mylog("+%08x(%d) tag_new2 add dependency %d to id %d, object %08x(%d)\n", (int)tag, tag->ob_refcnt, i, id, obj, obj->ob_refcnt); - tagmap_addMapping(tag->tagmap, id, obj); + tagmap_addMapping(mytagmap, id, obj); } free(positions); } - tag->tag = swf_InsertTag(0, t->id); - swf_SetBlock(tag->tag, t->data, t->len); - - int i=0,found=0; - while(tag_parsers[i].id>=0) { - if(tag_parsers[i].id == t->id) { - memcpy(&tag->internals, tag_parsers[i].spec, sizeof(tag_internals_t)); - found=1; - break; - } - i++; - } - if(!found) { + tag_internals_t*spec = get_parser(t->id); + if(spec) { + memcpy(&tag->internals, spec, sizeof(tag_internals_t)); + } else { memcpy(&tag->internals, &generic_tag, sizeof(tag_internals_t)); } - if(tag->internals.size) { - tag->internals.data = malloc(tag->internals.size); - memset(tag->internals.data, 0, tag->internals.size); + if(tag->internals.datasize) { + tag->internals.data = malloc(tag->internals.datasize); + memset(tag->internals.data, 0, tag->internals.datasize); } else { tag->internals.data = 0; } + tag->internals.tag = swf_InsertTag(0, t->id); + swf_SetBlock(tag->internals.tag, t->data, t->len); + tag->internals.tagmap = mytagmap; + + // call tag->internals.init()? + return (PyObject*)tag; } //---------------------------------------------------------------------------- @@ -527,10 +226,10 @@ TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap) if(!fillTAG(self)) return 0; - mylog(" %08x(%d) tag_getTAG: tag=%08x id=%d (%s)", (int)self, self->ob_refcnt, tag->tag, tag->tag->id, swf_TagGetName(tag->tag)); + mylog(" %08x(%d) tag_getTAG: tag=%08x id=%d (%s)", (int)self, self->ob_refcnt, tag->internals.tag, tag->internals.tag->id, swf_TagGetName(tag->internals.tag)); - TAG* t = swf_InsertTag(prevTag, tag->tag->id); - swf_SetBlock(t, tag->tag->data, tag->tag->len); + TAG* t = swf_InsertTag(prevTag, tag->internals.tag->id); + swf_SetBlock(t, tag->internals.tag->data, tag->internals.tag->len); if(swf_isDefiningTag(t)) { int newid = tagmap_add(tagmap, self); @@ -544,34 +243,44 @@ TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap) int i; for(i=0;idata[positions[i]]); - PyObject* obj = tagmap_id2obj(tag->tagmap, id); + PyObject* obj = tagmap_id2obj(tag->internals.tagmap, id); if(obj==NULL) { PyErr_SetString(PyExc_Exception, setError("Internal error: id %d not known in taglist:")); free(positions); return 0; } - int newid = tagmap_obj2id(tagmap, obj); + int newid = tagmap_obj2id(tag->internals.tagmap, obj); PUT16(&t->data[positions[i]], newid); } free(positions); } return t; } +//---------------------------------------------------------------------------- +tag_internals_t* tag_getinternals(PyObject*self) +{ + TagObject*tag = (TagObject*)self; + mylog(" %08x(%d) tag_getInternals\n", (int)self, self->ob_refcnt); + return &tag->internals; +} +//---------------------------------------------------------------------------- PyObject* tag_getDependencies(PyObject*self) { TagObject*tag = (TagObject*)self; mylog(" %08x(%d) tag_getDependencies\n", (int)self, self->ob_refcnt); - return tagmap_getObjectList(tag->tagmap); + return tagmap_getObjectList(tag->internals.tagmap); } +//---------------------------------------------------------------------------- int tag_print(PyObject * self, FILE * fi, int flags) { TagObject*tag = (TagObject*)self; mylog(" %08x(%d) tag_print flags=%08x\n", (int)self, self->ob_refcnt, flags); if(!fillTAG(self)) return -1; - fprintf(fi, "tag-%08x-%d-%s", (int)tag->tag, tag->tag->id, swf_TagGetName(tag->tag)); + fprintf(fi, "tag-%08x-%d-%s", (int)tag->internals.tag, tag->internals.tag->id, swf_TagGetName(tag->internals.tag)); return 0; } +//---------------------------------------------------------------------------- PyTypeObject TagClass = { PyObject_HEAD_INIT(NULL) @@ -583,19 +292,3 @@ PyTypeObject TagClass = tp_print: tag_print, tp_getattr: tag_getattr, }; -static PyMethodDef TagMethods[] = -{ - /* TAGS */ - {"SetBackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."}, - {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."}, - {"DefineFont", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."}, - {"DefineText", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."}, - {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."}, - {NULL, NULL, 0, NULL} -}; -PyMethodDef* tag_getMethods() -{ - TagClass.ob_type = &PyType_Type; - return TagMethods; -} - diff --git a/lib/python/tag.h b/lib/python/tag.h index dcdbec1..7ee0338 100644 --- a/lib/python/tag.h +++ b/lib/python/tag.h @@ -23,15 +23,33 @@ #ifndef __tag_h__ #define __tag_h__ -#include "../rfxswf.h" #undef HAVE_STAT #include +#include "../rfxswf.h" -extern PyTypeObject TagClass; - +#include "tagmap.h" + +typedef struct _tag_internals +{ + int (*parse)(struct _tag_internals*); + int (*fillTAG)(struct _tag_internals*); + void (*dealloc)(struct _tag_internals*); + int datasize; + void*data; + PyMethodDef* tagfunctions; + TAG*tag; + PyObject* tagmap; +} tag_internals_t; + +PyObject* tag_new(tag_internals_t*tag_internals); PyObject* tag_new2(TAG*_tag, PyObject* tagmap); TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap); PyObject* tag_getDependencies(PyObject*self); +tag_internals_t* tag_getinternals(PyObject*tag); +void register_tag(int id, tag_internals_t*spec); PyMethodDef* tag_getMethods(); + +extern PyTypeObject TagClass; + #endif diff --git a/lib/python/taglist.c b/lib/python/taglist.c index 182e8bb..d606ab3 100644 --- a/lib/python/taglist.c +++ b/lib/python/taglist.c @@ -284,7 +284,6 @@ static void taglist_dealloc(PyObject* self) { TagListObject*taglist = (TagListObject*)self; mylog("-%08x(%d) taglist_dealloc list=%08x(%d)\n", (int)self, self->ob_refcnt, taglist->taglist, taglist->taglist->ob_refcnt); - taglist_showcontents(self); Py_DECREF(taglist->taglist); taglist->taglist = 0; PyObject_Del(self); diff --git a/lib/python/tags.c b/lib/python/tags.c new file mode 100644 index 0000000..54625e6 --- /dev/null +++ b/lib/python/tags.c @@ -0,0 +1,457 @@ +#include "pyutils.h" +#include "primitives.h" +#include "action.h" +#include "tag.h" +#include "tags.h" + +//---------------------------------------------------------------------------- + +typedef struct _font_internal +{ + SWFFONT* font; +} font_internal_t; +staticforward tag_internals_t font_tag; + +static int font_parse(tag_internals_t*self) +{ + font_internal_t*font = (font_internal_t*)self->data; + /* TODO */ + PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet")); + return 0; +} +static void font_dealloc(tag_internals_t*self) +{ + font_internal_t*font = (font_internal_t*)self->data; + if(font->font) { + swf_FontFree(font->font); + font->font = 0; + } +} +static int font_fillTAG(tag_internals_t*self) +{ + font_internal_t*fi = (font_internal_t*)self->data; + if(self->tag) + return 1; + self->tag = swf_InsertTag(0, ST_DEFINEFONT2); + swf_FontSetDefine2(self->tag, fi->font); + return 1; +} +static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs) +{ + static char *kwlist[] = {"filename", NULL}; + char*filename = 0; + PyObject*tag; + SWFFONT* font; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &filename)) + return NULL; + + font = swf_LoadFont(filename); + if(!font) { + PyErr_SetString(PyExc_Exception, setError("Could not load %s", filename)); + return NULL; + } + + tag = tag_new(&font_tag); + tag_internals_t*itag = tag_getinternals(tag); + font_internal_t*fi = (font_internal_t*)itag->data; + font->id = 0; + fi->font = font; + return (PyObject*)tag; +} +static SWFFONT* font_getSWFFONT(PyObject*self) +{ + PY_ASSERT_TYPE(self, &TagClass); + tag_internals_t*itag = tag_getinternals(self); + font_internal_t*fi = (font_internal_t*)itag->data; + return fi->font; +} +static tag_internals_t font_tag = +{ + parse: font_parse, + fillTAG: font_fillTAG, + dealloc: font_dealloc, + tagfunctions: 0, + datasize: sizeof(font_internal_t), +}; +//---------------------------------------------------------------------------- + +typedef struct _placeobject_internal +{ + SWFPLACEOBJECT* po; +} placeobject_internal_t; +staticforward tag_internals_t placeobject_tag; + +static void po_dealloc(tag_internals_t*self) +{ + placeobject_internal_t*pi = (placeobject_internal_t*)self->data; + if(pi->po) { + swf_PlaceObjectFree(pi->po); + pi->po = 0; + } +} +static int po_parse(tag_internals_t*self) +{ + placeobject_internal_t*pi = (placeobject_internal_t*)self->data; + /* TODO */ + PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet")); + return 0; +} +static int po_fillTAG(tag_internals_t*self) +{ + placeobject_internal_t*pi = (placeobject_internal_t*)self->data; + self->tag = swf_InsertTag(0, ST_PLACEOBJECT2); + swf_SetPlaceObject(self->tag, pi->po); + return 1; +} +static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs) +{ + static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL}; + + PyObject*character = 0; + int depth; + int clipdepth = 0; + PyObject*matrix = 0; + PyObject*cxform = 0; + PyObject*action = 0; + int ratio = 0; + char* name = 0; + SWFPLACEOBJECT* po; + po = malloc(sizeof(SWFPLACEOBJECT)); + memset(po, 0, sizeof(SWFPLACEOBJECT)); + + swf_GetPlaceObject(0, po); + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|O!O!isiO!", kwlist, + &character, + &depth, + &MatrixClass, &matrix, + &CXFormClass, &cxform, + &ratio, + &name, + &clipdepth, + &ActionClass, &action + )) + return NULL; + po->depth = depth; + po->id = /*ID*/ 0; + po->clipdepth = clipdepth; + po->ratio = ratio; + po->name = name; + if(clipdepth) po->clipdepth = clipdepth; + if(matrix) po->matrix = matrix_getMatrix(matrix); + if(cxform) po->cxform = colortransform_getCXForm(cxform); + if(action) po->actions = action_getAction(action); + + PyObject*tag; + tag = tag_new(&placeobject_tag); + tag_internals_t*itag = tag_getinternals(tag); + placeobject_internal_t*pi = (placeobject_internal_t*)itag->data; + pi->po = po; + pi->po->id = tagmap_add(itag->tagmap,(PyObject*)character); + + mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt); + + return (PyObject*)tag; +} +static tag_internals_t placeobject_tag = +{ + parse: po_parse, + fillTAG: po_fillTAG, + dealloc: po_dealloc, + tagfunctions: 0, + datasize: sizeof(placeobject_internal_t), +}; +//---------------------------------------------------------------------------- +staticforward tag_internals_t bgcolor_tag; +static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other) +{ + tag_internals_t*itag = tag_getinternals(self); + int r,g,b; + r = itag->tag->data[0]; + g = itag->tag->data[1]; + b = itag->tag->data[2]; + return Py_BuildValue("(iii)", r,g,b); +} +static PyMethodDef setbgcolor_methods[] = +{{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"}, + {NULL, NULL, 0, NULL} +}; +static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs) +{ + static char *kwlist[] = {"color", NULL}; + int r=0,g=0,b=0; + PyObject*tag; + PyObject*color; + + tag = tag_new(&bgcolor_tag); + tag_internals_t*itag = tag_getinternals(tag); + + /* 1st try- copy constructor */ + if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) { + PyErr_Clear(); + /* 2nd try- color's contructor */ + color = f_Color(NULL, args, kwargs); + } + if(!color) + return NULL; + + itag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR); + RGBA rgba = color_getRGBA(color); + swf_SetU8(itag->tag, rgba.r); + swf_SetU8(itag->tag, rgba.g); + swf_SetU8(itag->tag, rgba.b); + mylog(" %08x(%d) SetBackgroundColor(%02x,%02x,%02x) (colorobj=%08x(%d))\n", (int)tag, tag->ob_refcnt, rgba.r, rgba.g, rgba.b, color, color->ob_refcnt); + Py_DECREF(color); + return (PyObject*)tag; +} +static tag_internals_t bgcolor_tag = +{ + parse: 0, + fillTAG: 0, + dealloc: 0, + tagfunctions: setbgcolor_methods, + datasize: 0, +}; +//---------------------------------------------------------------------------- +staticforward tag_internals_t protect_tag; +static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs) +{ + static char *kwlist[] = {"password", NULL}; + char*password = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password)) + return NULL; + + PyObject*tag = tag_new(&protect_tag); + tag_internals_t*itag = tag_getinternals(tag); + itag->tag = swf_InsertTag(0, ST_PROTECT); + if(password) { + swf_SetPassword(itag->tag, password); + } + mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt); + return (PyObject*)tag; +} +static tag_internals_t protect_tag = +{ + parse: 0, + fillTAG: 0, + dealloc: 0, + tagfunctions: 0, + datasize: 0, +}; +//---------------------------------------------------------------------------- +staticforward tag_internals_t end_tag; +static tag_internals_t end_tag = +{ + parse: 0, + fillTAG: 0, + dealloc: 0, + tagfunctions: 0, + datasize: 0, +}; +//---------------------------------------------------------------------------- +staticforward tag_internals_t text_tag; + +typedef struct _text_internal +{ + char*text; + SWFFONT* swffont; + RGBA rgba; + int size; +} text_internal_t; +staticforward tag_internals_t placeobject_tag; + +static int text_fillTAG(tag_internals_t*self) +{ + text_internal_t*ti = (text_internal_t*)self->data; + self->tag= swf_InsertTag(0, ST_DEFINETEXT2); + swf_SetU16(self->tag, /*ID*/0); + SRECT r = swf_SetDefineText(self->tag, ti->swffont, &ti->rgba, ti->text, ti->size); + return 1; +} +static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs) +{ + static char *kwlist[] = {"font", "text", "size", "color", NULL}; + PyObject*tag = 0; + char*text = 0; + int textlen = 0; + PyObject*unicode16; + PyObject*unicode8; + int size = 0; + RGBA rgba = {255,0,0,0}; + PyObject*color = 0; + PyObject*font = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!u#i|O!", kwlist, &TagClass, &font, &text, &textlen, &size, &ColorClass, &color)) + return NULL; + + unicode16 = PyUnicode_DecodeUTF16(text, textlen*2, NULL, NULL); + unicode8 = PyUnicode_AsUTF8String(unicode16); + text = PyString_AS_STRING(unicode8); + + if(color) + rgba = color_getRGBA(color); + + mylog("DefineText: text = %s", text); + + tag = tag_new(&text_tag); + tag_internals_t* itag = tag_getinternals(tag); + text_internal_t*ti = (text_internal_t*)itag->data; + + ti->swffont = font_getSWFFONT(font); + int font_id = tagmap_add(itag->tagmap, font); // add dependency on font + ti->swffont->id = font_id; // for swf_SetDefineTexts + ti->text = strdup(text); + ti->rgba = rgba; + ti->size = size; + + return (PyObject*)tag; +} +static tag_internals_t text_tag = +{ + parse: 0, + fillTAG: text_fillTAG, + dealloc: 0, + tagfunctions: 0, + datasize: sizeof(text_internal_t), +}; +//---------------------------------------------------------------------------- + +typedef struct _videostream_internal +{ + VIDEOSTREAM* stream; +} videostream_internal_t; +staticforward tag_internals_t videostream_tag; + +static int videostream_parse(tag_internals_t*self) +{ + videostream_internal_t*videostream = (videostream_internal_t*)self->data; + /* TODO */ + PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet")); + return 0; +} +static void videostream_dealloc(tag_internals_t*self) +{ + videostream_internal_t*videostream = (videostream_internal_t*)self->data; + if(videostream->stream) { + swf_VideoStreamClear(videostream->stream); + free(videostream->stream); + videostream->stream = 0; + } +} +static int videostream_fillTAG(tag_internals_t*self) +{ + videostream_internal_t*fi = (videostream_internal_t*)self->data; + if(self->tag) + return 1; + /* TODO */ + PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented yet"));return 0; + return 1; +} +static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs) +{ + PyObject*tag = tag_new(&videostream_tag); + + int width=0,height=0,quant=7,frames=65535; + static char *kwlist[] = {"width", "height", "quant", "frames", NULL}; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|ii", kwlist, &TagClass, &width, &height, &quant, &frames)) + return NULL; + + tag_internals_t*itag = tag_getinternals(tag); + videostream_internal_t*fi = (videostream_internal_t*)itag->data; + fi->stream = malloc(sizeof(VIDEOSTREAM)); + memset(fi->stream, 0, sizeof(VIDEOSTREAM)); + + TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM); + swf_SetVideoStreamDefine(t, fi->stream, frames, width, height); + return (PyObject*)tag; +} +static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self) +{ + PY_ASSERT_TYPE(self, &TagClass); + tag_internals_t*itag = tag_getinternals(self); + videostream_internal_t*fi = (videostream_internal_t*)itag->data; + return fi->stream; +} +static PyObject* videostream_getbwidth(PyObject*self, PyObject*args) +{ + tag_internals_t*itag = tag_getinternals(self); + videostream_internal_t*fi = (videostream_internal_t*)itag->data; + int width = fi->stream->bbx; + return Py_BuildValue("i", width); +} +static PyObject* videostream_getbheight(PyObject*self, PyObject*args) +{ + tag_internals_t*itag = tag_getinternals(self); + videostream_internal_t*fi = (videostream_internal_t*)itag->data; + int height = fi->stream->bby; + return Py_BuildValue("i", height); +} +static PyObject* videostream_addIFrame(PyObject*self, PyObject*args) +{ + tag_internals_t*itag = tag_getinternals(self); + videostream_internal_t*fi = (videostream_internal_t*)itag->data; + /* TODO */ + return Py_BuildValue("s", 0); +} +static PyObject* videostream_addPFrame(PyObject*self, PyObject*args) +{ + tag_internals_t*itag = tag_getinternals(self); + videostream_internal_t*fi = (videostream_internal_t*)itag->data; + /* TODO */ + return Py_BuildValue("s", 0); +} +static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args) +{ + tag_internals_t*itag = tag_getinternals(self); + videostream_internal_t*fi = (videostream_internal_t*)itag->data; + /* TODO */ + return Py_BuildValue("s", 0); +} +static PyMethodDef videostream_methods[] = +{{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"}, + {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"}, + {"addIFrame", videostream_addIFrame, METH_VARARGS, "add a I Video Frame"}, + {"addPFrame", videostream_addPFrame, METH_VARARGS, "add a P Video Frame"}, + {"addDistortionFrame", videostream_addDistortionFrame, METH_VARARGS, "add a MVD frame"}, + {NULL, NULL, 0, NULL} +}; + +static tag_internals_t videostream_tag = +{ + parse: videostream_parse, + fillTAG: videostream_fillTAG, + dealloc: videostream_dealloc, + tagfunctions: videostream_methods, + datasize: sizeof(videostream_internal_t), +}; + +//============================================================================ + +static PyMethodDef TagMethods[] = +{ + /* TAGS */ + {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."}, + {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."}, + {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."}, + {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."}, + {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."}, + {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."}, + {NULL, NULL, 0, NULL} +}; +PyMethodDef* tags_getMethods() +{ + TagClass.ob_type = &PyType_Type; + + register_tag(ST_PLACEOBJECT,&placeobject_tag); + register_tag(ST_PLACEOBJECT2,&placeobject_tag); + register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag); + register_tag(ST_DEFINEFONT,&font_tag); + register_tag(ST_PROTECT,&protect_tag); + register_tag(ST_DEFINETEXT,&text_tag); + register_tag(ST_END,&end_tag); + + return TagMethods; +} + diff --git a/lib/python/tags.h b/lib/python/tags.h new file mode 100644 index 0000000..da6a1eb --- /dev/null +++ b/lib/python/tags.h @@ -0,0 +1,32 @@ +/* tags.h + + Python wrapper for librfxswf- various tag implementations + + Part of the swftools package. + + Copyright (c) 2003 Matthias Kramm + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#ifndef __tags_h__ +#define __tags_h__ + +#include "../rfxswf.h" +#undef HAVE_STAT +#include + +PyMethodDef* tags_getMethods(); + +#endif