Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

LGmixin.cc

Go to the documentation of this file.
00001 #include "DualCoding/Sketch.h"
00002 #include "Shared/ProjectInterface.h"
00003 #include "Shared/ImageUtil.h"
00004 #include "Vision/JPEGGenerator.h"
00005 
00006 #include "LGmixin.h"
00007 
00008 #include <iostream>
00009 #include <fstream>
00010 #include <sys/stat.h>
00011 #include <sys/fcntl.h>
00012 #include <unistd.h>
00013 
00014 using namespace std;
00015 using namespace DualCoding;
00016 
00017 unsigned int LGmixin::instanceCount = 0;
00018 Socket* LGmixin::LGsock = NULL;
00019 LGmixin* LGmixin::theOne = NULL;
00020 
00021 LGmixin::LGmixin() {
00022   if ( instanceCount++ > 0 )
00023     return;
00024   if (theOne != NULL) {
00025       cerr << "LGmixin statics already constructed!?!?!" << endl;
00026       return;
00027     }
00028     theOne=this;
00029 
00030     LGsock = wireless->socket(Socket::SOCK_STREAM, 1024, LGbufferSize);
00031     wireless->setDaemon(LGsock, false);
00032     wireless->listen(LGsock, LGport);
00033 }
00034 
00035 LGmixin::~LGmixin() {
00036   if ( --instanceCount > 0 )
00037     return;
00038   if (theOne == NULL) {
00039     cerr << "LGmixin statics already destructed!?!?!" << endl;
00040     return;
00041   }
00042   wireless->close(LGsock->sock);
00043   theOne = NULL;
00044 }
00045 
00046 void LGmixin::uploadFile(const std::string &filename, bool display, bool isImage) {
00047   if ( !wireless->isConnected(LGsock->sock) ) {
00048     cerr << "LookingGlass not connected." << endl;
00049     return;
00050   }
00051 
00052   int in_file = open(filename.c_str(), O_RDONLY);
00053   if ( in_file < 0 ) {
00054     cerr << "Error: Unable to open file\n";
00055     return;
00056   }
00057   struct stat s;
00058   stat(filename.c_str(),&s);
00059   const std::string remoteFilename = filename.substr(filename.rfind('/')+1);
00060   LGsock->printf("UPLOAD_BINARY %s %u\n", remoteFilename.c_str(), (unsigned int)s.st_size);
00061   
00062   size_t remain = static_cast<size_t>(s.st_size);
00063   while(remain>0){
00064     char *buffer = (char*)LGsock->getWriteBuffer(remain);
00065     if ( buffer==NULL ) {
00066       cerr << "NULL buffer in LG uploadFile: file too big?" << endl;
00067       break;
00068     }
00069     int read_size = read(in_file, buffer, remain);
00070     if ( read_size < 0 ){
00071       cerr << "Error: Read error " << read_size << endl;
00072       read_size=0; // fall through
00073     }
00074     LGsock->write(read_size); // *must* always balance getWriteBuffer() with write()
00075     remain = (read_size==0) ? 0 : remain-read_size;
00076   }
00077   
00078   if(display) {
00079     if ( isImage )
00080       displayImageFile(remoteFilename);
00081     else
00082       displayHtmlFile(remoteFilename);
00083   }
00084 
00085   close(in_file);
00086 }
00087 
00088 void LGmixin::displayHtmlFile(const std::string &remoteFilename) {
00089   LGsock->printf("DISPLAY LookingGlass_Dummy_File\n"); // work-around because renderer can't display same file twice
00090   LGsock->printf("DISPLAY %s\n",remoteFilename.c_str());
00091 }
00092 
00093 void LGmixin::displayImageFile(const std::string &remoteFilename) {
00094   LGsock->printf((string("UPLOAD_HTML %s.html\n") +
00095      string("<html><body><table width=100%% height=100%%><tr><td align=center valign=middle>") +
00096       string("<img src=\"%s\"></td></tr></table></body>\n</html>\n")).c_str(),
00097      remoteFilename.c_str(), remoteFilename.c_str());
00098   displayHtmlFile(remoteFilename+".html");
00099 }
00100 
00101 void LGmixin::displayHtmlText(const std::string &text) {
00102   unsigned int const msgSize = text.size();
00103   string const tempfilename = "temp9999";
00104   LGsock->printf("UPLOAD_BINARY %s %u\n", tempfilename.c_str(), msgSize);
00105   char *buffer = (char*)LGsock->getWriteBuffer(msgSize);
00106   if ( buffer==NULL ) {
00107     cerr << "NULL buffer in LG displayHtmlText: file too big?" << endl;
00108     return;
00109   }
00110   memcpy(buffer,text.c_str(),msgSize);
00111   LGsock->write(msgSize);
00112   displayHtmlFile(tempfilename);
00113 }
00114 
00115 void LGmixin::sendCommand(const std::string &command) {
00116   LGsock->printf("%s\n", command.c_str());
00117 }
00118 
00119 void LGmixin::uploadCameraImage(const std::string &remoteFilename) {
00120     JPEGGenerator *jpeg = ProjectInterface::defColorJPEGGenerator;  
00121     // call to getImage must come before call to getImageSize
00122     char *image = (char*)jpeg->getImage(ProjectInterface::fullLayer, 0);
00123     int const imgSize = jpeg->getImageSize(ProjectInterface::fullLayer, 0);
00124     LGsock->printf("UPLOAD_BINARY %s %u\n", remoteFilename.c_str(), imgSize);
00125     char *buffer = (char*)LGsock->getWriteBuffer(imgSize);
00126     if ( buffer==NULL ) {
00127       cerr << "NULL buffer in LG uploadCameraImage: file too big?" << endl;
00128       return;
00129     }
00130     memcpy(buffer,image,imgSize);
00131     LGsock->write(imgSize);
00132 }
00133 
00134 void LGmixin::uploadSketch(const DualCoding::Sketch<DualCoding::uchar> &sketch,
00135          const std::string &remoteFilename) {
00136   // get the color table into a format suitable for fast indexing
00137   int const numColors = ProjectInterface::getNumColors();
00138   uchar Rbyte[numColors], Gbyte[numColors], Bbyte[numColors];
00139   for ( int i=0; i<numColors; i++ ) {
00140     rgb const c = ProjectInterface::getColorRGB(i);
00141     Rbyte[i] = c.red;
00142     Gbyte[i] = c.green;
00143     Bbyte[i] = c.blue;
00144   }
00145   // translate indexed color image into RGB image
00146   size_t const numPixels = sketch->getNumPixels();
00147   size_t const pixelsize = 3;
00148   size_t const inbuffsize = numPixels * pixelsize;
00149   char* inbuff = new char[inbuffsize];
00150   char* inbuffptr = inbuff;
00151   for ( size_t i=0; i<numPixels; i++ ) {
00152     uchar const p = sketch[i];
00153     *inbuffptr++ = Rbyte[p];
00154     *inbuffptr++ = Gbyte[p];
00155     *inbuffptr++ = Bbyte[p];
00156   }
00157   char *outbuff = NULL;
00158   size_t outbuffsize = 0;
00159   size_t const imgSize =
00160     image_util::encodeJPEG(inbuff, inbuffsize, sketch->getWidth(), sketch->getHeight(), pixelsize, 
00161          outbuff, outbuffsize, pixelsize, 70);
00162   LGsock->printf("UPLOAD_BINARY %s %lu\n", remoteFilename.c_str(), (unsigned long)imgSize);
00163   char *lgbuff = (char*)LGsock->getWriteBuffer(imgSize);
00164   if ( lgbuff==NULL ) {
00165     cerr << "NULL buffer in LG uploadSketch" << endl;
00166     return;
00167   }
00168   memcpy(lgbuff,outbuff,imgSize);
00169   LGsock->write(imgSize);
00170   delete [] outbuff;
00171   cout << "LG:  outbuffsize = " << outbuffsize << ",  imgSize = " << imgSize << endl;
00172   delete[] inbuff;
00173 }

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:43 2016 by Doxygen 1.6.3