1 /*=========================================================================
4 Module: $RCSfile: ToInTag.cxx,v $
6 Date: $Date: 2006/06/02 05:46:44 $
7 Version: $Revision: 1.3 $
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
41 typedef std::map<std::string, gdcm::File*> SortedFiles;
43 int main(int argc, char *argv[])
47 " - explores recursively the given directory, ",
48 " - keeps the requested series/ drops the unrequested series ",
49 " - orders the gdcm-readable found Files according to their ",
50 " (0x0010, 0x0010) Patient's Name ",
51 " (0x0020, 0x000e) Series Instance UID ",
52 " (0x0020, 0x0032) Image Position (Patient) ",
53 " (0x0018, 0x1060) Trigger Time ",
54 " (0x0018, 0x1312) In-plane Phase Encoding Direction ",
55 " - fills a single level (*) Directory with *all* the files, ",
56 " converted into a Brucker-like Dicom, InTags compliant ",
57 " (*) actually : creates as many directories as Patients ",
58 " -that shouldn't appear, but being carefull is better ! -",
60 " - fills a tree-like structure of directories as : ",
64 " Images are (sorted by Trigger Time / ",
65 " Encoding Direction (Row, Column) ",
67 " 0x0021, 0x1020 : 'SLICE INDEX' ",
68 " 0x0021, 0x1040 : 'FRAME INDEX' ",
69 " 0x0020, 0x0012 : 'SESSION INDEX' (Acquisition Number) ",
71 " PhilipsToBrucker dirin=rootDirectoryName ",
72 " dirout=outputDirectoryName ",
73 " { [keep= list of seriesNumber to process] ",
74 " | [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 " taggrid : user knows all the images are 'grid' -ie : not 'col', not 'raw'",
89 " extent : DO NOT forget the leading '.' ! ",
90 " skel: name skeleton eg : patName_1.nema -> skel=patName_ ",
91 " split: creates a tree-like structure of directories as : ",
95 " Images are (sorted by Trigger Time / ",
96 " Encoding Direction (Row, Column) ",
97 " noshadowseq: user doesn't want to load Private Sequences ",
98 " noshadow : user doesn't want to load Private groups (odd number) ",
99 " noseq : user doesn't want to load Sequences ",
100 " verbose : user wants to run the program in 'verbose mode' ",
101 " debug : *developer* wants to run the program in 'debug mode' ",
104 // ----- Initialize Arguments Manager ------
106 gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
108 if (argc == 1 || am->ArgMgrDefined("usage"))
110 am->ArgMgrUsage(usage); // Display 'usage'
115 const char *dirNamein;
116 dirNamein = am->ArgMgrGetString("dirin",".");
118 const char *dirNameout;
119 dirNameout = am->ArgMgrGetString("dirout",".");
121 int loadMode = gdcm::LD_ALL;
122 if ( am->ArgMgrDefined("noshadowseq") )
123 loadMode |= gdcm::LD_NOSHADOWSEQ;
126 if ( am->ArgMgrDefined("noshadow") )
127 loadMode |= gdcm::LD_NOSHADOW;
128 if ( am->ArgMgrDefined("noseq") )
129 loadMode |= gdcm::LD_NOSEQ;
132 if (am->ArgMgrDefined("debug"))
133 gdcm::Debug::DebugOn();
135 int verbose = am->ArgMgrDefined("verbose");
136 int split = am->ArgMgrDefined("split");
137 int listonly = am->ArgMgrDefined("listonly");
140 int *seriesToKeep = am->ArgMgrGetListOfInt("keep", &nbSeriesToKeep);
142 int *seriesToDrop = am->ArgMgrGetListOfInt("drop", &nbSeriesToDrop);
144 if ( nbSeriesToKeep!=0 && nbSeriesToDrop!=0)
146 std::cout << "KEEP and DROP are mutually exclusive !" << std::endl;
151 int taggrid = am->ArgMgrDefined("taggrid");
153 int hasSkel = am->ArgMgrDefined("skel");
156 skel = am->ArgMgrGetString("skel");
158 const char *extent = am->ArgMgrGetString("extent",".DCM");
160 // if unused Param we give up
161 if ( am->ArgMgrPrintUnusedLabels() )
163 am->ArgMgrUsage(usage);
167 delete am; // we don't need Argument Manager any longer
169 // ----- Begin Processing -----
171 if ( ! gdcm::DirList::IsDirectory(dirNamein) )
173 std::cout << "KO : [" << dirNamein << "] is not a Directory." << std::endl;
179 std::cout << "OK : [" << dirNamein << "] is a Directory." << std::endl;
182 std::string systemCommand;
184 std::cout << "Check for output directory :[" << dirNameout << "]."
186 if ( ! gdcm::DirList::IsDirectory(dirNameout) ) // dirout not found
188 std::string strDirNameout(dirNameout); // to please gcc 4
189 systemCommand = "mkdir " +strDirNameout; // create it!
191 std::cout << systemCommand << std::endl;
192 system (systemCommand.c_str());
193 if ( ! gdcm::DirList::IsDirectory(dirNameout) ) // be sure it worked
195 std::cout << "KO : not a dir : [" << dirNameout << "] (creation failure ?)" << std::endl;
201 std::cout << "Directory [" << dirNameout << "] created." << std::endl;
206 std::cout << "Output Directory [" << dirNameout << "] already exists; Used as is." << std::endl;
209 std::string strDirNamein(dirNamein);
210 gdcm::DirList dirList(strDirNamein, true); // get recursively the list of files
214 std::cout << "------------List of found files ------------" << std::endl;
218 gdcm::DirListType fileNames;
219 fileNames = dirList.GetFilenames();
220 gdcm::SerieHelper *s; // Needed only to may use SerieHelper::AddSeriesDetail()
221 s = gdcm::SerieHelper::New();
223 std::string token = "%%%"; // Hope it's enough!
225 std::cout << "---------------Print Serie--------------" << std::endl;
226 s->SetDirectory(dirNamein, true); // true : recursive exploration
227 s->SetUseSeriesDetails(true);
228 s->AddSeriesDetail(0x0018, 0x1312);
233 gdcm::FileHelper *fh;
234 std::vector<std::string> tokens;
235 std::vector<std::string> tokensForFileName;
237 std::cout << "---------------Print Unique Series identifiers---------"
239 std::string uniqueSeriesIdentifier;
241 for (gdcm::DirListType::iterator it) = fileNames.begin();
242 it != fileNames.end();
245 std::cout << "File Name : " << *it << std::endl;
246 f = gdcm::File::New();
247 f->SetLoadMode(gdcm::LD_ALL);
248 f->SetFileName( *it );
251 uniqueSeriesIdentifier=s->CreateUniqueSeriesIdentifier(f);
253 uniqueSeriesIdentifier << "]" << std::endl;
259 std::cout << "------------------Print Break levels-----------------" << std::endl;
261 std::string userFileIdentifier;
264 s->AddSeriesDetail(0x0010, 0x0010, false); // Patient's Name
265 s->AddSeriesDetail(0x0020, 0x000e, false); // Series Instance UID
266 s->AddSeriesDetail(0x0020, 0x0032, false); // Image Position (Patient)
267 s->AddSeriesDetail(0x0018, 0x1060, true); // Trigger Time (true: convert to keep numerical order)
268 s->AddSeriesDetail(0x0018, 0x1312, false); // In-plane Phase Encoding Direction
272 for (gdcm::DirListType::iterator it = fileNames.begin();
273 it != fileNames.end();
276 f = gdcm::File::New();
277 f->SetLoadMode(loadMode);
278 f->SetFileName( *it );
282 std::string strSeriesNumber;
286 // keep only requested Series
288 if (nbSeriesToKeep != 0)
290 strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
291 seriesNumber = atoi( strSeriesNumber.c_str() );
292 for (j=0;j<nbSeriesToKeep; j++)
294 if(seriesNumber == seriesToKeep[j])
306 // drop all unrequested Series
308 if (nbSeriesToDrop != 0)
310 strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
311 seriesNumber = atoi( strSeriesNumber.c_str() );
312 for (j=0;j<nbSeriesToDrop; j++)
314 if(seriesNumber == seriesToDrop[j])
327 userFileIdentifier=s->CreateUserDefinedFileIdentifier(f);
329 gdcm::Util::Tokenize (userFileIdentifier, tokens, token);
331 int imageNum; // Within FileName
334 if ( tokens[3] == "gdcmUnfound") // sometimes Trigger Time is not found. CreateUserDefinedFileIdentifier is not aware of the pb.
336 ///this is a trick to build up a lexicographical compliant name :
337 /// eg : fich001.ima vs fich100.ima as opposed to fich1.ima vs fich100.ima
338 std::string name = gdcm::Util::GetName( *it );
341 gdcm::Util::Tokenize (name, tokensForFileName, skel);
342 imageNum = atoi ( tokensForFileName[0].c_str() );
343 // probabely we could write something much more complicated using C++ !
344 sprintf (newName, "%s%06d%s", skel, imageNum, extent);
346 tokensForFileName.clear();
352 userFileIdentifier = tokens[0] + token + tokens[1] + token + tokens[2] + token
353 + tokens[3] + token + tokens[4] + token;
356 std::cout << "[" << userFileIdentifier << "]" << std::endl;
358 // storing in a map ensures automatic sorting !
359 sf[userFileIdentifier] = f;
362 std::string fullFilename, lastFilename;
363 std::string previousPatientName, currentPatientName;
364 std::string previousSerieInstanceUID, currentSerieInstanceUID;
365 std::string previousImagePosition, currentImagePosition;
366 std::string previousPhaseEncodingDirection, currentPhaseEncodingDirection;
367 std::string previousTriggerTime, currentTriggerTime;
369 std::string writeDir, currentWriteDir;
370 std::string currentPatientWriteDir, currentSerieWriteDir,
371 currentPositionWriteDir, currentPhaseEncodingDirectionWriteDir;
373 std::string fullWriteFilename;
374 std::string strExtent(extent);
376 writeDir = gdcm::Util::NormalizePath(dirNameout);
377 SortedFiles::iterator it2;
379 previousPatientName = "";
380 previousSerieInstanceUID = "";
381 previousImagePosition = "";
382 previousPhaseEncodingDirection = "";
383 previousTriggerTime = "";
385 int sliceIndex = 0; // Is incremented *at the beginning* of processing
389 gdcm::File *currentFile;
391 for (it2 = sf.begin() ; it2 != sf.end(); ++it2)
393 currentFile = it2->second;
395 fullFilename = currentFile->GetFileName();
396 lastFilename = gdcm::Util::GetName( fullFilename );
397 std::cout << "Rewrite [" <<lastFilename << "]" << std::endl;
400 gdcm::Util::Tokenize (it2->first, tokens, token);
402 currentPatientName = tokens[0];
403 currentSerieInstanceUID = tokens[1];
404 currentImagePosition = tokens[2];
405 currentTriggerTime = tokens[3];
406 currentPhaseEncodingDirection = tokens[4];
408 if ( currentImagePosition[0] == '-')
409 currentImagePosition[0] = 'M';
410 if ( currentImagePosition[0] == '+')
411 currentImagePosition[0] = 'P';
413 if (previousPatientName != currentPatientName)
415 previousPatientName = currentPatientName;
417 std::cout << "==== new Patient [" << currentPatientName << "]" << std::endl;
419 previousPatientName = currentPatientName;
420 previousSerieInstanceUID = ""; //currentSerieInstanceUID;
421 previousImagePosition = ""; //currentImagePosition;
422 previousTriggerTime = "";
423 previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
425 currentPatientWriteDir = writeDir + currentPatientName;
426 //if ( ! gdcm::DirList::IsDirectory(currentPatientWriteDir) )
428 systemCommand = "mkdir " + currentPatientWriteDir;
430 std::cout << systemCommand << std::endl;
431 system ( systemCommand.c_str() );
435 if (previousSerieInstanceUID != currentSerieInstanceUID)
438 std::cout << "==== === new Serie [" << currentSerieInstanceUID << "]"
442 currentSerieWriteDir = currentPatientWriteDir + gdcm::GDCM_FILESEPARATOR
443 + currentSerieInstanceUID;
444 systemCommand = "mkdir " + currentSerieWriteDir;
445 system (systemCommand.c_str());
447 previousSerieInstanceUID = currentSerieInstanceUID;
448 previousImagePosition = ""; //currentImagePosition;
449 previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
452 if (previousImagePosition != currentImagePosition)
457 std::cout << "=== === === new Position [" << currentImagePosition << "]"
461 currentPositionWriteDir = currentSerieWriteDir + gdcm::GDCM_FILESEPARATOR
462 + currentImagePosition;
463 systemCommand = "mkdir " + currentPositionWriteDir;
464 system (systemCommand.c_str());
466 previousImagePosition = currentImagePosition;
467 previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
469 sliceIndex = 1; // only *one* slice in a given directory
474 // We don't split on Row/Column!
476 if (previousPhaseEncodingDirection != currentPhaseEncodingDirection)
479 std::cout << "==== === === === new PhaseEncodingDirection ["
480 << currentPhaseEncodingDirection << "]" << std::endl;
484 currentPhaseEncodingDirectionWriteDir = currentPositionWriteDir
485 + gdcm::GDCM_FILESEPARATOR
486 + currentPhaseEncodingDirection;
487 systemCommand = "mkdir " + currentPhaseEncodingDirectionWriteDir;
488 system (systemCommand.c_str());
491 previousPhaseEncodingDirection = currentPhaseEncodingDirection;
496 std::cout << "--- --- --- --- --- " << (it2->second)->GetFileName()
499 if ( gdcm::Debug::GetDebugFlag())
500 std::cout << "--- --- --- --- --- " << it2->first << " "
501 << (it2->second)->GetFileName() << " "
502 << gdcm::Util::GetName( fullFilename ) << std::endl;
504 // Transform the image to be 'Brucker-Like'
505 // ----------------------------------------
507 // Deal with 0x0019, 0x1000 : 'FOV'
508 int nX = currentFile->GetXSize();
509 int nY = currentFile->GetYSize();
510 float pxSzX = currentFile->GetXSpacing();
511 float pxSzY = currentFile->GetYSpacing();
513 sprintf(fov, "%f\\%f",nX*pxSzX, nY*pxSzY);
514 if (currentFile->IsVRCoherent(0x0019) == 1 )
515 currentFile->InsertEntryString(fov, 0x0019, 0x1000, " ");
517 currentFile->InsertEntryString(fov, 0x0019, 0x1000, "DS");
520 // Deal with 0x0020, 0x0012 : 'SESSION INDEX' (Acquisition Number)
521 std::string chSessionIndex;
524 chSessionIndex = "1";
527 if (currentPhaseEncodingDirection == "COL" || currentPhaseEncodingDirection == "COL " || currentPhaseEncodingDirection == " COL")
528 chSessionIndex = "1";
529 else if (currentPhaseEncodingDirection == "ROW" || currentPhaseEncodingDirection == "ROW "|| currentPhaseEncodingDirection == " ROW")
530 chSessionIndex = "2";
533 std::cout << "====================== PhaseEncodingDirection "
534 << " neither COL nor ROW (?!?) : [ "
535 << currentPhaseEncodingDirection << "]" << std::endl;
536 chSessionIndex = "1";
539 if (currentFile->IsVRCoherent(0x0020) == 1 )
540 currentFile->InsertEntryString(chSessionIndex, 0x0020, 0x0012, " ");
542 currentFile->InsertEntryString(chSessionIndex, 0x0020, 0x0012, "IS");
544 // Deal with 0x0021, 0x1020 : 'SLICE INDEX'
545 char chSliceIndex[5];
546 sprintf(chSliceIndex, "%04d", sliceIndex);
547 std::string strChSliceIndex(chSliceIndex);
549 // Deal with 0x0021, 0x1040 : 'FRAME INDEX'
550 char chFrameIndex[5];
551 sprintf(chFrameIndex, "%04d", frameIndex);
553 std::string stringVR;
554 if (currentFile->IsVRCoherent(0x0021) == 1 )
559 currentFile->InsertEntryString(strChSliceIndex, 0x0021, 0x1020, stringVR);
560 currentFile->InsertEntryString(chFrameIndex, 0x0021, 0x1040, stringVR);
563 std::string strImagePositionPatient = currentFile->GetEntryString(0x0020, 0x0032 );
564 if (strImagePositionPatient == gdcm::GDCM_UNFOUND)
565 currentFile->InsertEntryString(currentFile->GetEntryString(0x0020, 0x0030), 0x0020, 0x0032, "DS" );
567 std::string strImageOrientationPatient = f->GetEntryString(0x0020, 0x0037 );
568 if (strImageOrientationPatient == gdcm::GDCM_UNFOUND)
569 currentFile->InsertEntryString(currentFile->GetEntryString(0x0020, 0x0035), 0x0020, 0x0037, "DS" );
588 //fullWriteFilename = currentPhaseEncodingDirectionWriteDir + gdcm::GDCM_FILESEPARATOR
589 // + lastFilename + strExtent;
590 fullWriteFilename = currentPositionWriteDir + gdcm::GDCM_FILESEPARATOR
591 + lastFilename + strExtent;
593 fullWriteFilename = currentPatientWriteDir + gdcm::GDCM_FILESEPARATOR
594 + lastFilename + strExtent;
597 systemCommand = "cp " + fullFilename + " " + fullWriteFilename;
598 std::cout << systemCommand << std::endl;
599 system ( systemCommand.c_str() );
602 // Load the pixels in RAM.
604 fh = gdcm::FileHelper::New(currentFile);
605 uint8_t *imageData = fh->GetImageDataRaw(); // Don't convert (Gray Pixels + LUT) into (RGB pixels) ?!?
606 fh->SetWriteTypeToDcmExplVR();
607 // We didn't modify pixels -> keep unchanged the following :
608 // 'Media Storage SOP Class UID' (0x0002,0x0002)
609 // 'SOP Class UID' (0x0008,0x0016)
610 // 'Image Type' (0x0008,0x0008)
611 // 'Conversion Type' (0x0008,0x0064)
613 // Put to Black the burnt-in number.
614 nX = currentFile->GetXSize();
615 nY = currentFile->GetYSize();
616 for(int y=nY-15; y<nY; y++)
617 for(int x=nX/3; x<nX/2+50; x++)
618 imageData[ y*nX*2 + x ] = 0;
620 fh->SetContentType(gdcm::UNMODIFIED_PIXELS_IMAGE);
621 if (!fh->Write(fullWriteFilename))
623 std::cout << "Fail to write :[" << fullWriteFilename << "]"