]> Creatis software - gdcm.git/blob - Testing/TestSequence.cxx
838d5e708a87a7c863a9211dfbecfd929907e698
[gdcm.git] / Testing / TestSequence.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestSequence.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/07 12:53:59 $
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
177 // Write the image
178    gdcm::FileHelper *hlp = new gdcm::FileHelper(file);
179    hlp->SetImageData(imageData,size);
180    hlp->SetWriteTypeToDcmExplVR();
181    if( !hlp->Write(fileName) )
182    {
183       std::cout << "Failed\n"
184                 << "        File in unwrittable\n";
185
186       delete hlp;
187       delete[] imageData;
188       return NULL;
189    }
190    delete[] imageData;
191    delete hlp;
192
193 // Read the written image
194    gdcm::File *reread = new gdcm::File( fileName );
195    if( !reread->IsReadable() )
196    {
197      std::cerr << "Failed" << std::endl
198                << "        Could not reread image written:" << fileName << std::endl;
199      delete reread;
200      return NULL;
201    }
202
203    return reread;
204 }
205
206 int TestSequence(int argc, char *argv[])
207 {
208    if (argc < 1) 
209    {
210       std::cerr << "usage: \n" 
211                 << argv[0] << " (without parameters) " << std::endl 
212                 << std::endl;
213       return 1;
214    }
215
216    std::cout << "   Description (Test::TestSequence): " << std::endl;
217    std::cout << "   Will test the creation of sequences of 4 images" << std::endl;
218    std::cout << "   with the following steps : "<< std::endl;
219    std::cout << "   step 1: create separed images without correspondance" << std::endl
220              << "           in UID for study and serie" << std::endl;
221    std::cout << "   step 2: create serie of image." << std::endl
222              << "           So the Serie and Study UID must be equal" << std::endl;
223    std::cout << "   step 3: create separed serie of image with same study" << std::endl
224              << "           So the Study UID must be equal" << std::endl;
225    std::cout << std::endl << std::endl;
226
227    gdcm::File *file;
228    gdcm::File *newFile;
229    FileList fileList;
230    int i;
231
232    std::cout<<"     step...";
233    std::string studyUID = gdcm::Util::CreateUniqueUID();
234    std::string serieUID = gdcm::Util::CreateUniqueUID();
235
236    // Step 1 : All files have different UID 
237    fileList.clear();
238    for(i = 0;i < 4;i++)
239    {
240       std::ostringstream fileName;
241       fileName << "FileSeq" << i << ".dcm";
242       file = new gdcm::File();
243
244       newFile = WriteImage(file,fileName.str());
245       if( !newFile )
246       {
247          delete file;
248          return 1;
249       }
250       else
251          fileList.push_back(newFile);
252
253       delete file;
254    }
255
256    if( CompareImages(fileList,false,false) )
257    {
258       ClearList(fileList);
259       return 1;
260    }
261    ClearList(fileList);
262
263    std::cout<<"1...";
264
265    // Step 2 : Same Serie & Study
266    fileList.clear();
267    for(i = 0;i < 4;i++)
268    {
269       std::ostringstream fileName;
270       fileName << "FileSeq" << i << ".dcm";
271       file = new gdcm::File();
272       file->SetValEntry(studyUID,0x0020,0x000d);
273       file->SetValEntry(serieUID,0x0020,0x000e);
274
275       newFile = WriteImage(file,fileName.str());
276       if( !newFile )
277       {
278          delete file;
279          return(1);
280       }
281       else
282          fileList.push_back(newFile);
283
284       delete file;
285    }
286
287    if( CompareImages(fileList,true,true) )
288    {
289       ClearList(fileList);
290       return 1;
291    }
292    ClearList(fileList);
293
294    std::cout<<"2...";
295
296    // Step 3 : Same Study
297    fileList.clear();
298    for(i = 0;i < 4;i++)
299    {
300       std::ostringstream fileName;
301       fileName << "FileSeq" << i << ".dcm";
302       file = new gdcm::File();
303       file->SetValEntry(studyUID,0x0020,0x000d);
304
305       newFile = WriteImage(file,fileName.str());
306       if( !newFile )
307       {
308          delete file;
309          return(1);
310       }
311       else
312          fileList.push_back(newFile);
313
314       delete file;
315    }
316
317    if( CompareImages(fileList,false,true) )
318    {
319       ClearList(fileList);
320       return 1;
321    }
322    ClearList(fileList);
323
324    std::cout<<"3...OK";
325
326    return 0;
327 }