]> Creatis software - gdcm.git/blob - Example/ToInTag.cxx
Fix troubles with :
[gdcm.git] / Example / ToInTag.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: ToInTag.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/29 10:36:10 $
7   Version:   $Revision: 1.12 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmDocEntry.h"
19 #include "gdcmDicomDir.h"
20 #include "gdcmDicomDirPatient.h"
21 #include "gdcmFile.h"
22 #include "gdcmFileHelper.h"
23 #include "gdcmDirList.h"
24 #include "gdcmDebug.h"
25 #include "gdcmArgMgr.h"
26 #include "gdcmUtil.h"
27 #include "gdcmSerieHelper.h"
28
29 #include <iostream>
30
31 /**
32   * \brief   
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
39   */  
40
41 typedef std::map<std::string, GDCM_NAME_SPACE::File*> SortedFiles;
42
43 int main(int argc, char *argv[]) 
44 {
45    START_USAGE(usage)
46    " \n ToInTag :\n                                                           ",
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!-  ",
59    " or                                                                       ",
60    " - fills a tree-like structure of directories as :                        ",
61    "        - Patient                                                         ",
62    "        -- Serie                                                          ",
63    "        --- Position                                                      ",
64    "        ---- Images (sorted by Trigger Time /                             ",
65    "                               Encoding Direction (Row, Column)           ",
66    "                                                                          ",
67    "      Note : when (0008|1090) [Model Name ] equals 'TrioTim ' :           ",
68    "         - (0008|103e)[Series Description ] is checked for                ",
69    "            '90' (-> COL) or '0' (-> ROW)                                 ",
70    "         - (0x0020, 0x000e) [Series Instance UID] is NOT dealt with,      ",
71    "           since row an col tagging are in 2 different Series             ",
72    "           DO NOT supply a directory holding different exams              ",
73    "           for the same Slice level!                                      ",
74    "      uses :                                                              ",
75    "           0x0021, 0x1020 : 'SLICE INDEX'                                 ",
76    "           0x0021, 0x1040 : 'FRAME INDEX'                                 ",
77    "           0x0020, 0x0012 : 'SESSION INDEX'  (Acquisition Number)         ",
78    "                                                                          ",
79    " usage:                                                                   ",
80    " -----                                                                    ",
81    " ToInTag          dirin=rootDirectoryName                                 ",
82    "                  dirout=outputDirectoryName                              ",
83    "                  {  [keep= list of seriesNumber to process]              ",
84    "                   | [drop= list of seriesNumber to ignore] }             ",
85    "                  [taggrid] [skel]                                        ",
86    "                  [input = {ACR|DCM}]                                     ", 
87    "                  [extent=image suffix (.IMA, .NEMA, .DCM, ...)]          ",
88    "                  [listonly] [split]                                      ",
89    "                  [noshadowseq][noshadow][noseq] [verbose] [debug]        ",
90    "                                                                          ",
91    " dirout : will be created if doesn't exist                                ",
92    " keep : if user wants to process a limited number of series               ",
93    "            he gives the list of 'SeriesNumber' (tag 0020|0011)           ",
94    " drop : if user wants to ignore a limited number of series                ",
95    "            he gives the list of 'SeriesNumber' (tag 0020|0011)           ",   
96    "        SeriesNumber are short enough to be human readable                ",
97    "        e.g : 1030,1035,1043                                              ", 
98    " taggrid : user knows all the images are 'grid' -ie: not 'col', not 'raw'-",
99    " extent : DO NOT forget the leading '.' !                                 ",
100    " skel: name skeleton eg : patName_1.nema -> skel=patName_                 ",
101    " split: creates a tree-like structure of directories as :                 ",
102    "        - Patient                                                         ",
103    "        -- Serie                                                          ",
104    "        --- Position                                                      ",
105    "        ---- Images (sorted by Trigger Time /                             ",
106    "                               Encoding Direction (Row, Column)           ",
107    " noshadowseq: user doesn't want to load Private Sequences                 ",
108    " noshadow : user doesn't want to load Private groups (odd number)         ",
109    " noseq    : user doesn't want to load Sequences                           ",
110    " verbose  : user wants to run the program in 'verbose mode'               ",
111    " debug    : *developer*  wants to run the program in 'debug mode'         ",
112    FINISH_USAGE
113
114    std::cout << "inside ToInTag" << std::endl;
115    
116    // ----- Initialize Arguments Manager ------
117       
118    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
119   
120    if (argc == 1 || am->ArgMgrDefined("usage")) 
121    {
122       am->ArgMgrUsage(usage); // Display 'usage'
123       delete am;
124       return 0;
125    }
126
127    const char *dirNamein;   
128    dirNamein  = am->ArgMgrGetString("dirin","."); 
129
130    const char *dirNameout;   
131    dirNameout  = am->ArgMgrGetString("dirout",".");  
132    
133    int loadMode = GDCM_NAME_SPACE::LD_ALL;
134    if ( am->ArgMgrDefined("noshadowseq") )
135       loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
136    else 
137    {
138    if ( am->ArgMgrDefined("noshadow") )
139          loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
140       if ( am->ArgMgrDefined("noseq") )
141          loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
142    }
143
144    if (am->ArgMgrDefined("debug"))
145       GDCM_NAME_SPACE::Debug::DebugOn();
146       
147    int verbose  = am->ArgMgrDefined("verbose");
148    int split    = am->ArgMgrDefined("split");
149    int listonly = am->ArgMgrDefined("listonly");
150          
151    int nbSeriesToKeep;
152    int *seriesToKeep = am->ArgMgrGetListOfInt("keep", &nbSeriesToKeep);
153    int nbSeriesToDrop;
154    int *seriesToDrop = am->ArgMgrGetListOfInt("drop", &nbSeriesToDrop);
155  
156    if ( nbSeriesToKeep!=0 && nbSeriesToDrop!=0)
157    {
158       std::cout << "KEEP and DROP are mutually exclusive !" << std::endl;
159       delete am;
160       return 0;         
161    }
162    
163    int taggrid = am->ArgMgrDefined("taggrid");
164    
165    int hasSkel = am->ArgMgrDefined("skel");
166    const char *skel;
167    if (hasSkel)
168       skel = am->ArgMgrGetString("skel");   
169       
170    const char *extent  = am->ArgMgrGetString("extent",".DCM");
171    
172    // if unused Param we give up
173    if ( am->ArgMgrPrintUnusedLabels() )
174    { 
175       am->ArgMgrUsage(usage);
176       delete am;
177       return 0;
178    }
179    delete am;  // we don't need Argument Manager any longer
180
181    // ----- Begin Processing -----
182    
183    if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNamein) )
184    {
185       std::cout << "KO : [" << dirNamein << "] is not a Directory." << std::endl;
186       return 0;
187
188    }
189    else
190    {
191       std::cout << "OK : [" << dirNamein << "] is a Directory." << std::endl;
192    }
193
194    std::string systemCommand;
195    
196    std::cout << "Check for output directory :[" << dirNameout << "]."
197              <<std::endl;
198    if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) )    // dirout not found
199    {
200       std::string strDirNameout(dirNameout);          // to please gcc 4
201       systemCommand = "mkdir " +strDirNameout;        // create it!
202       if (verbose)
203          std::cout << systemCommand << std::endl;
204       system (systemCommand.c_str());
205       if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) ) // be sure it worked
206       {
207           std::cout << "KO : not a dir : [" << dirNameout << "] (creation failure ?)" << std::endl;
208       return 0;
209
210       }
211       else
212       {
213         std::cout << "Directory [" << dirNameout << "] created." << std::endl;
214       }
215    }
216    else
217    {
218        std::cout << "Output Directory [" << dirNameout << "] already exists; Used as is." << std::endl;
219    }
220     
221    std::string strDirNamein(dirNamein);
222    GDCM_NAME_SPACE::DirList dirList(strDirNamein, true); // get recursively the list of files
223    
224    if (listonly)
225    {
226       std::cout << "------------List of found files ------------" << std::endl;
227       dirList.Print();
228       std::cout << std::endl;
229    }
230    
231    GDCM_NAME_SPACE::DirListType fileNames;
232    fileNames = dirList.GetFilenames();
233    GDCM_NAME_SPACE::SerieHelper *s;              // Needed to use SerieHelper::AddSeriesDetail()
234    s = GDCM_NAME_SPACE::SerieHelper::New();
235
236    std::string token = "%%%"; // Hope it's enough!
237 /*       
238    std::cout << "---------------Print Serie--------------" << std::endl; 
239    s->SetDirectory(dirNamein, true); // true : recursive exploration 
240    s->SetUseSeriesDetails(true);  
241    s->AddSeriesDetail(0x0018, 0x1312);   
242    s->Print();
243 */
244   
245    GDCM_NAME_SPACE::File *f;
246    GDCM_NAME_SPACE::FileHelper *fh;
247    std::vector<std::string> tokens;
248    std::vector<std::string> tokensForFileName;
249    
250    // For Siemens pb, we need Manufacturer's Model Name
251    
252    GDCM_NAME_SPACE::DirListType::iterator it1 = fileNames.begin();
253    f = GDCM_NAME_SPACE::File::New();
254    f->SetLoadMode(GDCM_NAME_SPACE::LD_ALL);
255    f->SetFileName( *it1 );
256    f->Load();
257    std::string modelName = f->GetEntryString(0x0008,0x1090);
258    f->Delete();   
259    
260 /*   
261    std::cout << "---------------Print Unique Series identifiers---------"  
262              << std::endl;     
263    std::string uniqueSeriesIdentifier;
264  
265    for (GDCM_NAME_SPACE::DirListType::iterator it) = fileNames.begin();  
266                                     it != fileNames.end();
267                                   ++it)
268    {
269       std::cout << "File Name : " << *it << std::endl;
270       f = GDCM_NAME_SPACE::File::New();
271       f->SetLoadMode(GDCM_NAME_SPACE::LD_ALL);
272       f->SetFileName( *it );
273       f->Load();
274         
275       uniqueSeriesIdentifier=s->CreateUniqueSeriesIdentifier(f);
276       std::cout << "                           [" <<
277                uniqueSeriesIdentifier  << "]" << std::endl;       
278       f->Delete();
279    }
280 */
281    
282    if (verbose)
283       std::cout << "------------------Print Break levels-----------------" << std::endl;
284
285    std::string userFileIdentifier; 
286    SortedFiles sf;
287
288    s->AddSeriesDetail(0x0010, 0x0010, false); // Patient's Name
289    // for Siemens TrioTim, don't deal with 'Series Instance UID'
290    if ( !GDCM_NAME_SPACE::Util::DicomStringEqual(modelName,"TrioTim") ) 
291       s->AddSeriesDetail(0x0020, 0x000e, false); // Series Instance UID
292    else
293       s->AddSeriesDetail(0x9999, 0x9999, false); // dirty trick to ignore 'Series Instance UID'
294    s->AddSeriesDetail(0x0020, 0x0032, false); // Image Position (Patient)     
295    s->AddSeriesDetail(0x0018, 0x1060, true);  // Trigger Time (true: convert to keep numerical order)
296    s->AddSeriesDetail(0x0018, 0x1312, false); // In-plane Phase Encoding Direction 
297    s->AddSeriesDetail(0x0008, 0x103e, false); // Series Description (special Siemens ...)  
298
299    //uint8_t *imageData; // Useless : pixels will not be loaded 
300                          //          (images are overwritten)
301          
302    for (GDCM_NAME_SPACE::DirListType::iterator it = fileNames.begin();  
303                                     it != fileNames.end();
304                                   ++it)
305    {
306       f = GDCM_NAME_SPACE::File::New();
307       f->SetLoadMode(loadMode);
308       f->SetFileName( *it );
309       f->Load();
310
311       std::string strSeriesNumber;
312       int seriesNumber;
313       int j;
314
315       // keep only requested Series      
316       bool keep = false;
317       if (nbSeriesToKeep != 0)
318       {     
319          strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
320          seriesNumber = atoi( strSeriesNumber.c_str() );
321          for (j=0;j<nbSeriesToKeep; j++)
322          {
323             if(seriesNumber == seriesToKeep[j])
324             {
325                keep = true;
326                break;
327             }
328          }
329          if ( !keep)
330          {
331             f->Delete();
332             continue;
333          } 
334       }
335       // drop all unrequested Series
336       bool drop = false;
337       if (nbSeriesToDrop != 0)
338       {     
339          strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
340          seriesNumber = atoi( strSeriesNumber.c_str() );
341          for (j=0;j<nbSeriesToDrop; j++)
342          {
343             if(seriesNumber == seriesToDrop[j])
344             { 
345                drop = true;
346                break;
347             }
348         }
349         if (drop)
350         {
351            f->Delete();
352            continue;
353         }
354       }      
355
356       userFileIdentifier=s->CreateUserDefinedFileIdentifier(f); 
357       tokens.clear();
358       GDCM_NAME_SPACE::Util::Tokenize (userFileIdentifier, tokens, token); 
359    
360       int imageNum; // Within FileName
361       char newName[1024];
362       
363       if ( tokens[3] == "gdcmUnfound")  // sometimes Trigger Time is not found. CreateUserDefinedFileIdentifier is not aware of the pb.
364       {
365          ///this is a trick to build up a lexicographical compliant name :
366          ///     eg : fich001.ima vs fich100.ima as opposed to fich1.ima vs fich100.ima
367          std::string name = GDCM_NAME_SPACE::Util::GetName( *it );
368          if (hasSkel)
369          {
370             GDCM_NAME_SPACE::Util::Tokenize (name, tokensForFileName, skel);
371             imageNum = atoi ( tokensForFileName[0].c_str() );
372             // probabely we could write something much more complicated using C++ !
373             sprintf (newName, "%s%06d%s", skel, imageNum, extent);
374             tokens[3] = newName;
375             tokensForFileName.clear();    
376          }
377          else
378             tokens[3] = name;
379  
380  
381          userFileIdentifier = tokens[0] + token + tokens[1] + token + tokens[2] + token 
382                     + tokens[3] + token + tokens[4] + token + tokens[5] + token;
383       }   
384       if (verbose) 
385          std::cout << "[" << userFileIdentifier  << "] : " << *it << std::endl;
386                
387       // storing in a map ensures automatic sorting !      
388       sf[userFileIdentifier] = f;
389    }
390    
391    if (verbose)
392       std::cout << "  " << std::endl;
393       
394    std::string fullFilename, lastFilename;
395    std::string previousPatientName, currentPatientName;
396    std::string previousSerieInstanceUID, currentSerieInstanceUID;
397    std::string previousImagePosition, currentImagePosition;
398    std::string previousPhaseEncodingDirection, currentPhaseEncodingDirection;
399    std::string previousTriggerTime, currentTriggerTime;
400    
401    std::string currentStudyUID;
402    std::string seriesDescription;  
403       
404    std::string writeDir, currentWriteDir;
405    std::string currentPatientWriteDir, currentSerieWriteDir, 
406                currentPositionWriteDir, currentPhaseEncodingDirectionWriteDir;
407
408    std::string fullWriteFilename;
409    std::string strExtent(extent); 
410            
411    writeDir = GDCM_NAME_SPACE::Util::NormalizePath(dirNameout);     
412    SortedFiles::iterator it2;
413  
414    previousPatientName            = "";
415    previousSerieInstanceUID       = "";   
416    previousImagePosition          = "";
417    previousPhaseEncodingDirection = "";
418    previousTriggerTime            = "";
419    
420    int sliceIndex = 0; // Is incremented *at the beginning* of processing
421    int frameIndex;
422    if (taggrid)
423        frameIndex = 0;
424    else
425        frameIndex = 1;
426       
427    int flag       = 0;
428        
429    GDCM_NAME_SPACE::File *currentFile;
430
431    std::string defaultStudyUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
432    std::string defaultSerieUID;
433      
434    for (it2 = sf.begin() ; it2 != sf.end(); ++it2)
435    {  
436       currentFile = it2->second;
437        
438       fullFilename =  currentFile->GetFileName();
439       lastFilename =  GDCM_NAME_SPACE::Util::GetName( fullFilename ); 
440       std::cout <<" ------------------------------------------------------------------------------" 
441                 << std::endl << " Deal with [" << it2->first << "] : ["<<fullFilename << "]" 
442                 << std::endl;
443      
444       tokens.clear();
445       GDCM_NAME_SPACE::Util::Tokenize (it2->first, tokens, token);
446       
447       currentPatientName            = tokens[0];
448       currentSerieInstanceUID       = tokens[1];
449       currentImagePosition          = tokens[2];
450       currentTriggerTime            = tokens[3];
451       currentPhaseEncodingDirection = tokens[4];
452       seriesDescription             = tokens[5];  // For Siemens pb
453
454       if ( currentImagePosition[0] == '-')
455           currentImagePosition[0] = 'M';
456       if ( currentImagePosition[0] == '+')
457           currentImagePosition[0] = 'P'; 
458
459       // Add a default ImagePositionPatient to avoid confusion at post processing time
460       if ( currentFile->GetEntryString(0x0020,0x0032) == GDCM_NAME_SPACE::GDCM_UNFOUND && 
461            currentFile->GetEntryString(0x0020,0x0030) == GDCM_NAME_SPACE::GDCM_UNFOUND )
462       {
463          currentFile->InsertEntryString("0.\\0.\\0.",0x0020, 0x0032, "DS" );
464       }
465
466       // Add a default ImagePositionPatient to avoid confusion at post processing time
467       if ( currentFile->GetEntryString(0x0020,0x0037) == GDCM_NAME_SPACE::GDCM_UNFOUND && 
468            currentFile->GetEntryString(0x0020,0x0035) == GDCM_NAME_SPACE::GDCM_UNFOUND )
469       {
470          currentFile->InsertEntryString("1.\\0.\\0.\\0.\\1.\\0.",0x0020, 0x0037, "DS" );
471       }
472       
473       if (previousPatientName != currentPatientName)
474       {      
475          if ( currentFile->GetEntryString(0x0020,0x000d) == GDCM_NAME_SPACE::GDCM_UNFOUND)
476          {
477             if (verbose)   
478                std::cout << "--- new  Study UID created" << std::endl;
479             defaultStudyUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
480             currentFile->InsertEntryString(defaultStudyUID, 0x0020, 0x000d, "UI" );
481          }
482   
483          previousPatientName = currentPatientName;
484          if (verbose)   
485             std::cout << "==== new Patient  [" << currentPatientName  << "]" << std::endl;
486     
487          previousPatientName            = currentPatientName;
488          previousSerieInstanceUID       = ""; //currentSerieInstanceUID;
489          previousImagePosition          = ""; //currentImagePosition;
490          previousTriggerTime            = "";
491          previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
492   
493          currentPatientWriteDir = writeDir + currentPatientName;
494          //if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(currentPatientWriteDir) )
495            {
496               systemCommand   = "mkdir " + currentPatientWriteDir;
497               if (verbose)
498                  std::cout << systemCommand << std::endl;
499               system ( systemCommand.c_str() );
500          }
501       }
502
503       if ( GDCM_NAME_SPACE::Util::DicomStringEqual(modelName,"TrioTim") ) // for Siemens TrioTim , don't deal with 'Series Instance UID'
504
505       if (previousSerieInstanceUID != currentSerieInstanceUID)
506       {        
507          if (verbose)   
508             std::cout << "==== === new Serie [" << currentSerieInstanceUID << "]"
509                       << std::endl;
510       
511          if ( currentFile->GetEntryString(0x0020,0x000e) == GDCM_NAME_SPACE::GDCM_UNFOUND)
512          {
513             if (verbose)   
514                std::cout << "--- --- new  Serie UID created" << std::endl;
515             defaultSerieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
516             currentFile->InsertEntryString(defaultSerieUID, 0x0020, 0x000e, "UI" );
517          }       
518       
519          if (split)
520          {
521              currentSerieWriteDir  = currentPatientWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
522                              + currentSerieInstanceUID;
523              systemCommand   = "mkdir " + currentSerieWriteDir;  
524              system (systemCommand.c_str());
525          }
526          previousSerieInstanceUID       = currentSerieInstanceUID;
527          previousImagePosition          = ""; //currentImagePosition;
528          previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
529       }
530       // end of modelName != "TrioTim "
531    
532       if (previousImagePosition != currentImagePosition)
533       {
534          frameIndex = 1;
535          flag = 0;        
536          if (verbose)   
537             std::cout << "=== === === new Position [" << currentImagePosition  << "]"
538                       << std::endl;
539          if (split)
540          {
541              currentPositionWriteDir  = currentSerieWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
542                              + currentImagePosition;
543              systemCommand   = "mkdir " + currentPositionWriteDir;     
544              system (systemCommand.c_str()); 
545          }
546          previousImagePosition          = currentImagePosition;
547          previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
548          if (split)
549             sliceIndex = 1; // only *one* slice in a given directory
550          else
551             sliceIndex += 1;
552       }
553
554 // We don't split on Row/Column!
555 /*
556       if (previousPhaseEncodingDirection != currentPhaseEncodingDirection)
557       {        
558          if (verbose)   
559             std::cout << "==== === === === new PhaseEncodingDirection [" 
560                       << currentPhaseEncodingDirection  << "]" << std::endl;
561       
562          if (split)
563          {
564              currentPhaseEncodingDirectionWriteDir  = currentPositionWriteDir 
565                              + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
566                              + currentPhaseEncodingDirection;
567              systemCommand   = "mkdir " + currentPhaseEncodingDirectionWriteDir;     
568              system (systemCommand.c_str());     
569          }      
570     
571          previousPhaseEncodingDirection = currentPhaseEncodingDirection;
572       } 
573 */    
574    
575       if ( GDCM_NAME_SPACE::Debug::GetDebugFlag())
576          std::cout << "--- --- --- --- --- " << it2->first << "  " 
577                    << (it2->second)->GetFileName() << " " 
578                    << GDCM_NAME_SPACE::Util::GetName( fullFilename ) << std::endl;           
579       
580       // Transform the image to be 'Brucker-Like'
581       // ----------------------------------------   
582     
583       // Deal with 0x0019, 0x1000 : 'FOV'
584       int nX = currentFile->GetXSize();
585       int nY = currentFile->GetYSize();
586       float pxSzX = currentFile->GetXSpacing();
587       float pxSzY = currentFile->GetYSpacing();
588       char fov[64];
589       sprintf(fov, "%f\\%f",nX*pxSzX, nY*pxSzY);
590       if (currentFile->IsVRCoherent(0x0019) == 1 )
591          currentFile->InsertEntryString(fov, 0x0019, 0x1000, "  ");
592       else     
593          currentFile->InsertEntryString(fov, 0x0019, 0x1000, "DS");
594
595      
596       // Deal with 0x0020, 0x0012 : 'SESSION INDEX'  (Acquisition Number)
597       std::string chSessionIndex;
598       // CLEANME
599
600       if (taggrid)
601       { 
602          chSessionIndex = "1";
603       }
604       else
605       {
606       /* for SIEMENS MRI :
607         D 0008|1090 [LO] [Manufacturer's Model Name ] [Triotim ]
608         we have to deal with :
609      
610         D 0008|103e [LO]  [Series Description ] [fl2d9_line PA 15 90deg] or anything that contains '90' !
611         D 0008|103e [LO]  [Series Description ] [fl2d9_line PA 15 0deg ]
612         (everything is flagged as 'ROW')
613       */  
614
615          if ( GDCM_NAME_SPACE::Util::DicomStringEqual(modelName,"TrioTim") )  
616          {
617             if (seriesDescription.find("90", 0) != -1)
618                chSessionIndex = "1";  // 90 deg -> COL
619             else if (seriesDescription.find("0", 0)!= -1)
620                chSessionIndex = "2";  // 0 deg -> ROW
621             else
622             {
623                std::cout << "====================== seriesDescription doesn't contain"
624                          << " neither '90' nor '0' (?!?) : ["
625                          << seriesDescription << "]" << std::endl;
626                chSessionIndex = "1";
627             }    
628          } 
629          else  // for all other 'normal' cases
630          {
631             if (currentPhaseEncodingDirection == "COL" || currentPhaseEncodingDirection == "COL " || currentPhaseEncodingDirection == " COL")
632                chSessionIndex = "1";
633             else if (currentPhaseEncodingDirection == "ROW" || currentPhaseEncodingDirection == "ROW "|| currentPhaseEncodingDirection == " ROW")
634                chSessionIndex = "2"; 
635             else
636             {
637                std::cout << "====================== PhaseEncodingDirection "
638                          << " neither COL nor ROW (?!?) : [ "
639                          << currentPhaseEncodingDirection << "]" << std::endl;
640                chSessionIndex = "1";
641             }
642          }
643       }
644       
645       if (currentFile->IsVRCoherent(0x0020) == 1 )     
646          currentFile->InsertEntryString(chSessionIndex, 0x0020, 0x0012, "  ");
647       else
648          currentFile->InsertEntryString(chSessionIndex, 0x0020, 0x0012, "IS");
649  
650       // Deal with  0x0021, 0x1020 : 'SLICE INDEX'
651       char chSliceIndex[5];
652       sprintf(chSliceIndex, "%04d", sliceIndex);
653       std::string strChSliceIndex(chSliceIndex);
654        
655       // Deal with  0x0021, 0x1040 : 'FRAME INDEX' 
656       char chFrameIndex[5];
657       sprintf(chFrameIndex, "%04d", frameIndex);
658
659       std::string stringVR;       
660       if (currentFile->IsVRCoherent(0x0021) == 1 )
661          stringVR = "  ";
662       else
663         stringVR = "IS";
664   
665       currentFile->InsertEntryString(strChSliceIndex, 0x0021, 0x1020, stringVR);
666       currentFile->InsertEntryString(chFrameIndex,    0x0021, 0x1040, stringVR);
667       
668       if (verbose) {     
669          std::cout << "0x0021, 0x1020 : strChSliceIndex " << strChSliceIndex << std::endl;
670          std::cout << "0x0021, 0x1040 : chFrameIndex  "   << chFrameIndex    << std::endl; 
671          std::cout << "0x0020, 0x0012 : chSessionIndex "  << chSessionIndex  << std::endl; 
672       }
673         
674       std::string strImagePositionPatient    = currentFile->GetEntryString(0x0020, 0x0032 );
675       if (strImagePositionPatient == GDCM_NAME_SPACE::GDCM_UNFOUND)
676       {
677          if (verbose)
678             std::cout << "Duplicate ImagePosition into ImagePositionPatient" << std::endl;
679          currentFile->InsertEntryString(currentFile->GetEntryString(0x0020, 0x0030), 0x0020, 0x0032, "DS" );
680       }  
681       
682       std::string strImageOrientationPatient = f->GetEntryString(0x0020, 0x0037 );
683       if (strImageOrientationPatient == GDCM_NAME_SPACE::GDCM_UNFOUND)
684       {
685          if (verbose)
686             std::cout << "Duplicate ImageOrientation into ImageOrientationPatient" << std::endl;          
687          currentFile->InsertEntryString(currentFile->GetEntryString(0x0020, 0x0035), 0x0020, 0x0037, "DS" );       
688       }
689                 
690       if (taggrid)
691          frameIndex++;
692       else     
693       {     
694          if (flag == 0)
695          {       
696             flag = 1;
697          }
698          else
699          {
700             frameIndex++;
701             flag = 0;
702          }
703       } 
704                  
705       if (split)
706       
707          //fullWriteFilename = currentPhaseEncodingDirectionWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR 
708          //                                + lastFilename + strExtent;      
709          fullWriteFilename = currentPositionWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR 
710                                          + lastFilename + strExtent; 
711       else
712          fullWriteFilename = currentPatientWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR 
713                                          + lastFilename + strExtent; 
714             
715       // Load the pixels in RAM.    
716       
717       fh = GDCM_NAME_SPACE::FileHelper::New(currentFile);     
718       uint8_t *imageData = fh->GetImageDataRaw(); // Don't convert (Gray Pixels + LUT) into (RGB pixels) ?!?
719       fh->SetWriteTypeToDcmExplVR();     
720       
721       // Put to Black the burnt-in number.
722       nX = currentFile->GetXSize();
723       nY = currentFile->GetYSize();
724       for(int y=nY-15; y<nY; y++)
725          for(int x=nX/3; x<nX/2+50; x++)
726            imageData[ y*nX*2 + x ] = 0;
727
728       // We didn't make any computation on the pixels -> keep unchanged the following :
729       // 'Media Storage SOP Class UID' (0x0002,0x0002)
730       // 'SOP Class UID'               (0x0008,0x0016)
731       // 'Image Type'                  (0x0008,0x0008)
732       // 'Conversion Type'             (0x0008,0x0064)        
733       fh->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
734       
735       if (!fh->Write(fullWriteFilename))
736       {
737          std::cout << "Fail to write :[" << fullWriteFilename << "]"
738                    << std::endl;
739       } 
740       fh->Delete();                
741    }
742    return 0;
743  }
744