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 imagetag_getWidth(PyObject* self)
564 tag_internals_t*itag = tag_getinternals(self);
565 image_internal_t*pi = (image_internal_t*)itag->data;
568 static int imagetag_getHeight(PyObject* self)
570 tag_internals_t*itag = tag_getinternals(self);
571 image_internal_t*pi = (image_internal_t*)itag->data;
574 static PyObject* f_DefineImage(PyObject* self, PyObject* args, PyObject* kwargs)
576 static char *kwlist[] = {"image", NULL};
578 PyObject*tag = tag_new(&image_tag);
580 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &image))
583 tag = tag_new(&image_tag);
584 tag_internals_t* itag = tag_getinternals(tag);
585 image_internal_t*ti = (image_internal_t*)itag->data;
587 ti->rgba = image_toRGBA(image);
588 if(!ti->rgba) // pass through exception
590 ti->width = image_getWidth(image);
591 ti->height = image_getHeight(image);
595 ti->size = ti->width*ti->height;
597 return (PyObject*)tag;
599 static tag_internals_t image_tag =
602 fillTAG: image_fillTAG,
603 dealloc: image_dealloc,
607 datasize: sizeof(image_internal_t),
609 //----------------------------------------------------------------------------
610 staticforward tag_internals_t shape_tag;
612 typedef struct _shape_internal
616 staticforward tag_internals_t shape_tag;
618 static int shape_fillTAG(tag_internals_t*self)
620 shape_internal_t*ti = (shape_internal_t*)self->data;
621 self->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
622 swf_SetU16(self->tag, /*ID*/0);
623 swf_SetShape2(self->tag, ti->shape2);
626 static int shape_parse(tag_internals_t*self)
628 shape_internal_t*i= (shape_internal_t*)self->data;
633 SHAPE2* shape2 = malloc(sizeof(SHAPE2));
634 swf_ParseDefineShape(self->tag, shape2);
636 swf_DeleteTag(self->tag);self->tag = 0;
639 static void shape_dealloc(tag_internals_t*self)
641 shape_internal_t*pi = (shape_internal_t*)self->data;
643 swf_Shape2Free(pi->shape2);
647 static PyObject* f_DefineImageShape(PyObject* self, PyObject* args, PyObject* kwargs)
649 static char *kwlist[] = {"image", NULL};
652 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &TagClass, &image))
655 PyObject*tag = tag_new(&shape_tag);
656 tag_internals_t* itag = tag_getinternals(tag);
657 shape_internal_t*ti = (shape_internal_t*)itag->data;
658 ti->shape2 = 0; /*HACK*/
660 int width = imagetag_getWidth(image);
661 int height = imagetag_getHeight(image);
662 int id = tagmap_add(itag->tagmap, image);
663 itag->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
664 swf_SetU16(itag->tag, 0);
665 swf_ShapeSetBitmapRect(itag->tag, id, width, height);
666 return (PyObject*)tag;
669 /* TODO: move to lib/ */
670 SHAPE2*swf_StringToShape2(char*s,FILLSTYLE*f, LINESTYLE*l)
673 swf_Shape11DrawerInit(&draw, 0);
674 draw_string(&draw, s);
676 SHAPE*s1 = swf_ShapeDrawerToShape(&draw);
677 SRECT r = swf_ShapeDrawerGetBBox(&draw);
678 RGBA col;col.r=col.g=col.b=128;col.a=255;
680 swf_ShapeAddLineStyle(s1, 1, &col);
682 swf_ShapeAddSolidFillStyle(s1, &col);
684 SHAPE2*shape2 = swf_ShapeToShape2(s1);
686 shape2->bbox = malloc(sizeof(SRECT));
688 if(f && shape2->numfillstyles)
689 shape2->fillstyles[0] = *f;
690 if(l && shape2->numlinestyles)
691 shape2->linestyles[0] = *l;
695 static PyObject* f_DefineShape(PyObject* self, PyObject* args, PyObject* kwargs)
697 static char *kwlist[] = {"s", "fill", "line", NULL};
699 PyObject*fillstyle=0,*linestyle=0;
701 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OO", kwlist, &s,&fillstyle,&linestyle))
704 PyObject*tag = tag_new(&shape_tag);
705 tag_internals_t* itag = tag_getinternals(tag);
706 shape_internal_t*ti = (shape_internal_t*)itag->data;
713 if(PY_CHECK_TYPE(fillstyle, &ColorClass)) {
714 f->type = /*FILL_SOLID*/ 0;
715 f->color = color_getRGBA(fillstyle);
717 return PY_ERROR("Invalid Fillstyle");
723 if(PyTuple_Check(linestyle) && PyTuple_GET_SIZE(linestyle)==2) {
726 if(!PyArg_ParseTuple(linestyle, "fO!", &f, &ColorClass, &color))
729 l->width = (int)(f*20);
730 l->color = color_getRGBA(color);
732 return PY_ERROR("Invalid Linestyle");
735 ti->shape2 = swf_StringToShape2(s,f,l);
739 return (PyObject*)tag;
741 static PyObject* shape_getfillstyles(PyObject*self, PyObject*args)
743 tag_internals_t*itag = tag_getinternals(self);
744 if(!shape_parse(itag))
745 return PY_ERROR("Couldn't parse shape");
746 shape_internal_t*fi = (shape_internal_t*)itag->data;
747 int num = fi->shape2->numfillstyles;
748 return Py_BuildValue("i", num);
750 static PyObject* shape_getlinestyles(PyObject*self, PyObject*args)
752 tag_internals_t*itag = tag_getinternals(self);
753 if(!shape_parse(itag))
754 return PY_ERROR("Couldn't parse shape");
755 shape_internal_t*fi = (shape_internal_t*)itag->data;
756 int num = fi->shape2->numlinestyles;
757 return Py_BuildValue("i", num);
759 static PyObject* shape_getfillstyle(PyObject*self, PyObject*args)
761 tag_internals_t*itag = tag_getinternals(self);
762 if(!shape_parse(itag))
763 return PY_ERROR("Couldn't parse shape");
764 shape_internal_t*fi = (shape_internal_t*)itag->data;
766 if(!PyArg_ParseTuple(args, "i", &nr))
769 int num = fi->shape2->numfillstyles;
770 if(nr < 0 || nr >=num)
771 return PY_ERROR("fillstyle index out of range");
772 return f_FillStyle2(fi->shape2->fillstyles[nr]);
774 static PyObject* shape_getlinestyle(PyObject*self, PyObject*args)
776 tag_internals_t*itag = tag_getinternals(self);
777 if(!shape_parse(itag))
778 return PY_ERROR("Couldn't parse shape");
779 shape_internal_t*fi = (shape_internal_t*)itag->data;
781 if(!PyArg_ParseTuple(args, "i", &nr))
784 int num = fi->shape2->numfillstyles;
785 if(nr < 0 || nr >=num)
786 return PY_ERROR("fillstyle index out of range");
787 return f_LineStyle3(fi->shape2->linestyles[nr]);
789 static PyObject* shape_setfillstyle(PyObject*self, PyObject*args)
791 tag_internals_t*itag = tag_getinternals(self);
792 if(!shape_parse(itag))
793 return PY_ERROR("Couldn't parse shape");
794 shape_internal_t*fi = (shape_internal_t*)itag->data;
797 if(!PyArg_ParseTuple(args, "iO!", &nr, &FillStyleClass, &fs))
800 int num = fi->shape2->numfillstyles;
801 if(nr < 0 || nr >=num)
802 return PY_ERROR("fillstyle index out of range");
803 fi->shape2->fillstyles[nr] = fillstyle_getFillStyle(fs);
806 static PyObject* shape_setlinestyle(PyObject*self, PyObject*args)
808 tag_internals_t*itag = tag_getinternals(self);
809 if(!shape_parse(itag))
810 return PY_ERROR("Couldn't parse shape");
811 shape_internal_t*fi = (shape_internal_t*)itag->data;
814 if(!PyArg_ParseTuple(args, "iO!", &nr, &LineStyleClass, &ls))
817 int num = fi->shape2->numlinestyles;
818 if(nr < 0 || nr >=num)
819 return PY_ERROR("linestyle index out of range");
820 fi->shape2->linestyles[nr] = linestyle_getLineStyle(ls);
823 static PyMethodDef shape_methods[] =
824 {{"numfillstyles", shape_getfillstyles, METH_VARARGS, "get's the number of fillstyles"},
825 {"numlinestyles", shape_getlinestyles, METH_VARARGS, "get's the number of linestyles"},
826 {"getfillstyle", shape_getfillstyle, METH_VARARGS, "get's one fillstyle"},
827 {"getlinestyle", shape_getlinestyle, METH_VARARGS, "get's one linestyle"},
828 {"setfillstyle", shape_setfillstyle, METH_VARARGS, "set's one fillstyle"},
829 {"setlinestyle", shape_setlinestyle, METH_VARARGS, "set's one linestyle"},
830 {NULL, NULL, 0, NULL}
833 static tag_internals_t shape_tag =
836 fillTAG: shape_fillTAG,
837 dealloc: shape_dealloc,
840 tagfunctions: shape_methods,
841 datasize: sizeof(shape_internal_t),
843 //----------------------------------------------------------------------------
845 typedef struct _videostream_internal
849 } videostream_internal_t;
850 staticforward tag_internals_t videostream_tag;
851 staticforward tag_internals_t videoframe_tag;
853 static int videostream_parse(tag_internals_t*self)
855 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
857 PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet"));
860 static void videostream_dealloc(tag_internals_t*self)
862 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
863 if(videostream->stream) {
864 swf_VideoStreamClear(videostream->stream);
865 free(videostream->stream);
866 videostream->stream = 0;
869 static int videostream_fillTAG(tag_internals_t*self)
871 videostream_internal_t*fi = (videostream_internal_t*)self->data;
874 PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented"));
877 static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs)
879 PyObject*tag = tag_new(&videostream_tag);
881 int width=0,height=0,frames=65535;
882 static char *kwlist[] = {"width", "height", "frames", NULL};
883 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", kwlist, &width, &height, &frames))
886 tag_internals_t*itag = tag_getinternals(tag);
887 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
888 fi->stream = malloc(sizeof(VIDEOSTREAM));
889 memset(fi->stream, 0, sizeof(VIDEOSTREAM));
891 TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
892 swf_SetU16(t, 0); /* id */
893 swf_SetVideoStreamDefine(t, fi->stream, frames, width, height);
895 fi->lastiframe = -65536;
896 return (PyObject*)tag;
898 static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self)
900 PY_ASSERT_TYPE(self, &TagClass);
901 tag_internals_t*itag = tag_getinternals(self);
902 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
905 static PyObject* videostream_getbwidth(PyObject*self, PyObject*args)
907 tag_internals_t*itag = tag_getinternals(self);
908 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
909 int width = fi->stream->bbx;
910 return Py_BuildValue("i", width);
912 static PyObject* videostream_getbheight(PyObject*self, PyObject*args)
914 tag_internals_t*itag = tag_getinternals(self);
915 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
916 int height = fi->stream->bby;
917 return Py_BuildValue("i", height);
919 static PyObject* videostream_addBlackFrame(PyObject*self, PyObject*args, PyObject*kwargs)
921 tag_internals_t*_itag = tag_getinternals(self);
922 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
924 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
926 PyObject*tag = tag_new(&videoframe_tag);
927 tag_internals_t*itag = tag_getinternals(tag);
929 swf_SetU16(t,0); /* id */
930 swf_SetVideoStreamBlackFrame(t, fi->stream);
931 fi->lastiframe = fi->stream->frame;
934 tagmap_addMapping(itag->tagmap, 0, self);
937 static PyObject* videostream_addFrame(PyObject*self, PyObject*args, PyObject*kwargs)
939 tag_internals_t*_itag = tag_getinternals(self);
940 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
943 char*type=0; // none, "i", "p"
945 static char *kwlist[] = {"image", "quant", "type", NULL};
946 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|is", kwlist, &image, &quant, &type))
948 if(fi->stream->owidth != image_getWidth(image)) {
949 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getWidth(image), fi->stream->width));return 0;
951 if(fi->stream->oheight != image_getHeight(image)) {
952 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getHeight(image), fi->stream->height));return 0;
954 PyObject*tag = tag_new(&videoframe_tag);
955 tag_internals_t*itag = tag_getinternals(tag);
957 RGBA*pic = image_toRGBA(image);
961 /*{ int f,j=0,i=0,rr,gg,bb;
964 char*filename="test.ppm";
965 printf("Creating %s %dx%d\n",filename, 512,512);
966 o=fopen(filename, "wb");
967 fprintf(o,"P6\n%d %d\n255\n",512, 512);
973 fprintf(o,"%c%c%c",rr,gg,bb);
980 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
981 if((type && (type[0]=='I' || type[0]=='i')) || (type==0 && fi->lastiframe+64 < fi->stream->frame)) {
982 swf_SetU16(t,0); /* id */
983 swf_SetVideoStreamIFrame(t, fi->stream, pic, quant);
984 fi->lastiframe = fi->stream->frame;
987 swf_SetVideoStreamPFrame(t, fi->stream, pic, quant);
990 tagmap_addMapping(itag->tagmap, 0, self);
994 static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args, PyObject*kwargs)
996 tag_internals_t*_itag = tag_getinternals(self);
997 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
999 static char *kwlist[] = {"image", "quant", NULL};
1001 PyObject* array = 0;
1002 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", kwlist, &array, &quant))
1005 signed char* movex = malloc(fi->stream->bbx * fi->stream->bby * 1);
1006 signed char* movey = malloc(fi->stream->bbx * fi->stream->bby * 1);
1007 RGBA** pics = (RGBA**)malloc(fi->stream->bbx * fi->stream->bby * sizeof(void*));
1008 signed char* itx=movex;
1009 signed char* ity=movey;
1012 if(!array || !PySequence_Check(array))
1013 return PY_ERROR("Not an array");
1014 if(PySequence_Length(array) < fi->stream->bby)
1015 return PY_ERROR("Array (y) has to have at least %d elements, but has only %d ", fi->stream->bby, PySequence_Length(array));
1016 for(y=0;y<fi->stream->bby;y++) {
1017 PyObject*line = PySequence_GetItem(array, y);
1018 if(!line || !PySequence_Check(line))
1019 return PY_ERROR("Not an array of arrays");
1020 if(PySequence_Length(line) < fi->stream->bbx)
1021 return PY_ERROR("Inner arrays (x) have to be at least %d long- %dth is only %d", fi->stream->bbx, y, PySequence_Length(line));
1023 for(x=0;x<fi->stream->bbx;x++) {
1024 PyObject*pixel = PySequence_GetItem(line, x);
1031 if(PyComplex_Check(pixel)) {
1032 xy = pixel; image = 0;
1033 } else if(PyString_Check(pixel)) {
1034 xy = 0; image = pixel;
1035 } else if(PyTuple_Check(pixel)) {
1036 int size = PyTuple_GET_SIZE(pixel);
1037 if(size!=2) return PY_ERROR("Tuples have to have size 2 (xy,img)");
1038 xy = PyTuple_GetItem(pixel, 0);
1040 if(!PyComplex_Check(xy)) return PY_ERROR("Tuples must be (COMPLEX,string)");
1041 image = PyTuple_GetItem(pixel, 1);
1042 if(!image) return 0;
1043 if(!PyString_Check(image)) return PY_ERROR("Tuples must be (complex,STRING)");
1051 *itx = (signed char)PyComplex_RealAsDouble(pixel);
1052 *ity = (signed char)PyComplex_ImagAsDouble(pixel);
1057 PyString_AsStringAndSize(image,&string,&size);
1059 return PY_ERROR("image strings must be >= 256*3");
1061 *pic = malloc(sizeof(RGBA)*16*16);
1063 for(t=0;t<16*16;t++) {
1064 (*pic)[t].r = string[t*3];
1065 (*pic)[t].g = string[t*3+1];
1066 (*pic)[t].b = string[t*3+2];
1076 PyObject*tag = tag_new(&videoframe_tag);
1077 tag_internals_t*itag = tag_getinternals(tag);
1079 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
1080 swf_SetU16(t,0); /* id */
1081 swf_SetVideoStreamMover(t, fi->stream, movex, movey,(void**)pics, quant);
1084 tagmap_addMapping(itag->tagmap, 0, self);
1086 for(x=0;x<fi->stream->bbx*fi->stream->bby;x++) {
1088 free(pics[x]);pics[x] = 0;
1098 static PyMethodDef videostream_methods[] =
1099 {{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"},
1100 {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"},
1101 {"addFrame", (PyCFunction)videostream_addFrame, METH_KEYWORDS, "add a Video Frame"},
1102 {"addBlackFrame", (PyCFunction)videostream_addBlackFrame, METH_KEYWORDS, "add a black Video Frame"},
1103 {"addDistortionFrame", (PyCFunction)videostream_addDistortionFrame, METH_KEYWORDS, "add a MVD frame"},
1104 {NULL, NULL, 0, NULL}
1107 static tag_internals_t videostream_tag =
1109 parse: videostream_parse,
1110 fillTAG: videostream_fillTAG,
1111 dealloc: videostream_dealloc,
1114 tagfunctions: videostream_methods,
1115 datasize: sizeof(videostream_internal_t),
1118 //============================================================================
1120 static tag_internals_t videoframe_tag =
1131 //============================================================================
1132 static PyMethodDef TagMethods[] =
1135 {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
1136 {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
1137 {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
1138 {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
1139 {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
1140 {"RemoveObject", (PyCFunction)f_RemoveObject, METH_KEYWORDS, "Create a RemoveObject Tag."},
1141 {"MoveObject", (PyCFunction)f_MoveObject, METH_KEYWORDS, "Create a PlaceObject Move Tag."},
1142 {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."},
1143 {"Image", (PyCFunction)f_DefineImage, METH_KEYWORDS, "Create an SWF Image Tag."},
1144 {"ImageShape", (PyCFunction)f_DefineImageShape, METH_KEYWORDS, "Create an SWF Image Shape Tag."},
1145 {"Shape", (PyCFunction)f_DefineShape, METH_KEYWORDS, "Create an SWF Shape Tag."},
1146 {"ShowFrame", (PyCFunction)f_ShowFrame, METH_KEYWORDS, "Create an SWF Show Frame Tag."},
1147 {"Sprite", (PyCFunction)f_Sprite, METH_KEYWORDS, "Create an SWF Sprite Tag."},
1149 {NULL, NULL, 0, NULL}
1151 PyMethodDef* tags_getMethods()
1153 TagClass.ob_type = &PyType_Type;
1155 register_tag(ST_PLACEOBJECT,&placeobject_tag);
1156 register_tag(ST_PLACEOBJECT2,&placeobject_tag);
1157 register_tag(ST_REMOVEOBJECT,&removeobject_tag);
1158 register_tag(ST_REMOVEOBJECT2,&removeobject_tag);
1159 register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag);
1160 register_tag(ST_DEFINEFONT,&font_tag);
1161 register_tag(ST_PROTECT,&protect_tag);
1162 register_tag(ST_DEFINETEXT,&text_tag);
1163 register_tag(ST_DEFINEBITSJPEG,&image_tag);
1164 register_tag(ST_DEFINEBITSJPEG2,&image_tag);
1165 register_tag(ST_DEFINEBITSJPEG3,&image_tag);
1166 register_tag(ST_DEFINEBITSLOSSLESS,&image_tag);
1167 register_tag(ST_DEFINEBITSLOSSLESS2,&image_tag);
1168 register_tag(ST_DEFINESHAPE,&shape_tag);
1169 register_tag(ST_DEFINESHAPE2,&shape_tag);
1170 register_tag(ST_DEFINESHAPE3,&shape_tag);
1171 register_tag(ST_SHOWFRAME,&showframe_tag);
1172 register_tag(ST_DEFINEVIDEOSTREAM,&videostream_tag);
1173 register_tag(ST_VIDEOFRAME,&videoframe_tag);
1174 register_tag(ST_DEFINESPRITE,&sprite_tag);
1175 register_tag(ST_END,&end_tag);