]> Creatis software - clitk.git/blob - vv/vv.cxx
Fixed reset after sequence reading bug and put copy/pasted code in
[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 "vvReadState.h"
34 #include "vvToolsList.h"
35 #include "vvConfiguration.h"
36
37 #include <vtkFileOutputWindow.h>
38 #include <vtkSmartPointer.h>
39
40 #include <itkFileOutputWindow.h>
41 #include <itkSmartPointer.h>
42 #include <itksys/SystemTools.hxx>
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <errno.h>
47
48 typedef enum {O_BASE,O_OVERLAY,O_FUSION,O_VF,O_CONTOUR} OpenModeType;
49 typedef enum {P_NORMAL,P_SEQUENCE,P_WINDOW,P_LEVEL} ParseModeType;
50
51 void load_image_first_error()
52 {
53   std::cerr << "You need to load an image before adding an overlay!" << std::endl;
54   exit(1);
55 }
56
57 std::string create_timed_string()
58 {
59   time_t t;
60   time(&t);
61
62   struct tm* pt = localtime(&t);
63
64   const int size = 64;
65   char st[size];
66   strftime(st, size, "%Y%m%d-%H%M%S", pt);
67
68   return st;
69 }
70
71 void open_sequence(vvMainWindow &window,
72                    OpenModeType &open_mode,
73                    ParseModeType &parse_mode,
74                    std::vector<std::string> &sequence_filenames,
75                    int n_image_loaded)
76 {
77   const std::string open_mode_names[] = {"base", "overlay", "fusion", "vf", "contour"};
78   if(open_mode==O_BASE)
79     window.LoadImages(sequence_filenames, vvImageReader::MERGEDWITHTIME);
80   else if (open_mode==O_OVERLAY)
81     window.AddOverlayImage(n_image_loaded-1,sequence_filenames,vvImageReader::IMAGE);
82   else {
83     std::cerr << "Sequences are not managed for opening " << open_mode_names[open_mode] << std::endl;
84     exit(1);
85   }
86
87   // Reset
88   sequence_filenames.clear();
89   parse_mode=P_NORMAL;
90   open_mode=O_BASE;
91 }
92
93 //------------------------------------------------------------------------------
94 #ifdef _WIN32
95 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nShowCmd)
96 {
97   int argc = __argc;
98   char **argv = __argv;
99 #else
100 int main( int argc, char** argv )
101 {
102 #endif
103
104   CLITK_INIT;
105
106   QApplication app( argc, argv );
107   Q_INIT_RESOURCE(vvIcons);
108   
109   // 
110   // ATTENTION: Rômulo Pinho - 05/08/2011
111   // Forcing the locale of the application is necessary
112   // because QT initialization changes it to the locale
113   // of the language of the system. This can cause 
114   // inconsistencies when, e.g., reading float values
115   // from DICOM fields with gdcm, since the decimal
116   // point may be changed for a comma (as in French).
117   // In practice, functions such as scanf and its
118   // variations are directly affected.
119   // https://bugreports.qt.nokia.com//browse/QTBUG-15247?page=com.atlassian.jira.plugin.system.issuetabpanels%253Achangehistory-tabpanel
120   //
121 #ifndef _WIN32
122   std::string old_locale = setlocale(LC_NUMERIC, NULL);
123   setlocale(LC_NUMERIC, "POSIX");
124 #endif
125
126   vvMainWindow window;
127
128   //Try to give the window a sensible default size
129   int width=QApplication::desktop()->width()*0.8;
130   int height=QApplication::desktop()->height()*0.9;
131   if (width> 1.5*height)
132     width=1.5*height;
133   window.resize(width,height);
134
135   window.show();
136
137   std::vector<std::string> sequence_filenames;
138   ParseModeType parse_mode = P_NORMAL;
139   OpenModeType open_mode = O_BASE;
140   int n_image_loaded=0;
141   std::string win(""), lev("");
142
143         bool link_images = false;
144   if (argc >1) {
145     for (int i = 1; i < argc; i++) {
146       std::string current = argv[i];
147       if (!current.compare(0,2,"--")) { //We are parsing an option
148         if (parse_mode == P_SEQUENCE) {//First finish the current sequence
149           open_sequence(window, open_mode, parse_mode, sequence_filenames, n_image_loaded);
150         }
151         if ((current=="--help") || (current=="-h")) {
152           std::cout << "vv " << VV_VERSION << ", the 2D, 2D+t, 3D and 3D+t (or 4D) image viewer" << std::endl << std::endl
153                     << "Synopsis: vv file(s) [OPTIONS] file(s)" << std::endl << std::endl
154                     << "Open file(s) for visualization." << std::endl << std::endl
155                     << "OPTIONS may be:" << std::endl
156                     << "--help         \t Print command line help and exit." << std::endl
157                     << "--window number\t Gray scale window width for all images." << std::endl
158                     << "--level number \t Gray scale window level for all images." << std::endl
159                     << "--linkall      \t Link pan, zoom and spatial position of crosshair in images." << std::endl
160                     << "--log          \t Log output messages in vv-log directory." << std::endl
161                     << "--state file   \t Read display parameters from file." << std::endl
162                     << "--sequence file\t Read all file(s) until next option in a single temporal sequence." << std::endl
163                     << std::endl
164                     << "These last options must follow a file name since they overlay something on an image:" << std::endl
165                     << "--vf file      \t Overlay the vector field in file." << std::endl
166                     << "--overlay file \t Overlay the image in file with complementary colors." << std::endl
167                     << "--fusion file  \t Overlay the image in file with alpha blending and colormap." << std::endl
168                     //<< "--roi file     \t Overlay binary mask images. Option may be repeated on a single base image." << std::endl
169                     << "--contour file \t Overlay DICOM RT-STRUCT contours." << std::endl;
170           exit(0);
171         }
172         if (current=="--vf") {
173           if (!n_image_loaded) load_image_first_error();
174           open_mode = O_VF;
175         } else if (current=="--overlay") {
176           if (!n_image_loaded) load_image_first_error();
177           open_mode = O_OVERLAY;
178         } else if (current=="--contour") {
179           if (!n_image_loaded) load_image_first_error();
180           open_mode = O_CONTOUR;
181         } else if (current=="--fusion") {
182           if (!n_image_loaded) load_image_first_error();
183           open_mode = O_FUSION;
184         } else if (current == "--sequence") {
185           if(open_mode==O_BASE) n_image_loaded++; //count only one for the whole sequence
186           parse_mode=P_SEQUENCE;
187         } else if (current == "--window") {
188           parse_mode=P_WINDOW;
189         } else if (current == "--level") {
190           parse_mode=P_LEVEL;
191         } else if (current == "--linkall") {
192           link_images = true;
193         }
194         else if (current == "--log") {
195           std::string log_dir = QDir::tempPath().toStdString() + std::string("/vv-log");
196
197           if(itksys::SystemTools::FileExists(log_dir.c_str()) &&
198               !itksys::SystemTools::FileIsDirectory(log_dir.c_str())) {
199             std::cerr << "Error creating log directory, file exists and is not a directory." << std::endl;
200             exit(1);
201           } else if(!itksys::SystemTools::MakeDirectory(log_dir.c_str())) {
202             std::cerr << "Error creating log directory." << std::endl;
203             exit(1);
204           }
205
206           std::string log_file = log_dir + "/" + create_timed_string() + ".log";
207
208           itk::SmartPointer<itk::FileOutputWindow> itk_log = itk::FileOutputWindow::New();
209           itk_log->SetFileName(log_file.c_str());
210           itk_log->FlushOn();
211           itk_log->AppendOn();
212           itk::OutputWindow::SetInstance(itk_log);
213
214           vtkSmartPointer<vtkFileOutputWindow> vtk_log = vtkFileOutputWindow::New();
215           vtk_log->SetFileName(log_file.c_str());
216           vtk_log->FlushOn();
217           vtk_log->AppendOn();
218           vtkOutputWindow::SetInstance(vtk_log);
219         } else if (current == "--state") {
220           //window.ReadSavedStateFile(argv[i+1]);
221           vvReadState read_state;
222           read_state.Run(&window, argv[i+1]);
223           n_image_loaded += read_state.GetNumberOfImages();
224           i++;
225         }
226         
227       } else if (parse_mode == P_SEQUENCE) {
228         sequence_filenames.push_back(current);
229       } else if (parse_mode == P_WINDOW) {
230         win=current;
231         parse_mode=P_NORMAL;
232       } else if (parse_mode == P_LEVEL) {
233         lev=current;
234         parse_mode=P_NORMAL;
235       } else {
236         std::vector<std::string> image;
237         image.push_back(current);
238         if(open_mode==O_BASE) {
239           window.LoadImages(image, vvImageReader::IMAGE);
240           n_image_loaded++;
241         }
242         else if (open_mode==O_VF)
243           window.AddField(current.c_str(), n_image_loaded-1);
244         else if (open_mode==O_OVERLAY)
245           window.AddOverlayImage(n_image_loaded-1,image,vvImageReader::IMAGE);
246         else if (open_mode==O_CONTOUR)
247           window.AddDCStructContour(n_image_loaded-1,current.c_str());
248         else if (open_mode==O_FUSION)
249           window.AddFusionImage(n_image_loaded-1,current.c_str());
250         open_mode = O_BASE;
251       }
252     }
253     if (parse_mode == P_SEQUENCE) { //Finish any current sequence
254       open_sequence(window, open_mode, parse_mode, sequence_filenames, n_image_loaded);
255     }
256   }
257
258   if(win!="" && lev!="") {
259     window.SetWindowLevel(atof(win.c_str()), atof(lev.c_str()));
260     window.ApplyWindowLevelToAllImages();
261   }
262
263   if (link_images)
264     window.LinkAllImages();
265
266   int ret = app.exec();
267   
268 #ifndef _WIN32
269   // restoring the locale, just to be clean...
270   setlocale(LC_NUMERIC, old_locale.c_str());
271 #endif
272
273   return ret;
274 }