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
86 } placeobject_internal_t;
87 staticforward tag_internals_t placeobject_tag;
89 static void po_dealloc(tag_internals_t*self)
91 placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
93 swf_PlaceObjectFree(pi->po);
97 static int po_parse(tag_internals_t*self)
99 placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
101 PyErr_SetString(PyExc_Exception, setError("Placeobject parsing not implemented yet"));
104 static int po_fillTAG(tag_internals_t*self)
106 placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
107 self->tag = swf_InsertTag(0, ST_PLACEOBJECT2);
108 swf_SetPlaceObject(self->tag, pi->po);
111 static PyObject* po_getattr(tag_internals_t*self,char*a)
113 placeobject_internal_t*si = (placeobject_internal_t*)self->data;
114 if(!strcmp(a, "cxform")) {
120 static int po_setattr(tag_internals_t*self,char*a, PyObject*obj)
122 placeobject_internal_t*si = (placeobject_internal_t*)self->data;
123 if(!strcmp(a, "cxform")) {
129 static PyObject* po_create(PyObject* self, PyObject* args, PyObject* kwargs,char move)
131 static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
133 PyObject*character = 0;
142 po = malloc(sizeof(SWFPLACEOBJECT));
143 memset(po, 0, sizeof(SWFPLACEOBJECT));
145 swf_GetPlaceObject(0, po);
148 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|O!O!isiO!", kwlist,
151 &MatrixClass, &matrix,
152 &CXFormClass, &cxform,
156 &ActionClass, &action
161 po->clipdepth = clipdepth;
165 if(clipdepth) po->clipdepth = clipdepth;
166 if(matrix) po->matrix = matrix_getMatrix(matrix);
167 if(cxform) po->cxform = colortransform_getCXForm(cxform);
168 if(action) po->actions = action_getAction(action);
171 tag = tag_new(&placeobject_tag);
172 tag_internals_t*itag = tag_getinternals(tag);
173 placeobject_internal_t*pi = (placeobject_internal_t*)itag->data;
176 pi->po->id = tagmap_add(itag->tagmap,(PyObject*)character);
181 mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt);
183 return (PyObject*)tag;
185 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
187 return po_create(self, args, kwargs, 0);
189 static PyObject* f_MoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
191 return po_create(self, args, kwargs, 1);
193 static tag_internals_t placeobject_tag =
201 datasize: sizeof(placeobject_internal_t),
203 //----------------------------------------------------------------------------
204 staticforward tag_internals_t bgcolor_tag;
205 static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other)
207 tag_internals_t*itag = tag_getinternals(self);
209 r = itag->tag->data[0];
210 g = itag->tag->data[1];
211 b = itag->tag->data[2];
212 return Py_BuildValue("(iii)", r,g,b);
214 static PyMethodDef setbgcolor_methods[] =
215 {{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"},
216 {NULL, NULL, 0, NULL}
218 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
220 static char *kwlist[] = {"color", NULL};
225 tag = tag_new(&bgcolor_tag);
226 tag_internals_t*itag = tag_getinternals(tag);
228 /* 1st try- copy constructor */
229 if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
231 /* 2nd try- color's contructor */
232 color = f_Color(NULL, args, kwargs);
237 itag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
238 RGBA rgba = color_getRGBA(color);
239 swf_SetU8(itag->tag, rgba.r);
240 swf_SetU8(itag->tag, rgba.g);
241 swf_SetU8(itag->tag, rgba.b);
242 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);
244 return (PyObject*)tag;
246 static tag_internals_t bgcolor_tag =
253 tagfunctions: setbgcolor_methods,
256 //----------------------------------------------------------------------------
257 staticforward tag_internals_t protect_tag;
258 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
260 static char *kwlist[] = {"password", NULL};
263 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
266 PyObject*tag = tag_new(&protect_tag);
267 tag_internals_t*itag = tag_getinternals(tag);
268 itag->tag = swf_InsertTag(0, ST_PROTECT);
270 swf_SetPassword(itag->tag, password);
272 mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt);
273 return (PyObject*)tag;
275 static tag_internals_t protect_tag =
285 //----------------------------------------------------------------------------
286 staticforward tag_internals_t showframe_tag;
287 static PyObject* f_ShowFrame(PyObject* self, PyObject* args, PyObject* kwargs)
289 static char *kwlist[] = {"name", NULL};
291 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
294 PyObject*tag = tag_new(&showframe_tag);
295 tag_internals_t*itag = tag_getinternals(tag);
296 itag->tag = swf_InsertTag(0, ST_SHOWFRAME);
297 mylog("+%08x(%d) f_ShowFrame", (int)tag, tag->ob_refcnt);
298 return (PyObject*)tag;
300 static tag_internals_t showframe_tag =
310 //----------------------------------------------------------------------------
311 staticforward tag_internals_t removeobject_tag;
312 static PyObject* f_RemoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
314 static char *kwlist[] = {"depth", NULL};
316 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &depth))
319 PyObject*tag = tag_new(&removeobject_tag);
320 tag_internals_t*itag = tag_getinternals(tag);
321 itag->tag = swf_InsertTag(0, ST_REMOVEOBJECT);
322 swf_SetU16(itag->tag, depth);
323 mylog("+%08x(%d) f_RemoveObject", (int)tag, tag->ob_refcnt);
324 return (PyObject*)tag;
326 static tag_internals_t removeobject_tag =
336 //----------------------------------------------------------------------------
337 staticforward tag_internals_t sprite_tag;
338 typedef struct _sprite_internal
343 static int sprite_fillTAG(tag_internals_t*self)
345 mylog("+%08x(?) sprite_fillTAG", (int)self);
347 sprite_internal_t*si = (sprite_internal_t*)self->data;
349 TAG*sprite = swf_InsertTag(0, ST_DEFINESPRITE);
350 swf_SetU16(sprite, 0); //id
351 swf_SetU16(sprite, 0); //frames
353 TAG*tag = taglist_getTAGs2(si->taglist, self->tagmap, 0);
355 /* pass through exception */
359 while(tag2->next) tag2 = tag2->next;
360 swf_InsertTag(tag2, ST_END);
365 swf_FoldSprite(sprite);
370 static PyObject* f_Sprite(PyObject* self, PyObject* args, PyObject* kwargs)
372 static char *kwlist[] = {"name", NULL};
374 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
377 PyObject*tag = tag_new(&sprite_tag);
378 tag_internals_t*itag = tag_getinternals(tag);
379 sprite_internal_t*si = (sprite_internal_t*)itag->data;
380 si->taglist = taglist_new();
381 mylog("+%08x(%d) f_DefineSprite", (int)tag, tag->ob_refcnt);
382 return (PyObject*)tag;
384 static PyObject* sprite_getattr(tag_internals_t*self,char*a)
386 sprite_internal_t*si = (sprite_internal_t*)self->data;
387 if(!strcmp(a, "tags")) {
388 Py_INCREF(si->taglist);
393 static int sprite_setattr(tag_internals_t*self,char*a, PyObject*obj)
395 sprite_internal_t*si = (sprite_internal_t*)self->data;
397 swf_DeleteTag(self->tag);
400 if(!strcmp(a, "tags")) {
401 PY_ASSERT_TYPE(obj,&TagListClass);
402 Py_DECREF(si->taglist);
404 Py_INCREF(si->taglist);
409 static tag_internals_t sprite_tag =
412 fillTAG: sprite_fillTAG,
414 getattr: sprite_getattr,
415 setattr: sprite_setattr,
417 datasize: sizeof(sprite_internal_t),
419 //----------------------------------------------------------------------------
420 staticforward tag_internals_t end_tag;
421 static tag_internals_t end_tag =
431 //----------------------------------------------------------------------------
432 staticforward tag_internals_t text_tag;
434 typedef struct _text_internal
442 staticforward tag_internals_t placeobject_tag;
444 static int text_fillTAG(tag_internals_t*self)
446 text_internal_t*ti = (text_internal_t*)self->data;
447 self->tag= swf_InsertTag(0, ST_DEFINETEXT2);
448 swf_SetU16(self->tag, /*ID*/0);
449 ti->bbox = swf_SetDefineText(self->tag, ti->swffont, &ti->rgba, ti->text, ti->size);
452 static PyObject* text_getattr(tag_internals_t*self,char*a)
454 text_internal_t*si = (text_internal_t*)self->data;
455 if(!strcmp(a, "bbox")) {
456 return f_BBox2(si->bbox);
460 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
462 static char *kwlist[] = {"font", "text", "size", "color", NULL};
467 RGBA rgba = {255,0,0,0};
471 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!Oi|O!", kwlist, &TagClass, &font, &otext, &size, &ColorClass, &color))
473 if(PyUnicode_Check(otext)) {
474 text = PyString_AS_STRING(PyUnicode_AsUTF8String(otext));
475 } else if(PyString_Check(otext)) {
476 text = PyString_AS_STRING(otext);
480 rgba = color_getRGBA(color);
482 mylog("DefineText: text = %s", text);
484 tag = tag_new(&text_tag);
485 tag_internals_t* itag = tag_getinternals(tag);
486 text_internal_t*ti = (text_internal_t*)itag->data;
488 ti->swffont = font_getSWFFONT(font);
489 int font_id = tagmap_add(itag->tagmap, font); // add dependency on font
490 ti->swffont->id = font_id; // for swf_SetDefineTexts
491 ti->text = strdup(text);
495 return (PyObject*)tag;
497 static tag_internals_t text_tag =
500 fillTAG: text_fillTAG,
502 getattr: text_getattr,
505 datasize: sizeof(text_internal_t),
507 //----------------------------------------------------------------------------
508 staticforward tag_internals_t image_tag;
510 typedef struct _image_internal
520 staticforward tag_internals_t image_tag;
522 static int image_fillTAG(tag_internals_t*self)
524 image_internal_t*ti = (image_internal_t*)self->data;
525 self->tag= swf_InsertTag(0, ST_DEFINEBITSLOSSLESS2);
526 swf_SetU16(self->tag, /*ID*/0);
527 swf_SetLosslessBits(self->tag, ti->width, ti->height, ti->rgba, BMF_32BIT);
530 static void image_dealloc(tag_internals_t*self)
532 image_internal_t*pi = (image_internal_t*)self->data;
534 free(pi->rgba);pi->rgba = 0;
537 static int imagetag_getWidth(PyObject* self)
539 tag_internals_t*itag = tag_getinternals(self);
540 image_internal_t*pi = (image_internal_t*)itag->data;
543 static int imagetag_getHeight(PyObject* self)
545 tag_internals_t*itag = tag_getinternals(self);
546 image_internal_t*pi = (image_internal_t*)itag->data;
549 static PyObject* f_DefineImage(PyObject* self, PyObject* args, PyObject* kwargs)
551 static char *kwlist[] = {"image", NULL};
553 PyObject*tag = tag_new(&image_tag);
555 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &image))
558 tag = tag_new(&image_tag);
559 tag_internals_t* itag = tag_getinternals(tag);
560 image_internal_t*ti = (image_internal_t*)itag->data;
562 ti->rgba = image_toRGBA(image);
563 if(!ti->rgba) // pass through exception
565 ti->width = image_getWidth(image);
566 ti->height = image_getHeight(image);
570 ti->size = ti->width*ti->height;
572 return (PyObject*)tag;
574 static tag_internals_t image_tag =
577 fillTAG: image_fillTAG,
578 dealloc: image_dealloc,
582 datasize: sizeof(image_internal_t),
584 //----------------------------------------------------------------------------
585 staticforward tag_internals_t shape_tag;
587 typedef struct _shape_internal
591 staticforward tag_internals_t shape_tag;
593 static int shape_fillTAG(tag_internals_t*self)
595 shape_internal_t*ti = (shape_internal_t*)self->data;
596 self->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
597 swf_SetU16(self->tag, /*ID*/0);
598 swf_SetShape2(self->tag, ti->shape2);
601 static int shape_parse(tag_internals_t*self)
603 shape_internal_t*i= (shape_internal_t*)self->data;
608 SHAPE2* shape2 = malloc(sizeof(SHAPE2));
609 swf_ParseDefineShape(self->tag, shape2);
611 swf_DeleteTag(self->tag);self->tag = 0;
614 static void shape_dealloc(tag_internals_t*self)
616 shape_internal_t*pi = (shape_internal_t*)self->data;
618 swf_Shape2Free(pi->shape2);
622 static PyObject* f_DefineImageShape(PyObject* self, PyObject* args, PyObject* kwargs)
624 static char *kwlist[] = {"image", NULL};
627 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &TagClass, &image))
630 PyObject*tag = tag_new(&shape_tag);
631 tag_internals_t* itag = tag_getinternals(tag);
632 shape_internal_t*ti = (shape_internal_t*)itag->data;
633 ti->shape2 = 0; /*HACK*/
635 int width = imagetag_getWidth(image);
636 int height = imagetag_getHeight(image);
637 int id = tagmap_add(itag->tagmap, image);
638 itag->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
639 swf_SetU16(itag->tag, 0);
640 swf_ShapeSetBitmapRect(itag->tag, id, width, height);
641 return (PyObject*)tag;
644 /* TODO: move to lib/ */
645 SHAPE2*swf_StringToShape2(char*s,FILLSTYLE*f, LINESTYLE*l)
648 swf_Shape11DrawerInit(&draw, 0);
649 draw_string(&draw, s);
651 SHAPE*s1 = swf_ShapeDrawerToShape(&draw);
652 SRECT r = swf_ShapeDrawerGetBBox(&draw);
653 RGBA col;col.r=col.g=col.b=128;col.a=255;
655 swf_ShapeAddLineStyle(s1, 1, &col);
657 swf_ShapeAddSolidFillStyle(s1, &col);
659 SHAPE2*shape2 = swf_ShapeToShape2(s1);
661 shape2->bbox = malloc(sizeof(SRECT));
663 if(f && shape2->numfillstyles)
664 shape2->fillstyles[0] = *f;
665 if(l && shape2->numlinestyles)
666 shape2->linestyles[0] = *l;
670 static PyObject* f_DefineShape(PyObject* self, PyObject* args, PyObject* kwargs)
672 static char *kwlist[] = {"s", "fill", "line", NULL};
674 PyObject*fillstyle=0,*linestyle=0;
676 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OO", kwlist, &s,&fillstyle,&linestyle))
679 PyObject*tag = tag_new(&shape_tag);
680 tag_internals_t* itag = tag_getinternals(tag);
681 shape_internal_t*ti = (shape_internal_t*)itag->data;
688 if(PY_CHECK_TYPE(fillstyle, &ColorClass)) {
689 f->type = /*FILL_SOLID*/ 0;
690 f->color = color_getRGBA(fillstyle);
692 return PY_ERROR("Invalid Fillstyle");
698 if(PyTuple_Check(linestyle) && PyTuple_GET_SIZE(linestyle)==2) {
701 if(!PyArg_ParseTuple(linestyle, "fO!", &f, &ColorClass, &color))
704 l->width = (int)(f*20);
705 l->color = color_getRGBA(color);
707 return PY_ERROR("Invalid Linestyle");
710 ti->shape2 = swf_StringToShape2(s,f,l);
714 return (PyObject*)tag;
716 static PyObject* shape_getfillstyles(PyObject*self, PyObject*args)
718 tag_internals_t*itag = tag_getinternals(self);
719 if(!shape_parse(itag))
720 return PY_ERROR("Couldn't parse shape");
721 shape_internal_t*fi = (shape_internal_t*)itag->data;
722 int num = fi->shape2->numfillstyles;
723 return Py_BuildValue("i", num);
725 static PyObject* shape_getlinestyles(PyObject*self, PyObject*args)
727 tag_internals_t*itag = tag_getinternals(self);
728 if(!shape_parse(itag))
729 return PY_ERROR("Couldn't parse shape");
730 shape_internal_t*fi = (shape_internal_t*)itag->data;
731 int num = fi->shape2->numlinestyles;
732 return Py_BuildValue("i", num);
734 static PyObject* shape_getfillstyle(PyObject*self, PyObject*args)
736 tag_internals_t*itag = tag_getinternals(self);
737 if(!shape_parse(itag))
738 return PY_ERROR("Couldn't parse shape");
739 shape_internal_t*fi = (shape_internal_t*)itag->data;
741 if(!PyArg_ParseTuple(args, "i", &nr))
744 int num = fi->shape2->numfillstyles;
745 if(nr < 0 || nr >=num)
746 return PY_ERROR("fillstyle index out of range");
747 return f_FillStyle2(fi->shape2->fillstyles[nr]);
749 static PyObject* shape_getlinestyle(PyObject*self, PyObject*args)
751 tag_internals_t*itag = tag_getinternals(self);
752 if(!shape_parse(itag))
753 return PY_ERROR("Couldn't parse shape");
754 shape_internal_t*fi = (shape_internal_t*)itag->data;
756 if(!PyArg_ParseTuple(args, "i", &nr))
759 int num = fi->shape2->numfillstyles;
760 if(nr < 0 || nr >=num)
761 return PY_ERROR("fillstyle index out of range");
762 return f_LineStyle3(fi->shape2->linestyles[nr]);
764 static PyObject* shape_setfillstyle(PyObject*self, PyObject*args)
766 tag_internals_t*itag = tag_getinternals(self);
767 if(!shape_parse(itag))
768 return PY_ERROR("Couldn't parse shape");
769 shape_internal_t*fi = (shape_internal_t*)itag->data;
772 if(!PyArg_ParseTuple(args, "iO!", &nr, &FillStyleClass, &fs))
775 int num = fi->shape2->numfillstyles;
776 if(nr < 0 || nr >=num)
777 return PY_ERROR("fillstyle index out of range");
778 fi->shape2->fillstyles[nr] = fillstyle_getFillStyle(fs);
781 static PyObject* shape_setlinestyle(PyObject*self, PyObject*args)
783 tag_internals_t*itag = tag_getinternals(self);
784 if(!shape_parse(itag))
785 return PY_ERROR("Couldn't parse shape");
786 shape_internal_t*fi = (shape_internal_t*)itag->data;
789 if(!PyArg_ParseTuple(args, "iO!", &nr, &LineStyleClass, &ls))
792 int num = fi->shape2->numlinestyles;
793 if(nr < 0 || nr >=num)
794 return PY_ERROR("linestyle index out of range");
795 fi->shape2->linestyles[nr] = linestyle_getLineStyle(ls);
798 static PyMethodDef shape_methods[] =
799 {{"numfillstyles", shape_getfillstyles, METH_VARARGS, "get's the number of fillstyles"},
800 {"numlinestyles", shape_getlinestyles, METH_VARARGS, "get's the number of linestyles"},
801 {"getfillstyle", shape_getfillstyle, METH_VARARGS, "get's one fillstyle"},
802 {"getlinestyle", shape_getlinestyle, METH_VARARGS, "get's one linestyle"},
803 {"setfillstyle", shape_setfillstyle, METH_VARARGS, "set's one fillstyle"},
804 {"setlinestyle", shape_setlinestyle, METH_VARARGS, "set's one linestyle"},
805 {NULL, NULL, 0, NULL}
808 static tag_internals_t shape_tag =
811 fillTAG: shape_fillTAG,
812 dealloc: shape_dealloc,
815 tagfunctions: shape_methods,
816 datasize: sizeof(shape_internal_t),
818 //----------------------------------------------------------------------------
820 typedef struct _videostream_internal
824 } videostream_internal_t;
825 staticforward tag_internals_t videostream_tag;
826 staticforward tag_internals_t videoframe_tag;
828 static int videostream_parse(tag_internals_t*self)
830 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
832 PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet"));
835 static void videostream_dealloc(tag_internals_t*self)
837 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
838 if(videostream->stream) {
839 swf_VideoStreamClear(videostream->stream);
840 free(videostream->stream);
841 videostream->stream = 0;
844 static int videostream_fillTAG(tag_internals_t*self)
846 videostream_internal_t*fi = (videostream_internal_t*)self->data;
849 PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented"));
852 static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs)
854 PyObject*tag = tag_new(&videostream_tag);
856 int width=0,height=0,frames=65535;
857 static char *kwlist[] = {"width", "height", "frames", NULL};
858 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", kwlist, &width, &height, &frames))
861 tag_internals_t*itag = tag_getinternals(tag);
862 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
863 fi->stream = malloc(sizeof(VIDEOSTREAM));
864 memset(fi->stream, 0, sizeof(VIDEOSTREAM));
866 TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
867 swf_SetU16(t, 0); /* id */
868 swf_SetVideoStreamDefine(t, fi->stream, frames, width, height);
870 fi->lastiframe = -65536;
871 return (PyObject*)tag;
873 static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self)
875 PY_ASSERT_TYPE(self, &TagClass);
876 tag_internals_t*itag = tag_getinternals(self);
877 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
880 static PyObject* videostream_getbwidth(PyObject*self, PyObject*args)
882 tag_internals_t*itag = tag_getinternals(self);
883 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
884 int width = fi->stream->bbx;
885 return Py_BuildValue("i", width);
887 static PyObject* videostream_getbheight(PyObject*self, PyObject*args)
889 tag_internals_t*itag = tag_getinternals(self);
890 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
891 int height = fi->stream->bby;
892 return Py_BuildValue("i", height);
894 static PyObject* videostream_addBlackFrame(PyObject*self, PyObject*args, PyObject*kwargs)
896 tag_internals_t*_itag = tag_getinternals(self);
897 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
899 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
901 PyObject*tag = tag_new(&videoframe_tag);
902 tag_internals_t*itag = tag_getinternals(tag);
904 swf_SetU16(t,0); /* id */
905 swf_SetVideoStreamBlackFrame(t, fi->stream);
906 fi->lastiframe = fi->stream->frame;
909 tagmap_addMapping(itag->tagmap, 0, self);
912 static PyObject* videostream_addFrame(PyObject*self, PyObject*args, PyObject*kwargs)
914 tag_internals_t*_itag = tag_getinternals(self);
915 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
918 char*type=0; // none, "i", "p"
920 static char *kwlist[] = {"image", "quant", "type", NULL};
921 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|is", kwlist, &image, &quant, &type))
923 if(fi->stream->owidth != image_getWidth(image)) {
924 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getWidth(image), fi->stream->width));return 0;
926 if(fi->stream->oheight != image_getHeight(image)) {
927 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getHeight(image), fi->stream->height));return 0;
929 PyObject*tag = tag_new(&videoframe_tag);
930 tag_internals_t*itag = tag_getinternals(tag);
932 RGBA*pic = image_toRGBA(image);
936 /*{ int f,j=0,i=0,rr,gg,bb;
939 char*filename="test.ppm";
940 printf("Creating %s %dx%d\n",filename, 512,512);
941 o=fopen(filename, "wb");
942 fprintf(o,"P6\n%d %d\n255\n",512, 512);
948 fprintf(o,"%c%c%c",rr,gg,bb);
955 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
956 if((type && (type[0]=='I' || type[0]=='i')) || (type==0 && fi->lastiframe+64 < fi->stream->frame)) {
957 swf_SetU16(t,0); /* id */
958 swf_SetVideoStreamIFrame(t, fi->stream, pic, quant);
959 fi->lastiframe = fi->stream->frame;
962 swf_SetVideoStreamPFrame(t, fi->stream, pic, quant);
965 tagmap_addMapping(itag->tagmap, 0, self);
969 static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args, PyObject*kwargs)
971 tag_internals_t*_itag = tag_getinternals(self);
972 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
974 static char *kwlist[] = {"image", "quant", NULL};
977 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", kwlist, &array, &quant))
980 signed char* movex = malloc(fi->stream->bbx * fi->stream->bby * 1);
981 signed char* movey = malloc(fi->stream->bbx * fi->stream->bby * 1);
982 RGBA** pics = (RGBA**)malloc(fi->stream->bbx * fi->stream->bby * sizeof(void*));
983 signed char* itx=movex;
984 signed char* ity=movey;
987 if(!array || !PySequence_Check(array))
988 return PY_ERROR("Not an array");
989 if(PySequence_Length(array) < fi->stream->bby)
990 return PY_ERROR("Array (y) has to have at least %d elements, but has only %d ", fi->stream->bby, PySequence_Length(array));
991 for(y=0;y<fi->stream->bby;y++) {
992 PyObject*line = PySequence_GetItem(array, y);
993 if(!line || !PySequence_Check(line))
994 return PY_ERROR("Not an array of arrays");
995 if(PySequence_Length(line) < fi->stream->bbx)
996 return PY_ERROR("Inner arrays (x) have to be at least %d long- %dth is only %d", fi->stream->bbx, y, PySequence_Length(line));
998 for(x=0;x<fi->stream->bbx;x++) {
999 PyObject*pixel = PySequence_GetItem(line, x);
1006 if(PyComplex_Check(pixel)) {
1007 xy = pixel; image = 0;
1008 } else if(PyString_Check(pixel)) {
1009 xy = 0; image = pixel;
1010 } else if(PyTuple_Check(pixel)) {
1011 int size = PyTuple_GET_SIZE(pixel);
1012 if(size!=2) return PY_ERROR("Tuples have to have size 2 (xy,img)");
1013 xy = PyTuple_GetItem(pixel, 0);
1015 if(!PyComplex_Check(xy)) return PY_ERROR("Tuples must be (COMPLEX,string)");
1016 image = PyTuple_GetItem(pixel, 1);
1017 if(!image) return 0;
1018 if(!PyString_Check(image)) return PY_ERROR("Tuples must be (complex,STRING)");
1026 *itx = (signed char)PyComplex_RealAsDouble(pixel);
1027 *ity = (signed char)PyComplex_ImagAsDouble(pixel);
1032 PyString_AsStringAndSize(image,&string,&size);
1034 return PY_ERROR("image strings must be >= 256*3");
1036 *pic = malloc(sizeof(RGBA)*16*16);
1038 for(t=0;t<16*16;t++) {
1039 (*pic)[t].r = string[t*3];
1040 (*pic)[t].g = string[t*3+1];
1041 (*pic)[t].b = string[t*3+2];
1051 PyObject*tag = tag_new(&videoframe_tag);
1052 tag_internals_t*itag = tag_getinternals(tag);
1054 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
1055 swf_SetU16(t,0); /* id */
1056 swf_SetVideoStreamMover(t, fi->stream, movex, movey,(void**)pics, quant);
1059 tagmap_addMapping(itag->tagmap, 0, self);
1061 for(x=0;x<fi->stream->bbx*fi->stream->bby;x++) {
1063 free(pics[x]);pics[x] = 0;
1073 static PyMethodDef videostream_methods[] =
1074 {{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"},
1075 {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"},
1076 {"addFrame", (PyCFunction)videostream_addFrame, METH_KEYWORDS, "add a Video Frame"},
1077 {"addBlackFrame", (PyCFunction)videostream_addBlackFrame, METH_KEYWORDS, "add a black Video Frame"},
1078 {"addDistortionFrame", (PyCFunction)videostream_addDistortionFrame, METH_KEYWORDS, "add a MVD frame"},
1079 {NULL, NULL, 0, NULL}
1082 static tag_internals_t videostream_tag =
1084 parse: videostream_parse,
1085 fillTAG: videostream_fillTAG,
1086 dealloc: videostream_dealloc,
1089 tagfunctions: videostream_methods,
1090 datasize: sizeof(videostream_internal_t),
1093 //============================================================================
1095 static tag_internals_t videoframe_tag =
1106 //============================================================================
1107 static PyMethodDef TagMethods[] =
1110 {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
1111 {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
1112 {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
1113 {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
1114 {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
1115 {"RemoveObject", (PyCFunction)f_RemoveObject, METH_KEYWORDS, "Create a RemoveObject Tag."},
1116 {"MoveObject", (PyCFunction)f_MoveObject, METH_KEYWORDS, "Create a PlaceObject Move Tag."},
1117 {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."},
1118 {"Image", (PyCFunction)f_DefineImage, METH_KEYWORDS, "Create an SWF Image Tag."},
1119 {"ImageShape", (PyCFunction)f_DefineImageShape, METH_KEYWORDS, "Create an SWF Image Shape Tag."},
1120 {"Shape", (PyCFunction)f_DefineShape, METH_KEYWORDS, "Create an SWF Shape Tag."},
1121 {"ShowFrame", (PyCFunction)f_ShowFrame, METH_KEYWORDS, "Create an SWF Show Frame Tag."},
1122 {"Sprite", (PyCFunction)f_Sprite, METH_KEYWORDS, "Create an SWF Sprite Tag."},
1124 {NULL, NULL, 0, NULL}
1126 PyMethodDef* tags_getMethods()
1128 TagClass.ob_type = &PyType_Type;
1130 register_tag(ST_PLACEOBJECT,&placeobject_tag);
1131 register_tag(ST_PLACEOBJECT2,&placeobject_tag);
1132 register_tag(ST_REMOVEOBJECT,&removeobject_tag);
1133 register_tag(ST_REMOVEOBJECT2,&removeobject_tag);
1134 register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag);
1135 register_tag(ST_DEFINEFONT,&font_tag);
1136 register_tag(ST_PROTECT,&protect_tag);
1137 register_tag(ST_DEFINETEXT,&text_tag);
1138 register_tag(ST_DEFINEBITSJPEG,&image_tag);
1139 register_tag(ST_DEFINEBITSJPEG2,&image_tag);
1140 register_tag(ST_DEFINEBITSJPEG3,&image_tag);
1141 register_tag(ST_DEFINEBITSLOSSLESS,&image_tag);
1142 register_tag(ST_DEFINEBITSLOSSLESS2,&image_tag);
1143 register_tag(ST_DEFINESHAPE,&shape_tag);
1144 register_tag(ST_DEFINESHAPE2,&shape_tag);
1145 register_tag(ST_DEFINESHAPE3,&shape_tag);
1146 register_tag(ST_SHOWFRAME,&showframe_tag);
1147 register_tag(ST_DEFINEVIDEOSTREAM,&videostream_tag);
1148 register_tag(ST_VIDEOFRAME,&videoframe_tag);
1149 register_tag(ST_DEFINESPRITE,&sprite_tag);
1150 register_tag(ST_END,&end_tag);