]> Creatis software - gdcm.git/blob - Testing/TestImageSet.cxx
Change meaningless name 'TestSequence' (that does NOT tests Sequences) to
[gdcm.git] / Testing / TestImageSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestImageSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/03/02 16:39:28 $
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
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 written image :" << 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 << "   Tests the creation of a 4 images Set" << std::endl;
219    std::cout << "   with the following steps : "<< std::endl;
220    std::cout << "   step 1: create images belonging" << std::endl
221              << "           to different Study and Terie" << std::endl;
222    std::cout << "   step 2: create images belonging" << std::endl
223              << "           to the same Serie (therefore to the same Study)" << std::endl;
224    std::cout << "   step 3: create images belonging" << std::endl
225              << "           to different Series within the same Study" << 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;
235    std::string serieUID;
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       // It's up to the user to initialize Serie UID and Study UID
245       // Study Instance UID
246       studyUID = gdcm::Util::CreateUniqueUID();
247       file->InsertValEntry(studyUID, 0x0020, 0x000d);
248       // Series Instance UID
249       serieUID = gdcm::Util::CreateUniqueUID();
250       file->InsertValEntry(serieUID, 0x0020, 0x000e);
251
252       newFile = WriteImage(file,fileName.str());
253       if( !newFile )
254       {
255          delete file;
256          return 1;
257       }
258       else
259          fileList.push_back(newFile);
260
261       delete file;
262    }
263
264    if( CompareImages(fileList,false,false) )
265    {
266       ClearList(fileList);
267       return 1;
268    }
269    ClearList(fileList);
270
271    std::cout<<"1...";
272
273    // Step 2 : Same Serie & Study
274    fileList.clear();
275    studyUID = gdcm::Util::CreateUniqueUID();
276    serieUID = gdcm::Util::CreateUniqueUID();
277    for(i = 0;i < 4;i++)
278    {
279       std::ostringstream fileName;
280       fileName << "FileSeq" << i << ".dcm";
281       file = new gdcm::File();
282       file->InsertValEntry(studyUID,0x0020,0x000d);
283       file->InsertValEntry(serieUID,0x0020,0x000e);
284
285       newFile = WriteImage(file,fileName.str());
286       if( !newFile )
287       {
288          delete file;
289          return(1);
290       }
291       else
292          fileList.push_back(newFile);
293
294       delete file;
295    }
296
297    if( CompareImages(fileList,true,true) )
298    {
299       ClearList(fileList);
300       return 1;
301    }
302    ClearList(fileList);
303
304    std::cout<<"2...";
305
306    // Step 3 : Same Study
307    fileList.clear();
308    serieUID = gdcm::Util::CreateUniqueUID();
309    for(i = 0;i < 4;i++)
310    {
311       std::ostringstream fileName;
312       fileName << "FileSeq" << i << ".dcm";
313       file = new gdcm::File();
314       file->InsertValEntry(studyUID,0x0020,0x000d);
315       serieUID = gdcm::Util::CreateUniqueUID();
316       file->InsertValEntry(serieUID,0x0020,0x000e);
317       newFile = WriteImage(file,fileName.str());
318       if( !newFile )
319       {
320          delete file;
321          return(1);
322       }
323       else
324          fileList.push_back(newFile);
325
326       delete file;
327    }
328
329    if( CompareImages(fileList,false,true) )
330    {
331       ClearList(fileList);
332       return 1;
333    }
334    ClearList(fileList);
335
336    std::cout<<"3...OK\n";
337
338    return 0;
339 }