+staticforward tag_internals_t image_tag;
+
+typedef struct _image_internal
+{
+ RGBA*rgba;
+ int size;
+ int width;
+ int height;
+ int bpp;
+ char isindexed;
+ char islossless;
+} image_internal_t;
+staticforward tag_internals_t image_tag;
+
+static int image_fillTAG(tag_internals_t*self)
+{
+ image_internal_t*ti = (image_internal_t*)self->data;
+ self->tag= swf_InsertTag(0, ST_DEFINEBITSLOSSLESS2);
+ swf_SetU16(self->tag, /*ID*/0);
+ swf_SetLosslessBits(self->tag, ti->width, ti->height, ti->rgba, BMF_32BIT);
+ return 1;
+}
+static void image_dealloc(tag_internals_t*self)
+{
+ image_internal_t*pi = (image_internal_t*)self->data;
+ if(pi->rgba) {
+ free(pi->rgba);pi->rgba = 0;
+ }
+}
+static int image_parse(tag_internals_t*self)
+{
+ image_internal_t*i= (image_internal_t*)self->data;
+ if(i->rgba)
+ return 1;
+ if(!self->tag)
+ return 0;
+
+ i->rgba = swf_ExtractImage(self->tag, &i->width, &i->height);
+ i->bpp = 32;
+ i->isindexed = 0;
+ i->islossless = 1;
+
+ swf_DeleteTag(0, self->tag);self->tag = 0;
+ return 1;
+}
+static int imagetag_getWidth(PyObject* self)
+{
+ tag_internals_t*itag = tag_getinternals(self);
+ image_internal_t*pi = (image_internal_t*)itag->data;
+ return pi->width;
+}
+static int imagetag_getHeight(PyObject* self)
+{
+ tag_internals_t*itag = tag_getinternals(self);
+ image_internal_t*pi = (image_internal_t*)itag->data;
+ return pi->height;
+}
+static PyObject* f_DefineImage(PyObject* self, PyObject* args, PyObject* kwargs)
+{
+ static char *kwlist[] = {"image", NULL};
+ PyObject*image = 0;
+ PyObject*tag = tag_new(&image_tag);
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &image))
+ return NULL;
+
+ tag = tag_new(&image_tag);
+ tag_internals_t* itag = tag_getinternals(tag);
+ image_internal_t*ti = (image_internal_t*)itag->data;
+
+ ti->rgba = image_toRGBA(image);
+ if(!ti->rgba) // pass through exception
+ return 0;
+ ti->width = image_getWidth(image);
+ ti->height = image_getHeight(image);
+ ti->isindexed = 0;
+ ti->islossless = 1;
+ ti->bpp = 32;
+ ti->size = ti->width*ti->height;
+
+ return (PyObject*)tag;
+}
+static PyObject* image_getattr(tag_internals_t*self,char*a)
+{
+ image_internal_t*i = (image_internal_t*)self->data;
+ if(!strcmp(a, "image")) {
+ if(!i->rgba) {
+ image_parse(self);
+ }
+ PyObject* image = rgba_to_image(i->rgba, i->width, i->height);
+ return image;
+ }
+ return 0;
+}
+
+static PyObject* image_save(PyObject*self, PyObject*args)
+{
+ tag_internals_t*itag = tag_getinternals(self);
+ if(!image_parse(itag))
+ return PY_ERROR("Couldn't parse image");
+ image_internal_t*fi = (image_internal_t*)itag->data;
+
+ char*filename = 0;
+ if(!PyArg_ParseTuple(args, "s", &filename))
+ return NULL;
+
+ writePNG(filename, (unsigned char*)fi->rgba ,fi->width, fi->height);
+
+ return PY_NONE;
+}
+
+static PyMethodDef image_methods[] =
+{{"save", image_save, METH_VARARGS, "saves an image as PNG"},
+ {NULL, NULL, 0, NULL}
+};
+
+static tag_internals_t image_tag =
+{
+ parse: image_parse,
+ fillTAG: image_fillTAG,
+ dealloc: image_dealloc,
+ getattr: image_getattr,
+ setattr: 0,
+ tagfunctions: image_methods,
+ datasize: sizeof(image_internal_t),
+};
+//----------------------------------------------------------------------------
+staticforward tag_internals_t shape_tag;
+
+typedef struct _shape_internal
+{
+ SHAPE2*shape2;
+} shape_internal_t;
+staticforward tag_internals_t shape_tag;
+
+static int shape_fillTAG(tag_internals_t*self)
+{
+ shape_internal_t*ti = (shape_internal_t*)self->data;
+ self->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
+ swf_SetU16(self->tag, /*ID*/0);
+ swf_SetShape2(self->tag, ti->shape2);
+ return 1;
+}
+static int shape_parse(tag_internals_t*self)
+{
+ shape_internal_t*i= (shape_internal_t*)self->data;
+ if(i->shape2)
+ return 1;
+ if(!self->tag)
+ return 0;
+ SHAPE2* shape2 = malloc(sizeof(SHAPE2));
+ swf_ParseDefineShape(self->tag, shape2);
+ i->shape2 = shape2;
+ swf_DeleteTag(0, self->tag);self->tag = 0;
+ return 1;
+}
+static void shape_dealloc(tag_internals_t*self)
+{
+ shape_internal_t*pi = (shape_internal_t*)self->data;
+ if(pi->shape2) {
+ swf_Shape2Free(pi->shape2);
+ pi->shape2 = 0;
+ }
+}
+static PyObject* f_DefineImageShape(PyObject* self, PyObject* args, PyObject* kwargs)
+{
+ static char *kwlist[] = {"image", NULL};
+ PyObject*image = 0;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &TagClass, &image))
+ return NULL;
+
+ PyObject*tag = tag_new(&shape_tag);
+ tag_internals_t* itag = tag_getinternals(tag);
+ shape_internal_t*ti = (shape_internal_t*)itag->data;
+ ti->shape2 = 0; /*HACK*/
+
+ int width = imagetag_getWidth(image);
+ int height = imagetag_getHeight(image);
+ int id = tagmap_add(itag->tagmap, image);
+ itag->tag= swf_InsertTag(0, ST_DEFINESHAPE3);
+ swf_SetU16(itag->tag, 0);
+ swf_ShapeSetBitmapRect(itag->tag, id, width, height);
+ return (PyObject*)tag;
+}
+
+/* TODO: move to lib/ */
+SHAPE2*swf_StringToShape2(char*s,FILLSTYLE*f, LINESTYLE*l)
+{
+ drawer_t draw;
+ swf_Shape11DrawerInit(&draw, 0);
+ draw_string(&draw, s);
+ draw.finish(&draw);
+ SHAPE*s1 = swf_ShapeDrawerToShape(&draw);
+ SRECT r = swf_ShapeDrawerGetBBox(&draw);
+ RGBA col;col.r=col.g=col.b=128;col.a=255;
+ if(l)
+ swf_ShapeAddLineStyle(s1, 1, &col);
+ if(f)
+ swf_ShapeAddSolidFillStyle(s1, &col);
+ draw.dealloc(&draw);
+ SHAPE2*shape2 = swf_ShapeToShape2(s1);
+ swf_ShapeFree(s1);
+ shape2->bbox = malloc(sizeof(SRECT));
+ *(shape2->bbox) = r;
+ if(f && shape2->numfillstyles)
+ shape2->fillstyles[0] = *f;
+ if(l && shape2->numlinestyles)
+ shape2->linestyles[0] = *l;
+ return shape2;
+}
+
+static PyObject* f_DefineShape(PyObject* self, PyObject* args, PyObject* kwargs)
+{
+ static char *kwlist[] = {"s", "fill", "line", NULL};
+ char*s = 0;
+ PyObject*fillstyle=0,*linestyle=0;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OO", kwlist, &s,&fillstyle,&linestyle))
+ return NULL;
+
+ PyObject*tag = tag_new(&shape_tag);
+ tag_internals_t* itag = tag_getinternals(tag);
+ shape_internal_t*ti = (shape_internal_t*)itag->data;
+
+ FILLSTYLE _f,*f=0;
+ LINESTYLE _l,*l=0;
+
+ if(fillstyle) {
+ f = &_f;
+ if(PY_CHECK_TYPE(fillstyle, &ColorClass)) {
+ f->type = /*FILL_SOLID*/ 0;
+ f->color = color_getRGBA(fillstyle);
+ } else {
+ return PY_ERROR("Invalid Fillstyle");
+ }
+ }
+
+ if(linestyle) {
+ l = &_l;
+ if(PyTuple_Check(linestyle) && PyTuple_GET_SIZE(linestyle)==2) {
+ float f = 0.0;
+ PyObject*color = 0;
+ if(!PyArg_ParseTuple(linestyle, "fO!", &f, &ColorClass, &color))
+ return 0;
+
+ l->width = (int)(f*20);
+ l->color = color_getRGBA(color);
+ } else {
+ return PY_ERROR("Invalid Linestyle");
+ }
+ }
+ ti->shape2 = swf_StringToShape2(s,f,l);
+
+ itag->tag = 0;
+
+ return (PyObject*)tag;
+}
+static PyObject* shape_getfillstyles(PyObject*self, PyObject*args)
+{
+ tag_internals_t*itag = tag_getinternals(self);
+ if(!shape_parse(itag))
+ return PY_ERROR("Couldn't parse shape");
+ shape_internal_t*fi = (shape_internal_t*)itag->data;
+ int num = fi->shape2->numfillstyles;
+ return Py_BuildValue("i", num);
+}
+static PyObject* shape_getlinestyles(PyObject*self, PyObject*args)
+{
+ tag_internals_t*itag = tag_getinternals(self);
+ if(!shape_parse(itag))
+ return PY_ERROR("Couldn't parse shape");
+ shape_internal_t*fi = (shape_internal_t*)itag->data;
+ int num = fi->shape2->numlinestyles;
+ return Py_BuildValue("i", num);
+}
+static PyObject* shape_getfillstyle(PyObject*self, PyObject*args)
+{
+ tag_internals_t*itag = tag_getinternals(self);
+ if(!shape_parse(itag))
+ return PY_ERROR("Couldn't parse shape");
+ shape_internal_t*fi = (shape_internal_t*)itag->data;
+ int nr = 0;
+ if(!PyArg_ParseTuple(args, "i", &nr))
+ return NULL;
+
+ int num = fi->shape2->numfillstyles;
+ if(nr < 0 || nr >=num)
+ return PY_ERROR("fillstyle index out of range");
+ return f_FillStyle2(fi->shape2->fillstyles[nr]);
+}
+static PyObject* shape_getlinestyle(PyObject*self, PyObject*args)
+{
+ tag_internals_t*itag = tag_getinternals(self);
+ if(!shape_parse(itag))
+ return PY_ERROR("Couldn't parse shape");
+ shape_internal_t*fi = (shape_internal_t*)itag->data;
+ int nr = 0;
+ if(!PyArg_ParseTuple(args, "i", &nr))
+ return NULL;
+
+ int num = fi->shape2->numfillstyles;
+ if(nr < 0 || nr >=num)
+ return PY_ERROR("fillstyle index out of range");
+ return f_LineStyle3(fi->shape2->linestyles[nr]);
+}
+static PyObject* shape_setfillstyle(PyObject*self, PyObject*args)
+{
+ tag_internals_t*itag = tag_getinternals(self);
+ if(!shape_parse(itag))
+ return PY_ERROR("Couldn't parse shape");
+ shape_internal_t*fi = (shape_internal_t*)itag->data;
+ int nr = 0;
+ PyObject*fs = 0;
+ if(!PyArg_ParseTuple(args, "iO!", &nr, &FillStyleClass, &fs))
+ return NULL;
+
+ int num = fi->shape2->numfillstyles;
+ if(nr < 0 || nr >=num)
+ return PY_ERROR("fillstyle index out of range");
+ fi->shape2->fillstyles[nr] = fillstyle_getFillStyle(fs);
+ return PY_NONE;
+}
+static PyObject* shape_setlinestyle(PyObject*self, PyObject*args)
+{
+ tag_internals_t*itag = tag_getinternals(self);
+ if(!shape_parse(itag))
+ return PY_ERROR("Couldn't parse shape");
+ shape_internal_t*fi = (shape_internal_t*)itag->data;
+ int nr = 0;
+ PyObject*ls = 0;
+ if(!PyArg_ParseTuple(args, "iO!", &nr, &LineStyleClass, &ls))
+ return NULL;
+
+ int num = fi->shape2->numlinestyles;
+ if(nr < 0 || nr >=num)
+ return PY_ERROR("linestyle index out of range");
+ fi->shape2->linestyles[nr] = linestyle_getLineStyle(ls);
+ return PY_NONE;
+}
+static PyMethodDef shape_methods[] =
+{{"numfillstyles", shape_getfillstyles, METH_VARARGS, "get's the number of fillstyles"},
+ {"numlinestyles", shape_getlinestyles, METH_VARARGS, "get's the number of linestyles"},
+ {"getfillstyle", shape_getfillstyle, METH_VARARGS, "get's one fillstyle"},
+ {"getlinestyle", shape_getlinestyle, METH_VARARGS, "get's one linestyle"},
+ {"setfillstyle", shape_setfillstyle, METH_VARARGS, "set's one fillstyle"},
+ {"setlinestyle", shape_setlinestyle, METH_VARARGS, "set's one linestyle"},
+ {NULL, NULL, 0, NULL}
+};
+
+static tag_internals_t shape_tag =
+{
+ parse: shape_parse,
+ fillTAG: shape_fillTAG,
+ dealloc: shape_dealloc,
+ getattr: 0,
+ setattr: 0,
+ tagfunctions: shape_methods,
+ datasize: sizeof(shape_internal_t),
+};
+//----------------------------------------------------------------------------