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