2 #include "primitives.h"
9 //----------------------------------------------------------------------------
11 typedef struct _font_internal
15 staticforward tag_internals_t font_tag;
17 static int font_parse(tag_internals_t*self)
19 font_internal_t*font = (font_internal_t*)self->data;
21 PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet"));
24 static void font_dealloc(tag_internals_t*self)
26 font_internal_t*font = (font_internal_t*)self->data;
28 swf_FontFree(font->font);
32 static int font_fillTAG(tag_internals_t*self)
34 font_internal_t*fi = (font_internal_t*)self->data;
37 self->tag = swf_InsertTag(0, ST_DEFINEFONT2);
38 swf_FontSetDefine2(self->tag, fi->font);
41 static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs)
43 static char *kwlist[] = {"filename", NULL};
48 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &filename))
51 font = swf_LoadFont(filename);
53 PyErr_SetString(PyExc_Exception, setError("Could not load %s", filename));
57 tag = tag_new(&font_tag);
58 tag_internals_t*itag = tag_getinternals(tag);
59 font_internal_t*fi = (font_internal_t*)itag->data;
62 return (PyObject*)tag;
64 static SWFFONT* font_getSWFFONT(PyObject*self)
66 PY_ASSERT_TYPE(self, &TagClass);
67 tag_internals_t*itag = tag_getinternals(self);
68 font_internal_t*fi = (font_internal_t*)itag->data;
71 static tag_internals_t font_tag =
74 fillTAG: font_fillTAG,
75 dealloc: font_dealloc,
79 datasize: sizeof(font_internal_t),
81 //----------------------------------------------------------------------------
83 typedef struct _placeobject_internal
87 } placeobject_internal_t;
88 staticforward tag_internals_t placeobject_tag;
90 static void po_dealloc(tag_internals_t*self)
92 placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
94 swf_PlaceObjectFree(pi->po);
98 static int po_parse(tag_internals_t*self)
100 placeobject_internal_t*i = (placeobject_internal_t*)self->data;
105 SWFPLACEOBJECT* swfpo = malloc(sizeof(SWFPLACEOBJECT));
106 swf_GetPlaceObject(self->tag, swfpo);
108 swf_DeleteTag(self->tag);self->tag = 0;
111 i->character = tagmap_id2obj(self->tagmap, i->po->id);
113 Py_INCREF(i->character);
120 static int po_fillTAG(tag_internals_t*self)
122 placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
123 self->tag = swf_InsertTag(0, ST_PLACEOBJECT2);
124 swf_SetPlaceObject(self->tag, pi->po);
127 static PyObject* po_getattr(tag_internals_t*self,char*a)
129 placeobject_internal_t*i = (placeobject_internal_t*)self->data;
131 return PY_ERROR("Couldn't parse placeobject");
132 if(!strcmp(a, "character")) {
135 Py_INCREF(i->character); //TODO: ??
137 } else if(!strcmp(a, "matrix")) {
138 return f_Matrix2(&i->po->matrix);
139 } else if(!strcmp(a, "cxform")) {
145 static int po_setattr(tag_internals_t*self,char*a, PyObject*obj)
147 placeobject_internal_t*si = (placeobject_internal_t*)self->data;
148 if(!strcmp(a, "cxform")) {
154 static PyObject* po_create(PyObject* self, PyObject* args, PyObject* kwargs,char move)
156 static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
158 PyObject*character = 0;
167 po = malloc(sizeof(SWFPLACEOBJECT));
168 memset(po, 0, sizeof(SWFPLACEOBJECT));
170 swf_GetPlaceObject(0, po);
173 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|O!O!isiO!", kwlist,
176 &MatrixClass, &matrix,
177 &CXFormClass, &cxform,
181 &ActionClass, &action
186 po->clipdepth = clipdepth;
190 if(clipdepth) po->clipdepth = clipdepth;
191 if(matrix) po->matrix = matrix_getMatrix(matrix);
192 if(cxform) po->cxform = colortransform_getCXForm(cxform);
193 if(action) po->actions = action_getAction(action);
196 tag = tag_new(&placeobject_tag);
197 tag_internals_t*itag = tag_getinternals(tag);
198 placeobject_internal_t*pi = (placeobject_internal_t*)itag->data;
201 pi->po->id = tagmap_add(itag->tagmap,(PyObject*)character);
206 mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt);
208 return (PyObject*)tag;
210 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
212 return po_create(self, args, kwargs, 0);
214 static PyObject* f_MoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
216 return po_create(self, args, kwargs, 1);
218 static tag_internals_t placeobject_tag =
226 datasize: sizeof(placeobject_internal_t),
228 //----------------------------------------------------------------------------
229 staticforward tag_internals_t bgcolor_tag;
230 static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other)
232 tag_internals_t*itag = tag_getinternals(self);
234 r = itag->tag->data[0];
235 g = itag->tag->data[1];
236 b = itag->tag->data[2];
237 return Py_BuildValue("(iii)", r,g,b);
239 static PyMethodDef setbgcolor_methods[] =
240 {{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"},
241 {NULL, NULL, 0, NULL}
243 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
245 static char *kwlist[] = {"color", NULL};
250 tag = tag_new(&bgcolor_tag);
251 tag_internals_t*itag = tag_getinternals(tag);
253 /* 1st try- copy constructor */
254 if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
256 /* 2nd try- color's contructor */
257 color = f_Color(NULL, args, kwargs);
262 itag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
263 RGBA rgba = color_getRGBA(color);
264 swf_SetU8(itag->tag, rgba.r);
265 swf_SetU8(itag->tag, rgba.g);
266 swf_SetU8(itag->tag, rgba.b);
267 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);
269 return (PyObject*)tag;
271 static tag_internals_t bgcolor_tag =
278 tagfunctions: setbgcolor_methods,
281 //----------------------------------------------------------------------------
282 staticforward tag_internals_t protect_tag;
283 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
285 static char *kwlist[] = {"password", NULL};
288 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
291 PyObject*tag = tag_new(&protect_tag);
292 tag_internals_t*itag = tag_getinternals(tag);
293 itag->tag = swf_InsertTag(0, ST_PROTECT);
295 swf_SetPassword(itag->tag, password);
297 mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt);
298 return (PyObject*)tag;
300 static tag_internals_t protect_tag =
310 //----------------------------------------------------------------------------
311 staticforward tag_internals_t showframe_tag;
312 static PyObject* f_ShowFrame(PyObject* self, PyObject* args, PyObject* kwargs)
314 static char *kwlist[] = {"name", NULL};
316 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
319 PyObject*tag = tag_new(&showframe_tag);
320 tag_internals_t*itag = tag_getinternals(tag);
321 itag->tag = swf_InsertTag(0, ST_SHOWFRAME);
322 mylog("+%08x(%d) f_ShowFrame", (int)tag, tag->ob_refcnt);
323 return (PyObject*)tag;
325 static tag_internals_t showframe_tag =
335 //----------------------------------------------------------------------------
336 staticforward tag_internals_t removeobject_tag;
337 static PyObject* f_RemoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
339 static char *kwlist[] = {"depth", NULL};
341 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &depth))
344 PyObject*tag = tag_new(&removeobject_tag);
345 tag_internals_t*itag = tag_getinternals(tag);
346 itag->tag = swf_InsertTag(0, ST_REMOVEOBJECT);
347 swf_SetU16(itag->tag, depth);
348 mylog("+%08x(%d) f_RemoveObject", (int)tag, tag->ob_refcnt);
349 return (PyObject*)tag;
351 static tag_internals_t removeobject_tag =
361 //----------------------------------------------------------------------------
362 staticforward tag_internals_t sprite_tag;
363 typedef struct _sprite_internal
368 static int sprite_fillTAG(tag_internals_t*self)
370 mylog("+%08x(?) sprite_fillTAG", (int)self);
372 sprite_internal_t*si = (sprite_internal_t*)self->data;
374 TAG*sprite = swf_InsertTag(0, ST_DEFINESPRITE);
375 swf_SetU16(sprite, 0); //id
376 swf_SetU16(sprite, 0); //frames
378 TAG*tag = taglist_getTAGs2(si->taglist, self->tagmap, 0);
380 /* pass through exception */
384 while(tag2->next) tag2 = tag2->next;
385 swf_InsertTag(tag2, ST_END);
390 swf_FoldSprite(sprite);
395 static PyObject* f_Sprite(PyObject* self, PyObject* args, PyObject* kwargs)
397 static char *kwlist[] = {"name", NULL};
399 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
402 PyObject*tag = tag_new(&sprite_tag);
403 tag_internals_t*itag = tag_getinternals(tag);
404 sprite_internal_t*si = (sprite_internal_t*)itag->data;
405 si->taglist = taglist_new();
406 mylog("+%08x(%d) f_DefineSprite", (int)tag, tag->ob_refcnt);
407 return (PyObject*)tag;
409 static PyObject* sprite_getattr(tag_internals_t*self,char*a)
411 sprite_internal_t*si = (sprite_internal_t*)self->data;
412 if(!strcmp(a, "tags")) {
413 Py_INCREF(si->taglist);
418 static int sprite_setattr(tag_internals_t*self,char*a, PyObject*obj)
420 sprite_internal_t*si = (sprite_internal_t*)self->data;
422 swf_DeleteTag(self->tag);
425 if(!strcmp(a, "tags")) {
426 PY_ASSERT_TYPE(obj,&TagListClass);
427 Py_DECREF(si->taglist);
429 Py_INCREF(si->taglist);
434 static tag_internals_t sprite_tag =
437 fillTAG: sprite_fillTAG,
439 getattr: sprite_getattr,
440 setattr: sprite_setattr,
442 datasize: sizeof(sprite_internal_t),
444 //----------------------------------------------------------------------------
445 staticforward tag_internals_t end_tag;
446 static tag_internals_t end_tag =
456 //----------------------------------------------------------------------------
457 staticforward tag_internals_t text_tag;
459 typedef struct _text_internal
467 staticforward tag_internals_t placeobject_tag;
469 static int text_fillTAG(tag_internals_t*self)
471 text_internal_t*ti = (text_internal_t*)self->data;
472 self->tag= swf_InsertTag(0, ST_DEFINETEXT2);
473 swf_SetU16(self->tag, /*ID*/0);
474 ti->bbox = swf_SetDefineText(self->tag, ti->swffont, &ti->rgba, ti->text, ti->size);
477 static PyObject* text_getattr(tag_internals_t*self,char*a)
479 text_internal_t*si = (text_internal_t*)self->data;
480 if(!strcmp(a, "bbox")) {
481 return f_BBox2(si->bbox);
485 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
487 static char *kwlist[] = {"font", "text", "size", "color", NULL};
492 RGBA rgba = {255,0,0,0};
496 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!Oi|O!", kwlist, &TagClass, &font, &otext, &size, &ColorClass, &color))
498 if(PyUnicode_Check(otext)) {
499 text = PyString_AS_STRING(PyUnicode_AsUTF8String(otext));
500 } else if(PyString_Check(otext)) {
501 text = PyString_AS_STRING(otext);
505 rgba = color_getRGBA(color);
507 mylog("DefineText: text = %s", text);
509 tag = tag_new(&text_tag);
510 tag_internals_t* itag = tag_getinternals(tag);
511 text_internal_t*ti = (text_internal_t*)itag->data;
513 ti->swffont = font_getSWFFONT(font);
514 int font_id = tagmap_add(itag->tagmap, font); // add dependency on font
515 ti->swffont->id = font_id; // for swf_SetDefineTexts
516 ti->text = strdup(text);
520 return (PyObject*)tag;
522 static tag_internals_t text_tag =
525 fillTAG: text_fillTAG,
527 getattr: text_getattr,
530 datasize: sizeof(text_internal_t),
532 //----------------------------------------------------------------------------
533 staticforward tag_internals_t image_tag;
535 typedef struct _image_internal
545 staticforward tag_internals_t image_tag;
547 static int image_fillTAG(tag_internals_t*self)
549 image_internal_t*ti = (image_internal_t*)self->data;
550 self->tag= swf_InsertTag(0, ST_DEFINEBITSLOSSLESS2);
551 swf_SetU16(self->tag, /*ID*/0);
552 swf_SetLosslessBits(self->tag, ti->width, ti->height, ti->rgba, BMF_32BIT);
555 static void image_dealloc(tag_internals_t*self)
557 image_internal_t*pi = (image_internal_t*)self->data;
559 free(pi->rgba);pi->rgba = 0;
562 static int image_parse(tag_internals_t*self)
564 image_internal_t*i= (image_internal_t*)self->data;
570 i->rgba = swf_ExtractImage(self->tag, &i->width, &i->height);
575 swf_DeleteTag(self->tag);self->tag = 0;
578 static int imagetag_getWidth(PyObject* self)
580 tag_internals_t*itag = tag_getinternals(self);
581 image_internal_t*pi = (image_internal_t*)itag->data;
584 static int imagetag_getHeight(PyObject* self)
586 tag_internals_t*itag = tag_getinternals(self);
587 image_internal_t*pi = (image_internal_t*)itag->data;
590 static PyObject* f_DefineImage(PyObject* self, PyObject* args, PyObject* kwargs)
592 static char *kwlist[] = {"image", NULL};
594 PyObject*tag = tag_new(&image_tag);
596 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &image))
599 tag = tag_new(&image_tag);
600 tag_internals_t* itag = tag_getinternals(tag);
601 image_internal_t*ti = (image_internal_t*)itag->data;
603 ti->rgba = image_toRGBA(image);
604 if(!ti->rgba) // pass through exception
606 ti->width = image_getWidth(image);
607 ti->height = image_getHeight(image);
611 ti->size = ti->width*ti->height;
613 return (PyObject*)tag;
615 static PyObject* image_getattr(tag_internals_t*self,char*a)
617 image_internal_t*i = (image_internal_t*)self->data;
618 if(!strcmp(a, "image")) {
622 PyObject* image = rgba_to_image(i->rgba, i->width, i->height);
627 static tag_internals_t image_tag =
630 fillTAG: image_fillTAG,
631 dealloc: image_dealloc,
632 getattr: image_getattr,
635 datasize: sizeof(image_internal_t),
637 //----------------------------------------------------------------------------
638 staticforward tag_internals_t shape_tag;
640 typedef struct _shape_internal
644 staticforward tag_internals_t shape_tag;
646 static int shape_fillTAG(tag_internals_t*self)
648 shape_internal_t*ti = (shape_internal_t*)self->data;
649 self->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
650 swf_SetU16(self->tag, /*ID*/0);
651 swf_SetShape2(self->tag, ti->shape2);
654 static int shape_parse(tag_internals_t*self)
656 shape_internal_t*i= (shape_internal_t*)self->data;
661 SHAPE2* shape2 = malloc(sizeof(SHAPE2));
662 swf_ParseDefineShape(self->tag, shape2);
664 swf_DeleteTag(self->tag);self->tag = 0;
667 static void shape_dealloc(tag_internals_t*self)
669 shape_internal_t*pi = (shape_internal_t*)self->data;
671 swf_Shape2Free(pi->shape2);
675 static PyObject* f_DefineImageShape(PyObject* self, PyObject* args, PyObject* kwargs)
677 static char *kwlist[] = {"image", NULL};
680 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &TagClass, &image))
683 PyObject*tag = tag_new(&shape_tag);
684 tag_internals_t* itag = tag_getinternals(tag);
685 shape_internal_t*ti = (shape_internal_t*)itag->data;
686 ti->shape2 = 0; /*HACK*/
688 int width = imagetag_getWidth(image);
689 int height = imagetag_getHeight(image);
690 int id = tagmap_add(itag->tagmap, image);
691 itag->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
692 swf_SetU16(itag->tag, 0);
693 swf_ShapeSetBitmapRect(itag->tag, id, width, height);
694 return (PyObject*)tag;
697 /* TODO: move to lib/ */
698 SHAPE2*swf_StringToShape2(char*s,FILLSTYLE*f, LINESTYLE*l)
701 swf_Shape11DrawerInit(&draw, 0);
702 draw_string(&draw, s);
704 SHAPE*s1 = swf_ShapeDrawerToShape(&draw);
705 SRECT r = swf_ShapeDrawerGetBBox(&draw);
706 RGBA col;col.r=col.g=col.b=128;col.a=255;
708 swf_ShapeAddLineStyle(s1, 1, &col);
710 swf_ShapeAddSolidFillStyle(s1, &col);
712 SHAPE2*shape2 = swf_ShapeToShape2(s1);
714 shape2->bbox = malloc(sizeof(SRECT));
716 if(f && shape2->numfillstyles)
717 shape2->fillstyles[0] = *f;
718 if(l && shape2->numlinestyles)
719 shape2->linestyles[0] = *l;
723 static PyObject* f_DefineShape(PyObject* self, PyObject* args, PyObject* kwargs)
725 static char *kwlist[] = {"s", "fill", "line", NULL};
727 PyObject*fillstyle=0,*linestyle=0;
729 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OO", kwlist, &s,&fillstyle,&linestyle))
732 PyObject*tag = tag_new(&shape_tag);
733 tag_internals_t* itag = tag_getinternals(tag);
734 shape_internal_t*ti = (shape_internal_t*)itag->data;
741 if(PY_CHECK_TYPE(fillstyle, &ColorClass)) {
742 f->type = /*FILL_SOLID*/ 0;
743 f->color = color_getRGBA(fillstyle);
745 return PY_ERROR("Invalid Fillstyle");
751 if(PyTuple_Check(linestyle) && PyTuple_GET_SIZE(linestyle)==2) {
754 if(!PyArg_ParseTuple(linestyle, "fO!", &f, &ColorClass, &color))
757 l->width = (int)(f*20);
758 l->color = color_getRGBA(color);
760 return PY_ERROR("Invalid Linestyle");
763 ti->shape2 = swf_StringToShape2(s,f,l);
767 return (PyObject*)tag;
769 static PyObject* shape_getfillstyles(PyObject*self, PyObject*args)
771 tag_internals_t*itag = tag_getinternals(self);
772 if(!shape_parse(itag))
773 return PY_ERROR("Couldn't parse shape");
774 shape_internal_t*fi = (shape_internal_t*)itag->data;
775 int num = fi->shape2->numfillstyles;
776 return Py_BuildValue("i", num);
778 static PyObject* shape_getlinestyles(PyObject*self, PyObject*args)
780 tag_internals_t*itag = tag_getinternals(self);
781 if(!shape_parse(itag))
782 return PY_ERROR("Couldn't parse shape");
783 shape_internal_t*fi = (shape_internal_t*)itag->data;
784 int num = fi->shape2->numlinestyles;
785 return Py_BuildValue("i", num);
787 static PyObject* shape_getfillstyle(PyObject*self, PyObject*args)
789 tag_internals_t*itag = tag_getinternals(self);
790 if(!shape_parse(itag))
791 return PY_ERROR("Couldn't parse shape");
792 shape_internal_t*fi = (shape_internal_t*)itag->data;
794 if(!PyArg_ParseTuple(args, "i", &nr))
797 int num = fi->shape2->numfillstyles;
798 if(nr < 0 || nr >=num)
799 return PY_ERROR("fillstyle index out of range");
800 return f_FillStyle2(fi->shape2->fillstyles[nr]);
802 static PyObject* shape_getlinestyle(PyObject*self, PyObject*args)
804 tag_internals_t*itag = tag_getinternals(self);
805 if(!shape_parse(itag))
806 return PY_ERROR("Couldn't parse shape");
807 shape_internal_t*fi = (shape_internal_t*)itag->data;
809 if(!PyArg_ParseTuple(args, "i", &nr))
812 int num = fi->shape2->numfillstyles;
813 if(nr < 0 || nr >=num)
814 return PY_ERROR("fillstyle index out of range");
815 return f_LineStyle3(fi->shape2->linestyles[nr]);
817 static PyObject* shape_setfillstyle(PyObject*self, PyObject*args)
819 tag_internals_t*itag = tag_getinternals(self);
820 if(!shape_parse(itag))
821 return PY_ERROR("Couldn't parse shape");
822 shape_internal_t*fi = (shape_internal_t*)itag->data;
825 if(!PyArg_ParseTuple(args, "iO!", &nr, &FillStyleClass, &fs))
828 int num = fi->shape2->numfillstyles;
829 if(nr < 0 || nr >=num)
830 return PY_ERROR("fillstyle index out of range");
831 fi->shape2->fillstyles[nr] = fillstyle_getFillStyle(fs);
834 static PyObject* shape_setlinestyle(PyObject*self, PyObject*args)
836 tag_internals_t*itag = tag_getinternals(self);
837 if(!shape_parse(itag))
838 return PY_ERROR("Couldn't parse shape");
839 shape_internal_t*fi = (shape_internal_t*)itag->data;
842 if(!PyArg_ParseTuple(args, "iO!", &nr, &LineStyleClass, &ls))
845 int num = fi->shape2->numlinestyles;
846 if(nr < 0 || nr >=num)
847 return PY_ERROR("linestyle index out of range");
848 fi->shape2->linestyles[nr] = linestyle_getLineStyle(ls);
851 static PyMethodDef shape_methods[] =
852 {{"numfillstyles", shape_getfillstyles, METH_VARARGS, "get's the number of fillstyles"},
853 {"numlinestyles", shape_getlinestyles, METH_VARARGS, "get's the number of linestyles"},
854 {"getfillstyle", shape_getfillstyle, METH_VARARGS, "get's one fillstyle"},
855 {"getlinestyle", shape_getlinestyle, METH_VARARGS, "get's one linestyle"},
856 {"setfillstyle", shape_setfillstyle, METH_VARARGS, "set's one fillstyle"},
857 {"setlinestyle", shape_setlinestyle, METH_VARARGS, "set's one linestyle"},
858 {NULL, NULL, 0, NULL}
861 static tag_internals_t shape_tag =
864 fillTAG: shape_fillTAG,
865 dealloc: shape_dealloc,
868 tagfunctions: shape_methods,
869 datasize: sizeof(shape_internal_t),
871 //----------------------------------------------------------------------------
873 typedef struct _videostream_internal
877 } videostream_internal_t;
878 staticforward tag_internals_t videostream_tag;
879 staticforward tag_internals_t videoframe_tag;
881 static int videostream_parse(tag_internals_t*self)
883 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
885 PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet"));
888 static void videostream_dealloc(tag_internals_t*self)
890 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
891 if(videostream->stream) {
892 swf_VideoStreamClear(videostream->stream);
893 free(videostream->stream);
894 videostream->stream = 0;
897 static int videostream_fillTAG(tag_internals_t*self)
899 videostream_internal_t*fi = (videostream_internal_t*)self->data;
902 PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented"));
905 static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs)
907 PyObject*tag = tag_new(&videostream_tag);
909 int width=0,height=0,frames=65535;
910 static char *kwlist[] = {"width", "height", "frames", NULL};
911 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", kwlist, &width, &height, &frames))
914 tag_internals_t*itag = tag_getinternals(tag);
915 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
916 fi->stream = malloc(sizeof(VIDEOSTREAM));
917 memset(fi->stream, 0, sizeof(VIDEOSTREAM));
919 TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
920 swf_SetU16(t, 0); /* id */
921 swf_SetVideoStreamDefine(t, fi->stream, frames, width, height);
923 fi->lastiframe = -65536;
924 return (PyObject*)tag;
926 static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self)
928 PY_ASSERT_TYPE(self, &TagClass);
929 tag_internals_t*itag = tag_getinternals(self);
930 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
933 static PyObject* videostream_getbwidth(PyObject*self, PyObject*args)
935 tag_internals_t*itag = tag_getinternals(self);
936 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
937 int width = fi->stream->bbx;
938 return Py_BuildValue("i", width);
940 static PyObject* videostream_getbheight(PyObject*self, PyObject*args)
942 tag_internals_t*itag = tag_getinternals(self);
943 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
944 int height = fi->stream->bby;
945 return Py_BuildValue("i", height);
947 static PyObject* videostream_addBlackFrame(PyObject*self, PyObject*args, PyObject*kwargs)
949 tag_internals_t*_itag = tag_getinternals(self);
950 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
952 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
954 PyObject*tag = tag_new(&videoframe_tag);
955 tag_internals_t*itag = tag_getinternals(tag);
957 swf_SetU16(t,0); /* id */
958 swf_SetVideoStreamBlackFrame(t, fi->stream);
959 fi->lastiframe = fi->stream->frame;
962 tagmap_addMapping(itag->tagmap, 0, self);
965 static PyObject* videostream_addFrame(PyObject*self, PyObject*args, PyObject*kwargs)
967 tag_internals_t*_itag = tag_getinternals(self);
968 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
971 char*type=0; // none, "i", "p"
973 static char *kwlist[] = {"image", "quant", "type", NULL};
974 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|is", kwlist, &image, &quant, &type))
976 if(fi->stream->owidth != image_getWidth(image)) {
977 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getWidth(image), fi->stream->width));return 0;
979 if(fi->stream->oheight != image_getHeight(image)) {
980 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getHeight(image), fi->stream->height));return 0;
982 PyObject*tag = tag_new(&videoframe_tag);
983 tag_internals_t*itag = tag_getinternals(tag);
985 RGBA*pic = image_toRGBA(image);
989 /*{ int f,j=0,i=0,rr,gg,bb;
992 char*filename="test.ppm";
993 printf("Creating %s %dx%d\n",filename, 512,512);
994 o=fopen(filename, "wb");
995 fprintf(o,"P6\n%d %d\n255\n",512, 512);
1001 fprintf(o,"%c%c%c",rr,gg,bb);
1008 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
1009 if((type && (type[0]=='I' || type[0]=='i')) || (type==0 && fi->lastiframe+64 < fi->stream->frame)) {
1010 swf_SetU16(t,0); /* id */
1011 swf_SetVideoStreamIFrame(t, fi->stream, pic, quant);
1012 fi->lastiframe = fi->stream->frame;
1015 swf_SetVideoStreamPFrame(t, fi->stream, pic, quant);
1018 tagmap_addMapping(itag->tagmap, 0, self);
1022 static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args, PyObject*kwargs)
1024 tag_internals_t*_itag = tag_getinternals(self);
1025 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
1027 static char *kwlist[] = {"image", "quant", NULL};
1029 PyObject* array = 0;
1030 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", kwlist, &array, &quant))
1033 signed char* movex = malloc(fi->stream->bbx * fi->stream->bby * 1);
1034 signed char* movey = malloc(fi->stream->bbx * fi->stream->bby * 1);
1035 RGBA** pics = (RGBA**)malloc(fi->stream->bbx * fi->stream->bby * sizeof(void*));
1036 signed char* itx=movex;
1037 signed char* ity=movey;
1040 if(!array || !PySequence_Check(array))
1041 return PY_ERROR("Not an array");
1042 if(PySequence_Length(array) < fi->stream->bby)
1043 return PY_ERROR("Array (y) has to have at least %d elements, but has only %d ", fi->stream->bby, PySequence_Length(array));
1044 for(y=0;y<fi->stream->bby;y++) {
1045 PyObject*line = PySequence_GetItem(array, y);
1046 if(!line || !PySequence_Check(line))
1047 return PY_ERROR("Not an array of arrays");
1048 if(PySequence_Length(line) < fi->stream->bbx)
1049 return PY_ERROR("Inner arrays (x) have to be at least %d long- %dth is only %d", fi->stream->bbx, y, PySequence_Length(line));
1051 for(x=0;x<fi->stream->bbx;x++) {
1052 PyObject*pixel = PySequence_GetItem(line, x);
1059 if(PyComplex_Check(pixel)) {
1060 xy = pixel; image = 0;
1061 } else if(PyString_Check(pixel)) {
1062 xy = 0; image = pixel;
1063 } else if(PyTuple_Check(pixel)) {
1064 int size = PyTuple_GET_SIZE(pixel);
1065 if(size!=2) return PY_ERROR("Tuples have to have size 2 (xy,img)");
1066 xy = PyTuple_GetItem(pixel, 0);
1068 if(!PyComplex_Check(xy)) return PY_ERROR("Tuples must be (COMPLEX,string)");
1069 image = PyTuple_GetItem(pixel, 1);
1070 if(!image) return 0;
1071 if(!PyString_Check(image)) return PY_ERROR("Tuples must be (complex,STRING)");
1079 *itx = (signed char)PyComplex_RealAsDouble(pixel);
1080 *ity = (signed char)PyComplex_ImagAsDouble(pixel);
1085 PyString_AsStringAndSize(image,&string,&size);
1087 return PY_ERROR("image strings must be >= 256*3");
1089 *pic = malloc(sizeof(RGBA)*16*16);
1091 for(t=0;t<16*16;t++) {
1092 (*pic)[t].r = string[t*3];
1093 (*pic)[t].g = string[t*3+1];
1094 (*pic)[t].b = string[t*3+2];
1104 PyObject*tag = tag_new(&videoframe_tag);
1105 tag_internals_t*itag = tag_getinternals(tag);
1107 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
1108 swf_SetU16(t,0); /* id */
1109 swf_SetVideoStreamMover(t, fi->stream, movex, movey,(void**)pics, quant);
1112 tagmap_addMapping(itag->tagmap, 0, self);
1114 for(x=0;x<fi->stream->bbx*fi->stream->bby;x++) {
1116 free(pics[x]);pics[x] = 0;
1126 static PyMethodDef videostream_methods[] =
1127 {{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"},
1128 {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"},
1129 {"addFrame", (PyCFunction)videostream_addFrame, METH_KEYWORDS, "add a Video Frame"},
1130 {"addBlackFrame", (PyCFunction)videostream_addBlackFrame, METH_KEYWORDS, "add a black Video Frame"},
1131 {"addDistortionFrame", (PyCFunction)videostream_addDistortionFrame, METH_KEYWORDS, "add a MVD frame"},
1132 {NULL, NULL, 0, NULL}
1135 static tag_internals_t videostream_tag =
1137 parse: videostream_parse,
1138 fillTAG: videostream_fillTAG,
1139 dealloc: videostream_dealloc,
1142 tagfunctions: videostream_methods,
1143 datasize: sizeof(videostream_internal_t),
1146 //============================================================================
1148 static tag_internals_t videoframe_tag =
1159 //============================================================================
1160 static PyMethodDef TagMethods[] =
1163 {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
1164 {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
1165 {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
1166 {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
1167 {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
1168 {"RemoveObject", (PyCFunction)f_RemoveObject, METH_KEYWORDS, "Create a RemoveObject Tag."},
1169 {"MoveObject", (PyCFunction)f_MoveObject, METH_KEYWORDS, "Create a PlaceObject Move Tag."},
1170 {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."},
1171 {"Image", (PyCFunction)f_DefineImage, METH_KEYWORDS, "Create an SWF Image Tag."},
1172 {"ImageShape", (PyCFunction)f_DefineImageShape, METH_KEYWORDS, "Create an SWF Image Shape Tag."},
1173 {"Shape", (PyCFunction)f_DefineShape, METH_KEYWORDS, "Create an SWF Shape Tag."},
1174 {"ShowFrame", (PyCFunction)f_ShowFrame, METH_KEYWORDS, "Create an SWF Show Frame Tag."},
1175 {"Sprite", (PyCFunction)f_Sprite, METH_KEYWORDS, "Create an SWF Sprite Tag."},
1177 {NULL, NULL, 0, NULL}
1179 PyMethodDef* tags_getMethods()
1181 TagClass.ob_type = &PyType_Type;
1183 register_tag(ST_PLACEOBJECT,&placeobject_tag);
1184 register_tag(ST_PLACEOBJECT2,&placeobject_tag);
1185 register_tag(ST_REMOVEOBJECT,&removeobject_tag);
1186 register_tag(ST_REMOVEOBJECT2,&removeobject_tag);
1187 register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag);
1188 register_tag(ST_DEFINEFONT,&font_tag);
1189 register_tag(ST_PROTECT,&protect_tag);
1190 register_tag(ST_DEFINETEXT,&text_tag);
1191 register_tag(ST_DEFINEBITSJPEG,&image_tag);
1192 register_tag(ST_DEFINEBITSJPEG2,&image_tag);
1193 register_tag(ST_DEFINEBITSJPEG3,&image_tag);
1194 register_tag(ST_DEFINEBITSLOSSLESS,&image_tag);
1195 register_tag(ST_DEFINEBITSLOSSLESS2,&image_tag);
1196 register_tag(ST_DEFINESHAPE,&shape_tag);
1197 register_tag(ST_DEFINESHAPE2,&shape_tag);
1198 register_tag(ST_DEFINESHAPE3,&shape_tag);
1199 register_tag(ST_SHOWFRAME,&showframe_tag);
1200 register_tag(ST_DEFINEVIDEOSTREAM,&videostream_tag);
1201 register_tag(ST_VIDEOFRAME,&videoframe_tag);
1202 register_tag(ST_DEFINESPRITE,&sprite_tag);
1203 register_tag(ST_END,&end_tag);