1 /*=========================================================================
4 Module: $RCSfile: TestDicomDir.cxx,v $
6 Date: $Date: 2006/05/31 16:51:41 $
7 Version: $Revision: 1.43 $
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::SQItem *pa1, gdcm::SQItem *pa2 )
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::DataEntry *>(e1) ||
54 !dynamic_cast<gdcm::DataEntry *>(e2) )
57 // a value is read as GDCM_UNFOUND
58 if ( ((gdcm::DataEntry *)e1)->GetString() == gdcm::GDCM_UNFOUND )
60 std::cout << "for gdcm source DicomDir : element (" << std::hex
61 << e1->GetGroup() << "," <<e1->GetElement()
62 << ") has values [" << gdcm::GDCM_UNFOUND << "]"
67 // values differ in source file and destination file
68 if ( ((gdcm::DataEntry *)e1)->GetString() !=
69 ((gdcm::DataEntry *)e2)->GetString() )
72 // serious trouble : values differ in source and destination file
73 std::cout << "for gdcm DicomDir element (" << std::hex
74 << e2->GetGroup() << "," <<e2->GetElement()
75 << ") values differ ["
76 << ((gdcm::DataEntry *)e1)->GetString() << "] != ["
77 << ((gdcm::DataEntry *)e2)->GetString() << "]"
85 int TestDicomDir(int argc, char *argv[])
87 gdcm::DicomDir *dicomdir;
89 gdcm::DicomDirPatient *pa1;
90 gdcm::DicomDirStudy *st1;
91 gdcm::DicomDirSerie *se1;
92 gdcm::DicomDirImage *im1;
101 file += GDCM_DATA_ROOT;
105 dicomdir = gdcm::DicomDir::New();
106 dicomdir->SetFileName(file);
110 int level = atoi(argv[2]);
111 dicomdir->SetPrintLevel(level);
114 // Test if the DicomDir is readable
115 if( !dicomdir->IsReadable() )
117 std::cout<<" DicomDir '"<<file
118 <<"' is not readable"<<std::endl
119 <<" ...Failed"<<std::endl;
126 std::cout<<" DicomDir '"<<file
127 <<"' is readable"<<std::endl;
130 // Test if the DicomDir contains any patient
131 if( !dicomdir->GetFirstPatient() )
133 std::cout<<" DicomDir '"<<file
134 <<" has no patient"<<std::endl
135 <<" ...Failed"<<std::endl;
141 // step by step structure full exploitation
142 std::cout << std::endl << std::endl
143 << " = PATIENT/STUDY/SERIE/IMAGE List ============================"
144 << std::endl<< std::endl;
146 pa1 = dicomdir->GetFirstPatient();
148 { // we process all the PATIENT of this DICOMDIR
149 std::cout << pa1->GetEntryString(0x0010, 0x0010) << std::endl; // Patient's Name
151 st1 = pa1->GetFirstStudy();
153 { // we process all the STUDY of this patient
154 std::cout << "--- "<< st1->GetEntryString(0x0008, 0x1030) // Study Description
156 std::cout << " Stud.ID:[" << st1->GetEntryString(0x0020, 0x0010) // Study ID
159 se1 = st1->GetFirstSerie();
161 { // we process all the SERIES of this study
162 std::cout << "--- --- "<< se1->GetEntryString(0x0008, 0x103e) << std::endl; // Serie Description
163 std::cout << " Ser.nb:[" << se1->GetEntryString(0x0020, 0x0011); // Series number
164 std::cout << "] Mod.:[" << se1->GetEntryString(0x0008, 0x0060) << "]"; // Modality
166 im1 = se1->GetFirstImage();
167 while ( im1 ) { // we process all the IMAGE of this serie
168 std::cout << "--- --- --- "<< im1->GetEntryString(0x0004, 0x1500) << std::endl; // File name
169 im1 = se1->GetNextImage();
171 se1 = st1->GetNextSerie();
173 st1 = pa1->GetNextStudy();
175 pa1 = dicomdir->GetNextPatient();
178 std::cout << std::endl << std::endl
179 << " = DICOMDIR full content ===================================="
180 << std::endl<< std::endl;
181 // dicomdir->Print();
183 // ------------------------- second stage ---------------------------
185 // Write on disc what we read
186 dicomdir->Write("NewDICOMDIR");
188 std::cout << std::endl << std::endl
189 << "NewDICOMDIR written on disc ================================="
190 << std::endl<< std::endl;
191 // Read what we wrote
192 gdcm::DicomDir *d2 = gdcm::DicomDir::New();
193 d2->SetFileName("NewDICOMDIR");
195 if (!d2->IsReadable())
197 std::cout << std::endl << std::endl
198 << "Read NewDicomDir from disc failed ========================"
199 << std::endl<< std::endl;
202 std::cout << std::endl << std::endl
203 << "NewDICOMDIR successfully read from disc ================================="
204 << std::endl<< std::endl;
206 gdcm::DicomDirPatient *pa2;
207 gdcm::DicomDirStudy *st2;
208 gdcm::DicomDirSerie *se2;
209 gdcm::DicomDirImage *im2;
211 pa1 = dicomdir->GetFirstPatient();
212 pa2 = d2->GetFirstPatient();
216 std::cout << "NewDICOMDIR contains no Patient ?!?" << std::endl;
223 { // we process all the PATIENT of this DICOMDIR
225 if ( CompareSQItem(pa2,pa1) == 1 )
232 // just to allow human reader to be sure ...
233 std::cout << pa2->GetEntryString(0x0010, 0x0010)
234 << std::endl; // Patient's Name
236 st1 = pa1->GetFirstStudy();
237 st2 = pa2->GetFirstStudy();
241 if ( CompareSQItem(st2,st1) == 1 )
248 // just to allow human reader to be sure ...
249 std::cout << "--- "<< st2->GetEntryString(0x0008, 0x1030);
250 // << std::endl; // Study Description
251 std::cout << " Stud.ID:["
252 << st2->GetEntryString(0x0020, 0x0010)
253 << "]" << std::endl; // Study ID
255 se1 = st1->GetFirstSerie();
256 se2 = st2->GetFirstSerie();
259 { // we process all the SERIE of this study
260 if ( CompareSQItem(se2,se1) == 1 )
263 std::cout << "--- --- " << se2->GetEntryString(0x0008, 0x103e); // Serie Description
264 std::cout << " Ser.nb:["<< se2->GetEntryString(0x0020, 0x0011); // Series number
265 std::cout << "] Mod.:[" << se2->GetEntryString(0x0008, 0x0060) << "]" << std::endl; // Modality
266 im1 = se1->GetFirstImage();
267 im2 = se2->GetFirstImage();
269 while ( im1 && im2 ) // we process all the IMAGE of this serie
271 if ( CompareSQItem(im2,im1) == 1 )
278 im1 = se1->GetNextImage();
279 im2 = se2->GetNextImage();
281 se1 = st1->GetNextSerie();
282 se2 = st2->GetNextSerie();
284 st1 = pa1->GetNextStudy();
285 st2 = pa2->GetNextStudy();
287 pa1 = dicomdir->GetNextPatient();
288 pa2 = dicomdir->GetNextPatient();
291 std::cout << std::flush;