1 /*=========================================================================
4 Module: $RCSfile: TestImageSet.cxx,v $
6 Date: $Date: 2005/03/02 17:38:35 $
7 Version: $Revision: 1.2 $
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.
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.
17 =========================================================================*/
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
26 #include "gdcmFileHelper.h"
27 #include "gdcmValEntry.h"
29 #include "gdcmDebug.h"
35 typedef std::list<gdcm::File *> FileList;
37 // If there is sameSerie, sameStudy is set to true
38 int CompareImages(FileList &list, bool sameSerie, bool sameStudy)
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;
49 FileList::iterator it;
50 for(it=list.begin();it!=list.end();++it)
53 entry=(*it)->GetValEntry(0x0008, 0x0018);
55 if( instUID.find(entry->GetValue())!=instUID.end() )
56 instUID[entry->GetValue()]++;
58 instUID[entry->GetValue()]=1;
59 // Media Storage SOP Instance UID
60 entry=(*it)->GetValEntry(0x0002,0x0003);
62 if( mediaUID.find(entry->GetValue())!=mediaUID.end() )
63 mediaUID[entry->GetValue()]++;
65 mediaUID[entry->GetValue()]=1;
66 // Series Instance UID
67 entry=(*it)->GetValEntry(0x0020,0x000e);
69 if( serieUID.find(entry->GetValue())!=serieUID.end() )
70 serieUID[entry->GetValue()]++;
72 serieUID[entry->GetValue()]=1;
74 entry=(*it)->GetValEntry(0x0020,0x000d);
76 if( studyUID.find(entry->GetValue())!=studyUID.end() )
77 studyUID[entry->GetValue()]++;
79 studyUID[entry->GetValue()]=1;
84 if( serieUID.size()>1 )
86 std::cout << "Failed\n"
87 << " Series UID not same (0x0020,0x000e)\n";
93 if( serieUID.size()!=list.size() )
95 std::cout << "Failed\n"
96 << " Some Series UID are same (0x0020,0x000e)\n";
103 if( studyUID.size()>1 )
105 std::cout << "Failed\n"
106 << " Studies UID not same (0x0020,0x000d)\n";
112 if( studyUID.size()!=list.size() )
114 std::cout << "Failed\n"
115 << " Some Studies UID are same (0x0020,0x000d)\n";
120 if( mediaUID.size()!=list.size() )
122 std::cout << "Failed\n"
123 << " Some Media UID are same (0x0002,0x0003)\n";
127 if( instUID.size()!=list.size() )
129 std::cout << "Failed\n"
130 << " Some Instance UID are same (0x0008,0x0018)\n";
137 void ClearList(FileList &list)
139 FileList::iterator it;
140 for(it=list.begin();it!=list.end();++it)
147 gdcm::File *WriteImage(gdcm::File *file,const std::string &fileName)
149 // Create a 256x256x1 image 8 bits, unsigned
150 std::ostringstream str;
152 // Set the image size
153 file->InsertValEntry("256",0x0028,0x0011); // Columns
154 file->InsertValEntry("256",0x0028,0x0010); // Rows
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
161 // Set the pixel representation
162 file->InsertValEntry("0",0x0028,0x0103); // Pixel Representation
164 // Set the samples per pixel
165 file->InsertValEntry("1",0x0028,0x0002); // Samples per Pixel
167 if( !file->IsReadable() )
169 std::cout << "Failed\n"
170 << " Prepared image isn't readable\n";
174 size_t size = 256 * 256 * 1;
175 unsigned char *imageData = new unsigned char[size];
176 memset(imageData,0,size);
179 gdcm::FileHelper *hlp = new gdcm::FileHelper(file);
180 hlp->SetImageData(imageData,size);
181 hlp->SetWriteTypeToDcmExplVR();
182 if( !hlp->Write(fileName) )
184 std::cout << "Failed\n"
185 << " File in unwrittable\n";
194 // Read the written image
195 gdcm::File *reread = new gdcm::File( fileName );
196 if( !reread->IsReadable() )
198 std::cerr << "Failed" << std::endl
199 << " Could not reread written image :" << fileName << std::endl;
207 int TestImageSet(int argc, char *argv[])
211 std::cerr << "usage: \n"
212 << argv[0] << " (without parameters) " << std::endl
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;
233 std::cout<<" step...";
234 std::string studyUID;
235 std::string serieUID;
237 // Step 1 : All files have different UID
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);
252 newFile = WriteImage(file,fileName.str());
259 fileList.push_back(newFile);
264 if( CompareImages(fileList,false,false) )
273 // Step 2 : Same Serie & Study
275 studyUID = gdcm::Util::CreateUniqueUID();
276 serieUID = gdcm::Util::CreateUniqueUID();
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);
285 newFile = WriteImage(file,fileName.str());
292 fileList.push_back(newFile);
297 if( CompareImages(fileList,true,true) )
306 // Step 3 : Same Study
308 serieUID = gdcm::Util::CreateUniqueUID();
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());
324 fileList.push_back(newFile);
329 if( CompareImages(fileList,false,true) )
336 std::cout<<"3...OK\n";