]> Creatis software - clitk.git/blob - vv/vv.cxx
Moved vvImageReader and vvImageWriter to clitkCommon for use in tools. Removed vvCons...
[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 <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 <vtkFileOutputWindow.h>
34 #include <vtkSmartPointer.h>
35 #include <itkFileOutputWindow.h>
36 #include <itkSmartPointer.h>
37 #include <itksys/SystemTools.hxx>
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include  <errno.h>
42
43 void load_image_first_error()
44 {
45   std::cerr << "You need to load an image before adding an overlay!" << std::endl;
46   exit(1);
47 }
48
49 std::string create_timed_string()
50 {
51   time_t t;
52   time(&t);
53
54   struct tm* pt = localtime(&t);
55
56   const int size = 64;
57   char st[size];
58   strftime(st, size, "%Y%m%d-%H%M%S", pt);
59
60   return st;
61 }
62
63 //------------------------------------------------------------------------------
64 int main( int argc, char** argv )
65 {
66   CLITK_INIT;
67
68   QApplication app( argc, argv );
69   Q_INIT_RESOURCE(vvIcons);
70   //QPixmap pixmap(":/splashscreen.PNG");
71   QSplashScreen *splash = new QSplashScreen(QPixmap(QString::fromUtf8(":/new/prefix1/splashscreen.PNG")));
72   /*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));*/
73   //  splash->show();
74   QTimer::singleShot(2000, splash, SLOT(close()));
75   while (!splash->isHidden())
76     app.processEvents();
77
78   vvMainWindow window;
79
80   //Try to give the window a sensible default size
81   int width=QApplication::desktop()->width()*0.8;
82   int height=QApplication::desktop()->height()*0.9;
83   if (width> 1.5*height)
84     width=1.5*height;
85   window.resize(width,height);
86
87   window.show();
88
89   std::vector<std::string> sequence_filenames;
90   enum {P_NORMAL,P_SEQUENCE,P_WINDOW,P_LEVEL};
91   int parse_mode=P_NORMAL;
92   int n_image_loaded=0;
93   std::string win(""), lev("");
94
95   if (argc >1) {
96     for (int i = 1; i < argc; i++) {
97       std::string current = argv[i];
98       if (!current.compare(0,2,"--")) { //We are parsing an option
99         if (parse_mode == P_SEQUENCE) {//First finish the current sequence
100           window.LoadImages(sequence_filenames, vvImageReader::MERGEDWITHTIME);
101           sequence_filenames.clear();
102           parse_mode=P_NORMAL;
103         }
104         if (current=="--vf") {
105           if (!n_image_loaded) load_image_first_error();
106           window.AddField(argv[i+1],n_image_loaded-1);
107           i++; //skip vf name
108         } else if (current=="--overlay") {
109           if (!n_image_loaded) load_image_first_error();
110           window.AddOverlayImage(n_image_loaded-1,argv[i+1]);
111           i++; //skip overlay name
112         } else if (current=="--roi") {
113           if (!n_image_loaded) load_image_first_error();
114           window.AddROI(n_image_loaded-1,argv[i+1]);
115           i++; //skip roi name
116         } else if (current=="--fusion") {
117           if (!n_image_loaded) load_image_first_error();
118           window.AddFusionImage(n_image_loaded-1,argv[i+1]);
119           i++; //skip fusion name
120         } else if (current == "--sequence") {
121           n_image_loaded++; //count only one for the sequence
122           parse_mode=P_SEQUENCE;
123         } else if (current == "--window") {
124           parse_mode=P_WINDOW;
125         } else if (current == "--level") {
126           parse_mode=P_LEVEL;
127         } else if (current == "--log") {
128           std::string log_dir = QDir::tempPath().toStdString() + std::string("/vv-log");
129
130           if(itksys::SystemTools::FileExists(log_dir.c_str()) &&
131               !itksys::SystemTools::FileIsDirectory(log_dir.c_str())) {
132             itkGenericExceptionMacro(<< "Error creating log directory, file exists and is not a directory.");
133           } else if(!itksys::SystemTools::MakeDirectory(log_dir.c_str())) {
134             itkGenericExceptionMacro(<< "Error creating log directory.");
135           }
136
137           std::string log_file = log_dir + "/" + create_timed_string() + ".log";
138
139           itk::SmartPointer<itk::FileOutputWindow> itk_log = itk::FileOutputWindow::New();
140           itk_log->SetFileName(log_file.c_str());
141           itk_log->FlushOn();
142           itk_log->AppendOn();
143           itk::OutputWindow::SetInstance(itk_log);
144
145           vtkSmartPointer<vtkFileOutputWindow> vtk_log = vtkFileOutputWindow::New();
146           vtk_log->SetFileName(log_file.c_str());
147           vtk_log->FlushOn();
148           vtk_log->AppendOn();
149           vtkOutputWindow::SetInstance(vtk_log);
150         }
151       } else if (parse_mode == P_SEQUENCE) {
152         sequence_filenames.push_back(current);
153       } else if (parse_mode == P_WINDOW) {
154         win=current;
155         parse_mode=P_NORMAL;
156       } else if (parse_mode == P_LEVEL) {
157         lev=current;
158         parse_mode=P_NORMAL;
159       } else {
160         std::vector<std::string> image;
161         image.push_back(current);
162         window.LoadImages(image, vvImageReader::IMAGE);
163         n_image_loaded++;
164       }
165     }
166     if (parse_mode == P_SEQUENCE) { //Finish any current sequence
167       window.LoadImages(sequence_filenames, vvImageReader::MERGEDWITHTIME);
168       sequence_filenames.clear();
169       parse_mode=P_NORMAL;
170     }
171   }
172
173   if(win!="" && lev!="") {
174     window.WindowLevelChanged(atof(win.c_str()), atof(lev.c_str()), 6, 0);
175     window.ApplyWindowLevelToAllImages();
176   }
177
178   return app.exec();
179 }