getHeliImage.C

00001 #include "Util/log.H"
00002 #include "Util/WorkThreadServer.H"
00003 #include "Util/JobWithSemaphore.H"
00004 #include "Component/ModelManager.H"
00005 #include "Raster/GenericFrame.H"
00006 #include "Image/Layout.H"
00007 #include "Image/MatrixOps.H"
00008 #include "Image/DrawOps.H"
00009 #include "GUI/DebugWin.H"
00010 #include "Media/FrameSeries.H"
00011 #include "Transport/FrameInfo.H"
00012 #include "Util/CpuTimer.H"
00013 #include "Image/JPEGUtil.H"
00014 
00015 #include <stdlib.h>
00016 #include <math.h>
00017 
00018 #include <stdlib.h>
00019 #include <string.h>
00020 #include <unistd.h>
00021 #include <sys/types.h>
00022 #include <sys/socket.h>
00023 #include <netinet/in.h>
00024 #include <netdb.h> 
00025 #include <fcntl.h>
00026 
00027 bool useTcp = true;
00028 
00029 int main(int argc, char *argv[])
00030 {
00031 
00032   ModelManager manager("Test view iMgae");
00033 
00034   nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00035   manager.addSubComponent(ofs);
00036 
00037   // Parse command-line:
00038   if (manager.parseCommandLine(argc, argv, "<server> <port>", 2, 2) == false) return(1);
00039   // let's get all our ModelComponent instances started:
00040   manager.start();
00041 
00042   const char* hostname = manager.getExtraArg(0).c_str();
00043   int portno = atoi(manager.getExtraArg(1).c_str());
00044 
00045   int sockfd = -1;
00046   if (useTcp)
00047     sockfd = socket(AF_INET, SOCK_STREAM, 0);
00048   else
00049     sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
00050 
00051   if (sockfd < 0) 
00052     LFATAL("ERROR opening socket");
00053 
00054   struct hostent* server = gethostbyname(hostname);
00055   if (server == NULL)
00056     LFATAL("ERROR, no such host as %s\n", hostname);
00057 
00058   /* build the server's Internet address */
00059   struct sockaddr_in serveraddr;
00060   bzero((char *) &serveraddr, sizeof(serveraddr));
00061   serveraddr.sin_family = AF_INET;
00062   bcopy((char *)server->h_addr, 
00063       (char *)&serveraddr.sin_addr.s_addr, server->h_length);
00064   serveraddr.sin_port = htons(portno);
00065 
00066   if (useTcp)
00067   {
00068     /* connect: create a connection with the server */
00069     if (connect(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0) 
00070       LFATAL("ERROR connecting");
00071   }
00072 
00073   CpuTimer timer;
00074 
00075   /* send the message line to the server */
00076   const char *startFrame = "S\n";
00077   int n = -1;
00078   if (useTcp)
00079     n = write(sockfd, startFrame, strlen(startFrame));
00080   else
00081     n = sendto(sockfd, startFrame, sizeof(startFrame), 0, (const struct sockaddr *)&serveraddr,sizeof(serveraddr));
00082 
00083   if (n < 0) 
00084     LFATAL("ERROR writing to socket");
00085   
00086   while(1)
00087   {
00088     timer.reset();
00089     for(int t=0; t<30; t++)
00090     {
00091       int n=-1;
00092 
00093       /* print the server's reply */
00094       unsigned int frameSize = 0; 
00095       socklen_t sin_size = sizeof(serveraddr);
00096       if (useTcp)
00097         n = recv(sockfd, &frameSize, sizeof(frameSize), MSG_WAITALL);
00098       else
00099         n = recvfrom(sockfd, &frameSize, sizeof(frameSize), MSG_WAITALL, (struct sockaddr *)&serveraddr,&sin_size);
00100 
00101       char buf[frameSize];
00102       if (useTcp)
00103         n = recv(sockfd, buf, frameSize, MSG_WAITALL);
00104       else
00105         n = recvfrom(sockfd, buf, frameSize, MSG_WAITALL, (struct sockaddr *)&serveraddr,&sin_size);
00106       LINFO("Size %i", n);
00107 
00108 
00109       if (n < 0) 
00110         LFATAL("ERROR reading from socket");
00111 
00112       if (n > 5000)
00113       {
00114         JPEGDecompressor jpdec;
00115         std::vector<unsigned char> data(n);
00116         for(int ii=0; ii<n; ii++)
00117           data[ii] = buf[ii];
00118 
00119         Image<PixRGB<byte> > img(320,240,ZEROS); // = jpdec.DecompressImage(data); //(320,240,ZEROS);
00120         img.attach((const_cast<PixRGB<byte>*> (reinterpret_cast<const PixRGB<byte>* >
00121                 (&buf))), 320, 240);
00122         Image<PixRGB<byte> > tmp = img.deepcopy(); //deepcopy?
00123         ofs->writeRGB(tmp, "gpsOutput", FrameInfo("gpsOutput", SRC_POS));
00124         usleep(10000);
00125       }
00126     }
00127     timer.mark();
00128     LINFO("Total time %0.2f sec", 30/timer.real_secs());
00129     
00130 
00131   }
00132 
00133   close(sockfd);
00134 
00135   manager.stop();
00136 
00137 }
00138 
00139 
Generated on Sun May 8 08:05:39 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3