urbiimage.cc

00001 /****************************************************************************
00002  * $Id: urbiimage.cpp,v 1.13 2005/09/30 17:48:00 nottale Exp $
00003  *
00004  * Sample image acqusition urbi client.
00005  *
00006  * Copyright (C) 2004, 2006, 2007 Jean-Christophe Baillie.  All rights reserved.
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 **********************************************************************/
00022 
00023 /*
00024  * This simple demonstration program display or save images from an Urbi server.
00025  */
00026 
00027 #include "libport/cstdio"
00028 #include <csignal>
00029 
00030 #include "urbi/usyncclient.hh"
00031 #include "monitor.h"
00032 
00033 int imcount;
00034 int format;
00035 Monitor *mon=0;
00036 unsigned char * buffer=NULL;
00037 
00038 
00039 /* Our callback function */
00040 urbi::UCallbackAction
00041 showImage(const urbi::UMessage &msg)
00042 {
00043   if (msg.type != urbi::MESSAGE_DATA
00044       || msg.value->type != urbi::DATA_BINARY
00045       || msg.value->binary->type != urbi::BINARY_IMAGE)
00046     return urbi::URBI_CONTINUE;
00047 
00048   urbi::UImage& img = msg.value->binary->image;
00049 
00050   static unsigned char* buffer = static_cast<unsigned char*> (malloc (3*400*400));
00051   int sz = 500000;
00052   static int tme = 0;
00053   /* Calculate framerate. */
00054   if (!(imcount % 20))
00055     {
00056       if (tme)
00057         {
00058           float it = msg.client.getCurrentTime() - tme;
00059           it = 20000.0 / it;
00060           printf("***Framerate: %f fps***\n", it);
00061         }
00062       tme = msg.client.getCurrentTime();
00063     }
00064 
00065   if (!mon)
00066     mon = new Monitor(img.width, img.height, "image");
00067 
00068   switch (img.imageFormat)
00069     {
00070     case urbi::IMAGE_JPEG:
00071       urbi::convertJPEGtoRGB((const urbi::byte *) img.data, img.size,
00072                              (urbi::byte *) buffer, sz);
00073       break;
00074     case urbi::IMAGE_YCbCr:
00075       sz = img.size;
00076       urbi::convertYCrCbtoRGB((const urbi::byte *) img.data, img.size,
00077                               (urbi::byte *) buffer);
00078       break;
00079     case urbi::IMAGE_RGB:
00080     case urbi::IMAGE_PPM:
00081     case urbi::IMAGE_UNKNOWN:
00082       break;
00083     }
00084 
00085   mon->setImage((bits8 *) buffer, sz);
00086   ++imcount;
00087   return urbi::URBI_CONTINUE;
00088 }
00089 
00090 void
00091 closeandquit (int)
00092 {
00093   delete urbi::getDefaultClient();
00094   urbi::exit(0);
00095 }
00096 
00097 
00098 int
00099 main (int argc, char *argv[])
00100 {
00101   signal(SIGINT, closeandquit);
00102   mon = NULL;
00103 
00104   if (argc != 3)
00105     {
00106       fprintf(stderr,
00107               "Missing argument\n"
00108               "usage: urbiimage format robotname [reconstruct]: display images\n"
00109               "\tFormat : jpeg=transfer jpeg, raw=transfer raw\n"
00110               "usage: urbiimage -tofile format filename robotname [reconstruct]: save 1 image.\n"
00111               "\t Format: rgb: RGB ycrcb:YCrCb jpeg: JPEG ppm:PPM\n");
00112       urbi::exit(1);
00113     }
00114 
00115   int mode = 0;
00116   if (argc >=5)
00117     mode = 1;
00118 
00119   urbi::USyncClient client (argv[2+mode*2]);
00120   if (client.error())
00121     urbi::exit(1);
00122 
00123   client.setCallback(showImage, "uimg");
00124 
00125   client.send("camera.resolution  = 0;");
00126   client.send("camera.jpegfactor = 70;");
00127 
00128   client << "camera.reconstruct = " << ((argc>3+mode*2)? 1:0)
00129          << urbi::semicolon;
00130 
00131   if (mode == 0)
00132     {
00133       imcount = 0;
00134       format = (argv[1][0]=='r')?0:1;
00135       client.send("camera.format = %d;", format);
00136       client.send("loop {uimg: camera.val; noop},");
00137       urbi::execute();
00138     }
00139   else
00140     {
00141       /* Use syncGetImage to save one image to a file. */
00142       char buff[1000000];
00143       int sz = 1000000;
00144       int w, h;
00145       switch (argv[2][0])
00146         {
00147         case 'r':
00148           format = urbi::IMAGE_RGB;
00149           break;
00150         case 'y':
00151           format = urbi::IMAGE_YCbCr;
00152           break;
00153         case 'p':
00154           format = urbi::IMAGE_PPM;
00155           break;
00156         case 'j':
00157           format = urbi::IMAGE_JPEG;
00158           break;
00159         };
00160 
00161       client.syncGetImage("camera", buff, sz,
00162                           format,
00163                           (format == urbi::IMAGE_JPEG
00164                            ? urbi::URBI_TRANSMIT_JPEG
00165                            : urbi::URBI_TRANSMIT_YCbCr),
00166                           w, h);
00167 
00168       FILE *f = fopen(argv[3], "w");
00169 
00170       if (!f)
00171         urbi::exit(2);
00172 
00173       fwrite(buff, 1, sz, f);
00174       fclose(f);
00175     }
00176 
00177   return 0;
00178 }

Generated on Tue Apr 10 17:45:45 2007 for URBISDK by  doxygen 1.5.1