1 /*=========================================================================
4 Module: $RCSfile: PhilipsToBrucker2.cxx,v $
6 Date: $Date: 2006/01/24 03:06:46 $
7 Version: $Revision: 1.5 $
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, 0x1060) Trigger Time ",
55 " (0x0018, 0x1312) In-plane Phase Encoding Direction ",
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 ! -",
61 " - fills a tree-like structure of directories as : ",
65 " Images are (sorted by Trigger Time / ",
66 " Encoding Direction (Row, Column) ",
68 " 0x0021, 0x1020 : 'SLICE INDEX' ",
69 " 0x0021, 0x1040 : 'FRAME INDEX' ",
70 " 0x0020, 0x0012 : 'SESSION INDEX' (Acquisition Number) ",
72 " PhilipsToBrucker dirin=rootDirectoryName ",
73 " dirout=outputDirectoryName ",
74 " { [keep= list of seriesNumber to process] ",
75 " | [drop= list of seriesNumber to ignore] } ",
76 " [input = {ACR|DCM}] ",
77 " [extent=image suffix (.IMA, .NEMA, .DCM, ...)] ",
78 " [listonly] [split] ",
79 " [noshadowseq][noshadow][noseq] [verbose] [debug] ",
81 " dirout : will be created if doesn't exist ",
82 " keep : if user wants to process a limited number of series ",
83 " he gives the list of 'SeriesNumber' (tag 0020|0011) ",
84 " drop : if user wants to ignore a limited number of series ",
85 " he gives the list of 'SeriesNumber' (tag 0020|0011) ",
86 " SeriesNumber are short enough to be human readable ",
87 " e.g : 1030,1035,1043 ",
88 " extent : DO NOT forget the leading '.' ! ",
89 " split: creates a tree-like structure of directories as : ",
93 " Images are (sorted by Trigger Time / ",
94 " Encoding Direction (Row, Column) ",
95 " noshadowseq: user doesn't want to load Private Sequences ",
96 " noshadow : user doesn't want to load Private groups (odd number) ",
97 " noseq : user doesn't want to load Sequences ",
98 " verbose : user wants to run the program in 'verbose mode' ",
99 " debug : *developer* wants to run the program in 'debug mode' ",
102 // ----- Initialize Arguments Manager ------
104 gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
106 if (argc == 1 || am->ArgMgrDefined("usage"))
108 am->ArgMgrUsage(usage); // Display 'usage'
114 dirNamein = am->ArgMgrGetString("dirin",(char *)".");
117 dirNameout = am->ArgMgrGetString("dirout",(char *)".");
119 int loadMode = gdcm::LD_ALL;
120 if ( am->ArgMgrDefined("noshadowseq") )
121 loadMode |= gdcm::LD_NOSHADOWSEQ;
124 if ( am->ArgMgrDefined("noshadow") )
125 loadMode |= gdcm::LD_NOSHADOW;
126 if ( am->ArgMgrDefined("noseq") )
127 loadMode |= gdcm::LD_NOSEQ;
130 if (am->ArgMgrDefined("debug"))
131 gdcm::Debug::DebugOn();
133 bool verbose = am->ArgMgrDefined("verbose");
134 bool split = am->ArgMgrDefined("split");
135 bool listonly = am->ArgMgrDefined("listonly");
138 int *seriesToKeep = am->ArgMgrGetListOfInt("keep", &nbSeriesToKeep);
140 int *seriesToDrop = am->ArgMgrGetListOfInt("drop", &nbSeriesToDrop);
142 if ( nbSeriesToKeep!=0 && nbSeriesToDrop!=0)
144 std::cout << "KEEP and DROP are mutually exclusive !" << std::endl;
149 char *extent = am->ArgMgrGetString("extent",".DCM");
151 // if unused Param we give up
152 if ( am->ArgMgrPrintUnusedLabels() )
154 am->ArgMgrUsage(usage);
158 delete am; // we don't need Argument Manager any longer
160 // ----- Begin Processing -----
162 if ( ! gdcm::DirList::IsDirectory(dirNamein) )
164 std::cout << "KO : [" << dirNamein << "] is not a Directory." << std::endl;
169 std::cout << "OK : [" << dirNamein << "] is a Directory." << std::endl;
172 std::string systemCommand;
174 std::cout << "Check for output directory :[" << dirNameout << "]."
176 if ( ! gdcm::DirList::IsDirectory(dirNameout) ) // dirout not found
178 std::string strDirNameout(dirNameout); // to please gcc 4
179 systemCommand = "mkdir " +strDirNameout; // create it!
181 std::cout << systemCommand << std::endl;
182 system (systemCommand.c_str());
183 if ( ! gdcm::DirList::IsDirectory(dirNameout) ) // be sure it worked
185 std::cout << "KO : not a dir : [" << dirNameout << "] (creation failure ?)" << std::endl;
190 std::cout << "Directory [" << dirNameout << "] created." << std::endl;
195 std::cout << "Output Directory [" << dirNameout << "] already exists; Used as is." << std::endl;
198 std::string strDirNamein(dirNamein);
199 gdcm::DirList dirList(strDirNamein, true); // get recursively the list of files
203 std::cout << "------------List of found files ------------" << std::endl;
207 gdcm::DirListType fileNames;
208 fileNames = dirList.GetFilenames();
209 gdcm::SerieHelper *s; // Needed only to may use SerieHelper::AddSeriesDetail()
210 s = gdcm::SerieHelper::New();
213 std::cout << "---------------Print Serie--------------" << std::endl;
214 s->SetDirectory(dirNamein, true); // true : recursive exploration
215 s->SetUseSeriesDetails(true);
216 s->AddSeriesDetail(0x0018, 0x1312);
221 gdcm::FileHelper *fh;
223 std::cout << "---------------Print Unique Series identifiers---------"
225 std::string uniqueSeriesIdentifier;
227 for (gdcm::DirListType::iterator it = fileNames.begin();
228 it != fileNames.end();
231 std::cout << "File Name : " << *it << std::endl;
232 f = gdcm::File::New();
233 f->SetLoadMode(gdcm::LD_ALL);
234 f->SetFileName( *it );
237 uniqueSeriesIdentifier=s->CreateUniqueSeriesIdentifier(f);
239 uniqueSeriesIdentifier << "]" << std::endl;
245 std::cout << "------------------Print Break levels-----------------" << std::endl;
247 std::string userFileIdentifier;
250 s->AddSeriesDetail(0x0010, 0x0010, false); // Patient's Name
251 s->AddSeriesDetail(0x0020, 0x000e, false); // Series Instance UID
252 s->AddSeriesDetail(0x0020, 0x0032, false); // Image Position (Patient)
253 s->AddSeriesDetail(0x0018, 0x1060, true); // Trigger Time (true: convert to keep numerical order)
254 s->AddSeriesDetail(0x0018, 0x1312, false); // In-plane Phase Encoding Direction
256 for (gdcm::DirListType::iterator it = fileNames.begin();
257 it != fileNames.end();
260 f = gdcm::File::New();
261 f->SetLoadMode(loadMode);
262 f->SetFileName( *it );
265 // keep only requested Series
266 std::string strSeriesNumber;
271 if (nbSeriesToKeep != 0)
273 strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
274 seriesNumber = atoi( strSeriesNumber.c_str() );
275 for (j=0;j<nbSeriesToKeep; j++)
277 if(seriesNumber == seriesToKeep[j])
289 // drop all unrequested Series
291 if (nbSeriesToDrop != 0)
293 strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
294 seriesNumber = atoi( strSeriesNumber.c_str() );
295 for (j=0;j<nbSeriesToDrop; j++)
297 if(seriesNumber == seriesToDrop[j])
310 userFileIdentifier=s->CreateUserDefinedFileIdentifier(f);
311 // userFileIdentifier += "_";
312 //userFileIdentifier += *it;
314 userFileIdentifier << "]" << std::endl;
316 // storing in a map ensures automatic sorting !
317 sf[userFileIdentifier] = f;
320 std::vector<std::string> tokens;
321 std::string fullFilename, lastFilename;
322 std::string previousPatientName, currentPatientName;
323 std::string previousSerieInstanceUID, currentSerieInstanceUID;
324 std::string previousImagePosition, currentImagePosition;
325 std::string previousPhaseEncodingDirection, currentPhaseEncodingDirection;
326 std::string previousTriggerTime, currentTriggerTime;
328 std::string writeDir, currentWriteDir;
329 std::string currentPatientWriteDir, currentSerieWriteDir,
330 currentPositionWriteDir, currentPhaseEncodingDirectionWriteDir;
332 std::string fullWriteFilename;
333 std::string strExtent(extent);
335 writeDir = gdcm::Util::NormalizePath(dirNameout);
336 SortedFiles::iterator it2;
338 previousPatientName = "";
339 previousSerieInstanceUID = "";
340 previousImagePosition = "";
341 previousPhaseEncodingDirection = "";
342 previousTriggerTime = "";
348 gdcm::File *currentFile;
350 for (it2 = sf.begin() ; it2 != sf.end(); ++it2)
352 currentFile = it2->second;
354 fullFilename = currentFile->GetFileName();
355 lastFilename = gdcm::Util::GetName( fullFilename );
356 std::cout << "Try to write" <<lastFilename << std::endl;
359 gdcm::Util::Tokenize (it2->first, tokens, "_");
361 currentPatientName = tokens[0];
362 currentSerieInstanceUID = tokens[1];
363 currentImagePosition = tokens[2];
364 currentTriggerTime = tokens[3];
365 currentPhaseEncodingDirection = tokens[4];
367 if ( currentImagePosition[0] == '-')
368 currentImagePosition[0] = 'M';
369 if ( currentImagePosition[0] == '+')
370 currentImagePosition[0] = 'P';
372 if (previousPatientName != currentPatientName)
374 previousPatientName = currentPatientName;
376 std::cout << "==== new Patient [" << currentPatientName << "]" << std::endl;
378 previousPatientName = currentPatientName;
379 previousSerieInstanceUID = ""; //currentSerieInstanceUID;
380 previousImagePosition = ""; //currentImagePosition;
381 previousTriggerTime = "";
382 previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
384 currentPatientWriteDir = writeDir + currentPatientName;
385 //if ( ! gdcm::DirList::IsDirectory(currentPatientWriteDir) )
387 systemCommand = "mkdir " + currentPatientWriteDir;
389 std::cout << systemCommand << std::endl;
390 system ( systemCommand.c_str() );
394 if (previousSerieInstanceUID != currentSerieInstanceUID)
397 std::cout << "==== === new Serie [" << currentSerieInstanceUID << "]"
401 currentSerieWriteDir = currentPatientWriteDir + gdcm::GDCM_FILESEPARATOR
402 + currentSerieInstanceUID;
403 systemCommand = "mkdir " + currentSerieWriteDir;
404 system (systemCommand.c_str());
406 previousSerieInstanceUID = currentSerieInstanceUID;
407 previousImagePosition = ""; //currentImagePosition;
408 previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
411 if (previousImagePosition != currentImagePosition)
416 std::cout << "=== === === new Position [" << currentImagePosition << "]"
420 currentPositionWriteDir = currentSerieWriteDir + gdcm::GDCM_FILESEPARATOR
421 + currentImagePosition;
422 systemCommand = "mkdir " + currentPositionWriteDir;
423 system (systemCommand.c_str());
425 previousImagePosition = currentImagePosition;
426 previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
430 // We don't split on Row/Column!
432 if (previousPhaseEncodingDirection != currentPhaseEncodingDirection)
435 std::cout << "==== === === === new PhaseEncodingDirection ["
436 << currentPhaseEncodingDirection << "]" << std::endl;
440 currentPhaseEncodingDirectionWriteDir = currentPositionWriteDir
441 + gdcm::GDCM_FILESEPARATOR
442 + currentPhaseEncodingDirection;
443 systemCommand = "mkdir " + currentPhaseEncodingDirectionWriteDir;
444 system (systemCommand.c_str());
447 previousPhaseEncodingDirection = currentPhaseEncodingDirection;
452 std::cout << "--- --- --- --- --- " << (it2->second)->GetFileName()
455 if ( gdcm::Debug::GetDebugFlag())
456 std::cout << "--- --- --- --- --- " << it2->first << " "
457 << (it2->second)->GetFileName() << " "
458 << gdcm::Util::GetName( fullFilename ) << std::endl;
460 // Transform the image to be 'Brucker-Like'
461 // ----------------------------------------
463 // Deal with 0x0019, 0x1000 : 'FOV'
464 int nX = currentFile->GetXSize();
465 int nY = currentFile->GetYSize();
466 float pxSzX = currentFile->GetXSpacing();
467 float pxSzY = currentFile->GetYSpacing();
469 sprintf(fov, "%f\\%f",nX*pxSzX, nY*pxSzY);
470 currentFile->InsertEntryString(fov, 0x0019, 0x1000, "DS");
472 // Deal with 0x0020, 0x0012 : 'SESSION INDEX' (Acquisition Number)
473 std::string chSessionIndex;
474 if (currentPhaseEncodingDirection == "ROW")
475 chSessionIndex = "1";
477 chSessionIndex = "2"; // suppose it's "COLUMN" !
478 currentFile->InsertEntryString(chSessionIndex, 0x0020, 0x0012, "IS");
480 // Deal with 0x0021, 0x1020 : 'SLICE INDEX'
481 char chSliceIndex[5];
482 sprintf(chSliceIndex, "%04d", sliceIndex);
483 std::string strChSliceIndex(chSliceIndex);
484 currentFile->InsertEntryString(strChSliceIndex, 0x0021, 0x1020, "IS");
486 // Deal with 0x0021, 0x1040 : 'FRAME INDEX'
487 char chFrameIndex[5];
488 sprintf(chFrameIndex, "%04d", frameIndex);
489 currentFile->InsertEntryString(chFrameIndex, 0x0021, 0x1040, "IS");
503 //fullWriteFilename = currentPhaseEncodingDirectionWriteDir + gdcm::GDCM_FILESEPARATOR
504 // + lastFilename + strExtent;
505 fullWriteFilename = currentPositionWriteDir + gdcm::GDCM_FILESEPARATOR
506 + lastFilename + strExtent;
508 fullWriteFilename = currentPatientWriteDir + gdcm::GDCM_FILESEPARATOR
509 + lastFilename + strExtent;
512 systemCommand = "cp " + fullFilename + " " + fullWriteFilename;
513 std::cout << systemCommand << std::endl;
514 system ( systemCommand.c_str() );
517 // Load the pixels in RAM.
519 fh = gdcm::FileHelper::New(currentFile);
520 fh->GetImageDataRaw(); // Don't convert (Gray Pixels + LUT) into (RGB pixels) ?!?
521 fh->SetWriteTypeToDcmExplVR();
522 if (!fh->Write(fullWriteFilename))
524 std::cout << "Fail to write :[" << fullWriteFilename << "]"