]> Creatis software - gdcm.git/blob - Example/PhilipsToBrucker.cxx
Minor Typo
[gdcm.git] / Example / PhilipsToBrucker.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: PhilipsToBrucker.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/12/21 15:01:04 $
7   Version:   $Revision: 1.1 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmDocEntry.h"
19 #include "gdcmDicomDir.h"
20 #include "gdcmDicomDirPatient.h"
21 #include "gdcmFile.h"
22 #include "gdcmDirList.h"
23 #include "gdcmDebug.h"
24 #include "gdcmArgMgr.h"
25 #include "gdcmUtil.h"
26 #include "gdcmSerieHelper.h"
27
28 #include <iostream>
29
30 /**
31   * \brief   
32   *          - explores recursively the given directory
33   *          - keeps the requested series
34   *          - orders the gdcm-readable found Files
35   *            according their Patient/Study/Serie/Image characteristics
36   *          - fills a single level Directory with *all* the files,
37   *            converted into a Brucker-like Dicom, Intags compliant
38   *          
39   */  
40
41 typedef std::map<std::string, gdcm::File*> SortedFiles;
42
43 int main(int argc, char *argv[]) 
44 {
45 /*
46    START_USAGE(usage)
47    " \n PhilipsToBrucker :\n                                                  ",
48    " Explores recursively the given directory,                                ",
49    "                                                                          ", 
50    " usage: PhilipsToBrucker dirin=rootDirectoryName                          ",
51    "                         dirout=outputDirectoryName                       ",
52    "        [noshadowseq][noshadow][noseq] [debug]                            ",
53    "                                                                          ",
54    "        noshadowseq: user doesn't want to load Private Sequences          ",
55    "        noshadow : user doesn't want to load Private groups (odd number)  ",
56    "        noseq    : user doesn't want to load Sequences                    ",
57    "        debug    : user wants to run the program in 'debug mode'          ",
58    FINISH_USAGE
59 */
60 const char **usage;
61
62    // ----- Initialize Arguments Manager ------   
63    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
64   
65    if (argc == 1 || am->ArgMgrDefined("usage")) 
66    {
67       am->ArgMgrUsage(usage); // Display 'usage'
68       delete am;
69       return 0;
70    }
71
72    char *dirNamein;   
73    dirNamein  = am->ArgMgrGetString("dirin",(char *)"."); 
74
75    char *dirNameout;   
76    dirNameout  = am->ArgMgrGetString("dirout",(char *)".");  
77    
78    int loadMode = gdcm::LD_ALL;
79    if ( am->ArgMgrDefined("noshadowseq") )
80       loadMode |= gdcm::LD_NOSHADOWSEQ;
81    else 
82    {
83    if ( am->ArgMgrDefined("noshadow") )
84          loadMode |= gdcm::LD_NOSHADOW;
85       if ( am->ArgMgrDefined("noseq") )
86          loadMode |= gdcm::LD_NOSEQ;
87    }
88
89    if (am->ArgMgrDefined("debug"))
90       gdcm::Debug::DebugOn();
91  
92    // if unused Param we give up
93    if ( am->ArgMgrPrintUnusedLabels() )
94    { 
95       am->ArgMgrUsage(usage);
96       delete am;
97       return 0;
98    }
99
100    delete am;  // we don't need Argument Manager any longer
101
102    // ----- Begin Processing -----
103
104    std::string strDirNamein(dirNamein);
105    gdcm::DirList dirList(strDirNamein, true); 
106    
107  /*  
108    std::cout << "---------------File list found ------------" << std::endl;
109    dirList.Print();
110  */   
111
112    gdcm::DirListType fileNames;
113    fileNames = dirList.GetFilenames();
114    gdcm::SerieHelper *s;  
115    s = gdcm::SerieHelper::New();
116
117 /*       
118    std::cout << "---------------Print Serie--------------" << std::endl; 
119    s->SetDirectory(dirNamein, true); // true : recursive exploration 
120    s->SetUseSeriesDetails(true);  
121    s->AddSeriesDetail(0x0018, 0x1312);   
122    s->Print();
123 */
124    
125
126    
127    gdcm::File *f;
128
129 /*    
130    std::cout << "---------------Print Unique Series identifiers---------"  << std::endl;     
131    std::string uniqueSeriesIdentifier;
132  
133    for (gdcm::DirListType::iterator it = fileNames.begin();  
134                                     it != fileNames.end();
135                                   ++it)
136    {
137       std::cout << "File Name : " << *it << std::endl;
138       f = gdcm::File::New();
139       f->SetLoadMode(gdcm::LD_ALL);
140       f->SetFileName( *it );
141       f->Load();
142         
143       uniqueSeriesIdentifier=s->CreateUniqueSeriesIdentifier(f);
144       std::cout << "                           [" <<
145                uniqueSeriesIdentifier  << "]" << std::endl;
146        
147       f->Delete();
148    }
149 */
150    
151    std::cout << "------------------Print Break levels-----------------" << std::endl;
152
153    std::string userFileIdentifier; 
154    SortedFiles sf;
155
156
157    s->AddSeriesDetail(0x0010, 0x0010, false); // Patient's Name
158    s->AddSeriesDetail(0x0020, 0x000e, false); // Series Instance UID
159    s->AddSeriesDetail(0x0020, 0x0032, false); // Image Position (Patient)     
160    s->AddSeriesDetail(0x0018, 0x1312, false); // In-plane Phase Encoding Direction 
161    s->AddSeriesDetail(0x0018, 0x1060, true);  // Trigger Time 
162    
163    for (gdcm::DirListType::iterator it = fileNames.begin();  
164                                     it != fileNames.end();
165                                   ++it)
166    {
167       //std::cout << "File Name : " << *it << std::endl;
168       f = gdcm::File::New();
169       f->SetLoadMode(gdcm::LD_ALL);
170       f->SetFileName( *it );
171       f->Load();
172
173       userFileIdentifier=s->CreateUserDefinedFileIdentifier(f);        
174       //std::cout << "                           [" <<
175       //        userFileIdentifier  << "]" << std::endl;
176       // storing in a map ensures automatic sorting !      
177       sf[userFileIdentifier] = f;
178    }
179       
180    std::vector<std::string> tokens;
181    std::string fullFilename;
182    std::string previousPatientName, currentPatientName;
183    std::string previousSerieInstanceUID, currentSerieInstanceUID;
184    std::string previousImagePosition, currentImagePosition;
185    std::string previousPhaseEncodingDirection, currentPhaseEncodingDirection;
186    
187    SortedFiles::iterator it2 = sf.begin();
188       
189    gdcm::Util::Tokenize (it2->first, tokens, "_");
190    
191    previousPatientName            = tokens[0];
192    previousSerieInstanceUID       = tokens[1];
193    previousImagePosition          = tokens[2];
194    previousPhaseEncodingDirection = tokens[3];
195    std::cout << "==== new Patient "                            << currentPatientName      << std::endl;   
196    std::cout << "==== === new Serie "                          << currentSerieInstanceUID << std::endl;
197    std::cout << "==== === === new Position "                   << currentImagePosition    << std::endl; 
198    std::cout << "==== === === === new PhaseEncodingDirection " << currentImagePosition    << std::endl;
199    std::cout << "==== === === ===    "                         << it2->first              << std::endl; 
200     
201    it2++;
202    
203    for ( ; it2 != sf.end(); ++it2)
204    {
205       tokens.clear();
206       gdcm::Util::Tokenize (it2->first, tokens, "_");
207       
208       currentPatientName            = tokens[0];
209       currentSerieInstanceUID       = tokens[1];
210       currentImagePosition          = tokens[2];
211       currentPhaseEncodingDirection = tokens[3];     
212       
213       if (previousPatientName != currentPatientName)
214       {
215          previousPatientName = currentPatientName;
216          std::cout << "==== new Patient " << currentPatientName << std::endl;
217          previousPatientName            = currentPatientName;
218          previousSerieInstanceUID       = currentSerieInstanceUID;
219          previousImagePosition          = currentImagePosition;
220          previousPhaseEncodingDirection = currentPhaseEncodingDirection;
221       }
222
223       if (previousSerieInstanceUID != currentSerieInstanceUID)
224       {        
225          std::cout << "==== === new Serie " << currentSerieInstanceUID << std::endl;
226          previousSerieInstanceUID       = currentSerieInstanceUID;
227          previousImagePosition          = currentImagePosition;
228          previousPhaseEncodingDirection = currentPhaseEncodingDirection;
229       }
230
231       if (previousImagePosition != currentImagePosition)
232       {        
233          std::cout << "==== === === new Position " << currentImagePosition << std::endl;
234          previousImagePosition          = currentImagePosition;
235          previousPhaseEncodingDirection = currentPhaseEncodingDirection;
236       }      
237
238       if (previousPhaseEncodingDirection != currentPhaseEncodingDirection)
239       {        
240          std::cout << "==== === === === new PhaseEncodingDirection " << currentImagePosition << std::endl;
241          previousPhaseEncodingDirection = currentPhaseEncodingDirection;
242       } 
243       fullFilename =  (it2->second)->GetFileName();          
244       std::cout << "==== === === ===    " << it2->first << "  " << (it2->second)->GetFileName() << " " 
245                 << gdcm::Util::GetName( fullFilename ) <<std::endl;
246       
247    }
248  }
249