]> Creatis software - gdcm.git/blob - Testing/TestSequence.cxx
* Testing/TestWriteSimple.cxx, TestSequence.cxx : fix memroy leaks
[gdcm.git] / Testing / TestSequence.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestSequence.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/10 14:23:18 $
7   Version:   $Revision: 1.2 $
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
19 /**
20  * Write a dicom file from nothing
21  * The written image is 256x256, 8 bits, unsigned char
22  * The image content is a horizontal grayscale from 
23  * 
24  */
25 #include "gdcmFile.h"
26 #include "gdcmFileHelper.h"
27 #include "gdcmValEntry.h"
28 #include "gdcmUtil.h"
29 #include "gdcmDebug.h"
30
31 #include <iostream>
32 #include <sstream>
33 #include <list>
34
35 typedef std::list<gdcm::File *> FileList;
36
37 // If there is sameSerie, sameStudy is set to true
38 int CompareImages(FileList &list,bool sameSerie,bool sameStudy)
39 {
40    if( sameSerie )
41       sameStudy = true;
42
43    gdcm::ValEntry *entry;
44    std::map<std::string, int> instUID;
45    std::map<std::string, int> mediaUID;
46    std::map<std::string, int> serieUID;
47    std::map<std::string, int> studyUID;
48
49    FileList::iterator it;
50    for(it=list.begin();it!=list.end();++it)
51    {
52       // SOP Instance UID
53       entry=(*it)->GetValEntry(0x0008,0x0018);
54       if( entry )
55          if( instUID.find(entry->GetValue())!=instUID.end() )
56             instUID[entry->GetValue()]++;
57          else
58             instUID[entry->GetValue()]=1;
59       // Media Storage SOP Instance UID
60       entry=(*it)->GetValEntry(0x0002,0x0003);
61       if( entry )
62          if( mediaUID.find(entry->GetValue())!=mediaUID.end() )
63             mediaUID[entry->GetValue()]++;
64          else
65             mediaUID[entry->GetValue()]=1;
66       // Series Instance UID
67       entry=(*it)->GetValEntry(0x0020,0x000e);
68       if( entry )
69          if( serieUID.find(entry->GetValue())!=serieUID.end() )
70             serieUID[entry->GetValue()]++;
71          else
72             serieUID[entry->GetValue()]=1;
73       // Study Instance UID
74       entry=(*it)->GetValEntry(0x0020,0x000d);
75       if( entry )
76          if( studyUID.find(entry->GetValue())!=studyUID.end() )
77             studyUID[entry->GetValue()]++;
78          else
79             studyUID[entry->GetValue()]=1;
80    }
81
82    if( sameSerie )
83    {
84       if( serieUID.size()>1 )
85       {
86          std::cout << "Failed\n"
87                    << "        Series UID not same (0x0020,0x000e)\n";
88          return 1;
89       }
90    }
91    else
92    {
93       if( serieUID.size()!=list.size() )
94       {
95          std::cout << "Failed\n"
96                    << "        Some Series UID are same (0x0020,0x000e)\n";
97          return 1;
98       }
99    }
100
101    if( sameStudy )
102    {
103       if( studyUID.size()>1 )
104       {
105          std::cout << "Failed\n"
106                    << "        Studies UID not same (0x0020,0x000d)\n";
107          return 1;
108       }
109    }
110    else
111    {
112       if( studyUID.size()!=list.size() )
113       {
114          std::cout << "Failed\n"
115                    << "        Some Studies UID are same (0x0020,0x000d)\n";
116          return 1;
117       }
118    }
119
120    if( mediaUID.size()!=list.size() )
121    {
122       std::cout << "Failed\n"
123                 << "        Some Media UID are same (0x0002,0x0003)\n";
124       return 1;
125    }
126
127    if( instUID.size()!=list.size() )
128    {
129       std::cout << "Failed\n"
130                 << "        Some Instance UID are same (0x0008,0x0018)\n";
131       return 1;
132    }
133
134    return 0;
135 }
136
137 void ClearList(FileList &list)
138 {
139    FileList::iterator it;
140    for(it=list.begin();it!=list.end();++it)
141    {
142       delete (*it);
143    }
144    list.clear();
145 }
146
147 gdcm::File *WriteImage(gdcm::File *file,const std::string &fileName)
148 {
149    // Create a 256x256x1 image 8 bits, unsigned 
150    std::ostringstream str;
151
152    // Set the image size
153    file->InsertValEntry("256",0x0028,0x0011); // Columns
154    file->InsertValEntry("256",0x0028,0x0010); // Rows
155
156    // Set the pixel type
157    file->InsertValEntry("8",0x0028,0x0100); // Bits Allocated
158    file->InsertValEntry("8",0x0028,0x0101); // Bits Stored
159    file->InsertValEntry("7",0x0028,0x0102); // High Bit
160
161    // Set the pixel representation
162    file->InsertValEntry("0",0x0028,0x0103); // Pixel Representation
163
164    // Set the samples per pixel
165    file->InsertValEntry("1",0x0028,0x0002); // Samples per Pixel
166
167    if( !file->IsReadable() )
168    {
169       std::cout << "Failed\n"
170                 << "        Prepared image isn't readable\n";
171       return NULL;
172    }
173
174    size_t size = 256 * 256 * 1;
175    unsigned char *imageData = new unsigned char[size];
176    memset(imageData,0,size);
177
178 // Write the image
179    gdcm::FileHelper *hlp = new gdcm::FileHelper(file);
180    hlp->SetImageData(imageData,size);
181    hlp->SetWriteTypeToDcmExplVR();
182    if( !hlp->Write(fileName) )
183    {
184       std::cout << "Failed\n"
185                 << "        File in unwrittable\n";
186
187       delete hlp;
188       delete[] imageData;
189       return NULL;
190    }
191    delete[] imageData;
192    delete hlp;
193
194 // Read the written image
195    gdcm::File *reread = new gdcm::File( fileName );
196    if( !reread->IsReadable() )
197    {
198      std::cerr << "Failed" << std::endl
199                << "        Could not reread image written:" << fileName << std::endl;
200      delete reread;
201      return NULL;
202    }
203
204    return reread;
205 }
206
207 int TestSequence(int argc, char *argv[])
208 {
209    if (argc < 1) 
210    {
211       std::cerr << "usage: \n" 
212                 << argv[0] << " (without parameters) " << std::endl 
213                 << std::endl;
214       return 1;
215    }
216
217    std::cout << "   Description (Test::TestSequence): " << std::endl;
218    std::cout << "   Will test the creation of sequences of 4 images" << std::endl;
219    std::cout << "   with the following steps : "<< std::endl;
220    std::cout << "   step 1: create separed images without correspondance" << std::endl
221              << "           in UID for study and serie" << std::endl;
222    std::cout << "   step 2: create serie of image." << std::endl
223              << "           So the Serie and Study UID must be equal" << std::endl;
224    std::cout << "   step 3: create separed serie of image with same study" << std::endl
225              << "           So the Study UID must be equal" << std::endl;
226    std::cout << std::endl << std::endl;
227
228    gdcm::File *file;
229    gdcm::File *newFile;
230    FileList fileList;
231    int i;
232
233    std::cout<<"     step...";
234    std::string studyUID = gdcm::Util::CreateUniqueUID();
235    std::string serieUID = gdcm::Util::CreateUniqueUID();
236
237    // Step 1 : All files have different UID 
238    fileList.clear();
239    for(i = 0;i < 4;i++)
240    {
241       std::ostringstream fileName;
242       fileName << "FileSeq" << i << ".dcm";
243       file = new gdcm::File();
244
245       newFile = WriteImage(file,fileName.str());
246       if( !newFile )
247       {
248          delete file;
249          return 1;
250       }
251       else
252          fileList.push_back(newFile);
253
254       delete file;
255    }
256
257    if( CompareImages(fileList,false,false) )
258    {
259       ClearList(fileList);
260       return 1;
261    }
262    ClearList(fileList);
263
264    std::cout<<"1...";
265
266    // Step 2 : Same Serie & Study
267    fileList.clear();
268    for(i = 0;i < 4;i++)
269    {
270       std::ostringstream fileName;
271       fileName << "FileSeq" << i << ".dcm";
272       file = new gdcm::File();
273       file->SetValEntry(studyUID,0x0020,0x000d);
274       file->SetValEntry(serieUID,0x0020,0x000e);
275
276       newFile = WriteImage(file,fileName.str());
277       if( !newFile )
278       {
279          delete file;
280          return(1);
281       }
282       else
283          fileList.push_back(newFile);
284
285       delete file;
286    }
287
288    if( CompareImages(fileList,true,true) )
289    {
290       ClearList(fileList);
291       return 1;
292    }
293    ClearList(fileList);
294
295    std::cout<<"2...";
296
297    // Step 3 : Same Study
298    fileList.clear();
299    for(i = 0;i < 4;i++)
300    {
301       std::ostringstream fileName;
302       fileName << "FileSeq" << i << ".dcm";
303       file = new gdcm::File();
304       file->SetValEntry(studyUID,0x0020,0x000d);
305
306       newFile = WriteImage(file,fileName.str());
307       if( !newFile )
308       {
309          delete file;
310          return(1);
311       }
312       else
313          fileList.push_back(newFile);
314
315       delete file;
316    }
317
318    if( CompareImages(fileList,false,true) )
319    {
320       ClearList(fileList);
321       return 1;
322    }
323    ClearList(fileList);
324
325    std::cout<<"3...OK\n";
326
327    return 0;
328 }