]> Creatis software - gdcm.git/blob - Example/ToInTag.cxx
Works for more old ACR-NEMA images
[gdcm.git] / Example / ToInTag.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: ToInTag.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/06/30 09:58:08 $
7   Version:   $Revision: 1.6 $
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::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 are (sorted by Trigger Time /                          ",
65    "                     Encoding Direction (Row, Column)                     ",
66    "      use :                                                               ",
67    "           0x0021, 0x1020 : 'SLICE INDEX'                                 ",
68    "           0x0021, 0x1040 : 'FRAME INDEX'                                 ",
69    "           0x0020, 0x0012 : 'SESSION INDEX'  (Acquisition Number)         ",
70    " usage:                                                                   ",
71    " PhilipsToBrucker dirin=rootDirectoryName                                 ",
72    "                  dirout=outputDirectoryName                              ",
73    "                  {  [keep= list of seriesNumber to process]              ",
74    "                   | [drop= list of seriesNumber to ignore] }             ",
75    "                  [taggrid] [skel]                                        ",
76    "                  [input = {ACR|DCM}]                                     ", 
77    "                  [extent=image suffix (.IMA, .NEMA, .DCM, ...)]          ",
78    "                  [listonly] [split]                                      ",
79    "                  [noshadowseq][noshadow][noseq] [verbose] [debug]        ",
80    "                                                                          ",
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 :                 ",
92    "        - Patient                                                         ",
93    "        -- Serie                                                          ",
94    "        --- Position                                                      ",
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'         ",
102    FINISH_USAGE
103
104    std::cout << "inside ToInTag" << std::endl;
105    
106    // ----- Initialize Arguments Manager ------
107       
108    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
109   
110    if (argc == 1 || am->ArgMgrDefined("usage")) 
111    {
112       am->ArgMgrUsage(usage); // Display 'usage'
113       delete am;
114       return 0;
115    }
116
117    const char *dirNamein;   
118    dirNamein  = am->ArgMgrGetString("dirin","."); 
119
120    const char *dirNameout;   
121    dirNameout  = am->ArgMgrGetString("dirout",".");  
122    
123    int loadMode = gdcm::LD_ALL;
124    if ( am->ArgMgrDefined("noshadowseq") )
125       loadMode |= gdcm::LD_NOSHADOWSEQ;
126    else 
127    {
128    if ( am->ArgMgrDefined("noshadow") )
129          loadMode |= gdcm::LD_NOSHADOW;
130       if ( am->ArgMgrDefined("noseq") )
131          loadMode |= gdcm::LD_NOSEQ;
132    }
133
134    if (am->ArgMgrDefined("debug"))
135       gdcm::Debug::DebugOn();
136       
137    int verbose  = am->ArgMgrDefined("verbose");
138    int split    = am->ArgMgrDefined("split");
139    int listonly = am->ArgMgrDefined("listonly");
140          
141    int nbSeriesToKeep;
142    int *seriesToKeep = am->ArgMgrGetListOfInt("keep", &nbSeriesToKeep);
143    int nbSeriesToDrop;
144    int *seriesToDrop = am->ArgMgrGetListOfInt("drop", &nbSeriesToDrop);
145  
146    if ( nbSeriesToKeep!=0 && nbSeriesToDrop!=0)
147    {
148       std::cout << "KEEP and DROP are mutually exclusive !" << std::endl;
149       delete am;
150       return 0;         
151    }
152    
153    int taggrid = am->ArgMgrDefined("taggrid");
154    
155    int hasSkel = am->ArgMgrDefined("skel");
156    const char *skel;
157    if (hasSkel)
158       skel = am->ArgMgrGetString("skel");   
159       
160    const char *extent  = am->ArgMgrGetString("extent",".DCM");
161    
162    // if unused Param we give up
163    if ( am->ArgMgrPrintUnusedLabels() )
164    { 
165       am->ArgMgrUsage(usage);
166       delete am;
167       return 0;
168    }
169    delete am;  // we don't need Argument Manager any longer
170
171    // ----- Begin Processing -----
172    
173    if ( ! gdcm::DirList::IsDirectory(dirNamein) )
174    {
175       std::cout << "KO : [" << dirNamein << "] is not a Directory." << std::endl;
176       return 0;
177
178    }
179    else
180    {
181       std::cout << "OK : [" << dirNamein << "] is a Directory." << std::endl;
182    }
183
184    std::string systemCommand;
185    
186    std::cout << "Check for output directory :[" << dirNameout << "]."
187              <<std::endl;
188    if ( ! gdcm::DirList::IsDirectory(dirNameout) )    // dirout not found
189    {
190       std::string strDirNameout(dirNameout);          // to please gcc 4
191       systemCommand = "mkdir " +strDirNameout;        // create it!
192       if (verbose)
193          std::cout << systemCommand << std::endl;
194       system (systemCommand.c_str());
195       if ( ! gdcm::DirList::IsDirectory(dirNameout) ) // be sure it worked
196       {
197           std::cout << "KO : not a dir : [" << dirNameout << "] (creation failure ?)" << std::endl;
198       return 0;
199
200       }
201       else
202       {
203         std::cout << "Directory [" << dirNameout << "] created." << std::endl;
204       }
205    }
206    else
207    {
208        std::cout << "Output Directory [" << dirNameout << "] already exists; Used as is." << std::endl;
209    }
210     
211    std::string strDirNamein(dirNamein);
212    gdcm::DirList dirList(strDirNamein, true); // get recursively the list of files
213    
214    if (listonly)
215    {
216       std::cout << "------------List of found files ------------" << std::endl;
217       dirList.Print();
218       std::cout << std::endl;
219    }
220    
221    gdcm::DirListType fileNames;
222    fileNames = dirList.GetFilenames();
223    gdcm::SerieHelper *s;              // Needed only to may use SerieHelper::AddSeriesDetail()
224    s = gdcm::SerieHelper::New();
225
226    std::string token = "%%%"; // Hope it's enough!
227 /*       
228    std::cout << "---------------Print Serie--------------" << std::endl; 
229    s->SetDirectory(dirNamein, true); // true : recursive exploration 
230    s->SetUseSeriesDetails(true);  
231    s->AddSeriesDetail(0x0018, 0x1312);   
232    s->Print();
233 */
234   
235    gdcm::File *f;
236    gdcm::FileHelper *fh;
237    std::vector<std::string> tokens;
238    std::vector<std::string> tokensForFileName;
239 /*   
240    std::cout << "---------------Print Unique Series identifiers---------"  
241              << std::endl;     
242    std::string uniqueSeriesIdentifier;
243  
244    for (gdcm::DirListType::iterator it) = fileNames.begin();  
245                                     it != fileNames.end();
246                                   ++it)
247    {
248       std::cout << "File Name : " << *it << std::endl;
249       f = gdcm::File::New();
250       f->SetLoadMode(gdcm::LD_ALL);
251       f->SetFileName( *it );
252       f->Load();
253         
254       uniqueSeriesIdentifier=s->CreateUniqueSeriesIdentifier(f);
255       std::cout << "                           [" <<
256                uniqueSeriesIdentifier  << "]" << std::endl;       
257       f->Delete();
258    }
259 */
260    
261    if (verbose)
262       std::cout << "------------------Print Break levels-----------------" << std::endl;
263
264    std::string userFileIdentifier; 
265    SortedFiles sf;
266
267    s->AddSeriesDetail(0x0010, 0x0010, false); // Patient's Name
268    s->AddSeriesDetail(0x0020, 0x000e, false); // Series Instance UID
269    s->AddSeriesDetail(0x0020, 0x0032, false); // Image Position (Patient)     
270    s->AddSeriesDetail(0x0018, 0x1060, true);  // Trigger Time (true: convert to keep numerical order)
271    s->AddSeriesDetail(0x0018, 0x1312, false); // In-plane Phase Encoding Direction 
272
273    //uint8_t *imageData; // Useless : pixels will not be loaded 
274                          //          (images are overwritten)
275          
276    for (gdcm::DirListType::iterator it = fileNames.begin();  
277                                     it != fileNames.end();
278                                   ++it)
279    {
280       f = gdcm::File::New();
281       f->SetLoadMode(loadMode);
282       f->SetFileName( *it );
283       f->Load();
284       
285
286       std::string strSeriesNumber;
287       int seriesNumber;
288       int j;
289
290       // keep only requested Series      
291       bool keep = false;
292       if (nbSeriesToKeep != 0)
293       {     
294          strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
295          seriesNumber = atoi( strSeriesNumber.c_str() );
296          for (j=0;j<nbSeriesToKeep; j++)
297          {
298             if(seriesNumber == seriesToKeep[j])
299             {
300                keep = true;
301                break;
302             }
303          }
304          if ( !keep)
305          {
306             f->Delete();
307             continue;
308          } 
309       }
310       // drop all unrequested Series
311       bool drop = false;
312       if (nbSeriesToDrop != 0)
313       {     
314          strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
315          seriesNumber = atoi( strSeriesNumber.c_str() );
316          for (j=0;j<nbSeriesToDrop; j++)
317          {
318             if(seriesNumber == seriesToDrop[j])
319             { 
320                drop = true;
321                break;
322             }
323         }
324         if (drop)
325         {
326            f->Delete();
327            continue;
328         }
329       }      
330
331       userFileIdentifier=s->CreateUserDefinedFileIdentifier(f); 
332       tokens.clear();
333       gdcm::Util::Tokenize (userFileIdentifier, tokens, token); 
334    
335       int imageNum; // Within FileName
336       char newName[1024];
337       
338       if ( tokens[3] == "gdcmUnfound")  // sometimes Trigger Time is not found. CreateUserDefinedFileIdentifier is not aware of the pb.
339       {
340          ///this is a trick to build up a lexicographical compliant name :
341          ///     eg : fich001.ima vs fich100.ima as opposed to fich1.ima vs fich100.ima
342          std::string name = gdcm::Util::GetName( *it );
343          if (hasSkel)
344          {
345             gdcm::Util::Tokenize (name, tokensForFileName, skel);
346             imageNum = atoi ( tokensForFileName[0].c_str() );
347             // probabely we could write something much more complicated using C++ !
348             sprintf (newName, "%s%06d%s", skel, imageNum, extent);
349             tokens[3] = newName;
350             tokensForFileName.clear();    
351          }
352          else
353             tokens[3] = name;
354  
355  
356          userFileIdentifier = tokens[0] + token + tokens[1] + token + tokens[2] + token 
357                     + tokens[3] + token + tokens[4] + token;
358       }   
359       if (verbose) 
360          std::cout << "[" << userFileIdentifier  << "]" << std::endl;
361                
362       // storing in a map ensures automatic sorting !      
363       sf[userFileIdentifier] = f;
364    }
365    
366    if (verbose)
367       std::cout << "  " << std::endl;
368       
369    std::string fullFilename, lastFilename;
370    std::string previousPatientName, currentPatientName;
371    std::string previousSerieInstanceUID, currentSerieInstanceUID;
372    std::string previousImagePosition, currentImagePosition;
373    std::string previousPhaseEncodingDirection, currentPhaseEncodingDirection;
374    std::string previousTriggerTime, currentTriggerTime;
375    
376    std::string currentStudyUID;   
377       
378    std::string writeDir, currentWriteDir;
379    std::string currentPatientWriteDir, currentSerieWriteDir, 
380                currentPositionWriteDir, currentPhaseEncodingDirectionWriteDir;
381
382    std::string fullWriteFilename;
383    std::string strExtent(extent); 
384            
385    writeDir = gdcm::Util::NormalizePath(dirNameout);     
386    SortedFiles::iterator it2;
387  
388    previousPatientName            = "";
389    previousSerieInstanceUID       = "";   
390    previousImagePosition          = "";
391    previousPhaseEncodingDirection = "";
392    previousTriggerTime            = "";
393    
394    int sliceIndex = 0; // Is incremented *at the beginning* of processing
395    int frameIndex;
396    if (taggrid)
397        frameIndex = 0;
398    else
399        frameIndex = 1;
400               
401    int flag       = 0;
402        
403    gdcm::File *currentFile;
404
405    std::string defaultStudyUID =  gdcm::Util::CreateUniqueUID();
406    std::string defaultSerieUID; 
407      
408    for (it2 = sf.begin() ; it2 != sf.end(); ++it2)
409    {  
410       currentFile = it2->second;
411        
412       fullFilename =  currentFile->GetFileName();
413       lastFilename =  gdcm::Util::GetName( fullFilename ); 
414       std::cout << " --------------------------------------------------"
415                 << " Rewrite [" <<lastFilename << "]" << std::endl;
416      
417       tokens.clear();
418       gdcm::Util::Tokenize (it2->first, tokens, token);
419       
420       currentPatientName            = tokens[0];
421       currentSerieInstanceUID       = tokens[1];
422       currentImagePosition          = tokens[2];
423       currentTriggerTime            = tokens[3];
424       currentPhaseEncodingDirection = tokens[4]; 
425
426       if ( currentImagePosition[0] == '-')
427           currentImagePosition[0] = 'M';
428       if ( currentImagePosition[0] == '+')
429           currentImagePosition[0] = 'P'; 
430       
431       if (previousPatientName != currentPatientName)
432       {      
433          if ( currentFile->GetEntryString(0x0020,0x000d) == gdcm::GDCM_UNFOUND)
434          {
435             if (verbose)   
436                std::cout << "--- new  Study UID created" << std::endl;
437             defaultStudyUID =  gdcm::Util::CreateUniqueUID();
438             currentFile->InsertEntryString(defaultStudyUID, 0x0020, 0x000d, "UI" );
439          }
440   
441          previousPatientName = currentPatientName;
442          if (verbose)   
443             std::cout << "==== new Patient  [" << currentPatientName  << "]" << std::endl;
444     
445          previousPatientName            = currentPatientName;
446          previousSerieInstanceUID       = ""; //currentSerieInstanceUID;
447          previousImagePosition          = ""; //currentImagePosition;
448          previousTriggerTime            = "";
449          previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
450   
451          currentPatientWriteDir = writeDir + currentPatientName;
452          //if ( ! gdcm::DirList::IsDirectory(currentPatientWriteDir) )
453            {
454               systemCommand   = "mkdir " + currentPatientWriteDir;
455               if (verbose)
456                  std::cout << systemCommand << std::endl;
457               system ( systemCommand.c_str() );
458          }
459       }
460
461       if (previousSerieInstanceUID != currentSerieInstanceUID)
462       {        
463          if (verbose)   
464             std::cout << "==== === new Serie [" << currentSerieInstanceUID << "]"
465                       << std::endl;
466       
467          if ( currentFile->GetEntryString(0x0020,0x000e) == gdcm::GDCM_UNFOUND)
468          {
469             if (verbose)   
470                std::cout << "--- --- new  Serie UID created" << std::endl;
471             defaultSerieUID =  gdcm::Util::CreateUniqueUID();
472             currentFile->InsertEntryString(defaultSerieUID, 0x0020, 0x000e, "UI" );
473          }       
474       
475          if (split)
476          {
477              currentSerieWriteDir  = currentPatientWriteDir + gdcm::GDCM_FILESEPARATOR
478                              + currentSerieInstanceUID;
479              systemCommand   = "mkdir " + currentSerieWriteDir;  
480              system (systemCommand.c_str());
481          }
482          previousSerieInstanceUID       = currentSerieInstanceUID;
483          previousImagePosition          = ""; //currentImagePosition;
484          previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
485       }
486
487       if (previousImagePosition != currentImagePosition)
488       {
489          frameIndex = 1;
490          flag = 0;        
491          if (verbose)   
492             std::cout << "=== === === new Position [" << currentImagePosition  << "]"
493                       << std::endl;
494          if (split)
495          {
496              currentPositionWriteDir  = currentSerieWriteDir + gdcm::GDCM_FILESEPARATOR
497                              + currentImagePosition;
498              systemCommand   = "mkdir " + currentPositionWriteDir;     
499              system (systemCommand.c_str()); 
500          }
501          previousImagePosition          = currentImagePosition;
502          previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
503          if (split)
504             sliceIndex = 1; // only *one* slice in a given directory
505          else
506             sliceIndex += 1;
507       }
508       if (verbose)
509          std::cout << "Slice Index : " << sliceIndex << std::endl;      
510
511 // We don't split on Row/Column!
512 /*
513       if (previousPhaseEncodingDirection != currentPhaseEncodingDirection)
514       {        
515          if (verbose)   
516             std::cout << "==== === === === new PhaseEncodingDirection [" 
517                       << currentPhaseEncodingDirection  << "]" << std::endl;
518       
519          if (split)
520          {
521              currentPhaseEncodingDirectionWriteDir  = currentPositionWriteDir 
522                              + gdcm::GDCM_FILESEPARATOR
523                              + currentPhaseEncodingDirection;
524              systemCommand   = "mkdir " + currentPhaseEncodingDirectionWriteDir;     
525              system (systemCommand.c_str());     
526          }      
527     
528          previousPhaseEncodingDirection = currentPhaseEncodingDirection;
529       } 
530 */    
531       
532       if (verbose)
533          std::cout << "--- --- --- --- --- " << (it2->second)->GetFileName() 
534                    << std::endl;
535    
536       if ( gdcm::Debug::GetDebugFlag())
537          std::cout << "--- --- --- --- --- " << it2->first << "  " 
538                    << (it2->second)->GetFileName() << " " 
539                    << gdcm::Util::GetName( fullFilename ) << std::endl;           
540       
541       // Transform the image to be 'Brucker-Like'
542       // ----------------------------------------   
543     
544       // Deal with 0x0019, 0x1000 : 'FOV'
545       int nX = currentFile->GetXSize();
546       int nY = currentFile->GetYSize();
547       float pxSzX = currentFile->GetXSpacing();
548       float pxSzY = currentFile->GetYSpacing();
549       char fov[64];
550       sprintf(fov, "%f\\%f",nX*pxSzX, nY*pxSzY);
551       if (currentFile->IsVRCoherent(0x0019) == 1 )
552          currentFile->InsertEntryString(fov, 0x0019, 0x1000, "  ");
553       else     
554          currentFile->InsertEntryString(fov, 0x0019, 0x1000, "DS");
555
556      
557       // Deal with 0x0020, 0x0012 : 'SESSION INDEX'  (Acquisition Number)
558       std::string chSessionIndex;
559       // CLEANME
560       if (taggrid) 
561          chSessionIndex = "1";
562       else
563       {
564          if (currentPhaseEncodingDirection == "COL" || currentPhaseEncodingDirection == "COL " || currentPhaseEncodingDirection == " COL")
565             chSessionIndex = "1";
566          else if (currentPhaseEncodingDirection == "ROW" || currentPhaseEncodingDirection == "ROW "|| currentPhaseEncodingDirection == " ROW")
567             chSessionIndex = "2"; 
568          else
569          {
570             std::cout << "====================== PhaseEncodingDirection "
571                       << " neither COL nor ROW (?!?) : [ "
572                       << currentPhaseEncodingDirection << "]" << std::endl;
573             chSessionIndex = "1";
574          }
575       }
576       if (currentFile->IsVRCoherent(0x0020) == 1 )     
577          currentFile->InsertEntryString(chSessionIndex, 0x0020, 0x0012, "  ");
578       else
579          currentFile->InsertEntryString(chSessionIndex, 0x0020, 0x0012, "IS");
580  
581       // Deal with  0x0021, 0x1020 : 'SLICE INDEX'
582       char chSliceIndex[5];
583       sprintf(chSliceIndex, "%04d", sliceIndex);
584       std::string strChSliceIndex(chSliceIndex);
585        
586       // Deal with  0x0021, 0x1040 : 'FRAME INDEX' 
587       char chFrameIndex[5];
588       sprintf(chFrameIndex, "%04d", frameIndex);
589
590       std::string stringVR;       
591       if (currentFile->IsVRCoherent(0x0021) == 1 )
592          stringVR = "  ";
593       else
594         stringVR = "IS";
595   
596       currentFile->InsertEntryString(strChSliceIndex, 0x0021, 0x1020, stringVR);
597       currentFile->InsertEntryString(chFrameIndex,    0x0021, 0x1040, stringVR); 
598  
599  
600       std::string strImagePositionPatient    = currentFile->GetEntryString(0x0020, 0x0032 );
601       if (strImagePositionPatient == gdcm::GDCM_UNFOUND)
602       {
603          if (verbose)
604             std::cout << "Duplicate ImagePosition into ImagePositionPatient" << std::endl;
605          currentFile->InsertEntryString(currentFile->GetEntryString(0x0020, 0x0030), 0x0020, 0x0032, "DS" );
606       }  
607       
608       std::string strImageOrientationPatient = f->GetEntryString(0x0020, 0x0037 );
609       if (strImageOrientationPatient == gdcm::GDCM_UNFOUND)
610       {
611          if (verbose)
612             std::cout << "Duplicate ImageOrientation into ImageOrientationPatient" << std::endl;      
613          currentFile->InsertEntryString(currentFile->GetEntryString(0x0020, 0x0035), 0x0020, 0x0037, "DS" );       
614       }
615                 
616       if (taggrid)
617          frameIndex++;
618       else     
619       {     
620          if (flag == 0)
621          {       
622             flag = 1;
623          }
624          else
625          {
626             frameIndex++;
627             flag = 0;
628          }
629       } 
630       if (verbose)
631          std::cout << "Frame Index : " << frameIndex << std::endl; 
632                  
633       if (split)
634       
635          //fullWriteFilename = currentPhaseEncodingDirectionWriteDir + gdcm::GDCM_FILESEPARATOR 
636          //                                + lastFilename + strExtent;      
637          fullWriteFilename = currentPositionWriteDir + gdcm::GDCM_FILESEPARATOR 
638                                          + lastFilename + strExtent; 
639       else
640          fullWriteFilename = currentPatientWriteDir + gdcm::GDCM_FILESEPARATOR 
641                                          + lastFilename + strExtent; 
642       
643       /*           
644       systemCommand  = "cp " + fullFilename + " " + fullWriteFilename;
645       std::cout << systemCommand << std::endl;
646       system (  systemCommand.c_str() );
647       */
648             
649       // Load the pixels in RAM.    
650       
651       fh = gdcm::FileHelper::New(currentFile);     
652       uint8_t *imageData = fh->GetImageDataRaw(); // Don't convert (Gray Pixels + LUT) into (RGB pixels) ?!?
653       fh->SetWriteTypeToDcmExplVR();
654       // We didn't modify pixels -> keep unchanged the following :
655       // 'Media Storage SOP Class UID' (0x0002,0x0002)
656       // 'SOP Class UID'               (0x0008,0x0016)
657       // 'Image Type'                  (0x0008,0x0008)
658       // 'Conversion Type'             (0x0008,0x0064)
659       
660       // Put to Black the burnt-in number.
661       nX = currentFile->GetXSize();
662       nY = currentFile->GetYSize();
663       for(int y=nY-15; y<nY; y++)
664          for(int x=nX/3; x<nX/2+50; x++)
665            imageData[ y*nX*2 + x ] = 0;   
666         
667       fh->SetContentType(gdcm::UNMODIFIED_PIXELS_IMAGE);
668       if (!fh->Write(fullWriteFilename))
669       {
670          std::cout << "Fail to write :[" << fullWriteFilename << "]"
671                    << std::endl;
672       } 
673       fh->Delete();                
674    }
675    return 0;
676  }