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