1 /*=========================================================================
4 Module: $RCSfile: PhilipsToBrucker.cxx,v $
6 Date: $Date: 2006/01/26 15:52:42 $
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 =========================================================================*/
18 #include "gdcmDocEntry.h"
19 #include "gdcmDicomDir.h"
20 #include "gdcmDicomDirPatient.h"
22 #include "gdcmFileHelper.h"
23 #include "gdcmDirList.h"
24 #include "gdcmDebug.h"
25 #include "gdcmArgMgr.h"
27 #include "gdcmSerieHelper.h"
33 * - explores recursively the given directory
34 * - keeps the requested series
35 * - orders the gdcm-readable found Files
36 * according to their Patient/Study/Serie/Image characteristics
37 * - fills a single level Directory with *all* the files,
38 * converted into a Brucker-like Dicom, Intags compliant
42 typedef std::map<std::string, gdcm::File*> SortedFiles;
44 int main(int argc, char *argv[])
47 " \n PhilipsToBrucker :\n ",
48 " - explores recursively the given directory, ",
49 " - keeps the requested series/ drops the unrequested series ",
50 " - orders the gdcm-readable found Files according to their ",
51 " (0x0010, 0x0010) Patient's Name ",
52 " (0x0020, 0x000e) Series Instance UID ",
53 " (0x0020, 0x0032) Image Position (Patient) ",
54 " (0x0018, 0x1312) In-plane Phase Encoding Direction ",
55 " (0x0018, 0x1060) Trigger Time ",
56 " - fills a single level (*) Directory with *all* the files, ",
57 " converted into a Brucker-like Dicom, InTags compliant ",
58 " (*) actually : creates as many directories as Patients ",
59 " -that shouldn't appear, but being carefull is better ! -",
62 " PhilipsToBrucker dirin=rootDirectoryName ",
63 " dirout=outputDirectoryName ",
64 " { [keep= list of seriesNumber to process] ",
65 " | [drop= list of seriesNumber to ignore] } ",
66 " [extent=image suffix (.IMA, .NEMA, .DCM, ...)] ",
67 " [noshadowseq][noshadow][noseq] [verbose] [debug] ",
69 " dirout : will be created if doesn't exist ",
70 " keep : if user wants to process a limited number of series ",
71 " he gives the list of 'SeriesNumber' (tag 0020|0011) ",
72 " drop : if user wants to ignore a limited number of series ",
73 " he gives the list of 'SeriesNumber' (tag 0020|0011) ",
74 " SeriesNumber are short enough to be human readable ",
75 " e.g : 1030,1035,1043 ",
76 " extent : DO NOT forget the leading '.' ! ",
77 " noshadowseq: user doesn't want to load Private Sequences ",
78 " noshadow : user doesn't want to load Private groups (odd number) ",
79 " noseq : user doesn't want to load Sequences ",
80 " verbose : user wants to run the program in 'verbose mode' ",
81 " debug : *developer* wants to run the program in 'debug mode' ",
84 // ----- Initialize Arguments Manager ------
86 gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
88 if (argc == 1 || am->ArgMgrDefined("usage"))
90 am->ArgMgrUsage(usage); // Display 'usage'
95 const char *dirNamein;
96 dirNamein = am->ArgMgrGetString("dirin",".");
98 const char *dirNameout;
99 dirNameout = am->ArgMgrGetString("dirout",".");
101 int loadMode = gdcm::LD_ALL;
102 if ( am->ArgMgrDefined("noshadowseq") )
103 loadMode |= gdcm::LD_NOSHADOWSEQ;
106 if ( am->ArgMgrDefined("noshadow") )
107 loadMode |= gdcm::LD_NOSHADOW;
108 if ( am->ArgMgrDefined("noseq") )
109 loadMode |= gdcm::LD_NOSEQ;
112 if (am->ArgMgrDefined("debug"))
113 gdcm::Debug::DebugOn();
115 bool verbose = am->ArgMgrDefined("verbose");
118 int *seriesToKeep = am->ArgMgrGetListOfInt("keep", &nbSeriesToKeep);
120 int *seriesToDrop = am->ArgMgrGetListOfInt("drop", &nbSeriesToDrop);
122 if ( nbSeriesToKeep!=0 && nbSeriesToDrop!=0)
124 std::cout << "KEEP and DROP are mutually exclusive !" << std::endl;
129 const char *extent = am->ArgMgrGetString("extent",".DCM");
131 // if unused Param we give up
132 if ( am->ArgMgrPrintUnusedLabels() )
134 am->ArgMgrUsage(usage);
138 delete am; // we don't need Argument Manager any longer
140 // ----- Begin Processing -----
142 if ( ! gdcm::DirList::IsDirectory(dirNamein) )
144 std::cout << "KO : [" << dirNamein << "] is not a Directory : " << std::endl;
149 std::cout << "OK : [" << dirNamein << "]" << " is a Directory" << std::endl;
153 std::string systemCommand;
154 if ( ! gdcm::DirList::IsDirectory(dirNameout) ) // dirout not found
156 std::string strDirNameout(dirNameout); // to please gcc 4
157 systemCommand = "mkdir " +strDirNameout; // create it!
159 std::cout << systemCommand << std::endl;
160 system (systemCommand.c_str());
161 if ( ! gdcm::DirList::IsDirectory(dirNameout) ) // be sure it worked
163 std::cout << "KO : not a dir : [" << dirNameout << "] (creation failure ?)" << std::endl;
168 std::cout << "Directory [" << dirNameout << "]" << " created" << std::endl;
173 std::cout << "Output Directory [" << dirNameout << "]" << " already exists; Used as is." << std::endl;
175 std::string strDirNamein(dirNamein);
176 gdcm::DirList dirList(strDirNamein, true); // get recursively the list of files
179 std::cout << "---------------List of found files ------------" << std::endl;
183 gdcm::DirListType fileNames;
184 fileNames = dirList.GetFilenames();
185 gdcm::SerieHelper *s; // Needed only to may use SerieHelper::AddSeriesDetail()
186 s = gdcm::SerieHelper::New();
189 std::cout << "---------------Print Serie--------------" << std::endl;
190 s->SetDirectory(dirNamein, true); // true : recursive exploration
191 s->SetUseSeriesDetails(true);
192 s->AddSeriesDetail(0x0018, 0x1312);
197 gdcm::FileHelper *fh;
199 std::cout << "---------------Print Unique Series identifiers---------"
201 std::string uniqueSeriesIdentifier;
203 for (gdcm::DirListType::iterator it = fileNames.begin();
204 it != fileNames.end();
207 std::cout << "File Name : " << *it << std::endl;
208 f = gdcm::File::New();
209 f->SetLoadMode(gdcm::LD_ALL);
210 f->SetFileName( *it );
213 uniqueSeriesIdentifier=s->CreateUniqueSeriesIdentifier(f);
215 uniqueSeriesIdentifier << "]" << std::endl;
221 std::cout << "------------------Print Break levels-----------------"
224 std::string userFileIdentifier;
227 s->AddSeriesDetail(0x0010, 0x0010, false); // Patient's Name
228 s->AddSeriesDetail(0x0020, 0x000e, false); // Series Instance UID
229 s->AddSeriesDetail(0x0020, 0x0032, false); // Image Position (Patient)
230 s->AddSeriesDetail(0x0018, 0x1312, false); // In-plane Phase Encoding Direction
231 s->AddSeriesDetail(0x0018, 0x1060, true); // Trigger Time
233 for (gdcm::DirListType::iterator it = fileNames.begin();
234 it != fileNames.end();
237 f = gdcm::File::New();
238 f->SetLoadMode(loadMode);
239 f->SetFileName( *it );
242 // keep only requested Series
243 std::string strSeriesNumber;
248 if (nbSeriesToKeep != 0)
250 strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
251 seriesNumber = atoi( strSeriesNumber.c_str() );
252 for (j=0;j<nbSeriesToKeep; j++)
254 if(seriesNumber == seriesToKeep[j])
266 // drop all unrequested Series
268 if (nbSeriesToDrop != 0)
270 strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
271 seriesNumber = atoi( strSeriesNumber.c_str() );
272 for (j=0;j<nbSeriesToDrop; j++)
274 if(seriesNumber == seriesToDrop[j])
287 userFileIdentifier=s->CreateUserDefinedFileIdentifier(f);
288 //std::cout << " [" <<
289 // userFileIdentifier << "]" << std::endl;
291 // storing in a map ensures automatic sorting !
292 sf[userFileIdentifier] = f;
295 std::vector<std::string> tokens;
296 std::string fullFilename, lastFilename;
297 std::string previousPatientName, currentPatientName;
298 std::string previousSerieInstanceUID, currentSerieInstanceUID;
299 std::string previousImagePosition, currentImagePosition;
300 std::string previousPhaseEncodingDirection, currentPhaseEncodingDirection;
302 std::string writeDir, currentWriteDir;
304 writeDir = gdcm::Util::NormalizePath(dirNameout);
305 SortedFiles::iterator it2;
307 previousPatientName = "";
308 previousSerieInstanceUID = "";
309 previousImagePosition = "";
310 previousPhaseEncodingDirection = "";
315 gdcm::File *currentFile;
317 for (it2 = sf.begin() ; it2 != sf.end(); ++it2)
319 currentFile = it2->second;
321 fullFilename = currentFile->GetFileName();
322 lastFilename = gdcm::Util::GetName( fullFilename );
326 gdcm::Util::Tokenize (it2->first, tokens, "_");
328 currentPatientName = tokens[0];
329 currentSerieInstanceUID = tokens[1];
330 currentImagePosition = tokens[2];
331 currentPhaseEncodingDirection = tokens[3];
333 if (previousPatientName != currentPatientName)
335 previousPatientName = currentPatientName;
337 std::cout << "==== new Patient " << currentPatientName << std::endl;
338 previousPatientName = currentPatientName;
339 previousSerieInstanceUID = ""; //currentSerieInstanceUID;
340 previousImagePosition = ""; //currentImagePosition;
341 previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
343 currentWriteDir = writeDir + currentPatientName;
344 if ( ! gdcm::DirList::IsDirectory(currentWriteDir) )
346 systemCommand = "mkdir " + currentWriteDir;
348 std::cout << systemCommand << std::endl;
349 system ( systemCommand.c_str() );
353 if (previousSerieInstanceUID != currentSerieInstanceUID)
356 std::cout << "==== === new Serie " << currentSerieInstanceUID
358 previousSerieInstanceUID = currentSerieInstanceUID;
359 previousImagePosition = ""; //currentImagePosition;
360 previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
363 if (previousImagePosition != currentImagePosition)
366 std::cout << "=== === === new Position " << currentImagePosition
368 previousImagePosition = currentImagePosition;
369 previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
373 if (previousPhaseEncodingDirection != currentPhaseEncodingDirection)
376 std::cout << "==== === === === new PhaseEncodingDirection "
377 << currentPhaseEncodingDirection << std::endl;
378 previousPhaseEncodingDirection = currentPhaseEncodingDirection;
382 std::cout << "--- --- --- --- --- " << (it2->second)->GetFileName()
384 if ( gdcm::Debug::GetDebugFlag())
385 std::cout << "--- --- --- --- --- " << it2->first << " "
386 << (it2->second)->GetFileName() << " "
387 << gdcm::Util::GetName( fullFilename ) << std::endl;
389 // Transform the image to be 'Brucker-Like'
390 // ----------------------------------------
392 // Deal with 0x0019, 0x1000 : 'FOV'
393 int nX = currentFile->GetXSize();
394 int nY = currentFile->GetYSize();
395 float pxSzX = currentFile->GetXSpacing();
396 float pxSzY = currentFile->GetYSpacing();
398 sprintf(fov, "%f\\%f",nX*pxSzX, nY*pxSzY);
399 currentFile->InsertEntryString(fov, 0x0019, 0x1000, "DS");
401 // Deal with 0x0020, 0x0012 : 'SESSION INDEX'
402 std::string chSessionIndex;
403 if (currentPhaseEncodingDirection == "ROW")
404 chSessionIndex = "1";
406 chSessionIndex = "2"; // suppose it's "COLUMN" !
407 currentFile->InsertEntryString(chSessionIndex, 0x0020, 0x0012, "IS");
409 // Deal with 0x0021, 01020 : 'SLICE INDEX'
410 char chSliceIndex[5];
411 sprintf(chSliceIndex, "%04d", sliceIndex);
412 std::string strChSliceIndex(chSliceIndex);
413 currentFile->InsertEntryString(strChSliceIndex, 0x0021, 0x1020, "IS");
415 // Deal with 0x0021, 01040 : 'FRAME INDEX'
416 char chFrameIndex[5];
417 sprintf(chFrameIndex, "%04d", frameIndex);
418 currentFile->InsertEntryString(chFrameIndex, 0x0021, 0x1040, "IS");
420 std::string strExtent(extent);
421 std::string fullWriteFilename = currentWriteDir + gdcm::GDCM_FILESEPARATOR + lastFilename + strExtent;
424 systemCommand = "cp " + fullFilename + " " + fullWriteFilename;
425 std::cout << systemCommand << std::endl;
426 system ( systemCommand.c_str() );
429 // Load the pixels in RAM.
431 fh = gdcm::FileHelper::New(currentFile);
432 fh->GetImageDataRaw(); // Don't convert (Gray Pixels + LUT) into (RGB pixels) ?!?
433 fh->SetWriteTypeToDcmExplVR();
434 fh->Write(fullWriteFilename);