]> Creatis software - clitk.git/blob - vv/vv.cxx
Merge branch 'master' of /home/dsarrut/clitk3.server
[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://www.centreleonberard.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 <sstream>
24 #include <QApplication>
25 #include <QPixmap>
26 #include <QSplashScreen>
27 #include <QTimer>
28 #include <QDesktopWidget>
29 #include <QDir>
30
31 #include "clitkIO.h"
32 #include "vvMainWindow.h"
33 #include "vvToolsList.h"
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 #ifdef _WIN32
66 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nShowCmd)
67 {
68   int argc = __argc;
69   char **argv = __argv;
70 #else
71 int main( int argc, char** argv )
72 {
73 #endif
74
75   CLITK_INIT;
76
77   QApplication app( argc, argv );
78   Q_INIT_RESOURCE(vvIcons);
79   
80   // 
81   // ATTENTION: Rômulo Pinho - 05/08/2011
82   // Forcing the locale of the application is necessary
83   // because QT initialization changes it to the locale
84   // of the language of the system. This can cause 
85   // inconsistencies when, e.g., reading float values
86   // from DICOM fields with gdcm, since the decimal
87   // point may be changed for a comma (as in French).
88   // In practice, functions such as scanf and its
89   // variations are directly affected.
90   // https://bugreports.qt.nokia.com//browse/QTBUG-15247?page=com.atlassian.jira.plugin.system.issuetabpanels%253Achangehistory-tabpanel
91   //
92 #ifndef _WIN32
93   std::string old_locale = setlocale(LC_NUMERIC, NULL);
94   setlocale(LC_NUMERIC, "POSIX");
95 #endif
96
97   vvMainWindow window;
98
99   //Try to give the window a sensible default size
100   int width=QApplication::desktop()->width()*0.8;
101   int height=QApplication::desktop()->height()*0.9;
102   if (width> 1.5*height)
103     width=1.5*height;
104   window.resize(width,height);
105
106   window.show();
107
108   std::vector<std::string> sequence_filenames;
109   enum {P_NORMAL,P_SEQUENCE,P_WINDOW,P_LEVEL};
110   int parse_mode=P_NORMAL;
111   int n_image_loaded=0;
112   std::string win(""), lev("");
113
114   if (argc >1) {
115     for (int i = 1; i < argc; i++) {
116       std::string current = argv[i];
117       if (!current.compare(0,2,"--")) { //We are parsing an option
118         if (parse_mode == P_SEQUENCE) {//First finish the current sequence
119           window.LoadImages(sequence_filenames, vvImageReader::MERGEDWITHTIME);
120           sequence_filenames.clear();
121           parse_mode=P_NORMAL;
122         }
123         if (current=="--vf") {
124           if (!n_image_loaded) load_image_first_error();
125           window.AddField(argv[i+1],n_image_loaded-1);
126           i++; //skip vf name
127         } else if (current=="--overlay") {
128           if (!n_image_loaded) load_image_first_error();
129           window.AddOverlayImage(n_image_loaded-1,argv[i+1]);
130           i++; //skip overlay name
131         } else if (current=="--roi") {
132           if (!n_image_loaded) load_image_first_error();
133           window.AddROI(n_image_loaded-1,argv[i+1]);
134           i++; //skip roi name
135         } else if (current=="--fusion") {
136           if (!n_image_loaded) load_image_first_error();
137           window.AddFusionImage(n_image_loaded-1,argv[i+1]);
138           i++; //skip fusion name
139         } else if (current == "--sequence") {
140           n_image_loaded++; //count only one for the sequence
141           parse_mode=P_SEQUENCE;
142         } else if (current == "--window") {
143           parse_mode=P_WINDOW;
144         } else if (current == "--level") {
145           parse_mode=P_LEVEL;
146         } else if (current == "--log") {
147           std::string log_dir = QDir::tempPath().toStdString() + std::string("/vv-log");
148
149           if(itksys::SystemTools::FileExists(log_dir.c_str()) &&
150               !itksys::SystemTools::FileIsDirectory(log_dir.c_str())) {
151             std::cerr << "Error creating log directory, file exists and is not a directory." << std::endl;
152             exit(1);
153           } else if(!itksys::SystemTools::MakeDirectory(log_dir.c_str())) {
154             std::cerr << "Error creating log directory." << std::endl;
155             exit(1);
156           }
157
158           std::string log_file = log_dir + "/" + create_timed_string() + ".log";
159
160           itk::SmartPointer<itk::FileOutputWindow> itk_log = itk::FileOutputWindow::New();
161           itk_log->SetFileName(log_file.c_str());
162           itk_log->FlushOn();
163           itk_log->AppendOn();
164           itk::OutputWindow::SetInstance(itk_log);
165
166           vtkSmartPointer<vtkFileOutputWindow> vtk_log = vtkFileOutputWindow::New();
167           vtk_log->SetFileName(log_file.c_str());
168           vtk_log->FlushOn();
169           vtk_log->AppendOn();
170           vtkOutputWindow::SetInstance(vtk_log);
171         }
172       } else if (parse_mode == P_SEQUENCE) {
173         sequence_filenames.push_back(current);
174       } else if (parse_mode == P_WINDOW) {
175         win=current;
176         parse_mode=P_NORMAL;
177       } else if (parse_mode == P_LEVEL) {
178         lev=current;
179         parse_mode=P_NORMAL;
180       } else {
181         std::vector<std::string> image;
182         image.push_back(current);
183         window.LoadImages(image, vvImageReader::IMAGE);
184         n_image_loaded++;
185       }
186     }
187     if (parse_mode == P_SEQUENCE) { //Finish any current sequence
188       window.LoadImages(sequence_filenames, vvImageReader::MERGEDWITHTIME);
189       sequence_filenames.clear();
190       parse_mode=P_NORMAL;
191     }
192   }
193
194   if(win!="" && lev!="") {
195     window.SetWindowLevel(atof(win.c_str()), atof(lev.c_str()));
196     window.ApplyWindowLevelToAllImages();
197   }
198
199 #ifndef _WIN32
200   // restoring the locale, just to be clean...
201   setlocale(LC_NUMERIC, old_locale.c_str());
202 #endif
203
204   return app.exec();
205 }