]> Creatis software - gdcm.git/blob - Testing/TestDicomDir.cxx
Typo normalization
[gdcm.git] / Testing / TestDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/03 10:00:06 $
7   Version:   $Revision: 1.38 $
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
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::ValEntry*>(e1) ||
52            !dynamic_cast<gdcm::ValEntry*>(e2) )
53          continue;
54
55       // a value is read as GDCM_UNFOUND 
56       if ( ((gdcm::ValEntry*)e1)->GetValue() == 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::ValEntry*)e1)->GetValue() != 
67            ((gdcm::ValEntry*)e2)->GetValue() )
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::ValEntry*)e1)->GetValue() << "] != [" 
75                    << ((gdcm::ValEntry*)e2)->GetValue() << "]"
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 = new gdcm::DicomDir(file);
104    if (argc > 2) 
105    {
106       int level = atoi(argv[2]);   
107       dicomdir->SetPrintLevel(level);
108    }
109
110    // Test if the DicomDir is readable
111    if( !dicomdir->IsReadable() )
112    {
113       std::cout<<"          DicomDir '"<<file
114                <<"' is not readable"<<std::endl
115                <<"          ...Failed"<<std::endl;
116
117       delete dicomdir;
118       return 1;
119    }
120    else
121    {
122       std::cout<<"          DicomDir '"<<file
123                <<"' is readable"<<std::endl;
124    }
125
126    // Test if the DicomDir contains any patient
127    if( !dicomdir->GetFirstPatient() )
128    {
129       std::cout<<"          DicomDir '"<<file
130                <<" has no patient"<<std::endl
131                <<"          ...Failed"<<std::endl;
132
133       delete dicomdir;
134       return 1;
135    }
136
137    // step by step structure full exploitation
138    std::cout << std::endl << std::endl  
139              << " = PATIENT/STUDY/SERIE/IMAGE List ============================" 
140              << std::endl<< std::endl;
141
142    pa1 = dicomdir->GetFirstPatient(); 
143    while ( pa1 ) 
144    {  // we process all the PATIENT of this DICOMDIR 
145       std::cout << pa1->GetEntryValue(0x0010, 0x0010) << std::endl; // Patient's Name
146
147       st1 = pa1->GetFirstStudy();
148       while ( st1 ) 
149       { // we process all the STUDY of this patient
150          std::cout << "--- "<< st1->GetEntryValue(0x0008, 0x1030) // Study Description
151          << std::endl;  
152          std::cout << " Stud.ID:[" << st1->GetEntryValue(0x0020, 0x0010) // Study ID
153          << "]"; 
154
155          se1 = st1->GetFirstSerie();
156          while ( se1 ) 
157          { // we process all the SERIES of this study
158             std::cout << "--- --- "<< se1->GetEntryValue(0x0008, 0x103e) << std::endl;      // Serie Description
159             std::cout << " Ser.nb:["         <<  se1->GetEntryValue(0x0020, 0x0011);        // Series number
160             std::cout << "] Mod.:["          <<  se1->GetEntryValue(0x0008, 0x0060) << "]"; // Modality
161
162             im1 = se1->GetFirstImage();
163             while ( im1 ) { // we process all the IMAGE of this serie
164                std::cout << "--- --- --- "<< im1->GetEntryValue(0x0004, 0x1500) << std::endl; // File name
165                im1 = se1->GetNextImage();   
166             }
167             se1 = st1->GetNextSerie();   
168          }
169          st1 = pa1->GetNextStudy();
170       } 
171       pa1 = dicomdir->GetNextPatient();
172    }  
173
174    std::cout << std::endl << std::endl  
175              << " = DICOMDIR full content ====================================" 
176              << std::endl<< std::endl;
177   // dicomdir->Print();
178
179    // ------------------------- second stage ---------------------------
180     
181    // Write on disc what we read
182    dicomdir->WriteDicomDir("NewDICOMDIR");
183
184    std::cout << std::endl << std::endl  
185              << "NewDICOMDIR written on disc =================================" 
186              << std::endl<< std::endl;
187   // Read what we wrote  
188    gdcm::DicomDir *d2 = new gdcm::DicomDir("NewDICOMDIR");
189    if (!d2)
190    {
191       std::cout << std::endl << std::endl  
192                 << "Read NewDicomDir from disc failed ========================" 
193                 << std::endl<< std::endl;
194       return 1;
195    }
196    std::cout << std::endl << std::endl  
197              << "NewDICOMDIR successfully read from disc =================================" 
198              << std::endl<< std::endl;
199   
200    gdcm::DicomDirPatient *pa2;
201    gdcm::DicomDirStudy   *st2;
202    gdcm::DicomDirSerie   *se2;
203    gdcm::DicomDirImage   *im2;
204
205    pa1 = dicomdir->GetFirstPatient(); 
206    pa2 = d2->GetFirstPatient(); 
207
208    if (!d2)
209    {
210       std::cout << "NewDICOMDIR contains no Patient ?!?" << std::endl;
211       delete dicomdir;
212       delete d2;
213       return 1;
214    }
215    
216    while ( pa1 && pa2 ) 
217    {  // we process all the PATIENT of this DICOMDIR
218
219       if ( CompareSQItem(pa2,pa1) == 1 )
220       {
221          delete dicomdir;
222          delete d2;
223          return 1;
224       }
225   
226       // just to allow human reader to be sure ...
227       std::cout << pa2->GetEntryValue(0x0010, 0x0010) 
228                 << std::endl; // Patient's Name
229  
230       st1 = pa1->GetFirstStudy();
231       st2 = pa2->GetFirstStudy();
232
233       while ( st1 && st2 )   
234       {
235          if ( CompareSQItem(st2,st1) == 1 )
236          {
237             delete dicomdir;
238             delete d2;
239             return 1;
240          }
241
242          // just to allow human reader to be sure ...
243          std::cout << "--- "<< st2->GetEntryValue(0x0008, 0x1030);
244          // << std::endl;    // Study Description
245          std::cout << " Stud.ID:["          
246                    << st2->GetEntryValue(0x0020, 0x0010)
247                    << "]" << std::endl; // Study ID
248   
249          se1 = st1->GetFirstSerie();
250          se2 = st2->GetFirstSerie();
251
252          while ( se1 && se2 ) 
253          { // we process all the SERIE of this study
254             if ( CompareSQItem(se2,se1) == 1 )
255               return 1; 
256             std::cout << "--- --- " << se2->GetEntryValue(0x0008, 0x103e);      // Serie Description
257             std::cout << " Ser.nb:["<< se2->GetEntryValue(0x0020, 0x0011);        // Series number
258             std::cout << "] Mod.:[" << se2->GetEntryValue(0x0008, 0x0060) << "]" << std::endl; // Modality
259             im1 = se1->GetFirstImage();
260             im2 = se2->GetFirstImage();
261
262             while ( im1 && im2 ) // we process all the IMAGE of this serie
263             {
264                if ( CompareSQItem(im2,im1) == 1 )
265                {
266                   delete dicomdir;
267                   delete d2;
268                   return 1; 
269                }
270
271                im1 = se1->GetNextImage();   
272                im2 = se2->GetNextImage();   
273             }
274             se1 = st1->GetNextSerie();   
275             se2 = st2->GetNextSerie();   
276          }
277          st1 = pa1->GetNextStudy();
278          st2 = pa2->GetNextStudy();
279       }
280       pa1 = dicomdir->GetNextPatient();
281       pa2 = dicomdir->GetNextPatient();
282    }
283    
284    std::cout << std::flush;
285    delete dicomdir;
286    delete d2;
287
288    return 0;
289 }