#include "iclFileGrabberPluginCSV.h" #include #include #include #include #include #include using namespace std; namespace icl{ namespace{ template void tokenzie_line_tmpl(const std::vector &v, T *data){ // {{{ open for(unsigned int i=0;i(atof(v[i].c_str())); } } // }}} void tokezize_line(const std::vector &v,ImgBase *image, int c, int y){ // {{{ open if(image->getWidth() > (int)v.size()){ throw InvalidFileFormatException(); } switch(image->getDepth()){ #define ICL_INSTANTIATE_DEPTH(D) case depth##D: tokenzie_line_tmpl(v,image->asImg()->getROIData(c,Point(0,y))); break; ICL_INSTANTIATE_ALL_DEPTHS; #undef ICL_INSTANTIATE_DEPTH } } // }}} } FileGrabberPluginCSV::FileGrabberPluginCSV(){ // {{{ open m_poReadBuffer = new Img64f; m_poReadBufferMutex = new Mutex; } // }}} FileGrabberPluginCSV::~FileGrabberPluginCSV(){ // {{{ open ICL_DELETE( m_poReadBuffer ); ICL_DELETE( m_poReadBufferMutex ); } // }}} void FileGrabberPluginCSV::grab(File &file, ImgBase **dest){ // {{{ open ICLASSERT_RETURN(dest); file.open(File::readText); ICLASSERT_RETURN(file.isOpen()); ////////////////////////////////////////////////////////////////////// //// Estimate Header Data 1st: look at the filename ///////////////// ////////////////////////////////////////////////////////////////////// FileGrabberPlugin::HeaderInfo oInfo; oInfo.channelCount = 1; oInfo.imageDepth = depth64f; oInfo.imageFormat = formatMatrix; oInfo.size = Size::null; oInfo.roi = Rect::null; oInfo.time = Time(); string filename = file.getBaseName(); //printf("basename is {%s} \n",filename.c_str()); string line; string::size_type t = string::npos; if((t=filename.find("-ICL:"))!= string::npos){ while(1){ // this is necessary to use break on errors vector ts = tok(filename.substr(t+5),":"); if(ts.size() != 3){ ERROR_LOG("Invalid ICL-CSV filen name appendix in \"" << filename << "\"[CODE 1]"); break; } vector whc = StrTok(ts[0],"x").allTokens(); if(whc.size() != 3) { ERROR_LOG("Invalid ICL-CSV filen name appendix in \"" << filename << "\"[CODE 2]"); break; } oInfo.imageDepth = translateDepth(ts[1]); oInfo.imageFormat = translateFormat(ts[2]); oInfo.size = translateSize(whc[0]+"x"+whc[1]); oInfo.roi = Rect(Point::null,oInfo.size); oInfo.channelCount = atoi(whc[2].c_str()); oInfo.time = Time(); break; } line = file.readLine(); }else do{ line = ioutils::skipWhitespaces(file.readLine()); if(line.length() && line[0] == '#'){ vector ts = tok(line," "); if(ts.size() < 3) continue; if (ts[1] == "ROI") { oInfo.roi = Rect(atoi(ts[2].c_str()),atoi(ts[3].c_str()),atoi(ts[4].c_str()),atoi(ts[5].c_str())); continue; } else if (ts[1] == "ImageDepth") { oInfo.imageDepth = translateDepth( ts[2] ); continue; } else if (ts[1] == "Format") { oInfo.imageFormat = translateFormat(ts[2]); } else if (ts[1] == "TimeStamp") { oInfo.time = Time::microSeconds(atoi(ts[2].c_str())); continue; } else if (ts[1] == "Size") { oInfo.size = Size(parse(ts[2]),parse(ts[3])); continue; } else if (ts[1] == "Channels") { oInfo.channelCount = parse(ts[2]); continue; } }else{ break; } }while(true); ////////////////////////////////////////////////////////////////////// // CHECK IF A "SIZE" HINT WAS FOUND (IN THE HEADER OR IN THE FILENAME/ ////////////////////////////////////////////////////////////////////// if(oInfo.size == Size::null){ /// analyse the file and reopen it! bool end = false; while(!end){ oInfo.size.height++; // additional line oInfo.size.width = iclMax((unsigned int)oInfo.size.width,tok(line,",").size()); if(file.hasMoreLines()){ line = file.readLine(); }else{ end = true; } } file.reset(); oInfo.imageDepth = depth64f; oInfo.imageFormat = formatMatrix; oInfo.roi = Rect(Point::null,oInfo.size); oInfo.channelCount = 1; oInfo.time = Time(); } ////////////////////////////////////////////////////////////////////// /// ADAPT THE DESTINATION IMAGE ////////////////////////////////////// ////////////////////////////////////////////////////////////////////// ensureCompatible (dest, oInfo.imageDepth, oInfo.size, oInfo.channelCount,oInfo.imageFormat, oInfo.roi); ImgBase *poImg = *dest; poImg->setTime(oInfo.time); ////////////////////////////////////////////////////////////////////// /// READ THE IMAGE DATA ///////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// for(int c=0;cgetChannels();c++){ for(int y=0;ygetHeight();++y){ tokezize_line(tok(line,","),poImg,c,y); if(!(c==poImg->getChannels()-1 && y == poImg->getHeight()-1)){ line = file.readLine(); } } } } // }}} }