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