1 /*=========================================================================
4 Module: $RCSfile: TestImageSet.cxx,v $
6 Date: $Date: 2005/10/25 14:52:31 $
7 Version: $Revision: 1.6 $
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 "gdcmDataEntry.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)
40 gdcm::Debug::DebugOn();
45 gdcm::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;
51 FileList::iterator it;
52 for(it=list.begin();it!=list.end();++it)
55 entry=(*it)->GetDataEntry(0x0008, 0x0018);
57 if( instUID.find(entry->GetString())!=instUID.end() )
58 instUID[entry->GetString()]++;
60 instUID[entry->GetString()]=1;
61 // Media Storage SOP Instance UID
62 entry=(*it)->GetDataEntry(0x0002,0x0003);
64 if( mediaUID.find(entry->GetString())!=mediaUID.end() )
65 mediaUID[entry->GetString()]++;
67 mediaUID[entry->GetString()]=1;
68 // Series Instance UID
69 entry=(*it)->GetDataEntry(0x0020,0x000e);
71 if( serieUID.find(entry->GetString())!=serieUID.end() )
72 serieUID[entry->GetString()]++;
74 serieUID[entry->GetString()]=1;
76 entry=(*it)->GetDataEntry(0x0020,0x000d);
78 if( studyUID.find(entry->GetString())!=studyUID.end() )
79 studyUID[entry->GetString()]++;
81 studyUID[entry->GetString()]=1;
86 if( serieUID.size()>1 )
88 std::cout << "Failed\n"
89 << " Series UID not same (0x0020,0x000e)\n";
95 if( serieUID.size()!=list.size() )
97 std::cout << "Failed\n"
98 << " Some Series UID are same (0x0020,0x000e)\n";
105 if( studyUID.size()>1 )
107 std::cout << "Failed\n"
108 << " Studies UID not same (0x0020,0x000d)\n";
114 if( studyUID.size()!=list.size() )
116 std::cout << "Failed\n"
117 << " Some Studies UID are same (0x0020,0x000d)\n";
122 if( mediaUID.size()!=list.size() )
124 std::cout << "Failed\n"
125 << " Some Media UID are same (0x0002,0x0003)\n";
129 if( instUID.size()!=list.size() )
131 std::cout << "Failed\n"
132 << " Some Instance UID are same (0x0008,0x0018)\n";
139 void ClearList(FileList &list)
141 FileList::iterator it;
142 for(it=list.begin();it!=list.end();++it)
149 gdcm::File *WriteImage(gdcm::File *file, const std::string &fileName)
151 // Create a 256x256x1 image 8 bits, unsigned
152 std::ostringstream str;
154 // Set the image size
155 file->InsertEntryString("256",0x0028,0x0011); // Columns
156 file->InsertEntryString("256",0x0028,0x0010); // Rows
158 // Set the pixel type
159 file->InsertEntryString("8",0x0028,0x0100); // Bits Allocated
160 file->InsertEntryString("8",0x0028,0x0101); // Bits Stored
161 file->InsertEntryString("7",0x0028,0x0102); // High Bit
163 // Set the pixel representation
164 file->InsertEntryString("0",0x0028,0x0103); // Pixel Representation
166 // Set the samples per pixel
167 file->InsertEntryString("1",0x0028,0x0002); // Samples per Pixel
169 // The so called 'prepared image', built ex nihilo just before,
170 // has NO Pixel Element yet.
171 // therefore, it's NEVER 'file readable' ...
173 //if( !file->IsReadable() )
175 // std::cout << "Failed\n"
176 // << " Prepared image isn't readable\n";
180 size_t size = 256 * 256 * 1;
181 unsigned char *imageData = new unsigned char[size];
182 memset(imageData,0,size);
185 gdcm::FileHelper *hlp = gdcm::FileHelper::New(file);
186 hlp->SetImageData(imageData,size);
187 hlp->SetWriteTypeToDcmExplVR();
188 if( !hlp->Write(fileName) )
190 std::cout << "Failed\n"
191 << " File in unwrittable\n";
200 // Read the written image
201 gdcm::File *reread = gdcm::File::New( );
202 reread->SetFileName( fileName );
204 if( !reread->IsReadable() )
206 std::cerr << "Failed" << std::endl
207 << " Could not reread written image :" << fileName << std::endl;
215 int TestImageSet(int argc, char *argv[])
219 std::cerr << "usage: \n"
220 << argv[0] << " (without parameters) " << std::endl
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;
241 std::cout<<" step...";
242 std::string studyUID;
243 std::string serieUID;
245 // Step 1 : All files have different UID
249 std::ostringstream fileName;
250 fileName << "FileSeq" << i << ".dcm";
251 file = gdcm::File::New();
252 // It's up to the user to initialize Serie UID and Study UID
253 // Study Instance UID
254 studyUID = gdcm::Util::CreateUniqueUID();
255 file->InsertEntryString(studyUID, 0x0020, 0x000d);
256 // Series Instance UID
257 serieUID = gdcm::Util::CreateUniqueUID();
258 file->InsertEntryString(serieUID, 0x0020, 0x000e);
260 newFile = WriteImage(file, fileName.str());
267 fileList.push_back(newFile);
272 if( CompareImages(fileList, false, false) )
281 // Step 2 : Same Serie & Study
283 studyUID = gdcm::Util::CreateUniqueUID();
284 serieUID = gdcm::Util::CreateUniqueUID();
287 std::ostringstream fileName;
288 fileName << "FileSeq" << i << ".dcm";
289 file = gdcm::File::New();
290 file->InsertEntryString(studyUID, 0x0020, 0x000d);
291 file->InsertEntryString(serieUID, 0x0020, 0x000e);
293 newFile = WriteImage(file, fileName.str());
300 fileList.push_back(newFile);
305 if( CompareImages(fileList, true, true) )
314 // Step 3 : Same Study
316 serieUID = gdcm::Util::CreateUniqueUID();
319 std::ostringstream fileName;
320 fileName << "FileSeq" << i << ".dcm";
321 file = gdcm::File::New();
322 file->InsertEntryString(studyUID, 0x0020, 0x000d);
323 serieUID = gdcm::Util::CreateUniqueUID();
324 file->InsertEntryString(serieUID, 0x0020, 0x000e);
325 newFile = WriteImage(file, fileName.str());
332 fileList.push_back(newFile);
337 if( CompareImages(fileList, false, true) )
344 std::cout<<"3...OK\n";