Index: source/blender/python/api2_2x/BGL.c =================================================================== RCS file: /cvsroot/bf-blender/blender/source/blender/python/api2_2x/BGL.c,v retrieving revision 1.8 diff -u -r1.8 BGL.c --- source/blender/python/api2_2x/BGL.c 24 Jun 2003 07:18:11 -0000 1.8 +++ source/blender/python/api2_2x/BGL.c 15 Feb 2004 18:45:16 -0000 @@ -1491,4 +1491,14 @@ EXPP_ADDCONST(GL_TEXTURE_BINDING_2D); return mod; +} + +void *BGL_BufferAsVoid(PyObject *py_obj) +{ + return ((Buffer*)py_obj)->buf.asvoid; +} + +int BGL_BufferCheckObject(PyObject *py_obj) +{ + return (py_obj->ob_type == &buffer_Type); } Index: source/blender/python/api2_2x/BGL.h =================================================================== RCS file: /cvsroot/bf-blender/blender/source/blender/python/api2_2x/BGL.h,v retrieving revision 1.6 diff -u -r1.6 BGL.h --- source/blender/python/api2_2x/BGL.h 26 Jun 2003 02:03:46 -0000 1.6 +++ source/blender/python/api2_2x/BGL.h 15 Feb 2004 18:45:17 -0000 @@ -423,3 +423,6 @@ /* #endif */ PyObject *BGL_Init(void); +void *BGL_BufferAsVoid(PyObject *buffer); +int BGL_BufferCheckObject(PyObject *py_obj); + Index: source/blender/python/api2_2x/Image.c =================================================================== RCS file: /cvsroot/bf-blender/blender/source/blender/python/api2_2x/Image.c,v retrieving revision 1.15 diff -u -r1.15 Image.c --- source/blender/python/api2_2x/Image.c 23 Nov 2003 17:46:05 -0000 1.15 +++ source/blender/python/api2_2x/Image.c 15 Feb 2004 18:45:19 -0000 @@ -34,10 +34,16 @@ #include #include #include +#include /* for FILE_MAX... */ #include /* for the IB_rect define */ +#include +#include +#include /* for free_realtime_image */ #include "gen_utils.h" #include "Image.h" +#include "modules.h" /* for BGL_BufferAsVoid */ + /*****************************************************************************/ /* Python BPy_Image defaults: */ @@ -230,9 +236,12 @@ static PyObject *Image_getDepth(BPy_Image *self); static PyObject *Image_getXRep(BPy_Image *self); static PyObject *Image_getYRep(BPy_Image *self); +static PyObject *Image_save(BPy_Image *self); static PyObject *Image_setName(BPy_Image *self, PyObject *args); static PyObject *Image_setXRep(BPy_Image *self, PyObject *args); static PyObject *Image_setYRep(BPy_Image *self, PyObject *args); +static PyObject *Image_readPixels(BPy_Image *self, PyObject *args); +static PyObject *Image_drawPixels(BPy_Image *self, PyObject *args); /*****************************************************************************/ /* Python BPy_Image methods table: */ @@ -253,10 +262,16 @@ "() - Return Image object y repetition value"}, {"setName", (PyCFunction)Image_setName, METH_VARARGS, "(str) - Change Image object name"}, + {"save", (PyCFunction)Image_save, METH_NOARGS, + "() - Save Image to file"}, {"setXRep", (PyCFunction)Image_setXRep, METH_VARARGS, "(int) - Change Image object x repetition value"}, {"setYRep", (PyCFunction)Image_setYRep, METH_VARARGS, "(int) - Change Image object y repetition value"}, + {"readPixels", (PyCFunction)Image_readPixels, METH_VARARGS, + "(int, int, int, int, Buffer) - Read pixels from image buffer."}, + {"drawPixels", (PyCFunction)Image_drawPixels, METH_VARARGS, + "(int, int, int, int, Buffer) - Draw pixels to image buffer."}, {0} }; @@ -483,6 +498,136 @@ return Py_None; } +static PyObject *Image_readPixels(BPy_Image *self, PyObject *args) +{ + Image *image = self->image; + int x, y, w, h, i, j; + PyObject *buffer; + unsigned char *rect, *buf; + + if (!image->ibuf) + load_image(image, IB_rect, "", 0); + if (!image->ibuf) + return EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't load image data in Blender"); + + if (!PyArg_ParseTuple(args, "iiiiO", &x, &y, &w, &h, &buffer)) + return (EXPP_ReturnPyObjError (PyExc_ValueError, + "expected four ints and a Buffer as arguments")); + + if (!BGL_BufferCheckObject(buffer)) + return (EXPP_ReturnPyObjError (PyExc_ValueError, + "expected four ints and a Buffer as arguments")); + + if (!(x >= 0 && y >= 0 && w + x <= image->ibuf->x && y + h <= image->ibuf->y)) + return (EXPP_ReturnPyObjError (PyExc_ValueError, + "invalid coordinates")); + + /* ibuf->rect is an array of unsigned ints, each containg 4 unsigned chars, + respectively representing RGBA */ + buf = (unsigned char*)BGL_BufferAsVoid(buffer); + rect = (unsigned char*)image->ibuf->rect; + for (i = 0; i < h; i++) + for (j = 0; j < w * 4; j++) + buf[i*w*4 + j] = rect[(i+y)*image->ibuf->x*4 + x*4 + j]; + + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject *Image_drawPixels(BPy_Image *self, PyObject *args) +{ + Image *image = self->image; + int x, y, w, h, i, j, channel = 4; + PyObject *buffer; + unsigned char *rect, *buf; + + if (image->packedfile) + return EXPP_ReturnPyObjError (PyExc_RuntimeError, + "can't paint packed image"); + + if (!image->ibuf) + load_image(image, IB_rect, "", 0); + if (!image->ibuf) + return EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't load image data in Blender"); + + if (!PyArg_ParseTuple(args, "iiiiO|i", &x, &y, &w, &h, &buffer, &channel)) + return (EXPP_ReturnPyObjError (PyExc_ValueError, + "expected four ints and a Buffer as arguments")); + + if (!BGL_BufferCheckObject(buffer)) + return (EXPP_ReturnPyObjError (PyExc_ValueError, + "expected four ints and a Buffer as arguments")); + + if (!(x >= 0 && y >= 0 && w + x <= image->ibuf->x && y + h <= image->ibuf->y)) + return (EXPP_ReturnPyObjError (PyExc_ValueError, + "invalid coordinates")); + + if (channel < 0 || channel > 4) + return (EXPP_ReturnPyObjError (PyExc_ValueError, + "invalid channel")); + + /* ibuf->rect is an array of unsigned ints, each containg 4 unsigned chars, + respectively representing RGBA */ + buf = (unsigned char*)BGL_BufferAsVoid(buffer); + rect = (unsigned char*)image->ibuf->rect; + if (channel == 4) { + for (i = 0; i < h; i++) + for (j = 0; j < w * 4; j++) + rect[(i+y)*image->ibuf->x*4 + x*4 + j] = buf[i*w*4 + j]; + } else { + for (i = 0; i < h; i++) + for (j = 0; j < w; j++) + rect[(i+y)*image->ibuf->x*4 + (x+j)*4 + channel] = buf[i*w + j]; + } + + /* make clear that the image has been modified */ + image->ibuf->userflags |= IB_BITMAPDIRTY; + /* force opengl reload */ + free_realtime_image(image); + + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject *Image_save(BPy_Image *self) +{ + Image *image = self->image; + ImBuf *ibuf; + char str[FILE_MAXDIR+FILE_MAXFILE]; + + if (image->packedfile) + return EXPP_ReturnPyObjError (PyExc_RuntimeError, + "can't save packed image"); + + if (!image->name) + return EXPP_ReturnPyObjError (PyExc_RuntimeError, + "can't save image, no valid filename"); + + if (!image->ibuf) + load_image(image, IB_rect, "", 0); + if (!image->ibuf) + return EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't load image data in Blender"); + + BLI_strncpy(str, image->name, sizeof(str)); + BLI_convertstringcode(str, G.sce, G.scene->r.cfra); + + ibuf = IMB_dupImBuf(image->ibuf); + if (ibuf) { + if (BIF_write_ibuf(ibuf, str)) { + image->ibuf->userflags &= ~IB_BITMAPDIRTY; + } else + return EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't save image"); + IMB_freeImBuf(ibuf); + } + + Py_INCREF(Py_None); + return Py_None; +} + /*****************************************************************************/ /* Function: Image_getAttr */ /* Description: This is a callback function for the BPy_Image type. It is */ @@ -505,10 +650,12 @@ attr = PyInt_FromLong(self->image->xrep); else if (strcmp(name, "yrep") == 0) attr = PyInt_FromLong(self->image->yrep); + else if (strcmp(name, "packed") == 0) + attr = PyInt_FromLong((self->image->packedfile) ? 1: 0); else if (strcmp(name, "__members__") == 0) attr = Py_BuildValue("[s,s,s,s,s,s]", - "name", "filename", "size", "depth", "xrep", "yrep"); + "name", "filename", "size", "depth", "xrep", "yrep", "packed"); if (!attr) return (EXPP_ReturnPyObjError (PyExc_MemoryError, Index: source/blender/python/api2_2x/modules.h =================================================================== RCS file: /cvsroot/bf-blender/blender/source/blender/python/api2_2x/modules.h,v retrieving revision 1.35 diff -u -r1.35 modules.h --- source/blender/python/api2_2x/modules.h 23 Nov 2003 17:46:05 -0000 1.35 +++ source/blender/python/api2_2x/modules.h 15 Feb 2004 18:45:19 -0000 @@ -180,6 +180,9 @@ /* Noise */ PyObject * Noise_Init (void); +/* BGL */ +void *BGL_BufferAsVoid(PyObject *buffer); +int BGL_BufferCheckObject(PyObject *py_obj); /* Init functions for other modules */ PyObject * Window_Init (void);