]> Creatis software - clitk.git/blob - vv/vv.cxx
Changed entry point of application on Windows to remove background console
[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 <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 #ifdef _WIN32
65 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nShowCmd)
66 {
67   int argc = __argc;
68   char **argv = __argv;
69 #else
70 int main( int argc, char** argv )
71 {
72 #endif
73
74   CLITK_INIT;
75
76   QApplication app( argc, argv );
77   Q_INIT_RESOURCE(vvIcons);
78   //QPixmap pixmap(":/splashscreen.PNG");
79   QSplashScreen *splash = new QSplashScreen(QPixmap(QString::fromUtf8(":/new/prefix1/splashscreen.PNG")));
80   /*splash->showMessage("VV 1.0 developped by Léon Bérard c`ancer center http://www.centreleonberard.fr and CREATIS-LRMN http://www.creatis.insa-lyon.fr",(Qt::AlignRight | Qt::AlignBottom));*/
81   //  splash->show();
82   QTimer::singleShot(2000, splash, SLOT(close()));
83   while (!splash->isHidden())
84     app.processEvents();
85
86   vvMainWindow window;
87
88   //Try to give the window a sensible default size
89   int width=QApplication::desktop()->width()*0.8;
90   int height=QApplication::desktop()->height()*0.9;
91   if (width> 1.5*height)
92     width=1.5*height;
93   window.resize(width,height);
94
95   window.show();
96
97   std::vector<std::string> sequence_filenames;
98   enum {P_NORMAL,P_SEQUENCE,P_WINDOW,P_LEVEL};
99   int parse_mode=P_NORMAL;
100   int n_image_loaded=0;
101   std::string win(""), lev("");
102
103   if (argc >1) {
104     for (int i = 1; i < argc; i++) {
105       std::string current = argv[i];
106       if (!current.compare(0,2,"--")) { //We are parsing an option
107         if (parse_mode == P_SEQUENCE) {//First finish the current sequence
108           window.LoadImages(sequence_filenames, vvImageReader::MERGEDWITHTIME);
109           sequence_filenames.clear();
110           parse_mode=P_NORMAL;
111         }
112         if (current=="--vf") {
113           if (!n_image_loaded) load_image_first_error();
114           window.AddField(argv[i+1],n_image_loaded-1);
115           i++; //skip vf name
116         } else if (current=="--overlay") {
117           if (!n_image_loaded) load_image_first_error();
118           window.AddOverlayImage(n_image_loaded-1,argv[i+1]);
119           i++; //skip overlay name
120         } else if (current=="--roi") {
121           if (!n_image_loaded) load_image_first_error();
122           window.AddROI(n_image_loaded-1,argv[i+1]);
123           i++; //skip roi name
124         } else if (current=="--fusion") {
125           if (!n_image_loaded) load_image_first_error();
126           window.AddFusionImage(n_image_loaded-1,argv[i+1]);
127           i++; //skip fusion name
128         } else if (current == "--sequence") {
129           n_image_loaded++; //count only one for the sequence
130           parse_mode=P_SEQUENCE;
131         } else if (current == "--window") {
132           parse_mode=P_WINDOW;
133         } else if (current == "--level") {
134           parse_mode=P_LEVEL;
135         } else if (current == "--log") {
136           std::string log_dir = QDir::tempPath().toStdString() + std::string("/vv-log");
137
138           if(itksys::SystemTools::FileExists(log_dir.c_str()) &&
139               !itksys::SystemTools::FileIsDirectory(log_dir.c_str())) {
140             std::cerr << "Error creating log directory, file exists and is not a directory." << std::endl;
141             exit(1);
142           } else if(!itksys::SystemTools::MakeDirectory(log_dir.c_str())) {
143             std::cerr << "Error creating log directory." << std::endl;
144             exit(1);
145           }
146
147           std::string log_file = log_dir + "/" + create_timed_string() + ".log";
148
149           itk::SmartPointer<itk::FileOutputWindow> itk_log = itk::FileOutputWindow::New();
150           itk_log->SetFileName(log_file.c_str());
151           itk_log->FlushOn();
152           itk_log->AppendOn();
153           itk::OutputWindow::SetInstance(itk_log);
154
155           vtkSmartPointer<vtkFileOutputWindow> vtk_log = vtkFileOutputWindow::New();
156           vtk_log->SetFileName(log_file.c_str());
157           vtk_log->FlushOn();
158           vtk_log->AppendOn();
159           vtkOutputWindow::SetInstance(vtk_log);
160         }
161       } else if (parse_mode == P_SEQUENCE) {
162         sequence_filenames.push_back(current);
163       } else if (parse_mode == P_WINDOW) {
164         win=current;
165         parse_mode=P_NORMAL;
166       } else if (parse_mode == P_LEVEL) {
167         lev=current;
168         parse_mode=P_NORMAL;
169       } else {
170         std::vector<std::string> image;
171         image.push_back(current);
172         window.LoadImages(image, vvImageReader::IMAGE);
173         n_image_loaded++;
174       }
175     }
176     if (parse_mode == P_SEQUENCE) { //Finish any current sequence
177       window.LoadImages(sequence_filenames, vvImageReader::MERGEDWITHTIME);
178       sequence_filenames.clear();
179       parse_mode=P_NORMAL;
180     }
181   }
182
183   if(win!="" && lev!="") {
184     window.WindowLevelChanged(atof(win.c_str()), atof(lev.c_str()), 6, 0);
185     window.ApplyWindowLevelToAllImages();
186   }
187
188   return app.exec();
189 }