added png save support
[swftools.git] / lib / python / tags.c
index a89c105..2434d44 100644 (file)
@@ -5,6 +5,7 @@
 #include "tag.h"
 #include "tags.h"
 #include "image.h"
+#include "../png.h"
 
 //----------------------------------------------------------------------------
 
@@ -14,7 +15,7 @@ typedef struct _font_internal
 } font_internal_t;
 staticforward tag_internals_t font_tag;
 
-static int font_parse(tag_internals_t*self, PyObject*swftagmap)
+static int font_parse(tag_internals_t*self)
 {
     font_internal_t*font = (font_internal_t*)self->data;
     /* TODO */
@@ -95,7 +96,7 @@ static void po_dealloc(tag_internals_t*self)
        pi->po = 0;
     }
 }
-static int po_parse(tag_internals_t*self, PyObject*swftagmap)
+static int po_parse(tag_internals_t*self)
 {
     placeobject_internal_t*i = (placeobject_internal_t*)self->data;
     if(i->po)
@@ -127,7 +128,7 @@ static int po_fillTAG(tag_internals_t*self)
 static PyObject* po_getattr(tag_internals_t*self,char*a)
 {
     placeobject_internal_t*i = (placeobject_internal_t*)self->data;
-    if(!shape_parse(itag,0))
+    if(!po_parse(self))
        return PY_ERROR("Couldn't parse placeobject");
     if(!strcmp(a, "character")) {
        if(!i->character)
@@ -559,6 +560,22 @@ static void image_dealloc(tag_internals_t*self)
        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(self->tag);self->tag = 0;
+    return 1;
+}
 static int imagetag_getWidth(PyObject* self)
 {
     tag_internals_t*itag = tag_getinternals(self);
@@ -596,14 +613,48 @@ static PyObject* f_DefineImage(PyObject* self, PyObject* args, PyObject* kwargs)
 
     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 shape");
+    image_internal_t*fi = (image_internal_t*)itag->data;
+   
+    char*filename = 0;
+    if(!PyArg_ParseTuple(args, "s", &filename))
+       return NULL;
+
+    writePNG(filename, 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: 0,
+    parse: image_parse,
     fillTAG: image_fillTAG,
     dealloc: image_dealloc,
-    getattr: 0, 
+    getattr: image_getattr, 
     setattr: 0,
-    tagfunctions: 0,
+    tagfunctions: image_methods,
     datasize: sizeof(image_internal_t),
 };
 //----------------------------------------------------------------------------
@@ -623,7 +674,7 @@ static int shape_fillTAG(tag_internals_t*self)
     swf_SetShape2(self->tag, ti->shape2);
     return 1;
 }
-static int shape_parse(tag_internals_t*self, PyObject*swftagmap)
+static int shape_parse(tag_internals_t*self)
 {
     shape_internal_t*i= (shape_internal_t*)self->data;
     if(i->shape2)
@@ -741,7 +792,7 @@ static PyObject* f_DefineShape(PyObject* self, PyObject* args, PyObject* kwargs)
 static PyObject* shape_getfillstyles(PyObject*self, PyObject*args)
 {
     tag_internals_t*itag = tag_getinternals(self);
-    if(!shape_parse(itag,0))
+    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;
@@ -750,7 +801,7 @@ static PyObject* shape_getfillstyles(PyObject*self, PyObject*args)
 static PyObject* shape_getlinestyles(PyObject*self, PyObject*args)
 {
     tag_internals_t*itag = tag_getinternals(self);
-    if(!shape_parse(itag,0))
+    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;
@@ -759,7 +810,7 @@ static PyObject* shape_getlinestyles(PyObject*self, PyObject*args)
 static PyObject* shape_getfillstyle(PyObject*self, PyObject*args)
 {
     tag_internals_t*itag = tag_getinternals(self);
-    if(!shape_parse(itag,0))
+    if(!shape_parse(itag))
        return PY_ERROR("Couldn't parse shape");
     shape_internal_t*fi = (shape_internal_t*)itag->data;
     int nr = 0;
@@ -774,7 +825,7 @@ static PyObject* shape_getfillstyle(PyObject*self, PyObject*args)
 static PyObject* shape_getlinestyle(PyObject*self, PyObject*args)
 {
     tag_internals_t*itag = tag_getinternals(self);
-    if(!shape_parse(itag,0))
+    if(!shape_parse(itag))
        return PY_ERROR("Couldn't parse shape");
     shape_internal_t*fi = (shape_internal_t*)itag->data;
     int nr = 0;
@@ -789,7 +840,7 @@ static PyObject* shape_getlinestyle(PyObject*self, PyObject*args)
 static PyObject* shape_setfillstyle(PyObject*self, PyObject*args)
 {
     tag_internals_t*itag = tag_getinternals(self);
-    if(!shape_parse(itag,0))
+    if(!shape_parse(itag))
        return PY_ERROR("Couldn't parse shape");
     shape_internal_t*fi = (shape_internal_t*)itag->data;
     int nr = 0;
@@ -806,7 +857,7 @@ static PyObject* shape_setfillstyle(PyObject*self, PyObject*args)
 static PyObject* shape_setlinestyle(PyObject*self, PyObject*args)
 {
     tag_internals_t*itag = tag_getinternals(self);
-    if(!shape_parse(itag,0))
+    if(!shape_parse(itag))
        return PY_ERROR("Couldn't parse shape");
     shape_internal_t*fi = (shape_internal_t*)itag->data;
     int nr = 0;
@@ -850,7 +901,7 @@ typedef struct _videostream_internal
 staticforward tag_internals_t videostream_tag;
 staticforward tag_internals_t videoframe_tag;
 
-static int videostream_parse(tag_internals_t*self, PyObject*swftagmap)
+static int videostream_parse(tag_internals_t*self)
 {
     videostream_internal_t*videostream = (videostream_internal_t*)self->data;
     /* TODO */