Index: Image.c =================================================================== --- Image.c (revision 11725) +++ Image.c (working copy) @@ -38,6 +38,7 @@ #include "BKE_library.h" #include "BKE_image.h" #include "BKE_idprop.h" +#include "BKE_utildefines.h" #include "BIF_drawimage.h" #include "BLI_blenlib.h" #include "DNA_space_types.h" /* FILE_MAXDIR = 160 */ @@ -126,11 +127,11 @@ static PyMethodDef BPy_Image_methods[] = { /* name, method, flags, doc */ {"getPixelF", ( PyCFunction ) Image_getPixelF, METH_VARARGS, - "(int, int) - Get pixel color as floats 0.0-1.0 returns [r,g,b,a]"}, + "(int, int) - Get pixel color as floats returns [r,g,b,a]"}, {"getPixelI", ( PyCFunction ) Image_getPixelI, METH_VARARGS, "(int, int) - Get pixel color as ints 0-255 returns [r,g,b,a]"}, {"setPixelF", ( PyCFunction ) Image_setPixelF, METH_VARARGS, - "(int, int, [f r,f g,f b,f a]) - Set pixel color using floats 0.0-1.0"}, + "(int, int, [f r,f g,f b,f a]) - Set pixel color using floats"}, {"setPixelI", ( PyCFunction ) Image_setPixelI, METH_VARARGS, "(int, int, [i r, i g, i b, i a]) - Set pixel color using ints 0-255"}, {"getMaxXY", ( PyCFunction ) Image_getMaxXY, METH_NOARGS, @@ -382,27 +383,25 @@ /** * getPixelF( x, y ) * returns float list of pixel colors in rgba order. - * returned values are floats normalized to 0.0 - 1.0. - * blender images are all 4x8 bit at the moment apr-2005 - */ + * returned values are floats , in the full range of the float image source. + */ static PyObject *Image_getPixelF( BPy_Image * self, PyObject * args ) { PyObject *attr; ImBuf *ibuf= BKE_image_get_ibuf(self->image, NULL); - char *pixel; /* image data */ int index; /* offset into image data */ int x = 0; int y = 0; - int pixel_size = 4; /* each pixel is 4 x 8-bits packed in unsigned int */ + int pixel_size = 4; /* each pixel is 4 x 32 bit float */ int i; if( !PyArg_ParseTuple( args, "ii", &x, &y ) ) return EXPP_ReturnPyObjError( PyExc_TypeError, "expected 2 integers" ); - if( !ibuf || !ibuf->rect ) /* loading didn't work */ + if( !ibuf || (!ibuf->rect_float && !ibuf->rect)) /* loading didn't work */ return EXPP_ReturnPyObjError( PyExc_RuntimeError, "couldn't load image data in Blender" ); @@ -428,15 +427,26 @@ "couldn't allocate memory for color list" ); index = ( x + y * ibuf->x ) * pixel_size; - - pixel = ( char * ) ibuf->rect; - for (i=0; i<4; i++) { - PyList_SetItem( attr, i, PyFloat_FromDouble( ( ( double ) pixel[index+i] ) / 255.0 )); + + /* if a float buffer exists, use it, otherwise convert the 8bpc buffer to float values */ + if (ibuf->rect_float) { + float *pixelf; /* image data */ + + pixelf = ibuf->rect_float; + for (i=0; i<4; i++) { + PyList_SetItem( attr, i, PyFloat_FromDouble( (double)pixelf[index+i] ) ); + } + } else { + char *pixelc; /* image data */ + + pixelc = ( char * ) ibuf->rect; + for (i=0; i<4; i++) { + PyList_SetItem( attr, i, PyFloat_FromDouble( ( ( double ) pixelc[index+i] ) / 255.0 )); + } } return attr; } - /** * getPixelI( x, y ) * returns integer list of pixel colors in rgba order. @@ -497,12 +507,11 @@ static PyObject *Image_setPixelF( BPy_Image * self, PyObject * args ) { ImBuf *ibuf= BKE_image_get_ibuf(self->image, NULL); - char *pixel; /* image data */ + float *pixel; /* image data */ int index; /* offset into image data */ int x = 0; int y = 0; - int a = 0; - int pixel_size = 4; /* each pixel is 4 x 8-bits packed in unsigned int */ + int pixel_size = 4; /* each pixel is 4 x 32 bit float */ float p[4]; if( !PyArg_ParseTuple @@ -510,7 +519,7 @@ return EXPP_ReturnPyObjError( PyExc_TypeError, "expected 2 integers and an array of 4 floats" ); - if( !ibuf || !ibuf->rect ) /* didn't work */ + if( !ibuf ) /* didn't work */ return EXPP_ReturnPyObjError( PyExc_RuntimeError, "couldn't load image data in Blender" ); @@ -522,29 +531,17 @@ || y > ( ibuf->y - 1 ) || x < ibuf->xorig || y < ibuf->yorig ) return EXPP_ReturnPyObjError( PyExc_RuntimeError, - "x or y is out of ruange" ); + "x or y is out of range" ); - for( a = 0; a < 4; a++ ) { - if( p[a] > 1.0 || p[a] < 0.0 ) - return EXPP_ReturnPyObjError( PyExc_RuntimeError, - "r, g, b, or a is out of range" ); - } - - - /* - assumption: from looking at source, skipx is often not set, - so we calc ourselves - */ - + /* if no float buffer already exists, add it */ + if (!ibuf->rect_float) imb_addrectfloatImBuf(ibuf); + index = ( x + y * ibuf->x ) * pixel_size; - pixel = ( char * ) ibuf->rect; + pixel = ibuf->rect_float + index; + + QUATCOPY(pixel, p); - pixel[index] = ( char ) ( p[0] * 255.0 ); - pixel[index + 1] = ( char ) ( p[1] * 255.0 ); - pixel[index + 2] = ( char ) ( p[2] * 255.0 ); - pixel[index + 3] = ( char ) ( p[3] * 255.0 ); - ibuf->userflags |= IB_BITMAPDIRTY; Py_RETURN_NONE; }