X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fpython%2Fimage.c;h=5b125e623fde37444c734c9974640c7dd5eee427;hb=879d0eec420fe0fd5ddcd56c8fe62b82a6744edd;hp=f855c8f92b49b386279f934336162ba3a5a8dbfb;hpb=129968d66dd1bb375a99c7741198e8e9c4eee2e1;p=swftools.git diff --git a/lib/python/image.c b/lib/python/image.c index f855c8f..5b125e6 100644 --- a/lib/python/image.c +++ b/lib/python/image.c @@ -1,23 +1,160 @@ #include +#ifdef HAVE_STAT +#undef HAVE_STAT +#endif +//#include "/usr/include/python2.3/Imaging.h" +#include "../../config.h" +#ifdef HAVE_PYTHON_IMAGING +#include +#endif +#include "pyutils.h" #undef HAVE_STAT #include "../rfxswf.h" -int image_getWidth(PyObject*image) -{ +/* redefine the ImagingObject struct defined in _imagingmodule.c */ +/* there should be a better way to do this... */ +typedef struct { + PyObject_HEAD +#ifdef HAVE_PYTHON_IMAGING + Imaging image; +#endif +} ImagingObject; + +int image_getWidth(PyObject*_image) { +#ifdef HAVE_PYTHON_IMAGING + if(strcmp(_image->ob_type->tp_name, "ImagingCore")) { + PyErr_SetString(PyExc_Exception, setError("not an image: %s", _image->ob_type->tp_name)); + return 0; + } + ImagingObject*image = (ImagingObject*)_image; + return image->image->xsize; +#else + PyErr_SetString(PyExc_Exception, "imaging not compiled in"); return 0; +#endif } -int image_getHeight(PyObject*image) -{ +int image_getHeight(PyObject*_image) { +#ifdef HAVE_PYTHON_IMAGING + if(strcmp(_image->ob_type->tp_name, "ImagingCore")) { + PyErr_SetString(PyExc_Exception, setError("not an image: %s", _image->ob_type->tp_name)); + return 0; + } + ImagingObject*image = (ImagingObject*)_image; + return image->image->ysize; +#else + PyErr_SetString(PyExc_Exception, "imaging not compiled in"); + return 0; +#endif +} + +int image_getBPP(PyObject*_image) { +#ifdef HAVE_PYTHON_IMAGING + if(strcmp(_image->ob_type->tp_name, "ImagingCore")) { + PyErr_SetString(PyExc_Exception, setError("not an image: %s", _image->ob_type->tp_name)); + return 0; + } + ImagingObject*image = (ImagingObject*)_image; + if(!strcmp(image->image->mode, "1") || + !strcmp(image->image->mode, "L") || + !strcmp(image->image->mode, "P")) { + return 8; + } + if(!strcmp(image->image->mode, "I") || + !strcmp(image->image->mode, "F")) { + return 32; + } + if(!strcmp(image->image->mode, "RGB") || + !strcmp(image->image->mode, "RGBA") || + !strcmp(image->image->mode, "CMYK") || + !strcmp(image->image->mode, "YCbCr")) { + return 32; + } + PyErr_SetString(PyExc_Exception, setError("Unknown image format (%s).", image->image->mode)); + return 0; +#else + PyErr_SetString(PyExc_Exception, "imaging not compiled in"); return 0; +#endif } -int image_getBPP(PyObject*image) +RGBA* image_toRGBA(PyObject*_image) { +#ifdef HAVE_PYTHON_IMAGING + if(strcmp(_image->ob_type->tp_name, "ImagingCore")) { + PyErr_SetString(PyExc_Exception, setError("not an image: %s", _image->ob_type->tp_name)); + return 0; + } + ImagingObject*image = (ImagingObject*)_image; + printf("mode: %s\n", image->image->mode); + printf("depth: %d\n", image->image->depth); + printf("bands: %d\n", image->image->bands); + printf("xsize: %d\n", image->image->xsize); + printf("ysize: %d\n", image->image->ysize); + int bpp = image_getBPP(_image); + if(!bpp) + return 0; + + RGBA*rgba = (RGBA*)malloc(image->image->xsize * image->image->ysize * sizeof(RGBA)); + + if(!strcmp(image->image->mode, "RGBA")) { + int y,ymax=image->image->ysize; + int width = image->image->xsize; + RGBA*dest = rgba; + for(y=0;yimage->image32[y]); + int x; + for(x=0;ximage->mode)); +#else + PyErr_SetString(PyExc_Exception, "imaging not compiled in"); +#endif return 0; } -RGBA* image_toRGBA(PyObject*image) +#ifdef HAVE_PYTHON_IMAGING +extern PyObject*PyImagingNew(Imaging imOut); +#endif + +PyObject* rgba_to_image(RGBA*rgba, int width, int height) { +#ifdef HAVE_PYTHON_IMAGING +#ifndef WIN32 + Imaging img = ImagingNew("RGBA", width, height); + int y; + if(!img->image32) { + fprintf(stderr, "No array allocated!\n"); + return 0; + } + for(y=0;yimage32[y]); + RGBA* src = &rgba[width*y]; + int x; + for(x=0;x