]> Creatis software - gdcm.git/blob - Testing/TestBuildUpDicomDir.cxx
Forget to Black list : MR_Philips_Intera_PrivateSequenceImplicitVR.dcm
[gdcm.git] / Testing / TestBuildUpDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestBuildUpDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/25 14:52:30 $
7   Version:   $Revision: 1.8 $
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    bool errorFound = false; 
44    gdcm::DicomDir *dcmdir;
45    std::string dirName;  
46
47    dcmdir = gdcm::DicomDir::New();
48
49    gdcm::DicomDirPatient *p1;
50    // --- Forget these 4 lines :
51    // just to improve test coverage.
52    p1=dcmdir->GetFirstPatient();
53    if (!p1)
54       std::cout << "BEFORE any Patient creation, a DicomDir has no Patient. Pffff"
55                 << std::endl;
56    // --- end forget
57
58    // Create patient ONE
59    // ------------------
60    p1 = dcmdir->NewPatient();
61    p1->SetEntryString("patientONE",0x0010, 0x0010);
62    // fill here other patient characteristics
63
64    gdcm::DicomDirStudy *s11;
65    // --- Forget these 4 lines :
66    // just to improve test coverage.
67    s11=p1->GetFirstStudy();
68    if (!s11)
69       std::cout << "BEFORE any Study creation, a Patient has no Study. Pffff"
70                 << std::endl;
71    // --- end forget
72
73    // Let's create and add a Study for this Patient
74    s11 = p1->NewStudy();  
75    s11->SetEntryString("StudyDescrOne.One_",0x0008, 0x1030);
76    // we know entry (0008,1060) is not yet created
77    s11->InsertEntryString("Dr Mabuse",     0x0008, 0x1060);
78    // fill here other Study characteristics
79
80    gdcm::DicomDirStudy *s12 = p1->NewStudy();    
81    s12->SetEntryString("StudyDescrOne.Two",0x0008, 0x1030);
82    s12->InsertEntryString("Dr Zorglub",    0x0008, 0x1060);
83    // fill here other Study characteristics
84
85    gdcm::DicomDirStudy *s13 = p1->NewStudy();  
86    s13->SetEntryString("StudyDescrOne.Tree",0x0008, 0x1030);
87    s13->InsertEntryString("Dr Follamour",   0x0008, 0x1060);
88    // fill here other Study characteristics
89  
90    gdcm::DicomDirSerie *s111;
91    // --- Forget these 4 lines :
92    // just to improve test coverage.
93    s111=s11->GetFirstSerie();
94    if (!s111)
95       std::cout << "BEFORE any Serie creation, a Study has no Serie. Pffff"
96                 << std::endl;
97    // --- end forget
98
99    // Let's create and add a Serie for this Study
100    s111 = s11->NewSerie();
101    s111->SetEntryString("01-01-111", 0x0008, 0x0021);
102    // fill here other Serie characteristics
103
104    gdcm::DicomDirImage *s1111;
105
106    // --- Forget these 4 lines :
107    // just to improve test coverage.
108    s1111=s111->GetFirstImage();
109    if (!s1111)
110       std::cout << "BEFORE any Image creation, a Serie has no Image. Pffff"
111                 << std::endl;
112    // --- end forget
113
114    // Let's create and add a Image for this Serie
115    s1111 = s111->NewImage();
116    s1111->SetEntryString("imageFileName1111",0x0004,0x1500);
117
118    gdcm::DicomDirImage *s1112 = s111->NewImage();
119    s1112->SetEntryString("imageFileName1112",0x0004,0x1500);
120
121    // Create patient TWO
122    // ------------------
123    gdcm::DicomDirPatient *p2 = dcmdir->NewPatient();
124    p2->SetEntryString("patientTWO",0x0010, 0x0010); 
125    // fill here other patient characteristics
126     
127    gdcm::DicomDirStudy *s21 = p2->NewStudy();  
128    s21->SetEntryString("StudyDescrTwo.One",0x0008, 0x1030);        
129    // fill here other Study characteristics
130
131    gdcm::DicomDirSerie *s211 = s21->NewSerie();
132    s111->SetEntryString("01-01-211", 0x0008, 0x0021);
133    // fill here other Serie characteristics
134
135    gdcm::DicomDirImage *s2111 = s211->NewImage();
136    s2111->SetEntryString("imageFileName2111",0x0004,0x1500);
137    // fill here other Image characteristics
138
139    gdcm::DicomDirImage *s2112 = s211->NewImage();
140    s2112->SetEntryString("imageFileName1122",0x0004,0x1500);
141    // fill here other Image characteristics
142
143    // Create patient TREE
144    // -------------------
145    gdcm::DicomDirPatient *p3 = dcmdir->NewPatient();
146    p3->SetEntryString("patientTHREE",0x0010, 0x0010);
147    // fill here other Patient characteristics
148
149    // Add a new Serie/Image for a Patient's Study created a long time ago
150    // -------------------------------------------------------------------
151    gdcm::DicomDirSerie *s131 = s13->NewSerie();
152    s111->SetEntryString("01-01-131", 0x0008, 0x0021);
153    // fill here other Serie characteristics
154
155    gdcm::DicomDirImage *s1311 = s131->NewImage();
156    s1311->SetEntryString("imageFileName1311",0x0004,0x1500);
157    // fill here other Image characteristics
158      
159    // Print
160    std::cout << "Test/BuildUpDicomDir: Test Print of patients\n";
161    p1->Print();
162    std::cout << "Test/BuildUpDicomDir: -------------------\n";
163    p2->Print();
164    std::cout << "Test/BuildUpDicomDir: -------------------\n";
165    p3->Print();
166    std::cout << "Test/BuildUpDicomDir: Test Print of patients ended\n";
167   
168    if( !dcmdir->IsReadable() )
169    {
170       std::cout<<"          Created DicomDir "
171                <<" is not readable"<<std::endl
172                <<"          ...Failed"<<std::endl;
173
174       dcmdir->Delete();
175       return 1;
176    }
177
178
179    // Print the 'in memory' built up DicomDir
180    std::cout << "Test/BuildUpDicomDir: Print all of the DicomDir" << std::endl;
181    dcmdir->SetPrintLevel(-1);
182    dcmdir->Print();
183
184    // Write it on disc
185    dcmdir->Write("NewDICOMDIR");
186
187    dcmdir->Delete();
188
189    // Read the newly written DicomDir
190    gdcm::DicomDir *newDicomDir = gdcm::DicomDir::New();
191    newDicomDir->SetFileName("NewDICOMDIR");
192    newDicomDir->Load( );
193    if( !newDicomDir->IsReadable() )
194    {
195       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
196                <<" is not readable"<<std::endl
197                <<"          ...Failed"<<std::endl;
198
199       newDicomDir->Delete();
200       return 1;
201    }
202    // Check some value we are sure
203    p1 = newDicomDir->GetFirstPatient();
204    p2 = newDicomDir->GetNextPatient();
205    p3 = newDicomDir->GetNextPatient();
206
207    if (!p1 || !p2 || !p3)
208    {
209       std::cout << "A patient is missing in written DicomDir"
210           << std::endl;
211       newDicomDir->Delete();
212       return 1;
213    }
214
215    std::cout <<std::endl
216              << "----------Final Check ---------------------" 
217              <<std::endl;
218  
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    if ( errorFound )
361    {
362       std::cout << "MissWritting / MissReading " << std::endl;
363    }
364
365    std::cout<<std::flush;
366    newDicomDir->Delete();
367
368    return errorFound;
369 }