]> Creatis software - gdcm.git/blob - Testing/TestBuildUpDicomDir.cxx
Name normalization
[gdcm.git] / Testing / TestBuildUpDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestBuildUpDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/02 10:41:10 $
7   Version:   $Revision: 1.1 $
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->WriteDicomDir("NewDICOMDIR");
185    delete dcmdir;
186
187    // Read the newly written DicomDir
188    gdcm::DicomDir *newDicomDir = new gdcm::DicomDir("NewDICOMDIR");
189    if( !newDicomDir->IsReadable() )
190    {
191       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
192                <<" is not readable"<<std::endl
193                <<"          ...Failed"<<std::endl;
194
195       delete newDicomDir;
196       return 1;
197    }
198    // Check some value we are sure
199    p1 = newDicomDir->GetFirstPatient();
200    p2 = newDicomDir->GetNextPatient();
201    p3 = newDicomDir->GetNextPatient();
202
203    if (!p1 || !p2 || !p3)
204    {
205       std::cout << "A patient is missing in written DicomDir"
206           << std::endl;
207       delete newDicomDir;
208       return 1;
209    }
210
211    std::cout <<std::endl
212              << "----------Final Check ---------------------" 
213              <<std::endl;
214  
215    bool errorFound = false; 
216    std::string valueStuff;  
217    for (;;) // exit on 'break'
218    {
219       if ( p1->GetEntryValue(0x0010, 0x0010) != "patientONE" )
220       {
221          std::cout << "0x0010,0x0010 [" 
222                    << p1->GetEntryValue(0x0010, 0x0010)
223                    << "]" << std::endl;
224          errorFound = true;
225          break;
226       }
227       std::cout << "Patient : [" 
228                 << p1->GetEntryValue(0x0010, 0x0010)
229                 << "]" << std::endl;
230
231       if ( !(s11 = p1->GetFirstStudy()) )
232       {
233          std::cout << "missing first Study Patient One" << std::endl;  
234          errorFound = true;
235          break;
236       }
237       valueStuff = s11->GetEntryValue(0x0008, 0x1030);
238       if ( valueStuff.find("StudyDescrOne.One_") >= valueStuff.length() )
239       {
240          std::cout << "1 : 0x0008,0x1030 [" 
241                    << valueStuff
242                    << "]" << std::endl;
243          errorFound = true;
244          break;
245       }
246       std::cout << "Study : [" 
247                 << valueStuff
248                 << "]" << std::endl;
249  
250       valueStuff = s11->GetEntryValue(0x0008, 0x1060);
251       if (!gdcm::Util::DicomStringEqual(valueStuff, "Dr Mabuse") )
252       {
253          std::cout << "2 : 0x0008,0x1060 [" 
254                    << s11->GetEntryValue(0x0008,0x1060)
255                    << "]" << std::endl;
256          errorFound = true;
257          break;
258       }
259       std::cout << "Pysician : [" 
260                 << valueStuff
261                 << "]" << std::endl;
262       if ( !(s12 = p1->GetNextStudy()) )
263       {
264          errorFound = true;
265          break;
266       }
267       if ( gdcm::Util::DicomStringEqual(s12->GetEntryValue(0x0008,
268                                            0x1030),"StudyDescrOne.Two " ))
269       {
270          std::cout << "3 0x0008,0x1030 [" 
271                    << s12->GetEntryValue(0x0008,0x1030)
272                    << "]" << std::endl;
273          errorFound = true;
274          break;
275       }
276       std::cout << "Study Descr : [" 
277                 << s12->GetEntryValue(0x0008,0x1030)
278                 << "]" << std::endl;
279
280       if ( gdcm::Util::DicomStringEqual(s12->GetEntryValue(0x0008,
281                                            0x1060),"Dr Zorglub " ))
282       {
283          std::cout << "4 0x0008,0x1060 [" 
284                    << s12->GetEntryValue(0x0008,0x1060)
285                    << "]" << std::endl;
286          errorFound = true;
287          break;
288       }
289       std::cout << "Pysician : [" 
290                 << s12->GetEntryValue(0x0008,0x1060)
291                 << "]" << std::endl;
292
293       if ( !(s13 = p1->GetNextStudy()) )
294       {
295          std::cout << "Study StudyDescrOne.Tree missing" << std::endl;
296          break;
297       }         
298       if ( s13->GetEntryValue(0x0008, 0x1030) != "StudyDescrOne.Tree" )
299       {
300          errorFound = true;
301          break;
302       }
303       std::cout << "Study : [" 
304                 << valueStuff
305                 << "]" << std::endl;
306
307       valueStuff = s13->GetEntryValue(0x0008, 0x1060);
308       if (!gdcm::Util::DicomStringEqual(valueStuff, "Dr Follamour") )
309       {
310          std::cout << "5 0x0008,0x1060 [" 
311                    << valueStuff
312                    << "]" << std::endl;
313          errorFound = true;
314          break;
315       }
316       std::cout << "Pysician : [" 
317                 << valueStuff
318                 << "]" << std::endl;
319
320       if (!(s111 = s11->GetFirstSerie()) )
321       {
322          std::cout << "Serie 01-01-111 missing" << std::endl;
323          errorFound = true;
324          break;
325       }
326
327       valueStuff = s111->GetEntryValue(0x0008, 0x0021);
328       if (!gdcm::Util::DicomStringEqual(valueStuff, "01-01-131") )
329       {
330          std::cout << "6 0x0008,0x0021 [" 
331                    << valueStuff
332                    << "]" << std::endl;
333          errorFound = true;
334          break;
335       }
336       std::cout << "Serie : [" 
337                 << valueStuff
338                 << "]" << std::endl;
339
340       if ( !(s1111 = s111->GetFirstImage()) )
341       {
342          std::cout << "missing image S1111" << std::endl;
343          errorFound = true;
344          break;
345       } 
346 /*
347       if ( s1111->GetEntryValue(0x0004,0x1500) != "imageFileName1111 " )
348       {
349          errorFound = true;
350          break;
351       }
352 */
353       if ( !(s1112 = s111->GetNextImage()) )
354       {
355          std::cout << "missing image S1112" << std::endl;
356          errorFound = true;
357          break;
358       }
359 /*
360       if ( s1112->GetEntryValue(0x0004,0x1500) != "imageFileName1112 " )
361       {
362          errorFound = true;
363          break;
364       }
365   */
366      break; // No error found. Stop looping
367    }
368
369    delete newDicomDir;
370    if ( errorFound )
371    {
372       std::cout << "MissWritting / MissReading " << std::endl;
373       std::cout<<std::flush;
374       return(1);
375    }
376
377    std::cout<<std::flush;
378    return 0;
379 }