2 #include "primitives.h"
10 //----------------------------------------------------------------------------
12 typedef struct _font_internal
16 staticforward tag_internals_t font_tag;
18 static int font_parse(tag_internals_t*self)
20 font_internal_t*font = (font_internal_t*)self->data;
22 PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet"));
25 static void font_dealloc(tag_internals_t*self)
27 font_internal_t*font = (font_internal_t*)self->data;
29 swf_FontFree(font->font);
33 static int font_fillTAG(tag_internals_t*self)
35 font_internal_t*fi = (font_internal_t*)self->data;
38 self->tag = swf_InsertTag(0, ST_DEFINEFONT2);
39 swf_FontSetDefine2(self->tag, fi->font);
42 static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs)
44 static char *kwlist[] = {"filename", NULL};
49 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &filename))
52 font = swf_LoadFont(filename);
54 PyErr_SetString(PyExc_Exception, setError("Could not load %s", filename));
58 tag = tag_new(&font_tag);
59 tag_internals_t*itag = tag_getinternals(tag);
60 font_internal_t*fi = (font_internal_t*)itag->data;
63 return (PyObject*)tag;
65 static SWFFONT* font_getSWFFONT(PyObject*self)
67 PY_ASSERT_TYPE(self, &TagClass);
68 tag_internals_t*itag = tag_getinternals(self);
69 font_internal_t*fi = (font_internal_t*)itag->data;
72 static tag_internals_t font_tag =
75 fillTAG: font_fillTAG,
76 dealloc: font_dealloc,
80 datasize: sizeof(font_internal_t),
82 //----------------------------------------------------------------------------
84 typedef struct _placeobject_internal
88 } placeobject_internal_t;
89 staticforward tag_internals_t placeobject_tag;
91 static void po_dealloc(tag_internals_t*self)
93 placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
95 swf_PlaceObjectFree(pi->po);
99 static int po_parse(tag_internals_t*self)
101 placeobject_internal_t*i = (placeobject_internal_t*)self->data;
106 SWFPLACEOBJECT* swfpo = malloc(sizeof(SWFPLACEOBJECT));
107 swf_GetPlaceObject(self->tag, swfpo);
109 swf_DeleteTag(self->tag);self->tag = 0;
112 i->character = tagmap_id2obj(self->tagmap, i->po->id);
114 Py_INCREF(i->character);
121 static int po_fillTAG(tag_internals_t*self)
123 placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
124 self->tag = swf_InsertTag(0, ST_PLACEOBJECT2);
125 swf_SetPlaceObject(self->tag, pi->po);
128 static PyObject* po_getattr(tag_internals_t*self,char*a)
130 placeobject_internal_t*i = (placeobject_internal_t*)self->data;
132 return PY_ERROR("Couldn't parse placeobject");
133 if(!strcmp(a, "character")) {
136 Py_INCREF(i->character); //TODO: ??
138 } else if(!strcmp(a, "matrix")) {
139 return f_Matrix2(&i->po->matrix);
140 } else if(!strcmp(a, "cxform")) {
146 static int po_setattr(tag_internals_t*self,char*a, PyObject*obj)
148 placeobject_internal_t*si = (placeobject_internal_t*)self->data;
149 if(!strcmp(a, "cxform")) {
155 static PyObject* po_create(PyObject* self, PyObject* args, PyObject* kwargs,char move)
157 static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
159 PyObject*character = 0;
168 po = malloc(sizeof(SWFPLACEOBJECT));
169 memset(po, 0, sizeof(SWFPLACEOBJECT));
171 swf_GetPlaceObject(0, po);
174 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|O!O!isiO!", kwlist,
177 &MatrixClass, &matrix,
178 &CXFormClass, &cxform,
182 &ActionClass, &action
187 po->clipdepth = clipdepth;
191 if(clipdepth) po->clipdepth = clipdepth;
192 if(matrix) po->matrix = matrix_getMatrix(matrix);
193 if(cxform) po->cxform = colortransform_getCXForm(cxform);
194 if(action) po->actions = action_getAction(action);
197 tag = tag_new(&placeobject_tag);
198 tag_internals_t*itag = tag_getinternals(tag);
199 placeobject_internal_t*pi = (placeobject_internal_t*)itag->data;
202 pi->po->id = tagmap_add(itag->tagmap,(PyObject*)character);
207 mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt);
209 return (PyObject*)tag;
211 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
213 return po_create(self, args, kwargs, 0);
215 static PyObject* f_MoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
217 return po_create(self, args, kwargs, 1);
219 static tag_internals_t placeobject_tag =
227 datasize: sizeof(placeobject_internal_t),
229 //----------------------------------------------------------------------------
230 staticforward tag_internals_t bgcolor_tag;
231 static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other)
233 tag_internals_t*itag = tag_getinternals(self);
235 r = itag->tag->data[0];
236 g = itag->tag->data[1];
237 b = itag->tag->data[2];
238 return Py_BuildValue("(iii)", r,g,b);
240 static PyMethodDef setbgcolor_methods[] =
241 {{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"},
242 {NULL, NULL, 0, NULL}
244 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
246 static char *kwlist[] = {"color", NULL};
251 tag = tag_new(&bgcolor_tag);
252 tag_internals_t*itag = tag_getinternals(tag);
254 /* 1st try- copy constructor */
255 if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
257 /* 2nd try- color's contructor */
258 color = f_Color(NULL, args, kwargs);
263 itag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
264 RGBA rgba = color_getRGBA(color);
265 swf_SetU8(itag->tag, rgba.r);
266 swf_SetU8(itag->tag, rgba.g);
267 swf_SetU8(itag->tag, rgba.b);
268 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);
270 return (PyObject*)tag;
272 static tag_internals_t bgcolor_tag =
279 tagfunctions: setbgcolor_methods,
282 //----------------------------------------------------------------------------
283 staticforward tag_internals_t protect_tag;
284 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
286 static char *kwlist[] = {"password", NULL};
289 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
292 PyObject*tag = tag_new(&protect_tag);
293 tag_internals_t*itag = tag_getinternals(tag);
294 itag->tag = swf_InsertTag(0, ST_PROTECT);
296 swf_SetPassword(itag->tag, password);
298 mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt);
299 return (PyObject*)tag;
301 static tag_internals_t protect_tag =
311 //----------------------------------------------------------------------------
312 staticforward tag_internals_t showframe_tag;
313 static PyObject* f_ShowFrame(PyObject* self, PyObject* args, PyObject* kwargs)
315 static char *kwlist[] = {"name", NULL};
317 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
320 PyObject*tag = tag_new(&showframe_tag);
321 tag_internals_t*itag = tag_getinternals(tag);
322 itag->tag = swf_InsertTag(0, ST_SHOWFRAME);
323 mylog("+%08x(%d) f_ShowFrame", (int)tag, tag->ob_refcnt);
324 return (PyObject*)tag;
326 static tag_internals_t showframe_tag =
336 //----------------------------------------------------------------------------
337 staticforward tag_internals_t removeobject_tag;
338 static PyObject* f_RemoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
340 static char *kwlist[] = {"depth", NULL};
342 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &depth))
345 PyObject*tag = tag_new(&removeobject_tag);
346 tag_internals_t*itag = tag_getinternals(tag);
347 itag->tag = swf_InsertTag(0, ST_REMOVEOBJECT);
348 swf_SetU16(itag->tag, depth);
349 mylog("+%08x(%d) f_RemoveObject", (int)tag, tag->ob_refcnt);
350 return (PyObject*)tag;
352 static tag_internals_t removeobject_tag =
362 //----------------------------------------------------------------------------
363 staticforward tag_internals_t sprite_tag;
364 typedef struct _sprite_internal
369 static int sprite_fillTAG(tag_internals_t*self)
371 mylog("+%08x(?) sprite_fillTAG", (int)self);
373 sprite_internal_t*si = (sprite_internal_t*)self->data;
375 TAG*sprite = swf_InsertTag(0, ST_DEFINESPRITE);
376 swf_SetU16(sprite, 0); //id
377 swf_SetU16(sprite, 0); //frames
379 TAG*tag = taglist_getTAGs2(si->taglist, self->tagmap, 0);
381 /* pass through exception */
385 while(tag2->next) tag2 = tag2->next;
386 swf_InsertTag(tag2, ST_END);
391 swf_FoldSprite(sprite);
396 static PyObject* f_Sprite(PyObject* self, PyObject* args, PyObject* kwargs)
398 static char *kwlist[] = {"name", NULL};
400 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
403 PyObject*tag = tag_new(&sprite_tag);
404 tag_internals_t*itag = tag_getinternals(tag);
405 sprite_internal_t*si = (sprite_internal_t*)itag->data;
406 si->taglist = taglist_new();
407 mylog("+%08x(%d) f_DefineSprite", (int)tag, tag->ob_refcnt);
408 return (PyObject*)tag;
410 static PyObject* sprite_getattr(tag_internals_t*self,char*a)
412 sprite_internal_t*si = (sprite_internal_t*)self->data;
413 if(!strcmp(a, "tags")) {
414 Py_INCREF(si->taglist);
419 static int sprite_setattr(tag_internals_t*self,char*a, PyObject*obj)
421 sprite_internal_t*si = (sprite_internal_t*)self->data;
423 swf_DeleteTag(self->tag);
426 if(!strcmp(a, "tags")) {
427 PY_ASSERT_TYPE(obj,&TagListClass);
428 Py_DECREF(si->taglist);
430 Py_INCREF(si->taglist);
435 static tag_internals_t sprite_tag =
438 fillTAG: sprite_fillTAG,
440 getattr: sprite_getattr,
441 setattr: sprite_setattr,
443 datasize: sizeof(sprite_internal_t),
445 //----------------------------------------------------------------------------
446 staticforward tag_internals_t end_tag;
447 static tag_internals_t end_tag =
457 //----------------------------------------------------------------------------
458 staticforward tag_internals_t text_tag;
460 typedef struct _text_internal
468 staticforward tag_internals_t placeobject_tag;
470 static int text_fillTAG(tag_internals_t*self)
472 text_internal_t*ti = (text_internal_t*)self->data;
473 self->tag= swf_InsertTag(0, ST_DEFINETEXT2);
474 swf_SetU16(self->tag, /*ID*/0);
475 ti->bbox = swf_SetDefineText(self->tag, ti->swffont, &ti->rgba, ti->text, ti->size);
478 static PyObject* text_getattr(tag_internals_t*self,char*a)
480 text_internal_t*si = (text_internal_t*)self->data;
481 if(!strcmp(a, "bbox")) {
482 return f_BBox2(si->bbox);
486 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
488 static char *kwlist[] = {"font", "text", "size", "color", NULL};
493 RGBA rgba = {255,0,0,0};
497 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!Oi|O!", kwlist, &TagClass, &font, &otext, &size, &ColorClass, &color))
499 if(PyUnicode_Check(otext)) {
500 text = PyString_AS_STRING(PyUnicode_AsUTF8String(otext));
501 } else if(PyString_Check(otext)) {
502 text = PyString_AS_STRING(otext);
506 rgba = color_getRGBA(color);
508 mylog("DefineText: text = %s", text);
510 tag = tag_new(&text_tag);
511 tag_internals_t* itag = tag_getinternals(tag);
512 text_internal_t*ti = (text_internal_t*)itag->data;
514 ti->swffont = font_getSWFFONT(font);
515 int font_id = tagmap_add(itag->tagmap, font); // add dependency on font
516 ti->swffont->id = font_id; // for swf_SetDefineTexts
517 ti->text = strdup(text);
521 return (PyObject*)tag;
523 static tag_internals_t text_tag =
526 fillTAG: text_fillTAG,
528 getattr: text_getattr,
531 datasize: sizeof(text_internal_t),
533 //----------------------------------------------------------------------------
534 staticforward tag_internals_t image_tag;
536 typedef struct _image_internal
546 staticforward tag_internals_t image_tag;
548 static int image_fillTAG(tag_internals_t*self)
550 image_internal_t*ti = (image_internal_t*)self->data;
551 self->tag= swf_InsertTag(0, ST_DEFINEBITSLOSSLESS2);
552 swf_SetU16(self->tag, /*ID*/0);
553 swf_SetLosslessBits(self->tag, ti->width, ti->height, ti->rgba, BMF_32BIT);
556 static void image_dealloc(tag_internals_t*self)
558 image_internal_t*pi = (image_internal_t*)self->data;
560 free(pi->rgba);pi->rgba = 0;
563 static int image_parse(tag_internals_t*self)
565 image_internal_t*i= (image_internal_t*)self->data;
571 i->rgba = swf_ExtractImage(self->tag, &i->width, &i->height);
576 swf_DeleteTag(self->tag);self->tag = 0;
579 static int imagetag_getWidth(PyObject* self)
581 tag_internals_t*itag = tag_getinternals(self);
582 image_internal_t*pi = (image_internal_t*)itag->data;
585 static int imagetag_getHeight(PyObject* self)
587 tag_internals_t*itag = tag_getinternals(self);
588 image_internal_t*pi = (image_internal_t*)itag->data;
591 static PyObject* f_DefineImage(PyObject* self, PyObject* args, PyObject* kwargs)
593 static char *kwlist[] = {"image", NULL};
595 PyObject*tag = tag_new(&image_tag);
597 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &image))
600 tag = tag_new(&image_tag);
601 tag_internals_t* itag = tag_getinternals(tag);
602 image_internal_t*ti = (image_internal_t*)itag->data;
604 ti->rgba = image_toRGBA(image);
605 if(!ti->rgba) // pass through exception
607 ti->width = image_getWidth(image);
608 ti->height = image_getHeight(image);
612 ti->size = ti->width*ti->height;
614 return (PyObject*)tag;
616 static PyObject* image_getattr(tag_internals_t*self,char*a)
618 image_internal_t*i = (image_internal_t*)self->data;
619 if(!strcmp(a, "image")) {
623 PyObject* image = rgba_to_image(i->rgba, i->width, i->height);
629 static PyObject* image_save(PyObject*self, PyObject*args)
631 tag_internals_t*itag = tag_getinternals(self);
632 if(!image_parse(itag))
633 return PY_ERROR("Couldn't parse image");
634 image_internal_t*fi = (image_internal_t*)itag->data;
637 if(!PyArg_ParseTuple(args, "s", &filename))
640 writePNG(filename, fi->rgba ,fi->width, fi->height);
645 static PyMethodDef image_methods[] =
646 {{"save", image_save, METH_VARARGS, "saves an image as PNG"},
647 {NULL, NULL, 0, NULL}
650 static tag_internals_t image_tag =
653 fillTAG: image_fillTAG,
654 dealloc: image_dealloc,
655 getattr: image_getattr,
657 tagfunctions: image_methods,
658 datasize: sizeof(image_internal_t),
660 //----------------------------------------------------------------------------
661 staticforward tag_internals_t shape_tag;
663 typedef struct _shape_internal
667 staticforward tag_internals_t shape_tag;
669 static int shape_fillTAG(tag_internals_t*self)
671 shape_internal_t*ti = (shape_internal_t*)self->data;
672 self->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
673 swf_SetU16(self->tag, /*ID*/0);
674 swf_SetShape2(self->tag, ti->shape2);
677 static int shape_parse(tag_internals_t*self)
679 shape_internal_t*i= (shape_internal_t*)self->data;
684 SHAPE2* shape2 = malloc(sizeof(SHAPE2));
685 swf_ParseDefineShape(self->tag, shape2);
687 swf_DeleteTag(self->tag);self->tag = 0;
690 static void shape_dealloc(tag_internals_t*self)
692 shape_internal_t*pi = (shape_internal_t*)self->data;
694 swf_Shape2Free(pi->shape2);
698 static PyObject* f_DefineImageShape(PyObject* self, PyObject* args, PyObject* kwargs)
700 static char *kwlist[] = {"image", NULL};
703 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &TagClass, &image))
706 PyObject*tag = tag_new(&shape_tag);
707 tag_internals_t* itag = tag_getinternals(tag);
708 shape_internal_t*ti = (shape_internal_t*)itag->data;
709 ti->shape2 = 0; /*HACK*/
711 int width = imagetag_getWidth(image);
712 int height = imagetag_getHeight(image);
713 int id = tagmap_add(itag->tagmap, image);
714 itag->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
715 swf_SetU16(itag->tag, 0);
716 swf_ShapeSetBitmapRect(itag->tag, id, width, height);
717 return (PyObject*)tag;
720 /* TODO: move to lib/ */
721 SHAPE2*swf_StringToShape2(char*s,FILLSTYLE*f, LINESTYLE*l)
724 swf_Shape11DrawerInit(&draw, 0);
725 draw_string(&draw, s);
727 SHAPE*s1 = swf_ShapeDrawerToShape(&draw);
728 SRECT r = swf_ShapeDrawerGetBBox(&draw);
729 RGBA col;col.r=col.g=col.b=128;col.a=255;
731 swf_ShapeAddLineStyle(s1, 1, &col);
733 swf_ShapeAddSolidFillStyle(s1, &col);
735 SHAPE2*shape2 = swf_ShapeToShape2(s1);
737 shape2->bbox = malloc(sizeof(SRECT));
739 if(f && shape2->numfillstyles)
740 shape2->fillstyles[0] = *f;
741 if(l && shape2->numlinestyles)
742 shape2->linestyles[0] = *l;
746 static PyObject* f_DefineShape(PyObject* self, PyObject* args, PyObject* kwargs)
748 static char *kwlist[] = {"s", "fill", "line", NULL};
750 PyObject*fillstyle=0,*linestyle=0;
752 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OO", kwlist, &s,&fillstyle,&linestyle))
755 PyObject*tag = tag_new(&shape_tag);
756 tag_internals_t* itag = tag_getinternals(tag);
757 shape_internal_t*ti = (shape_internal_t*)itag->data;
764 if(PY_CHECK_TYPE(fillstyle, &ColorClass)) {
765 f->type = /*FILL_SOLID*/ 0;
766 f->color = color_getRGBA(fillstyle);
768 return PY_ERROR("Invalid Fillstyle");
774 if(PyTuple_Check(linestyle) && PyTuple_GET_SIZE(linestyle)==2) {
777 if(!PyArg_ParseTuple(linestyle, "fO!", &f, &ColorClass, &color))
780 l->width = (int)(f*20);
781 l->color = color_getRGBA(color);
783 return PY_ERROR("Invalid Linestyle");
786 ti->shape2 = swf_StringToShape2(s,f,l);
790 return (PyObject*)tag;
792 static PyObject* shape_getfillstyles(PyObject*self, PyObject*args)
794 tag_internals_t*itag = tag_getinternals(self);
795 if(!shape_parse(itag))
796 return PY_ERROR("Couldn't parse shape");
797 shape_internal_t*fi = (shape_internal_t*)itag->data;
798 int num = fi->shape2->numfillstyles;
799 return Py_BuildValue("i", num);
801 static PyObject* shape_getlinestyles(PyObject*self, PyObject*args)
803 tag_internals_t*itag = tag_getinternals(self);
804 if(!shape_parse(itag))
805 return PY_ERROR("Couldn't parse shape");
806 shape_internal_t*fi = (shape_internal_t*)itag->data;
807 int num = fi->shape2->numlinestyles;
808 return Py_BuildValue("i", num);
810 static PyObject* shape_getfillstyle(PyObject*self, PyObject*args)
812 tag_internals_t*itag = tag_getinternals(self);
813 if(!shape_parse(itag))
814 return PY_ERROR("Couldn't parse shape");
815 shape_internal_t*fi = (shape_internal_t*)itag->data;
817 if(!PyArg_ParseTuple(args, "i", &nr))
820 int num = fi->shape2->numfillstyles;
821 if(nr < 0 || nr >=num)
822 return PY_ERROR("fillstyle index out of range");
823 return f_FillStyle2(fi->shape2->fillstyles[nr]);
825 static PyObject* shape_getlinestyle(PyObject*self, PyObject*args)
827 tag_internals_t*itag = tag_getinternals(self);
828 if(!shape_parse(itag))
829 return PY_ERROR("Couldn't parse shape");
830 shape_internal_t*fi = (shape_internal_t*)itag->data;
832 if(!PyArg_ParseTuple(args, "i", &nr))
835 int num = fi->shape2->numfillstyles;
836 if(nr < 0 || nr >=num)
837 return PY_ERROR("fillstyle index out of range");
838 return f_LineStyle3(fi->shape2->linestyles[nr]);
840 static PyObject* shape_setfillstyle(PyObject*self, PyObject*args)
842 tag_internals_t*itag = tag_getinternals(self);
843 if(!shape_parse(itag))
844 return PY_ERROR("Couldn't parse shape");
845 shape_internal_t*fi = (shape_internal_t*)itag->data;
848 if(!PyArg_ParseTuple(args, "iO!", &nr, &FillStyleClass, &fs))
851 int num = fi->shape2->numfillstyles;
852 if(nr < 0 || nr >=num)
853 return PY_ERROR("fillstyle index out of range");
854 fi->shape2->fillstyles[nr] = fillstyle_getFillStyle(fs);
857 static PyObject* shape_setlinestyle(PyObject*self, PyObject*args)
859 tag_internals_t*itag = tag_getinternals(self);
860 if(!shape_parse(itag))
861 return PY_ERROR("Couldn't parse shape");
862 shape_internal_t*fi = (shape_internal_t*)itag->data;
865 if(!PyArg_ParseTuple(args, "iO!", &nr, &LineStyleClass, &ls))
868 int num = fi->shape2->numlinestyles;
869 if(nr < 0 || nr >=num)
870 return PY_ERROR("linestyle index out of range");
871 fi->shape2->linestyles[nr] = linestyle_getLineStyle(ls);
874 static PyMethodDef shape_methods[] =
875 {{"numfillstyles", shape_getfillstyles, METH_VARARGS, "get's the number of fillstyles"},
876 {"numlinestyles", shape_getlinestyles, METH_VARARGS, "get's the number of linestyles"},
877 {"getfillstyle", shape_getfillstyle, METH_VARARGS, "get's one fillstyle"},
878 {"getlinestyle", shape_getlinestyle, METH_VARARGS, "get's one linestyle"},
879 {"setfillstyle", shape_setfillstyle, METH_VARARGS, "set's one fillstyle"},
880 {"setlinestyle", shape_setlinestyle, METH_VARARGS, "set's one linestyle"},
881 {NULL, NULL, 0, NULL}
884 static tag_internals_t shape_tag =
887 fillTAG: shape_fillTAG,
888 dealloc: shape_dealloc,
891 tagfunctions: shape_methods,
892 datasize: sizeof(shape_internal_t),
894 //----------------------------------------------------------------------------
896 typedef struct _videostream_internal
900 } videostream_internal_t;
901 staticforward tag_internals_t videostream_tag;
902 staticforward tag_internals_t videoframe_tag;
904 static int videostream_parse(tag_internals_t*self)
906 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
908 PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet"));
911 static void videostream_dealloc(tag_internals_t*self)
913 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
914 if(videostream->stream) {
915 swf_VideoStreamClear(videostream->stream);
916 free(videostream->stream);
917 videostream->stream = 0;
920 static int videostream_fillTAG(tag_internals_t*self)
922 videostream_internal_t*fi = (videostream_internal_t*)self->data;
925 PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented"));
928 static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs)
930 PyObject*tag = tag_new(&videostream_tag);
932 int width=0,height=0,frames=65535;
933 static char *kwlist[] = {"width", "height", "frames", NULL};
934 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", kwlist, &width, &height, &frames))
937 tag_internals_t*itag = tag_getinternals(tag);
938 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
939 fi->stream = malloc(sizeof(VIDEOSTREAM));
940 memset(fi->stream, 0, sizeof(VIDEOSTREAM));
942 TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
943 swf_SetU16(t, 0); /* id */
944 swf_SetVideoStreamDefine(t, fi->stream, frames, width, height);
946 fi->lastiframe = -65536;
947 return (PyObject*)tag;
949 static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self)
951 PY_ASSERT_TYPE(self, &TagClass);
952 tag_internals_t*itag = tag_getinternals(self);
953 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
956 static PyObject* videostream_getbwidth(PyObject*self, PyObject*args)
958 tag_internals_t*itag = tag_getinternals(self);
959 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
960 int width = fi->stream->bbx;
961 return Py_BuildValue("i", width);
963 static PyObject* videostream_getbheight(PyObject*self, PyObject*args)
965 tag_internals_t*itag = tag_getinternals(self);
966 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
967 int height = fi->stream->bby;
968 return Py_BuildValue("i", height);
970 static PyObject* videostream_addBlackFrame(PyObject*self, PyObject*args, PyObject*kwargs)
972 tag_internals_t*_itag = tag_getinternals(self);
973 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
975 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
977 PyObject*tag = tag_new(&videoframe_tag);
978 tag_internals_t*itag = tag_getinternals(tag);
980 swf_SetU16(t,0); /* id */
981 swf_SetVideoStreamBlackFrame(t, fi->stream);
982 fi->lastiframe = fi->stream->frame;
985 tagmap_addMapping(itag->tagmap, 0, self);
988 static PyObject* videostream_addFrame(PyObject*self, PyObject*args, PyObject*kwargs)
990 tag_internals_t*_itag = tag_getinternals(self);
991 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
994 char*type=0; // none, "i", "p"
996 static char *kwlist[] = {"image", "quant", "type", NULL};
997 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|is", kwlist, &image, &quant, &type))
999 if(fi->stream->owidth != image_getWidth(image)) {
1000 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getWidth(image), fi->stream->width));return 0;
1002 if(fi->stream->oheight != image_getHeight(image)) {
1003 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getHeight(image), fi->stream->height));return 0;
1005 PyObject*tag = tag_new(&videoframe_tag);
1006 tag_internals_t*itag = tag_getinternals(tag);
1008 RGBA*pic = image_toRGBA(image);
1012 /*{ int f,j=0,i=0,rr,gg,bb;
1015 char*filename="test.ppm";
1016 printf("Creating %s %dx%d\n",filename, 512,512);
1017 o=fopen(filename, "wb");
1018 fprintf(o,"P6\n%d %d\n255\n",512, 512);
1024 fprintf(o,"%c%c%c",rr,gg,bb);
1031 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
1032 if((type && (type[0]=='I' || type[0]=='i')) || (type==0 && fi->lastiframe+64 < fi->stream->frame)) {
1033 swf_SetU16(t,0); /* id */
1034 swf_SetVideoStreamIFrame(t, fi->stream, pic, quant);
1035 fi->lastiframe = fi->stream->frame;
1038 swf_SetVideoStreamPFrame(t, fi->stream, pic, quant);
1041 tagmap_addMapping(itag->tagmap, 0, self);
1045 static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args, PyObject*kwargs)
1047 tag_internals_t*_itag = tag_getinternals(self);
1048 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
1050 static char *kwlist[] = {"image", "quant", NULL};
1052 PyObject* array = 0;
1053 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", kwlist, &array, &quant))
1056 signed char* movex = malloc(fi->stream->bbx * fi->stream->bby * 1);
1057 signed char* movey = malloc(fi->stream->bbx * fi->stream->bby * 1);
1058 RGBA** pics = (RGBA**)malloc(fi->stream->bbx * fi->stream->bby * sizeof(void*));
1059 signed char* itx=movex;
1060 signed char* ity=movey;
1063 if(!array || !PySequence_Check(array))
1064 return PY_ERROR("Not an array");
1065 if(PySequence_Length(array) < fi->stream->bby)
1066 return PY_ERROR("Array (y) has to have at least %d elements, but has only %d ", fi->stream->bby, PySequence_Length(array));
1067 for(y=0;y<fi->stream->bby;y++) {
1068 PyObject*line = PySequence_GetItem(array, y);
1069 if(!line || !PySequence_Check(line))
1070 return PY_ERROR("Not an array of arrays");
1071 if(PySequence_Length(line) < fi->stream->bbx)
1072 return PY_ERROR("Inner arrays (x) have to be at least %d long- %dth is only %d", fi->stream->bbx, y, PySequence_Length(line));
1074 for(x=0;x<fi->stream->bbx;x++) {
1075 PyObject*pixel = PySequence_GetItem(line, x);
1082 if(PyComplex_Check(pixel)) {
1083 xy = pixel; image = 0;
1084 } else if(PyString_Check(pixel)) {
1085 xy = 0; image = pixel;
1086 } else if(PyTuple_Check(pixel)) {
1087 int size = PyTuple_GET_SIZE(pixel);
1088 if(size!=2) return PY_ERROR("Tuples have to have size 2 (xy,img)");
1089 xy = PyTuple_GetItem(pixel, 0);
1091 if(!PyComplex_Check(xy)) return PY_ERROR("Tuples must be (COMPLEX,string)");
1092 image = PyTuple_GetItem(pixel, 1);
1093 if(!image) return 0;
1094 if(!PyString_Check(image)) return PY_ERROR("Tuples must be (complex,STRING)");
1102 *itx = (signed char)PyComplex_RealAsDouble(pixel);
1103 *ity = (signed char)PyComplex_ImagAsDouble(pixel);
1108 PyString_AsStringAndSize(image,&string,&size);
1110 return PY_ERROR("image strings must be >= 256*3");
1112 *pic = malloc(sizeof(RGBA)*16*16);
1114 for(t=0;t<16*16;t++) {
1115 (*pic)[t].r = string[t*3];
1116 (*pic)[t].g = string[t*3+1];
1117 (*pic)[t].b = string[t*3+2];
1127 PyObject*tag = tag_new(&videoframe_tag);
1128 tag_internals_t*itag = tag_getinternals(tag);
1130 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
1131 swf_SetU16(t,0); /* id */
1132 swf_SetVideoStreamMover(t, fi->stream, movex, movey,(void**)pics, quant);
1135 tagmap_addMapping(itag->tagmap, 0, self);
1137 for(x=0;x<fi->stream->bbx*fi->stream->bby;x++) {
1139 free(pics[x]);pics[x] = 0;
1149 static PyMethodDef videostream_methods[] =
1150 {{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"},
1151 {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"},
1152 {"addFrame", (PyCFunction)videostream_addFrame, METH_KEYWORDS, "add a Video Frame"},
1153 {"addBlackFrame", (PyCFunction)videostream_addBlackFrame, METH_KEYWORDS, "add a black Video Frame"},
1154 {"addDistortionFrame", (PyCFunction)videostream_addDistortionFrame, METH_KEYWORDS, "add a MVD frame"},
1155 {NULL, NULL, 0, NULL}
1158 static tag_internals_t videostream_tag =
1160 parse: videostream_parse,
1161 fillTAG: videostream_fillTAG,
1162 dealloc: videostream_dealloc,
1165 tagfunctions: videostream_methods,
1166 datasize: sizeof(videostream_internal_t),
1169 //============================================================================
1171 static tag_internals_t videoframe_tag =
1182 //============================================================================
1183 static PyMethodDef TagMethods[] =
1186 {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
1187 {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
1188 {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
1189 {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
1190 {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
1191 {"RemoveObject", (PyCFunction)f_RemoveObject, METH_KEYWORDS, "Create a RemoveObject Tag."},
1192 {"MoveObject", (PyCFunction)f_MoveObject, METH_KEYWORDS, "Create a PlaceObject Move Tag."},
1193 {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."},
1194 {"Image", (PyCFunction)f_DefineImage, METH_KEYWORDS, "Create an SWF Image Tag."},
1195 {"ImageShape", (PyCFunction)f_DefineImageShape, METH_KEYWORDS, "Create an SWF Image Shape Tag."},
1196 {"Shape", (PyCFunction)f_DefineShape, METH_KEYWORDS, "Create an SWF Shape Tag."},
1197 {"ShowFrame", (PyCFunction)f_ShowFrame, METH_KEYWORDS, "Create an SWF Show Frame Tag."},
1198 {"Sprite", (PyCFunction)f_Sprite, METH_KEYWORDS, "Create an SWF Sprite Tag."},
1200 {NULL, NULL, 0, NULL}
1202 PyMethodDef* tags_getMethods()
1204 TagClass.ob_type = &PyType_Type;
1206 register_tag(ST_PLACEOBJECT,&placeobject_tag);
1207 register_tag(ST_PLACEOBJECT2,&placeobject_tag);
1208 register_tag(ST_REMOVEOBJECT,&removeobject_tag);
1209 register_tag(ST_REMOVEOBJECT2,&removeobject_tag);
1210 register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag);
1211 register_tag(ST_DEFINEFONT,&font_tag);
1212 register_tag(ST_PROTECT,&protect_tag);
1213 register_tag(ST_DEFINETEXT,&text_tag);
1214 register_tag(ST_DEFINEBITSJPEG,&image_tag);
1215 register_tag(ST_DEFINEBITSJPEG2,&image_tag);
1216 register_tag(ST_DEFINEBITSJPEG3,&image_tag);
1217 register_tag(ST_DEFINEBITSLOSSLESS,&image_tag);
1218 register_tag(ST_DEFINEBITSLOSSLESS2,&image_tag);
1219 register_tag(ST_DEFINESHAPE,&shape_tag);
1220 register_tag(ST_DEFINESHAPE2,&shape_tag);
1221 register_tag(ST_DEFINESHAPE3,&shape_tag);
1222 register_tag(ST_SHOWFRAME,&showframe_tag);
1223 register_tag(ST_DEFINEVIDEOSTREAM,&videostream_tag);
1224 register_tag(ST_VIDEOFRAME,&videoframe_tag);
1225 register_tag(ST_DEFINESPRITE,&sprite_tag);
1226 register_tag(ST_END,&end_tag);