View Full Version : Callback function to process buffers

December 19, 2001, 22:18:32
I am interested in capturing each frame and modifying it before I display it.

I believe the best way to do this is using the setCallback() fuction and then process the buffer it receives and display the result.

My questions are:
1) Is this the best way to do this ?

2) If so, could you give me a short example of how to write a callback function. I am having a difficult time writing something that the compiler accepts. I am using MicroSoft Visual C++.


This message was originally posted by Ed Cohen in the old IC Imaging Control Support Forum.

December 21, 2001, 19:17:56
Hello Ed

Using a callback is the best way to what you want to do. Here is a very simple example which shows how to use a callback.

IC Support

************************************************** ****

// This example demonstrates the usage of a callback function.
// The installed callback function saves one frame to disk and counts how many
// frames were saved so far.
// After snapImages() returns, the main function will save the remaining buffers.

#include "stdafx.h"
#include "tisudshl.h"

using namespace _DSHOWLIB_NAMESPACE;

// Prototype for the callback function
void callBackFunc(void *data, DShowLib::Grabber::tMemBufferPtr buffer, unsigned long ctr);

// Structure for storing the user data of the callback function
struct userDataStruct{
int count;

int main(int argc, char* argv[])
DShowLib::InitLibary( 0 );

DShowLib::Grabber *grabber=new DShowLib::Grabber();
struct userDataStruct userdata;

// Open the grabber with some video device
DShowLib::Grabber::tVidCapDevListPtr pVidCapDevList = grabber->getAvailableVideoCaptureDevices();
if( pVidCapDevList == 0 || pVidCapDevList->empty() )
printf( "No video capture device available!\n" );
exit( 1 ); // no device available
grabber->openDev( (*pVidCapDevList)[2] );

// Create the tsFrameEndCallback structure
DShowLib::tsFrameEndCallback& callback = DShowLib::tsFrameEndCallback( callBackFunc, &userdata );
// Set the callback function
grabber->setCallback( callback );

// Set a MemBufferCollection
DShowLib::Grabber::tMemBufferCollectionPtr pMemBuffColl = grabber->newMemBufferCollection( 10 );
grabber->setActiveMemBufferCollection( pMemBuffColl );

grabber->startLive( false ); // start the grabber in no show mode
grabber->snapImages( 10 ); // grab 10 images
grabber->stopLive(); // stop the grabber

while( userdata.count < 10 )
char filename[255];
DShowLib::Grabber::tMemBufferPtr pMemBuff = pMemBuffColl->getBuffer( userdata.count );
sprintf( &filename[0], "image%i.bmp", userdata.count++ );
pMemBuff->save( filename );


return 0;

This is the callback function which will be installed with setCallback.
void callBackFunc(void *data, DShowLib::Grabber::tMemBufferPtr buffer, unsigned long ctr)
struct userDataStruct *userdata=(struct userDataStruct *)data;
char filename[255];
DShowLib::MemBufferCollection MemBuffColl = buffer->getCollection();
DShowLib::Grabber::tMemBufferPtr pMemBuff = MemBuffColl.getBuffer( userdata->count );
sprintf( &filename[0], "image%i.bmp", userdata->count++ );
pMemBuff->save( filename );
return ;

This message was originally posted by IC Support in the old IC Imaging Control Support Forum.

January 17, 2002, 03:37:19
The above example seems to only be able to process 10 images.
How would this be modified to continuously capture?

Our image-processing application would benefit greatly from a library that provided a general interface to cameras and framegrabbers, however I can't figure out how IC Imaging Control library could be used.

To explain:

Our system employs a standard C++ framework of an image-processing pipeline.

Essentially, each node in the pipeline is passed an image buffer, and then passes it to the next node.

The last node "releases" the image buffer back into a pool of image buffers that are available for image capture.

The pipeline is supplied with image buffers by a worker thread, which gets the next available buffer from the buffer-pool and schedules it for acquisition using a call to a framegrabber library, and then hands it to the first node.

ie: Processing nodes in the pipeline (out of the buffer-pool) are guaranteed not to have their image data altered by the framegrabber.

Does the library provide the functionality to control which image buffer is written next, and if so, would it be efficient?
The callback function has access to the entire collection of buffers, but could it things like flag a buffer for read-only access, or remove a buffer from the collection, and replace it with a different buffer etc?

Any suggestions greatly appreciated...

This message was originally posted by Tim Edwards in the old IC Imaging Control Support Forum.

June 2, 2002, 19:42:52
I tried the callback function, and unfortunatelly it seems true that only snapImages() will cause callback. And when stopLive(), it's also called once. I tried to call snapImages() in callback, and cause crash. Then I use snapImages(10000) for a long time, and it can work, although it will not return until all the images are snapped. So, in the OCX version, I wonder whether the event is using both snapImages and a timer. And it will cause frame lost every several seconds. Any suggestions? Thanks.

This message was originally posted by Grant in the old IC Imaging Control Support Forum.