]> Creatis software - clitk.git/blob - vv/vv.cxx
Corrected log option for portability
[clitk.git] / vv / vv.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #include <utility>
19 #include <cassert>
20 #include <ctime>
21 #include <string>
22 #include <ostream>
23 #include <QApplication>
24 #include <QPixmap>
25 #include <QSplashScreen>
26 #include <QTimer>
27 #include <QDesktopWidget>
28 #include <QDir>
29
30 #include "clitkIO.h"
31 #include "vvMainWindow.h"
32 #include "vvConstants.h"
33
34 #include <vtkFileOutputWindow.h>
35 #include <vtkSmartPointer.h>
36 #include <itkFileOutputWindow.h>
37 #include <itkSmartPointer.h>
38 #include <itksys/SystemTools.hxx>
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include  <errno.h> 
43
44 void load_image_first_error()
45 {
46   std::cerr << "You need to load an image before adding an overlay!" << std::endl;
47   exit(1);
48 }
49
50 std::string create_timed_string()
51 {
52   time_t t;
53   time(&t);
54   
55   struct tm* pt = localtime(&t);
56   
57   const int size = 64;
58   char st[size];
59   strftime(st, size, "%Y%m%d-%H%M%S", pt);
60   
61   return st;
62 }
63
64 //------------------------------------------------------------------------------
65 int main( int argc, char** argv )
66 {
67   CLITK_INIT;
68
69   QApplication app( argc, argv );
70   Q_INIT_RESOURCE(vvIcons);
71   //QPixmap pixmap(":/splashscreen.PNG");
72   QSplashScreen *splash = new QSplashScreen(QPixmap(QString::fromUtf8(":/new/prefix1/splashscreen.PNG")));
73   /*splash->showMessage("VV 1.0 developped by Léon Bérard c`ancer center http://oncora1.lyon.fnclcc.fr and CREATIS-LRMN http://www.creatis.insa-lyon.fr",(Qt::AlignRight | Qt::AlignBottom));*/
74   //  splash->show();
75   QTimer::singleShot(2000, splash, SLOT(close()));
76   while (!splash->isHidden())
77     app.processEvents();
78
79   vvMainWindow window;
80
81   //Try to give the window a sensible default size
82   int width=QApplication::desktop()->width()*0.8;
83   int height=QApplication::desktop()->height()*0.9;
84   if (width> 1.5*height)
85     width=1.5*height;
86   window.resize(width,height);
87
88   window.show();
89
90   std::vector<std::string> sequence_filenames;
91   enum {P_NORMAL,P_SEQUENCE,P_WINDOW,P_LEVEL};
92   int parse_mode=P_NORMAL;
93   int n_image_loaded=0;
94   std::string win(""), lev("");
95
96   if (argc >1) {
97     for (int i = 1; i < argc; i++) {
98       std::string current = argv[i];
99       if (!current.compare(0,2,"--")) { //We are parsing an option
100         if (parse_mode == P_SEQUENCE) {//First finish the current sequence
101           window.LoadImages(sequence_filenames,MERGEDWITHTIME);
102           sequence_filenames.clear();
103           parse_mode=P_NORMAL;
104         }
105         if (current=="--vf") {
106           if (!n_image_loaded) load_image_first_error();
107           window.AddField(argv[i+1],n_image_loaded-1);
108           i++; //skip vf name
109         } else if (current=="--overlay") {
110           if (!n_image_loaded) load_image_first_error();
111           window.AddOverlayImage(n_image_loaded-1,argv[i+1]);
112           i++; //skip overlay name
113         } else if (current=="--roi") {
114           if (!n_image_loaded) load_image_first_error();
115           window.AddROI(n_image_loaded-1,argv[i+1]);
116           i++; //skip roi name
117         } else if (current=="--fusion") {
118           if (!n_image_loaded) load_image_first_error();
119           window.AddFusionImage(n_image_loaded-1,argv[i+1]);
120           i++; //skip fusion name
121         } else if (current == "--sequence") {
122           n_image_loaded++; //count only one for the sequence
123           parse_mode=P_SEQUENCE;
124         } else if (current == "--window") {
125           parse_mode=P_WINDOW;
126         } else if (current == "--level") {
127           parse_mode=P_LEVEL;
128         } else if (current == "--log") {
129           std::string log_dir = QDir::tempPath().toStdString() + std::string("/vv-log");
130
131           if(itksys::SystemTools::FileExists(log_dir.c_str()) &&
132              !itksys::SystemTools::FileIsDirectory(log_dir.c_str())) {
133             itkGenericExceptionMacro(<< "Error creating log directory, file exists and is not a directory.");
134           }
135           else if(!itksys::SystemTools::MakeDirectory(log_dir.c_str())) {
136             itkGenericExceptionMacro(<< "Error creating log directory.");
137           }
138
139           std::string log_file = log_dir + "/" + create_timed_string() + ".log";
140           vtkSmartPointer<vtkFileOutputWindow> vtk_log = vtkFileOutputWindow::New();
141           vtk_log->SetFileName(log_file.c_str());
142           vtk_log->FlushOn();
143           vtkOutputWindow::SetInstance(vtk_log);
144           
145           itk::SmartPointer<itk::FileOutputWindow> itk_log = itk::FileOutputWindow::New();
146           itk_log->SetFileName(log_file.c_str());
147           itk_log->FlushOn();
148           itk::OutputWindow::SetInstance(itk_log);
149         }
150       } else if (parse_mode == P_SEQUENCE) {
151         sequence_filenames.push_back(current);
152       } else if (parse_mode == P_WINDOW) {
153         win=current;
154         parse_mode=P_NORMAL;
155       } else if (parse_mode == P_LEVEL) {
156         lev=current;
157         parse_mode=P_NORMAL;
158       } else {
159         std::vector<std::string> image;
160         image.push_back(current);
161         window.LoadImages(image,IMAGE);
162         n_image_loaded++;
163       }
164     }
165     if (parse_mode == P_SEQUENCE) { //Finish any current sequence
166       window.LoadImages(sequence_filenames,MERGEDWITHTIME);
167       sequence_filenames.clear();
168       parse_mode=P_NORMAL;
169     }
170   }
171
172   if(win!="" && lev!="") {
173     window.WindowLevelChanged(atof(win.c_str()), atof(lev.c_str()), 6, 0);
174     window.ApplyWindowLevelToAllImages();
175   }
176
177   return app.exec();
178 }