image tags now have an "image" attribute
[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 image_parse(tag_internals_t*self)
563 {
564     image_internal_t*i= (image_internal_t*)self->data;
565     if(i->rgba)
566         return 1;
567     if(!self->tag)
568         return 0;
569    
570     i->rgba = swf_ExtractImage(self->tag, &i->width, &i->height);
571     i->bpp = 32;
572     i->isindexed = 0;
573     i->islossless = 1;
574
575     swf_DeleteTag(self->tag);self->tag = 0;
576     return 1;
577 }
578 static int imagetag_getWidth(PyObject* self)
579 {
580     tag_internals_t*itag = tag_getinternals(self);
581     image_internal_t*pi = (image_internal_t*)itag->data;
582     return pi->width;
583 }
584 static int imagetag_getHeight(PyObject* self)
585 {
586     tag_internals_t*itag = tag_getinternals(self);
587     image_internal_t*pi = (image_internal_t*)itag->data;
588     return pi->height;
589 }
590 static PyObject* f_DefineImage(PyObject* self, PyObject* args, PyObject* kwargs)
591 {
592     static char *kwlist[] = {"image", NULL};
593     PyObject*image = 0;
594     PyObject*tag = tag_new(&image_tag);
595
596     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &image))
597         return NULL;
598     
599     tag = tag_new(&image_tag);
600     tag_internals_t* itag = tag_getinternals(tag);
601     image_internal_t*ti = (image_internal_t*)itag->data;
602
603     ti->rgba = image_toRGBA(image);
604     if(!ti->rgba) // pass through exception
605         return 0;
606     ti->width = image_getWidth(image);
607     ti->height = image_getHeight(image);
608     ti->isindexed = 0;
609     ti->islossless = 1;
610     ti->bpp = 32;
611     ti->size = ti->width*ti->height;
612
613     return (PyObject*)tag;
614 }
615 static PyObject* image_getattr(tag_internals_t*self,char*a)
616 {
617     image_internal_t*i = (image_internal_t*)self->data;
618     if(!strcmp(a, "image")) {
619         if(!i->rgba) {
620             image_parse(self);
621         }
622         PyObject* image = rgba_to_image(i->rgba, i->width, i->height);
623         return image;
624     }
625     return 0;
626 }
627 static tag_internals_t image_tag =
628 {
629     parse: image_parse,
630     fillTAG: image_fillTAG,
631     dealloc: image_dealloc,
632     getattr: image_getattr, 
633     setattr: 0,
634     tagfunctions: 0,
635     datasize: sizeof(image_internal_t),
636 };
637 //----------------------------------------------------------------------------
638 staticforward tag_internals_t shape_tag;
639
640 typedef struct _shape_internal
641 {
642     SHAPE2*shape2;
643 } shape_internal_t;
644 staticforward tag_internals_t shape_tag;
645
646 static int shape_fillTAG(tag_internals_t*self)
647 {
648     shape_internal_t*ti = (shape_internal_t*)self->data;
649     self->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
650     swf_SetU16(self->tag, /*ID*/0);
651     swf_SetShape2(self->tag, ti->shape2);
652     return 1;
653 }
654 static int shape_parse(tag_internals_t*self)
655 {
656     shape_internal_t*i= (shape_internal_t*)self->data;
657     if(i->shape2)
658         return 1;
659     if(!self->tag)
660         return 0;
661     SHAPE2* shape2 = malloc(sizeof(SHAPE2));
662     swf_ParseDefineShape(self->tag, shape2);
663     i->shape2 = shape2;
664     swf_DeleteTag(self->tag);self->tag = 0;
665     return 1;
666 }
667 static void shape_dealloc(tag_internals_t*self)
668 {
669     shape_internal_t*pi = (shape_internal_t*)self->data;
670     if(pi->shape2) {
671         swf_Shape2Free(pi->shape2);
672         pi->shape2 = 0;
673     }
674 }
675 static PyObject* f_DefineImageShape(PyObject* self, PyObject* args, PyObject* kwargs)
676 {
677     static char *kwlist[] = {"image", NULL};
678     PyObject*image = 0;
679
680     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &TagClass, &image))
681         return NULL;
682     
683     PyObject*tag = tag_new(&shape_tag);
684     tag_internals_t* itag = tag_getinternals(tag);
685     shape_internal_t*ti = (shape_internal_t*)itag->data;
686     ti->shape2 = 0; /*HACK*/
687
688     int width = imagetag_getWidth(image);
689     int height = imagetag_getHeight(image);
690     int id = tagmap_add(itag->tagmap, image);
691     itag->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
692     swf_SetU16(itag->tag, 0);
693     swf_ShapeSetBitmapRect(itag->tag, id, width, height);
694     return (PyObject*)tag;
695 }
696
697 /* TODO: move to lib/ */
698 SHAPE2*swf_StringToShape2(char*s,FILLSTYLE*f, LINESTYLE*l)
699 {
700     drawer_t draw;
701     swf_Shape11DrawerInit(&draw, 0);
702     draw_string(&draw, s);
703     draw.finish(&draw);
704     SHAPE*s1 =  swf_ShapeDrawerToShape(&draw);
705     SRECT r = swf_ShapeDrawerGetBBox(&draw);
706     RGBA col;col.r=col.g=col.b=128;col.a=255;
707     if(l) 
708         swf_ShapeAddLineStyle(s1, 1, &col);
709     if(f)
710         swf_ShapeAddSolidFillStyle(s1, &col);
711     draw.dealloc(&draw);
712     SHAPE2*shape2 = swf_ShapeToShape2(s1);
713     swf_ShapeFree(s1);
714     shape2->bbox = malloc(sizeof(SRECT));
715     *(shape2->bbox) = r;
716     if(f && shape2->numfillstyles)
717         shape2->fillstyles[0] = *f;
718     if(l && shape2->numlinestyles)
719         shape2->linestyles[0] = *l;
720     return shape2;
721 }
722
723 static PyObject* f_DefineShape(PyObject* self, PyObject* args, PyObject* kwargs)
724 {
725     static char *kwlist[] = {"s", "fill", "line", NULL};
726     char*s = 0;
727     PyObject*fillstyle=0,*linestyle=0;
728
729     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OO", kwlist, &s,&fillstyle,&linestyle))
730         return NULL;
731
732     PyObject*tag = tag_new(&shape_tag);
733     tag_internals_t* itag = tag_getinternals(tag);
734     shape_internal_t*ti = (shape_internal_t*)itag->data;
735     
736     FILLSTYLE _f,*f=0;
737     LINESTYLE _l,*l=0;
738
739     if(fillstyle) {
740         f = &_f;
741         if(PY_CHECK_TYPE(fillstyle, &ColorClass)) {
742             f->type = /*FILL_SOLID*/ 0;
743             f->color = color_getRGBA(fillstyle);
744         } else {
745             return PY_ERROR("Invalid Fillstyle");
746         }
747     }
748
749     if(linestyle) {
750         l = &_l;
751         if(PyTuple_Check(linestyle) && PyTuple_GET_SIZE(linestyle)==2) {
752             float f = 0.0;
753             PyObject*color = 0;
754             if(!PyArg_ParseTuple(linestyle, "fO!", &f, &ColorClass, &color))
755                 return 0;
756
757             l->width = (int)(f*20);
758             l->color = color_getRGBA(color);
759         } else {
760             return PY_ERROR("Invalid Linestyle");
761         }
762     }
763     ti->shape2 = swf_StringToShape2(s,f,l);
764
765     itag->tag = 0;
766     
767     return (PyObject*)tag;
768 }
769 static PyObject* shape_getfillstyles(PyObject*self, PyObject*args)
770 {
771     tag_internals_t*itag = tag_getinternals(self);
772     if(!shape_parse(itag))
773         return PY_ERROR("Couldn't parse shape");
774     shape_internal_t*fi = (shape_internal_t*)itag->data;
775     int num = fi->shape2->numfillstyles;
776     return Py_BuildValue("i", num);
777 }
778 static PyObject* shape_getlinestyles(PyObject*self, PyObject*args)
779 {
780     tag_internals_t*itag = tag_getinternals(self);
781     if(!shape_parse(itag))
782         return PY_ERROR("Couldn't parse shape");
783     shape_internal_t*fi = (shape_internal_t*)itag->data;
784     int num = fi->shape2->numlinestyles;
785     return Py_BuildValue("i", num);
786 }
787 static PyObject* shape_getfillstyle(PyObject*self, PyObject*args)
788 {
789     tag_internals_t*itag = tag_getinternals(self);
790     if(!shape_parse(itag))
791         return PY_ERROR("Couldn't parse shape");
792     shape_internal_t*fi = (shape_internal_t*)itag->data;
793     int nr = 0;
794     if(!PyArg_ParseTuple(args, "i", &nr))
795         return NULL;
796     
797     int num = fi->shape2->numfillstyles;
798     if(nr < 0 || nr >=num)
799         return PY_ERROR("fillstyle index out of range");
800     return f_FillStyle2(fi->shape2->fillstyles[nr]);
801 }
802 static PyObject* shape_getlinestyle(PyObject*self, PyObject*args)
803 {
804     tag_internals_t*itag = tag_getinternals(self);
805     if(!shape_parse(itag))
806         return PY_ERROR("Couldn't parse shape");
807     shape_internal_t*fi = (shape_internal_t*)itag->data;
808     int nr = 0;
809     if(!PyArg_ParseTuple(args, "i", &nr))
810         return NULL;
811     
812     int num = fi->shape2->numfillstyles;
813     if(nr < 0 || nr >=num)
814         return PY_ERROR("fillstyle index out of range");
815     return f_LineStyle3(fi->shape2->linestyles[nr]);
816 }
817 static PyObject* shape_setfillstyle(PyObject*self, PyObject*args)
818 {
819     tag_internals_t*itag = tag_getinternals(self);
820     if(!shape_parse(itag))
821         return PY_ERROR("Couldn't parse shape");
822     shape_internal_t*fi = (shape_internal_t*)itag->data;
823     int nr = 0;
824     PyObject*fs = 0;
825     if(!PyArg_ParseTuple(args, "iO!", &nr, &FillStyleClass, &fs))
826         return NULL;
827     
828     int num = fi->shape2->numfillstyles;
829     if(nr < 0 || nr >=num)
830         return PY_ERROR("fillstyle index out of range");
831     fi->shape2->fillstyles[nr] = fillstyle_getFillStyle(fs);
832     return PY_NONE;
833 }
834 static PyObject* shape_setlinestyle(PyObject*self, PyObject*args)
835 {
836     tag_internals_t*itag = tag_getinternals(self);
837     if(!shape_parse(itag))
838         return PY_ERROR("Couldn't parse shape");
839     shape_internal_t*fi = (shape_internal_t*)itag->data;
840     int nr = 0;
841     PyObject*ls = 0;
842     if(!PyArg_ParseTuple(args, "iO!", &nr, &LineStyleClass, &ls))
843         return NULL;
844     
845     int num = fi->shape2->numlinestyles;
846     if(nr < 0 || nr >=num)
847         return PY_ERROR("linestyle index out of range");
848     fi->shape2->linestyles[nr] = linestyle_getLineStyle(ls);
849     return PY_NONE;
850 }
851 static PyMethodDef shape_methods[] = 
852 {{"numfillstyles", shape_getfillstyles, METH_VARARGS, "get's the number of fillstyles"},
853  {"numlinestyles", shape_getlinestyles, METH_VARARGS, "get's the number of linestyles"},
854  {"getfillstyle", shape_getfillstyle, METH_VARARGS, "get's one fillstyle"},
855  {"getlinestyle", shape_getlinestyle, METH_VARARGS, "get's one linestyle"},
856  {"setfillstyle", shape_setfillstyle, METH_VARARGS, "set's one fillstyle"},
857  {"setlinestyle", shape_setlinestyle, METH_VARARGS, "set's one linestyle"},
858  {NULL, NULL, 0, NULL}
859 };
860
861 static tag_internals_t shape_tag =
862 {
863     parse: shape_parse,
864     fillTAG: shape_fillTAG,
865     dealloc: shape_dealloc,
866     getattr: 0, 
867     setattr: 0,
868     tagfunctions: shape_methods,
869     datasize: sizeof(shape_internal_t),
870 };
871 //----------------------------------------------------------------------------
872
873 typedef struct _videostream_internal
874 {
875     VIDEOSTREAM* stream;
876     int lastiframe;
877 } videostream_internal_t;
878 staticforward tag_internals_t videostream_tag;
879 staticforward tag_internals_t videoframe_tag;
880
881 static int videostream_parse(tag_internals_t*self)
882 {
883     videostream_internal_t*videostream = (videostream_internal_t*)self->data;
884     /* TODO */
885     PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet"));
886     return 0;
887 }
888 static void videostream_dealloc(tag_internals_t*self)
889 {
890     videostream_internal_t*videostream = (videostream_internal_t*)self->data;
891     if(videostream->stream) {
892         swf_VideoStreamClear(videostream->stream);
893         free(videostream->stream);
894         videostream->stream = 0;
895     }
896 }
897 static int videostream_fillTAG(tag_internals_t*self)
898 {
899     videostream_internal_t*fi = (videostream_internal_t*)self->data;
900     if(self->tag)
901         return 1;
902     PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented"));
903     return 0;
904 }
905 static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs)
906 {
907     PyObject*tag = tag_new(&videostream_tag);
908    
909     int width=0,height=0,frames=65535;
910     static char *kwlist[] = {"width", "height", "frames", NULL};
911     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", kwlist, &width, &height, &frames))
912         return NULL;
913
914     tag_internals_t*itag = tag_getinternals(tag);
915     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
916     fi->stream = malloc(sizeof(VIDEOSTREAM));
917     memset(fi->stream, 0, sizeof(VIDEOSTREAM));
918
919     TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
920     swf_SetU16(t, 0); /* id */
921     swf_SetVideoStreamDefine(t, fi->stream, frames, width, height);
922     itag->tag = t;
923     fi->lastiframe = -65536;
924     return (PyObject*)tag;
925 }
926 static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self)
927 {
928     PY_ASSERT_TYPE(self, &TagClass);
929     tag_internals_t*itag = tag_getinternals(self);
930     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
931     return fi->stream;
932 }
933 static PyObject* videostream_getbwidth(PyObject*self, PyObject*args)
934 {
935     tag_internals_t*itag = tag_getinternals(self);
936     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
937     int width = fi->stream->bbx;
938     return Py_BuildValue("i", width);
939 }
940 static PyObject* videostream_getbheight(PyObject*self, PyObject*args)
941 {
942     tag_internals_t*itag = tag_getinternals(self);
943     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
944     int height = fi->stream->bby;
945     return Py_BuildValue("i", height);
946 }
947 static PyObject* videostream_addBlackFrame(PyObject*self, PyObject*args, PyObject*kwargs)
948 {
949     tag_internals_t*_itag = tag_getinternals(self);
950     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
951     
952     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
953     
954     PyObject*tag = tag_new(&videoframe_tag);
955     tag_internals_t*itag = tag_getinternals(tag);
956     
957     swf_SetU16(t,0); /* id */
958     swf_SetVideoStreamBlackFrame(t, fi->stream);
959     fi->lastiframe = fi->stream->frame;
960     
961     itag->tag = t;
962     tagmap_addMapping(itag->tagmap, 0, self);
963     return tag;
964 }
965 static PyObject* videostream_addFrame(PyObject*self, PyObject*args, PyObject*kwargs)
966 {
967     tag_internals_t*_itag = tag_getinternals(self);
968     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
969    
970     PyObject*image = 0;
971     char*type=0; // none, "i", "p"
972     int quant=7;
973     static char *kwlist[] = {"image", "quant", "type", NULL};
974     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|is", kwlist, &image, &quant, &type))
975         return NULL;
976     if(fi->stream->owidth != image_getWidth(image)) {
977         PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getWidth(image), fi->stream->width));return 0;
978     }
979     if(fi->stream->oheight != image_getHeight(image)) {
980         PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getHeight(image), fi->stream->height));return 0;
981     }
982     PyObject*tag = tag_new(&videoframe_tag);
983     tag_internals_t*itag = tag_getinternals(tag);
984
985     RGBA*pic = image_toRGBA(image);
986     if(!pic)
987         return 0;
988
989 /*{  int f,j=0,i=0,rr,gg,bb;
990    FILE *o;
991    RGBA*it = pic;
992    char*filename="test.ppm";
993    printf("Creating %s %dx%d\n",filename, 512,512);
994    o=fopen(filename, "wb");
995    fprintf(o,"P6\n%d %d\n255\n",512, 512);
996    
997    while(j<512*512) {
998     rr=it->r;
999     gg=it->g;
1000     bb=it->b;
1001     fprintf(o,"%c%c%c",rr,gg,bb);
1002     it++;
1003     j++;
1004    }
1005    fclose(o);
1006 }*/
1007
1008     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
1009     if((type && (type[0]=='I' || type[0]=='i')) || (type==0 && fi->lastiframe+64 < fi->stream->frame)) {
1010         swf_SetU16(t,0); /* id */
1011         swf_SetVideoStreamIFrame(t, fi->stream, pic, quant);
1012         fi->lastiframe = fi->stream->frame;
1013     } else {
1014         swf_SetU16(t,0);
1015         swf_SetVideoStreamPFrame(t, fi->stream, pic, quant);
1016     }
1017     itag->tag = t;
1018     tagmap_addMapping(itag->tagmap, 0, self);
1019     free(pic);
1020     return tag;
1021 }
1022 static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args, PyObject*kwargs)
1023 {
1024     tag_internals_t*_itag = tag_getinternals(self);
1025     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
1026     
1027     static char *kwlist[] = {"image", "quant", NULL};
1028     int quant=7;
1029     PyObject* array = 0;
1030     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", kwlist, &array, &quant))
1031         return NULL;
1032
1033     signed char* movex = malloc(fi->stream->bbx * fi->stream->bby * 1);
1034     signed char* movey = malloc(fi->stream->bbx * fi->stream->bby * 1);
1035     RGBA** pics = (RGBA**)malloc(fi->stream->bbx * fi->stream->bby * sizeof(void*));
1036     signed char* itx=movex;
1037     signed char* ity=movey;
1038     RGBA**pic=pics;
1039     int x,y;
1040     if(!array || !PySequence_Check(array))
1041         return PY_ERROR("Not an array");
1042     if(PySequence_Length(array) < fi->stream->bby)
1043         return PY_ERROR("Array (y) has to have at least %d elements, but has only %d ", fi->stream->bby, PySequence_Length(array));
1044     for(y=0;y<fi->stream->bby;y++) {
1045         PyObject*line = PySequence_GetItem(array, y);
1046         if(!line || !PySequence_Check(line))
1047             return PY_ERROR("Not an array of arrays");
1048         if(PySequence_Length(line) < fi->stream->bbx)
1049             return PY_ERROR("Inner arrays (x) have to be at least %d long- %dth is only %d", fi->stream->bbx, y, PySequence_Length(line));
1050
1051         for(x=0;x<fi->stream->bbx;x++) {
1052             PyObject*pixel = PySequence_GetItem(line, x);
1053             PyObject*xy = 0;
1054             PyObject*image = 0;
1055
1056             if(!pixel) {
1057                 xy = image = 0;
1058             } else {
1059                 if(PyComplex_Check(pixel)) {
1060                     xy = pixel; image = 0;
1061                 } else if(PyString_Check(pixel)) {
1062                     xy = 0; image = pixel;
1063                 } else if(PyTuple_Check(pixel)) {
1064                     int size = PyTuple_GET_SIZE(pixel);
1065                     if(size!=2) return PY_ERROR("Tuples have to have size 2 (xy,img)");
1066                     xy = PyTuple_GetItem(pixel, 0);
1067                     if(!xy) return 0;
1068                     if(!PyComplex_Check(xy)) return PY_ERROR("Tuples must be (COMPLEX,string)");
1069                     image = PyTuple_GetItem(pixel, 1);
1070                     if(!image) return 0;
1071                     if(!PyString_Check(image)) return PY_ERROR("Tuples must be (complex,STRING)");
1072                 }
1073             }
1074
1075             *itx = *ity = 0;
1076             *pic= 0;
1077
1078             if(xy) {
1079                 *itx = (signed char)PyComplex_RealAsDouble(pixel);
1080                 *ity = (signed char)PyComplex_ImagAsDouble(pixel);
1081             }
1082             if(image) {
1083                 char*string;
1084                 int size;
1085                 PyString_AsStringAndSize(image,&string,&size);
1086                 if(size<256*3) {
1087                     return PY_ERROR("image strings must be >= 256*3");
1088                 }
1089                 *pic = malloc(sizeof(RGBA)*16*16);
1090                 int t;
1091                 for(t=0;t<16*16;t++) {
1092                     (*pic)[t].r = string[t*3];
1093                     (*pic)[t].g = string[t*3+1];
1094                     (*pic)[t].b = string[t*3+2];
1095                     (*pic)[t].a = 255;
1096                 }
1097             }
1098             itx++;
1099             ity++;
1100             pic++;
1101         }
1102     }
1103     
1104     PyObject*tag = tag_new(&videoframe_tag);
1105     tag_internals_t*itag = tag_getinternals(tag);
1106     
1107     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
1108     swf_SetU16(t,0); /* id */
1109     swf_SetVideoStreamMover(t, fi->stream, movex, movey,(void**)pics, quant);
1110
1111     itag->tag = t;
1112     tagmap_addMapping(itag->tagmap, 0, self);
1113
1114     for(x=0;x<fi->stream->bbx*fi->stream->bby;x++) {
1115         if(pics[x]) {
1116             free(pics[x]);pics[x] = 0;
1117         }
1118     }
1119
1120     free(movex);
1121     free(movey);
1122     free(pics);
1123
1124     return tag;
1125 }
1126 static PyMethodDef videostream_methods[] = 
1127 {{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"},
1128  {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"},
1129  {"addFrame", (PyCFunction)videostream_addFrame, METH_KEYWORDS, "add a Video Frame"},
1130  {"addBlackFrame", (PyCFunction)videostream_addBlackFrame, METH_KEYWORDS, "add a black Video Frame"},
1131  {"addDistortionFrame", (PyCFunction)videostream_addDistortionFrame, METH_KEYWORDS, "add a MVD frame"},
1132  {NULL, NULL, 0, NULL}
1133 };
1134
1135 static tag_internals_t videostream_tag =
1136 {
1137     parse: videostream_parse,
1138     fillTAG: videostream_fillTAG,
1139     dealloc: videostream_dealloc,
1140     getattr: 0, 
1141     setattr: 0,
1142     tagfunctions: videostream_methods,
1143     datasize: sizeof(videostream_internal_t),
1144 };
1145
1146 //============================================================================
1147
1148 static tag_internals_t videoframe_tag =
1149 {
1150     parse: 0,
1151     fillTAG: 0,
1152     dealloc: 0,
1153     getattr: 0, 
1154     setattr: 0,
1155     tagfunctions: 0,
1156     datasize: 0
1157 };
1158
1159 //============================================================================
1160 static PyMethodDef TagMethods[] = 
1161 {
1162     /* TAGS */
1163     {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
1164     {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
1165     {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
1166     {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
1167     {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
1168     {"RemoveObject", (PyCFunction)f_RemoveObject, METH_KEYWORDS, "Create a RemoveObject Tag."},
1169     {"MoveObject", (PyCFunction)f_MoveObject, METH_KEYWORDS, "Create a PlaceObject Move Tag."},
1170     {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."},
1171     {"Image", (PyCFunction)f_DefineImage, METH_KEYWORDS, "Create an SWF Image Tag."},
1172     {"ImageShape", (PyCFunction)f_DefineImageShape, METH_KEYWORDS, "Create an SWF Image Shape Tag."},
1173     {"Shape", (PyCFunction)f_DefineShape, METH_KEYWORDS, "Create an SWF Shape Tag."},
1174     {"ShowFrame", (PyCFunction)f_ShowFrame, METH_KEYWORDS, "Create an SWF Show Frame Tag."},
1175     {"Sprite", (PyCFunction)f_Sprite, METH_KEYWORDS, "Create an SWF Sprite Tag."},
1176     
1177     {NULL, NULL, 0, NULL}
1178 };
1179 PyMethodDef* tags_getMethods()
1180 {
1181     TagClass.ob_type = &PyType_Type;
1182     
1183     register_tag(ST_PLACEOBJECT,&placeobject_tag);
1184     register_tag(ST_PLACEOBJECT2,&placeobject_tag);
1185     register_tag(ST_REMOVEOBJECT,&removeobject_tag);
1186     register_tag(ST_REMOVEOBJECT2,&removeobject_tag);
1187     register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag);
1188     register_tag(ST_DEFINEFONT,&font_tag);
1189     register_tag(ST_PROTECT,&protect_tag);
1190     register_tag(ST_DEFINETEXT,&text_tag);
1191     register_tag(ST_DEFINEBITSJPEG,&image_tag);
1192     register_tag(ST_DEFINEBITSJPEG2,&image_tag);
1193     register_tag(ST_DEFINEBITSJPEG3,&image_tag);
1194     register_tag(ST_DEFINEBITSLOSSLESS,&image_tag);
1195     register_tag(ST_DEFINEBITSLOSSLESS2,&image_tag);
1196     register_tag(ST_DEFINESHAPE,&shape_tag);
1197     register_tag(ST_DEFINESHAPE2,&shape_tag);
1198     register_tag(ST_DEFINESHAPE3,&shape_tag);
1199     register_tag(ST_SHOWFRAME,&showframe_tag);
1200     register_tag(ST_DEFINEVIDEOSTREAM,&videostream_tag);
1201     register_tag(ST_VIDEOFRAME,&videoframe_tag);
1202     register_tag(ST_DEFINESPRITE,&sprite_tag);
1203     register_tag(ST_END,&end_tag);
1204
1205     return TagMethods;
1206 }