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