]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/OpenImageDialog.cxx
#2844 creaMaracasVisu Feature New Normal - ManualPaint_model box, Refresh TransferF...
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / OpenImageDialog.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26
27 #include "OpenImageDialog.h"
28 #include "creaWx.h"
29
30 #if defined(__GNUC__)
31 // GCC compiler
32 // dlopen, dlsym, etc.
33   #include <dlfcn.h>
34 #endif
35
36 typedef vtkImageData* (*GETIMAGEDATADIALOG)();
37
38 namespace creaMaracasVisuKernel{
39         OpenImageDialog::OpenImageDialog(bool usegimmick)
40         {
41                 img = NULL;             
42                 _filename = "";
43                 bool gimmickfound = false;
44
45                 if(usegimmick){
46                 #ifdef WIN32
47                         HINSTANCE gimmickhandle;
48                         gimmickhandle = LoadLibrary(TEXT("creaImageIO.dll"));
49                         std::cout<<"imagehandle "<<gimmickhandle<<std::endl;
50                         if(gimmickhandle!=NULL){
51
52                                 GETIMAGEDATADIALOG getImageDataDialog;
53
54                                 getImageDataDialog = (GETIMAGEDATADIALOG)GetProcAddress(gimmickhandle, "getImageDataDialog");
55
56                                 std::cout<<"imagedatadialog "<<getImageDataDialog<<std::endl;
57
58                                 if(getImageDataDialog!=NULL){
59                                         img = getImageDataDialog();
60                                         gimmickfound = true;
61                                 }
62                                 if(!FreeLibrary(gimmickhandle)){
63                                         std::cout<<"Error while releasing the handle creaImageIO2.dll"<<std::endl;
64                                 }
65                         }
66                 #else
67                         void * gimmickhandle;   
68                         GETIMAGEDATADIALOG getImageDataDialog;
69                         char * erreur;
70                         int closingerror;
71                         gimmickhandle = dlopen ("libcreaImageIO.so", RTLD_LAZY);                        
72                         if (! gimmickhandle) {
73                                 erreur = dlerror ();
74                                 std::cout<<erreur<<"imagehandle "<<gimmickhandle<<std::endl;                
75                         }else{
76                                 getImageDataDialog = (GETIMAGEDATADIALOG) dlsym (gimmickhandle, "getImageDataDialog");
77                                 if(!getImageDataDialog)
78                                 {
79                                         erreur = dlerror ();
80                                         std::cout<<erreur<<"getImageDataDialog "<<getImageDataDialog<<std::endl;                        
81                                 }else{
82                                         img = (*getImageDataDialog)();
83                                         gimmickfound = true;
84                                         
85                                 } // if getImageDataDialog
86                                 /*dlclose returns 0 on success, non-zero on error*/
87                                 closingerror = dlclose(gimmickhandle);
88                                 if(closingerror!=0){
89                                         std::cout<<closingerror<<"error closing handle  "<<gimmickhandle<<std::endl;  
90                                 }else{
91                                         std::cout<<"handle released..."<<std::endl;  
92                                 } // if closingerror
93                         } // if gimmickhandle
94
95                 #endif
96                 }
97                 
98                 if(!gimmickfound || !usegimmick){
99                         //int i, j, k, extint[6];
100                         //double spacing[3];
101                         //unsigned short* ptr1, *ptr2;
102                         wxFileDialog* FD =
103                           new wxFileDialog( 0, _T("Read Image"),wxEmptyString,wxEmptyString, _T("*.mhd"));
104
105                         if (FD->ShowModal()==wxID_OK)
106                         {
107                                 std::string  path= crea::wx2std(FD->GetPath());
108                                 vtkMetaImageReader* reader =  vtkMetaImageReader::New();
109                                 reader->SetFileName(path.c_str());
110                                 reader->Update();
111                                 _filename = path;
112                                 img = vtkImageData::New();
113                                 img->DeepCopy(reader->GetOutput());
114                                 //reader->GetOutput()->Delete();
115                                 reader->Delete();
116                                 /*std::cout<<"image reference count "<<img->GetReferenceCount()<<std::endl;
117                                 reader->GetOutput()->GetSpacing(spacing);
118                                 img->SetSpacing(spacing);
119                                 reader->GetOutput()->GetExtent(extint);
120                                 img->SetExtent(extint);
121                                 img->SetScalarType(reader->GetOutput()->GetScalarType());                               
122                                 img->AllocateScalars();
123                                 std::cout<<"image reference count "<<img->GetReferenceCount()<<std::endl;
124                                 for (i=extint[0];i <= extint[1];i++){
125                                         for (j=extint[2];j<=extint[3];j++){
126                                                 for (k=extint[4];k<=extint[5];k++){
127                                                         
128                                                                 ptr1=(unsigned short*) reader->GetOutput()->GetScalarPointer(i,j,k); 
129                                                                 ptr2=(unsigned short*)img->GetScalarPointer(i,j,k);
130                                                                 *ptr2 = *ptr1;
131                                                 }
132                                         }
133                                 }*/
134                                 
135                                 /*std::cout<<"image reference count "<<img->GetReferenceCount()<<std::endl;*/
136                         }
137                         //delete FD;
138                 }
139         }
140
141         //-------------------------------------------------------------------
142         OpenImageDialog::~OpenImageDialog()
143         {
144                 img = NULL;
145         }
146
147         vtkImageData* OpenImageDialog::getImageData(){          
148                 return img;
149         }
150         std::string OpenImageDialog::getFilename(){
151                 return _filename;
152         }
153 }