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