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