Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

ImageProcessorDescription.cpp

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00020 #include<ImLib3D/ImageProcessorDescription.hpp>
00021 #include<ImLib3D/Image3D.hpp>
00022 
00023 
00024 bool 
00025 ImageArgument::IsValid(const string &strArg,string &message)
00026 {
00027     if(IsInput())
00028     {
00029         string imType=Container3D_ReadImageType(strArg);
00030         if(imType=="NOT IMLIB3D IMAGE"){message="invalid image:\""+strArg+"\"";return false;}
00031     }
00032     else
00033     {
00034         if(strArg==""){message="empty result filename";return false;}
00035     }
00036     return true;
00037 }
00038 
00039 ImProcStringArgument *
00040 ImProcStringArgument::Create(XIPD::Argument &argument,ImageProcessorsDescription &improcs)
00041 {
00042     string type=argument.type;
00043     if(type=="bool"    ){return new ImProcStringArgument(argument,improcs);}
00044     if(type=="string"  ){return new ImProcStringArgument(argument,improcs);}
00045     if(type=="int"     ){return new ImProcStringArgument(argument,improcs);}
00046     if(type=="float"   ){return new ImProcStringArgument(argument,improcs);}
00047 
00048     if(type=="ImageType"){return new ImageArgument(argument,improcs);}
00049 //  if(type=="StructureElement"   ){return new ImProcStringArgument(argument,improcs);}
00050     // FIXME this should find a smarter, reliable way of knowing
00051     // if this is an image argument (maybe l this argument stuff 
00052     // is not in its right place, maybe this should be done by applications
00053     // needs
00054     vector<string> imageTypes;
00055     imageTypes.push_back("BitImage3D<byte,1,byte>");
00056     imageTypes.push_back("BitImage3D<byte,2,byte>");
00057     imageTypes.push_back("Image3Dd");
00058     imageTypes.push_back("Image3Df");
00059     imageTypes.push_back("Image3Dlinear<complex<double> >");
00060     imageTypes.push_back("Image3Dlinear<complex<float> >");
00061     imageTypes.push_back("Image3Dreal<int>");
00062     imageTypes.push_back("Image3Dreal<short int>");
00063     imageTypes.push_back("Mask3D");
00064     imageTypes.push_back("Image3Dlinear<Vect3Df>");
00065     imageTypes.push_back("Image3Dlinear<Vect3D<float, float> >");
00066     imageTypes.push_back("Field3Df");
00067  
00068     if(find(imageTypes.begin(),imageTypes.end(),type)!=imageTypes.end())
00069     {
00070         return new ImageArgument(argument,improcs);
00071     }
00072 
00073     if(type=="ImageTypeName") {return new ImProcStringArgument(argument,improcs);}
00074 
00075     // types with descriptions
00076     XIPD::ArgumentType *typeDesc=improcs.GetArgumentType(type,false);
00077     if(typeDesc && typeDesc->type=="list"){return new ListArgument(argument,improcs);}
00078 
00079 //  printf("unknown argument description type:\"%s\" ... using default\n",type.c_str());
00080     return new ImProcStringArgument(argument,improcs);
00081 }
00082 
00083 vector<string> 
00084 ImageProcessorDescription::BuildCmdLine(const vector<string> &args0,bool stripTrailingSpaces)
00085 {
00086     vector<string> res;
00087     res.push_back(name);
00088     for(uint i=0;i<args.size();i++)
00089     {
00090         string str=args0[i];
00091         if(str.size()!=0)
00092         {
00093             if(stripTrailingSpaces)
00094             {
00095                 int j;
00096                 for(j=str.size()-1;j>=0 && isspace(str[j]);j--);
00097                 str=str.substr(0,j+1);
00098             }
00099             if(str[0]=='-' && str.size()>1 && str[1]=='-')
00100             {ThrowError("ImageProcessorDescription::BuildCmdLine arg starts with \"--\", could be mistaken with switch");} 
00101         }
00102         res.push_back(args[i]->FullSwitch()+"=\""+str+"\"");
00103     }
00104     return res;
00105 }
00106 
00107 vector<string> 
00108 ImageProcessorDescription::ParseCmdLine(vector<string> cmdline)
00109 {
00110     vector<string> resArgs(size());
00111 
00112     // **** remove full-switch'ed args (must be quoted if spaces)
00113     for(uint i=0;i<cmdline.size();i++)
00114     {
00115         string arg=cmdline[i];
00116         if(arg.substr(0,2)=="--")
00117         {
00118             uint eq=arg.find('=');
00119             if(eq==string::npos){ThrowError("no argument for switch \"%s\"",arg);}
00120             resArgs[FindFullSwitch(arg.substr(0,eq))]=arg.substr(eq+1);
00121             cmdline.erase(cmdline.begin()+i);
00122             i--;
00123             continue;
00124         }
00125     }
00126     // **** remove short-switch'ed args
00127     // **** fit remaining args      
00128     int carg=1; // current index in initial cmdline args (ignore 0->procName)
00129     for(uint i=0;i<size();i++)
00130     {
00131         if(resArgs[i]!=""){continue;}// arg has already been processed
00132         ImProcStringArgument *arg=args[i];
00133         if(arg->IsOptional() && carg>=(int)cmdline.size())
00134         {break;}// no more optional args on command line
00135 
00136         try
00137         {
00138             // find number of expected elements for this argument
00139             int cmdLineNumber=1;
00140             if(arg->TypeDescritption())
00141             {
00142                 cmdLineNumber=LongInt(arg->TypeDescritption()->cmdLineNumber);
00143             }
00144             if(cmdLineNumber==0){cmdLineNumber=1;}
00145             if((carg+cmdLineNumber)>(int)cmdline.size()){ThrowError("not enough args\n");}
00146             // put all  elements for this argument in res string
00147             for(int j=0;j<cmdLineNumber;j++){resArgs[i]+=(j>0 ? " " : "")+cmdline[carg++];}
00148         }catch(std::exception& __fail)
00149         {ThrowError("problem in parsing argument \"%s\"",arg->Name());}
00150     }
00151     if(carg!=(int)cmdline.size()){ThrowError("too many commandline args");}
00152     return resArgs;
00153 }
00154 
00155 void  
00156 ImageProcessorDescription::ValidateArguments(const vector<string> &resArgs)
00157 {
00158     // validate arguments
00159     for(uint i=0;i<size();i++)
00160     {
00161         string message;
00162         if(args[i]->IsOptional() && resArgs[i]==""){continue;}
00163         if(!args[i]->IsValid(resArgs[i],message))
00164         {ThrowError("Invalid argument \"%s\":%s",args[i]->Name(),message);}
00165     }
00166     for(uint i=0;i<resArgs.size();i++)
00167     {
00168         printf("validated final arg:%d:\"%s\"\n",i,resArgs[i].c_str());
00169     }
00170 }
00171 
00172 
00173 int 
00174 ImageProcessorDescription::ProcTemplateTypeNamePos()
00175 {
00176     XIPD::ImageProcessor *proc=improcs.GetImageProcessor(name);
00177     if(proc->type=="standard templated" && proc->templateGroup==""){return 0;}
00178     if(proc->type=="standard templated" && proc->templateGroup!=""){return LongInt(proc->templateGroup);}
00179     return -1;
00180 }
00181 
00182 string 
00183 ImageProcessorDescription::ProcTemplateTypeName(vector<string> resArgs)
00184 {
00185     string typeName;
00186     XIPD::ImageProcessor *proc=improcs.GetImageProcessor(name);
00187     if(proc->type=="standard templated" )
00188     {
00189         typeName=Container3D_ReadImageType(resArgs[0]);
00190     }
00191 //  if(proc->type=="standard templated" && proc->templateGroup==""){typeName=Container3D_ReadImageType(resArgs[1]);}
00192 //  if(proc->type=="standard templated" && proc->templateGroup!=""){typeName=resArgs[LongInt(proc->templateGroup)];}
00193     return typeName;
00194 }
00195 
00196 
00197 void 
00198 ImageProcessorDescription::PrintDoxyDoc(const string& type)
00199 {
00200     if(type=="class head")
00201     {
00202         improcs.Print(shortDescription,4,80);
00203         improcs.Print(longDescription,8,76);            
00204         vector<string> argnames;
00205         for(uint i=0;i<args.size();i++){argnames.push_back(args[i]->Name().c_str());}
00206         improcs.Pad(argnames);
00207         printf("     <UL>Args:\n");
00208         for(uint i=0;i<args.size();i++)
00209         {
00210             printf("      <LI>%s  type:%s\n",
00211                    argnames[i].c_str(),
00212                    args[i]->Type().c_str() );
00213                 
00214         }
00215         printf("     </UL>\n");
00216     }
00217 }
00218 
00219 void 
00220 ImageProcessorDescription::PrintHelp()
00221 {
00222     printf("%s (%s)\n",name.c_str(),parent.c_str());
00223     improcs.Print(shortDescription,4,80);
00224     improcs.Print(longDescription,8,76);
00225 //          printf("%s\n\n",GetContent(name,"ShortDescription").c_str());
00226 //          printf("%s\n",GetContent(name,"LongDescription").c_str());
00227     vector<string> argnames;
00228     for(uint i=0;i<args.size();i++)
00229     {
00230         string name=args[i]->Name().c_str();
00231         if(args[i]->IsOptional()){name="[opt] "+name;}
00232         argnames.push_back(name);
00233     }
00234     improcs.Pad(argnames);
00235     printf("    Args:\n");
00236     for(uint i=0;i<args.size();i++)
00237     {
00238         printf("      %s  type:%s\n",
00239                argnames[i].c_str(),
00240                args[i]->Type().c_str() );
00241                    
00242     }
00243 }

Generated on Fri Jun 17 13:36:04 2005 for ImLib3D by  doxygen 1.4.2