creaImageIO_lib
creaImageIOWxDumpPanel.cpp
Go to the documentation of this file.
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  DumpText = new wxTextCtrl( this, wxID_ANY,_T(""), wxPoint(5,30), wxSize(520,510), wxTE_READONLY| wxMac | wxTE_MULTILINE | wxTE_RICH );
65  Layout();
66  Print();
67  }
68 
69  // DTor
71 
76  {
77  std::stringstream os;
78  std::string result = "";
79  if ( !filename.empty()) // ====== Deal with a single file ======
80  {
81 
82 #if defined(USE_GDCM)
83  GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
84  f->SetLoadMode(GDCM_NAME_SPACE::LD_ALL);
85  f->SetFileName( filename );
86  f->SetMaxSizeLoadEntry(0xffff);
87  f->Load();
88  GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
89  f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ |GDCM_NAME_SPACE::LD_NOSHADOW);
90  fh->SetPrintLevel( 0 );
91  fh->Print(os);
92 
93  std::string line;
94  while(std::getline(os, line))
95  {
96  result +=clean(line.c_str());
97  result += "\n";
98  }
99 
100 
101 #endif
102 #if defined(USE_GDCM2)
103  gdcm::Reader reader;
104  reader.SetFileName( filename.c_str() );
105  if (reader.Read())
106  {
107  gdcm::Printer printer;
108  printer.SetFile ( reader.GetFile() );
109  printer.SetColor( 0 );
110  printer.Print( os );
111  result = os.str();
112  }
113 
114 
115 
116  //const gdcm::DataSet& ds = reader.GetFile().GetDataSet();
117  // // (5400,0100) SQ (Sequence with undefined length #=1) # u/l, 1 WaveformSequence
118  // gdcm::Tag twsq(0x5400,0x0100);
119  // bool btest = ds.FindDataElement( twsq) ;
120  //const gdcm::DataElement &wsq = ds.GetDataElement( twsq );
121  // gdcm::SmartPointer<gdcm::SequenceOfItems> sqi = wsq.GetValueAsSQ();
122  // int num = sqi->GetNumberOfItems();
123  //
124  // const gdcm::Item & item = sqi->GetItem(1); // Item start at #1
125  // const gdcm::DataSet& nestedds = item.GetNestedDataSet();
126 
127  // // (5400,1004) US 16 # 2, 1 WaveformBitsAllocated
128  // gdcm::Tag twba(0x5400,0x1004);
129  // if( !nestedds.FindDataElement( twba ) )
130  // {
131  //
132  // }
133  // const gdcm::DataElement &wba= nestedds.GetDataElement( twba );
134 
135  // //std::cout << wba << std::endl;
136  // // (5400,1006) CS [SS] # 2, 1 WaveformSampleInterpretation
137  // // (5400,1010) OW 00ba\0030\ff76\ff8b\00a2\ffd3\ffae\ff50\0062\00c4\011e\00c2\00ba... # 57600, 1 WaveformData
138  // gdcm::Tag twd(0x5400,0x1010);
139  // if( !nestedds.FindDataElement( twd ) )
140  // {
141 
142  // }
143  // const gdcm::DataElement &wd = nestedds.GetDataElement( twd );
144  // const gdcm::ByteValue *bv = wd.GetByteValue();
145  // size_t len = bv->GetLength();
146  // int16_t *p = (int16_t*)bv;
147  //
148  // size_t npts = len / 2;
149  // //npts = 10; // DEBUG !
150  // for(size_t i = 0; i < npts; ++i )
151  // {
152  // float x;
153  // x = (float)p[i];
154  // }
161 
164 
172 
173 
174 
175  // gdcm::DataElement data = reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(0x5400,0x0100));
176  // std::string seq_old_begin = "(fffe,e000) na (Item with undefined length)";
177  // std::string seq_new_begin = "<SEQ>";
178  // boost::algorithm::replace_all(result, seq_old_begin, seq_new_begin);
179  // std::string seq_old_end = "(fffe,e00d)";
180  // std::string seq_new_end = "<\\SEQ>";
181  // boost::algorithm::replace_all(result, seq_old_end, seq_new_end);
182  //seq_old_end = "(fffe,e0dd)"; boost::algorithm::replace_all(result, seq_old_end, "\n");
183 #endif
184  DumpText->SetValue(crea::std2wx(result));
185 
186  }
187  }
188 
189 
190  const std::string WxDumpPanel::clean(const std::string &i_line)
191  {
192 
193  if (i_line.substr(4,1) == "|")
194  {
195  std::string tag;
196  std::string line;
197  std::string signification;
198  std::string value;
199  std::string resultat;
200 
201  tag = "(" + i_line.substr(0,9) + ")";
202  line = i_line.substr(14,i_line.size()-10);
203  int pos1 = (int)line.find_first_of("[");
204  int pos2 = (int)line.find_first_of("]");
205  signification = line.substr(pos1+1, pos2-pos1-1);
206  line = line.substr(pos2+1);
207  pos1 = (int)line.find_first_of("[");
208  pos2 = (int)line.find_first_of("]");
209  value = line.substr(pos1+1, pos2-pos1-1);
210  resultat = tag + " " + signification + ": " +value;
211  return resultat;
212  }
213  else
214  {
215  return i_line;
216  }
217  }
218 
222  void WxDumpPanel::SaveInfos(wxCommandEvent& event)
223  {
224  wxFileDialog* FD = new wxFileDialog( 0,_T("Select file"), _T(""), _T(""),
225  crea::std2wx("*.txt"), wxOPEN, wxDefaultPosition);
226  if (FD->ShowModal()==wxID_OK)
227  {
228  wxBusyCursor busy;
229  std::ofstream ofs(crea::wx2std(FD->GetPath()).c_str());
230  ofs.clear();
231  ofs << crea::wx2std(DumpText->GetValue());;
232  ofs.close();
233  }
234  Close();
235  }
236 
239 BEGIN_EVENT_TABLE(WxDumpPanel, wxDialog)
240  EVT_TOOL(DUMP_SAVE_ID, WxDumpPanel::SaveInfos)
241 END_EVENT_TABLE()
242 }
243