]> Creatis software - gdcm.git/blob - Testing/TestDicomDir.cxx
Fix mistypings
[gdcm.git] / Testing / TestDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/09/15 15:49:21 $
7   Version:   $Revision: 1.47 $
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_NAME_SPACE::DocEntry)
33 // of this GDCM_NAME_SPACE::DicomDirObject
34 int CompareSQItem(GDCM_NAME_SPACE::SQItem *pa1, GDCM_NAME_SPACE::SQItem *pa2 )
35 {
36    GDCM_NAME_SPACE::DocEntry *e1;
37    GDCM_NAME_SPACE::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_NAME_SPACE::DataEntry *>(e1) ||
54            !dynamic_cast<GDCM_NAME_SPACE::DataEntry *>(e2) )
55          continue;
56
57       // a value is read as GDCM_UNFOUND 
58       if ( ((GDCM_NAME_SPACE::DataEntry *)e1)->GetString() == GDCM_NAME_SPACE::GDCM_UNFOUND )
59       {
60          std::cout << "for gdcm source DicomDir : element (" << std::hex 
61                    << e1->GetGroup() << "," <<e1->GetElement() 
62                    << ") has values [" << GDCM_NAME_SPACE::GDCM_UNFOUND << "]"
63                    << std::endl;
64          return 1;
65       }
66
67       // values differ in source file and destination file
68       if ( ((GDCM_NAME_SPACE::DataEntry *)e1)->GetString() != 
69            ((GDCM_NAME_SPACE::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_NAME_SPACE::DataEntry *)e1)->GetString() << "] != [" 
80                    << ((GDCM_NAME_SPACE::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_NAME_SPACE::DicomDir *dicomdir;
91    
92    GDCM_NAME_SPACE::DicomDirPatient *pa1;
93    GDCM_NAME_SPACE::DicomDirStudy   *st1;
94    GDCM_NAME_SPACE::DicomDirSerie   *se1;
95    GDCM_NAME_SPACE::DicomDirImage   *im1;
96
97    GDCM_NAME_SPACE::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    std::cout << "DicomDir we're going to deal with : ["<< file << "]" 
109              << std::endl;
110
111    dicomdir = GDCM_NAME_SPACE::DicomDir::New();
112    dicomdir->SetFileName(file);
113    dicomdir->Load();
114    if (argc > 2) 
115    {
116       int level = atoi(argv[2]);   
117       dicomdir->SetPrintLevel(level);
118    }
119
120    // Test if the DICOMDIR file is readable
121    if( !dicomdir->IsReadable() )
122    {
123       std::cout<<"          DicomDir '"<<file
124                <<"' is not readable"<<std::endl
125                <<"          ...Failed"<<std::endl;
126
127       dicomdir->Delete();
128       return 1;
129    }
130    else
131    {
132       std::cout<<"          DicomDir '"<<file
133                <<"' is readable"<<std::endl;
134    }
135
136    // Test if the GDCM_NAME_SPACE::DicomDir contains any patient
137    if( !dicomdir->GetFirstPatient() )
138    {
139       std::cout<<"          DicomDir '"<<file
140                <<" has no patient"<<std::endl
141                <<"          ...Failed"<<std::endl;
142
143       dicomdir->Delete();
144       return 1;
145    }
146
147    // step by step structure full exploitation
148    std::cout << std::endl << std::endl  
149              << " = PATIENT/STUDY/SERIE/IMAGE List ============================" 
150              << std::endl<< std::endl;
151
152    pa1 = dicomdir->GetFirstPatient(); 
153    while ( pa1 ) 
154    {  // we process all the PATIENT of this GDCM_NAME_SPACE::DicomDir 
155       std::cout << pa1->GetEntryString(0x0010, 0x0010) << std::endl; // Patient's Name
156
157       st1 = pa1->GetFirstStudy();
158       while ( st1 ) 
159       { // we process all the STUDY of this patient
160          std::cout << "--- "<< st1->GetEntryString(0x0008, 0x1030) // Study Description
161          << std::endl;  
162          std::cout << " Stud.ID:[" << st1->GetEntryString(0x0020, 0x0010) // Study ID
163          << "]"; 
164
165          se1 = st1->GetFirstSerie();
166          while ( se1 ) 
167          { // we process all the SERIES of this study
168             std::cout << "--- --- "<< se1->GetEntryString(0x0008, 0x103e) << std::endl;      // Serie Description
169             std::cout << " Ser.nb:["         <<  se1->GetEntryString(0x0020, 0x0011);        // Series number
170             std::cout << "] Mod.:["          <<  se1->GetEntryString(0x0008, 0x0060) << "]"; // Modality
171
172             im1 = se1->GetFirstImage();
173             while ( im1 ) { // we process all the IMAGE of this serie
174                std::cout << "--- --- --- "<< im1->GetEntryString(0x0004, 0x1500) << std::endl; // File name
175                im1 = se1->GetNextImage();   
176             }
177             se1 = st1->GetNextSerie();   
178          }
179          st1 = pa1->GetNextStudy();
180       } 
181       pa1 = dicomdir->GetNextPatient();
182    }  
183
184 /*
185 // this one is *really* too much verbose!
186
187    std::cout << std::endl << std::endl  
188              << " = DICOMDIR full content ====================================" 
189              << std::endl<< std::endl;
190    dicomdir->Print();
191    std::cout << std::endl << std::endl  
192              << " = end of DICOMDIR full content ====================================" 
193              << std::endl<< std::endl;
194 */
195    // ------------------------- second stage ---------------------------
196  
197  
198  GDCM_NAME_SPACE::Debug::DebugOn();
199  
200     
201    // Write on disc what we read
202    dicomdir->Write("NewDICOMDIR");
203
204    std::cout << std::endl << std::endl  
205              << "NewDICOMDIR written on disc =================================" 
206              << std::endl<< std::endl;
207   // Read what we wrote  
208    GDCM_NAME_SPACE::DicomDir *d2 = GDCM_NAME_SPACE::DicomDir::New();
209    d2->SetFileName("NewDICOMDIR");
210    d2->Load();
211    if (!d2->IsReadable())
212    {
213       std::cout << std::endl << std::endl  
214                 << "Read NewDicomDir from disc failed ========================" 
215                 << std::endl<< std::endl;
216       return 1;
217    }
218    std::cout << std::endl << std::endl  
219              << "NewDICOMDIR successfully read from disc =================================" 
220              << std::endl<< std::endl;
221   
222    GDCM_NAME_SPACE::DicomDirPatient *pa2;
223    GDCM_NAME_SPACE::DicomDirStudy   *st2;
224    GDCM_NAME_SPACE::DicomDirSerie   *se2;
225    GDCM_NAME_SPACE::DicomDirImage   *im2;
226
227    pa1 = dicomdir->GetFirstPatient(); 
228    pa2 = d2->GetFirstPatient(); 
229
230    if (!d2)
231    {
232       std::cout << "NewDICOMDIR contains no Patient ?!?" << std::endl;
233       dicomdir->Delete();
234       d2->Delete();
235       return 1;
236    }
237    
238    while ( pa1 && pa2 ) 
239    {  // we process all the PATIENT of this DICOMDIR
240
241       if ( CompareSQItem(pa2,pa1) == 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 << pa2->GetEntryString(0x0010, 0x0010) 
250                 << std::endl; // Patient's Name
251  
252       st1 = pa1->GetFirstStudy();
253       st2 = pa2->GetFirstStudy();
254
255       while ( st1 && st2 )   
256       {
257          if ( CompareSQItem(st2,st1) == 1 )
258          {
259             dicomdir->Delete();
260             d2->Delete();
261             return 1;
262          }
263
264          // just to allow human reader to be sure ...
265          std::cout << "--- "<< st2->GetEntryString(0x0008, 0x1030);
266          // << std::endl;    // Study Description
267          std::cout << " Stud.ID:["          
268                    << st2->GetEntryString(0x0020, 0x0010)
269                    << "]" << std::endl; // Study ID
270   
271          se1 = st1->GetFirstSerie();
272          se2 = st2->GetFirstSerie();
273
274          while ( se1 && se2 ) 
275          { // we process all the SERIE of this study
276             if ( CompareSQItem(se2,se1) == 1 )
277               return 1;
278
279             std::cout << "--- --- " << se2->GetEntryString(0x0008, 0x103e);      // Serie Description
280             std::cout << " Ser.nb:["<< se2->GetEntryString(0x0020, 0x0011);        // Series number
281             std::cout << "] Mod.:[" << se2->GetEntryString(0x0008, 0x0060) << "]" << std::endl; // Modality
282             im1 = se1->GetFirstImage();
283             im2 = se2->GetFirstImage();
284
285             while ( im1 && im2 ) // we process all the IMAGE of this serie
286             {
287                if ( CompareSQItem(im2,im1) == 1 )
288                {
289                   dicomdir->Delete();
290                   d2->Delete();
291                   return 1; 
292                }
293
294                im1 = se1->GetNextImage();   
295                im2 = se2->GetNextImage();   
296             }
297             se1 = st1->GetNextSerie();   
298             se2 = st2->GetNextSerie();   
299          }
300          st1 = pa1->GetNextStudy();
301          st2 = pa2->GetNextStudy();
302       }
303       pa1 = dicomdir->GetNextPatient();
304       pa2 = dicomdir->GetNextPatient();
305    }
306    
307    std::cout << std::flush;
308    dicomdir->Delete();
309    d2->Delete();
310
311    return 0;
312 }