]> Creatis software - creaImageIO.git/blob - src/creaImageIOWxDumpPanel.cpp
a820960acf2e9c62b063cfa3523d6b871238d915
[creaImageIO.git] / src / creaImageIOWxDumpPanel.cpp
1 #include <creaImageIOWxDumpPanel.h>
2 #include <creaImageIOSystem.h>
3 #include <creaImageIOGimmick.h>
4 #include <boost/algorithm/string.hpp>
5 #if defined(USE_GDCM)
6 #include <gdcmGlobal.h>
7 #include <gdcmDictSet.h>
8 #include "gdcmFile.h"
9 #include "gdcmDocument.h"
10 #include "gdcmFileHelper.h"
11 #endif
12
13 #if defined(USE_GDCM2)
14 #include "gdcmGlobal.h"
15 #include "gdcmFile.h"
16 #include "gdcmDictPrinter.h"
17 #include "gdcmPrinter.h"
18 #include "gdcmReader.h"
19 #include "gdcmSmartPointer.h"
20 #include "gdcmSequenceOfItems.h"
21 #endif
22 #include "icons/save.xpm"
23
24 namespace creaImageIO
25 {
26         // CTor
27    WxDumpPanel::WxDumpPanel(wxWindow *parent,  std::string i_filename)
28     : wxDialog(parent, -1,_T("DICOM TAGS"), wxDefaultPosition, wxSize(550,580)), filename(i_filename)
29    {
30         int size = 16;
31         mIcon = new wxImageList(size,size,true);
32         mIcon->Add(wxBitmap(wxBitmap(wxIcon(save_xpm)).ConvertToImage().Rescale(size, size)));
33         wxToolBar *mToolBar = new wxToolBar(this,-1,wxDefaultPosition,wxDefaultSize);
34         mToolBar->AddTool( DUMP_SAVE_ID,_T("Save"), mIcon->GetBitmap(0), _T("Save Dicom Tags in text file"));
35         mToolBar->Realize();
36         DumpText = new wxTextCtrl( this, wxID_ANY,_T(""), wxPoint(5,30), wxSize(520,510), wxTE_READONLY| wxMac | wxTE_MULTILINE | wxTE_RICH );
37         Layout(); 
38         Print();
39         }
40
41         // DTor
42         WxDumpPanel::~WxDumpPanel(){}
43
44         ///////////////////////////////////////////////////
45         /// Display in a control Text all dicom tags
46         ///////////////////////////////////////////////////
47         void WxDumpPanel::Print()
48         {
49            std::stringstream os;
50            std::string result = "";
51            if ( !filename.empty()) // ====== Deal with a single file ======
52            {
53
54 #if defined(USE_GDCM)
55                    GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
56                    f->SetLoadMode(GDCM_NAME_SPACE::LD_ALL);
57                    f->SetFileName( filename );
58                    f->SetMaxSizeLoadEntry(0xffff);
59                    f->Load();
60                    GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
61                       f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ |GDCM_NAME_SPACE::LD_NOSHADOW); 
62                    fh->SetPrintLevel( 0 );
63                    fh->Print(os);
64                  
65                    std::string line;
66                    while(std::getline(os, line))
67                    {
68                           result +=clean(line.c_str());
69                           result += "\n";
70                    }
71                    
72
73 #endif
74 #if defined(USE_GDCM2)
75                   gdcm::Reader reader;
76                   reader.SetFileName( filename.c_str() );
77                   if (reader.Read())
78                   {
79                           gdcm::Printer printer;
80                           printer.SetFile ( reader.GetFile() );
81                           printer.SetColor( 0 );
82                           printer.Print( os );
83                           result = os.str();
84                   }
85
86
87
88         //const gdcm::DataSet& ds = reader.GetFile().GetDataSet();
89  // // (5400,0100) SQ (Sequence with undefined length #=1)     # u/l, 1 WaveformSequence
90         // gdcm::Tag twsq(0x5400,0x0100);
91         // bool btest = ds.FindDataElement( twsq) ;
92         //const gdcm::DataElement &wsq = ds.GetDataElement( twsq );
93         // gdcm::SmartPointer<gdcm::SequenceOfItems> sqi = wsq.GetValueAsSQ();
94         // int num = sqi->GetNumberOfItems();
95  // 
96  // const gdcm::Item & item = sqi->GetItem(1); // Item start at #1
97  // const gdcm::DataSet& nestedds = item.GetNestedDataSet();
98
99  // // (5400,1004) US 16                                       #   2, 1 WaveformBitsAllocated
100  // gdcm::Tag twba(0x5400,0x1004);
101  // if( !nestedds.FindDataElement( twba ) )
102  //   {
103  // 
104  //   }
105  // const gdcm::DataElement &wba= nestedds.GetDataElement( twba );
106
107  // //std::cout << wba << std::endl;
108  // //  (5400,1006) CS [SS]                                     #   2, 1 WaveformSampleInterpretation
109  // // (5400,1010) OW 00ba\0030\ff76\ff8b\00a2\ffd3\ffae\ff50\0062\00c4\011e\00c2\00ba... # 57600, 1 WaveformData
110  // gdcm::Tag twd(0x5400,0x1010);
111  // if( !nestedds.FindDataElement( twd ) )
112  //   {
113
114  //   }
115  // const gdcm::DataElement &wd = nestedds.GetDataElement( twd );
116  // const gdcm::ByteValue *bv = wd.GetByteValue();
117  // size_t len = bv->GetLength();
118  // int16_t *p = (int16_t*)bv;
119         // 
120  // size_t npts = len / 2;
121  // //npts = 10; // DEBUG !
122  // for(size_t i = 0; i < npts; ++i )
123  //   {
124  //   float x;
125  //   x = (float)p[i];
126  //   }
127  //// 
128         //// gdcm::SmartPointer<gdcm::SequenceOfItems> sqi = wsq.GetValueAsSQ();
129         ////if( !sqi || !sqi->GetNumberOfItems() )
130  ////   {
131  ////   
132  ////   }
133
134  //// const gdcm::Item & item = sqi->GetItem(1); // Item start at #1
135  //// const gdcm::DataSet& nestedds = item.GetNestedDataSet();
136
137  //// // (5400,1004) US 16                                       #   2, 1 WaveformBitsAllocated
138  //// gdcm::Tag twba(0x5400,0x1004);
139  //// if( !nestedds.FindDataElement( twba ) )
140  ////   {
141  ////   return 0;
142  ////   }
143  //// const gdcm::DataElement &wba= nestedds.GetDataElement( twba );
144
145
146
147         //        gdcm::DataElement data = reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(0x5400,0x0100));
148         //        std::string seq_old_begin = "(fffe,e000) na (Item with undefined length)";
149         //        std::string seq_new_begin = "<SEQ>";
150         //        boost::algorithm::replace_all(result, seq_old_begin, seq_new_begin);
151         //        std::string seq_old_end = "(fffe,e00d)";
152         //        std::string seq_new_end = "<\\SEQ>";
153         //        boost::algorithm::replace_all(result, seq_old_end, seq_new_end);
154                   //seq_old_end = "(fffe,e0dd)";                  boost::algorithm::replace_all(result, seq_old_end, "\n");
155 #endif
156                   DumpText->SetValue(crea::std2wx(result));                
157
158                 }
159         }
160
161
162         const std::string WxDumpPanel::clean(const std::string &i_line)
163         {
164                 
165                   if (i_line.substr(4,1) == "|")
166                            {
167                                    std::string tag;
168                                    std::string line;
169                                    std:string signification;
170                                    std::string value;
171                                    std::string resultat;
172
173                                    tag = "(" + i_line.substr(0,9) + ")";
174                                    line = i_line.substr(14,i_line.size()-10);
175                                    int pos1 = line.find_first_of("[");
176                                    int pos2 = line.find_first_of("]");
177                                    signification = line.substr(pos1+1, pos2-pos1-1);
178                                    line = line.substr(pos2+1);
179                                     pos1 = line.find_first_of("[");
180                                     pos2 = line.find_first_of("]");
181                                    value = line.substr(pos1+1, pos2-pos1-1);
182                                    resultat = tag + " " + signification + ": " +value;
183                                   return resultat;
184                            }
185                    else
186                    {
187                            return i_line;
188                    }
189         }
190
191 ///////////////////////////////////////////////////     
192 /// wxEvent to save Dicom Tags in a text file    //
193 ///////////////////////////////////////////////////
194         void WxDumpPanel::SaveInfos(wxCommandEvent& event)
195         {
196         wxFileDialog* FD = new wxFileDialog( 0,_T("Select file"), _T(""), _T(""),
197                                                crea::std2wx("*.txt"), wxOPEN, wxDefaultPosition);
198         if (FD->ShowModal()==wxID_OK)
199                 {
200                         wxBusyCursor busy;
201                         std::ofstream ofs(crea::wx2std(FD->GetPath()).c_str());
202                         ofs.clear();
203                         ofs << crea::wx2std(DumpText->GetValue());;
204                         ofs.close();
205                 }
206                 Close();
207         }
208
209 ////////////////////////////////////////////////////////////
210 ////////////////////////////////////////////////////////////
211 BEGIN_EVENT_TABLE(WxDumpPanel, wxDialog)
212     EVT_TOOL(DUMP_SAVE_ID, WxDumpPanel::SaveInfos)
213 END_EVENT_TABLE()
214 }
215