1 /*=========================================================================
4 Module: $RCSfile: TestDicomDir.cxx,v $
6 Date: $Date: 2005/10/25 14:52:30 $
7 Version: $Revision: 1.42 $
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"
30 // check *all* the dicom elements (gdcm::DocEntry)
31 // of this gdcm::DicomDirObject
32 int CompareSQItem(gdcm::SQItem *pa1, gdcm::SQItem *pa2 )
37 e2 = pa2->GetFirstEntry();
40 // locate the corresponding element in 'source' file
41 e1 = pa1->GetDocEntry( e2->GetGroup(),e2->GetElement() );
43 // an element doesn't exist in origin file
46 std::cout << "DicomDir element " << std::hex
47 << e2->GetGroup() << "," <<e2->GetElement() << std::endl;
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) )
55 // a value is read as GDCM_UNFOUND
56 if ( ((gdcm::DataEntry *)e1)->GetString() == gdcm::GDCM_UNFOUND )
58 std::cout << "for gdcm source DicomDir : element (" << std::hex
59 << e1->GetGroup() << "," <<e1->GetElement()
60 << ") has values [" << gdcm::GDCM_UNFOUND << "]"
65 // values differ in source file and destination file
66 if ( ((gdcm::DataEntry *)e1)->GetString() !=
67 ((gdcm::DataEntry *)e2)->GetString() )
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() << "]"
83 int TestDicomDir(int argc, char *argv[])
85 gdcm::DicomDir *dicomdir;
87 gdcm::DicomDirPatient *pa1;
88 gdcm::DicomDirStudy *st1;
89 gdcm::DicomDirSerie *se1;
90 gdcm::DicomDirImage *im1;
99 file += GDCM_DATA_ROOT;
103 dicomdir = gdcm::DicomDir::New();
104 dicomdir->SetFileName(file);
108 int level = atoi(argv[2]);
109 dicomdir->SetPrintLevel(level);
112 // Test if the DicomDir is readable
113 if( !dicomdir->IsReadable() )
115 std::cout<<" DicomDir '"<<file
116 <<"' is not readable"<<std::endl
117 <<" ...Failed"<<std::endl;
124 std::cout<<" DicomDir '"<<file
125 <<"' is readable"<<std::endl;
128 // Test if the DicomDir contains any patient
129 if( !dicomdir->GetFirstPatient() )
131 std::cout<<" DicomDir '"<<file
132 <<" has no patient"<<std::endl
133 <<" ...Failed"<<std::endl;
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;
144 pa1 = dicomdir->GetFirstPatient();
146 { // we process all the PATIENT of this DICOMDIR
147 std::cout << pa1->GetEntryString(0x0010, 0x0010) << std::endl; // Patient's Name
149 st1 = pa1->GetFirstStudy();
151 { // we process all the STUDY of this patient
152 std::cout << "--- "<< st1->GetEntryString(0x0008, 0x1030) // Study Description
154 std::cout << " Stud.ID:[" << st1->GetEntryString(0x0020, 0x0010) // Study ID
157 se1 = st1->GetFirstSerie();
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
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();
169 se1 = st1->GetNextSerie();
171 st1 = pa1->GetNextStudy();
173 pa1 = dicomdir->GetNextPatient();
176 std::cout << std::endl << std::endl
177 << " = DICOMDIR full content ===================================="
178 << std::endl<< std::endl;
179 // dicomdir->Print();
181 // ------------------------- second stage ---------------------------
183 // Write on disc what we read
184 dicomdir->Write("NewDICOMDIR");
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");
193 if (!d2->IsReadable())
195 std::cout << std::endl << std::endl
196 << "Read NewDicomDir from disc failed ========================"
197 << std::endl<< std::endl;
200 std::cout << std::endl << std::endl
201 << "NewDICOMDIR successfully read from disc ================================="
202 << std::endl<< std::endl;
204 gdcm::DicomDirPatient *pa2;
205 gdcm::DicomDirStudy *st2;
206 gdcm::DicomDirSerie *se2;
207 gdcm::DicomDirImage *im2;
209 pa1 = dicomdir->GetFirstPatient();
210 pa2 = d2->GetFirstPatient();
214 std::cout << "NewDICOMDIR contains no Patient ?!?" << std::endl;
221 { // we process all the PATIENT of this DICOMDIR
223 if ( CompareSQItem(pa2,pa1) == 1 )
230 // just to allow human reader to be sure ...
231 std::cout << pa2->GetEntryString(0x0010, 0x0010)
232 << std::endl; // Patient's Name
234 st1 = pa1->GetFirstStudy();
235 st2 = pa2->GetFirstStudy();
239 if ( CompareSQItem(st2,st1) == 1 )
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
253 se1 = st1->GetFirstSerie();
254 se2 = st2->GetFirstSerie();
257 { // we process all the SERIE of this study
258 if ( CompareSQItem(se2,se1) == 1 )
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();
267 while ( im1 && im2 ) // we process all the IMAGE of this serie
269 if ( CompareSQItem(im2,im1) == 1 )
276 im1 = se1->GetNextImage();
277 im2 = se2->GetNextImage();
279 se1 = st1->GetNextSerie();
280 se2 = st2->GetNextSerie();
282 st1 = pa1->GetNextStudy();
283 st2 = pa2->GetNextStudy();
285 pa1 = dicomdir->GetNextPatient();
286 pa2 = dicomdir->GetNextPatient();
289 std::cout << std::flush;