1 /*=========================================================================
4 Module: $RCSfile: TestDicomDir.cxx,v $
6 Date: $Date: 2007/06/21 14:59:06 $
7 Version: $Revision: 1.46 $
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.
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.
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"
32 // check *all* the dicom elements (gdcm::DocEntry)
33 // of this gdcm::DicomDirObject
34 int CompareSQItem(GDCM_NAME_SPACE::SQItem *pa1, GDCM_NAME_SPACE::SQItem *pa2 )
36 GDCM_NAME_SPACE::DocEntry *e1;
37 GDCM_NAME_SPACE::DocEntry *e2;
39 e2 = pa2->GetFirstEntry();
42 // locate the corresponding element in 'source' file
43 e1 = pa1->GetDocEntry( e2->GetGroup(),e2->GetElement() );
45 // an element doesn't exist in origin file
48 std::cout << "DicomDir element " << std::hex
49 << e2->GetGroup() << "," <<e2->GetElement() << std::endl;
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) )
57 // a value is read as GDCM_UNFOUND
58 if ( ((GDCM_NAME_SPACE::DataEntry *)e1)->GetString() == GDCM_NAME_SPACE::GDCM_UNFOUND )
60 std::cout << "for gdcm source DicomDir : element (" << std::hex
61 << e1->GetGroup() << "," <<e1->GetElement()
62 << ") has values [" << GDCM_NAME_SPACE::GDCM_UNFOUND << "]"
67 // values differ in source file and destination file
68 if ( ((GDCM_NAME_SPACE::DataEntry *)e1)->GetString() !=
69 ((GDCM_NAME_SPACE::DataEntry *)e2)->GetString() )
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" ...)
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() << "]"
88 int TestDicomDir(int argc, char *argv[])
90 GDCM_NAME_SPACE::DicomDir *dicomdir;
92 GDCM_NAME_SPACE::DicomDirPatient *pa1;
93 GDCM_NAME_SPACE::DicomDirStudy *st1;
94 GDCM_NAME_SPACE::DicomDirSerie *se1;
95 GDCM_NAME_SPACE::DicomDirImage *im1;
97 GDCM_NAME_SPACE::TSKey v;
104 file += GDCM_DATA_ROOT;
108 std::cout << "DicomDir we're going to deal with : ["<< file << "]"
111 dicomdir = GDCM_NAME_SPACE::DicomDir::New();
112 dicomdir->SetFileName(file);
116 int level = atoi(argv[2]);
117 dicomdir->SetPrintLevel(level);
120 // Test if the DICOMDIR file is readable
121 if( !dicomdir->IsReadable() )
123 std::cout<<" DicomDir '"<<file
124 <<"' is not readable"<<std::endl
125 <<" ...Failed"<<std::endl;
132 std::cout<<" DicomDir '"<<file
133 <<"' is readable"<<std::endl;
136 // Test if the gdcm::DicomDir contains any patient
137 if( !dicomdir->GetFirstPatient() )
139 std::cout<<" DicomDir '"<<file
140 <<" has no patient"<<std::endl
141 <<" ...Failed"<<std::endl;
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;
152 pa1 = dicomdir->GetFirstPatient();
154 { // we process all the PATIENT of this gdcm::DicomDir
155 std::cout << pa1->GetEntryString(0x0010, 0x0010) << std::endl; // Patient's Name
157 st1 = pa1->GetFirstStudy();
159 { // we process all the STUDY of this patient
160 std::cout << "--- "<< st1->GetEntryString(0x0008, 0x1030) // Study Description
162 std::cout << " Stud.ID:[" << st1->GetEntryString(0x0020, 0x0010) // Study ID
165 se1 = st1->GetFirstSerie();
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
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();
177 se1 = st1->GetNextSerie();
179 st1 = pa1->GetNextStudy();
181 pa1 = dicomdir->GetNextPatient();
185 // this one is *really* too much verbose!
187 std::cout << std::endl << std::endl
188 << " = DICOMDIR full content ===================================="
189 << std::endl<< std::endl;
191 std::cout << std::endl << std::endl
192 << " = end of DICOMDIR full content ===================================="
193 << std::endl<< std::endl;
195 // ------------------------- second stage ---------------------------
198 GDCM_NAME_SPACE::Debug::DebugOn();
201 // Write on disc what we read
202 dicomdir->Write("NewDICOMDIR");
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");
211 if (!d2->IsReadable())
213 std::cout << std::endl << std::endl
214 << "Read NewDicomDir from disc failed ========================"
215 << std::endl<< std::endl;
218 std::cout << std::endl << std::endl
219 << "NewDICOMDIR successfully read from disc ================================="
220 << std::endl<< std::endl;
222 GDCM_NAME_SPACE::DicomDirPatient *pa2;
223 GDCM_NAME_SPACE::DicomDirStudy *st2;
224 GDCM_NAME_SPACE::DicomDirSerie *se2;
225 GDCM_NAME_SPACE::DicomDirImage *im2;
227 pa1 = dicomdir->GetFirstPatient();
228 pa2 = d2->GetFirstPatient();
232 std::cout << "NewDICOMDIR contains no Patient ?!?" << std::endl;
239 { // we process all the PATIENT of this DICOMDIR
241 if ( CompareSQItem(pa2,pa1) == 1 )
248 // just to allow human reader to be sure ...
249 std::cout << pa2->GetEntryString(0x0010, 0x0010)
250 << std::endl; // Patient's Name
252 st1 = pa1->GetFirstStudy();
253 st2 = pa2->GetFirstStudy();
257 if ( CompareSQItem(st2,st1) == 1 )
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
271 se1 = st1->GetFirstSerie();
272 se2 = st2->GetFirstSerie();
275 { // we process all the SERIE of this study
276 if ( CompareSQItem(se2,se1) == 1 )
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();
285 while ( im1 && im2 ) // we process all the IMAGE of this serie
287 if ( CompareSQItem(im2,im1) == 1 )
294 im1 = se1->GetNextImage();
295 im2 = se2->GetNextImage();
297 se1 = st1->GetNextSerie();
298 se2 = st2->GetNextSerie();
300 st1 = pa1->GetNextStudy();
301 st2 = pa2->GetNextStudy();
303 pa1 = dicomdir->GetNextPatient();
304 pa2 = dicomdir->GetNextPatient();
307 std::cout << std::flush;