]> Creatis software - gdcm.git/blob - Testing/TestDicomDir.cxx
Add GetImagePositionPatient() checking
[gdcm.git] / Testing / TestDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/05/31 16:51:41 $
7   Version:   $Revision: 1.43 $
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          // serious trouble : values differ in source and destination file
73          std::cout << "for gdcm DicomDir element (" << std::hex 
74                    << e2->GetGroup() << "," <<e2->GetElement() 
75                    << ") values differ [" 
76                    << ((gdcm::DataEntry *)e1)->GetString() << "] != [" 
77                    << ((gdcm::DataEntry *)e2)->GetString() << "]"
78                    << std::endl;
79           return 1;
80       }
81    }
82    return 0;
83
84  
85 int TestDicomDir(int argc, char *argv[])
86 {  
87    gdcm::DicomDir *dicomdir;
88    
89    gdcm::DicomDirPatient *pa1;
90    gdcm::DicomDirStudy   *st1;
91    gdcm::DicomDirSerie   *se1;
92    gdcm::DicomDirImage   *im1;
93
94    gdcm::TSKey v;
95     
96    std::string file; 
97    if (argc > 1) 
98       file = argv[1];    
99    else 
100    {
101       file += GDCM_DATA_ROOT;
102       file += "/DICOMDIR";
103    }
104
105    dicomdir = gdcm::DicomDir::New();
106    dicomdir->SetFileName(file);
107    dicomdir->Load();
108    if (argc > 2) 
109    {
110       int level = atoi(argv[2]);   
111       dicomdir->SetPrintLevel(level);
112    }
113
114    // Test if the DicomDir is readable
115    if( !dicomdir->IsReadable() )
116    {
117       std::cout<<"          DicomDir '"<<file
118                <<"' is not readable"<<std::endl
119                <<"          ...Failed"<<std::endl;
120
121       dicomdir->Delete();
122       return 1;
123    }
124    else
125    {
126       std::cout<<"          DicomDir '"<<file
127                <<"' is readable"<<std::endl;
128    }
129
130    // Test if the DicomDir contains any patient
131    if( !dicomdir->GetFirstPatient() )
132    {
133       std::cout<<"          DicomDir '"<<file
134                <<" has no patient"<<std::endl
135                <<"          ...Failed"<<std::endl;
136
137       dicomdir->Delete();
138       return 1;
139    }
140
141    // step by step structure full exploitation
142    std::cout << std::endl << std::endl  
143              << " = PATIENT/STUDY/SERIE/IMAGE List ============================" 
144              << std::endl<< std::endl;
145
146    pa1 = dicomdir->GetFirstPatient(); 
147    while ( pa1 ) 
148    {  // we process all the PATIENT of this DICOMDIR 
149       std::cout << pa1->GetEntryString(0x0010, 0x0010) << std::endl; // Patient's Name
150
151       st1 = pa1->GetFirstStudy();
152       while ( st1 ) 
153       { // we process all the STUDY of this patient
154          std::cout << "--- "<< st1->GetEntryString(0x0008, 0x1030) // Study Description
155          << std::endl;  
156          std::cout << " Stud.ID:[" << st1->GetEntryString(0x0020, 0x0010) // Study ID
157          << "]"; 
158
159          se1 = st1->GetFirstSerie();
160          while ( se1 ) 
161          { // we process all the SERIES of this study
162             std::cout << "--- --- "<< se1->GetEntryString(0x0008, 0x103e) << std::endl;      // Serie Description
163             std::cout << " Ser.nb:["         <<  se1->GetEntryString(0x0020, 0x0011);        // Series number
164             std::cout << "] Mod.:["          <<  se1->GetEntryString(0x0008, 0x0060) << "]"; // Modality
165
166             im1 = se1->GetFirstImage();
167             while ( im1 ) { // we process all the IMAGE of this serie
168                std::cout << "--- --- --- "<< im1->GetEntryString(0x0004, 0x1500) << std::endl; // File name
169                im1 = se1->GetNextImage();   
170             }
171             se1 = st1->GetNextSerie();   
172          }
173          st1 = pa1->GetNextStudy();
174       } 
175       pa1 = dicomdir->GetNextPatient();
176    }  
177
178    std::cout << std::endl << std::endl  
179              << " = DICOMDIR full content ====================================" 
180              << std::endl<< std::endl;
181   // dicomdir->Print();
182
183    // ------------------------- second stage ---------------------------
184     
185    // Write on disc what we read
186    dicomdir->Write("NewDICOMDIR");
187
188    std::cout << std::endl << std::endl  
189              << "NewDICOMDIR written on disc =================================" 
190              << std::endl<< std::endl;
191   // Read what we wrote  
192    gdcm::DicomDir *d2 = gdcm::DicomDir::New();
193    d2->SetFileName("NewDICOMDIR");
194    d2->Load();
195    if (!d2->IsReadable())
196    {
197       std::cout << std::endl << std::endl  
198                 << "Read NewDicomDir from disc failed ========================" 
199                 << std::endl<< std::endl;
200       return 1;
201    }
202    std::cout << std::endl << std::endl  
203              << "NewDICOMDIR successfully read from disc =================================" 
204              << std::endl<< std::endl;
205   
206    gdcm::DicomDirPatient *pa2;
207    gdcm::DicomDirStudy   *st2;
208    gdcm::DicomDirSerie   *se2;
209    gdcm::DicomDirImage   *im2;
210
211    pa1 = dicomdir->GetFirstPatient(); 
212    pa2 = d2->GetFirstPatient(); 
213
214    if (!d2)
215    {
216       std::cout << "NewDICOMDIR contains no Patient ?!?" << std::endl;
217       dicomdir->Delete();
218       d2->Delete();
219       return 1;
220    }
221    
222    while ( pa1 && pa2 ) 
223    {  // we process all the PATIENT of this DICOMDIR
224
225       if ( CompareSQItem(pa2,pa1) == 1 )
226       {
227          dicomdir->Delete();
228          d2->Delete();
229          return 1;
230       }
231   
232       // just to allow human reader to be sure ...
233       std::cout << pa2->GetEntryString(0x0010, 0x0010) 
234                 << std::endl; // Patient's Name
235  
236       st1 = pa1->GetFirstStudy();
237       st2 = pa2->GetFirstStudy();
238
239       while ( st1 && st2 )   
240       {
241          if ( CompareSQItem(st2,st1) == 1 )
242          {
243             dicomdir->Delete();
244             d2->Delete();
245             return 1;
246          }
247
248          // just to allow human reader to be sure ...
249          std::cout << "--- "<< st2->GetEntryString(0x0008, 0x1030);
250          // << std::endl;    // Study Description
251          std::cout << " Stud.ID:["          
252                    << st2->GetEntryString(0x0020, 0x0010)
253                    << "]" << std::endl; // Study ID
254   
255          se1 = st1->GetFirstSerie();
256          se2 = st2->GetFirstSerie();
257
258          while ( se1 && se2 ) 
259          { // we process all the SERIE of this study
260             if ( CompareSQItem(se2,se1) == 1 )
261               return 1;
262
263             std::cout << "--- --- " << se2->GetEntryString(0x0008, 0x103e);      // Serie Description
264             std::cout << " Ser.nb:["<< se2->GetEntryString(0x0020, 0x0011);        // Series number
265             std::cout << "] Mod.:[" << se2->GetEntryString(0x0008, 0x0060) << "]" << std::endl; // Modality
266             im1 = se1->GetFirstImage();
267             im2 = se2->GetFirstImage();
268
269             while ( im1 && im2 ) // we process all the IMAGE of this serie
270             {
271                if ( CompareSQItem(im2,im1) == 1 )
272                {
273                   dicomdir->Delete();
274                   d2->Delete();
275                   return 1; 
276                }
277
278                im1 = se1->GetNextImage();   
279                im2 = se2->GetNextImage();   
280             }
281             se1 = st1->GetNextSerie();   
282             se2 = st2->GetNextSerie();   
283          }
284          st1 = pa1->GetNextStudy();
285          st2 = pa2->GetNextStudy();
286       }
287       pa1 = dicomdir->GetNextPatient();
288       pa2 = dicomdir->GetNextPatient();
289    }
290    
291    std::cout << std::flush;
292    dicomdir->Delete();
293    d2->Delete();
294
295    return 0;
296 }