f1b69a9e15720d94551165a73f2477f5b4d8186b
[swftools.git] / lib / python / tags.c
1 #include "pyutils.h"
2 #include "primitives.h"
3 #include "action.h"
4 #include "taglist.h"
5 #include "tag.h"
6 #include "tags.h"
7 #include "image.h"
8
9 //----------------------------------------------------------------------------
10
11 typedef struct _font_internal
12 {
13     SWFFONT* font;
14 } font_internal_t;
15 staticforward tag_internals_t font_tag;
16
17 static int font_parse(tag_internals_t*self)
18 {
19     font_internal_t*font = (font_internal_t*)self->data;
20     /* TODO */
21     PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet"));
22     return 0;
23 }
24 static void font_dealloc(tag_internals_t*self)
25 {
26     font_internal_t*font = (font_internal_t*)self->data;
27     if(font->font) {
28         swf_FontFree(font->font);
29         font->font = 0;
30     }
31 }
32 static int font_fillTAG(tag_internals_t*self)
33 {
34     font_internal_t*fi = (font_internal_t*)self->data;
35     if(self->tag)
36         return 1;
37     self->tag = swf_InsertTag(0, ST_DEFINEFONT2);
38     swf_FontSetDefine2(self->tag, fi->font);
39     return 1;
40 }
41 static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs)
42 {
43     static char *kwlist[] = {"filename", NULL};
44     char*filename = 0;
45     PyObject*tag;
46     SWFFONT* font;
47
48     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &filename))
49         return NULL;
50
51     font = swf_LoadFont(filename);
52     if(!font) {
53         PyErr_SetString(PyExc_Exception, setError("Could not load %s", filename));
54         return NULL;
55     }
56
57     tag = tag_new(&font_tag);
58     tag_internals_t*itag = tag_getinternals(tag);
59     font_internal_t*fi = (font_internal_t*)itag->data;
60     font->id = 0;
61     fi->font = font;
62     return (PyObject*)tag;
63 }
64 static SWFFONT* font_getSWFFONT(PyObject*self)
65 {
66     PY_ASSERT_TYPE(self, &TagClass);
67     tag_internals_t*itag = tag_getinternals(self);
68     font_internal_t*fi = (font_internal_t*)itag->data;
69     return fi->font;
70 }
71 static tag_internals_t font_tag =
72 {
73     parse: font_parse,
74     fillTAG: font_fillTAG,
75     dealloc: font_dealloc,
76     getattr: 0, 
77     setattr: 0,
78     tagfunctions: 0,
79     datasize: sizeof(font_internal_t),
80 };
81 //----------------------------------------------------------------------------
82
83 typedef struct _placeobject_internal
84 {
85     SWFPLACEOBJECT* po;
86 } placeobject_internal_t;
87 staticforward tag_internals_t placeobject_tag;
88
89 static void po_dealloc(tag_internals_t*self)
90 {
91     placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
92     if(pi->po) {
93         swf_PlaceObjectFree(pi->po);
94         pi->po = 0;
95     }
96 }
97 static int po_parse(tag_internals_t*self)
98 {
99     placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
100     /* TODO */
101     PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet"));
102     return 0;
103 }
104 static int po_fillTAG(tag_internals_t*self)
105 {
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);
109     return 1;
110 }
111 static PyObject* po_create(PyObject* self, PyObject* args, PyObject* kwargs,char move)
112 {
113     static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
114     
115     PyObject*character = 0;
116     int depth;
117     int clipdepth = 0;
118     PyObject*matrix = 0;
119     PyObject*cxform = 0;
120     PyObject*action = 0;
121     int ratio = 0;
122     char* name = 0;
123     SWFPLACEOBJECT* po;
124     po = malloc(sizeof(SWFPLACEOBJECT));
125     memset(po, 0, sizeof(SWFPLACEOBJECT));
126     
127     swf_GetPlaceObject(0, po);
128    
129     PyErr_Clear();
130     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|O!O!isiO!", kwlist, 
131                 &character, 
132                 &depth, 
133                 &MatrixClass, &matrix, 
134                 &CXFormClass, &cxform,
135                 &ratio,
136                 &name,
137                 &clipdepth,
138                 &ActionClass, &action
139                 ))
140         return NULL;
141
142     po->depth = depth;
143     po->clipdepth = clipdepth;
144     po->ratio = ratio;
145     po->name = name;
146     po->move = move;
147     if(clipdepth) po->clipdepth = clipdepth;
148     if(matrix) po->matrix = matrix_getMatrix(matrix);
149     if(cxform) po->cxform = colortransform_getCXForm(cxform);
150     if(action) po->actions = action_getAction(action);
151
152     PyObject*tag;
153     tag = tag_new(&placeobject_tag);
154     tag_internals_t*itag = tag_getinternals(tag);
155     placeobject_internal_t*pi = (placeobject_internal_t*)itag->data;
156     pi->po = po;
157     if(!move) {
158         pi->po->id = tagmap_add(itag->tagmap,(PyObject*)character);
159     } else {
160         pi->po->id = 0;
161     }
162     
163     mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt);
164
165     return (PyObject*)tag;
166 }
167 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
168 {
169     return po_create(self, args, kwargs, 0);
170 }
171 static PyObject* f_MoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
172 {
173     return po_create(self, args, kwargs, 1);
174 }
175 static tag_internals_t placeobject_tag =
176 {
177     parse: po_parse,
178     fillTAG: po_fillTAG,
179     dealloc: po_dealloc,
180     getattr: 0, 
181     setattr: 0,
182     tagfunctions: 0,
183     datasize: sizeof(placeobject_internal_t),
184 };
185 //----------------------------------------------------------------------------
186 staticforward tag_internals_t bgcolor_tag;
187 static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other)
188 {
189     tag_internals_t*itag = tag_getinternals(self);
190     int r,g,b;
191     r = itag->tag->data[0];
192     g = itag->tag->data[1];
193     b = itag->tag->data[2];
194     return Py_BuildValue("(iii)", r,g,b);
195 }
196 static PyMethodDef setbgcolor_methods[] = 
197 {{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"},
198  {NULL, NULL, 0, NULL}
199 };
200 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
201 {
202     static char *kwlist[] = {"color", NULL};
203     int r=0,g=0,b=0;
204     PyObject*tag;
205     PyObject*color;
206     
207     tag = tag_new(&bgcolor_tag);
208     tag_internals_t*itag = tag_getinternals(tag);
209
210     /* 1st try- copy constructor */
211     if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
212         PyErr_Clear();
213         /* 2nd try- color's contructor */
214         color = f_Color(NULL, args, kwargs);
215     }
216     if(!color)
217         return NULL;
218
219     itag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
220     RGBA rgba = color_getRGBA(color);
221     swf_SetU8(itag->tag, rgba.r);
222     swf_SetU8(itag->tag, rgba.g);
223     swf_SetU8(itag->tag, rgba.b);
224     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);
225     Py_DECREF(color);
226     return (PyObject*)tag;
227 }
228 static tag_internals_t bgcolor_tag =
229 {
230     parse: 0,
231     fillTAG: 0,
232     dealloc: 0,
233     getattr: 0, 
234     setattr: 0,
235     tagfunctions: setbgcolor_methods,
236     datasize: 0,
237 };
238 //----------------------------------------------------------------------------
239 staticforward tag_internals_t protect_tag;
240 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
241 {
242     static char *kwlist[] = {"password", NULL};
243     char*password = 0;
244
245     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
246         return NULL;
247
248     PyObject*tag = tag_new(&protect_tag);
249     tag_internals_t*itag = tag_getinternals(tag);
250     itag->tag = swf_InsertTag(0, ST_PROTECT);
251     if(password) {
252         swf_SetPassword(itag->tag, password);
253     }
254     mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt);
255     return (PyObject*)tag;
256 }
257 static tag_internals_t protect_tag =
258 {
259     parse: 0,
260     fillTAG: 0,
261     dealloc: 0,
262     getattr: 0, 
263     setattr: 0,
264     tagfunctions: 0,
265     datasize: 0,
266 };
267 //----------------------------------------------------------------------------
268 staticforward tag_internals_t showframe_tag;
269 static PyObject* f_ShowFrame(PyObject* self, PyObject* args, PyObject* kwargs)
270 {
271     static char *kwlist[] = {"name", NULL};
272     char*name= 0;
273     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
274         return NULL;
275
276     PyObject*tag = tag_new(&showframe_tag);
277     tag_internals_t*itag = tag_getinternals(tag);
278     itag->tag = swf_InsertTag(0, ST_SHOWFRAME);
279     mylog("+%08x(%d) f_ShowFrame", (int)tag, tag->ob_refcnt);
280     return (PyObject*)tag;
281 }
282 static tag_internals_t showframe_tag =
283 {
284     parse: 0,
285     fillTAG: 0,
286     dealloc: 0,
287     getattr: 0, 
288     setattr: 0,
289     tagfunctions: 0,
290     datasize: 0,
291 };
292 //----------------------------------------------------------------------------
293 staticforward tag_internals_t removeobject_tag;
294 static PyObject* f_RemoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
295 {
296     static char *kwlist[] = {"depth", NULL};
297     int depth;
298     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &depth))
299         return NULL;
300
301     PyObject*tag = tag_new(&removeobject_tag);
302     tag_internals_t*itag = tag_getinternals(tag);
303     itag->tag = swf_InsertTag(0, ST_REMOVEOBJECT);
304     swf_SetU16(itag->tag, depth);
305     mylog("+%08x(%d) f_RemoveObject", (int)tag, tag->ob_refcnt);
306     return (PyObject*)tag;
307 }
308 static tag_internals_t removeobject_tag =
309 {
310     parse: 0,
311     fillTAG: 0,
312     dealloc: 0,
313     getattr: 0, 
314     setattr: 0,
315     tagfunctions: 0,
316     datasize: 0,
317 };
318 //----------------------------------------------------------------------------
319 staticforward tag_internals_t sprite_tag;
320 typedef struct _sprite_internal
321 {
322     PyObject* taglist;
323 } sprite_internal_t;
324     
325 static int sprite_fillTAG(tag_internals_t*self)
326 {
327     mylog("+%08x(?) sprite_fillTAG", (int)self);
328
329     sprite_internal_t*si = (sprite_internal_t*)self->data;
330
331     TAG*sprite = swf_InsertTag(0, ST_DEFINESPRITE);
332     swf_SetU16(sprite, 0); //id
333     swf_SetU16(sprite, 0); //frames
334
335     TAG*tag = taglist_getTAGs2(si->taglist, self->tagmap, 0);
336     if(!tag) {
337         /* pass through exception */
338         return 0;
339     }
340     TAG*tag2 = tag;
341     while(tag2->next) tag2 = tag2->next;
342     swf_InsertTag(tag2, ST_END);
343
344     sprite->next = tag;
345     tag->prev = sprite;
346
347     swf_FoldSprite(sprite);
348     self->tag = sprite;
349     return 1;
350 }
351
352 static PyObject* f_Sprite(PyObject* self, PyObject* args, PyObject* kwargs)
353 {
354     static char *kwlist[] = {"name", NULL};
355     char*name= 0;
356     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
357         return NULL;
358
359     PyObject*tag = tag_new(&sprite_tag);
360     tag_internals_t*itag = tag_getinternals(tag);
361     sprite_internal_t*si = (sprite_internal_t*)itag->data;
362     si->taglist = taglist_new();
363     mylog("+%08x(%d) f_DefineSprite", (int)tag, tag->ob_refcnt);
364     return (PyObject*)tag;
365 }
366 static PyObject* sprite_getattr(tag_internals_t*self,char*a)
367 {
368     sprite_internal_t*si = (sprite_internal_t*)self->data;
369     if(!strcmp(a, "tags")) {
370         Py_INCREF(si->taglist);
371         return si->taglist;
372     }
373     return 0;
374 }
375 static int sprite_setattr(tag_internals_t*self,char*a, PyObject*obj)
376 {
377     sprite_internal_t*si = (sprite_internal_t*)self->data;
378     if(self->tag) {
379         swf_DeleteTag(self->tag);
380         self->tag = 0;
381     }
382     if(!strcmp(a, "tags")) {
383         PY_ASSERT_TYPE(obj,&TagListClass);
384         Py_DECREF(si->taglist);
385         si->taglist = obj;
386         Py_INCREF(si->taglist);
387         return 0;
388     }
389     return 1;
390 }
391 static tag_internals_t sprite_tag =
392 {
393     parse: 0,
394     fillTAG: sprite_fillTAG,
395     dealloc: 0,
396     getattr: sprite_getattr, 
397     setattr: sprite_setattr,
398     tagfunctions: 0,
399     datasize: sizeof(sprite_internal_t),
400 };
401 //----------------------------------------------------------------------------
402 staticforward tag_internals_t end_tag;
403 static tag_internals_t end_tag =
404 {
405     parse: 0,
406     fillTAG: 0,
407     dealloc: 0,
408     getattr: 0, 
409     setattr: 0,
410     tagfunctions: 0,
411     datasize: 0,
412 };
413 //----------------------------------------------------------------------------
414 staticforward tag_internals_t text_tag;
415
416 typedef struct _text_internal
417 {
418     char*text;
419     SWFFONT* swffont;
420     RGBA rgba;
421     int size;
422     SRECT bbox;
423 } text_internal_t;
424 staticforward tag_internals_t placeobject_tag;
425
426 static int text_fillTAG(tag_internals_t*self)
427 {
428     text_internal_t*ti = (text_internal_t*)self->data;
429     self->tag= swf_InsertTag(0, ST_DEFINETEXT2);
430     swf_SetU16(self->tag, /*ID*/0);
431     ti->bbox = swf_SetDefineText(self->tag, ti->swffont, &ti->rgba, ti->text, ti->size);
432     return 1;
433 }
434 static PyObject* text_getattr(tag_internals_t*self,char*a)
435 {
436     text_internal_t*si = (text_internal_t*)self->data;
437     if(!strcmp(a, "bbox")) {
438         return f_BBox2(si->bbox);
439     }
440     return 0;
441 }
442 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
443 {
444     static char *kwlist[] = {"font", "text", "size", "color", NULL};
445     PyObject*tag = 0;
446     PyObject*otext;
447     char*text = 0;
448     int size = 0;
449     RGBA rgba = {255,0,0,0};
450     PyObject*color = 0;
451     PyObject*font = 0;
452
453     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!Oi|O!", kwlist, &TagClass, &font, &otext, &size, &ColorClass, &color))
454         return NULL;
455     if(PyUnicode_Check(otext)) {
456         text = PyString_AS_STRING(PyUnicode_AsUTF8String(otext));
457     } else if(PyString_Check(otext)) {
458         text = PyString_AS_STRING(otext);
459     }
460
461     if(color)
462         rgba = color_getRGBA(color);
463
464     mylog("DefineText: text = %s", text);
465     
466     tag = tag_new(&text_tag);
467     tag_internals_t* itag = tag_getinternals(tag);
468     text_internal_t*ti = (text_internal_t*)itag->data;
469
470     ti->swffont = font_getSWFFONT(font);
471     int font_id = tagmap_add(itag->tagmap, font); // add dependency on font
472     ti->swffont->id = font_id; // for swf_SetDefineTexts
473     ti->text = strdup(text);
474     ti->rgba = rgba;
475     ti->size = size;
476
477     return (PyObject*)tag;
478 }
479 static tag_internals_t text_tag =
480 {
481     parse: 0,
482     fillTAG: text_fillTAG,
483     dealloc: 0,
484     getattr: text_getattr, 
485     setattr: 0,
486     tagfunctions: 0,
487     datasize: sizeof(text_internal_t),
488 };
489 //----------------------------------------------------------------------------
490 staticforward tag_internals_t image_tag;
491
492 typedef struct _image_internal
493 {
494     RGBA*rgba;
495     int size;
496     int width;
497     int height;
498     int bpp;
499     char isindexed;
500     char islossless;
501 } image_internal_t;
502 staticforward tag_internals_t image_tag;
503
504 static int image_fillTAG(tag_internals_t*self)
505 {
506     image_internal_t*ti = (image_internal_t*)self->data;
507     self->tag= swf_InsertTag(0, ST_DEFINEBITSLOSSLESS2);
508     swf_SetU16(self->tag, /*ID*/0);
509     swf_SetLosslessBits(self->tag, ti->width, ti->height, ti->rgba, BMF_32BIT);
510     return 1;
511 }
512 static void image_dealloc(tag_internals_t*self)
513 {
514     image_internal_t*pi = (image_internal_t*)self->data;
515     if(pi->rgba) {
516         free(pi->rgba);pi->rgba = 0;
517     }
518 }
519 static int imagetag_getWidth(PyObject* self)
520 {
521     tag_internals_t*itag = tag_getinternals(self);
522     image_internal_t*pi = (image_internal_t*)itag->data;
523     return pi->width;
524 }
525 static int imagetag_getHeight(PyObject* self)
526 {
527     tag_internals_t*itag = tag_getinternals(self);
528     image_internal_t*pi = (image_internal_t*)itag->data;
529     return pi->height;
530 }
531 static PyObject* f_DefineImage(PyObject* self, PyObject* args, PyObject* kwargs)
532 {
533     static char *kwlist[] = {"image"};
534     PyObject*image = 0;
535     PyObject*tag = tag_new(&image_tag);
536
537     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &image))
538         return NULL;
539     
540     tag = tag_new(&image_tag);
541     tag_internals_t* itag = tag_getinternals(tag);
542     image_internal_t*ti = (image_internal_t*)itag->data;
543
544     ti->rgba = image_toRGBA(image);
545     if(!ti->rgba) // pass through exception
546         return 0;
547     ti->width = image_getWidth(image);
548     ti->height = image_getHeight(image);
549     ti->isindexed = 0;
550     ti->islossless = 1;
551     ti->bpp = 32;
552     ti->size = ti->width*ti->height;
553
554     return (PyObject*)tag;
555 }
556 static tag_internals_t image_tag =
557 {
558     parse: 0,
559     fillTAG: image_fillTAG,
560     dealloc: image_dealloc,
561     getattr: 0, 
562     setattr: 0,
563     tagfunctions: 0,
564     datasize: sizeof(image_internal_t),
565 };
566 //----------------------------------------------------------------------------
567 staticforward tag_internals_t shape_tag;
568
569 typedef struct _shape_internal
570 {
571     SHAPE2*shape;
572 } shape_internal_t;
573 staticforward tag_internals_t shape_tag;
574
575 static int shape_fillTAG(tag_internals_t*self)
576 {
577     shape_internal_t*ti = (shape_internal_t*)self->data;
578     self->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
579     swf_SetU16(self->tag, /*ID*/0);
580     swf_SetShape2(self->tag, ti->shape);
581     return 1;
582 }
583 static void shape_dealloc(tag_internals_t*self)
584 {
585     shape_internal_t*pi = (shape_internal_t*)self->data;
586     if(pi->shape) {
587         swf_Shape2Free(pi->shape);
588         pi->shape = 0;
589     }
590 }
591 static PyObject* f_DefineImageShape(PyObject* self, PyObject* args, PyObject* kwargs)
592 {
593     static char *kwlist[] = {"image"};
594     PyObject*shape = 0;
595     PyObject*tag = tag_new(&shape_tag);
596     PyObject*image = 0;
597
598     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &TagClass, &image))
599         return NULL;
600     
601     tag = tag_new(&shape_tag);
602     tag_internals_t* itag = tag_getinternals(tag);
603     shape_internal_t*ti = (shape_internal_t*)itag->data;
604     ti->shape = 0; /*HACK*/
605
606     int width = imagetag_getWidth(image);
607     int height = imagetag_getHeight(image);
608     int id = tagmap_add(itag->tagmap, image);
609     itag->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
610     swf_SetU16(itag->tag, 0);
611     swf_ShapeSetBitmapRect(itag->tag, id, width, height);
612     return (PyObject*)tag;
613 }
614 static tag_internals_t shape_tag =
615 {
616     parse: 0,
617     fillTAG: shape_fillTAG,
618     dealloc: shape_dealloc,
619     getattr: 0, 
620     setattr: 0,
621     tagfunctions: 0,
622     datasize: sizeof(shape_internal_t),
623 };
624 //----------------------------------------------------------------------------
625
626 typedef struct _videostream_internal
627 {
628     VIDEOSTREAM* stream;
629     int lastiframe;
630 } videostream_internal_t;
631 staticforward tag_internals_t videostream_tag;
632 staticforward tag_internals_t videoframe_tag;
633
634 static int videostream_parse(tag_internals_t*self)
635 {
636     videostream_internal_t*videostream = (videostream_internal_t*)self->data;
637     /* TODO */
638     PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet"));
639     return 0;
640 }
641 static void videostream_dealloc(tag_internals_t*self)
642 {
643     videostream_internal_t*videostream = (videostream_internal_t*)self->data;
644     if(videostream->stream) {
645         swf_VideoStreamClear(videostream->stream);
646         free(videostream->stream);
647         videostream->stream = 0;
648     }
649 }
650 static int videostream_fillTAG(tag_internals_t*self)
651 {
652     videostream_internal_t*fi = (videostream_internal_t*)self->data;
653     if(self->tag)
654         return 1;
655     PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented"));
656     return 0;
657 }
658 static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs)
659 {
660     PyObject*tag = tag_new(&videostream_tag);
661    
662     int width=0,height=0,frames=65535;
663     static char *kwlist[] = {"width", "height", "frames", NULL};
664     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", kwlist, &width, &height, &frames))
665         return NULL;
666
667     tag_internals_t*itag = tag_getinternals(tag);
668     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
669     fi->stream = malloc(sizeof(VIDEOSTREAM));
670     memset(fi->stream, 0, sizeof(VIDEOSTREAM));
671
672     TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
673     swf_SetU16(t, 0); /* id */
674     swf_SetVideoStreamDefine(t, fi->stream, frames, width, height);
675     itag->tag = t;
676     fi->lastiframe = -65536;
677     return (PyObject*)tag;
678 }
679 static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self)
680 {
681     PY_ASSERT_TYPE(self, &TagClass);
682     tag_internals_t*itag = tag_getinternals(self);
683     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
684     return fi->stream;
685 }
686 static PyObject* videostream_getbwidth(PyObject*self, PyObject*args)
687 {
688     tag_internals_t*itag = tag_getinternals(self);
689     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
690     int width = fi->stream->bbx;
691     return Py_BuildValue("i", width);
692 }
693 static PyObject* videostream_getbheight(PyObject*self, PyObject*args)
694 {
695     tag_internals_t*itag = tag_getinternals(self);
696     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
697     int height = fi->stream->bby;
698     return Py_BuildValue("i", height);
699 }
700 static PyObject* videostream_addBlackFrame(PyObject*self, PyObject*args, PyObject*kwargs)
701 {
702     tag_internals_t*_itag = tag_getinternals(self);
703     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
704     
705     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
706     
707     PyObject*tag = tag_new(&videoframe_tag);
708     tag_internals_t*itag = tag_getinternals(tag);
709     
710     swf_SetU16(t,0); /* id */
711     swf_SetVideoStreamBlackFrame(t, fi->stream);
712     fi->lastiframe = fi->stream->frame;
713     
714     itag->tag = t;
715     tagmap_addMapping(itag->tagmap, 0, self);
716     return tag;
717 }
718 static PyObject* videostream_addFrame(PyObject*self, PyObject*args, PyObject*kwargs)
719 {
720     tag_internals_t*_itag = tag_getinternals(self);
721     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
722    
723     PyObject*image = 0;
724     char*type=0; // none, "i", "p"
725     int quant=7;
726     static char *kwlist[] = {"image", "quant", "type", NULL};
727     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|is", kwlist, &image, &quant, &type))
728         return NULL;
729     if(fi->stream->width != image_getWidth(image)) {
730         PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getWidth(image), fi->stream->width));return 0;
731     }
732     if(fi->stream->height != image_getHeight(image)) {
733         PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getHeight(image), fi->stream->height));return 0;
734     }
735     PyObject*tag = tag_new(&videoframe_tag);
736     tag_internals_t*itag = tag_getinternals(tag);
737
738     RGBA*pic = image_toRGBA(image);
739     if(!pic)
740         return 0;
741
742 /*{  int f,j=0,i=0,rr,gg,bb;
743    FILE *o;
744    RGBA*it = pic;
745    char*filename="test.ppm";
746    printf("Creating %s %dx%d\n",filename, 512,512);
747    o=fopen(filename, "wb");
748    fprintf(o,"P6\n%d %d\n255\n",512, 512);
749    
750    while(j<512*512) {
751     rr=it->r;
752     gg=it->g;
753     bb=it->b;
754     fprintf(o,"%c%c%c",rr,gg,bb);
755     it++;
756     j++;
757    }
758    fclose(o);
759 }*/
760
761     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
762     if((type && (type[0]=='I' || type[0]=='i')) || (type==0 && fi->lastiframe+64 < fi->stream->frame)) {
763         swf_SetU16(t,0); /* id */
764         swf_SetVideoStreamIFrame(t, fi->stream, pic, quant);
765         fi->lastiframe = fi->stream->frame;
766     } else {
767         swf_SetU16(t,0);
768         swf_SetVideoStreamPFrame(t, fi->stream, pic, quant);
769     }
770     itag->tag = t;
771     tagmap_addMapping(itag->tagmap, 0, self);
772     free(pic);
773     return tag;
774 }
775 static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args, PyObject*kwargs)
776 {
777     tag_internals_t*_itag = tag_getinternals(self);
778     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
779     
780     static char *kwlist[] = {"image", "quant", NULL};
781     int quant=7;
782     PyObject* array = 0;
783     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", kwlist, &array, &quant))
784         return NULL;
785
786     signed char* movex = malloc(fi->stream->bbx * fi->stream->bby * 1);
787     signed char* movey = malloc(fi->stream->bbx * fi->stream->bby * 1);
788     RGBA** pics = (RGBA**)malloc(fi->stream->bbx * fi->stream->bby * sizeof(void*));
789     signed char* itx=movex;
790     signed char* ity=movey;
791     RGBA**pic=pics;
792     int x,y;
793     if(!array || !PySequence_Check(array))
794         return PY_ERROR("Not an array");
795     if(PySequence_Length(array) < fi->stream->bby)
796         return PY_ERROR("Array (y) has to have at least %d elements, but has only %d ", fi->stream->bby, PySequence_Length(array));
797     for(y=0;y<fi->stream->bby;y++) {
798         PyObject*line = PySequence_GetItem(array, y);
799         if(!line || !PySequence_Check(line))
800             return PY_ERROR("Not an array of arrays");
801         if(PySequence_Length(line) < fi->stream->bbx)
802             return PY_ERROR("Inner arrays (x) have to be at least %d long- %dth is only %d", fi->stream->bbx, y, PySequence_Length(line));
803
804         for(x=0;x<fi->stream->bbx;x++) {
805             PyObject*pixel = PySequence_GetItem(line, x);
806             PyObject*xy = 0;
807             PyObject*image = 0;
808
809             if(!pixel) {
810                 xy = image = 0;
811             } else {
812                 if(PyComplex_Check(pixel)) {
813                     xy = pixel; image = 0;
814                 } else if(PyString_Check(pixel)) {
815                     xy = 0; image = pixel;
816                 } else if(PyTuple_Check(pixel)) {
817                     int size = PyTuple_GET_SIZE(pixel);
818                     if(size!=2) return PY_ERROR("Tuples have to have size 2 (xy,img)");
819                     xy = PyTuple_GetItem(pixel, 0);
820                     if(!xy) return 0;
821                     if(!PyComplex_Check(xy)) return PY_ERROR("Tuples must be (COMPLEX,string)");
822                     image = PyTuple_GetItem(pixel, 1);
823                     if(!image) return 0;
824                     if(!PyString_Check(image)) return PY_ERROR("Tuples must be (complex,STRING)");
825                 }
826             }
827
828             *itx = *ity = 0;
829             *pic= 0;
830
831             if(xy) {
832                 *itx = (signed char)PyComplex_RealAsDouble(pixel);
833                 *ity = (signed char)PyComplex_ImagAsDouble(pixel);
834             }
835             if(image) {
836                 char*string;
837                 int size;
838                 PyString_AsStringAndSize(image,&string,&size);
839                 if(size<256*3) {
840                     return PY_ERROR("image strings must be >= 256*3");
841                 }
842                 *pic = malloc(sizeof(RGBA)*16*16);
843                 int t;
844                 for(t=0;t<16*16;t++) {
845                     (*pic)[t].r = string[t*3];
846                     (*pic)[t].g = string[t*3+1];
847                     (*pic)[t].b = string[t*3+2];
848                     (*pic)[t].a = 255;
849                 }
850             }
851             itx++;
852             ity++;
853             pic++;
854         }
855     }
856     
857     PyObject*tag = tag_new(&videoframe_tag);
858     tag_internals_t*itag = tag_getinternals(tag);
859     
860     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
861     swf_SetU16(t,0); /* id */
862     swf_SetVideoStreamMover(t, fi->stream, movex, movey,(void**)pics, quant);
863
864     itag->tag = t;
865     tagmap_addMapping(itag->tagmap, 0, self);
866
867     for(x=0;x<fi->stream->bbx*fi->stream->bby;x++) {
868         if(pics[x]) {
869             free(pics[x]);pics[x] = 0;
870         }
871     }
872
873     free(movex);
874     free(movey);
875     free(pics);
876
877     return tag;
878 }
879 static PyMethodDef videostream_methods[] = 
880 {{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"},
881  {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"},
882  {"addFrame", (PyCFunction)videostream_addFrame, METH_KEYWORDS, "add a Video Frame"},
883  {"addBlackFrame", (PyCFunction)videostream_addBlackFrame, METH_KEYWORDS, "add a black Video Frame"},
884  {"addDistortionFrame", (PyCFunction)videostream_addDistortionFrame, METH_KEYWORDS, "add a MVD frame"},
885  {NULL, NULL, 0, NULL}
886 };
887
888 static tag_internals_t videostream_tag =
889 {
890     parse: videostream_parse,
891     fillTAG: videostream_fillTAG,
892     dealloc: videostream_dealloc,
893     getattr: 0, 
894     setattr: 0,
895     tagfunctions: videostream_methods,
896     datasize: sizeof(videostream_internal_t),
897 };
898
899 //============================================================================
900
901 static tag_internals_t videoframe_tag =
902 {
903     parse: 0,
904     fillTAG: 0,
905     dealloc: 0,
906     getattr: 0, 
907     setattr: 0,
908     tagfunctions: 0,
909     datasize: 0
910 };
911
912 //============================================================================
913
914 static PyMethodDef TagMethods[] = 
915 {
916     /* TAGS */
917     {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
918     {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
919     {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
920     {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
921     {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
922     {"RemoveObject", (PyCFunction)f_RemoveObject, METH_KEYWORDS, "Create a RemoveObject Tag."},
923     {"MoveObject", (PyCFunction)f_MoveObject, METH_KEYWORDS, "Create a PlaceObject Move Tag."},
924     {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."},
925     {"Image", (PyCFunction)f_DefineImage, METH_KEYWORDS, "Create an SWF Image Tag."},
926     {"ImageShape", (PyCFunction)f_DefineImageShape, METH_KEYWORDS, "Create an SWF Image Shape Tag."},
927     {"ShowFrame", (PyCFunction)f_ShowFrame, METH_KEYWORDS, "Create an SWF Show Frame Tag."},
928     {"Sprite", (PyCFunction)f_Sprite, METH_KEYWORDS, "Create an SWF Sprite Tag."},
929     {NULL, NULL, 0, NULL}
930 };
931 PyMethodDef* tags_getMethods()
932 {
933     TagClass.ob_type = &PyType_Type;
934     
935     register_tag(ST_PLACEOBJECT,&placeobject_tag);
936     register_tag(ST_PLACEOBJECT2,&placeobject_tag);
937     register_tag(ST_REMOVEOBJECT,&removeobject_tag);
938     register_tag(ST_REMOVEOBJECT2,&removeobject_tag);
939     register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag);
940     register_tag(ST_DEFINEFONT,&font_tag);
941     register_tag(ST_PROTECT,&protect_tag);
942     register_tag(ST_DEFINETEXT,&text_tag);
943     register_tag(ST_DEFINEBITSJPEG,&image_tag);
944     register_tag(ST_DEFINEBITSJPEG2,&image_tag);
945     register_tag(ST_DEFINEBITSJPEG3,&image_tag);
946     register_tag(ST_DEFINEBITSLOSSLESS,&image_tag);
947     register_tag(ST_DEFINEBITSLOSSLESS2,&image_tag);
948     register_tag(ST_SHOWFRAME,&showframe_tag);
949     register_tag(ST_DEFINEVIDEOSTREAM,&videostream_tag);
950     register_tag(ST_VIDEOFRAME,&videoframe_tag);
951     register_tag(ST_DEFINESPRITE,&sprite_tag);
952     register_tag(ST_END,&end_tag);
953
954     return TagMethods;
955 }