]> Creatis software - gdcm.git/blob - Testing/TestDicomDir.cxx
Fix some comments
[gdcm.git] / Testing / TestDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/03/23 15:03:01 $
7   Version:   $Revision: 1.44 $
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 "gdcmDataEntry.h"
20 #include "gdcmDicomDir.h"
21 #include "gdcmDicomDirPatient.h"
22 #include "gdcmDicomDirStudy.h"
23 #include "gdcmDicomDirSerie.h"
24 #include "gdcmDicomDirImage.h"
25 #include "gdcmTS.h"
26
27 #include <iostream>
28 #include <fstream>
29
30 #include <stdlib.h>
31
32 // check *all* the dicom elements (gdcm::DocEntry)
33 // of this gdcm::DicomDirObject
34 int CompareSQItem(gdcm::SQItem *pa1, gdcm::SQItem *pa2 )
35 {
36    gdcm::DocEntry *e1;
37    gdcm::DocEntry *e2;
38
39    e2 = pa2->GetFirstEntry();
40    while (!e2)
41    {
42       // locate the corresponding element in 'source' file 
43       e1 = pa1->GetDocEntry( e2->GetGroup(),e2->GetElement() );
44
45       // an element doesn't exist in origin file
46       if (!e1)
47       {
48        std::cout << "DicomDir element " << std::hex 
49                  << e2->GetGroup() << "," <<e2->GetElement() << std::endl;
50        return 1; 
51       }
52       // skip SeqEntries (I don't want to deal with 'recursion pbs' here)
53       if ( !dynamic_cast<gdcm::DataEntry *>(e1) ||
54            !dynamic_cast<gdcm::DataEntry *>(e2) )
55          continue;
56
57       // a value is read as GDCM_UNFOUND 
58       if ( ((gdcm::DataEntry *)e1)->GetString() == gdcm::GDCM_UNFOUND )
59       {
60          std::cout << "for gdcm source DicomDir : element (" << std::hex 
61                    << e1->GetGroup() << "," <<e1->GetElement() 
62                    << ") has values [" << gdcm::GDCM_UNFOUND << "]"
63                    << std::endl;
64          return 1;
65       }
66
67       // values differ in source file and destination file
68       if ( ((gdcm::DataEntry *)e1)->GetString() != 
69            ((gdcm::DataEntry *)e2)->GetString() )
70       {
71  
72  /// \todo : check the value *written on disc*, not the value converted as a std::string
73  ///         (this comparison doesn't make the difference between Ox(ff) and "255" ...)
74  
75          // serious trouble : values differ in source and destination file
76          std::cout << "for gdcm DicomDir element (" << std::hex 
77                    << e2->GetGroup() << "," <<e2->GetElement() 
78                    << ") values differ [" 
79                    << ((gdcm::DataEntry *)e1)->GetString() << "] != [" 
80                    << ((gdcm::DataEntry *)e2)->GetString() << "]"
81                    << std::endl;
82           return 1;
83       }
84    }
85    return 0;
86
87  
88 int TestDicomDir(int argc, char *argv[])
89 {  
90    gdcm::DicomDir *dicomdir;
91    
92    gdcm::DicomDirPatient *pa1;
93    gdcm::DicomDirStudy   *st1;
94    gdcm::DicomDirSerie   *se1;
95    gdcm::DicomDirImage   *im1;
96
97    gdcm::TSKey v;
98     
99    std::string file; 
100    if (argc > 1) 
101       file = argv[1];    
102    else 
103    {
104       file += GDCM_DATA_ROOT;
105       file += "/DICOMDIR";
106    }
107
108    dicomdir = gdcm::DicomDir::New();
109    dicomdir->SetFileName(file);
110    dicomdir->Load();
111    if (argc > 2) 
112    {
113       int level = atoi(argv[2]);   
114       dicomdir->SetPrintLevel(level);
115    }
116
117    // Test if the DICOMDIR file is readable
118    if( !dicomdir->IsReadable() )
119    {
120       std::cout<<"          DicomDir '"<<file
121                <<"' is not readable"<<std::endl
122                <<"          ...Failed"<<std::endl;
123
124       dicomdir->Delete();
125       return 1;
126    }
127    else
128    {
129       std::cout<<"          DicomDir '"<<file
130                <<"' is readable"<<std::endl;
131    }
132
133    // Test if the gdcm::DicomDir contains any patient
134    if( !dicomdir->GetFirstPatient() )
135    {
136       std::cout<<"          DicomDir '"<<file
137                <<" has no patient"<<std::endl
138                <<"          ...Failed"<<std::endl;
139
140       dicomdir->Delete();
141       return 1;
142    }
143
144    // step by step structure full exploitation
145    std::cout << std::endl << std::endl  
146              << " = PATIENT/STUDY/SERIE/IMAGE List ============================" 
147              << std::endl<< std::endl;
148
149    pa1 = dicomdir->GetFirstPatient(); 
150    while ( pa1 ) 
151    {  // we process all the PATIENT of this gdcm::DicomDir 
152       std::cout << pa1->GetEntryString(0x0010, 0x0010) << std::endl; // Patient's Name
153
154       st1 = pa1->GetFirstStudy();
155       while ( st1 ) 
156       { // we process all the STUDY of this patient
157          std::cout << "--- "<< st1->GetEntryString(0x0008, 0x1030) // Study Description
158          << std::endl;  
159          std::cout << " Stud.ID:[" << st1->GetEntryString(0x0020, 0x0010) // Study ID
160          << "]"; 
161
162          se1 = st1->GetFirstSerie();
163          while ( se1 ) 
164          { // we process all the SERIES of this study
165             std::cout << "--- --- "<< se1->GetEntryString(0x0008, 0x103e) << std::endl;      // Serie Description
166             std::cout << " Ser.nb:["         <<  se1->GetEntryString(0x0020, 0x0011);        // Series number
167             std::cout << "] Mod.:["          <<  se1->GetEntryString(0x0008, 0x0060) << "]"; // Modality
168
169             im1 = se1->GetFirstImage();
170             while ( im1 ) { // we process all the IMAGE of this serie
171                std::cout << "--- --- --- "<< im1->GetEntryString(0x0004, 0x1500) << std::endl; // File name
172                im1 = se1->GetNextImage();   
173             }
174             se1 = st1->GetNextSerie();   
175          }
176          st1 = pa1->GetNextStudy();
177       } 
178       pa1 = dicomdir->GetNextPatient();
179    }  
180
181    std::cout << std::endl << std::endl  
182              << " = DICOMDIR full content ====================================" 
183              << std::endl<< std::endl;
184   // dicomdir->Print();
185
186    // ------------------------- second stage ---------------------------
187     
188    // Write on disc what we read
189    dicomdir->Write("NewDICOMDIR");
190
191    std::cout << std::endl << std::endl  
192              << "NewDICOMDIR written on disc =================================" 
193              << std::endl<< std::endl;
194   // Read what we wrote  
195    gdcm::DicomDir *d2 = gdcm::DicomDir::New();
196    d2->SetFileName("NewDICOMDIR");
197    d2->Load();
198    if (!d2->IsReadable())
199    {
200       std::cout << std::endl << std::endl  
201                 << "Read NewDicomDir from disc failed ========================" 
202                 << std::endl<< std::endl;
203       return 1;
204    }
205    std::cout << std::endl << std::endl  
206              << "NewDICOMDIR successfully read from disc =================================" 
207              << std::endl<< std::endl;
208   
209    gdcm::DicomDirPatient *pa2;
210    gdcm::DicomDirStudy   *st2;
211    gdcm::DicomDirSerie   *se2;
212    gdcm::DicomDirImage   *im2;
213
214    pa1 = dicomdir->GetFirstPatient(); 
215    pa2 = d2->GetFirstPatient(); 
216
217    if (!d2)
218    {
219       std::cout << "NewDICOMDIR contains no Patient ?!?" << std::endl;
220       dicomdir->Delete();
221       d2->Delete();
222       return 1;
223    }
224    
225    while ( pa1 && pa2 ) 
226    {  // we process all the PATIENT of this DICOMDIR
227
228       if ( CompareSQItem(pa2,pa1) == 1 )
229       {
230          dicomdir->Delete();
231          d2->Delete();
232          return 1;
233       }
234   
235       // just to allow human reader to be sure ...
236       std::cout << pa2->GetEntryString(0x0010, 0x0010) 
237                 << std::endl; // Patient's Name
238  
239       st1 = pa1->GetFirstStudy();
240       st2 = pa2->GetFirstStudy();
241
242       while ( st1 && st2 )   
243       {
244          if ( CompareSQItem(st2,st1) == 1 )
245          {
246             dicomdir->Delete();
247             d2->Delete();
248             return 1;
249          }
250
251          // just to allow human reader to be sure ...
252          std::cout << "--- "<< st2->GetEntryString(0x0008, 0x1030);
253          // << std::endl;    // Study Description
254          std::cout << " Stud.ID:["          
255                    << st2->GetEntryString(0x0020, 0x0010)
256                    << "]" << std::endl; // Study ID
257   
258          se1 = st1->GetFirstSerie();
259          se2 = st2->GetFirstSerie();
260
261          while ( se1 && se2 ) 
262          { // we process all the SERIE of this study
263             if ( CompareSQItem(se2,se1) == 1 )
264               return 1;
265
266             std::cout << "--- --- " << se2->GetEntryString(0x0008, 0x103e);      // Serie Description
267             std::cout << " Ser.nb:["<< se2->GetEntryString(0x0020, 0x0011);        // Series number
268             std::cout << "] Mod.:[" << se2->GetEntryString(0x0008, 0x0060) << "]" << std::endl; // Modality
269             im1 = se1->GetFirstImage();
270             im2 = se2->GetFirstImage();
271
272             while ( im1 && im2 ) // we process all the IMAGE of this serie
273             {
274                if ( CompareSQItem(im2,im1) == 1 )
275                {
276                   dicomdir->Delete();
277                   d2->Delete();
278                   return 1; 
279                }
280
281                im1 = se1->GetNextImage();   
282                im2 = se2->GetNextImage();   
283             }
284             se1 = st1->GetNextSerie();   
285             se2 = st2->GetNextSerie();   
286          }
287          st1 = pa1->GetNextStudy();
288          st2 = pa2->GetNextStudy();
289       }
290       pa1 = dicomdir->GetNextPatient();
291       pa2 = dicomdir->GetNextPatient();
292    }
293    
294    std::cout << std::flush;
295    dicomdir->Delete();
296    d2->Delete();
297
298    return 0;
299 }