]> Creatis software - gdcm.git/blob - Testing/TestImageSet.cxx
A 'prepared image' built ex nihilo just before, has never Pixel Element.
[gdcm.git] / Testing / TestImageSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestImageSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/06/03 09:55:18 $
7   Version:   $Revision: 1.3 $
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    gdcm::Debug::DebugOn();
41
42    if( sameSerie )
43       sameStudy = true;
44
45    gdcm::ValEntry *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)->GetValEntry(0x0008, 0x0018);
56       if( entry )
57          if( instUID.find(entry->GetValue())!=instUID.end() )
58             instUID[entry->GetValue()]++;
59          else
60             instUID[entry->GetValue()]=1;
61       // Media Storage SOP Instance UID
62       entry=(*it)->GetValEntry(0x0002,0x0003);
63       if( entry )
64          if( mediaUID.find(entry->GetValue())!=mediaUID.end() )
65             mediaUID[entry->GetValue()]++;
66          else
67             mediaUID[entry->GetValue()]=1;
68       // Series Instance UID
69       entry=(*it)->GetValEntry(0x0020,0x000e);
70       if( entry )
71          if( serieUID.find(entry->GetValue())!=serieUID.end() )
72             serieUID[entry->GetValue()]++;
73          else
74             serieUID[entry->GetValue()]=1;
75       // Study Instance UID
76       entry=(*it)->GetValEntry(0x0020,0x000d);
77       if( entry )
78          if( studyUID.find(entry->GetValue())!=studyUID.end() )
79             studyUID[entry->GetValue()]++;
80          else
81             studyUID[entry->GetValue()]=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       delete (*it);
145    }
146    list.clear();
147 }
148
149 gdcm::File *WriteImage(gdcm::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->InsertValEntry("256",0x0028,0x0011); // Columns
156    file->InsertValEntry("256",0x0028,0x0010); // Rows
157
158    // Set the pixel type
159    file->InsertValEntry("8",0x0028,0x0100); // Bits Allocated
160    file->InsertValEntry("8",0x0028,0x0101); // Bits Stored
161    file->InsertValEntry("7",0x0028,0x0102); // High Bit
162
163    // Set the pixel representation
164    file->InsertValEntry("0",0x0028,0x0103); // Pixel Representation
165
166    // Set the samples per pixel
167    file->InsertValEntry("1",0x0028,0x0002); // 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::FileHelper *hlp = new gdcm::FileHelper(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       delete hlp;
194       delete[] imageData;
195       return NULL;
196    }
197    delete[] imageData;
198    delete hlp;
199
200 // Read the written image
201    gdcm::File *reread = new gdcm::File( fileName );
202    if( !reread->IsReadable() )
203    {
204      std::cerr << "Failed" << std::endl
205                << "        Could not reread written image :" << fileName << std::endl;
206      delete reread;
207      return NULL;
208    }
209
210    return reread;
211 }
212
213 int TestImageSet(int argc, char *argv[])
214 {
215    if (argc < 1) 
216    {
217       std::cerr << "usage: \n" 
218                 << argv[0] << " (without parameters) " << std::endl 
219                 << std::endl;
220       return 1;
221    }
222
223    std::cout << "   Description (Test::TestSequence): " << std::endl;
224    std::cout << "   Tests the creation of a 4 images Set" << std::endl;
225    std::cout << "   with the following steps : "<< std::endl;
226    std::cout << "   step 1: create images belonging" << std::endl
227              << "           to different Study and Terie" << std::endl;
228    std::cout << "   step 2: create images belonging" << std::endl
229              << "           to the same Serie (therefore to the same Study)" << std::endl;
230    std::cout << "   step 3: create images belonging" << std::endl
231              << "           to different Series within the same Study" << std::endl;
232    std::cout << std::endl << std::endl;
233
234    gdcm::File *file;
235    gdcm::File *newFile;
236    FileList fileList;
237    int i;
238
239    std::cout<<"     step...";
240    std::string studyUID;
241    std::string serieUID;
242
243    // Step 1 : All files have different UID 
244    fileList.clear();
245    for(i = 0;i < 4;i++)
246    {
247       std::ostringstream fileName;
248       fileName << "FileSeq" << i << ".dcm";
249       file = new gdcm::File();
250       // It's up to the user to initialize Serie UID and Study UID
251       // Study Instance UID
252       studyUID = gdcm::Util::CreateUniqueUID();
253       file->InsertValEntry(studyUID, 0x0020, 0x000d);
254       // Series Instance UID
255       serieUID = gdcm::Util::CreateUniqueUID();
256       file->InsertValEntry(serieUID, 0x0020, 0x000e);
257
258       newFile = WriteImage(file, fileName.str());
259       if( !newFile )
260       {
261          delete file;
262          return 1;
263       }
264       else
265          fileList.push_back(newFile);
266
267       delete file;
268    }
269
270    if( CompareImages(fileList, false, false) )
271    {
272       ClearList(fileList);
273       return 1;
274    }
275    ClearList(fileList);
276
277    std::cout<<"1...";
278
279    // Step 2 : Same Serie & Study
280    fileList.clear();
281    studyUID = gdcm::Util::CreateUniqueUID();
282    serieUID = gdcm::Util::CreateUniqueUID();
283    for(i = 0;i < 4;i++)
284    {
285       std::ostringstream fileName;
286       fileName << "FileSeq" << i << ".dcm";
287       file = new gdcm::File();
288       file->InsertValEntry(studyUID, 0x0020, 0x000d);
289       file->InsertValEntry(serieUID, 0x0020, 0x000e);
290
291       newFile = WriteImage(file, fileName.str());
292       if( !newFile )
293       {
294          delete file;
295          return(1);
296       }
297       else
298          fileList.push_back(newFile);
299
300       delete file;
301    }
302
303    if( CompareImages(fileList, true, true) )
304    {
305       ClearList(fileList);
306       return 1;
307    }
308    ClearList(fileList);
309
310    std::cout<<"2...";
311
312    // Step 3 : Same Study
313    fileList.clear();
314    serieUID = gdcm::Util::CreateUniqueUID();
315    for(i = 0;i < 4;i++)
316    {
317       std::ostringstream fileName;
318       fileName << "FileSeq" << i << ".dcm";
319       file = new gdcm::File();
320       file->InsertValEntry(studyUID, 0x0020, 0x000d);
321       serieUID = gdcm::Util::CreateUniqueUID();
322       file->InsertValEntry(serieUID, 0x0020, 0x000e);
323       newFile = WriteImage(file, fileName.str());
324       if( !newFile )
325       {
326          delete file;
327          return(1);
328       }
329       else
330          fileList.push_back(newFile);
331
332       delete file;
333    }
334
335    if( CompareImages(fileList, false, true) )
336    {
337       ClearList(fileList);
338       return 1;
339    }
340    ClearList(fileList);
341
342    std::cout<<"3...OK\n";
343
344    return 0;
345 }