Homepage Demos Overview Downloads Tutorials Reference
Credits

SegmentedColorGenerator.cc

Go to the documentation of this file.
00001 #include "SegmentedColorGenerator.h"
00002 #include "Events/EventRouter.h"
00003 #include "Events/SegmentedColorFilterBankEvent.h"
00004 #include "Wireless/Wireless.h"
00005 #include "Shared/WorldState.h"
00006 
00007 #include "Shared/debuget.h"
00008 
00009 SegmentedColorGenerator::SegmentedColorGenerator(EventBase::EventGeneratorID_t gid, unsigned int sid, unsigned int mysid)
00010   : FilterBankGenerator("SegmentedColorGenerator",gid,sid,EventBase::visSegmentEGID,mysid), srcYChan(0), srcUChan(1), srcVChan(2), tmaps(), tmapNames(), numColors(0), colorNames()
00011 { }
00012 
00013 SegmentedColorGenerator::SegmentedColorGenerator(EventBase::EventGeneratorID_t gid, unsigned int sid, unsigned int mysid, unsigned int syc, unsigned int suc, unsigned int svc)
00014   : FilterBankGenerator("SegmentedColorGenerator",gid,sid,EventBase::visSegmentEGID,mysid), srcYChan(syc), srcUChan(suc), srcVChan(svc), tmaps(), tmapNames(), numColors(0), colorNames()
00015 { }
00016 
00017 SegmentedColorGenerator::~SegmentedColorGenerator() {
00018   freeCaches();
00019   destruct();
00020   for(unsigned int i=0; i<tmaps.size(); i++)
00021     delete [] tmaps[i];
00022 
00023   //hashmap::iterator it=colorNames.begin(); //not the way i'd like to iterate, but there seems to be a bug in the current hashmap implementation we're using...
00024   //for(unsigned int i=0; i<colorNames.size(); it++,i++)
00025   //free(const_cast<char*>(it->first));
00026   //colorNames.clear();
00027 
00028   colorNames.clear(); //we're leaking the memory of the names themselves...
00029 }
00030 
00031 void
00032 SegmentedColorGenerator::processEvent(const EventBase& event) {
00033   FilterBankGenerator::processEvent(event);
00034   erouter->postEvent(new SegmentedColorFilterBankEvent(this,getGeneratorID(),getSourceID(),this,getNumColors(),getColors(),&colorNames));
00035 }
00036 
00037 unsigned int
00038 SegmentedColorGenerator::loadThresholdMap(const std::string& tm_file) {
00039   const unsigned int size = 1 << (BITS_Y + BITS_U + BITS_V);
00040   cmap_t * tmap = new cmap_t[size];
00041   if(!CMVision::LoadThresholdFile(tmap,NUM_Y,NUM_U,NUM_V,tm_file.c_str())) {
00042     serr->printf("  ERROR: Could not load threshold file '%s'.\n",tm_file.c_str());
00043     delete [] tmap;
00044     return -1U;
00045   }
00046   if(numColors!=0) {
00047     //we've already loaded color info, check against that for invalid indexes
00048     int remapped=CMVision::CheckTMapColors(tmap,NUM_Y,NUM_U,NUM_V,numColors,0);
00049     if(remapped>0)
00050       serr->printf("remapped %d colors in threshold file '%s'\n",remapped, tm_file.c_str());
00051   }
00052 
00053   tmapNames.push_back(tm_file);
00054   tmaps.push_back(tmap);
00055   setNumImages(numLayers,tmaps.size());
00056   return tmaps.size()-1;
00057 }
00058 
00059 bool
00060 SegmentedColorGenerator::loadColorInfo(const std::string& col_file) {
00061   //hashmap::iterator it=colorNames.begin(); //not the way i'd like to iterate, but there seems to be a bug in the current hashmap implementation we're using...
00062   //for(unsigned int i=0; i<colorNames.size(); it++,i++)
00063   //free(const_cast<char*>(it->first));
00064   //colorNames.clear();
00065 
00066   colorNames.clear(); //we're leaking the memory of the names themselves...
00067 
00068   numColors=CMVision::LoadColorInformation(colors,MAX_COLORS,col_file.c_str(),colorNames);
00069   if(numColors > 0){
00070     sout->printf("  Loaded %d colors.\n",numColors);
00071   } else {
00072     serr->printf("  ERROR: Could not load colors file:%s\n", col_file.c_str());
00073     return false;
00074   }
00075 
00076   //we'd better check the already loaded thresholds (if any) for out of bound indexes
00077   for(unsigned int i=0; i<tmaps.size(); i++) {
00078     int remapped=CMVision::CheckTMapColors(tmaps[i],NUM_Y,NUM_U,NUM_V,numColors,0);
00079     if(remapped>0)
00080       serr->printf("remapped %d colors in threshold file '%s'\n",remapped, tmapNames[i].c_str());
00081   }
00082   return true;
00083 }
00084 
00085 
00086 unsigned int
00087 SegmentedColorGenerator::getBinSize() const {
00088   unsigned int used=FilterBankGenerator::getBinSize();
00089   used+=strlen("SegColorImage")+LoadSave::stringpad;
00090   used+=widths[selectedSaveLayer]*heights[selectedSaveLayer];
00091   return used;
00092 }
00093 
00094 unsigned int
00095 SegmentedColorGenerator::LoadBuffer(const char buf[], unsigned int len) {
00096   unsigned int origlen=len;
00097   unsigned int used;
00098   std::string tmp;
00099   if(0==(used=FilterBankGenerator::LoadBuffer(buf,len))) return 0;
00100   len-=used; buf+=used;
00101   if(0==(used=decode(tmp,buf,len))) return 0;
00102   len-=used; buf+=used;
00103   if(tmp!="SegColorImage") {
00104     serr->printf("Unhandled image type for SegmentedColorGenerator: %s",tmp.c_str());
00105     return 0;
00106   } else {
00107     // actual image
00108     used=widths[selectedSaveLayer]*heights[selectedSaveLayer];
00109     if(used>len)
00110       return 0;
00111     if(images[selectedSaveLayer][selectedSaveChannel]==NULL)
00112       images[selectedSaveLayer][selectedSaveChannel]=createImageCache(selectedSaveLayer,selectedSaveChannel);
00113     unsigned char* img=images[selectedSaveLayer][selectedSaveChannel];
00114     if(img==NULL)
00115       return 0;
00116     memcpy(img,buf,used);
00117     len-=used; buf+=used;
00118 
00119     // color table
00120     if(0==(used=decodeColors(buf,len))) return 0;
00121     len-=used; buf+=used;
00122     
00123     imageValids[selectedSaveLayer][selectedSaveChannel]=true;
00124     return origlen-len; 
00125   }
00126 }
00127 
00128 unsigned int
00129 SegmentedColorGenerator::SaveBuffer(char buf[], unsigned int len) const {
00130   unsigned int origlen=len;
00131   unsigned int used;
00132   if(0==(used=FilterBankGenerator::SaveBuffer(buf,len))) return 0;
00133   len-=used; buf+=used;
00134   if(0==(used=encode("SegColorImage",buf,len))) return 0;
00135   len-=used; buf+=used;
00136   
00137   // actual image
00138   used=widths[selectedSaveLayer]*heights[selectedSaveLayer];
00139   if(used>len)
00140     return 0;
00141   unsigned char* img=getImage(selectedSaveLayer,selectedSaveChannel);
00142   if(img==NULL)
00143     return 0;
00144   memcpy(buf,img,used);
00145   len-=used; buf+=used;
00146 
00147   // color table
00148   if(0==(used=encodeColors(buf,len))) return 0;
00149   len-=used; buf+=used;
00150   
00151   return origlen-len;
00152 }
00153 
00154 unsigned int
00155 SegmentedColorGenerator::encodeColors(char buf[], unsigned int len) const {
00156   unsigned int origlen=len;
00157   unsigned int used;
00158   if(0==(used=encode(numColors,buf,len))) return 0;
00159   len-=used; buf+=used;
00160   for(unsigned int i=0; i<numColors; i++) {
00161     if(0==(used=encode(colors[i].color.red,buf,len))) return 0;
00162     len-=used; buf+=used;
00163     if(0==(used=encode(colors[i].color.green,buf,len))) return 0;
00164     len-=used; buf+=used;
00165     if(0==(used=encode(colors[i].color.blue,buf,len))) return 0;
00166     len-=used; buf+=used;
00167   }
00168   return origlen-len; 
00169 }
00170 
00171 unsigned int
00172 SegmentedColorGenerator::decodeColors(const char buf[], unsigned int len) {
00173   unsigned int origlen=len;
00174   unsigned int used;
00175   if(0==(used=decode(numColors,buf,len))) return 0;
00176   len-=used; buf+=used;
00177   for(unsigned int i=0; i<numColors; i++) {
00178     if(0==(used=decode(colors[i].color.red,buf,len))) return 0;
00179     len-=used; buf+=used;
00180     if(0==(used=decode(colors[i].color.green,buf,len))) return 0;
00181     len-=used; buf+=used;
00182     if(0==(used=decode(colors[i].color.blue,buf,len))) return 0;
00183     len-=used; buf+=used;
00184   }
00185   return origlen-len; 
00186 }
00187 
00188 void
00189 SegmentedColorGenerator::setDimensions() {
00190   FilterBankGenerator::setDimensions();
00191   for(unsigned int i=0; i<numLayers; i++)
00192     strides[i]=widths[i];
00193 }
00194 
00195 void
00196 SegmentedColorGenerator::setNumImages(unsigned int nLayers, unsigned int /*nChannels*/) {
00197   FilterBankGenerator::setNumImages(nLayers,tmaps.size());
00198 }
00199 
00200 unsigned char *
00201 SegmentedColorGenerator::createImageCache(unsigned int layer, unsigned int /*chan*/) const {
00202   return new unsigned char[widths[layer]*heights[layer]];
00203 }
00204 
00205 void
00206 SegmentedColorGenerator::calcImage(unsigned int layer, unsigned int chan) const {
00207   PROFSECTION("SegmentedColorGenerator::calcImage(...)",state->mainProfile);
00208   CMVision::image_yuv<const cmap_t> img;
00209   img.buf_y=src->getImage(layer,srcYChan);
00210   img.buf_u=src->getImage(layer,srcUChan);
00211   img.buf_v=src->getImage(layer,srcVChan);
00212   img.width=getWidth(layer);
00213   img.height=getHeight(layer);
00214   img.row_stride=src->getStride(layer);
00215 
00216   CMVision::ThresholdImageYUVPlanar<cmap_t,CMVision::image_yuv<const cmap_t>,const cmap_t,BITS_Y,BITS_U,BITS_V>(images[layer][chan],img,tmaps[chan]);
00217   imageValids[layer][chan]=true;
00218 }
00219 
00220 /*! @file
00221  * @brief Implements SegmentedColorGenerator, which generates FilterBankEvents indexed color images based on a color threshold file
00222  * @author alokl (Creator)
00223  * @author ejt (reorganized)
00224  *
00225  * $Author: ejt $
00226  * $Name: tekkotsu-2_2 $
00227  * $Revision: 1.6 $
00228  * $State: Exp $
00229  * $Date: 2004/02/18 21:13:32 $
00230  */
00231 

Tekkotsu v2.2
Generated Tue Oct 19 14:19:15 2004 by Doxygen 1.3.9.1