creaImageIOWxDumpPanel.cpp

Go to the documentation of this file.
00001 #include <creaImageIOWxDumpPanel.h>
00002 #include <creaImageIOSystem.h>
00003 #include <creaImageIOGimmick.h>
00004 #include <boost/algorithm/string.hpp>
00005 #if defined(USE_GDCM)
00006 #include <gdcmGlobal.h>
00007 #include <gdcmDictSet.h>
00008 #include "gdcmFile.h"
00009 #include "gdcmDocument.h"
00010 #include "gdcmFileHelper.h"
00011 #endif
00012 
00013 #if defined(USE_GDCM2)
00014 #include "gdcmGlobal.h"
00015 #include "gdcmFile.h"
00016 #include "gdcmDictPrinter.h"
00017 #include "gdcmPrinter.h"
00018 #include "gdcmReader.h"
00019 #include "gdcmSmartPointer.h"
00020 #include "gdcmSequenceOfItems.h"
00021 #endif
00022 #include "icons/save.xpm"
00023 
00024 namespace creaImageIO
00025 {
00026         // CTor
00027    WxDumpPanel::WxDumpPanel(wxWindow *parent,  std::string i_filename)
00028     : wxDialog(parent, -1,_T("DICOM TAGS"), wxDefaultPosition, wxSize(550,580)), filename(i_filename)
00029    {
00030         int size = 16;
00031         mIcon = new wxImageList(size,size,true);
00032         mIcon->Add(wxBitmap(wxBitmap(wxIcon(save_xpm)).ConvertToImage().Rescale(size, size)));
00033         wxToolBar *mToolBar = new wxToolBar(this,-1,wxDefaultPosition,wxDefaultSize);
00034         mToolBar->AddTool( DUMP_SAVE_ID,_T("Save"), mIcon->GetBitmap(0), _T("Save Dicom Tags in text file"));
00035         mToolBar->Realize();
00036         DumpText = new wxTextCtrl( this, wxID_ANY,_T(""), wxPoint(5,30), wxSize(520,510), wxTE_READONLY| wxMac | wxTE_MULTILINE | wxTE_RICH );
00037         Layout(); 
00038         Print();
00039         }
00040 
00041         // DTor
00042         WxDumpPanel::~WxDumpPanel(){}
00043 
00047         void WxDumpPanel::Print()
00048         {
00049            std::stringstream os;
00050            std::string result = "";
00051            if ( !filename.empty()) // ====== Deal with a single file ======
00052            {
00053 
00054 #if defined(USE_GDCM)
00055                    GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
00056                    f->SetLoadMode(GDCM_NAME_SPACE::LD_ALL);
00057                    f->SetFileName( filename );
00058                    f->SetMaxSizeLoadEntry(0xffff);
00059                    f->Load();
00060                    GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
00061                       f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ |GDCM_NAME_SPACE::LD_NOSHADOW); 
00062                    fh->SetPrintLevel( 0 );
00063                    fh->Print(os);
00064                  
00065                    std::string line;
00066                    while(std::getline(os, line))
00067                    {
00068                           result +=clean(line.c_str());
00069                           result += "\n";
00070                    }
00071                    
00072 
00073 #endif
00074 #if defined(USE_GDCM2)
00075                   gdcm::Reader reader;
00076                   reader.SetFileName( filename.c_str() );
00077                   if (reader.Read())
00078                   {
00079                           gdcm::Printer printer;
00080                           printer.SetFile ( reader.GetFile() );
00081                           printer.SetColor( 0 );
00082                           printer.Print( os );
00083                           result = os.str();
00084                   }
00085 
00086 
00087 
00088         //const gdcm::DataSet& ds = reader.GetFile().GetDataSet();
00089  // // (5400,0100) SQ (Sequence with undefined length #=1)     # u/l, 1 WaveformSequence
00090         // gdcm::Tag twsq(0x5400,0x0100);
00091         // bool btest = ds.FindDataElement( twsq) ;
00092         //const gdcm::DataElement &wsq = ds.GetDataElement( twsq );
00093         // gdcm::SmartPointer<gdcm::SequenceOfItems> sqi = wsq.GetValueAsSQ();
00094         // int num = sqi->GetNumberOfItems();
00095  // 
00096  // const gdcm::Item & item = sqi->GetItem(1); // Item start at #1
00097  // const gdcm::DataSet& nestedds = item.GetNestedDataSet();
00098 
00099  // // (5400,1004) US 16                                       #   2, 1 WaveformBitsAllocated
00100  // gdcm::Tag twba(0x5400,0x1004);
00101  // if( !nestedds.FindDataElement( twba ) )
00102  //   {
00103  // 
00104  //   }
00105  // const gdcm::DataElement &wba= nestedds.GetDataElement( twba );
00106 
00107  // //std::cout << wba << std::endl;
00108  // //  (5400,1006) CS [SS]                                     #   2, 1 WaveformSampleInterpretation
00109  // // (5400,1010) OW 00ba\0030\ff76\ff8b\00a2\ffd3\ffae\ff50\0062\00c4\011e\00c2\00ba... # 57600, 1 WaveformData
00110  // gdcm::Tag twd(0x5400,0x1010);
00111  // if( !nestedds.FindDataElement( twd ) )
00112  //   {
00113 
00114  //   }
00115  // const gdcm::DataElement &wd = nestedds.GetDataElement( twd );
00116  // const gdcm::ByteValue *bv = wd.GetByteValue();
00117  // size_t len = bv->GetLength();
00118  // int16_t *p = (int16_t*)bv;
00119         // 
00120  // size_t npts = len / 2;
00121  // //npts = 10; // DEBUG !
00122  // for(size_t i = 0; i < npts; ++i )
00123  //   {
00124  //   float x;
00125  //   x = (float)p[i];
00126  //   }
00133 
00136 
00144 
00145 
00146 
00147         //        gdcm::DataElement data = reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(0x5400,0x0100));
00148         //        std::string seq_old_begin = "(fffe,e000) na (Item with undefined length)";
00149         //        std::string seq_new_begin = "<SEQ>";
00150         //        boost::algorithm::replace_all(result, seq_old_begin, seq_new_begin);
00151         //        std::string seq_old_end = "(fffe,e00d)";
00152         //        std::string seq_new_end = "<\\SEQ>";
00153         //        boost::algorithm::replace_all(result, seq_old_end, seq_new_end);
00154                   //seq_old_end = "(fffe,e0dd)";                  boost::algorithm::replace_all(result, seq_old_end, "\n");
00155 #endif
00156                   DumpText->SetValue(crea::std2wx(result));                
00157 
00158                 }
00159         }
00160 
00161 
00162         const std::string WxDumpPanel::clean(const std::string &i_line)
00163         {
00164                 
00165                   if (i_line.substr(4,1) == "|")
00166                            {
00167                                    std::string tag;
00168                                    std::string line;
00169                                    std:string signification;
00170                                    std::string value;
00171                                    std::string resultat;
00172 
00173                                    tag = "(" + i_line.substr(0,9) + ")";
00174                                    line = i_line.substr(14,i_line.size()-10);
00175                                    int pos1 = line.find_first_of("[");
00176                                    int pos2 = line.find_first_of("]");
00177                                    signification = line.substr(pos1+1, pos2-pos1-1);
00178                                    line = line.substr(pos2+1);
00179                                     pos1 = line.find_first_of("[");
00180                                     pos2 = line.find_first_of("]");
00181                                    value = line.substr(pos1+1, pos2-pos1-1);
00182                                    resultat = tag + " " + signification + ": " +value;
00183                                   return resultat;
00184                            }
00185                    else
00186                    {
00187                            return i_line;
00188                    }
00189         }
00190 
00194         void WxDumpPanel::SaveInfos(wxCommandEvent& event)
00195         {
00196         wxFileDialog* FD = new wxFileDialog( 0,_T("Select file"), _T(""), _T(""),
00197                                                crea::std2wx("*.txt"), wxOPEN, wxDefaultPosition);
00198         if (FD->ShowModal()==wxID_OK)
00199                 {
00200                         wxBusyCursor busy;
00201                         std::ofstream ofs(crea::wx2std(FD->GetPath()).c_str());
00202                         ofs.clear();
00203                         ofs << crea::wx2std(DumpText->GetValue());;
00204                         ofs.close();
00205                 }
00206                 Close();
00207         }
00208 
00211 BEGIN_EVENT_TABLE(WxDumpPanel, wxDialog)
00212     EVT_TOOL(DUMP_SAVE_ID, WxDumpPanel::SaveInfos)
00213 END_EVENT_TABLE()
00214 }
00215