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