]> Creatis software - creaImageIO.git/blob - src/creaImageIOWxDumpPanel.cpp
Clean code
[creaImageIO.git] / src / creaImageIOWxDumpPanel.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28
29 #include <creaImageIOWxDumpPanel.h>
30
31 #include <creaImageIOGimmick.h>
32 #include <boost/algorithm/string.hpp>
33 #if defined(USE_GDCM)
34 #include <gdcmGlobal.h>
35 #include <gdcmDictSet.h>
36 #include "gdcmFile.h"
37 #include "gdcmDocument.h"
38 #include "gdcmFileHelper.h"
39 #endif
40
41 #if defined(USE_GDCM2)
42 #include "gdcmGlobal.h"
43 #include "gdcmFile.h"
44 #include "gdcmDictPrinter.h"
45 #include "gdcmPrinter.h"
46 #include "gdcmReader.h"
47 #include "gdcmSmartPointer.h"
48 #include "gdcmSequenceOfItems.h"
49 #endif
50 #include "icons/save.xpm"
51
52 namespace creaImageIO
53 {
54         // CTor
55    WxDumpPanel::WxDumpPanel(wxWindow *parent,  std::string i_filename)
56     : wxDialog(parent, -1,_T("DICOM TAGS"), wxDefaultPosition, wxSize(550,580)), filename(i_filename)
57    {
58         int size = 16;
59         mIcon = new wxImageList(size,size,true);
60         mIcon->Add(wxBitmap(wxBitmap(wxIcon(save_xpm)).ConvertToImage().Rescale(size, size)));
61         wxToolBar *mToolBar = new wxToolBar(this,-1,wxDefaultPosition,wxDefaultSize);
62         mToolBar->AddTool( DUMP_SAVE_ID,_T("Save"), mIcon->GetBitmap(0), _T("Save Dicom Tags in text file"));
63         mToolBar->Realize();
64
65 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
66 #if wxMAJOR_VERSION <= 2
67         DumpText = new wxTextCtrl( this, wxID_ANY,_T(""), wxPoint(5,30), wxSize(520,510), wxTE_READONLY| wxMac | wxTE_MULTILINE | wxTE_RICH );
68 #else
69         DumpText = new wxTextCtrl( this, wxID_ANY,_T(""), wxPoint(5,30), wxSize(520,510), wxTE_READONLY | wxTE_MULTILINE | wxTE_RICH );
70 #endif
71
72         Layout(); 
73         Print();
74         }
75
76         // DTor
77         WxDumpPanel::~WxDumpPanel(){}
78
79         ///////////////////////////////////////////////////
80         /// Display in a control Text all dicom tags
81         ///////////////////////////////////////////////////
82         void WxDumpPanel::Print()
83         {
84            std::stringstream os;
85            std::string result = "";
86            if ( !filename.empty()) // ====== Deal with a single file ======
87            {
88
89 #if defined(USE_GDCM)
90                    GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
91                    f->SetLoadMode(GDCM_NAME_SPACE::LD_ALL);
92                    f->SetFileName( filename );
93                    f->SetMaxSizeLoadEntry(0xffff);
94                    f->Load();
95                    GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
96                       f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ |GDCM_NAME_SPACE::LD_NOSHADOW); 
97                    fh->SetPrintLevel( 0 );
98                    fh->Print(os);
99                  
100                    std::string line;
101                    while(std::getline(os, line))
102                    {
103                           result +=clean(line.c_str());
104                           result += "\n";
105                    }
106                    
107
108 #endif
109 #if defined(USE_GDCM2)
110                   gdcm::Reader reader;
111                   reader.SetFileName( filename.c_str() );
112                   if (reader.Read())
113                   {
114                           gdcm::Printer printer;
115                           printer.SetFile ( reader.GetFile() );
116                           printer.SetColor( 0 );
117                           printer.Print( os );
118                           result = os.str();
119                   }
120
121
122
123         //const gdcm::DataSet& ds = reader.GetFile().GetDataSet();
124  // // (5400,0100) SQ (Sequence with undefined length #=1)     # u/l, 1 WaveformSequence
125         // gdcm::Tag twsq(0x5400,0x0100);
126         // bool btest = ds.FindDataElement( twsq) ;
127         //const gdcm::DataElement &wsq = ds.GetDataElement( twsq );
128         // gdcm::SmartPointer<gdcm::SequenceOfItems> sqi = wsq.GetValueAsSQ();
129         // int num = sqi->GetNumberOfItems();
130  // 
131  // const gdcm::Item & item = sqi->GetItem(1); // Item start at #1
132  // const gdcm::DataSet& nestedds = item.GetNestedDataSet();
133
134  // // (5400,1004) US 16                                       #   2, 1 WaveformBitsAllocated
135  // gdcm::Tag twba(0x5400,0x1004);
136  // if( !nestedds.FindDataElement( twba ) )
137  //   {
138  // 
139  //   }
140  // const gdcm::DataElement &wba= nestedds.GetDataElement( twba );
141
142  // //std::cout << wba << std::endl;
143  // //  (5400,1006) CS [SS]                                     #   2, 1 WaveformSampleInterpretation
144  // // (5400,1010) OW 00ba\0030\ff76\ff8b\00a2\ffd3\ffae\ff50\0062\00c4\011e\00c2\00ba... # 57600, 1 WaveformData
145  // gdcm::Tag twd(0x5400,0x1010);
146  // if( !nestedds.FindDataElement( twd ) )
147  //   {
148
149  //   }
150  // const gdcm::DataElement &wd = nestedds.GetDataElement( twd );
151  // const gdcm::ByteValue *bv = wd.GetByteValue();
152  // size_t len = bv->GetLength();
153  // int16_t *p = (int16_t*)bv;
154         // 
155  // size_t npts = len / 2;
156  // //npts = 10; // DEBUG !
157  // for(size_t i = 0; i < npts; ++i )
158  //   {
159  //   float x;
160  //   x = (float)p[i];
161  //   }
162  //// 
163         //// gdcm::SmartPointer<gdcm::SequenceOfItems> sqi = wsq.GetValueAsSQ();
164         ////if( !sqi || !sqi->GetNumberOfItems() )
165  ////   {
166  ////   
167  ////   }
168
169  //// const gdcm::Item & item = sqi->GetItem(1); // Item start at #1
170  //// const gdcm::DataSet& nestedds = item.GetNestedDataSet();
171
172  //// // (5400,1004) US 16                                       #   2, 1 WaveformBitsAllocated
173  //// gdcm::Tag twba(0x5400,0x1004);
174  //// if( !nestedds.FindDataElement( twba ) )
175  ////   {
176  ////   return 0;
177  ////   }
178  //// const gdcm::DataElement &wba= nestedds.GetDataElement( twba );
179
180
181
182         //        gdcm::DataElement data = reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(0x5400,0x0100));
183         //        std::string seq_old_begin = "(fffe,e000) na (Item with undefined length)";
184         //        std::string seq_new_begin = "<SEQ>";
185         //        boost::algorithm::replace_all(result, seq_old_begin, seq_new_begin);
186         //        std::string seq_old_end = "(fffe,e00d)";
187         //        std::string seq_new_end = "<\\SEQ>";
188         //        boost::algorithm::replace_all(result, seq_old_end, seq_new_end);
189                   //seq_old_end = "(fffe,e0dd)";                  boost::algorithm::replace_all(result, seq_old_end, "\n");
190 #endif
191                   DumpText->SetValue(crea::std2wx(result));                
192
193                 }
194         }
195
196
197         const std::string WxDumpPanel::clean(const std::string &i_line)
198         {
199                 
200                   if (i_line.substr(4,1) == "|")
201                            {
202                                    std::string tag;
203                                    std::string line;
204                                    std::string signification;
205                                    std::string value;
206                                    std::string resultat;
207
208                                    tag = "(" + i_line.substr(0,9) + ")";
209                                    line =  i_line.substr(14,i_line.size()-10);
210                                    int pos1 = (int)line.find_first_of("[");
211                                    int pos2 = (int)line.find_first_of("]");
212                                    signification = line.substr(pos1+1, pos2-pos1-1);
213                                    line = line.substr(pos2+1);
214                                     pos1 = (int)line.find_first_of("[");
215                                     pos2 = (int)line.find_first_of("]");
216                                    value = line.substr(pos1+1, pos2-pos1-1);
217                                    resultat = tag + " " + signification + ": " +value;
218                                   return resultat;
219                            }
220                    else
221                    {
222                            return i_line;
223                    }
224         }
225
226 ///////////////////////////////////////////////////     
227 /// wxEvent to save Dicom Tags in a text file    //
228 ///////////////////////////////////////////////////
229         void WxDumpPanel::SaveInfos(wxCommandEvent& event)
230         {
231 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
232 #if wxMAJOR_VERSION <= 2
233         wxFileDialog* FD = new wxFileDialog( 0,_T("Select file"), _T(""), _T(""),
234                                                crea::std2wx("*.txt"), wxOPEN, wxDefaultPosition);
235 #else
236         wxFileDialog* FD = new wxFileDialog( 0,_T("Select file"), _T(""), _T(""),
237                                                crea::std2wx("*.txt"), wxFD_OPEN, wxDefaultPosition);
238 #endif
239         if (FD->ShowModal()==wxID_OK)
240                 {
241                         wxBusyCursor busy;
242                         std::ofstream ofs(crea::wx2std(FD->GetPath()).c_str());
243                         ofs.clear();
244                         ofs << crea::wx2std(DumpText->GetValue());;
245                         ofs.close();
246                 }
247                 Close();
248         }
249
250 ////////////////////////////////////////////////////////////
251 ////////////////////////////////////////////////////////////
252 BEGIN_EVENT_TABLE(WxDumpPanel, wxDialog)
253     EVT_TOOL(DUMP_SAVE_ID, WxDumpPanel::SaveInfos)
254 END_EVENT_TABLE()
255 }
256