URBIimage is a simple program written in C++ with the liburbi-C++ to get and display images from an URBI server. URBIimage does two things: it sets a callback on a tag named uimg and then receives the images in this callback and send them to a display object Monitor. Let's have a look at the general code and the main function. First, the callback interface:
Monitor *mon;
/* Our callback function */
UCallbackAction showImage(const UMessage &msg)
{
...
}
Then, the main function:
int main(int argc, char *argv[])
{
mon = NULL;
client = new UClient(argv[2]);
if (client->error() != 0)
exit(0);
client->setCallback(showImage, "uimg");
// Some image initialization
client->send("camera.resolution = 0;");
client->send("camera.jpegfactor = 80;");
// Start the loop
client->send("loop uimg: camera,");
urbi::execute();
}
The code to handle the image is stored in showImage:
UCallbackAction showImage(const UMessage &msg)
{
if (msg.type != MESSAGE_DATA || ((UImage)msg).imageFormat == IMAGE_UNKNOWN)
return URBI_CONTINUE;
UImage img = (UImage)msg;
unsigned char buffer[500000];
int sz = 500000;
static int tme = 0;
if (!mon)
mon = new Monitor(msg.image.width, msg.image.height);
convertJPEGtoRGB((const byte *) img.data,
img.size, (byte *) buffer, sz);
mon->setImage((bits8 *) buffer, sz);
return URBI_CONTINUE;
}
It first tests for the msg type, and returns without doing anything if this is not the type expected (for example, if the callback is waken up by an error message).
Then, the conversion function convertJPEGtoRGB is used to transform the image buffer in something readable for the Monitor object, which then receives the image.
Finally, URBI_CONTINUE is returned to carry on receiving future callbacks.
This little program illustrates very well how a liburbi-based URBI program is built: set callbacks, send URBI scripts, receive callbacks in specified functions. You might have a look at the GPL source code of URBILab which is built with liburbi-C++ and shows a more advanced use of this methodology.