]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/OpenImageDialog.cxx
665e7a23c560a73e33e0af66eaf45140b71a5f08
[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                                 erreur = dlerror ();
79                                 std::cout<<erreur<<"getImageDataDialog "<<getImageDataDialog<<std::endl;                        
80                                 }else{
81                                         img = (*getImageDataDialog)();
82                                         gimmickfound = true;
83                                         
84                                 }
85                                 /*dlclose returns 0 on success, non-zero on error*/
86                                 closingerror = dlclose(gimmickhandle);
87                                 if(closingerror!=0){
88                                         std::cout<<closingerror<<"error closing handle  "<<gimmickhandle<<std::endl;  
89                                 }else{
90                                         std::cout<<"handle released..."<<std::endl;  
91                                 }
92                         }
93
94                 #endif
95                 }
96                 
97                 if(!gimmickfound || !usegimmick){
98                         //int i, j, k, extint[6];
99                         //double spacing[3];
100                         //unsigned short* ptr1, *ptr2;
101                         wxFileDialog* FD =
102                           new wxFileDialog( 0, _T("Read Image"),wxEmptyString,wxEmptyString, _T("*.mhd"));
103
104                         if (FD->ShowModal()==wxID_OK)
105                         {
106                                 std::string  path= crea::wx2std(FD->GetPath());
107                                 vtkMetaImageReader* reader =  vtkMetaImageReader::New();
108                                 reader->SetFileName(path.c_str());
109                                 reader->Update();
110                                 _filename = path;
111                                 img = vtkImageData::New();
112                                 img->DeepCopy(reader->GetOutput());
113                                 //reader->GetOutput()->Delete();
114                                 reader->Delete();
115                                 /*std::cout<<"image reference count "<<img->GetReferenceCount()<<std::endl;
116                                 reader->GetOutput()->GetSpacing(spacing);
117                                 img->SetSpacing(spacing);
118                                 reader->GetOutput()->GetExtent(extint);
119                                 img->SetExtent(extint);
120                                 img->SetScalarType(reader->GetOutput()->GetScalarType());                               
121                                 img->AllocateScalars();
122                                 std::cout<<"image reference count "<<img->GetReferenceCount()<<std::endl;
123                                 for (i=extint[0];i <= extint[1];i++){
124                                         for (j=extint[2];j<=extint[3];j++){
125                                                 for (k=extint[4];k<=extint[5];k++){
126                                                         
127                                                                 ptr1=(unsigned short*) reader->GetOutput()->GetScalarPointer(i,j,k); 
128                                                                 ptr2=(unsigned short*)img->GetScalarPointer(i,j,k);
129                                                                 *ptr2 = *ptr1;
130                                                 }
131                                         }
132                                 }*/
133                                 
134                                 /*std::cout<<"image reference count "<<img->GetReferenceCount()<<std::endl;*/
135                         }
136                         //delete FD;
137                 }
138         }
139
140         //-------------------------------------------------------------------
141         OpenImageDialog::~OpenImageDialog()
142         {
143                 img = NULL;
144         }
145
146         vtkImageData* OpenImageDialog::getImageData(){          
147                 return img;
148         }
149         std::string OpenImageDialog::getFilename(){
150                 return _filename;
151         }
152 }