[C++-sig] help passing image data

J.D. Yamokoski yamokosk at ufl.edu
Mon May 22 16:45:58 CEST 2006


I am writing a simple test application to teach myself the ins and outs 
of embedding Python in a C++ program. But I am currrently stuck trying 
to pass non-standard data across the C/Python interface. Plus I may be 
complicating matters by trying to do all this embedding in MFC, but 
thats what I know for GUI programming. Here are the details:

Using boost.python, I have created a module of the CImg 
(http://cimg.sourceforge.net/) library - a very light module as it only 
supports a handful of functions. I know python extensions for image 
manipulation already exist but this was to practice with boost - plus my 
main app already uses CImg.

My test app is a SDI MFC program. In the document, I have one member 
variable - a CImg object. Image data stored in this object is displayed 
in the main window of the app.

Now for the python. The app has a popup window where I can type in text 
which is then passed to the interpreter. For instance (please excuse any 
blatant python errors - treat this as pseudo python code):

# Import my CImg extension and the set/get function from the main app
import CImg
import MyAppInterface

# Get the image from the app. Function should return a CImg object
DocImg = MyAppInterface.GetCurrentImage()

# Call some CImg function
DocImg.blur()

# Return the data to the app for displaying
MyAppInterface.SetCurrentImage( DocImg )
					

To create the application interface functions, I have the following code 
in C:

static PyObject*
get_current_image(PyObject *self, PyObject *args)
{
     if( !PyArg_ParseTuple(args, "") ) return NULL;

     CImg<float> img = gDocument->getCurrentImage();

     return Py_BuildValue("o", &img);
};

static PyObject*
set_current_image(PyObject *self, PyObject *args)
{
	PyObject *obj = NULL;
	if( !PyArg_ParseTuple(args, "o", &obj) ) return NULL;

	CImg<float> img( (CImg<float>)*obj );
	Py_DECREF(obj); obj = NULL;

	gDocument->setCurrentImage( img );

	Py_INCREF(Py_None);
     return Py_None;
};

static PyMethodDef ImageMethods[] = {
     {"GetCurrentImage", get_current_image, METH_VARARGS,
      "Get the current image in the workspace."},
	{"SetCurrentImage", set_current_image, METH_VARARGS,
	 "Set the current image in the workspace."},
     {NULL, NULL, 0, NULL}
};

I have not tried to compile the above code because I am sure its wrong 
on many levels. But I wanted to see if I was heading in the right 
direction with this. Or is there a much more efficient way of doing 
this? (gDocument is a global pointer to the app's document - I know, bad 
practice. I would like to avoid this, but I don't know how else to give 
C functions access to data in an MFC class).

Thanks for reading all this if you have made it this far!

J.D. Yamokoski



More information about the Cplusplus-sig mailing list