[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [14067] trunk/blender/source/blender/ python/api2_2x: Moved recent addition of get/setPixelF to get/ setPixelHDR and kept get/setPixelF limited to 0.0-1.0 range, to prevent existing scripts breaking.
Campbell Barton
ideasman42 at gmail.com
Wed Mar 12 12:14:04 CET 2008
Revision: 14067
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=14067
Author: campbellbarton
Date: 2008-03-12 12:13:57 +0100 (Wed, 12 Mar 2008)
Log Message:
-----------
Moved recent addition of get/setPixelF to get/setPixelHDR and kept get/setPixelF limited to 0.0-1.0 range, to prevent existing scripts breaking.
Modified Paths:
--------------
trunk/blender/source/blender/python/api2_2x/Image.c
trunk/blender/source/blender/python/api2_2x/doc/Image.py
Modified: trunk/blender/source/blender/python/api2_2x/Image.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/Image.c 2008-03-12 10:41:47 UTC (rev 14066)
+++ trunk/blender/source/blender/python/api2_2x/Image.c 2008-03-12 11:13:57 UTC (rev 14067)
@@ -111,8 +111,10 @@
static PyObject *Image_glLoad( BPy_Image * self );
static PyObject *Image_glFree( BPy_Image * self );
static PyObject *Image_getPixelF( BPy_Image * self, PyObject * args );
+static PyObject *Image_getPixelHDR( BPy_Image * self, PyObject * args );
static PyObject *Image_getPixelI( BPy_Image * self, PyObject * args );
static PyObject *Image_setPixelF( BPy_Image * self, PyObject * args );
+static PyObject *Image_setPixelHDR( BPy_Image * self, PyObject * args );
static PyObject *Image_setPixelI( BPy_Image * self, PyObject * args );
static PyObject *Image_getMaxXY( BPy_Image * self );
static PyObject *Image_getMinXY( BPy_Image * self );
@@ -129,10 +131,14 @@
/* name, method, flags, doc */
{"getPixelF", ( PyCFunction ) Image_getPixelF, METH_VARARGS,
"(int, int) - Get pixel color as floats returns [r,g,b,a]"},
+ {"getPixelHDR", ( PyCFunction ) Image_getPixelHDR, METH_VARARGS,
+ "(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"},
+ {"setPixelHDR", ( PyCFunction ) Image_setPixelHDR, METH_VARARGS,
+ "(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,
@@ -384,12 +390,12 @@
/**
- * getPixelF( x, y )
+ * getPixelHDR( x, y )
* returns float list of pixel colors in rgba order.
* returned values are floats , in the full range of the float image source.
*/
-static PyObject *Image_getPixelF( BPy_Image * self, PyObject * args )
+static PyObject *Image_getPixelHDR( BPy_Image * self, PyObject * args )
{
PyObject *attr;
@@ -505,9 +511,66 @@
}
+/**
+ * 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
+ */
+
+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 i;
+
+ if( !PyArg_ParseTuple( args, "ii", &x, &y ) )
+ return EXPP_ReturnPyObjError( PyExc_TypeError,
+ "expected 2 integers" );
+
+ if( !ibuf || !ibuf->rect ) /* loading didn't work */
+ return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+ "couldn't load image data in Blender" );
+
+ if( ibuf->type == 1 ) /* bitplane image */
+ return EXPP_ReturnPyObjError( PyExc_TypeError,
+ "unsupported bitplane image format" );
+
+ if( x > ( ibuf->x - 1 )
+ || y > ( ibuf->y - 1 )
+ || x < ibuf->xorig || y < ibuf->yorig )
+ return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+ "x or y is out of range" );
+
+ /*
+ assumption: from looking at source, skipx is often not set,
+ so we calc ourselves
+ */
+
+ attr = PyList_New(4);
+
+ if (!attr)
+ return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+ "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 ));
+ }
+ return attr;
+}
+
/* set pixel as floats */
-static PyObject *Image_setPixelF( BPy_Image * self, PyObject * args )
+static PyObject *Image_setPixelHDR( BPy_Image * self, PyObject * args )
{
ImBuf *ibuf= BKE_image_get_ibuf(self->image, NULL);
float *pixel; /* image data */
@@ -606,7 +669,63 @@
Py_RETURN_NONE;
}
+/* set pixel as floats */
+static PyObject *Image_setPixelF( BPy_Image * self, PyObject * args )
+{
+ 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 a = 0;
+ int pixel_size = 4; /* each pixel is 4 x 8-bits packed in unsigned int */
+ float p[4];
+
+ if( !PyArg_ParseTuple
+ ( args, "ii(ffff)", &x, &y, &p[0], &p[1], &p[2], &p[3] ) )
+ return EXPP_ReturnPyObjError( PyExc_TypeError,
+ "expected 2 integers and an array of 4 floats" );
+
+ if( !ibuf || !ibuf->rect ) /* didn't work */
+ return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+ "couldn't load image data in Blender" );
+
+ if( ibuf->type == 1 ) /* bitplane image */
+ return EXPP_ReturnPyObjError( PyExc_TypeError,
+ "unsupported bitplane image format" );
+
+ if( x > ( ibuf->x - 1 )
+ || y > ( ibuf->y - 1 )
+ || x < ibuf->xorig || y < ibuf->yorig )
+ return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+ "x or y is out of ruange" );
+
+ 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
+ */
+
+ index = ( x + y * ibuf->x ) * pixel_size;
+
+ pixel = ( char * ) ibuf->rect;
+
+ 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;
+}
+
/* get max extent of image */
static PyObject *Image_getMaxXY( BPy_Image * self )
Modified: trunk/blender/source/blender/python/api2_2x/doc/Image.py
===================================================================
--- trunk/blender/source/blender/python/api2_2x/doc/Image.py 2008-03-12 10:41:47 UTC (rev 14066)
+++ trunk/blender/source/blender/python/api2_2x/doc/Image.py 2008-03-12 11:13:57 UTC (rev 14067)
@@ -141,7 +141,7 @@
@rtype: int
"""
- def getPixelF(x, y):
+ def getPixelHDR(x, y):
"""
Get the the colors of the current pixel in the form [r,g,b,a].
For float image types, returned values can be greater then the useual [0.0, 1.0] range.
@@ -153,6 +153,20 @@
@param x: the x coordinate of pixel.
@param y: the y coordinate of pixel.
"""
+
+ def getPixelF(x, y):
+ """
+ Get the the colors of the current pixel in the form [r,g,b,a].
+ Returned values are floats normalized to 0.0 - 1.0.
+ Pixel coordinates are in the range from 0 to N-1. See L{getMaxXY}
+ @returns: [ r, g, b, a]
+ @rtype: list of 4 floats
+ @type x: int
+ @type y: int
+ @param x: the x coordinate of pixel.
+ @param y: the y coordinate of pixel.
+ """
+
def getPixelI(x, y):
"""
Get the the colors of the current pixel in the form [r,g,b,a].
@@ -309,7 +323,7 @@
@param speed: The new value in [1, 100].
"""
- def setPixelF(x, y, (r, g, b,a )):
+ def setPixelHDR(x, y, (r, g, b,a )):
"""
Set the the colors of the current pixel in the form [r,g,b,a].
For float image types, returned values can be greater then the useual [0.0, 1.0] range.
@@ -324,6 +338,21 @@
@rtype: none
"""
+ def setPixelF(x, y, (r, g, b,a )):
+ """
+ Set the the colors of the current pixel in the form [r,g,b,a].
+ Color values must be floats in the range 0.0 - 1.0.
+ Pixel coordinates are in the range from 0 to N-1. See L{getMaxXY}
+ @type x: int
+ @type y: int
+ @type r: float
+ @type g: float
+ @type b: float
+ @type a: float
+ @returns: nothing
+ @rtype: none
+ """
+
def setPixelI(x, y, (r, g, b, a)):
"""
Set the the colors of the current pixel in the form [r,g,b,a].
More information about the Bf-blender-cvs
mailing list