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