c2ed4d390e1d083ecca3776a2ac1e5dcbb9da4cb
[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", NULL};
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*shape2;
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->shape2);
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->shape2) {
587         swf_Shape2Free(pi->shape2);
588         pi->shape2 = 0;
589     }
590 }
591 static PyObject* f_DefineImageShape(PyObject* self, PyObject* args, PyObject* kwargs)
592 {
593     static char *kwlist[] = {"image", NULL};
594     PyObject*image = 0;
595
596     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &TagClass, &image))
597         return NULL;
598     
599     PyObject*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->shape2 = 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
613 /* TODO: move to lib/ */
614 SHAPE2*swf_StringToShape2(char*s,FILLSTYLE*f, LINESTYLE*l)
615 {
616     drawer_t draw;
617     swf_Shape11DrawerInit(&draw, 0);
618     draw_string(&draw, s);
619     draw.finish(&draw);
620     SHAPE*s1 =  swf_ShapeDrawerToShape(&draw);
621     SRECT r = swf_ShapeDrawerGetBBox(&draw);
622     RGBA col;col.r=col.g=col.b=128;col.a=255;
623     if(l) 
624         swf_ShapeAddLineStyle(s1, 1, &col);
625     if(f)
626         swf_ShapeAddSolidFillStyle(s1, &col);
627     draw.dealloc(&draw);
628     SHAPE2*shape2 = swf_ShapeToShape2(s1);
629     swf_ShapeFree(s1);
630     shape2->bbox = malloc(sizeof(SRECT));
631     *(shape2->bbox) = r;
632     if(f && shape2->numfillstyles)
633         shape2->fillstyles[0] = *f;
634     if(l && shape2->numlinestyles)
635         shape2->linestyles[0] = *l;
636     return shape2;
637 }
638
639 static PyObject* f_DefineShape(PyObject* self, PyObject* args, PyObject* kwargs)
640 {
641     static char *kwlist[] = {"s", "fill", "line", NULL};
642     char*s = 0;
643     PyObject*fillstyle=0,*linestyle=0;
644
645     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OO", kwlist, &s,&fillstyle,&linestyle))
646         return NULL;
647
648     PyObject*tag = tag_new(&shape_tag);
649     tag_internals_t* itag = tag_getinternals(tag);
650     shape_internal_t*ti = (shape_internal_t*)itag->data;
651     
652     FILLSTYLE _f,*f=0;
653     LINESTYLE _l,*l=0;
654
655     if(fillstyle) {
656         f = &_f;
657         if(PY_CHECK_TYPE(fillstyle, &ColorClass)) {
658             f->type = /*FILL_SOLID*/ 0;
659             f->color = color_getRGBA(fillstyle);
660         } else {
661             return PY_ERROR("Invalid Fillstyle");
662         }
663     }
664
665     if(linestyle) {
666         l = &_l;
667         if(PyTuple_Check(linestyle) && PyTuple_GET_SIZE(linestyle)==2) {
668             float f = 0.0;
669             PyObject*color = 0;
670             if(!PyArg_ParseTuple(linestyle, "fO!", &f, &ColorClass, &color))
671                 return 0;
672
673             l->width = (int)(f*20);
674             l->color = color_getRGBA(color);
675         } else {
676             return PY_ERROR("Invalid Linestyle");
677         }
678     }
679     ti->shape2 = swf_StringToShape2(s,f,l);
680
681     itag->tag = 0;
682     
683     return (PyObject*)tag;
684 }
685 static PyObject* shape_getfillstyles(PyObject*self, PyObject*args)
686 {
687     tag_internals_t*itag = tag_getinternals(self);
688     shape_internal_t*fi = (shape_internal_t*)itag->data;
689     int num = fi->shape2->numfillstyles;
690     return Py_BuildValue("i", num);
691 }
692 static PyObject* shape_getlinestyles(PyObject*self, PyObject*args)
693 {
694     tag_internals_t*itag = tag_getinternals(self);
695     shape_internal_t*fi = (shape_internal_t*)itag->data;
696     int num = fi->shape2->numlinestyles;
697     return Py_BuildValue("i", num);
698 }
699 static PyMethodDef shape_methods[] = 
700 {{"fillstyles", shape_getfillstyles, METH_VARARGS, "get's the number of fillstyles"},
701  {"linestyles", shape_getlinestyles, METH_VARARGS, "get's the number of linestyles"},
702  {NULL, NULL, 0, NULL}
703 };
704
705 static tag_internals_t shape_tag =
706 {
707     parse: 0,
708     fillTAG: shape_fillTAG,
709     dealloc: shape_dealloc,
710     getattr: 0, 
711     setattr: 0,
712     tagfunctions: shape_methods,
713     datasize: sizeof(shape_internal_t),
714 };
715 //----------------------------------------------------------------------------
716
717 typedef struct _videostream_internal
718 {
719     VIDEOSTREAM* stream;
720     int lastiframe;
721 } videostream_internal_t;
722 staticforward tag_internals_t videostream_tag;
723 staticforward tag_internals_t videoframe_tag;
724
725 static int videostream_parse(tag_internals_t*self)
726 {
727     videostream_internal_t*videostream = (videostream_internal_t*)self->data;
728     /* TODO */
729     PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet"));
730     return 0;
731 }
732 static void videostream_dealloc(tag_internals_t*self)
733 {
734     videostream_internal_t*videostream = (videostream_internal_t*)self->data;
735     if(videostream->stream) {
736         swf_VideoStreamClear(videostream->stream);
737         free(videostream->stream);
738         videostream->stream = 0;
739     }
740 }
741 static int videostream_fillTAG(tag_internals_t*self)
742 {
743     videostream_internal_t*fi = (videostream_internal_t*)self->data;
744     if(self->tag)
745         return 1;
746     PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented"));
747     return 0;
748 }
749 static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs)
750 {
751     PyObject*tag = tag_new(&videostream_tag);
752    
753     int width=0,height=0,frames=65535;
754     static char *kwlist[] = {"width", "height", "frames", NULL};
755     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", kwlist, &width, &height, &frames))
756         return NULL;
757
758     tag_internals_t*itag = tag_getinternals(tag);
759     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
760     fi->stream = malloc(sizeof(VIDEOSTREAM));
761     memset(fi->stream, 0, sizeof(VIDEOSTREAM));
762
763     TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
764     swf_SetU16(t, 0); /* id */
765     swf_SetVideoStreamDefine(t, fi->stream, frames, width, height);
766     itag->tag = t;
767     fi->lastiframe = -65536;
768     return (PyObject*)tag;
769 }
770 static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self)
771 {
772     PY_ASSERT_TYPE(self, &TagClass);
773     tag_internals_t*itag = tag_getinternals(self);
774     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
775     return fi->stream;
776 }
777 static PyObject* videostream_getbwidth(PyObject*self, PyObject*args)
778 {
779     tag_internals_t*itag = tag_getinternals(self);
780     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
781     int width = fi->stream->bbx;
782     return Py_BuildValue("i", width);
783 }
784 static PyObject* videostream_getbheight(PyObject*self, PyObject*args)
785 {
786     tag_internals_t*itag = tag_getinternals(self);
787     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
788     int height = fi->stream->bby;
789     return Py_BuildValue("i", height);
790 }
791 static PyObject* videostream_addBlackFrame(PyObject*self, PyObject*args, PyObject*kwargs)
792 {
793     tag_internals_t*_itag = tag_getinternals(self);
794     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
795     
796     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
797     
798     PyObject*tag = tag_new(&videoframe_tag);
799     tag_internals_t*itag = tag_getinternals(tag);
800     
801     swf_SetU16(t,0); /* id */
802     swf_SetVideoStreamBlackFrame(t, fi->stream);
803     fi->lastiframe = fi->stream->frame;
804     
805     itag->tag = t;
806     tagmap_addMapping(itag->tagmap, 0, self);
807     return tag;
808 }
809 static PyObject* videostream_addFrame(PyObject*self, PyObject*args, PyObject*kwargs)
810 {
811     tag_internals_t*_itag = tag_getinternals(self);
812     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
813    
814     PyObject*image = 0;
815     char*type=0; // none, "i", "p"
816     int quant=7;
817     static char *kwlist[] = {"image", "quant", "type", NULL};
818     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|is", kwlist, &image, &quant, &type))
819         return NULL;
820     if(fi->stream->owidth != image_getWidth(image)) {
821         PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getWidth(image), fi->stream->width));return 0;
822     }
823     if(fi->stream->oheight != image_getHeight(image)) {
824         PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getHeight(image), fi->stream->height));return 0;
825     }
826     PyObject*tag = tag_new(&videoframe_tag);
827     tag_internals_t*itag = tag_getinternals(tag);
828
829     RGBA*pic = image_toRGBA(image);
830     if(!pic)
831         return 0;
832
833 /*{  int f,j=0,i=0,rr,gg,bb;
834    FILE *o;
835    RGBA*it = pic;
836    char*filename="test.ppm";
837    printf("Creating %s %dx%d\n",filename, 512,512);
838    o=fopen(filename, "wb");
839    fprintf(o,"P6\n%d %d\n255\n",512, 512);
840    
841    while(j<512*512) {
842     rr=it->r;
843     gg=it->g;
844     bb=it->b;
845     fprintf(o,"%c%c%c",rr,gg,bb);
846     it++;
847     j++;
848    }
849    fclose(o);
850 }*/
851
852     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
853     if((type && (type[0]=='I' || type[0]=='i')) || (type==0 && fi->lastiframe+64 < fi->stream->frame)) {
854         swf_SetU16(t,0); /* id */
855         swf_SetVideoStreamIFrame(t, fi->stream, pic, quant);
856         fi->lastiframe = fi->stream->frame;
857     } else {
858         swf_SetU16(t,0);
859         swf_SetVideoStreamPFrame(t, fi->stream, pic, quant);
860     }
861     itag->tag = t;
862     tagmap_addMapping(itag->tagmap, 0, self);
863     free(pic);
864     return tag;
865 }
866 static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args, PyObject*kwargs)
867 {
868     tag_internals_t*_itag = tag_getinternals(self);
869     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
870     
871     static char *kwlist[] = {"image", "quant", NULL};
872     int quant=7;
873     PyObject* array = 0;
874     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", kwlist, &array, &quant))
875         return NULL;
876
877     signed char* movex = malloc(fi->stream->bbx * fi->stream->bby * 1);
878     signed char* movey = malloc(fi->stream->bbx * fi->stream->bby * 1);
879     RGBA** pics = (RGBA**)malloc(fi->stream->bbx * fi->stream->bby * sizeof(void*));
880     signed char* itx=movex;
881     signed char* ity=movey;
882     RGBA**pic=pics;
883     int x,y;
884     if(!array || !PySequence_Check(array))
885         return PY_ERROR("Not an array");
886     if(PySequence_Length(array) < fi->stream->bby)
887         return PY_ERROR("Array (y) has to have at least %d elements, but has only %d ", fi->stream->bby, PySequence_Length(array));
888     for(y=0;y<fi->stream->bby;y++) {
889         PyObject*line = PySequence_GetItem(array, y);
890         if(!line || !PySequence_Check(line))
891             return PY_ERROR("Not an array of arrays");
892         if(PySequence_Length(line) < fi->stream->bbx)
893             return PY_ERROR("Inner arrays (x) have to be at least %d long- %dth is only %d", fi->stream->bbx, y, PySequence_Length(line));
894
895         for(x=0;x<fi->stream->bbx;x++) {
896             PyObject*pixel = PySequence_GetItem(line, x);
897             PyObject*xy = 0;
898             PyObject*image = 0;
899
900             if(!pixel) {
901                 xy = image = 0;
902             } else {
903                 if(PyComplex_Check(pixel)) {
904                     xy = pixel; image = 0;
905                 } else if(PyString_Check(pixel)) {
906                     xy = 0; image = pixel;
907                 } else if(PyTuple_Check(pixel)) {
908                     int size = PyTuple_GET_SIZE(pixel);
909                     if(size!=2) return PY_ERROR("Tuples have to have size 2 (xy,img)");
910                     xy = PyTuple_GetItem(pixel, 0);
911                     if(!xy) return 0;
912                     if(!PyComplex_Check(xy)) return PY_ERROR("Tuples must be (COMPLEX,string)");
913                     image = PyTuple_GetItem(pixel, 1);
914                     if(!image) return 0;
915                     if(!PyString_Check(image)) return PY_ERROR("Tuples must be (complex,STRING)");
916                 }
917             }
918
919             *itx = *ity = 0;
920             *pic= 0;
921
922             if(xy) {
923                 *itx = (signed char)PyComplex_RealAsDouble(pixel);
924                 *ity = (signed char)PyComplex_ImagAsDouble(pixel);
925             }
926             if(image) {
927                 char*string;
928                 int size;
929                 PyString_AsStringAndSize(image,&string,&size);
930                 if(size<256*3) {
931                     return PY_ERROR("image strings must be >= 256*3");
932                 }
933                 *pic = malloc(sizeof(RGBA)*16*16);
934                 int t;
935                 for(t=0;t<16*16;t++) {
936                     (*pic)[t].r = string[t*3];
937                     (*pic)[t].g = string[t*3+1];
938                     (*pic)[t].b = string[t*3+2];
939                     (*pic)[t].a = 255;
940                 }
941             }
942             itx++;
943             ity++;
944             pic++;
945         }
946     }
947     
948     PyObject*tag = tag_new(&videoframe_tag);
949     tag_internals_t*itag = tag_getinternals(tag);
950     
951     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
952     swf_SetU16(t,0); /* id */
953     swf_SetVideoStreamMover(t, fi->stream, movex, movey,(void**)pics, quant);
954
955     itag->tag = t;
956     tagmap_addMapping(itag->tagmap, 0, self);
957
958     for(x=0;x<fi->stream->bbx*fi->stream->bby;x++) {
959         if(pics[x]) {
960             free(pics[x]);pics[x] = 0;
961         }
962     }
963
964     free(movex);
965     free(movey);
966     free(pics);
967
968     return tag;
969 }
970 static PyMethodDef videostream_methods[] = 
971 {{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"},
972  {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"},
973  {"addFrame", (PyCFunction)videostream_addFrame, METH_KEYWORDS, "add a Video Frame"},
974  {"addBlackFrame", (PyCFunction)videostream_addBlackFrame, METH_KEYWORDS, "add a black Video Frame"},
975  {"addDistortionFrame", (PyCFunction)videostream_addDistortionFrame, METH_KEYWORDS, "add a MVD frame"},
976  {NULL, NULL, 0, NULL}
977 };
978
979 static tag_internals_t videostream_tag =
980 {
981     parse: videostream_parse,
982     fillTAG: videostream_fillTAG,
983     dealloc: videostream_dealloc,
984     getattr: 0, 
985     setattr: 0,
986     tagfunctions: videostream_methods,
987     datasize: sizeof(videostream_internal_t),
988 };
989
990 //============================================================================
991
992 static tag_internals_t videoframe_tag =
993 {
994     parse: 0,
995     fillTAG: 0,
996     dealloc: 0,
997     getattr: 0, 
998     setattr: 0,
999     tagfunctions: 0,
1000     datasize: 0
1001 };
1002
1003 //============================================================================
1004
1005 static PyMethodDef TagMethods[] = 
1006 {
1007     /* TAGS */
1008     {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
1009     {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
1010     {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
1011     {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
1012     {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
1013     {"RemoveObject", (PyCFunction)f_RemoveObject, METH_KEYWORDS, "Create a RemoveObject Tag."},
1014     {"MoveObject", (PyCFunction)f_MoveObject, METH_KEYWORDS, "Create a PlaceObject Move Tag."},
1015     {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."},
1016     {"Image", (PyCFunction)f_DefineImage, METH_KEYWORDS, "Create an SWF Image Tag."},
1017     {"ImageShape", (PyCFunction)f_DefineImageShape, METH_KEYWORDS, "Create an SWF Image Shape Tag."},
1018     {"Shape", (PyCFunction)f_DefineShape, METH_KEYWORDS, "Create an SWF Shape Tag."},
1019     {"ShowFrame", (PyCFunction)f_ShowFrame, METH_KEYWORDS, "Create an SWF Show Frame Tag."},
1020     {"Sprite", (PyCFunction)f_Sprite, METH_KEYWORDS, "Create an SWF Sprite Tag."},
1021     {NULL, NULL, 0, NULL}
1022 };
1023 PyMethodDef* tags_getMethods()
1024 {
1025     TagClass.ob_type = &PyType_Type;
1026     
1027     register_tag(ST_PLACEOBJECT,&placeobject_tag);
1028     register_tag(ST_PLACEOBJECT2,&placeobject_tag);
1029     register_tag(ST_REMOVEOBJECT,&removeobject_tag);
1030     register_tag(ST_REMOVEOBJECT2,&removeobject_tag);
1031     register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag);
1032     register_tag(ST_DEFINEFONT,&font_tag);
1033     register_tag(ST_PROTECT,&protect_tag);
1034     register_tag(ST_DEFINETEXT,&text_tag);
1035     register_tag(ST_DEFINEBITSJPEG,&image_tag);
1036     register_tag(ST_DEFINEBITSJPEG2,&image_tag);
1037     register_tag(ST_DEFINEBITSJPEG3,&image_tag);
1038     register_tag(ST_DEFINEBITSLOSSLESS,&image_tag);
1039     register_tag(ST_DEFINEBITSLOSSLESS2,&image_tag);
1040     register_tag(ST_DEFINESHAPE,&shape_tag);
1041     register_tag(ST_DEFINESHAPE2,&shape_tag);
1042     register_tag(ST_DEFINESHAPE3,&shape_tag);
1043     register_tag(ST_SHOWFRAME,&showframe_tag);
1044     register_tag(ST_DEFINEVIDEOSTREAM,&videostream_tag);
1045     register_tag(ST_VIDEOFRAME,&videoframe_tag);
1046     register_tag(ST_DEFINESPRITE,&sprite_tag);
1047     register_tag(ST_END,&end_tag);
1048
1049     return TagMethods;
1050 }