Skip to content

Commit

Permalink
updated Python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
dusty-nv committed Jun 17, 2020
1 parent 16056b7 commit b987c40
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 75 deletions.
94 changes: 65 additions & 29 deletions python/bindings/PyCUDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,53 @@ PyCudaImage* PyCUDA_GetImage( PyObject* object )
return NULL;
}

// PyCUDA_GetImage
void* PyCUDA_GetImage( PyObject* capsule, int* width, int* height, imageFormat* format )
{
PyCudaImage* img = PyCUDA_GetImage(capsule);
void* ptr = NULL;

if( img != NULL )
{
ptr = img->base.ptr;
*width = img->width;
*height = img->height;
*format = img->format;
}
else
{
PyCudaMemory* mem = PyCUDA_GetMemory(capsule);

if( !mem )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "function wasn't passed a valid cudaImage or cudaMemory object");
return NULL;
}

ptr = mem->ptr;

if( *width <= 0 || *height <= 0 )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "image dimensions are invalid");
return NULL;
}

if( *format == IMAGE_UNKNOWN )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "invalid image format");
return NULL;
}
}

if( !ptr )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "image pointer was NULL (should be cudaImage or cudaMemory)");
return NULL;
}

return ptr;
}


//-------------------------------------------------------------------------------
// PyCUDA_Malloc
Expand Down Expand Up @@ -922,20 +969,27 @@ static PyObject* PyFont_OverlayText( PyFont_Object* self, PyObject* args, PyObje
PyObject* bg = NULL;

const char* text = NULL;
const char* format_str = "rgba32f";

int width = 0;
int height = 0;

int x = 0;
int y = 0;

static char* kwlist[] = {"image", "text", "x", "y", "color", "background", NULL};
static char* kwlist[] = {"image", "width", "height", "text", "x", "y", "color", "background", "format", NULL};

if( !PyArg_ParseTupleAndKeywords(args, kwds, "Os|iiOO", kwlist, &input, &text, &x, &y, &color, &bg))
if( !PyArg_ParseTupleAndKeywords(args, kwds, "O|iisiiOOs", kwlist, &input, &width, &height, &text, &x, &y, &color, &bg, &format_str))
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "cudaFont.OverlayText() failed to parse args (note that the width/height args have been removed)");
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "cudaFont.OverlayText() failed to parse function arguments");
return NULL;
}

//if( !output )
// output = input;
if( !text )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "cudaFont.OverlayText() was not passed in a text string");
return NULL;
}

// parse color tuple
float4 rgba = make_float4(0, 0, 0, 255);
Expand Down Expand Up @@ -973,35 +1027,17 @@ static PyObject* PyFont_OverlayText( PyFont_Object* self, PyObject* args, PyObje
}
}

// verify dimensions
/*if( width <= 0 || height <= 0 )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "cudaFont.OverlayText() image dimensions are invalid");
return NULL;
}*/
// parse format string
imageFormat format = imageFormatFromStr(format_str);

// get pointer to input image data
PyCudaImage* img = PyCUDA_GetImage(input);
// get pointer to image data
void* ptr = PyCUDA_GetImage(input, &width, &height, &format);

if( !img )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "cudaFont.OverlayText() failed to get input image pointer from first arg (should be cudaImage)");
if( !ptr )
return NULL;
}

// get pointer to output image data
/*void* output_img = PyCUDA_GetPointer(output);
if( !output_img )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "cudaFont.Overlay() failed to get output image pointer from PyCapsule container");
return NULL;
}*/

//LogDebug("cudaFont.Overlay(%p, %p, %i, %i, '%s', %i, %i, (%f, %f, %f, %f))\n", input_img, output_img, width, height, text, x, y, rgba.x, rgba.y, rgba.z, rgba.w);

// render the font overlay
self->font->OverlayText(img->base.ptr, img->format, img->width, img->height, text, x, y, rgba, bg_rgba);
self->font->OverlayText(ptr, format, width, height, text, x, y, rgba, bg_rgba);

// return void
Py_RETURN_NONE;
Expand Down
3 changes: 3 additions & 0 deletions python/bindings/PyCUDA.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ bool PyCUDA_IsImage( PyObject* object );
PyCudaMemory* PyCUDA_GetMemory( PyObject* object );
PyCudaImage* PyCUDA_GetImage( PyObject* object );

// retrieve from capsule
void* PyCUDA_GetImage( PyObject* object, int* width, int* height, imageFormat* format );

// Register functions
PyMethodDef* PyCUDA_RegisterFunctions();

