removed swftagmap from parse function.
[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     PyObject*character;
87 } placeobject_internal_t;
88 staticforward tag_internals_t placeobject_tag;
89
90 static void po_dealloc(tag_internals_t*self)
91 {
92     placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
93     if(pi->po) {
94         swf_PlaceObjectFree(pi->po);
95         pi->po = 0;
96     }
97 }
98 static int po_parse(tag_internals_t*self)
99 {
100     placeobject_internal_t*i = (placeobject_internal_t*)self->data;
101     if(i->po)
102         return 1;
103     if(!self->tag)
104         return 0;
105     SWFPLACEOBJECT* swfpo = malloc(sizeof(SWFPLACEOBJECT));
106     swf_GetPlaceObject(self->tag, swfpo);
107     i->po = swfpo;
108     swf_DeleteTag(self->tag);self->tag = 0;
109         
110     if(i->po->id) {
111         i->character = tagmap_id2obj(self->tagmap, i->po->id);
112         if(i->character) {
113             Py_INCREF(i->character);
114         } else {
115             //PyErr_Clear(); //?
116         }
117     }
118     return 1;
119 }
120 static int po_fillTAG(tag_internals_t*self)
121 {
122     placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
123     self->tag = swf_InsertTag(0, ST_PLACEOBJECT2);
124     swf_SetPlaceObject(self->tag, pi->po);
125     return 1;
126 }
127 static PyObject* po_getattr(tag_internals_t*self,char*a)
128 {
129     placeobject_internal_t*i = (placeobject_internal_t*)self->data;
130     if(!po_parse(self))
131         return PY_ERROR("Couldn't parse placeobject");
132     if(!strcmp(a, "character")) {
133         if(!i->character)
134             return PY_NONE;
135         Py_INCREF(i->character); //TODO: ??
136         return i->character;
137     } else if(!strcmp(a, "matrix")) {
138         return f_Matrix2(&i->po->matrix);
139     } else if(!strcmp(a, "cxform")) {
140         /* TODO */
141         return 0;
142     }
143     return 0;
144 }
145 static int po_setattr(tag_internals_t*self,char*a, PyObject*obj)
146 {
147     placeobject_internal_t*si = (placeobject_internal_t*)self->data;
148     if(!strcmp(a, "cxform")) {
149         /* TODO */
150         return 0;
151     }
152     return -1;
153 }
154 static PyObject* po_create(PyObject* self, PyObject* args, PyObject* kwargs,char move)
155 {
156     static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
157     
158     PyObject*character = 0;
159     int depth;
160     int clipdepth = 0;
161     PyObject*matrix = 0;
162     PyObject*cxform = 0;
163     PyObject*action = 0;
164     int ratio = 0;
165     char* name = 0;
166     SWFPLACEOBJECT* po;
167     po = malloc(sizeof(SWFPLACEOBJECT));
168     memset(po, 0, sizeof(SWFPLACEOBJECT));
169     
170     swf_GetPlaceObject(0, po);
171    
172     PyErr_Clear();
173     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|O!O!isiO!", kwlist, 
174                 &character, 
175                 &depth, 
176                 &MatrixClass, &matrix, 
177                 &CXFormClass, &cxform,
178                 &ratio,
179                 &name,
180                 &clipdepth,
181                 &ActionClass, &action
182                 ))
183         return NULL;
184
185     po->depth = depth;
186     po->clipdepth = clipdepth;
187     po->ratio = ratio;
188     po->name = name;
189     po->move = move;
190     if(clipdepth) po->clipdepth = clipdepth;
191     if(matrix) po->matrix = matrix_getMatrix(matrix);
192     if(cxform) po->cxform = colortransform_getCXForm(cxform);
193     if(action) po->actions = action_getAction(action);
194
195     PyObject*tag;
196     tag = tag_new(&placeobject_tag);
197     tag_internals_t*itag = tag_getinternals(tag);
198     placeobject_internal_t*pi = (placeobject_internal_t*)itag->data;
199     pi->po = po;
200     if(!move) {
201         pi->po->id = tagmap_add(itag->tagmap,(PyObject*)character);
202     } else {
203         pi->po->id = 0;
204     }
205     
206     mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt);
207
208     return (PyObject*)tag;
209 }
210 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
211 {
212     return po_create(self, args, kwargs, 0);
213 }
214 static PyObject* f_MoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
215 {
216     return po_create(self, args, kwargs, 1);
217 }
218 static tag_internals_t placeobject_tag =
219 {
220     parse: po_parse,
221     fillTAG: po_fillTAG,
222     dealloc: po_dealloc,
223     getattr: po_getattr, 
224     setattr: po_setattr,
225     tagfunctions: 0,
226     datasize: sizeof(placeobject_internal_t),
227 };
228 //----------------------------------------------------------------------------
229 staticforward tag_internals_t bgcolor_tag;
230 static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other)
231 {
232     tag_internals_t*itag = tag_getinternals(self);
233     int r,g,b;
234     r = itag->tag->data[0];
235     g = itag->tag->data[1];
236     b = itag->tag->data[2];
237     return Py_BuildValue("(iii)", r,g,b);
238 }
239 static PyMethodDef setbgcolor_methods[] = 
240 {{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"},
241  {NULL, NULL, 0, NULL}
242 };
243 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
244 {
245     static char *kwlist[] = {"color", NULL};
246     int r=0,g=0,b=0;
247     PyObject*tag;
248     PyObject*color;
249     
250     tag = tag_new(&bgcolor_tag);
251     tag_internals_t*itag = tag_getinternals(tag);
252
253     /* 1st try- copy constructor */
254     if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
255         PyErr_Clear();
256         /* 2nd try- color's contructor */
257         color = f_Color(NULL, args, kwargs);
258     }
259     if(!color)
260         return NULL;
261
262     itag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
263     RGBA rgba = color_getRGBA(color);
264     swf_SetU8(itag->tag, rgba.r);
265     swf_SetU8(itag->tag, rgba.g);
266     swf_SetU8(itag->tag, rgba.b);
267     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);
268     Py_DECREF(color);
269     return (PyObject*)tag;
270 }
271 static tag_internals_t bgcolor_tag =
272 {
273     parse: 0,
274     fillTAG: 0,
275     dealloc: 0,
276     getattr: 0, 
277     setattr: 0,
278     tagfunctions: setbgcolor_methods,
279     datasize: 0,
280 };
281 //----------------------------------------------------------------------------
282 staticforward tag_internals_t protect_tag;
283 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
284 {
285     static char *kwlist[] = {"password", NULL};
286     char*password = 0;
287
288     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
289         return NULL;
290
291     PyObject*tag = tag_new(&protect_tag);
292     tag_internals_t*itag = tag_getinternals(tag);
293     itag->tag = swf_InsertTag(0, ST_PROTECT);
294     if(password) {
295         swf_SetPassword(itag->tag, password);
296     }
297     mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt);
298     return (PyObject*)tag;
299 }
300 static tag_internals_t protect_tag =
301 {
302     parse: 0,
303     fillTAG: 0,
304     dealloc: 0,
305     getattr: 0, 
306     setattr: 0,
307     tagfunctions: 0,
308     datasize: 0,
309 };
310 //----------------------------------------------------------------------------
311 staticforward tag_internals_t showframe_tag;
312 static PyObject* f_ShowFrame(PyObject* self, PyObject* args, PyObject* kwargs)
313 {
314     static char *kwlist[] = {"name", NULL};
315     char*name= 0;
316     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
317         return NULL;
318
319     PyObject*tag = tag_new(&showframe_tag);
320     tag_internals_t*itag = tag_getinternals(tag);
321     itag->tag = swf_InsertTag(0, ST_SHOWFRAME);
322     mylog("+%08x(%d) f_ShowFrame", (int)tag, tag->ob_refcnt);
323     return (PyObject*)tag;
324 }
325 static tag_internals_t showframe_tag =
326 {
327     parse: 0,
328     fillTAG: 0,
329     dealloc: 0,
330     getattr: 0, 
331     setattr: 0,
332     tagfunctions: 0,
333     datasize: 0,
334 };
335 //----------------------------------------------------------------------------
336 staticforward tag_internals_t removeobject_tag;
337 static PyObject* f_RemoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
338 {
339     static char *kwlist[] = {"depth", NULL};
340     int depth;
341     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &depth))
342         return NULL;
343
344     PyObject*tag = tag_new(&removeobject_tag);
345     tag_internals_t*itag = tag_getinternals(tag);
346     itag->tag = swf_InsertTag(0, ST_REMOVEOBJECT);
347     swf_SetU16(itag->tag, depth);
348     mylog("+%08x(%d) f_RemoveObject", (int)tag, tag->ob_refcnt);
349     return (PyObject*)tag;
350 }
351 static tag_internals_t removeobject_tag =
352 {
353     parse: 0,
354     fillTAG: 0,
355     dealloc: 0,
356     getattr: 0, 
357     setattr: 0,
358     tagfunctions: 0,
359     datasize: 0,
360 };
361 //----------------------------------------------------------------------------
362 staticforward tag_internals_t sprite_tag;
363 typedef struct _sprite_internal
364 {
365     PyObject* taglist;
366 } sprite_internal_t;
367     
368 static int sprite_fillTAG(tag_internals_t*self)
369 {
370     mylog("+%08x(?) sprite_fillTAG", (int)self);
371
372     sprite_internal_t*si = (sprite_internal_t*)self->data;
373
374     TAG*sprite = swf_InsertTag(0, ST_DEFINESPRITE);
375     swf_SetU16(sprite, 0); //id
376     swf_SetU16(sprite, 0); //frames
377
378     TAG*tag = taglist_getTAGs2(si->taglist, self->tagmap, 0);
379     if(!tag) {
380         /* pass through exception */
381         return 0;
382     }
383     TAG*tag2 = tag;
384     while(tag2->next) tag2 = tag2->next;
385     swf_InsertTag(tag2, ST_END);
386
387     sprite->next = tag;
388     tag->prev = sprite;
389
390     swf_FoldSprite(sprite);
391     self->tag = sprite;
392     return 1;
393 }
394
395 static PyObject* f_Sprite(PyObject* self, PyObject* args, PyObject* kwargs)
396 {
397     static char *kwlist[] = {"name", NULL};
398     char*name= 0;
399     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
400         return NULL;
401
402     PyObject*tag = tag_new(&sprite_tag);
403     tag_internals_t*itag = tag_getinternals(tag);
404     sprite_internal_t*si = (sprite_internal_t*)itag->data;
405     si->taglist = taglist_new();
406     mylog("+%08x(%d) f_DefineSprite", (int)tag, tag->ob_refcnt);
407     return (PyObject*)tag;
408 }
409 static PyObject* sprite_getattr(tag_internals_t*self,char*a)
410 {
411     sprite_internal_t*si = (sprite_internal_t*)self->data;
412     if(!strcmp(a, "tags")) {
413         Py_INCREF(si->taglist);
414         return si->taglist;
415     }
416     return 0;
417 }
418 static int sprite_setattr(tag_internals_t*self,char*a, PyObject*obj)
419 {
420     sprite_internal_t*si = (sprite_internal_t*)self->data;
421     if(self->tag) {
422         swf_DeleteTag(self->tag);
423         self->tag = 0;
424     }
425     if(!strcmp(a, "tags")) {
426         PY_ASSERT_TYPE(obj,&TagListClass);
427         Py_DECREF(si->taglist);
428         si->taglist = obj;
429         Py_INCREF(si->taglist);
430         return 0;
431     }
432     return 1;
433 }
434 static tag_internals_t sprite_tag =
435 {
436     parse: 0,
437     fillTAG: sprite_fillTAG,
438     dealloc: 0,
439     getattr: sprite_getattr, 
440     setattr: sprite_setattr,
441     tagfunctions: 0,
442     datasize: sizeof(sprite_internal_t),
443 };
444 //----------------------------------------------------------------------------
445 staticforward tag_internals_t end_tag;
446 static tag_internals_t end_tag =
447 {
448     parse: 0,
449     fillTAG: 0,
450     dealloc: 0,
451     getattr: 0, 
452     setattr: 0,
453     tagfunctions: 0,
454     datasize: 0,
455 };
456 //----------------------------------------------------------------------------
457 staticforward tag_internals_t text_tag;
458
459 typedef struct _text_internal
460 {
461     char*text;
462     SWFFONT* swffont;
463     RGBA rgba;
464     int size;
465     SRECT bbox;
466 } text_internal_t;
467 staticforward tag_internals_t placeobject_tag;
468
469 static int text_fillTAG(tag_internals_t*self)
470 {
471     text_internal_t*ti = (text_internal_t*)self->data;
472     self->tag= swf_InsertTag(0, ST_DEFINETEXT2);
473     swf_SetU16(self->tag, /*ID*/0);
474     ti->bbox = swf_SetDefineText(self->tag, ti->swffont, &ti->rgba, ti->text, ti->size);
475     return 1;
476 }
477 static PyObject* text_getattr(tag_internals_t*self,char*a)
478 {
479     text_internal_t*si = (text_internal_t*)self->data;
480     if(!strcmp(a, "bbox")) {
481         return f_BBox2(si->bbox);
482     }
483     return 0;
484 }
485 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
486 {
487     static char *kwlist[] = {"font", "text", "size", "color", NULL};
488     PyObject*tag = 0;
489     PyObject*otext;
490     char*text = 0;
491     int size = 0;
492     RGBA rgba = {255,0,0,0};
493     PyObject*color = 0;
494     PyObject*font = 0;
495
496     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!Oi|O!", kwlist, &TagClass, &font, &otext, &size, &ColorClass, &color))
497         return NULL;
498     if(PyUnicode_Check(otext)) {
499         text = PyString_AS_STRING(PyUnicode_AsUTF8String(otext));
500     } else if(PyString_Check(otext)) {
501         text = PyString_AS_STRING(otext);
502     }
503
504     if(color)
505         rgba = color_getRGBA(color);
506
507     mylog("DefineText: text = %s", text);
508     
509     tag = tag_new(&text_tag);
510     tag_internals_t* itag = tag_getinternals(tag);
511     text_internal_t*ti = (text_internal_t*)itag->data;
512
513     ti->swffont = font_getSWFFONT(font);
514     int font_id = tagmap_add(itag->tagmap, font); // add dependency on font
515     ti->swffont->id = font_id; // for swf_SetDefineTexts
516     ti->text = strdup(text);
517     ti->rgba = rgba;
518     ti->size = size;
519
520     return (PyObject*)tag;
521 }
522 static tag_internals_t text_tag =
523 {
524     parse: 0,
525     fillTAG: text_fillTAG,
526     dealloc: 0,
527     getattr: text_getattr, 
528     setattr: 0,
529     tagfunctions: 0,
530     datasize: sizeof(text_internal_t),
531 };
532 //----------------------------------------------------------------------------
533 staticforward tag_internals_t image_tag;
534
535 typedef struct _image_internal
536 {
537     RGBA*rgba;
538     int size;
539     int width;
540     int height;
541     int bpp;
542     char isindexed;
543     char islossless;
544 } image_internal_t;
545 staticforward tag_internals_t image_tag;
546
547 static int image_fillTAG(tag_internals_t*self)
548 {
549     image_internal_t*ti = (image_internal_t*)self->data;
550     self->tag= swf_InsertTag(0, ST_DEFINEBITSLOSSLESS2);
551     swf_SetU16(self->tag, /*ID*/0);
552     swf_SetLosslessBits(self->tag, ti->width, ti->height, ti->rgba, BMF_32BIT);
553     return 1;
554 }
555 static void image_dealloc(tag_internals_t*self)
556 {
557     image_internal_t*pi = (image_internal_t*)self->data;
558     if(pi->rgba) {
559         free(pi->rgba);pi->rgba = 0;
560     }
561 }
562 static int imagetag_getWidth(PyObject* self)
563 {
564     tag_internals_t*itag = tag_getinternals(self);
565     image_internal_t*pi = (image_internal_t*)itag->data;
566     return pi->width;
567 }
568 static int imagetag_getHeight(PyObject* self)
569 {
570     tag_internals_t*itag = tag_getinternals(self);
571     image_internal_t*pi = (image_internal_t*)itag->data;
572     return pi->height;
573 }
574 static PyObject* f_DefineImage(PyObject* self, PyObject* args, PyObject* kwargs)
575 {
576     static char *kwlist[] = {"image", NULL};
577     PyObject*image = 0;
578     PyObject*tag = tag_new(&image_tag);
579
580     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &image))
581         return NULL;
582     
583     tag = tag_new(&image_tag);
584     tag_internals_t* itag = tag_getinternals(tag);
585     image_internal_t*ti = (image_internal_t*)itag->data;
586
587     ti->rgba = image_toRGBA(image);
588     if(!ti->rgba) // pass through exception
589         return 0;
590     ti->width = image_getWidth(image);
591     ti->height = image_getHeight(image);
592     ti->isindexed = 0;
593     ti->islossless = 1;
594     ti->bpp = 32;
595     ti->size = ti->width*ti->height;
596
597     return (PyObject*)tag;
598 }
599 static tag_internals_t image_tag =
600 {
601     parse: 0,
602     fillTAG: image_fillTAG,
603     dealloc: image_dealloc,
604     getattr: 0, 
605     setattr: 0,
606     tagfunctions: 0,
607     datasize: sizeof(image_internal_t),
608 };
609 //----------------------------------------------------------------------------
610 staticforward tag_internals_t shape_tag;
611
612 typedef struct _shape_internal
613 {
614     SHAPE2*shape2;
615 } shape_internal_t;
616 staticforward tag_internals_t shape_tag;
617
618 static int shape_fillTAG(tag_internals_t*self)
619 {
620     shape_internal_t*ti = (shape_internal_t*)self->data;
621     self->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
622     swf_SetU16(self->tag, /*ID*/0);
623     swf_SetShape2(self->tag, ti->shape2);
624     return 1;
625 }
626 static int shape_parse(tag_internals_t*self)
627 {
628     shape_internal_t*i= (shape_internal_t*)self->data;
629     if(i->shape2)
630         return 1;
631     if(!self->tag)
632         return 0;
633     SHAPE2* shape2 = malloc(sizeof(SHAPE2));
634     swf_ParseDefineShape(self->tag, shape2);
635     i->shape2 = shape2;
636     swf_DeleteTag(self->tag);self->tag = 0;
637     return 1;
638 }
639 static void shape_dealloc(tag_internals_t*self)
640 {
641     shape_internal_t*pi = (shape_internal_t*)self->data;
642     if(pi->shape2) {
643         swf_Shape2Free(pi->shape2);
644         pi->shape2 = 0;
645     }
646 }
647 static PyObject* f_DefineImageShape(PyObject* self, PyObject* args, PyObject* kwargs)
648 {
649     static char *kwlist[] = {"image", NULL};
650     PyObject*image = 0;
651
652     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &TagClass, &image))
653         return NULL;
654     
655     PyObject*tag = tag_new(&shape_tag);
656     tag_internals_t* itag = tag_getinternals(tag);
657     shape_internal_t*ti = (shape_internal_t*)itag->data;
658     ti->shape2 = 0; /*HACK*/
659
660     int width = imagetag_getWidth(image);
661     int height = imagetag_getHeight(image);
662     int id = tagmap_add(itag->tagmap, image);
663     itag->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
664     swf_SetU16(itag->tag, 0);
665     swf_ShapeSetBitmapRect(itag->tag, id, width, height);
666     return (PyObject*)tag;
667 }
668
669 /* TODO: move to lib/ */
670 SHAPE2*swf_StringToShape2(char*s,FILLSTYLE*f, LINESTYLE*l)
671 {
672     drawer_t draw;
673     swf_Shape11DrawerInit(&draw, 0);
674     draw_string(&draw, s);
675     draw.finish(&draw);
676     SHAPE*s1 =  swf_ShapeDrawerToShape(&draw);
677     SRECT r = swf_ShapeDrawerGetBBox(&draw);
678     RGBA col;col.r=col.g=col.b=128;col.a=255;
679     if(l) 
680         swf_ShapeAddLineStyle(s1, 1, &col);
681     if(f)
682         swf_ShapeAddSolidFillStyle(s1, &col);
683     draw.dealloc(&draw);
684     SHAPE2*shape2 = swf_ShapeToShape2(s1);
685     swf_ShapeFree(s1);
686     shape2->bbox = malloc(sizeof(SRECT));
687     *(shape2->bbox) = r;
688     if(f && shape2->numfillstyles)
689         shape2->fillstyles[0] = *f;
690     if(l && shape2->numlinestyles)
691         shape2->linestyles[0] = *l;
692     return shape2;
693 }
694
695 static PyObject* f_DefineShape(PyObject* self, PyObject* args, PyObject* kwargs)
696 {
697     static char *kwlist[] = {"s", "fill", "line", NULL};
698     char*s = 0;
699     PyObject*fillstyle=0,*linestyle=0;
700
701     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OO", kwlist, &s,&fillstyle,&linestyle))
702         return NULL;
703
704     PyObject*tag = tag_new(&shape_tag);
705     tag_internals_t* itag = tag_getinternals(tag);
706     shape_internal_t*ti = (shape_internal_t*)itag->data;
707     
708     FILLSTYLE _f,*f=0;
709     LINESTYLE _l,*l=0;
710
711     if(fillstyle) {
712         f = &_f;
713         if(PY_CHECK_TYPE(fillstyle, &ColorClass)) {
714             f->type = /*FILL_SOLID*/ 0;
715             f->color = color_getRGBA(fillstyle);
716         } else {
717             return PY_ERROR("Invalid Fillstyle");
718         }
719     }
720
721     if(linestyle) {
722         l = &_l;
723         if(PyTuple_Check(linestyle) && PyTuple_GET_SIZE(linestyle)==2) {
724             float f = 0.0;
725             PyObject*color = 0;
726             if(!PyArg_ParseTuple(linestyle, "fO!", &f, &ColorClass, &color))
727                 return 0;
728
729             l->width = (int)(f*20);
730             l->color = color_getRGBA(color);
731         } else {
732             return PY_ERROR("Invalid Linestyle");
733         }
734     }
735     ti->shape2 = swf_StringToShape2(s,f,l);
736
737     itag->tag = 0;
738     
739     return (PyObject*)tag;
740 }
741 static PyObject* shape_getfillstyles(PyObject*self, PyObject*args)
742 {
743     tag_internals_t*itag = tag_getinternals(self);
744     if(!shape_parse(itag))
745         return PY_ERROR("Couldn't parse shape");
746     shape_internal_t*fi = (shape_internal_t*)itag->data;
747     int num = fi->shape2->numfillstyles;
748     return Py_BuildValue("i", num);
749 }
750 static PyObject* shape_getlinestyles(PyObject*self, PyObject*args)
751 {
752     tag_internals_t*itag = tag_getinternals(self);
753     if(!shape_parse(itag))
754         return PY_ERROR("Couldn't parse shape");
755     shape_internal_t*fi = (shape_internal_t*)itag->data;
756     int num = fi->shape2->numlinestyles;
757     return Py_BuildValue("i", num);
758 }
759 static PyObject* shape_getfillstyle(PyObject*self, PyObject*args)
760 {
761     tag_internals_t*itag = tag_getinternals(self);
762     if(!shape_parse(itag))
763         return PY_ERROR("Couldn't parse shape");
764     shape_internal_t*fi = (shape_internal_t*)itag->data;
765     int nr = 0;
766     if(!PyArg_ParseTuple(args, "i", &nr))
767         return NULL;
768     
769     int num = fi->shape2->numfillstyles;
770     if(nr < 0 || nr >=num)
771         return PY_ERROR("fillstyle index out of range");
772     return f_FillStyle2(fi->shape2->fillstyles[nr]);
773 }
774 static PyObject* shape_getlinestyle(PyObject*self, PyObject*args)
775 {
776     tag_internals_t*itag = tag_getinternals(self);
777     if(!shape_parse(itag))
778         return PY_ERROR("Couldn't parse shape");
779     shape_internal_t*fi = (shape_internal_t*)itag->data;
780     int nr = 0;
781     if(!PyArg_ParseTuple(args, "i", &nr))
782         return NULL;
783     
784     int num = fi->shape2->numfillstyles;
785     if(nr < 0 || nr >=num)
786         return PY_ERROR("fillstyle index out of range");
787     return f_LineStyle3(fi->shape2->linestyles[nr]);
788 }
789 static PyObject* shape_setfillstyle(PyObject*self, PyObject*args)
790 {
791     tag_internals_t*itag = tag_getinternals(self);
792     if(!shape_parse(itag))
793         return PY_ERROR("Couldn't parse shape");
794     shape_internal_t*fi = (shape_internal_t*)itag->data;
795     int nr = 0;
796     PyObject*fs = 0;
797     if(!PyArg_ParseTuple(args, "iO!", &nr, &FillStyleClass, &fs))
798         return NULL;
799     
800     int num = fi->shape2->numfillstyles;
801     if(nr < 0 || nr >=num)
802         return PY_ERROR("fillstyle index out of range");
803     fi->shape2->fillstyles[nr] = fillstyle_getFillStyle(fs);
804     return PY_NONE;
805 }
806 static PyObject* shape_setlinestyle(PyObject*self, PyObject*args)
807 {
808     tag_internals_t*itag = tag_getinternals(self);
809     if(!shape_parse(itag))
810         return PY_ERROR("Couldn't parse shape");
811     shape_internal_t*fi = (shape_internal_t*)itag->data;
812     int nr = 0;
813     PyObject*ls = 0;
814     if(!PyArg_ParseTuple(args, "iO!", &nr, &LineStyleClass, &ls))
815         return NULL;
816     
817     int num = fi->shape2->numlinestyles;
818     if(nr < 0 || nr >=num)
819         return PY_ERROR("linestyle index out of range");
820     fi->shape2->linestyles[nr] = linestyle_getLineStyle(ls);
821     return PY_NONE;
822 }
823 static PyMethodDef shape_methods[] = 
824 {{"numfillstyles", shape_getfillstyles, METH_VARARGS, "get's the number of fillstyles"},
825  {"numlinestyles", shape_getlinestyles, METH_VARARGS, "get's the number of linestyles"},
826  {"getfillstyle", shape_getfillstyle, METH_VARARGS, "get's one fillstyle"},
827  {"getlinestyle", shape_getlinestyle, METH_VARARGS, "get's one linestyle"},
828  {"setfillstyle", shape_setfillstyle, METH_VARARGS, "set's one fillstyle"},
829  {"setlinestyle", shape_setlinestyle, METH_VARARGS, "set's one linestyle"},
830  {NULL, NULL, 0, NULL}
831 };
832
833 static tag_internals_t shape_tag =
834 {
835     parse: shape_parse,
836     fillTAG: shape_fillTAG,
837     dealloc: shape_dealloc,
838     getattr: 0, 
839     setattr: 0,
840     tagfunctions: shape_methods,
841     datasize: sizeof(shape_internal_t),
842 };
843 //----------------------------------------------------------------------------
844
845 typedef struct _videostream_internal
846 {
847     VIDEOSTREAM* stream;
848     int lastiframe;
849 } videostream_internal_t;
850 staticforward tag_internals_t videostream_tag;
851 staticforward tag_internals_t videoframe_tag;
852
853 static int videostream_parse(tag_internals_t*self)
854 {
855     videostream_internal_t*videostream = (videostream_internal_t*)self->data;
856     /* TODO */
857     PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet"));
858     return 0;
859 }
860 static void videostream_dealloc(tag_internals_t*self)
861 {
862     videostream_internal_t*videostream = (videostream_internal_t*)self->data;
863     if(videostream->stream) {
864         swf_VideoStreamClear(videostream->stream);
865         free(videostream->stream);
866         videostream->stream = 0;
867     }
868 }
869 static int videostream_fillTAG(tag_internals_t*self)
870 {
871     videostream_internal_t*fi = (videostream_internal_t*)self->data;
872     if(self->tag)
873         return 1;
874     PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented"));
875     return 0;
876 }
877 static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs)
878 {
879     PyObject*tag = tag_new(&videostream_tag);
880    
881     int width=0,height=0,frames=65535;
882     static char *kwlist[] = {"width", "height", "frames", NULL};
883     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", kwlist, &width, &height, &frames))
884         return NULL;
885
886     tag_internals_t*itag = tag_getinternals(tag);
887     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
888     fi->stream = malloc(sizeof(VIDEOSTREAM));
889     memset(fi->stream, 0, sizeof(VIDEOSTREAM));
890
891     TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
892     swf_SetU16(t, 0); /* id */
893     swf_SetVideoStreamDefine(t, fi->stream, frames, width, height);
894     itag->tag = t;
895     fi->lastiframe = -65536;
896     return (PyObject*)tag;
897 }
898 static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self)
899 {
900     PY_ASSERT_TYPE(self, &TagClass);
901     tag_internals_t*itag = tag_getinternals(self);
902     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
903     return fi->stream;
904 }
905 static PyObject* videostream_getbwidth(PyObject*self, PyObject*args)
906 {
907     tag_internals_t*itag = tag_getinternals(self);
908     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
909     int width = fi->stream->bbx;
910     return Py_BuildValue("i", width);
911 }
912 static PyObject* videostream_getbheight(PyObject*self, PyObject*args)
913 {
914     tag_internals_t*itag = tag_getinternals(self);
915     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
916     int height = fi->stream->bby;
917     return Py_BuildValue("i", height);
918 }
919 static PyObject* videostream_addBlackFrame(PyObject*self, PyObject*args, PyObject*kwargs)
920 {
921     tag_internals_t*_itag = tag_getinternals(self);
922     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
923     
924     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
925     
926     PyObject*tag = tag_new(&videoframe_tag);
927     tag_internals_t*itag = tag_getinternals(tag);
928     
929     swf_SetU16(t,0); /* id */
930     swf_SetVideoStreamBlackFrame(t, fi->stream);
931     fi->lastiframe = fi->stream->frame;
932     
933     itag->tag = t;
934     tagmap_addMapping(itag->tagmap, 0, self);
935     return tag;
936 }
937 static PyObject* videostream_addFrame(PyObject*self, PyObject*args, PyObject*kwargs)
938 {
939     tag_internals_t*_itag = tag_getinternals(self);
940     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
941    
942     PyObject*image = 0;
943     char*type=0; // none, "i", "p"
944     int quant=7;
945     static char *kwlist[] = {"image", "quant", "type", NULL};
946     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|is", kwlist, &image, &quant, &type))
947         return NULL;
948     if(fi->stream->owidth != image_getWidth(image)) {
949         PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getWidth(image), fi->stream->width));return 0;
950     }
951     if(fi->stream->oheight != image_getHeight(image)) {
952         PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getHeight(image), fi->stream->height));return 0;
953     }
954     PyObject*tag = tag_new(&videoframe_tag);
955     tag_internals_t*itag = tag_getinternals(tag);
956
957     RGBA*pic = image_toRGBA(image);
958     if(!pic)
959         return 0;
960
961 /*{  int f,j=0,i=0,rr,gg,bb;
962    FILE *o;
963    RGBA*it = pic;
964    char*filename="test.ppm";
965    printf("Creating %s %dx%d\n",filename, 512,512);
966    o=fopen(filename, "wb");
967    fprintf(o,"P6\n%d %d\n255\n",512, 512);
968    
969    while(j<512*512) {
970     rr=it->r;
971     gg=it->g;
972     bb=it->b;
973     fprintf(o,"%c%c%c",rr,gg,bb);
974     it++;
975     j++;
976    }
977    fclose(o);
978 }*/
979
980     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
981     if((type && (type[0]=='I' || type[0]=='i')) || (type==0 && fi->lastiframe+64 < fi->stream->frame)) {
982         swf_SetU16(t,0); /* id */
983         swf_SetVideoStreamIFrame(t, fi->stream, pic, quant);
984         fi->lastiframe = fi->stream->frame;
985     } else {
986         swf_SetU16(t,0);
987         swf_SetVideoStreamPFrame(t, fi->stream, pic, quant);
988     }
989     itag->tag = t;
990     tagmap_addMapping(itag->tagmap, 0, self);
991     free(pic);
992     return tag;
993 }
994 static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args, PyObject*kwargs)
995 {
996     tag_internals_t*_itag = tag_getinternals(self);
997     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
998     
999     static char *kwlist[] = {"image", "quant", NULL};
1000     int quant=7;
1001     PyObject* array = 0;
1002     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", kwlist, &array, &quant))
1003         return NULL;
1004
1005     signed char* movex = malloc(fi->stream->bbx * fi->stream->bby * 1);
1006     signed char* movey = malloc(fi->stream->bbx * fi->stream->bby * 1);
1007     RGBA** pics = (RGBA**)malloc(fi->stream->bbx * fi->stream->bby * sizeof(void*));
1008     signed char* itx=movex;
1009     signed char* ity=movey;
1010     RGBA**pic=pics;
1011     int x,y;
1012     if(!array || !PySequence_Check(array))
1013         return PY_ERROR("Not an array");
1014     if(PySequence_Length(array) < fi->stream->bby)
1015         return PY_ERROR("Array (y) has to have at least %d elements, but has only %d ", fi->stream->bby, PySequence_Length(array));
1016     for(y=0;y<fi->stream->bby;y++) {
1017         PyObject*line = PySequence_GetItem(array, y);
1018         if(!line || !PySequence_Check(line))
1019             return PY_ERROR("Not an array of arrays");
1020         if(PySequence_Length(line) < fi->stream->bbx)
1021             return PY_ERROR("Inner arrays (x) have to be at least %d long- %dth is only %d", fi->stream->bbx, y, PySequence_Length(line));
1022
1023         for(x=0;x<fi->stream->bbx;x++) {
1024             PyObject*pixel = PySequence_GetItem(line, x);
1025             PyObject*xy = 0;
1026             PyObject*image = 0;
1027
1028             if(!pixel) {
1029                 xy = image = 0;
1030             } else {
1031                 if(PyComplex_Check(pixel)) {
1032                     xy = pixel; image = 0;
1033                 } else if(PyString_Check(pixel)) {
1034                     xy = 0; image = pixel;
1035                 } else if(PyTuple_Check(pixel)) {
1036                     int size = PyTuple_GET_SIZE(pixel);
1037                     if(size!=2) return PY_ERROR("Tuples have to have size 2 (xy,img)");
1038                     xy = PyTuple_GetItem(pixel, 0);
1039                     if(!xy) return 0;
1040                     if(!PyComplex_Check(xy)) return PY_ERROR("Tuples must be (COMPLEX,string)");
1041                     image = PyTuple_GetItem(pixel, 1);
1042                     if(!image) return 0;
1043                     if(!PyString_Check(image)) return PY_ERROR("Tuples must be (complex,STRING)");
1044                 }
1045             }
1046
1047             *itx = *ity = 0;
1048             *pic= 0;
1049
1050             if(xy) {
1051                 *itx = (signed char)PyComplex_RealAsDouble(pixel);
1052                 *ity = (signed char)PyComplex_ImagAsDouble(pixel);
1053             }
1054             if(image) {
1055                 char*string;
1056                 int size;
1057                 PyString_AsStringAndSize(image,&string,&size);
1058                 if(size<256*3) {
1059                     return PY_ERROR("image strings must be >= 256*3");
1060                 }
1061                 *pic = malloc(sizeof(RGBA)*16*16);
1062                 int t;
1063                 for(t=0;t<16*16;t++) {
1064                     (*pic)[t].r = string[t*3];
1065                     (*pic)[t].g = string[t*3+1];
1066                     (*pic)[t].b = string[t*3+2];
1067                     (*pic)[t].a = 255;
1068                 }
1069             }
1070             itx++;
1071             ity++;
1072             pic++;
1073         }
1074     }
1075     
1076     PyObject*tag = tag_new(&videoframe_tag);
1077     tag_internals_t*itag = tag_getinternals(tag);
1078     
1079     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
1080     swf_SetU16(t,0); /* id */
1081     swf_SetVideoStreamMover(t, fi->stream, movex, movey,(void**)pics, quant);
1082
1083     itag->tag = t;
1084     tagmap_addMapping(itag->tagmap, 0, self);
1085
1086     for(x=0;x<fi->stream->bbx*fi->stream->bby;x++) {
1087         if(pics[x]) {
1088             free(pics[x]);pics[x] = 0;
1089         }
1090     }
1091
1092     free(movex);
1093     free(movey);
1094     free(pics);
1095
1096     return tag;
1097 }
1098 static PyMethodDef videostream_methods[] = 
1099 {{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"},
1100  {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"},
1101  {"addFrame", (PyCFunction)videostream_addFrame, METH_KEYWORDS, "add a Video Frame"},
1102  {"addBlackFrame", (PyCFunction)videostream_addBlackFrame, METH_KEYWORDS, "add a black Video Frame"},
1103  {"addDistortionFrame", (PyCFunction)videostream_addDistortionFrame, METH_KEYWORDS, "add a MVD frame"},
1104  {NULL, NULL, 0, NULL}
1105 };
1106
1107 static tag_internals_t videostream_tag =
1108 {
1109     parse: videostream_parse,
1110     fillTAG: videostream_fillTAG,
1111     dealloc: videostream_dealloc,
1112     getattr: 0, 
1113     setattr: 0,
1114     tagfunctions: videostream_methods,
1115     datasize: sizeof(videostream_internal_t),
1116 };
1117
1118 //============================================================================
1119
1120 static tag_internals_t videoframe_tag =
1121 {
1122     parse: 0,
1123     fillTAG: 0,
1124     dealloc: 0,
1125     getattr: 0, 
1126     setattr: 0,
1127     tagfunctions: 0,
1128     datasize: 0
1129 };
1130
1131 //============================================================================
1132 static PyMethodDef TagMethods[] = 
1133 {
1134     /* TAGS */
1135     {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
1136     {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
1137     {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
1138     {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
1139     {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
1140     {"RemoveObject", (PyCFunction)f_RemoveObject, METH_KEYWORDS, "Create a RemoveObject Tag."},
1141     {"MoveObject", (PyCFunction)f_MoveObject, METH_KEYWORDS, "Create a PlaceObject Move Tag."},
1142     {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."},
1143     {"Image", (PyCFunction)f_DefineImage, METH_KEYWORDS, "Create an SWF Image Tag."},
1144     {"ImageShape", (PyCFunction)f_DefineImageShape, METH_KEYWORDS, "Create an SWF Image Shape Tag."},
1145     {"Shape", (PyCFunction)f_DefineShape, METH_KEYWORDS, "Create an SWF Shape Tag."},
1146     {"ShowFrame", (PyCFunction)f_ShowFrame, METH_KEYWORDS, "Create an SWF Show Frame Tag."},
1147     {"Sprite", (PyCFunction)f_Sprite, METH_KEYWORDS, "Create an SWF Sprite Tag."},
1148     
1149     {NULL, NULL, 0, NULL}
1150 };
1151 PyMethodDef* tags_getMethods()
1152 {
1153     TagClass.ob_type = &PyType_Type;
1154     
1155     register_tag(ST_PLACEOBJECT,&placeobject_tag);
1156     register_tag(ST_PLACEOBJECT2,&placeobject_tag);
1157     register_tag(ST_REMOVEOBJECT,&removeobject_tag);
1158     register_tag(ST_REMOVEOBJECT2,&removeobject_tag);
1159     register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag);
1160     register_tag(ST_DEFINEFONT,&font_tag);
1161     register_tag(ST_PROTECT,&protect_tag);
1162     register_tag(ST_DEFINETEXT,&text_tag);
1163     register_tag(ST_DEFINEBITSJPEG,&image_tag);
1164     register_tag(ST_DEFINEBITSJPEG2,&image_tag);
1165     register_tag(ST_DEFINEBITSJPEG3,&image_tag);
1166     register_tag(ST_DEFINEBITSLOSSLESS,&image_tag);
1167     register_tag(ST_DEFINEBITSLOSSLESS2,&image_tag);
1168     register_tag(ST_DEFINESHAPE,&shape_tag);
1169     register_tag(ST_DEFINESHAPE2,&shape_tag);
1170     register_tag(ST_DEFINESHAPE3,&shape_tag);
1171     register_tag(ST_SHOWFRAME,&showframe_tag);
1172     register_tag(ST_DEFINEVIDEOSTREAM,&videostream_tag);
1173     register_tag(ST_VIDEOFRAME,&videoframe_tag);
1174     register_tag(ST_DEFINESPRITE,&sprite_tag);
1175     register_tag(ST_END,&end_tag);
1176
1177     return TagMethods;
1178 }