fixed tag organisation, started video stuff
[swftools.git] / lib / python / tags.c
1 #include "pyutils.h"
2 #include "primitives.h"
3 #include "action.h"
4 #include "tag.h"
5 #include "tags.h"
6
7 //----------------------------------------------------------------------------
8
9 typedef struct _font_internal
10 {
11     SWFFONT* font;
12 } font_internal_t;
13 staticforward tag_internals_t font_tag;
14
15 static int font_parse(tag_internals_t*self)
16 {
17     font_internal_t*font = (font_internal_t*)self->data;
18     /* TODO */
19     PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet"));
20     return 0;
21 }
22 static void font_dealloc(tag_internals_t*self)
23 {
24     font_internal_t*font = (font_internal_t*)self->data;
25     if(font->font) {
26         swf_FontFree(font->font);
27         font->font = 0;
28     }
29 }
30 static int font_fillTAG(tag_internals_t*self)
31 {
32     font_internal_t*fi = (font_internal_t*)self->data;
33     if(self->tag)
34         return 1;
35     self->tag = swf_InsertTag(0, ST_DEFINEFONT2);
36     swf_FontSetDefine2(self->tag, fi->font);
37     return 1;
38 }
39 static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs)
40 {
41     static char *kwlist[] = {"filename", NULL};
42     char*filename = 0;
43     PyObject*tag;
44     SWFFONT* font;
45
46     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &filename))
47         return NULL;
48
49     font = swf_LoadFont(filename);
50     if(!font) {
51         PyErr_SetString(PyExc_Exception, setError("Could not load %s", filename));
52         return NULL;
53     }
54
55     tag = tag_new(&font_tag);
56     tag_internals_t*itag = tag_getinternals(tag);
57     font_internal_t*fi = (font_internal_t*)itag->data;
58     font->id = 0;
59     fi->font = font;
60     return (PyObject*)tag;
61 }
62 static SWFFONT* font_getSWFFONT(PyObject*self)
63 {
64     PY_ASSERT_TYPE(self, &TagClass);
65     tag_internals_t*itag = tag_getinternals(self);
66     font_internal_t*fi = (font_internal_t*)itag->data;
67     return fi->font;
68 }
69 static tag_internals_t font_tag =
70 {
71     parse: font_parse,
72     fillTAG: font_fillTAG,
73     dealloc: font_dealloc,
74     tagfunctions: 0,
75     datasize: sizeof(font_internal_t),
76 };
77 //----------------------------------------------------------------------------
78
79 typedef struct _placeobject_internal
80 {
81     SWFPLACEOBJECT* po;
82 } placeobject_internal_t;
83 staticforward tag_internals_t placeobject_tag;
84
85 static void po_dealloc(tag_internals_t*self)
86 {
87     placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
88     if(pi->po) {
89         swf_PlaceObjectFree(pi->po);
90         pi->po = 0;
91     }
92 }
93 static int po_parse(tag_internals_t*self)
94 {
95     placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
96     /* TODO */
97     PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet"));
98     return 0;
99 }
100 static int po_fillTAG(tag_internals_t*self)
101 {
102     placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
103     self->tag = swf_InsertTag(0, ST_PLACEOBJECT2);
104     swf_SetPlaceObject(self->tag, pi->po);
105     return 1;
106 }
107 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
108 {
109     static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
110     
111     PyObject*character = 0;
112     int depth;
113     int clipdepth = 0;
114     PyObject*matrix = 0;
115     PyObject*cxform = 0;
116     PyObject*action = 0;
117     int ratio = 0;
118     char* name = 0;
119     SWFPLACEOBJECT* po;
120     po = malloc(sizeof(SWFPLACEOBJECT));
121     memset(po, 0, sizeof(SWFPLACEOBJECT));
122
123     swf_GetPlaceObject(0, po);
124
125     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|O!O!isiO!", kwlist, 
126                 &character, 
127                 &depth, 
128                 &MatrixClass, &matrix, 
129                 &CXFormClass, &cxform,
130                 &ratio,
131                 &name,
132                 &clipdepth,
133                 &ActionClass, &action
134                 ))
135         return NULL;
136     po->depth = depth;
137     po->id = /*ID*/ 0;
138     po->clipdepth = clipdepth;
139     po->ratio = ratio;
140     po->name = name;
141     if(clipdepth) po->clipdepth = clipdepth;
142     if(matrix) po->matrix = matrix_getMatrix(matrix);
143     if(cxform) po->cxform = colortransform_getCXForm(cxform);
144     if(action) po->actions = action_getAction(action);
145
146     PyObject*tag;
147     tag = tag_new(&placeobject_tag);
148     tag_internals_t*itag = tag_getinternals(tag);
149     placeobject_internal_t*pi = (placeobject_internal_t*)itag->data;
150     pi->po = po;
151     pi->po->id = tagmap_add(itag->tagmap,(PyObject*)character);
152     
153     mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt);
154
155     return (PyObject*)tag;
156 }
157 static tag_internals_t placeobject_tag =
158 {
159     parse: po_parse,
160     fillTAG: po_fillTAG,
161     dealloc: po_dealloc,
162     tagfunctions: 0,
163     datasize: sizeof(placeobject_internal_t),
164 };
165 //----------------------------------------------------------------------------
166 staticforward tag_internals_t bgcolor_tag;
167 static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other)
168 {
169     tag_internals_t*itag = tag_getinternals(self);
170     int r,g,b;
171     r = itag->tag->data[0];
172     g = itag->tag->data[1];
173     b = itag->tag->data[2];
174     return Py_BuildValue("(iii)", r,g,b);
175 }
176 static PyMethodDef setbgcolor_methods[] = 
177 {{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"},
178  {NULL, NULL, 0, NULL}
179 };
180 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
181 {
182     static char *kwlist[] = {"color", NULL};
183     int r=0,g=0,b=0;
184     PyObject*tag;
185     PyObject*color;
186     
187     tag = tag_new(&bgcolor_tag);
188     tag_internals_t*itag = tag_getinternals(tag);
189
190     /* 1st try- copy constructor */
191     if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
192         PyErr_Clear();
193         /* 2nd try- color's contructor */
194         color = f_Color(NULL, args, kwargs);
195     }
196     if(!color)
197         return NULL;
198
199     itag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
200     RGBA rgba = color_getRGBA(color);
201     swf_SetU8(itag->tag, rgba.r);
202     swf_SetU8(itag->tag, rgba.g);
203     swf_SetU8(itag->tag, rgba.b);
204     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);
205     Py_DECREF(color);
206     return (PyObject*)tag;
207 }
208 static tag_internals_t bgcolor_tag =
209 {
210     parse: 0,
211     fillTAG: 0,
212     dealloc: 0,
213     tagfunctions: setbgcolor_methods,
214     datasize: 0,
215 };
216 //----------------------------------------------------------------------------
217 staticforward tag_internals_t protect_tag;
218 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
219 {
220     static char *kwlist[] = {"password", NULL};
221     char*password = 0;
222
223     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
224         return NULL;
225
226     PyObject*tag = tag_new(&protect_tag);
227     tag_internals_t*itag = tag_getinternals(tag);
228     itag->tag = swf_InsertTag(0, ST_PROTECT);
229     if(password) {
230         swf_SetPassword(itag->tag, password);
231     }
232     mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt);
233     return (PyObject*)tag;
234 }
235 static tag_internals_t protect_tag =
236 {
237     parse: 0,
238     fillTAG: 0,
239     dealloc: 0,
240     tagfunctions: 0,
241     datasize: 0,
242 };
243 //----------------------------------------------------------------------------
244 staticforward tag_internals_t end_tag;
245 static tag_internals_t end_tag =
246 {
247     parse: 0,
248     fillTAG: 0,
249     dealloc: 0,
250     tagfunctions: 0,
251     datasize: 0,
252 };
253 //----------------------------------------------------------------------------
254 staticforward tag_internals_t text_tag;
255
256 typedef struct _text_internal
257 {
258     char*text;
259     SWFFONT* swffont;
260     RGBA rgba;
261     int size;
262 } text_internal_t;
263 staticforward tag_internals_t placeobject_tag;
264
265 static int text_fillTAG(tag_internals_t*self)
266 {
267     text_internal_t*ti = (text_internal_t*)self->data;
268     self->tag= swf_InsertTag(0, ST_DEFINETEXT2);
269     swf_SetU16(self->tag, /*ID*/0);
270     SRECT r = swf_SetDefineText(self->tag, ti->swffont, &ti->rgba, ti->text, ti->size);
271     return 1;
272 }
273 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
274 {
275     static char *kwlist[] = {"font", "text", "size", "color", NULL};
276     PyObject*tag = 0;
277     char*text = 0;
278     int textlen = 0;
279     PyObject*unicode16;
280     PyObject*unicode8;
281     int size = 0;
282     RGBA rgba = {255,0,0,0};
283     PyObject*color = 0;
284     PyObject*font = 0;
285
286     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!u#i|O!", kwlist, &TagClass, &font, &text, &textlen, &size, &ColorClass, &color))
287         return NULL;
288     
289     unicode16 = PyUnicode_DecodeUTF16(text, textlen*2, NULL, NULL);
290     unicode8 = PyUnicode_AsUTF8String(unicode16);
291     text = PyString_AS_STRING(unicode8);
292
293     if(color)
294         rgba = color_getRGBA(color);
295
296     mylog("DefineText: text = %s", text);
297     
298     tag = tag_new(&text_tag);
299     tag_internals_t* itag = tag_getinternals(tag);
300     text_internal_t*ti = (text_internal_t*)itag->data;
301
302     ti->swffont = font_getSWFFONT(font);
303     int font_id = tagmap_add(itag->tagmap, font); // add dependency on font
304     ti->swffont->id = font_id; // for swf_SetDefineTexts
305     ti->text = strdup(text);
306     ti->rgba = rgba;
307     ti->size = size;
308
309     return (PyObject*)tag;
310 }
311 static tag_internals_t text_tag =
312 {
313     parse: 0,
314     fillTAG: text_fillTAG,
315     dealloc: 0,
316     tagfunctions: 0,
317     datasize: sizeof(text_internal_t),
318 };
319 //----------------------------------------------------------------------------
320
321 typedef struct _videostream_internal
322 {
323     VIDEOSTREAM* stream;
324 } videostream_internal_t;
325 staticforward tag_internals_t videostream_tag;
326
327 static int videostream_parse(tag_internals_t*self)
328 {
329     videostream_internal_t*videostream = (videostream_internal_t*)self->data;
330     /* TODO */
331     PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet"));
332     return 0;
333 }
334 static void videostream_dealloc(tag_internals_t*self)
335 {
336     videostream_internal_t*videostream = (videostream_internal_t*)self->data;
337     if(videostream->stream) {
338         swf_VideoStreamClear(videostream->stream);
339         free(videostream->stream);
340         videostream->stream = 0;
341     }
342 }
343 static int videostream_fillTAG(tag_internals_t*self)
344 {
345     videostream_internal_t*fi = (videostream_internal_t*)self->data;
346     if(self->tag)
347         return 1;
348     /* TODO */
349     PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented yet"));return 0;
350     return 1;
351 }
352 static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs)
353 {
354     PyObject*tag = tag_new(&videostream_tag);
355    
356     int width=0,height=0,quant=7,frames=65535;
357     static char *kwlist[] = {"width", "height", "quant", "frames", NULL};
358     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|ii", kwlist, &TagClass, &width, &height, &quant, &frames))
359         return NULL;
360     
361     tag_internals_t*itag = tag_getinternals(tag);
362     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
363     fi->stream = malloc(sizeof(VIDEOSTREAM));
364     memset(fi->stream, 0, sizeof(VIDEOSTREAM));
365
366     TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
367     swf_SetVideoStreamDefine(t, fi->stream, frames, width, height);
368     return (PyObject*)tag;
369 }
370 static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self)
371 {
372     PY_ASSERT_TYPE(self, &TagClass);
373     tag_internals_t*itag = tag_getinternals(self);
374     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
375     return fi->stream;
376 }
377 static PyObject* videostream_getbwidth(PyObject*self, PyObject*args)
378 {
379     tag_internals_t*itag = tag_getinternals(self);
380     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
381     int width = fi->stream->bbx;
382     return Py_BuildValue("i", width);
383 }
384 static PyObject* videostream_getbheight(PyObject*self, PyObject*args)
385 {
386     tag_internals_t*itag = tag_getinternals(self);
387     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
388     int height = fi->stream->bby;
389     return Py_BuildValue("i", height);
390 }
391 static PyObject* videostream_addIFrame(PyObject*self, PyObject*args)
392 {
393     tag_internals_t*itag = tag_getinternals(self);
394     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
395     /* TODO */
396     return Py_BuildValue("s", 0);
397 }
398 static PyObject* videostream_addPFrame(PyObject*self, PyObject*args)
399 {
400     tag_internals_t*itag = tag_getinternals(self);
401     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
402     /* TODO */
403     return Py_BuildValue("s", 0);
404 }
405 static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args)
406 {
407     tag_internals_t*itag = tag_getinternals(self);
408     videostream_internal_t*fi = (videostream_internal_t*)itag->data;
409     /* TODO */
410     return Py_BuildValue("s", 0);
411 }
412 static PyMethodDef videostream_methods[] = 
413 {{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"},
414  {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"},
415  {"addIFrame", videostream_addIFrame, METH_VARARGS, "add a I Video Frame"},
416  {"addPFrame", videostream_addPFrame, METH_VARARGS, "add a P Video Frame"},
417  {"addDistortionFrame", videostream_addDistortionFrame, METH_VARARGS, "add a MVD frame"},
418  {NULL, NULL, 0, NULL}
419 };
420
421 static tag_internals_t videostream_tag =
422 {
423     parse: videostream_parse,
424     fillTAG: videostream_fillTAG,
425     dealloc: videostream_dealloc,
426     tagfunctions: videostream_methods,
427     datasize: sizeof(videostream_internal_t),
428 };
429
430 //============================================================================
431
432 static PyMethodDef TagMethods[] = 
433 {
434     /* TAGS */
435     {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
436     {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
437     {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
438     {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
439     {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
440     {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."},
441     {NULL, NULL, 0, NULL}
442 };
443 PyMethodDef* tags_getMethods()
444 {
445     TagClass.ob_type = &PyType_Type;
446     
447     register_tag(ST_PLACEOBJECT,&placeobject_tag);
448     register_tag(ST_PLACEOBJECT2,&placeobject_tag);
449     register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag);
450     register_tag(ST_DEFINEFONT,&font_tag);
451     register_tag(ST_PROTECT,&protect_tag);
452     register_tag(ST_DEFINETEXT,&text_tag);
453     register_tag(ST_END,&end_tag);
454
455     return TagMethods;
456 }
457