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