2 #include "primitives.h"
8 //----------------------------------------------------------------------------
10 typedef struct _font_internal
14 staticforward tag_internals_t font_tag;
16 static int font_parse(tag_internals_t*self)
18 font_internal_t*font = (font_internal_t*)self->data;
20 PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet"));
23 static void font_dealloc(tag_internals_t*self)
25 font_internal_t*font = (font_internal_t*)self->data;
27 swf_FontFree(font->font);
31 static int font_fillTAG(tag_internals_t*self)
33 font_internal_t*fi = (font_internal_t*)self->data;
36 self->tag = swf_InsertTag(0, ST_DEFINEFONT2);
37 swf_FontSetDefine2(self->tag, fi->font);
40 static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs)
42 static char *kwlist[] = {"filename", NULL};
47 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &filename))
50 font = swf_LoadFont(filename);
52 PyErr_SetString(PyExc_Exception, setError("Could not load %s", filename));
56 tag = tag_new(&font_tag);
57 tag_internals_t*itag = tag_getinternals(tag);
58 font_internal_t*fi = (font_internal_t*)itag->data;
61 return (PyObject*)tag;
63 static SWFFONT* font_getSWFFONT(PyObject*self)
65 PY_ASSERT_TYPE(self, &TagClass);
66 tag_internals_t*itag = tag_getinternals(self);
67 font_internal_t*fi = (font_internal_t*)itag->data;
70 static tag_internals_t font_tag =
73 fillTAG: font_fillTAG,
74 dealloc: font_dealloc,
76 datasize: sizeof(font_internal_t),
78 //----------------------------------------------------------------------------
80 typedef struct _placeobject_internal
83 } placeobject_internal_t;
84 staticforward tag_internals_t placeobject_tag;
86 static void po_dealloc(tag_internals_t*self)
88 placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
90 swf_PlaceObjectFree(pi->po);
94 static int po_parse(tag_internals_t*self)
96 placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
98 PyErr_SetString(PyExc_Exception, setError("Font parsing not implemented yet"));
101 static int po_fillTAG(tag_internals_t*self)
103 placeobject_internal_t*pi = (placeobject_internal_t*)self->data;
104 self->tag = swf_InsertTag(0, ST_PLACEOBJECT2);
105 swf_SetPlaceObject(self->tag, pi->po);
108 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
110 static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
112 PyObject*character = 0;
121 po = malloc(sizeof(SWFPLACEOBJECT));
122 memset(po, 0, sizeof(SWFPLACEOBJECT));
124 swf_GetPlaceObject(0, po);
126 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|O!O!isiO!", kwlist,
129 &MatrixClass, &matrix,
130 &CXFormClass, &cxform,
134 &ActionClass, &action
139 po->clipdepth = clipdepth;
142 if(clipdepth) po->clipdepth = clipdepth;
143 if(matrix) po->matrix = matrix_getMatrix(matrix);
144 if(cxform) po->cxform = colortransform_getCXForm(cxform);
145 if(action) po->actions = action_getAction(action);
148 tag = tag_new(&placeobject_tag);
149 tag_internals_t*itag = tag_getinternals(tag);
150 placeobject_internal_t*pi = (placeobject_internal_t*)itag->data;
152 pi->po->id = tagmap_add(itag->tagmap,(PyObject*)character);
154 mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt);
156 return (PyObject*)tag;
158 static tag_internals_t placeobject_tag =
164 datasize: sizeof(placeobject_internal_t),
166 //----------------------------------------------------------------------------
167 staticforward tag_internals_t bgcolor_tag;
168 static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other)
170 tag_internals_t*itag = tag_getinternals(self);
172 r = itag->tag->data[0];
173 g = itag->tag->data[1];
174 b = itag->tag->data[2];
175 return Py_BuildValue("(iii)", r,g,b);
177 static PyMethodDef setbgcolor_methods[] =
178 {{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"},
179 {NULL, NULL, 0, NULL}
181 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
183 static char *kwlist[] = {"color", NULL};
188 tag = tag_new(&bgcolor_tag);
189 tag_internals_t*itag = tag_getinternals(tag);
191 /* 1st try- copy constructor */
192 if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
194 /* 2nd try- color's contructor */
195 color = f_Color(NULL, args, kwargs);
200 itag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
201 RGBA rgba = color_getRGBA(color);
202 swf_SetU8(itag->tag, rgba.r);
203 swf_SetU8(itag->tag, rgba.g);
204 swf_SetU8(itag->tag, rgba.b);
205 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);
207 return (PyObject*)tag;
209 static tag_internals_t bgcolor_tag =
214 tagfunctions: setbgcolor_methods,
217 //----------------------------------------------------------------------------
218 staticforward tag_internals_t protect_tag;
219 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
221 static char *kwlist[] = {"password", NULL};
224 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
227 PyObject*tag = tag_new(&protect_tag);
228 tag_internals_t*itag = tag_getinternals(tag);
229 itag->tag = swf_InsertTag(0, ST_PROTECT);
231 swf_SetPassword(itag->tag, password);
233 mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt);
234 return (PyObject*)tag;
236 static tag_internals_t protect_tag =
244 //----------------------------------------------------------------------------
245 staticforward tag_internals_t end_tag;
246 static tag_internals_t end_tag =
254 //----------------------------------------------------------------------------
255 staticforward tag_internals_t text_tag;
257 typedef struct _text_internal
264 staticforward tag_internals_t placeobject_tag;
266 static int text_fillTAG(tag_internals_t*self)
268 text_internal_t*ti = (text_internal_t*)self->data;
269 self->tag= swf_InsertTag(0, ST_DEFINETEXT2);
270 swf_SetU16(self->tag, /*ID*/0);
271 SRECT r = swf_SetDefineText(self->tag, ti->swffont, &ti->rgba, ti->text, ti->size);
274 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
276 static char *kwlist[] = {"font", "text", "size", "color", NULL};
283 RGBA rgba = {255,0,0,0};
287 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!u#i|O!", kwlist, &TagClass, &font, &text, &textlen, &size, &ColorClass, &color))
290 unicode16 = PyUnicode_DecodeUTF16(text, textlen*2, NULL, NULL);
291 unicode8 = PyUnicode_AsUTF8String(unicode16);
292 text = PyString_AS_STRING(unicode8);
295 rgba = color_getRGBA(color);
297 mylog("DefineText: text = %s", text);
299 tag = tag_new(&text_tag);
300 tag_internals_t* itag = tag_getinternals(tag);
301 text_internal_t*ti = (text_internal_t*)itag->data;
303 ti->swffont = font_getSWFFONT(font);
304 int font_id = tagmap_add(itag->tagmap, font); // add dependency on font
305 ti->swffont->id = font_id; // for swf_SetDefineTexts
306 ti->text = strdup(text);
310 return (PyObject*)tag;
312 static tag_internals_t text_tag =
315 fillTAG: text_fillTAG,
318 datasize: sizeof(text_internal_t),
320 //----------------------------------------------------------------------------
321 staticforward tag_internals_t image_tag;
323 typedef struct _image_internal
333 staticforward tag_internals_t image_tag;
335 static int image_fillTAG(tag_internals_t*self)
337 image_internal_t*ti = (image_internal_t*)self->data;
338 self->tag= swf_InsertTag(0, ST_DEFINEBITSLOSSLESS2);
339 swf_SetU16(self->tag, /*ID*/0);
340 swf_SetLosslessBits(self->tag, ti->width, ti->height, ti->rgba, BMF_32BIT);
343 static void image_dealloc(tag_internals_t*self)
345 image_internal_t*pi = (image_internal_t*)self->data;
347 free(pi->rgba);pi->rgba = 0;
350 static PyObject* f_DefineImage(PyObject* self, PyObject* args, PyObject* kwargs)
352 static char *kwlist[] = {"image"};
354 PyObject*tag = tag_new(&image_tag);
356 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &image))
359 tag = tag_new(&image_tag);
360 tag_internals_t* itag = tag_getinternals(tag);
361 image_internal_t*ti = (image_internal_t*)itag->data;
363 ti->rgba = image_toRGBA(image);
364 if(!ti->rgba) // pass through exception
366 ti->width = image_getWidth(image);
367 ti->height = image_getHeight(image);
371 ti->size = ti->width*ti->height;
373 return (PyObject*)tag;
375 static tag_internals_t image_tag =
378 fillTAG: image_fillTAG,
379 dealloc: image_dealloc,
381 datasize: sizeof(image_internal_t),
383 //----------------------------------------------------------------------------
384 staticforward tag_internals_t shape_tag;
386 typedef struct _shape_internal
390 staticforward tag_internals_t shape_tag;
392 static int shape_fillTAG(tag_internals_t*self)
394 shape_internal_t*ti = (shape_internal_t*)self->data;
395 self->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
396 swf_SetU16(self->tag, /*ID*/0);
397 swf_SetShape2(self->tag, ti->shape);
400 static void shape_dealloc(tag_internals_t*self)
402 shape_internal_t*pi = (shape_internal_t*)self->data;
404 swf_Shape2Free(pi->shape);
408 static PyObject* f_DefineImageShape(PyObject* self, PyObject* args, PyObject* kwargs)
410 static char *kwlist[] = {"image"};
412 PyObject*tag = tag_new(&shape_tag);
415 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &TagClass, &image))
418 tag = tag_new(&shape_tag);
419 tag_internals_t* itag = tag_getinternals(tag);
420 shape_internal_t*ti = (shape_internal_t*)itag->data;
421 ti->shape = 0; /*HACK*/
423 int width = image_getWidth(image);
424 int height = image_getHeight(image);
425 int id = tagmap_add(itag->tagmap, image);
426 itag->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
427 swf_SetU16(itag->tag, 0);
428 swf_ShapeSetBitmapRect(itag->tag, id, width, height);
429 return (PyObject*)tag;
431 static tag_internals_t shape_tag =
434 fillTAG: shape_fillTAG,
435 dealloc: shape_dealloc,
437 datasize: sizeof(shape_internal_t),
439 //----------------------------------------------------------------------------
441 typedef struct _videostream_internal
445 } videostream_internal_t;
446 staticforward tag_internals_t videostream_tag;
447 staticforward tag_internals_t videoframe_tag;
449 static int videostream_parse(tag_internals_t*self)
451 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
453 PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet"));
456 static void videostream_dealloc(tag_internals_t*self)
458 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
459 if(videostream->stream) {
460 swf_VideoStreamClear(videostream->stream);
461 free(videostream->stream);
462 videostream->stream = 0;
465 static int videostream_fillTAG(tag_internals_t*self)
467 videostream_internal_t*fi = (videostream_internal_t*)self->data;
471 PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented yet"));return 0;
474 static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs)
476 PyObject*tag = tag_new(&videostream_tag);
478 int width=0,height=0,frames=65535;
479 static char *kwlist[] = {"width", "height", "frames", NULL};
480 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", kwlist, &TagClass, &width, &height, &frames))
483 tag_internals_t*itag = tag_getinternals(tag);
484 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
485 fi->stream = malloc(sizeof(VIDEOSTREAM));
486 memset(fi->stream, 0, sizeof(VIDEOSTREAM));
488 TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
489 swf_SetVideoStreamDefine(t, fi->stream, frames, width, height);
490 fi->lastiframe = -65536;
491 return (PyObject*)tag;
493 static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self)
495 PY_ASSERT_TYPE(self, &TagClass);
496 tag_internals_t*itag = tag_getinternals(self);
497 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
500 static PyObject* videostream_getbwidth(PyObject*self, PyObject*args)
502 tag_internals_t*itag = tag_getinternals(self);
503 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
504 int width = fi->stream->bbx;
505 return Py_BuildValue("i", width);
507 static PyObject* videostream_getbheight(PyObject*self, PyObject*args)
509 tag_internals_t*itag = tag_getinternals(self);
510 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
511 int height = fi->stream->bby;
512 return Py_BuildValue("i", height);
514 static PyObject* videostream_addFrame(PyObject*self, PyObject*args, PyObject*kwargs)
516 tag_internals_t*_itag = tag_getinternals(self);
517 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
520 char*type=0; // none, "i", "p"
522 static char *kwlist[] = {"image", "quant", "type", NULL};
523 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|is", kwlist, &image, &quant, &type))
525 if(fi->stream->width != image_getWidth(image)) {
526 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getWidth(image), fi->stream->width));return 0;
528 if(fi->stream->height != image_getHeight(image)) {
529 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getHeight(image), fi->stream->height));return 0;
531 PyObject*tag = tag_new(&videoframe_tag);
532 tag_internals_t*itag = tag_getinternals(tag);
534 RGBA*pic = image_toRGBA(image);
535 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
536 if((type && (type[0]=='I' || type[0]=='i')) || (type==0 && fi->lastiframe+64 < fi->stream->frame)) {
538 swf_SetVideoStreamIFrame(t, fi->stream, pic, quant);
539 fi->lastiframe = fi->stream->frame;
542 swf_SetVideoStreamPFrame(t, fi->stream, pic, quant);
545 tagmap_addMapping(itag->tagmap, 0, self);
547 return Py_BuildValue("s", 0);
549 static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args)
551 tag_internals_t*itag = tag_getinternals(self);
552 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
555 return Py_BuildValue("s", 0);
557 static PyMethodDef videostream_methods[] =
558 {{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"},
559 {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"},
560 {"addFrame", (PyCFunction)videostream_addFrame, METH_KEYWORDS, "add a Video Frame"},
561 {"addDistortionFrame", (PyCFunction)videostream_addDistortionFrame, METH_KEYWORDS, "add a MVD frame"},
562 {NULL, NULL, 0, NULL}
565 static tag_internals_t videostream_tag =
567 parse: videostream_parse,
568 fillTAG: videostream_fillTAG,
569 dealloc: videostream_dealloc,
570 tagfunctions: videostream_methods,
571 datasize: sizeof(videostream_internal_t),
574 //============================================================================
576 static tag_internals_t videoframe_tag =
585 //============================================================================
587 static PyMethodDef TagMethods[] =
590 {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
591 {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
592 {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
593 {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
594 {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
595 {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."},
596 {"Image", (PyCFunction)f_DefineImage, METH_KEYWORDS, "Create an SWF Image Tag."},
597 {"ImageShape", (PyCFunction)f_DefineImageShape, METH_KEYWORDS, "Create an SWF Image Shape Tag."},
598 {NULL, NULL, 0, NULL}
600 PyMethodDef* tags_getMethods()
602 TagClass.ob_type = &PyType_Type;
604 register_tag(ST_PLACEOBJECT,&placeobject_tag);
605 register_tag(ST_PLACEOBJECT2,&placeobject_tag);
606 register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag);
607 register_tag(ST_DEFINEFONT,&font_tag);
608 register_tag(ST_PROTECT,&protect_tag);
609 register_tag(ST_DEFINETEXT,&text_tag);
610 register_tag(ST_DEFINEBITSJPEG,&image_tag);
611 register_tag(ST_DEFINEBITSJPEG2,&image_tag);
612 register_tag(ST_DEFINEBITSJPEG3,&image_tag);
613 register_tag(ST_DEFINEBITSLOSSLESS,&image_tag);
614 register_tag(ST_DEFINEBITSLOSSLESS2,&image_tag);
615 register_tag(ST_END,&end_tag);