00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00050
00051
00052
00053
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
00076 XIPD::ArgumentType *typeDesc=improcs.GetArgumentType(type,false);
00077 if(typeDesc && typeDesc->type=="list"){return new ListArgument(argument,improcs);}
00078
00079
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
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
00127
00128 int carg=1;
00129 for(uint i=0;i<size();i++)
00130 {
00131 if(resArgs[i]!=""){continue;}
00132 ImProcStringArgument *arg=args[i];
00133 if(arg->IsOptional() && carg>=(int)cmdline.size())
00134 {break;}
00135
00136 try
00137 {
00138
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
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
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
00192
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
00226
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 }