]> Creatis software - gdcm.git/blob - Testing/TestBuildUpDicomDir.cxx
* Remove memory leaks
[gdcm.git] / Testing / TestBuildUpDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestBuildUpDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/19 13:15:36 $
7   Version:   $Revision: 1.6 $
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 "gdcmDicomDir.h"
20 #include "gdcmDicomDirPatient.h"
21 #include "gdcmDicomDirStudy.h"
22 #include "gdcmDicomDirSerie.h"
23 #include "gdcmDicomDirImage.h"
24 #include "gdcmDirList.h"
25 #include "gdcmDebug.h"
26 #include "gdcmUtil.h"
27
28 // ===============================================================
29
30 /**
31   * \brief   Builds up ex-nihilo a DICOMDIR file 
32   *          adding Patient, Study, Serie, Image descriptions
33   *          to an empty gdcmDicomDir occurence
34   *          and writes a file named NewDICOMDIR. 
35   */  
36 int TestBuildUpDicomDir(int argc, char *argv[])
37 {
38    if (argc) 
39    {
40       std::cerr << "Usage: " << argv[0] << " dummy " << std::endl;
41    }
42
43    gdcm::DicomDir *dcmdir;
44    std::string dirName;  
45
46    dcmdir = new gdcm::DicomDir();
47
48    gdcm::DicomDirPatient *p1;
49    // --- Forget these 4 lines :
50    // just to improve test coverage.
51    p1=dcmdir->GetFirstPatient();
52    if (!p1)
53       std::cout << "BEFORE any Patient creation, a DicomDir has no Patient. Pffff"
54                 << std::endl;
55    // --- end forget
56
57    // Create patient ONE
58    // ------------------
59    p1 = dcmdir->NewPatient();
60    p1->SetEntryString("patientONE",0x0010, 0x0010);
61    // fill here other patient characteristics
62
63    gdcm::DicomDirStudy *s11;
64    // --- Forget these 4 lines :
65    // just to improve test coverage.
66    s11=p1->GetFirstStudy();
67    if (!s11)
68       std::cout << "BEFORE any Study creation, a Patient has no Study. Pffff"
69                 << std::endl;
70    // --- end forget
71
72    // Let's create and add a Study for this Patient
73    s11 = p1->NewStudy();  
74    s11->SetEntryString("StudyDescrOne.One_",0x0008, 0x1030);
75    // we know entry (0008,1060) is not yet created
76    s11->InsertEntryString("Dr Mabuse",     0x0008, 0x1060);
77    // fill here other Study characteristics
78
79    gdcm::DicomDirStudy *s12 = p1->NewStudy();    
80    s12->SetEntryString("StudyDescrOne.Two",0x0008, 0x1030);
81    s12->InsertEntryString("Dr Zorglub",    0x0008, 0x1060);
82    // fill here other Study characteristics
83
84    gdcm::DicomDirStudy *s13 = p1->NewStudy();  
85    s13->SetEntryString("StudyDescrOne.Tree",0x0008, 0x1030);
86    s13->InsertEntryString("Dr Follamour",   0x0008, 0x1060);
87    // fill here other Study characteristics
88  
89    gdcm::DicomDirSerie *s111;
90    // --- Forget these 4 lines :
91    // just to improve test coverage.
92    s111=s11->GetFirstSerie();
93    if (!s111)
94       std::cout << "BEFORE any Serie creation, a Study has no Serie. Pffff"
95                 << std::endl;
96    // --- end forget
97
98    // Let's create and add a Serie for this Study
99    s111 = s11->NewSerie();
100    s111->SetEntryString("01-01-111", 0x0008, 0x0021);
101    // fill here other Serie characteristics
102
103    gdcm::DicomDirImage *s1111;
104
105    // --- Forget these 4 lines :
106    // just to improve test coverage.
107    s1111=s111->GetFirstImage();
108    if (!s1111)
109       std::cout << "BEFORE any Image creation, a Serie has no Image. Pffff"
110                 << std::endl;
111    // --- end forget
112
113    // Let's create and add a Image for this Serie
114    s1111 = s111->NewImage();
115    s1111->SetEntryString("imageFileName1111",0x0004,0x1500);
116
117    gdcm::DicomDirImage *s1112 = s111->NewImage();
118    s1112->SetEntryString("imageFileName1112",0x0004,0x1500);
119
120    // Create patient TWO
121    // ------------------
122    gdcm::DicomDirPatient *p2 = dcmdir->NewPatient();
123    p2->SetEntryString("patientTWO",0x0010, 0x0010); 
124    // fill here other patient characteristics
125     
126    gdcm::DicomDirStudy *s21 = p2->NewStudy();  
127    s21->SetEntryString("StudyDescrTwo.One",0x0008, 0x1030);        
128    // fill here other Study characteristics
129
130    gdcm::DicomDirSerie *s211 = s21->NewSerie();
131    s111->SetEntryString("01-01-211", 0x0008, 0x0021);
132    // fill here other Serie characteristics
133
134    gdcm::DicomDirImage *s2111 = s211->NewImage();
135    s2111->SetEntryString("imageFileName2111",0x0004,0x1500);
136    // fill here other Image characteristics
137
138    gdcm::DicomDirImage *s2112 = s211->NewImage();
139    s2112->SetEntryString("imageFileName1122",0x0004,0x1500);
140    // fill here other Image characteristics
141
142    // Create patient TREE
143    // -------------------
144    gdcm::DicomDirPatient *p3 = dcmdir->NewPatient();
145    p3->SetEntryString("patientTHREE",0x0010, 0x0010);
146    // fill here other Patient characteristics
147
148    // Add a new Serie/Image for a Patient's Study created a long time ago
149    // -------------------------------------------------------------------
150    gdcm::DicomDirSerie *s131 = s13->NewSerie();
151    s111->SetEntryString("01-01-131", 0x0008, 0x0021);
152    // fill here other Serie characteristics
153
154    gdcm::DicomDirImage *s1311 = s131->NewImage();
155    s1311->SetEntryString("imageFileName1311",0x0004,0x1500);
156    // fill here other Image characteristics
157      
158    // Print
159    std::cout << "Test/BuildUpDicomDir: Test Print of patients\n";
160    p1->Print();
161    std::cout << "Test/BuildUpDicomDir: -------------------\n";
162    p2->Print();
163    std::cout << "Test/BuildUpDicomDir: -------------------\n";
164    p3->Print();
165    std::cout << "Test/BuildUpDicomDir: Test Print of patients ended\n";
166   
167    if( !dcmdir->IsReadable() )
168    {
169       std::cout<<"          Created DicomDir "
170                <<" is not readable"<<std::endl
171                <<"          ...Failed"<<std::endl;
172
173       delete dcmdir;
174       return 1;
175    }
176
177
178    // Print the 'in memory' built up DicomDir
179    std::cout << "Test/BuildUpDicomDir: Print all of the DicomDir" << std::endl;
180    dcmdir->SetPrintLevel(-1);
181    dcmdir->Print();
182
183    // Write it on disc
184    dcmdir->Write("NewDICOMDIR");
185
186    delete dcmdir;
187
188    // Read the newly written DicomDir
189    gdcm::DicomDir *newDicomDir = new gdcm::DicomDir();
190    newDicomDir->SetFileName("NewDICOMDIR");
191    newDicomDir->Load( );
192    if( !newDicomDir->IsReadable() )
193    {
194       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
195                <<" is not readable"<<std::endl
196                <<"          ...Failed"<<std::endl;
197
198       delete newDicomDir;
199       return 1;
200    }
201    // Check some value we are sure
202    p1 = newDicomDir->GetFirstPatient();
203    p2 = newDicomDir->GetNextPatient();
204    p3 = newDicomDir->GetNextPatient();
205
206    if (!p1 || !p2 || !p3)
207    {
208       std::cout << "A patient is missing in written DicomDir"
209           << std::endl;
210       delete newDicomDir;
211       return 1;
212    }
213
214    std::cout <<std::endl
215              << "----------Final Check ---------------------" 
216              <<std::endl;
217  
218    bool errorFound = false; 
219    std::string valueStuff;  
220    for (;;) // exit on 'break'
221    {
222       if ( p1->GetEntryString(0x0010, 0x0010) != "patientONE" )
223       {
224          std::cout << "0x0010,0x0010 [" 
225                    << p1->GetEntryString(0x0010, 0x0010)
226                    << "]" << std::endl;
227          errorFound = true;
228          break;
229       }
230       std::cout << "Patient : [" 
231                 << p1->GetEntryString(0x0010, 0x0010)
232                 << "]" << std::endl;
233
234       if ( !(s11 = p1->GetFirstStudy()) )
235       {
236          std::cout << "missing first Study Patient One" << std::endl;  
237          errorFound = true;
238          break;
239       }
240       valueStuff = s11->GetEntryString(0x0008, 0x1030);
241       if ( valueStuff.find("StudyDescrOne.One_") >= valueStuff.length() )
242       {
243          std::cout << "1 : 0x0008,0x1030 [" 
244                    << valueStuff
245                    << "]" << std::endl;
246          errorFound = true;
247          break;
248       }
249       std::cout << "Study : [" 
250                 << valueStuff
251                 << "]" << std::endl;
252  
253       valueStuff = s11->GetEntryString(0x0008, 0x1060);
254       if (!gdcm::Util::DicomStringEqual(valueStuff, "Dr Mabuse") )
255       {
256          std::cout << "2 : 0x0008,0x1060 [" 
257                    << s11->GetEntryString(0x0008,0x1060)
258                    << "]" << std::endl;
259          errorFound = true;
260          break;
261       }
262       std::cout << "Pysician : [" 
263                 << valueStuff
264                 << "]" << std::endl;
265       if ( (s12 = p1->GetNextStudy()) == 0 )
266       {
267          errorFound = true;
268          break;
269       }
270       if ( gdcm::Util::DicomStringEqual(s12->GetEntryString(0x0008,
271                                            0x1030),"StudyDescrOne.Two " ))
272       {
273          std::cout << "3 0x0008,0x1030 [" 
274                    << s12->GetEntryString(0x0008,0x1030)
275                    << "]" << std::endl;
276          errorFound = true;
277          break;
278       }
279       std::cout << "Study Descr : [" 
280                 << s12->GetEntryString(0x0008,0x1030)
281                 << "]" << std::endl;
282
283       if ( gdcm::Util::DicomStringEqual(s12->GetEntryString(0x0008,
284                                            0x1060),"Dr Zorglub " ))
285       {
286          std::cout << "4 0x0008,0x1060 [" 
287                    << s12->GetEntryString(0x0008,0x1060)
288                    << "]" << std::endl;
289          errorFound = true;
290          break;
291       }
292       std::cout << "Pysician : [" 
293                 << s12->GetEntryString(0x0008,0x1060)
294                 << "]" << std::endl;
295
296       if ( (s13 = p1->GetNextStudy()) == 0 )
297       {
298          std::cout << "Study StudyDescrOne.Tree missing" << std::endl;
299          break;
300       }         
301       if ( s13->GetEntryString(0x0008, 0x1030) != "StudyDescrOne.Tree" )
302       {
303          errorFound = true;
304          break;
305       }
306       std::cout << "Study : [" 
307                 << valueStuff
308                 << "]" << std::endl;
309
310       valueStuff = s13->GetEntryString(0x0008, 0x1060);
311       if (!gdcm::Util::DicomStringEqual(valueStuff, "Dr Follamour") )
312       {
313          std::cout << "5 0x0008,0x1060 [" 
314                    << valueStuff
315                    << "]" << std::endl;
316          errorFound = true;
317          break;
318       }
319       std::cout << "Pysician : [" 
320                 << valueStuff
321                 << "]" << std::endl;
322
323       if ((s111 = s11->GetFirstSerie()) == 0 )
324       {
325          std::cout << "Serie 01-01-111 missing" << std::endl;
326          errorFound = true;
327          break;
328       }
329
330       valueStuff = s111->GetEntryString(0x0008, 0x0021);
331       if (!gdcm::Util::DicomStringEqual(valueStuff, "01-01-131") )
332       {
333          std::cout << "6 0x0008,0x0021 [" 
334                    << valueStuff
335                    << "]" << std::endl;
336          errorFound = true;
337          break;
338       }
339       std::cout << "Serie : [" 
340                 << valueStuff
341                 << "]" << std::endl;
342
343       if ( (s1111 = s111->GetFirstImage()) == 0 )
344       {
345          std::cout << "missing image S1111" << std::endl;
346          errorFound = true;
347          break;
348       } 
349
350       if ( (s1112 = s111->GetNextImage()) == 0 )
351       {
352          std::cout << "missing image S1112" << std::endl;
353          errorFound = true;
354          break;
355       }
356
357       break; // No error found. Stop looping
358    }
359
360    delete newDicomDir;
361    if ( errorFound )
362    {
363       std::cout << "MissWritting / MissReading " << std::endl;
364       std::cout<<std::flush;
365       return(1);
366    }
367
368    std::cout<<std::flush;
369    return 0;
370 }