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* po_create(PyObject* self, PyObject* args, PyObject* kwargs,char move)
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;
143 if(clipdepth) po->clipdepth = clipdepth;
144 if(matrix) po->matrix = matrix_getMatrix(matrix);
145 if(cxform) po->cxform = colortransform_getCXForm(cxform);
146 if(action) po->actions = action_getAction(action);
149 tag = tag_new(&placeobject_tag);
150 tag_internals_t*itag = tag_getinternals(tag);
151 placeobject_internal_t*pi = (placeobject_internal_t*)itag->data;
153 pi->po->id = tagmap_add(itag->tagmap,(PyObject*)character);
155 mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt);
157 return (PyObject*)tag;
159 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
161 return po_create(self, args, kwargs, 0);
163 static PyObject* f_MoveObject(PyObject* self, PyObject* args, PyObject* kwargs)
165 return po_create(self, args, kwargs, 1);
167 static tag_internals_t placeobject_tag =
173 datasize: sizeof(placeobject_internal_t),
175 //----------------------------------------------------------------------------
176 staticforward tag_internals_t bgcolor_tag;
177 static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other)
179 tag_internals_t*itag = tag_getinternals(self);
181 r = itag->tag->data[0];
182 g = itag->tag->data[1];
183 b = itag->tag->data[2];
184 return Py_BuildValue("(iii)", r,g,b);
186 static PyMethodDef setbgcolor_methods[] =
187 {{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"},
188 {NULL, NULL, 0, NULL}
190 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
192 static char *kwlist[] = {"color", NULL};
197 tag = tag_new(&bgcolor_tag);
198 tag_internals_t*itag = tag_getinternals(tag);
200 /* 1st try- copy constructor */
201 if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
203 /* 2nd try- color's contructor */
204 color = f_Color(NULL, args, kwargs);
209 itag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
210 RGBA rgba = color_getRGBA(color);
211 swf_SetU8(itag->tag, rgba.r);
212 swf_SetU8(itag->tag, rgba.g);
213 swf_SetU8(itag->tag, rgba.b);
214 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);
216 return (PyObject*)tag;
218 static tag_internals_t bgcolor_tag =
223 tagfunctions: setbgcolor_methods,
226 //----------------------------------------------------------------------------
227 staticforward tag_internals_t protect_tag;
228 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
230 static char *kwlist[] = {"password", NULL};
233 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
236 PyObject*tag = tag_new(&protect_tag);
237 tag_internals_t*itag = tag_getinternals(tag);
238 itag->tag = swf_InsertTag(0, ST_PROTECT);
240 swf_SetPassword(itag->tag, password);
242 mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt);
243 return (PyObject*)tag;
245 static tag_internals_t protect_tag =
253 //----------------------------------------------------------------------------
254 staticforward tag_internals_t showframe_tag;
255 static PyObject* f_ShowFrame(PyObject* self, PyObject* args, PyObject* kwargs)
257 static char *kwlist[] = {"name", NULL};
259 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name))
262 PyObject*tag = tag_new(&showframe_tag);
263 tag_internals_t*itag = tag_getinternals(tag);
264 itag->tag = swf_InsertTag(0, ST_SHOWFRAME);
265 mylog("+%08x(%d) f_ShowFrame", (int)tag, tag->ob_refcnt);
266 return (PyObject*)tag;
268 static tag_internals_t showframe_tag =
276 //----------------------------------------------------------------------------
277 staticforward tag_internals_t end_tag;
278 static tag_internals_t end_tag =
286 //----------------------------------------------------------------------------
287 staticforward tag_internals_t text_tag;
289 typedef struct _text_internal
296 staticforward tag_internals_t placeobject_tag;
298 static int text_fillTAG(tag_internals_t*self)
300 text_internal_t*ti = (text_internal_t*)self->data;
301 self->tag= swf_InsertTag(0, ST_DEFINETEXT2);
302 swf_SetU16(self->tag, /*ID*/0);
303 SRECT r = swf_SetDefineText(self->tag, ti->swffont, &ti->rgba, ti->text, ti->size);
306 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
308 static char *kwlist[] = {"font", "text", "size", "color", NULL};
315 RGBA rgba = {255,0,0,0};
319 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!u#i|O!", kwlist, &TagClass, &font, &text, &textlen, &size, &ColorClass, &color))
322 unicode16 = PyUnicode_DecodeUTF16(text, textlen*2, NULL, NULL);
323 unicode8 = PyUnicode_AsUTF8String(unicode16);
324 text = PyString_AS_STRING(unicode8);
327 rgba = color_getRGBA(color);
329 mylog("DefineText: text = %s", text);
331 tag = tag_new(&text_tag);
332 tag_internals_t* itag = tag_getinternals(tag);
333 text_internal_t*ti = (text_internal_t*)itag->data;
335 ti->swffont = font_getSWFFONT(font);
336 int font_id = tagmap_add(itag->tagmap, font); // add dependency on font
337 ti->swffont->id = font_id; // for swf_SetDefineTexts
338 ti->text = strdup(text);
342 return (PyObject*)tag;
344 static tag_internals_t text_tag =
347 fillTAG: text_fillTAG,
350 datasize: sizeof(text_internal_t),
352 //----------------------------------------------------------------------------
353 staticforward tag_internals_t image_tag;
355 typedef struct _image_internal
365 staticforward tag_internals_t image_tag;
367 static int image_fillTAG(tag_internals_t*self)
369 image_internal_t*ti = (image_internal_t*)self->data;
370 self->tag= swf_InsertTag(0, ST_DEFINEBITSLOSSLESS2);
371 swf_SetU16(self->tag, /*ID*/0);
372 swf_SetLosslessBits(self->tag, ti->width, ti->height, ti->rgba, BMF_32BIT);
375 static void image_dealloc(tag_internals_t*self)
377 image_internal_t*pi = (image_internal_t*)self->data;
379 free(pi->rgba);pi->rgba = 0;
382 static int imagetag_getWidth(PyObject* self)
384 tag_internals_t*itag = tag_getinternals(self);
385 image_internal_t*pi = (image_internal_t*)itag->data;
388 static int imagetag_getHeight(PyObject* self)
390 tag_internals_t*itag = tag_getinternals(self);
391 image_internal_t*pi = (image_internal_t*)itag->data;
394 static PyObject* f_DefineImage(PyObject* self, PyObject* args, PyObject* kwargs)
396 static char *kwlist[] = {"image"};
398 PyObject*tag = tag_new(&image_tag);
400 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &image))
403 tag = tag_new(&image_tag);
404 tag_internals_t* itag = tag_getinternals(tag);
405 image_internal_t*ti = (image_internal_t*)itag->data;
407 ti->rgba = image_toRGBA(image);
408 if(!ti->rgba) // pass through exception
410 ti->width = image_getWidth(image);
411 ti->height = image_getHeight(image);
415 ti->size = ti->width*ti->height;
417 return (PyObject*)tag;
419 static tag_internals_t image_tag =
422 fillTAG: image_fillTAG,
423 dealloc: image_dealloc,
425 datasize: sizeof(image_internal_t),
427 //----------------------------------------------------------------------------
428 staticforward tag_internals_t shape_tag;
430 typedef struct _shape_internal
434 staticforward tag_internals_t shape_tag;
436 static int shape_fillTAG(tag_internals_t*self)
438 shape_internal_t*ti = (shape_internal_t*)self->data;
439 self->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
440 swf_SetU16(self->tag, /*ID*/0);
441 swf_SetShape2(self->tag, ti->shape);
444 static void shape_dealloc(tag_internals_t*self)
446 shape_internal_t*pi = (shape_internal_t*)self->data;
448 swf_Shape2Free(pi->shape);
452 static PyObject* f_DefineImageShape(PyObject* self, PyObject* args, PyObject* kwargs)
454 static char *kwlist[] = {"image"};
456 PyObject*tag = tag_new(&shape_tag);
459 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &TagClass, &image))
462 tag = tag_new(&shape_tag);
463 tag_internals_t* itag = tag_getinternals(tag);
464 shape_internal_t*ti = (shape_internal_t*)itag->data;
465 ti->shape = 0; /*HACK*/
467 int width = imagetag_getWidth(image);
468 int height = imagetag_getHeight(image);
469 int id = tagmap_add(itag->tagmap, image);
470 itag->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
471 swf_SetU16(itag->tag, 0);
472 swf_ShapeSetBitmapRect(itag->tag, id, width, height);
473 return (PyObject*)tag;
475 static tag_internals_t shape_tag =
478 fillTAG: shape_fillTAG,
479 dealloc: shape_dealloc,
481 datasize: sizeof(shape_internal_t),
483 //----------------------------------------------------------------------------
485 typedef struct _videostream_internal
489 } videostream_internal_t;
490 staticforward tag_internals_t videostream_tag;
491 staticforward tag_internals_t videoframe_tag;
493 static int videostream_parse(tag_internals_t*self)
495 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
497 PyErr_SetString(PyExc_Exception, setError("videostream parsing not implemented yet"));
500 static void videostream_dealloc(tag_internals_t*self)
502 videostream_internal_t*videostream = (videostream_internal_t*)self->data;
503 if(videostream->stream) {
504 swf_VideoStreamClear(videostream->stream);
505 free(videostream->stream);
506 videostream->stream = 0;
509 static int videostream_fillTAG(tag_internals_t*self)
511 videostream_internal_t*fi = (videostream_internal_t*)self->data;
514 PyErr_SetString(PyExc_Exception, setError("videostream filling not implemented"));
517 static PyObject* f_DefineVideoStream(PyObject* self, PyObject* args, PyObject* kwargs)
519 PyObject*tag = tag_new(&videostream_tag);
521 int width=0,height=0,frames=65535;
522 static char *kwlist[] = {"width", "height", "frames", NULL};
523 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", kwlist, &width, &height, &frames))
526 printf(": %d %d\n", width, height);
528 tag_internals_t*itag = tag_getinternals(tag);
529 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
530 fi->stream = malloc(sizeof(VIDEOSTREAM));
531 memset(fi->stream, 0, sizeof(VIDEOSTREAM));
533 TAG*t = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
534 swf_SetU16(t, 0); /* id */
535 swf_SetVideoStreamDefine(t, fi->stream, frames, width, height);
537 fi->lastiframe = -65536;
538 return (PyObject*)tag;
540 static VIDEOSTREAM* videostream_getVIDEOSTREAM(PyObject*self)
542 PY_ASSERT_TYPE(self, &TagClass);
543 tag_internals_t*itag = tag_getinternals(self);
544 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
547 static PyObject* videostream_getbwidth(PyObject*self, PyObject*args)
549 tag_internals_t*itag = tag_getinternals(self);
550 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
551 int width = fi->stream->bbx;
552 return Py_BuildValue("i", width);
554 static PyObject* videostream_getbheight(PyObject*self, PyObject*args)
556 tag_internals_t*itag = tag_getinternals(self);
557 videostream_internal_t*fi = (videostream_internal_t*)itag->data;
558 int height = fi->stream->bby;
559 return Py_BuildValue("i", height);
561 static PyObject* videostream_addFrame(PyObject*self, PyObject*args, PyObject*kwargs)
563 tag_internals_t*_itag = tag_getinternals(self);
564 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
567 char*type=0; // none, "i", "p"
569 static char *kwlist[] = {"image", "quant", "type", NULL};
570 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|is", kwlist, &image, &quant, &type))
572 if(fi->stream->width != image_getWidth(image)) {
573 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getWidth(image), fi->stream->width));return 0;
575 if(fi->stream->height != image_getHeight(image)) {
576 PyErr_SetString(PyExc_Exception, setError("bad image width %d!=%d", image_getHeight(image), fi->stream->height));return 0;
578 PyObject*tag = tag_new(&videoframe_tag);
579 tag_internals_t*itag = tag_getinternals(tag);
581 RGBA*pic = image_toRGBA(image);
584 TAG* t = swf_InsertTag(0, ST_VIDEOFRAME);
585 if((type && (type[0]=='I' || type[0]=='i')) || (type==0 && fi->lastiframe+64 < fi->stream->frame)) {
587 swf_SetVideoStreamIFrame(t, fi->stream, pic, quant);
588 fi->lastiframe = fi->stream->frame;
591 swf_SetVideoStreamPFrame(t, fi->stream, pic, quant);
594 tagmap_addMapping(itag->tagmap, 0, self);
598 static PyObject* videostream_addDistortionFrame(PyObject*self, PyObject*args)
600 tag_internals_t*_itag = tag_getinternals(self);
601 videostream_internal_t*fi = (videostream_internal_t*)_itag->data;
603 PyObject*tag = tag_new(&videoframe_tag);
604 tag_internals_t*itag = tag_getinternals(tag);
606 TAG* t = swf_InsertTag(0, ST_REFLEX);
611 static PyMethodDef videostream_methods[] =
612 {{"xblocks", videostream_getbwidth, METH_VARARGS, "get's the number of horizontal blocks"},
613 {"yblocks", videostream_getbheight, METH_VARARGS, "get's the number of vertical blocks"},
614 {"addFrame", (PyCFunction)videostream_addFrame, METH_KEYWORDS, "add a Video Frame"},
615 {"addDistortionFrame", (PyCFunction)videostream_addDistortionFrame, METH_KEYWORDS, "add a MVD frame"},
616 {NULL, NULL, 0, NULL}
619 static tag_internals_t videostream_tag =
621 parse: videostream_parse,
622 fillTAG: videostream_fillTAG,
623 dealloc: videostream_dealloc,
624 tagfunctions: videostream_methods,
625 datasize: sizeof(videostream_internal_t),
628 //============================================================================
630 static tag_internals_t videoframe_tag =
639 //============================================================================
641 static PyMethodDef TagMethods[] =
644 {"BackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
645 {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
646 {"Font", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
647 {"Text", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
648 {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
649 {"MoveObject", (PyCFunction)f_MoveObject, METH_KEYWORDS, "Create a PlaceObject Move Tag."},
650 {"VideoStream", (PyCFunction)f_DefineVideoStream, METH_KEYWORDS, "Create a Videostream."},
651 {"Image", (PyCFunction)f_DefineImage, METH_KEYWORDS, "Create an SWF Image Tag."},
652 {"ImageShape", (PyCFunction)f_DefineImageShape, METH_KEYWORDS, "Create an SWF Image Shape Tag."},
653 {"ShowFrame", (PyCFunction)f_ShowFrame, METH_KEYWORDS, "Create an SWF Show Frame Tag."},
654 {NULL, NULL, 0, NULL}
656 PyMethodDef* tags_getMethods()
658 TagClass.ob_type = &PyType_Type;
660 register_tag(ST_PLACEOBJECT,&placeobject_tag);
661 register_tag(ST_PLACEOBJECT2,&placeobject_tag);
662 register_tag(ST_SETBACKGROUNDCOLOR,&bgcolor_tag);
663 register_tag(ST_DEFINEFONT,&font_tag);
664 register_tag(ST_PROTECT,&protect_tag);
665 register_tag(ST_DEFINETEXT,&text_tag);
666 register_tag(ST_DEFINEBITSJPEG,&image_tag);
667 register_tag(ST_DEFINEBITSJPEG2,&image_tag);
668 register_tag(ST_DEFINEBITSJPEG3,&image_tag);
669 register_tag(ST_DEFINEBITSLOSSLESS,&image_tag);
670 register_tag(ST_DEFINEBITSLOSSLESS2,&image_tag);
671 register_tag(ST_END,&end_tag);