00001 #include <odindata/fileio.h>
00002 #include <odindata/utils.h>
00003 #include <odindata/filter.h>
00004
00030 struct MiConvOpts : JcampDxBlock {
00031
00032 JDXstring protfile;
00033 JDXstring parlist;
00034 JDXbool non_verbose;
00035
00036 MiConvOpts() {
00037
00038 protfile.set_cmdline_option("p").set_description("Load this protcol for defaults");
00039 append_member(protfile,"protfile");
00040
00041 parlist.set_cmdline_option("l").set_description("Lists space-separated set of protocol parameters and exits. Use '-l help' to get a list of possible parameters and '-l all' to list all parameters. The parameter 'output-file' must be omitted when using this option.");
00042 append_member(parlist,"parlist");
00043
00044 non_verbose.set_cmdline_option("nv").set_description("be less verbose, show progress in percent only");
00045 append_member(non_verbose,"non_verbose");
00046 }
00047
00048 };
00049
00051
00052 void usage(const Protocol& prot) {
00053 FileReadOpts ropts;
00054 FileWriteOpts wopts;
00055 MiConvOpts mopts;
00056 FilterFactory filter;
00057 STD_cout << "miconv: Converts medical image data between formats." << STD_endl;
00058 STD_cout << " File formats are automatically identified by their file extension." << STD_endl;
00059 STD_cout << " It expects at least two filenames/directories (for the input and output file) at the end of the command line." << STD_endl;
00060 STD_cout << " An exception is the option '-l' which requires only one filename/directory to list its content." << STD_endl;
00061 STD_cout << "Usage: miconv [options] <input-file> [<output-file>]" << STD_endl;
00062 STD_cout << "General options:" << STD_endl;
00063 STD_cout << mopts.get_cmdline_usage("\t");
00064 STD_cout << "Protocol options:" << STD_endl;
00065 STD_cout << prot.get_cmdline_usage("\t");
00066 STD_cout << "File read options:" << STD_endl;
00067 STD_cout << ropts.get_cmdline_usage("\t");
00068 STD_cout << "File write options:" << STD_endl;
00069 STD_cout << wopts.get_cmdline_usage("\t");
00070 STD_cout << "Filters:" << STD_endl;
00071 STD_cout << filter.get_cmdline_usage("\t");
00072 STD_cout << "Other options:" << STD_endl;
00073 STD_cout << "\t" << LogBase::get_usage() << STD_endl;
00074 STD_cout << "\t" << helpUsage() << STD_endl;
00075 STD_cout << "Supported file extensions(formats):" << STD_endl;
00076 STD_cout << FileIO::autoformats_str("\t") << STD_endl;
00077 }
00078
00079
00081
00082 int main(int argc, char* argv[]) {
00083 if(LogBase::set_log_levels(argc,argv)) return 0;
00084
00085 Log<OdinData> odinlog("miconv","main");
00086
00087 char optval[ODIN_MAXCHAR];
00088
00089 Protocol prot_template;
00090 FileReadOpts ropts;
00091 FileWriteOpts wopts;
00092 MiConvOpts mopts;
00093
00094 if(hasHelpOption(argc,argv)) {usage(prot_template); return 0;}
00095 if(argc<3) {usage(prot_template); return 0;}
00096
00097
00098 prot_template.seqpars.set_MatrixSize(readDirection,1);
00099 prot_template.seqpars.set_MatrixSize(phaseDirection,1);
00100 prot_template.seqpars.set_MatrixSize(sliceDirection,1);
00101
00102 mopts.parse_cmdline_options(argc,argv);
00103
00104 STD_string infile;
00105 STD_string outfile;
00106
00107
00108 if(mopts.parlist!="") {
00109 infile=argv[argc-1];
00110 } else {
00111 infile=argv[argc-2];
00112 outfile=argv[argc-1];
00113 }
00114
00115 if(mopts.parlist=="help") {
00116 STD_cout << "Possible protocol parameters:" << STD_endl;
00117 for(unsigned int i=0; i<prot_template.numof_pars(); i++) {
00118 JcampDxClass& ldr=prot_template[i];
00119 if(ldr.get_filemode()!=exclude) {
00120 STD_cout << ldr.get_label() << STD_endl;
00121 }
00122 }
00123 return 0;
00124 }
00125
00126 svector pars;
00127 if(mopts.parlist=="all") {
00128 for(unsigned int i=0; i<prot_template.numof_pars(); i++) {
00129 JcampDxClass& ldr=prot_template[i];
00130 if(ldr.get_filemode()!=exclude) pars.push_back(ldr.get_label());
00131 }
00132 } else {
00133 pars=tokens(mopts.parlist);
00134 }
00135
00136 int npars=pars.size();
00137 if(npars) FileIO::set_trace_status(false);
00138
00139 if(mopts.protfile!="") {
00140 ODINLOG(odinlog,infoLog) << "Loading default protocol " << optval << STD_endl;
00141 prot_template.load(mopts.protfile);
00142 }
00143
00144
00145 prot_template.parse_cmdline_options(argc,argv,false);
00146
00147 FileIO::ProtocolDataMap pdmap_in;
00148
00149
00150 ropts.parse_cmdline_options(argc,argv);
00151
00152 int readresult;
00153 if(mopts.non_verbose){
00154 ProgressDisplayConsole display;
00155 ProgressMeter mProgress(display);
00156 readresult=FileIO::autoread(pdmap_in, infile, ropts, prot_template,&mProgress);
00157 }
00158 else readresult=FileIO::autoread(pdmap_in, infile, ropts, prot_template);
00159
00160 if(readresult<0) {
00161 ODINLOG(odinlog,errorLog) << "autoread failed" << STD_endl;
00162 return readresult;
00163 }
00164
00165
00166 if(npars) {
00167 sarray table(npars+1,pdmap_in.size()+1);
00168 for(int ipar=0; ipar<npars; ipar++) {
00169 table(ipar,0)=pars[ipar];
00170 }
00171 table(npars,0)="Dataset";
00172 int imap=1;
00173 for(FileIO::ProtocolDataMap::const_iterator pdit=pdmap_in.begin(); pdit!=pdmap_in.end(); ++pdit) {
00174 for(int ipar=0; ipar<npars; ipar++) {
00175 table(ipar,imap)=rmblock(pdit->first.printval(pars[ipar]), "\n", "");
00176 }
00177 STD_ostringstream oss;
00178 oss << pdit->second.shape();
00179 table(npars,imap)=oss.str();
00180 imap++;
00181 }
00182
00183 STD_cout << print_table(table);
00184
00185 return 0;
00186 }
00187
00188 ODINLOG(odinlog,normalDebug) << "mem(in ): " << Profiler::get_memory_usage() << STD_endl;
00189
00190
00191 FileIO::ProtocolDataMap pdmap_out;
00192 for(FileIO::ProtocolDataMap::const_iterator pdit=pdmap_in.begin(); pdit!=pdmap_in.end(); ++pdit) {
00193 Protocol prot(pdit->first);
00194 prot.parse_cmdline_options(argc,argv);
00195 pdmap_out[prot].reference(pdit->second);
00196 }
00197
00198 ODINLOG(odinlog,normalDebug) << "mem(out): " << Profiler::get_memory_usage() << STD_endl;
00199
00200
00201 wopts.parse_cmdline_options(argc,argv);
00202
00203 FilterChain filterchain(argc,argv);
00204
00205
00206 if(!filterchain.apply(pdmap_out)) return -1;
00207
00208
00209
00210 int result=FileIO::autowrite(pdmap_out, outfile, wopts);
00211
00212 if(result>0) result=0;
00213 return result;
00214 }