* added sprite tag
[swftools.git] / lib / python / tags.c
1 #include "pyutils.h"
2 #include "primitives.h"
3 #include "action.h"
4 #include "taglist.h"
5 #include "tag.h"
6 #include "tags.h"
7 #include "image.h"
8
9 //----------------------------------------------------------------------------
10
11 typedef struct _font_internal
12 {
13     SWFFONT* font;
14 } font_internal_t;
15 staticforward tag_internals_t font_tag;
16
17 static int font_parse(tag_internals_t*self)
18 {
19     font_internal_t*font = (font_internal_t*)self->data;
20     /* TODO */
21     PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet"));
22     return 0;
23 }
24 static void font_dealloc(tag_internals_t*self)
25 {
26     font_internal_t*font = (font_internal_t*)self->data;
27     if(font->font) {
28         swf_FontFree(font->font);
29         font->font = 0;
30     }
31 }
32 static int font_fillTAG(tag_internals_t*self)
33 {
34     font_internal_t*fi = (font_internal_t*)self->data;
35     if(self->tag)
36         return 1;
37     self->tag = swf_InsertTag(0, ST_DEFINEFONT2);
38     swf_FontSetDefine2(self->tag, fi->font);
39     return 1;
40 }
41 static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs)
42 {
43     static char *kwlist[] = {"filename", NULL};
44     char*filename = 0;
45     PyObject*tag;
46     SWFFONT* font;
47
48     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &filename))
49         return NULL;
50
51     font = swf_LoadFont(filename);
52     if(!font) {
53         PyErr_SetString(PyExc_Exception, setError("Could not load %s", filename));
54         return NULL;
55     }
56
57     tag = tag_new(&font_tag);
58     tag_internals_t*itag = tag_getinternals(tag);
59     font_internal_t*fi = (font_internal_t*)itag->data;
60     font->id = 0;
61     fi->font = font;
62     return (PyObject*)tag;
63 }
64 static SWFFONT* font_getSWFFONT(PyObject*self)
65 {
66     PY_ASSERT_TYPE(self, &TagClass);
67     tag_internals_t*itag = tag_getinternals(self);
68     font_internal_t*fi = (font_internal_t*)itag->data;
69     return fi->font;
70 }
71 static tag_internals_t font_tag =
72 {
73     parse: font_parse,
74     fillTAG: font_fillTAG,
75     dealloc: font_dealloc,
76     getattr: 0, 
77     setattr: 0,
78     tagfunctions: 0,
79     datasize: sizeof(font_internal_t),
80 };
81 //----------------------------------------------------------------------------
82
83 typedef struct _placeobject_internal
84 {
85     SWFPLACEOBJECT* po;
86 } placeobject_internal_t;
87 staticforward tag_internals_t placeobject_tag;
88
89 static void po_dealloc(tag_internals_t*self)
90 {
91     placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
92     if(pi->po) {
93         swf_PlaceObjectFree(pi->po);
94         pi->po = 0;
95     }
96 }
97 static int po_parse(tag_internals_t*self)
98 {
99     placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
100     /* TODO */
101     PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet"));
102     return 0;
103 }
104 static int po_fillTAG(tag_internals_t*self)
105 {
106     placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
107     self->tag = swf_InsertTag(0, ST_PLACEOBJECT2);
108     swf_SetPlaceObject(self->tag, pi->po);
109     return 1;
110 }
111 static PyObject* po_create(PyObject* self, PyObject* args, PyObject* kwargs,char move)
112 {
113     static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
114     
115     PyObject*character = 0;
116     int depth;
117     int clipdepth = 0;
118     PyObject*matrix = 0;
119     PyObject*cxform = 0;
120     PyObject*action = 0;
121     int ratio = 0;
122     char* name = 0;
123     SWFPLACEOBJECT* po;
124     po = malloc(sizeof(SWFPLACEOBJECT));
125     memset(po, 0, sizeof(SWFPLACEOBJECT));
126
127     swf_GetPlaceObject(0, po);
128
129     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|O!O!isiO!", kwlist, 
130                 &character, 
131                 &depth, 
132                 &MatrixClass, &matrix, 
133                 &CXFormClass, &cxform,
134                 &ratio,
135                 &name,
136                 &clipdepth,
137                 &ActionClass, &action
138                 ))
139         return NULL;
140     po->depth = depth;
141     po->clipdepth = clipdepth;
142     po->ratio = ratio;
143     po->name = name;
144     po->move = move;
145     if(clipdepth) po->clipdepth = clipdepth;
146     if(matrix) po->matrix = matrix_getMatrix(matrix);
147     if(cxform) po->cxform = colortransform_getCXForm(cxform);
148     if(action) po->actions = action_getAction(action);
149
150     PyObject*tag;
151     tag = tag_new(&placeobject_tag);
152     tag_internals_t*itag = tag_getinternals(tag);
153     placeobject_internal_t*pi = (placeobject_internal_t*)itag->data;
154     pi->po = po;
155     if(!move) {
156         pi->po->id = tagmap_add(itag->tagmap,(PyObject*)character);
157     } else {
158         pi->po->id = 0;
159     }
160     
161     mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt);
162
163     return (PyObject*)tag;
164 }
165 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
166 {
167     return po_create(self, args, kwargs, 0);
168 }
169 static PyObject* f_MoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
170 {
171     return po_create(self, args, kwargs, 1);
172 }
173 static tag_internals_t placeobject_tag =
174 {
175     parse: po_parse,
176     fillTAG: po_fillTAG,
177     dealloc: po_dealloc,
178     getattr: 0, 
179     setattr: 0,
180     tagfunctions: 0,
181     datasize: sizeof(placeobject_internal_t),
182 };
183 //----------------------------------------------------------------------------
184 staticforward tag_internals_t bgcolor_tag;
185 static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other)
186 {
187     tag_internals_t*itag = tag_getinternals(self);
188     int r,g,b;
189     r = itag->tag->data[0];
190     g = itag->tag->data[1];
191     b = itag->tag->data[2];
192     return Py_BuildValue("(iii)", r,g,b);
193 }
194 static PyMethodDef setbgcolor_methods[] = 
195 {{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"},
196  {NULL, NULL, 0, NULL}
197 };
198 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
199 {
200     static char *kwlist[] = {"color", NULL};
201     int r=0,g=0,b=0;
202     PyObject*tag;
203     PyObject*color;
204     
205     tag = tag_new(&bgcolor_tag);
206     tag_internals_t*itag = tag_getinternals(tag);
207
208     /* 1st try- copy constructor */
209     if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
210         PyErr_Clear();
211         /* 2nd try- color's contructor */
212         color = f_Color(NULL, args, kwargs);
213     }
214     if(!color)
215         return NULL;
216
217     itag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
218     RGBA rgba = color_getRGBA(color);
219     swf_SetU8(itag->tag, rgba.r);
220     swf_SetU8(itag->tag, rgba.g);
221     swf_SetU8(itag->tag, rgba.b);
222     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);
223     Py_DECREF(color);
224     return (PyObject*)tag;
225 }
226 static tag_internals_t bgcolor_tag =
227 {
228     parse: 0,
229     fillTAG: 0,
230     dealloc: 0,
231     getattr: 0, 
232     setattr: 0,
233     tagfunctions: setbgcolor_methods,
234     datasize: 0,
235 };
236 //----------------------------------------------------------------------------
237 staticforward tag_internals_t protect_tag;
238 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
239 {
240     static char *kwlist[] = {"password", NULL};
241     char*password = 0;
242
243     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
244         return NULL;
245
246     PyObject*tag = tag_new(&protect_tag);
247     tag_internals_t*itag = tag_getinternals(tag);
248     itag->tag = swf_InsertTag(0, ST_PROTECT);
249     if(password) {
250         swf_SetPassword(itag->tag, password);
251     }
252     mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt);
253     return (PyObject*)tag;
254 }
255 static tag_internals_t protect_tag =
256 {
257     parse: 0,
258     fillTAG: 0,
259     dealloc: 0,
260     getattr: 0, 
261     setattr: 0,
262     tagfunctions: 0,
263     datasize: 0,
264 };
265 //----------------------------------------------------------------------------
266 staticforward tag_internals_t showframe_tag;
267 static PyObject* f_ShowFrame(PyObject* self, PyObject* args, PyObject* kwargs)
268 {
269     static char *kwlist[] = {"name", NULL};
270     char*name= 0;
271     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
272         return NULL;
273
274     PyObject*tag = tag_new(&showframe_tag);
275     tag_internals_t*itag = tag_getinternals(tag);
276     itag->tag = swf_InsertTag(0, ST_SHOWFRAME);
277     mylog("+%08x(%d) f_ShowFrame", (int)tag, tag->ob_refcnt);
278     return (PyObject*)tag;
279 }
280 static tag_internals_t showframe_tag =
281 {
282     parse: 0,
283     fillTAG: 0,
284     dealloc: 0,
285     getattr: 0, 
286     setattr: 0,
287     tagfunctions: 0,
288     datasize: 0,
289 };
290 //----------------------------------------------------------------------------
291 staticforward tag_internals_t sprite_tag;
292 typedef struct _sprite_internal
293 {
294     PyObject* taglist;
295 } sprite_internal_t;
296     
297 static int sprite_fillTAG(tag_internals_t*self)
298 {
299     mylog("+%08x(?) sprite_fillTAG", (int)self);
300
301     sprite_internal_t*si = (sprite_internal_t*)self->data;
302
303     TAG*sprite = swf_InsertTag(0, ST_DEFINESPRITE);
304     swf_SetU16(sprite, 0); //id
305     swf_SetU16(sprite, 0); //frames
306
307     TAG*tag = taglist_getTAGs2(si->taglist, self->tagmap, 0);
308     if(!tag) {
309         /* pass through exception */
310         return 0;
311     }
312     TAG*tag2 = tag;
313     while(tag2->next) tag2 = tag2->next;
314     swf_InsertTag(tag2, ST_END);
315
316     sprite->next = tag;
317     tag->prev = sprite;
318
319     swf_FoldSprite(sprite);
320     self->tag = sprite;
321     return 1;
322 }
323
324 static PyObject* f_Sprite(PyObject* self, PyObject* args, PyObject* kwargs)
325 {
326     static char *kwlist[] = {"name", NULL};
327     char*name= 0;
328     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
329         return NULL;
330
331     PyObject*tag = tag_new(&sprite_tag);
332     tag_internals_t*itag = tag_getinternals(tag);
333     sprite_internal_t*si = (sprite_internal_t*)itag->data;
334     si->taglist = taglist_new();
335     mylog("+%08x(%d) f_DefineSprite", (int)tag, tag->ob_refcnt);
336     return (PyObject*)tag;
337 }
338 static PyObject* sprite_getattr(tag_internals_t*self,char*a)
339 {
340     sprite_internal_t*si = (sprite_internal_t*)self->data;
341     if(!strcmp(a, "tags")) {
342         Py_INCREF(si->taglist);
343         return si->taglist;
344     }
345     return 0;
346 }
347 static int sprite_setattr(tag_internals_t*self,char*a, PyObject*obj)
348 {
349     sprite_internal_t*si = (sprite_internal_t*)self->data;
350     if(self->tag) {
351         swf_DeleteTag(self->tag);
352         self->tag = 0;
353     }
354     if(!strcmp(a, "tags")) {
355         PY_ASSERT_TYPE(obj,&TagListClass);
356         Py_DECREF(si->taglist);
357         si->taglist = obj;
358         Py_INCREF(si->taglist);
359         return 0;
360     }
361     return 1;
362 }
363 static tag_internals_t sprite_tag =
364 {
365     parse: 0,
366     fillTAG: sprite_fillTAG,
367     dealloc: 0,
368     getattr: sprite_getattr, 
369     setattr: sprite_setattr,
370     tagfunctions: 0,
371     datasize: sizeof(sprite_internal_t),
372 };
373 //----------------------------------------------------------------------------
374 staticforward tag_internals_t end_tag;
375 static tag_internals_t end_tag =
376 {
377     parse: 0,
378     fillTAG: 0,
379     dealloc: 0,
380     getattr: 0, 
381     setattr: 0,
382     tagfunctions: 0,
383     datasize: 0,
384 };
385 //----------------------------------------------------------------------------
386 staticforward tag_internals_t text_tag;
387
388 typedef struct _text_internal
389 {
390     char*text;
391     SWFFONT* swffont;
392     RGBA rgba;
393     int size;
394 } text_internal_t;
395 staticforward tag_internals_t placeobject_tag;
396
397 static int text_fillTAG(tag_internals_t*self)
398 {
399     text_internal_t*ti = (text_internal_t*)self->data;
400     self->tag= swf_InsertTag(0, ST_DEFINETEXT2);
401     swf_SetU16(self->tag, /*ID*/0);
402     SRECT r = swf_SetDefineText(self->tag, ti->swffont, &ti->rgba, ti->text, ti->size);
403     return 1;
404 }
405 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
406 {
407     static char *kwlist[] = {"font", "text", "size", "color", NULL};
408     PyObject*tag = 0;
409     PyObject*otext;
410     char*text = 0;
411     int size = 0;
412     RGBA rgba = {255,0,0,0};
413     PyObject*color = 0;
414     PyObject*font = 0;
415
416     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!Oi|O!", kwlist, &TagClass, &font, &otext, &size, &ColorClass, &color))
417         return NULL;
418     text = PyString_AS_STRING(PyUnicode_AsUTF8String(otext));
419
420     if(color)
421         rgba = color_getRGBA(color);
422
423     mylog("DefineText: text = %s", text);
424     
425     tag = tag_new(&text_tag);
426     tag_internals_t* itag = tag_getinternals(tag);
427     text_internal_t*ti = (text_internal_t*)itag->data;
428
429     ti->swffont = font_getSWFFONT(font);
430     int font_id = tagmap_add(itag->tagmap, font); // add dependency on font
431     ti->swffont->id = font_id; // for swf_SetDefineTexts
432     ti->text = strdup(text);
433     ti->rgba = rgba;
434     ti->size = size;
435
436     return (PyObject*)tag;
437 }
438 static tag_internals_t text_tag =
439 {
440     parse: 0,
441     fillTAG: text_fillTAG,
442     dealloc: 0,
443     getattr: 0, 
444     setattr: 0,
445     tagfunctions: 0,
446     datasize: sizeof(text_internal_t),
447 };
448 //----------------------------------------------------------------------------
449 staticforward tag_internals_t image_tag;
450
451 typedef struct _image_internal
452 {
453     RGBA*rgba;
454     int size;
455     int width;
456     int height;
457     int bpp;
458     char isindexed;
459     char islossless;
460 } image_internal_t;
461 staticforward tag_internals_t image_tag;
462
463 static int image_fillTAG(tag_internals_t*self)
464 {
465     image_internal_t*ti = (image_internal_t*)self->data;
466     self->tag= swf_InsertTag(0, ST_DEFINEBITSLOSSLESS2);
467     swf_SetU16(self->tag, /*ID*/0);
468     swf_SetLosslessBits(self->tag, ti->width, ti->height, ti->rgba, BMF_32BIT);
469     return 1;
470 }
471 static void image_dealloc(tag_internals_t*self)
472 {
473     image_internal_t*pi = (image_internal_t*)self->data;
474     if(pi->rgba) {
475         free(pi->rgba);pi->rgba = 0;
476     }
477 }
478 static int imagetag_getWidth(PyObject* self)
479 {
480     tag_internals_t*itag = tag_getinternals(self);
481     image_internal_t*pi = (image_internal_t*)itag->data;
482     return pi->width;
483 }
484 static int imagetag_getHeight(PyObject* self)
485 {
486     tag_internals_t*itag = tag_getinternals(self);
487     image_internal_t*pi = (image_internal_t*)itag->data;
488     return pi->height;
489 }
490 static PyObject* f_DefineImage(PyObject* self, PyObject* args, PyObject* kwargs)
491 {
492     static char *kwlist[] = {"image"};
493     PyObject*image = 0;
494     PyObject*tag = tag_new(&image_tag);
495
496     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &image))
497         return NULL;
498     
499     tag = tag_new(&image_tag);
500     tag_internals_t* itag = tag_getinternals(tag);
501     image_internal_t*ti = (image_internal_t*)itag->data;
502
503     ti->rgba = image_toRGBA(image);
504     if(!ti->rgba) // pass through exception
505         return 0;
506     ti->width = image_getWidth(image);
507     ti->height = image_getHeight(image);
508     ti->isindexed = 0;
509     ti->islossless = 1;
510     ti->bpp = 32;
511     ti->size = ti->width*ti->height;
512
513     return (PyObject*)tag;
514 }
515 static tag_internals_t image_tag =
516 {
517     parse: 0,
518     fillTAG: image_fillTAG,
519     dealloc: image_dealloc,
520     getattr: 0, 
521     setattr: 0,
522     tagfunctions: 0,
523     datasize: sizeof(image_internal_t),
524 };
525 //----------------------------------------------------------------------------
526 staticforward tag_internals_t shape_tag;
527
528 typedef struct _shape_internal
529 {
530     SHAPE2*shape;
531 } shape_internal_t;
532 staticforward tag_internals_t shape_tag;
533
534 static int shape_fillTAG(tag_internals_t*self)
535 {
536     shape_internal_t*ti = (shape_internal_t*)self->data;
537     self->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
538     swf_SetU16(self->tag, /*ID*/0);
539     swf_SetShape2(self->tag, ti->shape);
540     return 1;
541 }
542 static void shape_dealloc(tag_internals_t*self)
543 {
544     shape_internal_t*pi = (shape_internal_t*)self->data;
545     if(pi->shape) {
546         swf_Shape2Free(pi->shape);
547         pi->shape = 0;
548     }
549 }
550 static PyObject* f_DefineImageShape(PyObject* self, PyObject* args, PyObject* kwargs)
551 {
552     static char *kwlist[] = {"image"};
553     PyObject*shape = 0;
554     PyObject*tag = tag_new(&shape_tag);
555     PyObject*image = 0;
556
557     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &TagClass, &image))
558         return NULL;
559     
560     tag = tag_new(&shape_tag);
561     tag_internals_t* itag = tag_getinternals(tag);
562     shape_internal_t*ti = (shape_internal_t*)itag->data;
563     ti->shape = 0; /*HACK*/
564
565     int width = imagetag_getWidth(image);
566     int height = imagetag_getHeight(image);
567     int id = tagmap_add(itag->tagmap, image);
568     itag->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
569     swf_SetU16(itag->tag, 0);
570     swf_ShapeSetBitmapRect(itag->tag, id, width, height);
571     return (PyObject*)tag;
572 }
573 static tag_internals_t shape_tag =
574 {
575     parse: 0,
576     fillTAG: shape_fillTAG,
577     dealloc: shape_dealloc,
578     getattr: 0, 
579     setattr: 0,
580     tagfunctions: 0,
581     datasize: sizeof(shape_internal_t),
582 };
583 //----------------------------------------------------------------------------
584
585 typedef struct _videostream_internal
586 {
587     VIDEOSTREAM* stream;
588     int lastiframe;
589 } videostream_internal_t;
590 staticforward tag_internals_t videostream_tag;
591 staticforward tag_internals_t videoframe_tag;
592
593 static int videostream_parse(tag_internals_t*self)
594 {
595     videostream_internal_t*videostream = (videostream_internal_t*)self->data;
596     /* TODO */
597     PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet"));
598     return 0;
599 }
600 static void videostream_dealloc(tag_internals_t*self)
601 {
602     videostream_internal_t*videostream = (videostream_internal_t*)self->data;
603     if(videostream->stream) {
604         swf_VideoStreamClear(videostream->stream);
605         free(videostream->stream);
606         videostream->stream = 0;
607     }
608 }
609 static int videostream_fillTAG(tag_internals_t*self)
610 {
611     videostream_internal_t*fi = (videostream_internal_t*)self->data;
612     if(self->tag)
613         return 1;
614     PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented"));
615     return 0;
616 }
617 static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs)
618 {
619     PyObject*tag = tag_new(&videostream_tag);
620    
621     int width=0,height=0,frames=65535;
622     static char *kwlist[] = {"width", "height", "frames", NULL};
623     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", kwlist, &width, &height, &frames))
624         return NULL;
625
626     printf(": %d %d\n", width, height);
627     
628     tag_internals_t*itag = tag_getinternals(tag);
629     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
630     fi->stream = malloc(sizeof(VIDEOSTREAM));
631     memset(fi->stream, 0, sizeof(VIDEOSTREAM));
632
633     TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
634     swf_SetU16(t, 0); /* id */
635     swf_SetVideoStreamDefine(t, fi->stream, frames, width, height);
636     itag->tag = t;
637     fi->lastiframe = -65536;
638     return (PyObject*)tag;
639 }
640 static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self)
641 {
642     PY_ASSERT_TYPE(self, &TagClass);
643     tag_internals_t*itag = tag_getinternals(self);
644     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
645     return fi->stream;
646 }
647 static PyObject* videostream_getbwidth(PyObject*self, PyObject*args)
648 {
649     tag_internals_t*itag = tag_getinternals(self);
650     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
651     int width = fi->stream->bbx;
652     return Py_BuildValue("i", width);
653 }
654 static PyObject* videostream_getbheight(PyObject*self, PyObject*args)
655 {
656     tag_internals_t*itag = tag_getinternals(self);
657     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
658     int height = fi->stream->bby;
659     return Py_BuildValue("i", height);
660 }
661 static PyObject* videostream_addFrame(PyObject*self, PyObject*args, PyObject*kwargs)
662 {
663     tag_internals_t*_itag = tag_getinternals(self);
664     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
665    
666     PyObject*image = 0;
667     char*type=0; // none, "i", "p"
668     int quant=7;
669     static char *kwlist[] = {"image", "quant", "type", NULL};
670     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|is", kwlist, &image, &quant, &type))
671         return NULL;
672     if(fi->stream->width != image_getWidth(image)) {
673         PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getWidth(image), fi->stream->width));return 0;
674     }
675     if(fi->stream->height != image_getHeight(image)) {
676         PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getHeight(image), fi->stream->height));return 0;
677     }
678     PyObject*tag = tag_new(&videoframe_tag);
679     tag_internals_t*itag = tag_getinternals(tag);
680
681     RGBA*pic = image_toRGBA(image);
682     if(!pic)
683         return 0;
684
685     
686 {  int f,j=0,i=0,rr,gg,bb;
687    FILE *o;
688    RGBA*it = pic;
689    char*filename="test.ppm";
690    printf("Creating %s %dx%d\n",filename, 512,512);
691    o=fopen(filename, "wb");
692    fprintf(o,"P6\n%d %d\n255\n",512, 512);
693    
694    while(j<512*512) {
695     rr=it->r;
696     gg=it->g;
697     bb=it->b;
698     fprintf(o,"%c%c%c",rr,gg,bb);
699     it++;
700     j++;
701    }
702    fclose(o);
703 }
704
705     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
706     if((type && (type[0]=='I' || type[0]=='i')) || (type==0 && fi->lastiframe+64 < fi->stream->frame)) {
707         swf_SetU16(t,0); /* id */
708         swf_SetVideoStreamIFrame(t, fi->stream, pic, quant);
709         fi->lastiframe = fi->stream->frame;
710     } else {
711         swf_SetU16(t,0);
712         swf_SetVideoStreamPFrame(t, fi->stream, pic, quant);
713     }
714     itag->tag = t;
715     tagmap_addMapping(itag->tagmap, 0, self);
716     free(pic);
717     return tag;
718 }
719 static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args, PyObject*kwargs)
720 {
721     tag_internals_t*_itag = tag_getinternals(self);
722     videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
723     
724     static char *kwlist[] = {"image", "quant", NULL};
725     int quant=7;
726     PyObject* array = 0;
727     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", kwlist, &array, &quant))
728         return NULL;
729
730     signed char* movex = malloc(fi->stream->bbx * fi->stream->bby);
731     signed char* movey = malloc(fi->stream->bbx * fi->stream->bby);
732     signed char* itx=movex;
733     signed char* ity=movey;
734     int x,y;
735     if(!array || !PySequence_Check(array))
736         return PY_ERROR("Not an array");
737     if(PySequence_Length(array) < fi->stream->bby)
738         return PY_ERROR("Array (y) has to have at least %d elements, but has only %d ", fi->stream->bby, PySequence_Length(array));
739     for(y=0;y<fi->stream->bby;y++) {
740         PyObject*line = PySequence_GetItem(array, y);
741         if(!line || !PySequence_Check(line))
742             return PY_ERROR("Not an array of arrays");
743         if(PySequence_Length(line) < fi->stream->bbx)
744             return PY_ERROR("Inner arrays (x) have to be at least %d long- %dth is only %d", fi->stream->bbx, y, PySequence_Length(line));
745
746         for(x=0;x<fi->stream->bbx;x++) {
747             PyObject*pixel = PySequence_GetItem(line, x);
748             if(!pixel) {
749                 *itx = 0;
750                 *ity = 0;
751             } else {
752                 if(!PyComplex_Check(pixel)) {
753                     return PY_ERROR("Not an array of arrays of complex numbers");
754                 }
755                 *itx = (signed char)PyComplex_RealAsDouble(pixel);
756                 *ity = (signed char)PyComplex_ImagAsDouble(pixel);
757             }
758             itx++;
759             ity++;
760         }
761     }
762     
763     PyObject*tag = tag_new(&videoframe_tag);
764     tag_internals_t*itag = tag_getinternals(tag);
765     
766     TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
767     swf_SetU16(t,0); /* id */
768     swf_SetVideoStreamMover(t, fi->stream, movex, movey, quant);
769
770     itag->tag = t;
771     tagmap_addMapping(itag->tagmap, 0, self);
772
773     free(movex);
774     free(movey);
775
776     return tag;
777 }
778 static PyMethodDef videostream_methods[] = 
779 {{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"},
780  {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"},
781  {"addFrame", (PyCFunction)videostream_addFrame, METH_KEYWORDS, "add a Video Frame"},
782  {"addDistortionFrame", (PyCFunction)videostream_addDistortionFrame, METH_KEYWORDS, "add a MVD frame"},
783  {NULL, NULL, 0, NULL}
784 };
785
786 static tag_internals_t videostream_tag =
787 {
788     parse: videostream_parse,
789     fillTAG: videostream_fillTAG,
790     dealloc: videostream_dealloc,
791     getattr: 0, 
792     setattr: 0,
793     tagfunctions: videostream_methods,
794     datasize: sizeof(videostream_internal_t),
795 };
796
797 //============================================================================
798
799 static tag_internals_t videoframe_tag =
800 {
801     parse: 0,
802     fillTAG: 0,
803     dealloc: 0,
804     getattr: 0, 
805     setattr: 0,
806     tagfunctions: 0,
807     datasize: 0
808 };
809
810 //============================================================================
811
812 static PyMethodDef TagMethods[] = 
813 {
814     /* TAGS */
815     {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
816     {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
817     {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
818     {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
819     {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
820     {"MoveObject", (PyCFunction)f_MoveObject, METH_KEYWORDS, "Create a PlaceObject Move Tag."},
821     {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."},
822     {"Image", (PyCFunction)f_DefineImage, METH_KEYWORDS, "Create an SWF Image Tag."},
823     {"ImageShape", (PyCFunction)f_DefineImageShape, METH_KEYWORDS, "Create an SWF Image Shape Tag."},
824     {"ShowFrame", (PyCFunction)f_ShowFrame, METH_KEYWORDS, "Create an SWF Show Frame Tag."},
825     {"Sprite", (PyCFunction)f_Sprite, METH_KEYWORDS, "Create an SWF Sprite Tag."},
826     {NULL, NULL, 0, NULL}
827 };
828 PyMethodDef* tags_getMethods()
829 {
830     TagClass.ob_type = &PyType_Type;
831     
832     register_tag(ST_PLACEOBJECT,&placeobject_tag);
833     register_tag(ST_PLACEOBJECT2,&placeobject_tag);
834     register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag);
835     register_tag(ST_DEFINEFONT,&font_tag);
836     register_tag(ST_PROTECT,&protect_tag);
837     register_tag(ST_DEFINETEXT,&text_tag);
838     register_tag(ST_DEFINEBITSJPEG,&image_tag);
839     register_tag(ST_DEFINEBITSJPEG2,&image_tag);
840     register_tag(ST_DEFINEBITSJPEG3,&image_tag);
841     register_tag(ST_DEFINEBITSLOSSLESS,&image_tag);
842     register_tag(ST_DEFINEBITSLOSSLESS2,&image_tag);
843     register_tag(ST_SHOWFRAME,&showframe_tag);
844     register_tag(ST_DEFINEVIDEOSTREAM,&videostream_tag);
845     register_tag(ST_VIDEOFRAME,&videoframe_tag);
846     register_tag(ST_DEFINESPRITE,&sprite_tag);
847     register_tag(ST_END,&end_tag);
848
849     return TagMethods;
850 }