/home/fwph/code/wurde/modules/visionModule/Operators/ImageWriter.cpp

Go to the documentation of this file.
00001 #include "ImageWriter.H"
00002 #include <Logger.H>
00003 #include <string>
00004 #include <opencv/cv.h>
00005 #include <opencv/highgui.h>
00006 
00007 using namespace std;
00008 using namespace WURDE;
00009 using namespace WURDEVision;
00010 
00011 ImageWriter::ImageWriter(std::string name) : VisionOperator(name) {
00012 
00013 }
00014 
00015 void ImageWriter::initOperator() {
00016        // Potentially add two image saving threads here so that this operator doesn't
00017        // hold things up too badly.
00018 
00019        t1state=STATE_RUN;
00020        t2state=STATE_RUN;
00021        t3state=STATE_RUN;
00022 
00023        m_newt1image=false;
00024        m_newt2image=false;
00025        m_newt3image=false;
00026 
00027        m_t1image=cvCreateImage(cvSize(320, 240), IPL_DEPTH_8U, 3);
00028        m_t2image=cvCreateImage(cvSize(320, 240), IPL_DEPTH_8U, 3);
00029        m_t3image=cvCreateImage(cvSize(320, 240), IPL_DEPTH_8U, 3);
00030 
00031        pthread_create(&thread1,NULL,runImageWriterThread1,(void *)this);
00032        pthread_create(&thread2,NULL,runImageWriterThread2,(void *)this);
00033        pthread_create(&thread3,NULL,runImageWriterThread3,(void *)this);
00034 
00035        if(g_globalConfiguration.haveModuleOption(m_name,"imageDir")){
00036               m_imageDir=g_globalConfiguration.getModuleOption(m_name,"imageDir");
00037        }else{
00038               m_imageDir=g_globalConfiguration.getDataDirectory();
00039        }
00040 
00041        if(g_globalConfiguration.haveModuleOption(m_name,"imagePrefix")){
00042               m_imagePrefix=g_globalConfiguration.getModuleOption(m_name,"imagePrefix");
00043        }else{
00044               m_imagePrefix=m_name;
00045        }
00046 
00047        m_state=STATE_RUN;
00048        m_format=FORMAT_CHAR_111_RGB;
00049        m_stereo=false;
00050        next=1;
00051 }
00052 
00053 void ImageWriter::runOperator(WURDEImage mImage) {
00054        
00055        if(next==1){
00056               if(!m_newt1image){                
00057                      g_debug("Checkpoint 1");     
00058                      if(m_t1image->width!=mImage.second->width||
00059                         m_t1image->height!=mImage.second->height){
00060                             m_t1image=NULL;
00061                             m_t1image=cvCreateImage(cvSize(mImage.second->width, mImage.second->height), IPL_DEPTH_8U, 3);
00062                      }
00063 
00064                      t1time=mImage.first;
00065                      // memcpy(m_t1image->imageData,mImage.second->imageData,3*mImage.second->height*mImage.second->width);
00066                      cvConvertImage(mImage.second,m_t1image,CV_CVTIMG_SWAP_RB);
00067                      next=2;
00068 
00069                      m_newt1image=true;
00070               }
00071        }else if (next==2){
00072               if(!m_newt2image){
00073                      if(m_t2image->width!=mImage.second->width||
00074                         m_t2image->height!=mImage.second->height){
00075                             //                      cvReleaseImage(&m_t2image);
00076                             m_t2image=NULL;
00077                             m_t2image=cvCreateImage(cvSize(mImage.second->width, mImage.second->height), IPL_DEPTH_8U, 3);
00078                      }
00079                      t2time=mImage.first;
00080                      //memcpy(m_t2image->imageData,mImage.second->imageData,3*mImage.second->height*mImage.second->width);
00081                      cvConvertImage(mImage.second,m_t2image,CV_CVTIMG_SWAP_RB);
00082                      next=3;
00083                      m_newt2image=true;
00084               }
00085        }else{
00086               if(!m_newt3image){
00087                      if(m_t3image->width!=mImage.second->width||
00088                         m_t3image->height!=mImage.second->height){
00089                             //                      cvReleaseImage(&m_t3image);
00090                             m_t3image=NULL;
00091                             m_t3image=cvCreateImage(cvSize(mImage.second->width, mImage.second->height), IPL_DEPTH_8U, 3);
00092                      }
00093                      
00094                      t3time=mImage.first;
00095                      cvConvertImage(mImage.second,m_t3image,CV_CVTIMG_SWAP_RB);
00096                      //              memcpy(m_t3image->imageData,mImage.second->imageData,3*mImage.second->height*mImage.second->width);
00097                      next=1;
00098                      m_newt3image=true;
00099               }
00100        }
00101 
00102 }
00103 
00104 void ImageWriter::quitOperator() {
00105        t1state=STATE_QUIT;
00106        t2state=STATE_QUIT;
00107        t3state=STATE_QUIT;
00108 
00109        pthread_join(thread1,NULL);
00110        pthread_join(thread2,NULL);
00111        pthread_join(thread3,NULL);
00112 
00113        
00114        cvReleaseImage(&m_t1image);
00115        cvReleaseImage(&m_t2image);
00116        cvReleaseImage(&m_t3image);
00117 
00118        m_t1image=NULL;
00119        m_t2image=NULL;
00120        m_t3image=NULL;
00121 }
00122 
00123 void ImageWriter::fileWriterThread1(){
00124        while(t1state==STATE_RUN){
00125               if(m_newt1image){
00126                      std::stringstream filename;
00127                      filename.setf(ios_base::showpoint);
00128                      filename.setf(ios_base::fixed,ios_base::floatfield);
00129                      double stamp;
00130                      stamp=t1time.getSeconds()+t1time.getUSeconds()/1000000.0;
00131                      
00132                      filename << m_imageDir << "/" << m_imagePrefix << "-" << stamp << ".ppm";
00133                      g_debug("Thread 1 writing..");
00134                      cvSaveImage((filename.str()).c_str(),m_t1image);
00135 
00136                      m_newt1image=false;
00137               }
00138               usleep(50000);
00139        }
00140 }
00141 
00142 void ImageWriter::fileWriterThread2(){
00143        while(t1state==STATE_RUN){
00144               if(m_newt2image){
00145                      std::stringstream filename;
00146                      filename.setf(ios_base::showpoint);
00147                      filename.setf(ios_base::fixed,ios_base::floatfield);
00148                      double stamp;
00149                      stamp=t2time.getSeconds()+t2time.getUSeconds()/1000000.0;
00150                      filename << m_imageDir << "/" << m_imagePrefix << "-" << stamp << ".ppm";
00151                      g_debug("Thread 2 writing..");
00152                      cvSaveImage((filename.str()).c_str(),m_t2image);
00153                      
00154                      m_newt2image=false;
00155               }
00156               usleep(50000);
00157        }
00158 }
00159 
00160 void ImageWriter::fileWriterThread3(){
00161        while(t1state==STATE_RUN){
00162               if(m_newt3image){
00163                      std::stringstream filename;
00164                      filename.setf(ios_base::showpoint);
00165                      filename.setf(ios_base::fixed,ios_base::floatfield);
00166                      double stamp;
00167                      stamp=t3time.getSeconds()+t3time.getUSeconds()/1000000.0;
00168                      
00169                      filename << m_imageDir << "/" << m_imagePrefix << "-" << stamp << ".ppm";
00170                      g_debug("Thread 3 writing..");
00171                      cvSaveImage((filename.str()).c_str(),m_t3image) ;
00172 
00173                      m_newt3image=false;
00174               
00175 }             usleep(50000);
00176        }
00177 }
00178 
00179 extern "C" {
00180        void *runImageWriterThread1(void *ptr){
00181               ImageWriter *theObject=(ImageWriter *)ptr;
00182               
00183               theObject->fileWriterThread1();
00184               pthread_exit(NULL);
00185        }
00186 
00187        void *runImageWriterThread2(void *ptr){
00188               ImageWriter *theObject=(ImageWriter *)ptr;
00189               
00190               theObject->fileWriterThread2();
00191               pthread_exit(NULL);
00192        }
00193        
00194        void *runImageWriterThread3(void *ptr){
00195               ImageWriter *theObject=(ImageWriter *)ptr;
00196               
00197               theObject->fileWriterThread3();
00198               pthread_exit(NULL);
00199        }
00200 
00201        VisionOperator *ImageWriter_factory(std::string name) {
00202               return new ImageWriter(name);
00203        }
00204        
00205        class ImageWriterProxy { 
00206        public:
00207               ImageWriterProxy(){
00208                      operatorFactory["ImageWriter"] = ImageWriter_factory;
00209               }
00210        };
00211        
00212        ImageWriterProxy p_imagewriter;
00213 }

Generated on Thu Feb 1 15:31:52 2007 for WURDE by  doxygen 1.5.1