]> Creatis software - gdcm.git/blob - Testing/TestBuildUpDicomDir.cxx
Replace stupid method names :
[gdcm.git] / Testing / TestBuildUpDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestBuildUpDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/08/31 09:29:11 $
7   Version:   $Revision: 1.4 $
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 ";
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->SetValEntry("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->SetValEntry("StudyDescrOne.One_",0x0008, 0x1030);
75    // we know entry (0008,1060) is not yet created
76    s11->InsertValEntry("Dr Mabuse",     0x0008, 0x1060);
77    // fill here other Study characteristics
78
79    gdcm::DicomDirStudy *s12 = p1->NewStudy();    
80    s12->SetValEntry("StudyDescrOne.Two",0x0008, 0x1030);
81    s12->InsertValEntry("Dr Zorglub",    0x0008, 0x1060);
82    // fill here other Study characteristics
83
84    gdcm::DicomDirStudy *s13 = p1->NewStudy();  
85    s13->SetValEntry("StudyDescrOne.Tree",0x0008, 0x1030);
86    s13->InsertValEntry("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->SetValEntry("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->SetValEntry("imageFileName1111",0x0004,0x1500);
116
117    gdcm::DicomDirImage *s1112 = s111->NewImage();
118    s1112->SetValEntry("imageFileName1112",0x0004,0x1500);
119
120    // Create patient TWO
121    // ------------------
122    gdcm::DicomDirPatient *p2 = dcmdir->NewPatient();
123    p2->SetValEntry("patientTWO",0x0010, 0x0010); 
124    // fill here other patient characteristics
125     
126    gdcm::DicomDirStudy *s21 = p2->NewStudy();  
127    s21->SetValEntry("StudyDescrTwo.One",0x0008, 0x1030);        
128    // fill here other Study characteristics
129
130    gdcm::DicomDirSerie *s211 = s21->NewSerie();
131    s111->SetValEntry("01-01-211", 0x0008, 0x0021);
132    // fill here other Serie characteristics
133
134    gdcm::DicomDirImage *s2111 = s211->NewImage();
135    s2111->SetValEntry("imageFileName2111",0x0004,0x1500);
136    // fill here other Image characteristics
137
138    gdcm::DicomDirImage *s2112 = s211->NewImage();
139    s2112->SetValEntry("imageFileName1122",0x0004,0x1500);
140    // fill here other Image characteristics
141
142    // Create patient TREE
143    // -------------------
144    gdcm::DicomDirPatient *p3 = dcmdir->NewPatient();
145    p3->SetValEntry("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->SetValEntry("01-01-131", 0x0008, 0x0021);
152    // fill here other Serie characteristics
153
154    gdcm::DicomDirImage *s1311 = s131->NewImage();
155    s1311->SetValEntry("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    delete dcmdir;
186
187    // Read the newly written DicomDir
188    gdcm::DicomDir *newDicomDir = new gdcm::DicomDir();
189    newDicomDir->SetFileName("NewDICOMDIR");
190    newDicomDir->Load( );
191    if( !newDicomDir->IsReadable() )
192    {
193       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
194                <<" is not readable"<<std::endl
195                <<"          ...Failed"<<std::endl;
196
197       delete newDicomDir;
198       return 1;
199    }
200    // Check some value we are sure
201    p1 = newDicomDir->GetFirstPatient();
202    p2 = newDicomDir->GetNextPatient();
203    p3 = newDicomDir->GetNextPatient();
204
205    if (!p1 || !p2 || !p3)
206    {
207       std::cout << "A patient is missing in written DicomDir"
208           << std::endl;
209       delete newDicomDir;
210       return 1;
211    }
212
213    std::cout <<std::endl
214              << "----------Final Check ---------------------" 
215              <<std::endl;
216  
217    bool errorFound = false; 
218    std::string valueStuff;  
219    for (;;) // exit on 'break'
220    {
221       if ( p1->GetEntryValue(0x0010, 0x0010) != "patientONE" )
222       {
223          std::cout << "0x0010,0x0010 [" 
224                    << p1->GetEntryValue(0x0010, 0x0010)
225                    << "]" << std::endl;
226          errorFound = true;
227          break;
228       }
229       std::cout << "Patient : [" 
230                 << p1->GetEntryValue(0x0010, 0x0010)
231                 << "]" << std::endl;
232
233       if ( !(s11 = p1->GetFirstStudy()) )
234       {
235          std::cout << "missing first Study Patient One" << std::endl;  
236          errorFound = true;
237          break;
238       }
239       valueStuff = s11->GetEntryValue(0x0008, 0x1030);
240       if ( valueStuff.find("StudyDescrOne.One_") >= valueStuff.length() )
241       {
242          std::cout << "1 : 0x0008,0x1030 [" 
243                    << valueStuff
244                    << "]" << std::endl;
245          errorFound = true;
246          break;
247       }
248       std::cout << "Study : [" 
249                 << valueStuff
250                 << "]" << std::endl;
251  
252       valueStuff = s11->GetEntryValue(0x0008, 0x1060);
253       if (!gdcm::Util::DicomStringEqual(valueStuff, "Dr Mabuse") )
254       {
255          std::cout << "2 : 0x0008,0x1060 [" 
256                    << s11->GetEntryValue(0x0008,0x1060)
257                    << "]" << std::endl;
258          errorFound = true;
259          break;
260       }
261       std::cout << "Pysician : [" 
262                 << valueStuff
263                 << "]" << std::endl;
264       if ( (s12 = p1->GetNextStudy()) == 0 )
265       {
266          errorFound = true;
267          break;
268       }
269       if ( gdcm::Util::DicomStringEqual(s12->GetEntryValue(0x0008,
270                                            0x1030),"StudyDescrOne.Two " ))
271       {
272          std::cout << "3 0x0008,0x1030 [" 
273                    << s12->GetEntryValue(0x0008,0x1030)
274                    << "]" << std::endl;
275          errorFound = true;
276          break;
277       }
278       std::cout << "Study Descr : [" 
279                 << s12->GetEntryValue(0x0008,0x1030)
280                 << "]" << std::endl;
281
282       if ( gdcm::Util::DicomStringEqual(s12->GetEntryValue(0x0008,
283                                            0x1060),"Dr Zorglub " ))
284       {
285          std::cout << "4 0x0008,0x1060 [" 
286                    << s12->GetEntryValue(0x0008,0x1060)
287                    << "]" << std::endl;
288          errorFound = true;
289          break;
290       }
291       std::cout << "Pysician : [" 
292                 << s12->GetEntryValue(0x0008,0x1060)
293                 << "]" << std::endl;
294
295       if ( (s13 = p1->GetNextStudy()) == 0 )
296       {
297          std::cout << "Study StudyDescrOne.Tree missing" << std::endl;
298          break;
299       }         
300       if ( s13->GetEntryValue(0x0008, 0x1030) != "StudyDescrOne.Tree" )
301       {
302          errorFound = true;
303          break;
304       }
305       std::cout << "Study : [" 
306                 << valueStuff
307                 << "]" << std::endl;
308
309       valueStuff = s13->GetEntryValue(0x0008, 0x1060);
310       if (!gdcm::Util::DicomStringEqual(valueStuff, "Dr Follamour") )
311       {
312          std::cout << "5 0x0008,0x1060 [" 
313                    << valueStuff
314                    << "]" << std::endl;
315          errorFound = true;
316          break;
317       }
318       std::cout << "Pysician : [" 
319                 << valueStuff
320                 << "]" << std::endl;
321
322       if ((s111 = s11->GetFirstSerie()) == 0 )
323       {
324          std::cout << "Serie 01-01-111 missing" << std::endl;
325          errorFound = true;
326          break;
327       }
328
329       valueStuff = s111->GetEntryValue(0x0008, 0x0021);
330       if (!gdcm::Util::DicomStringEqual(valueStuff, "01-01-131") )
331       {
332          std::cout << "6 0x0008,0x0021 [" 
333                    << valueStuff
334                    << "]" << std::endl;
335          errorFound = true;
336          break;
337       }
338       std::cout << "Serie : [" 
339                 << valueStuff
340                 << "]" << std::endl;
341
342       if ( (s1111 = s111->GetFirstImage()) == 0 )
343       {
344          std::cout << "missing image S1111" << std::endl;
345          errorFound = true;
346          break;
347       } 
348 /*
349       if ( s1111->GetEntryValue(0x0004,0x1500) != "imageFileName1111 " )
350       {
351          errorFound = true;
352          break;
353       }
354 */
355       if ( (s1112 = s111->GetNextImage()) == 0 )
356       {
357          std::cout << "missing image S1112" << std::endl;
358          errorFound = true;
359          break;
360       }
361 /*
362       if ( s1112->GetEntryValue(0x0004,0x1500) != "imageFileName1112 " )
363       {
364          errorFound = true;
365          break;
366       }
367   */
368      break; // No error found. Stop looping
369    }
370
371    delete newDicomDir;
372    if ( errorFound )
373    {
374       std::cout << "MissWritting / MissReading " << std::endl;
375       std::cout<<std::flush;
376       return(1);
377    }
378
379    std::cout<<std::flush;
380    return 0;
381 }