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