Expand Down
2 changes: 1 addition & 1 deletion python/bindings/PyCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static PyObject* PyCamera_CaptureRGBA( PyCamera_Object* self, PyObject* args, Py
Py_DECREF(pyWidth);
Py_DECREF(pyHeight);

return capsule;
return tuple;
}


Expand Down
44 changes: 16 additions & 28 deletions python/bindings/PyGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,32 +159,26 @@ static PyObject* PyDisplay_Render( PyDisplay_Object* self, PyObject* args, PyObj
int height = 0;
int norm = 1;

static char* kwlist[] = {"image", "width", "height", "x", "y", "normalize", NULL};
const char* format_str = "rgba32f";
static char* kwlist[] = {"image", "width", "height", "x", "y", "normalize", "format", NULL};

if( !PyArg_ParseTupleAndKeywords(args, kwds, "O|iiffi", kwlist, &capsule, &width, &height, &x, &y, &norm))
if( !PyArg_ParseTupleAndKeywords(args, kwds, "O|iiffis", kwlist, &capsule, &width, &height, &x, &y, &norm, &format_str))
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "glDisplay.Render() failed to parse args tuple");
return NULL;
}

// verify dimensions
/*if( width <= 0 || height <= 0 )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "glDisplay.Render() image dimensions are invalid");
return NULL;
}*/
// parse format string
imageFormat format = imageFormatFromStr(format_str);

// get pointer to image data
PyCudaImage* img = PyCUDA_GetImage(capsule);
void* ptr = PyCUDA_GetImage(capsule, &width, &height, &format);

if( !img )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "glDisplay.Render() failed to get image pointer from first arg (should be cudaImage)");
if( !ptr )
return NULL;
}

// render the image
self->display->RenderImage(img->base.ptr, img->width, img->height, img->format, x, y, norm > 0 ? true : false);
self->display->RenderImage(ptr, width, height, format, x, y, norm > 0 ? true : false);

// return void
Py_RETURN_NONE;
Expand All @@ -210,32 +204,26 @@ static PyObject* PyDisplay_RenderOnce( PyDisplay_Object* self, PyObject* args, P
int height = 0;
int norm = 1;

static char* kwlist[] = {"image", "width", "height", "x", "y", "normalize", NULL};
const char* format_str = "rgba32f";
static char* kwlist[] = {"image", "width", "height", "x", "y", "normalize", "format", NULL};

if( !PyArg_ParseTupleAndKeywords(args, kwds, "O|iiffi", kwlist, &capsule, &width, &height, &x, &y, &norm))
if( !PyArg_ParseTupleAndKeywords(args, kwds, "O|iiffis", kwlist, &capsule, &width, &height, &x, &y, &norm))
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "glDisplay.RenderOnce() failed to parse args tuple");
return NULL;
}

// verify dimensions
/*if( width <= 0 || height <= 0 )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "glDisplay.RenderOnce() image dimensions are invalid");
return NULL;
}*/
// parse format string
imageFormat format = imageFormatFromStr(format_str);

// get pointer to image data
PyCudaImage* img = PyCUDA_GetImage(capsule);
void* ptr = PyCUDA_GetImage(capsule, &width, &height, &format);

if( !img )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "glDisplay.RenderOnce() failed to get image pointer from first arg (should be cudaImage)");
if( !ptr )
return NULL;
}

// render the image
self->display->RenderOnce(img->base.ptr, img->width, img->height, img->format, x, y, norm > 0 ? true : false);
self->display->RenderOnce(ptr, width, height, format, x, y, norm > 0 ? true : false);

// return void
Py_RETURN_NONE;
Expand Down
17 changes: 0 additions & 17 deletions python/bindings/PyImageIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,23 +178,6 @@ PyObject* PyImageIO_SaveRGBA( PyObject* self, PyObject* args, PyObject* kwds )
return NULL;
}

// verify dimensions
#if 0
if( width <= 0 || height <= 0 )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "saveImageRGBA() image dimensions are invalid");
return NULL;
}

void* img = PyCapsule_GetPointer(capsule, CUDA_MAPPED_MEMORY_CAPSULE); // TODO support GPU-only memory

if( !img )
{
PyErr_SetString(PyExc_Exception, LOG_PY_UTILS "saveImageRGBA() failed to get input image pointer from PyCapsule container");
return NULL;
}
#endif

// get pointer to image data
PyCudaImage* img = PyCUDA_GetImage(capsule);

Expand Down

0 comments on commit b987c40

Please sign in to comment.