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 //PyObject*character //TODO
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*pi = (placeobject_internal_t*)self->data;
103 PyErr_SetString(PyExc_Exception, setError("Placeobject parsing not implemented yet"));
106 static int po_fillTAG(tag_internals_t*self)
108 placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
109 self->tag = swf_InsertTag(0, ST_PLACEOBJECT2);
110 swf_SetPlaceObject(self->tag, pi->po);
113 static PyObject* po_getattr(tag_internals_t*self,char*a)
115 placeobject_internal_t*si = (placeobject_internal_t*)self->data;
116 if(!strcmp(a, "cxform")) {
122 static int po_setattr(tag_internals_t*self,char*a, PyObject*obj)
124 placeobject_internal_t*si = (placeobject_internal_t*)self->data;
125 if(!strcmp(a, "cxform")) {
131 static PyObject* po_create(PyObject* self, PyObject* args, PyObject* kwargs,char move)
133 static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
135 PyObject*character = 0;
144 po = malloc(sizeof(SWFPLACEOBJECT));
145 memset(po, 0, sizeof(SWFPLACEOBJECT));
147 swf_GetPlaceObject(0, po);
150 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|O!O!isiO!", kwlist,
153 &MatrixClass, &matrix,
154 &CXFormClass, &cxform,
158 &ActionClass, &action
163 po->clipdepth = clipdepth;
167 if(clipdepth) po->clipdepth = clipdepth;
168 if(matrix) po->matrix = matrix_getMatrix(matrix);
169 if(cxform) po->cxform = colortransform_getCXForm(cxform);
170 if(action) po->actions = action_getAction(action);
173 tag = tag_new(&placeobject_tag);
174 tag_internals_t*itag = tag_getinternals(tag);
175 placeobject_internal_t*pi = (placeobject_internal_t*)itag->data;
178 pi->po->id = tagmap_add(itag->tagmap,(PyObject*)character);
183 mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt);
185 return (PyObject*)tag;
187 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
189 return po_create(self, args, kwargs, 0);
191 static PyObject* f_MoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
193 return po_create(self, args, kwargs, 1);
195 static tag_internals_t placeobject_tag =
203 datasize: sizeof(placeobject_internal_t),
205 //----------------------------------------------------------------------------
206 staticforward tag_internals_t bgcolor_tag;
207 static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other)
209 tag_internals_t*itag = tag_getinternals(self);
211 r = itag->tag->data[0];
212 g = itag->tag->data[1];
213 b = itag->tag->data[2];
214 return Py_BuildValue("(iii)", r,g,b);
216 static PyMethodDef setbgcolor_methods[] =
217 {{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"},
218 {NULL, NULL, 0, NULL}
220 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
222 static char *kwlist[] = {"color", NULL};
227 tag = tag_new(&bgcolor_tag);
228 tag_internals_t*itag = tag_getinternals(tag);
230 /* 1st try- copy constructor */
231 if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
233 /* 2nd try- color's contructor */
234 color = f_Color(NULL, args, kwargs);
239 itag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
240 RGBA rgba = color_getRGBA(color);
241 swf_SetU8(itag->tag, rgba.r);
242 swf_SetU8(itag->tag, rgba.g);
243 swf_SetU8(itag->tag, rgba.b);
244 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);
246 return (PyObject*)tag;
248 static tag_internals_t bgcolor_tag =
255 tagfunctions: setbgcolor_methods,
258 //----------------------------------------------------------------------------
259 staticforward tag_internals_t protect_tag;
260 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
262 static char *kwlist[] = {"password", NULL};
265 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
268 PyObject*tag = tag_new(&protect_tag);
269 tag_internals_t*itag = tag_getinternals(tag);
270 itag->tag = swf_InsertTag(0, ST_PROTECT);
272 swf_SetPassword(itag->tag, password);
274 mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt);
275 return (PyObject*)tag;
277 static tag_internals_t protect_tag =
287 //----------------------------------------------------------------------------
288 staticforward tag_internals_t showframe_tag;
289 static PyObject* f_ShowFrame(PyObject* self, PyObject* args, PyObject* kwargs)
291 static char *kwlist[] = {"name", NULL};
293 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
296 PyObject*tag = tag_new(&showframe_tag);
297 tag_internals_t*itag = tag_getinternals(tag);
298 itag->tag = swf_InsertTag(0, ST_SHOWFRAME);
299 mylog("+%08x(%d) f_ShowFrame", (int)tag, tag->ob_refcnt);
300 return (PyObject*)tag;
302 static tag_internals_t showframe_tag =
312 //----------------------------------------------------------------------------
313 staticforward tag_internals_t removeobject_tag;
314 static PyObject* f_RemoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
316 static char *kwlist[] = {"depth", NULL};
318 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &depth))
321 PyObject*tag = tag_new(&removeobject_tag);
322 tag_internals_t*itag = tag_getinternals(tag);
323 itag->tag = swf_InsertTag(0, ST_REMOVEOBJECT);
324 swf_SetU16(itag->tag, depth);
325 mylog("+%08x(%d) f_RemoveObject", (int)tag, tag->ob_refcnt);
326 return (PyObject*)tag;
328 static tag_internals_t removeobject_tag =
338 //----------------------------------------------------------------------------
339 staticforward tag_internals_t sprite_tag;
340 typedef struct _sprite_internal
345 static int sprite_fillTAG(tag_internals_t*self)
347 mylog("+%08x(?) sprite_fillTAG", (int)self);
349 sprite_internal_t*si = (sprite_internal_t*)self->data;
351 TAG*sprite = swf_InsertTag(0, ST_DEFINESPRITE);
352 swf_SetU16(sprite, 0); //id
353 swf_SetU16(sprite, 0); //frames
355 TAG*tag = taglist_getTAGs2(si->taglist, self->tagmap, 0);
357 /* pass through exception */
361 while(tag2->next) tag2 = tag2->next;
362 swf_InsertTag(tag2, ST_END);
367 swf_FoldSprite(sprite);
372 static PyObject* f_Sprite(PyObject* self, PyObject* args, PyObject* kwargs)
374 static char *kwlist[] = {"name", NULL};
376 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
379 PyObject*tag = tag_new(&sprite_tag);
380 tag_internals_t*itag = tag_getinternals(tag);
381 sprite_internal_t*si = (sprite_internal_t*)itag->data;
382 si->taglist = taglist_new();
383 mylog("+%08x(%d) f_DefineSprite", (int)tag, tag->ob_refcnt);
384 return (PyObject*)tag;
386 static PyObject* sprite_getattr(tag_internals_t*self,char*a)
388 sprite_internal_t*si = (sprite_internal_t*)self->data;
389 if(!strcmp(a, "tags")) {
390 Py_INCREF(si->taglist);
395 static int sprite_setattr(tag_internals_t*self,char*a, PyObject*obj)
397 sprite_internal_t*si = (sprite_internal_t*)self->data;
399 swf_DeleteTag(self->tag);
402 if(!strcmp(a, "tags")) {
403 PY_ASSERT_TYPE(obj,&TagListClass);
404 Py_DECREF(si->taglist);
406 Py_INCREF(si->taglist);
411 static tag_internals_t sprite_tag =
414 fillTAG: sprite_fillTAG,
416 getattr: sprite_getattr,
417 setattr: sprite_setattr,
419 datasize: sizeof(sprite_internal_t),
421 //----------------------------------------------------------------------------
422 staticforward tag_internals_t end_tag;
423 static tag_internals_t end_tag =
433 //----------------------------------------------------------------------------
434 staticforward tag_internals_t text_tag;
436 typedef struct _text_internal
444 staticforward tag_internals_t placeobject_tag;
446 static int text_fillTAG(tag_internals_t*self)
448 text_internal_t*ti = (text_internal_t*)self->data;
449 self->tag= swf_InsertTag(0, ST_DEFINETEXT2);
450 swf_SetU16(self->tag, /*ID*/0);
451 ti->bbox = swf_SetDefineText(self->tag, ti->swffont, &ti->rgba, ti->text, ti->size);
454 static PyObject* text_getattr(tag_internals_t*self,char*a)
456 text_internal_t*si = (text_internal_t*)self->data;
457 if(!strcmp(a, "bbox")) {
458 return f_BBox2(si->bbox);
462 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
464 static char *kwlist[] = {"font", "text", "size", "color", NULL};
469 RGBA rgba = {255,0,0,0};
473 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!Oi|O!", kwlist, &TagClass, &font, &otext, &size, &ColorClass, &color))
475 if(PyUnicode_Check(otext)) {
476 text = PyString_AS_STRING(PyUnicode_AsUTF8String(otext));
477 } else if(PyString_Check(otext)) {
478 text = PyString_AS_STRING(otext);
482 rgba = color_getRGBA(color);
484 mylog("DefineText: text = %s", text);
486 tag = tag_new(&text_tag);
487 tag_internals_t* itag = tag_getinternals(tag);
488 text_internal_t*ti = (text_internal_t*)itag->data;
490 ti->swffont = font_getSWFFONT(font);
491 int font_id = tagmap_add(itag->tagmap, font); // add dependency on font
492 ti->swffont->id = font_id; // for swf_SetDefineTexts
493 ti->text = strdup(text);
497 return (PyObject*)tag;
499 static tag_internals_t text_tag =
502 fillTAG: text_fillTAG,
504 getattr: text_getattr,
507 datasize: sizeof(text_internal_t),
509 //----------------------------------------------------------------------------
510 staticforward tag_internals_t image_tag;
512 typedef struct _image_internal
522 staticforward tag_internals_t image_tag;
524 static int image_fillTAG(tag_internals_t*self)
526 image_internal_t*ti = (image_internal_t*)self->data;
527 self->tag= swf_InsertTag(0, ST_DEFINEBITSLOSSLESS2);
528 swf_SetU16(self->tag, /*ID*/0);
529 swf_SetLosslessBits(self->tag, ti->width, ti->height, ti->rgba, BMF_32BIT);
532 static void image_dealloc(tag_internals_t*self)
534 image_internal_t*pi = (image_internal_t*)self->data;
536 free(pi->rgba);pi->rgba = 0;
539 static int imagetag_getWidth(PyObject* self)
541 tag_internals_t*itag = tag_getinternals(self);
542 image_internal_t*pi = (image_internal_t*)itag->data;
545 static int imagetag_getHeight(PyObject* self)
547 tag_internals_t*itag = tag_getinternals(self);
548 image_internal_t*pi = (image_internal_t*)itag->data;
551 static PyObject* f_DefineImage(PyObject* self, PyObject* args, PyObject* kwargs)
553 static char *kwlist[] = {"image", NULL};
555 PyObject*tag = tag_new(&image_tag);
557 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &image))
560 tag = tag_new(&image_tag);
561 tag_internals_t* itag = tag_getinternals(tag);
562 image_internal_t*ti = (image_internal_t*)itag->data;
564 ti->rgba = image_toRGBA(image);
565 if(!ti->rgba) // pass through exception
567 ti->width = image_getWidth(image);
568 ti->height = image_getHeight(image);
572 ti->size = ti->width*ti->height;
574 return (PyObject*)tag;
576 static tag_internals_t image_tag =
579 fillTAG: image_fillTAG,
580 dealloc: image_dealloc,
584 datasize: sizeof(image_internal_t),
586 //----------------------------------------------------------------------------
587 staticforward tag_internals_t shape_tag;
589 typedef struct _shape_internal
593 staticforward tag_internals_t shape_tag;
595 static int shape_fillTAG(tag_internals_t*self)
597 shape_internal_t*ti = (shape_internal_t*)self->data;
598 self->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
599 swf_SetU16(self->tag, /*ID*/0);
600 swf_SetShape2(self->tag, ti->shape2);
603 static int shape_parse(tag_internals_t*self)
605 shape_internal_t*i= (shape_internal_t*)self->data;
610 SHAPE2* shape2 = malloc(sizeof(SHAPE2));
611 swf_ParseDefineShape(self->tag, shape2);
613 swf_DeleteTag(self->tag);self->tag = 0;
616 static void shape_dealloc(tag_internals_t*self)
618 shape_internal_t*pi = (shape_internal_t*)self->data;
620 swf_Shape2Free(pi->shape2);
624 static PyObject* f_DefineImageShape(PyObject* self, PyObject* args, PyObject* kwargs)
626 static char *kwlist[] = {"image", NULL};
629 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &TagClass, &image))
632 PyObject*tag = tag_new(&shape_tag);
633 tag_internals_t* itag = tag_getinternals(tag);
634 shape_internal_t*ti = (shape_internal_t*)itag->data;
635 ti->shape2 = 0; /*HACK*/
637 int width = imagetag_getWidth(image);
638 int height = imagetag_getHeight(image);
639 int id = tagmap_add(itag->tagmap, image);
640 itag->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
641 swf_SetU16(itag->tag, 0);
642 swf_ShapeSetBitmapRect(itag->tag, id, width, height);
643 return (PyObject*)tag;
646 /* TODO: move to lib/ */
647 SHAPE2*swf_StringToShape2(char*s,FILLSTYLE*f, LINESTYLE*l)
650 swf_Shape11DrawerInit(&draw, 0);
651 draw_string(&draw, s);
653 SHAPE*s1 = swf_ShapeDrawerToShape(&draw);
654 SRECT r = swf_ShapeDrawerGetBBox(&draw);
655 RGBA col;col.r=col.g=col.b=128;col.a=255;
657 swf_ShapeAddLineStyle(s1, 1, &col);
659 swf_ShapeAddSolidFillStyle(s1, &col);
661 SHAPE2*shape2 = swf_ShapeToShape2(s1);
663 shape2->bbox = malloc(sizeof(SRECT));
665 if(f && shape2->numfillstyles)
666 shape2->fillstyles[0] = *f;
667 if(l && shape2->numlinestyles)
668 shape2->linestyles[0] = *l;
672 static PyObject* f_DefineShape(PyObject* self, PyObject* args, PyObject* kwargs)
674 static char *kwlist[] = {"s", "fill", "line", NULL};
676 PyObject*fillstyle=0,*linestyle=0;
678 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OO", kwlist, &s,&fillstyle,&linestyle))
681 PyObject*tag = tag_new(&shape_tag);
682 tag_internals_t* itag = tag_getinternals(tag);
683 shape_internal_t*ti = (shape_internal_t*)itag->data;
690 if(PY_CHECK_TYPE(fillstyle, &ColorClass)) {
691 f->type = /*FILL_SOLID*/ 0;
692 f->color = color_getRGBA(fillstyle);
694 return PY_ERROR("Invalid Fillstyle");
700 if(PyTuple_Check(linestyle) && PyTuple_GET_SIZE(linestyle)==2) {
703 if(!PyArg_ParseTuple(linestyle, "fO!", &f, &ColorClass, &color))
706 l->width = (int)(f*20);
707 l->color = color_getRGBA(color);
709 return PY_ERROR("Invalid Linestyle");
712 ti->shape2 = swf_StringToShape2(s,f,l);
716 return (PyObject*)tag;
718 static PyObject* shape_getfillstyles(PyObject*self, PyObject*args)
720 tag_internals_t*itag = tag_getinternals(self);
721 if(!shape_parse(itag))
722 return PY_ERROR("Couldn't parse shape");
723 shape_internal_t*fi = (shape_internal_t*)itag->data;
724 int num = fi->shape2->numfillstyles;
725 return Py_BuildValue("i", num);
727 static PyObject* shape_getlinestyles(PyObject*self, PyObject*args)
729 tag_internals_t*itag = tag_getinternals(self);
730 if(!shape_parse(itag))
731 return PY_ERROR("Couldn't parse shape");
732 shape_internal_t*fi = (shape_internal_t*)itag->data;
733 int num = fi->shape2->numlinestyles;
734 return Py_BuildValue("i", num);
736 static PyObject* shape_getfillstyle(PyObject*self, PyObject*args)
738 tag_internals_t*itag = tag_getinternals(self);
739 if(!shape_parse(itag))
740 return PY_ERROR("Couldn't parse shape");
741 shape_internal_t*fi = (shape_internal_t*)itag->data;
743 if(!PyArg_ParseTuple(args, "i", &nr))
746 int num = fi->shape2->numfillstyles;
747 if(nr < 0 || nr >=num)
748 return PY_ERROR("fillstyle index out of range");
749 return f_FillStyle2(fi->shape2->fillstyles[nr]);
751 static PyObject* shape_getlinestyle(PyObject*self, PyObject*args)
753 tag_internals_t*itag = tag_getinternals(self);
754 if(!shape_parse(itag))
755 return PY_ERROR("Couldn't parse shape");
756 shape_internal_t*fi = (shape_internal_t*)itag->data;
758 if(!PyArg_ParseTuple(args, "i", &nr))
761 int num = fi->shape2->numfillstyles;
762 if(nr < 0 || nr >=num)
763 return PY_ERROR("fillstyle index out of range");
764 return f_LineStyle3(fi->shape2->linestyles[nr]);
766 static PyObject* shape_setfillstyle(PyObject*self, PyObject*args)
768 tag_internals_t*itag = tag_getinternals(self);
769 if(!shape_parse(itag))
770 return PY_ERROR("Couldn't parse shape");
771 shape_internal_t*fi = (shape_internal_t*)itag->data;
774 if(!PyArg_ParseTuple(args, "iO!", &nr, &FillStyleClass, &fs))
777 int num = fi->shape2->numfillstyles;
778 if(nr < 0 || nr >=num)
779 return PY_ERROR("fillstyle index out of range");
780 fi->shape2->fillstyles[nr] = fillstyle_getFillStyle(fs);
783 static PyObject* shape_setlinestyle(PyObject*self, PyObject*args)
785 tag_internals_t*itag = tag_getinternals(self);
786 if(!shape_parse(itag))
787 return PY_ERROR("Couldn't parse shape");
788 shape_internal_t*fi = (shape_internal_t*)itag->data;
791 if(!PyArg_ParseTuple(args, "iO!", &nr, &LineStyleClass, &ls))
794 int num = fi->shape2->numlinestyles;
795 if(nr < 0 || nr >=num)
796 return PY_ERROR("linestyle index out of range");
797 fi->shape2->linestyles[nr] = linestyle_getLineStyle(ls);
800 static PyMethodDef shape_methods[] =
801 {{"numfillstyles", shape_getfillstyles, METH_VARARGS, "get's the number of fillstyles"},
802 {"numlinestyles", shape_getlinestyles, METH_VARARGS, "get's the number of linestyles"},
803 {"getfillstyle", shape_getfillstyle, METH_VARARGS, "get's one fillstyle"},
804 {"getlinestyle", shape_getlinestyle, METH_VARARGS, "get's one linestyle"},
805 {"setfillstyle", shape_setfillstyle, METH_VARARGS, "set's one fillstyle"},
806 {"setlinestyle", shape_setlinestyle, METH_VARARGS, "set's one linestyle"},
807 {NULL, NULL, 0, NULL}
810 static tag_internals_t shape_tag =
813 fillTAG: shape_fillTAG,
814 dealloc: shape_dealloc,
817 tagfunctions: shape_methods,
818 datasize: sizeof(shape_internal_t),
820 //----------------------------------------------------------------------------
822 typedef struct _videostream_internal
826 } videostream_internal_t;
827 staticforward tag_internals_t videostream_tag;
828 staticforward tag_internals_t videoframe_tag;
830 static int videostream_parse(tag_internals_t*self)
832 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
834 PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet"));
837 static void videostream_dealloc(tag_internals_t*self)
839 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
840 if(videostream->stream) {
841 swf_VideoStreamClear(videostream->stream);
842 free(videostream->stream);
843 videostream->stream = 0;
846 static int videostream_fillTAG(tag_internals_t*self)
848 videostream_internal_t*fi = (videostream_internal_t*)self->data;
851 PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented"));
854 static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs)
856 PyObject*tag = tag_new(&videostream_tag);
858 int width=0,height=0,frames=65535;
859 static char *kwlist[] = {"width", "height", "frames", NULL};
860 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", kwlist, &width, &height, &frames))
863 tag_internals_t*itag = tag_getinternals(tag);
864 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
865 fi->stream = malloc(sizeof(VIDEOSTREAM));
866 memset(fi->stream, 0, sizeof(VIDEOSTREAM));
868 TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
869 swf_SetU16(t, 0); /* id */
870 swf_SetVideoStreamDefine(t, fi->stream, frames, width, height);
872 fi->lastiframe = -65536;
873 return (PyObject*)tag;
875 static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self)
877 PY_ASSERT_TYPE(self, &TagClass);
878 tag_internals_t*itag = tag_getinternals(self);
879 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
882 static PyObject* videostream_getbwidth(PyObject*self, PyObject*args)
884 tag_internals_t*itag = tag_getinternals(self);
885 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
886 int width = fi->stream->bbx;
887 return Py_BuildValue("i", width);
889 static PyObject* videostream_getbheight(PyObject*self, PyObject*args)
891 tag_internals_t*itag = tag_getinternals(self);
892 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
893 int height = fi->stream->bby;
894 return Py_BuildValue("i", height);
896 static PyObject* videostream_addBlackFrame(PyObject*self, PyObject*args, PyObject*kwargs)
898 tag_internals_t*_itag = tag_getinternals(self);
899 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
901 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
903 PyObject*tag = tag_new(&videoframe_tag);
904 tag_internals_t*itag = tag_getinternals(tag);
906 swf_SetU16(t,0); /* id */
907 swf_SetVideoStreamBlackFrame(t, fi->stream);
908 fi->lastiframe = fi->stream->frame;
911 tagmap_addMapping(itag->tagmap, 0, self);
914 static PyObject* videostream_addFrame(PyObject*self, PyObject*args, PyObject*kwargs)
916 tag_internals_t*_itag = tag_getinternals(self);
917 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
920 char*type=0; // none, "i", "p"
922 static char *kwlist[] = {"image", "quant", "type", NULL};
923 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|is", kwlist, &image, &quant, &type))
925 if(fi->stream->owidth != image_getWidth(image)) {
926 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getWidth(image), fi->stream->width));return 0;
928 if(fi->stream->oheight != image_getHeight(image)) {
929 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getHeight(image), fi->stream->height));return 0;
931 PyObject*tag = tag_new(&videoframe_tag);
932 tag_internals_t*itag = tag_getinternals(tag);
934 RGBA*pic = image_toRGBA(image);
938 /*{ int f,j=0,i=0,rr,gg,bb;
941 char*filename="test.ppm";
942 printf("Creating %s %dx%d\n",filename, 512,512);
943 o=fopen(filename, "wb");
944 fprintf(o,"P6\n%d %d\n255\n",512, 512);
950 fprintf(o,"%c%c%c",rr,gg,bb);
957 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
958 if((type && (type[0]=='I' || type[0]=='i')) || (type==0 && fi->lastiframe+64 < fi->stream->frame)) {
959 swf_SetU16(t,0); /* id */
960 swf_SetVideoStreamIFrame(t, fi->stream, pic, quant);
961 fi->lastiframe = fi->stream->frame;
964 swf_SetVideoStreamPFrame(t, fi->stream, pic, quant);
967 tagmap_addMapping(itag->tagmap, 0, self);
971 static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args, PyObject*kwargs)
973 tag_internals_t*_itag = tag_getinternals(self);
974 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
976 static char *kwlist[] = {"image", "quant", NULL};
979 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", kwlist, &array, &quant))
982 signed char* movex = malloc(fi->stream->bbx * fi->stream->bby * 1);
983 signed char* movey = malloc(fi->stream->bbx * fi->stream->bby * 1);
984 RGBA** pics = (RGBA**)malloc(fi->stream->bbx * fi->stream->bby * sizeof(void*));
985 signed char* itx=movex;
986 signed char* ity=movey;
989 if(!array || !PySequence_Check(array))
990 return PY_ERROR("Not an array");
991 if(PySequence_Length(array) < fi->stream->bby)
992 return PY_ERROR("Array (y) has to have at least %d elements, but has only %d ", fi->stream->bby, PySequence_Length(array));
993 for(y=0;y<fi->stream->bby;y++) {
994 PyObject*line = PySequence_GetItem(array, y);
995 if(!line || !PySequence_Check(line))
996 return PY_ERROR("Not an array of arrays");
997 if(PySequence_Length(line) < fi->stream->bbx)
998 return PY_ERROR("Inner arrays (x) have to be at least %d long- %dth is only %d", fi->stream->bbx, y, PySequence_Length(line));
1000 for(x=0;x<fi->stream->bbx;x++) {
1001 PyObject*pixel = PySequence_GetItem(line, x);
1008 if(PyComplex_Check(pixel)) {
1009 xy = pixel; image = 0;
1010 } else if(PyString_Check(pixel)) {
1011 xy = 0; image = pixel;
1012 } else if(PyTuple_Check(pixel)) {
1013 int size = PyTuple_GET_SIZE(pixel);
1014 if(size!=2) return PY_ERROR("Tuples have to have size 2 (xy,img)");
1015 xy = PyTuple_GetItem(pixel, 0);
1017 if(!PyComplex_Check(xy)) return PY_ERROR("Tuples must be (COMPLEX,string)");
1018 image = PyTuple_GetItem(pixel, 1);
1019 if(!image) return 0;
1020 if(!PyString_Check(image)) return PY_ERROR("Tuples must be (complex,STRING)");
1028 *itx = (signed char)PyComplex_RealAsDouble(pixel);
1029 *ity = (signed char)PyComplex_ImagAsDouble(pixel);
1034 PyString_AsStringAndSize(image,&string,&size);
1036 return PY_ERROR("image strings must be >= 256*3");
1038 *pic = malloc(sizeof(RGBA)*16*16);
1040 for(t=0;t<16*16;t++) {
1041 (*pic)[t].r = string[t*3];
1042 (*pic)[t].g = string[t*3+1];
1043 (*pic)[t].b = string[t*3+2];
1053 PyObject*tag = tag_new(&videoframe_tag);
1054 tag_internals_t*itag = tag_getinternals(tag);
1056 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
1057 swf_SetU16(t,0); /* id */
1058 swf_SetVideoStreamMover(t, fi->stream, movex, movey,(void**)pics, quant);
1061 tagmap_addMapping(itag->tagmap, 0, self);
1063 for(x=0;x<fi->stream->bbx*fi->stream->bby;x++) {
1065 free(pics[x]);pics[x] = 0;
1075 static PyMethodDef videostream_methods[] =
1076 {{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"},
1077 {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"},
1078 {"addFrame", (PyCFunction)videostream_addFrame, METH_KEYWORDS, "add a Video Frame"},
1079 {"addBlackFrame", (PyCFunction)videostream_addBlackFrame, METH_KEYWORDS, "add a black Video Frame"},
1080 {"addDistortionFrame", (PyCFunction)videostream_addDistortionFrame, METH_KEYWORDS, "add a MVD frame"},
1081 {NULL, NULL, 0, NULL}
1084 static tag_internals_t videostream_tag =
1086 parse: videostream_parse,
1087 fillTAG: videostream_fillTAG,
1088 dealloc: videostream_dealloc,
1091 tagfunctions: videostream_methods,
1092 datasize: sizeof(videostream_internal_t),
1095 //============================================================================
1097 static tag_internals_t videoframe_tag =
1108 //============================================================================
1109 static PyMethodDef TagMethods[] =
1112 {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
1113 {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
1114 {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
1115 {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
1116 {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
1117 {"RemoveObject", (PyCFunction)f_RemoveObject, METH_KEYWORDS, "Create a RemoveObject Tag."},
1118 {"MoveObject", (PyCFunction)f_MoveObject, METH_KEYWORDS, "Create a PlaceObject Move Tag."},
1119 {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."},
1120 {"Image", (PyCFunction)f_DefineImage, METH_KEYWORDS, "Create an SWF Image Tag."},
1121 {"ImageShape", (PyCFunction)f_DefineImageShape, METH_KEYWORDS, "Create an SWF Image Shape Tag."},
1122 {"Shape", (PyCFunction)f_DefineShape, METH_KEYWORDS, "Create an SWF Shape Tag."},
1123 {"ShowFrame", (PyCFunction)f_ShowFrame, METH_KEYWORDS, "Create an SWF Show Frame Tag."},
1124 {"Sprite", (PyCFunction)f_Sprite, METH_KEYWORDS, "Create an SWF Sprite Tag."},
1126 {NULL, NULL, 0, NULL}
1128 PyMethodDef* tags_getMethods()
1130 TagClass.ob_type = &PyType_Type;
1132 register_tag(ST_PLACEOBJECT,&placeobject_tag);
1133 register_tag(ST_PLACEOBJECT2,&placeobject_tag);
1134 register_tag(ST_REMOVEOBJECT,&removeobject_tag);
1135 register_tag(ST_REMOVEOBJECT2,&removeobject_tag);
1136 register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag);
1137 register_tag(ST_DEFINEFONT,&font_tag);
1138 register_tag(ST_PROTECT,&protect_tag);
1139 register_tag(ST_DEFINETEXT,&text_tag);
1140 register_tag(ST_DEFINEBITSJPEG,&image_tag);
1141 register_tag(ST_DEFINEBITSJPEG2,&image_tag);
1142 register_tag(ST_DEFINEBITSJPEG3,&image_tag);
1143 register_tag(ST_DEFINEBITSLOSSLESS,&image_tag);
1144 register_tag(ST_DEFINEBITSLOSSLESS2,&image_tag);
1145 register_tag(ST_DEFINESHAPE,&shape_tag);
1146 register_tag(ST_DEFINESHAPE2,&shape_tag);
1147 register_tag(ST_DEFINESHAPE3,&shape_tag);
1148 register_tag(ST_SHOWFRAME,&showframe_tag);
1149 register_tag(ST_DEFINEVIDEOSTREAM,&videostream_tag);
1150 register_tag(ST_VIDEOFRAME,&videoframe_tag);
1151 register_tag(ST_DEFINESPRITE,&sprite_tag);
1152 register_tag(ST_END,&end_tag);