• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

apps/pnm2vti/pnm2vti.cpp

Go to the documentation of this file.
00001 
00022 #include "vtkImageData.h"
00023 #include "vtkPNMReader.h"
00024 #include "vtkXMLImageDataWriter.h"
00025 
00026 #include "DGVAliases.h"
00027 
00029 void processArgs(int argcount, char *argvars[], int &binary, string &prefix, int &startSlice, int &endSlice);
00030 
00031 int main(int argc, char *argv[])
00032 {
00033     int start = 0, end = 10, tmp = 0;
00034     string filePrefix, tmpStr1, tmpStr2;
00035     bool writeBinary = false, isPrefix = true;
00036 
00037     cout << ">| Portable aNy Map (PNM) Format to VTK Image Format (VTI)" << endl;
00038     cout << ">| PNM formats have the extension *.pbm *.pgm *.pnm *.ppm" << endl;
00039     cout << ">| It may read in a single file or a stack like: <FileName>.pgm.[Number]" << endl;
00040     cout << ">| Copyright Shekhar Chandra, 2007-2009." << endl;
00041 
00043         processArgs(argc,argv,tmp,filePrefix,start,end);
00044 
00045         if(start == 0 && end == 0)
00046         isPrefix = false;
00047     if(tmp == 1)
00048         writeBinary = true;
00049 
00050     cout << ">| File to be read";
00051     if(isPrefix)
00052     {
00053         cout << " with prefix: " << filePrefix << endl;
00054         cout << ">| Slices to be read: " << start << "-" << end << endl;
00055     }
00056     else
00057         cout << " with name: " << filePrefix << endl;
00058     cout << ">| VTI file written to : " << filePrefix+".vti";
00059     if(writeBinary)
00060         cout << " (Binary)" << endl;
00061     else
00062         cout << " (ASCII)" << endl;
00063     cout << ">| You may wish to rename this" << endl;
00064 
00066     vtkPNMReader* reader = vtkPNMReader::New();
00067     tmpStr1 = filePrefix + "." + numToString(start);
00068     tmpStr2 = filePrefix + "." + numToString(end);
00069     if( (reader->CanReadFile(tmpStr1.c_str())
00070         && reader->CanReadFile(tmpStr2.c_str()))
00071         || reader->CanReadFile(filePrefix.c_str()) )
00072     {
00073         reader->SetDebug(true);
00074         if(isPrefix)
00075         {
00076             int bounds[6] = {0};
00077 
00078             bounds[4] = start;
00079             bounds[5] = end;
00080 
00081             reader->SetDataExtent(bounds);
00082             reader->SetFilePrefix(filePrefix.c_str());
00083             reader->Update();
00084         }
00085         else
00086         {
00087             reader->SetFileName(filePrefix.c_str());
00088             reader->Update();
00089         }
00090 
00091         vtkImageData *data = reader->GetOutput();
00092 
00094         cout << ">| Writing VTI File ";
00095         vtkXMLImageDataWriter *XMLwriter = vtkXMLImageDataWriter::New();
00096             filePrefix += ".vti";
00097             XMLwriter->SetFileName(filePrefix.c_str());
00098             if(writeBinary)
00099                 XMLwriter->SetDataModeToBinary();
00100             else
00101                 XMLwriter->SetDataModeToAscii();
00102             XMLwriter->SetInput(data);
00103             XMLwriter->Write();
00104         cout << "Done." << endl;
00105 
00106         XMLwriter->Delete();
00107     }
00108     reader->Delete();
00109 
00110     return EXIT_SUCCESS;
00111 }
00112 
00114 void processArgs(int argcount, char *argvars[], int &binary, string &prefix, int &startSlice, int &endSlice)
00115 {
00116     switch(argcount)
00117     {
00118         case 2:
00119             prefix = argvars[1];
00120             binary = 0;
00121             startSlice = 0;
00122             endSlice = 0;
00123             break;
00124         case 3:
00125             prefix = argvars[1];
00126             binary = atoi(argvars[2]);
00127             startSlice = 0;
00128             endSlice = 0;
00129             break;
00130         case 5:
00131             prefix = argvars[1];
00132             binary = atoi(argvars[2]);
00133             startSlice = atoi(argvars[3]);
00134             endSlice = atoi(argvars[4]);
00135             break;
00136         default:
00137             printf(">| Usage: %s <File Name/Prefix> [Write Binary?] [Start Bound] [End Bound]\n",argvars[0]);
00138             printf(">| <File Name/Prefix> is required and the rest are optional.\n");
00139             printf(">| [Write Binary] should be a Boolean.");
00140             exit(EXIT_FAILURE);
00141     }
00142 }

Generated on Wed Sep 8 2010 01:36:51 for DGV by  doxygen 1.7.1