1 /*=========================================================================
4 Module: $RCSfile: exConvert3DplusT.cxx,v $
6 Date: $Date: 2006/07/19 09:03:24 $
7 Version: $Revision: 1.1 $
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 =========================================================================*/
19 // This program is used to convert huge amounts of functionnal (3D + T) MR images
20 // into 'image tiles' (one title per volume) processable by clinical softwares
23 #include "gdcmFileHelper.h"
24 #include "gdcmCommon.h"
25 #include "gdcmDebug.h"
26 #include "gdcmDirList.h"
29 #include "gdcmArgMgr.h"
33 #include <string.h> // for memset
35 int main(int argc, char *argv[])
38 "\n exConvert3plusT :\n ",
39 " Converts the Dicom files inside a single-level Directory ",
40 " into a '3D + T' image set ",
41 " usage: exConvert3plusT ",
42 " dirin=inputDirectoryName ",
43 " dirout=outputDirectoryName ",
44 " [studyUID = ] [patName = ] ",
45 " [ { [noshadowseq] | [noshadow][noseq] } ] [debug] [verbose]",
47 " dirin : single-level Directory containing the images ",
48 " (no recursive parsing) ",
49 " dirout : will be created if doesn't exist ",
51 " [imdimx = ] [imdimy = ] [imgline = ] [imgcol = ] ",
52 " [pixelsize = ] [imagesinvolume = ] ",
54 " studyUID : *aware* user wants to add the serie ",
55 " to an already existing study ",
56 " imdimx,imdimy : 'elementary image' size (default : 64) ",
57 " used to reject erroneous images ",
58 " imgline, imgcol : sizes of the 'image tile' defaulted as 6x6 ",
59 " pixelsize : in bytes; defaulted as 2 (uint16_t) ",
60 " used to reject erroneous images ",
61 " imagesinvolume : we have no way to guess it! ",
62 " defaulted as imgline*imgcol ",
63 " noshadowseq: user doesn't want to load Private Sequences ",
64 " noshadow : user doesn't want to load Private groups (odd number) ",
65 " noseq : user doesn't want to load Sequences ",
66 " verbose : user wants to run the program in 'verbose mode' ",
67 " debug : developper wants to run the program in 'debug mode' ",
71 // ----- Initialize Arguments Manager ------
73 gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
75 if (am->ArgMgrDefined("usage") || argc == 1)
77 am->ArgMgrUsage(usage); // Display 'usage'
82 if (am->ArgMgrDefined("debug"))
83 gdcm::Debug::DebugOn();
84 int verbose = am->ArgMgrDefined("verbose");
85 int oververbose = am->ArgMgrDefined("oververbose");
87 std::string patName = am->ArgMgrGetString("patname", "g^PatientName");
88 //float zSpacing = am->ArgMgrGetFloat("zSpacing", 1.0);
90 const char *dirIn = am->ArgMgrGetString("dirin");
91 const char *dirOut = am->ArgMgrGetString("dirout");
93 bool userDefinedStudy = am->ArgMgrDefined("studyUID");
94 const char *studyUID = am->ArgMgrGetString("studyUID");
96 // not described *on purpose* in the Usage !
97 bool userDefinedSerie = am->ArgMgrDefined("serieUID");
98 const char *serieUID = am->ArgMgrGetString("serieUID");
101 int imageDimX = am->ArgMgrGetInt("imdimx",64);
102 int imageDimY = am->ArgMgrGetInt("imdimy",64);
103 int imagePixelSize = am->ArgMgrGetInt("pixelsize", 2);
104 int imagetteLineNumber = am->ArgMgrGetInt("imgline",6);
105 int imagetteRowNumber = am->ArgMgrGetInt("imgcol",6);
106 int nbOfImagesInVolume = am->ArgMgrGetInt("imagesinvolume",
107 imagetteLineNumber*imagetteRowNumber);
109 int loadMode = gdcm::LD_ALL;
110 if ( am->ArgMgrDefined("noshadowseq") )
111 loadMode |= gdcm::LD_NOSHADOWSEQ;
114 if ( am->ArgMgrDefined("noshadow") )
115 loadMode |= gdcm::LD_NOSHADOW;
116 if ( am->ArgMgrDefined("noseq") )
117 loadMode |= gdcm::LD_NOSEQ;
120 /* if unused Param we give up */
121 if ( am->ArgMgrPrintUnusedLabels() )
123 am->ArgMgrUsage(usage);
128 delete am; // ------ we don't need Arguments Manager any longer ------
130 // ====== Deal with a (single level, single Patient) Directory ======
133 //std::cout << "dirIn [" << dirIn << "]" << std::endl;
134 if ( ! gdcm::DirList::IsDirectory(dirIn) )
136 std::cout << "KO : [" << dirIn << "] is not a Directory." << std::endl;
143 std::cout << "OK : [" << dirIn << "] is a Directory." << std::endl;
146 std::string systemCommand;
147 std::string strDirNameout(dirOut); // to please gcc 4
149 std::cout << "Check for output directory :[" << dirOut << "]."
151 if ( ! gdcm::DirList::IsDirectory(dirOut) ) // dirout not found
153 systemCommand = "mkdir " +strDirNameout; // create it!
155 std::cout << systemCommand << std::endl;
156 system (systemCommand.c_str());
157 if ( ! gdcm::DirList::IsDirectory(dirOut) ) // be sure it worked
159 std::cout << "KO : not a dir : [" << dirOut
160 << "] (creation failure ?)" << std::endl;
167 std::cout << "Directory [" << dirOut << "] created." << std::endl;
173 std::cout << "Output Directory [" << dirOut
174 << "] already exists; Used as is." << std::endl;
177 gdcm::DirList dirList(dirIn,false); // gets (at single level) the file list
178 gdcm::DirListType fileList = dirList.GetFilenames();
180 // 'Study Instance UID'
181 // The user is allowed to create his own Study,
182 // keeping the same 'Study Instance UID' for various images
183 // The user may add images to a 'Manufacturer Study',
184 // adding new Series to an already existing Study
185 std::string strStudyUID;
186 if ( !userDefinedStudy)
187 strStudyUID = gdcm::Util::CreateUniqueUID();
189 strStudyUID = studyUID;
192 // 'Serie Instance UID'
193 // The user is allowed to create his own Series,
194 // keeping the same 'Serie Instance UID' for various images
195 // The user shouldn't add any image to a 'Manufacturer Serie'
196 // but there is no way no to prevent him for doing that
198 std::string strSerieUID;
199 if ( !userDefinedSerie)
200 strSerieUID = gdcm::Util::CreateUniqueUID();
202 strSerieUID = serieUID;
204 std::ostringstream str;
206 size_t totalNumberOfPixels = imageDimX*imageDimY * imagetteLineNumber*imagetteRowNumber;
207 int16_t *imageTable = new int16_t[totalNumberOfPixels];
209 //std::cout << " imageTable length " << totalNumberOfPixels;
210 memset(imageTable, 0, totalNumberOfPixels * imagePixelSize);
212 int16_t **tabImageData = new int16_t *[nbOfImagesInVolume];
215 gdcm::FileHelper *fh;
216 std::string fullFilename, lastFilename;
217 float zPositionComponent = 0.0;
221 for( gdcm::DirListType::iterator it = fileList.begin();
222 it != fileList.end();
226 f = gdcm::File::New( );
227 f->SetLoadMode(loadMode);
228 f->SetFileName( it->c_str() );
231 std::cout << "file [" << it->c_str() << "]" << std::endl;
235 std::cout << "fail to load [" << it->c_str() << "]" << std::endl;
240 // Load the pixels in RAM.
242 fh = gdcm::FileHelper::New(f);
243 // Don't convert (Gray Pixels + LUT) into (RGB pixels) ?!?
245 tabImageData[imageNumber] = (int16_t *)fh->GetImageDataRaw();
246 if (!tabImageData[imageNumber])
248 std::cout << "fail to read [" << it->c_str() << std::endl;
254 if (imageNumber == nbOfImagesInVolume)
256 for(imageNumber=0; imageNumber < nbOfImagesInVolume; imageNumber++)
258 int debMove = (imageNumber%imagetteRowNumber) * imageDimX
259 + (imageNumber/imagetteRowNumber) *imageDimX*imageDimY*imagetteRowNumber;
262 std::cout << "imageNumber " << imageNumber << " debMove " << debMove << std::endl;
264 for(int i=0; i<imageDimY; i++)
266 int debLigne = debMove + i*imagetteRowNumber*imageDimX;
268 // std::cout << "numLigne " << i << " debLigne " << debLigne
269 // << ": " << debMove << " + " << i << " * " << imagetteRowNumber*imageDimX << std::endl;
270 for (int j=0; j<imageDimX; j++)
272 //std::cout << "j " << j << std::endl;
274 imageTable[debLigne + j] = *(tabImageData[imageNumber] + i*imageDimY + j);
275 if (imageTable[debLigne + j] < 0) imageTable[debLigne + j]=0;
276 if (imageTable[debLigne + j] > 100) imageTable[debLigne + j]=100;
277 //std::cout << debLigne + j << " : " << imageTable[debLigne + j] << std::endl;
279 if (*(tabImageData[imageNumber] + i*imageDimY + j) < mini)
280 mini=*(tabImageData[imageNumber] + i*imageDimY + j);
281 else if (*(tabImageData[imageNumber] + i*imageDimY + j) > maxi)
282 maxi=*(tabImageData[imageNumber] + i*imageDimY + j);
287 // std::cout << " mini = " << mini << " maxi = " << maxi << std::endl;
288 // just to check (actually, it's useless)
292 for (int k=0; k < totalNumberOfPixels; k++)
294 if (imageTable[k] < mini) mini=imageTable[k];
295 else if (imageTable[k] > maxi) maxi=imageTable[k];
299 // std::cout << " mini = " << mini << " maxi = " << maxi << std::endl;
300 /// \todo : FIXME what do I do ? Smallest/Largest Image Pixel Value is vr=US, Pixels are signed ?!?
304 fh->InsertEntryString(str.str(),0x0028,0x0106, "US"); // Smallest Image Pixel Value
308 fh->InsertEntryString(str.str(),0x0028,0x0107, "US"); // Largest Image Pixel Value
312 // write the imagette
314 // Set the image size
316 str << imageDimX*imagetteRowNumber;
317 fh->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
320 str << imageDimY*imagetteLineNumber;
321 fh->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
323 fh->InsertEntryString(strStudyUID,0x0020,0x000d,"UI");
324 fh->InsertEntryString(strSerieUID,0x0020,0x000e,"UI");
325 fh->InsertEntryString(patName,0x0010,0x0010, "PN"); // Patient's Name
327 fh->SetImageData((uint8_t *)imageTable, totalNumberOfPixels * imagePixelSize);
329 // ==================================================================================================
331 // This is a dirty heuristics, but no other way :-(
333 // if Image Orientation (Patient) is not present
334 // I create one, (as Axial)
335 // if Image Position (Patient) is not present
336 // I create one, incrementing zPositionComponent up by user supplied zSpacing
337 // if Slice Location is not present
338 // I create one, as zPositionComponent
340 // Aware use is free to supply his own one !
343 if (! f->CheckIfEntryExist(0x0020,0x0037) ) // 0020 0037 DS 6 Image Orientation (Patient)
345 fh->InsertEntryString("1.0\\0.0\\0.0\\0.0\\1.0\\0.0",0x0020,0x0037, "DS"); //[1\0\0\0\1\0] : Axial
347 char charImagePosition[256];
349 sprintf(charImagePosition,"%f\\0.0\\0.0",zPositionComponent);
350 zPositionComponent += zSpacing;
352 if (! f->CheckIfEntryExist(0x0020,0x0032) ) //0020 0032 DS 3 Image Position (Patient)
353 fh->InsertEntryString(charImagePosition,0x0020,0x0032, "DS");
355 if (! f->CheckIfEntryExist(0x0020,0x1041) ) // 0020 0x1041 DS 1 Slice Location
357 sprintf(charImagePosition,"%f",zPositionComponent);
358 fh->InsertEntryString(charImagePosition,0x0020,0x1041, "DS");
364 // ==================================================================================================
366 fh->SetWriteTypeToDcmExplVR();
367 fh->SetContentType(gdcm::UNMODIFIED_PIXELS_IMAGE);
370 lastFilename = gdcm::Util::GetName( fullFilename );
371 std::string fullWriteFilename = strDirNameout + gdcm::GDCM_FILESEPARATOR
374 std::cout << "Write : [" << fullWriteFilename << "]" << std::endl;
375 if (!fh->Write(fullWriteFilename))
377 std::cout << "Fail to write :[" << fullWriteFilename << "]"
381 } // end : 'write the imagette'