New methods for Image module:

.getPixel( x, y ) returns float list of [r, g, b, a] for pixel
.getMaxXY()  returns image size as float list [x, y]

Contributed by Austin Benesh (mchs3d).  Thanks!

also ran code thru indent.
This commit is contained in:
Stephen Swaney
2005-04-30 19:30:35 +00:00
parent ec52d4a0d3
commit 9aadcfc2c4
2 changed files with 124 additions and 16 deletions

View File

@@ -25,7 +25,8 @@
*
* This is a new part of Blender.
*
* Contributor(s): Willian P. Germano, Campbell Barton, Joilnen B. Leite
* Contributor(s): Willian P. Germano, Campbell Barton, Joilnen B. Leite,
* Austin Benesh
*
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
@@ -131,8 +132,7 @@ static PyObject *M_Image_Get( PyObject * self, PyObject * args )
while( ( img_iter ) && ( wanted_image == NULL ) ) {
if( strcmp( name, img_iter->id.name + 2 ) == 0 ) {
wanted_image =
( BPy_Image * )
wanted_image = ( BPy_Image * )
PyObject_NEW( BPy_Image, &Image_Type );
if( wanted_image )
wanted_image->image = img_iter;
@@ -211,6 +211,87 @@ static PyObject *M_Image_Load( PyObject * self, PyObject * args )
return ( PyObject * ) img;
}
/**
* getPixel( x, y )
* returns float list of pixel colors in rgba order
* blender images are all 4x8 bit at the moment apr-2005
*/
static PyObject *Image_getPixel( BPy_Image * self, PyObject * args )
{
PyObject *attr;
Image *image = self->image;
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 */
if( !PyArg_ParseTuple( args, "ii", &x, &y ) )
return EXPP_ReturnPyObjError( PyExc_TypeError,
"expected 2 integers" );
if( !image->ibuf || !image->ibuf->rect ) /* if no image data available */
load_image( image, IB_rect, "", 0 ); /* loading it */
if( !image->ibuf || !image->ibuf->rect ) /* didn't work */
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
"couldn't load image data in Blender" );
if( image->ibuf->type == 1 ) /* bitplane image */
return EXPP_ReturnPyObjError( PyExc_TypeError,
"unsupported bitplane image format" );
if( x > ( image->ibuf->x - 1 )
|| y > ( image->ibuf->y - 1 )
|| x < image->ibuf->xorig || y < image->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
*/
index = ( x + y * image->ibuf->x ) * pixel_size;
pixel = ( char * ) image->ibuf->rect;
attr = Py_BuildValue( "[f,f,f,f]",
( ( float ) pixel[index] ) / 255.0,
( ( float ) pixel[index + 1] ) / 255.0,
( ( float ) pixel[index + 2] ) / 255.0,
( ( float ) pixel[index + 3] / 255.0 ) );
if( attr ) /* normal return */
return attr;
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
"couldn't get pixel colors" );
}
static PyObject *Image_getMaxXY( BPy_Image * self )
{
Image *image = self->image;
PyObject *attr;
if( !image->ibuf || !image->ibuf->rect ) /* if no image data available */
load_image( image, IB_rect, "", 0 ); /* loading it */
if( !image->ibuf || !image->ibuf->rect ) /* didn't work */
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
"couldn't load image data in Blender" );
attr = Py_BuildValue( "[i,i]", image->ibuf->x, image->ibuf->y );
if( attr )
return attr;
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
"could not determine max x or y" );
}
/*****************************************************************************/
/* Function: Image_Init */
/*****************************************************************************/
@@ -245,15 +326,21 @@ static PyObject *Image_setName( BPy_Image * self, PyObject * args );
static PyObject *Image_setFilename( 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_reload( BPy_Image * self ); /* by Campbell */
static PyObject *Image_reload( BPy_Image * self );
static PyObject *Image_glLoad( BPy_Image * self );
static PyObject *Image_glFree( BPy_Image * self );
static PyObject *Image_getPixel( BPy_Image * self, PyObject * args );
static PyObject *Image_getMaxXY( BPy_Image * self );
/*****************************************************************************/
/* Python BPy_Image methods table: */
/*****************************************************************************/
static PyMethodDef BPy_Image_methods[] = {
/* name, method, flags, doc */
{"getPixel", ( PyCFunction ) Image_getPixel, METH_VARARGS,
"(float, float) - Get colors of specified pixel as [r,g,b,a]"},
{"getMaxXY", ( PyCFunction ) Image_getMaxXY, METH_VARARGS,
"() - Get maximum x & y coordinates of current image as [x, y]"},
{"getName", ( PyCFunction ) Image_getName, METH_NOARGS,
"() - Return Image object name"},
{"getFilename", ( PyCFunction ) Image_getFilename, METH_NOARGS,
@@ -300,7 +387,7 @@ static PyObject *Image_repr( BPy_Image * self );
/* Python Image_Type structure definition: */
/*****************************************************************************/
PyTypeObject Image_Type = {
PyObject_HEAD_INIT( NULL )
PyObject_HEAD_INIT( NULL ) /* required macro. ( no comma needed ) */
0, /* ob_size */
"Blender Image", /* tp_name */
sizeof( BPy_Image ), /* tp_basicsize */
@@ -480,7 +567,8 @@ static PyObject *Image_reload( BPy_Image * self )
free_image_buffers( img ); /* force read again */
img->ok = 1;
if (G.sima) image_changed( G.sima, 0 );
if( G.sima )
image_changed( G.sima, 0 );
Py_INCREF( Py_None );
return Py_None;
@@ -556,11 +644,12 @@ static PyObject *Image_setFilename( BPy_Image * self, PyObject * args )
return ( EXPP_ReturnPyObjError( PyExc_TypeError,
"expected a string argument" ) );
if( namelen >= 160)
if( namelen >= FILE_MAXDIR )
return ( EXPP_ReturnPyObjError( PyExc_TypeError,
"string argument is limited to 160 chars at most" ) );
PyOS_snprintf( self->image->name, FILE_MAXDIR * sizeof( char ), "%s", name );
PyOS_snprintf( self->image->name, FILE_MAXDIR * sizeof( char ), "%s",
name );
Py_INCREF( Py_None );
return Py_None;

View File

@@ -94,6 +94,25 @@ class Image:
@rtype: int
"""
def getPixel(x, y):
"""
Get the the colors of the current pixel in the form [r,g,b,a].
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 getMaxXY():
"""
Get the x & y size for the image. Image coordinates range from 0 to size-1.
@returns: [x, y]
@rtype: list of 2 ints
"""
def getXRep():
"""
Get the number of repetitions in the x (horizontal) axis for this Image.